2012-12-09 07:54:58 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2012-12-09 21:51:50 +01:00
|
|
|
"strings"
|
2012-12-09 07:54:58 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Identifier interface {
|
|
|
|
Id() string
|
2012-12-15 23:34:20 +01:00
|
|
|
PublicId() string
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Reply interface {
|
|
|
|
String(client *Client) string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BasicReply struct {
|
|
|
|
source Identifier
|
|
|
|
code string
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func NewBasicReply(source Identifier, code string, format string, args ...interface{}) *BasicReply {
|
|
|
|
message := fmt.Sprintf(format, args...)
|
2012-12-13 08:27:17 +01:00
|
|
|
fullMessage := fmt.Sprintf(":%s %s %s\r\n", source.Id(), code, message)
|
|
|
|
return &BasicReply{source, code, fullMessage}
|
|
|
|
}
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
func (reply *BasicReply) String(client *Client) string {
|
2012-12-13 08:27:17 +01:00
|
|
|
return reply.message
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
type NumericReply struct {
|
2012-12-09 07:54:58 +01:00
|
|
|
*BasicReply
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func NewNumericReply(source Identifier, code string, format string, args ...interface{}) *NumericReply {
|
|
|
|
return &NumericReply{&BasicReply{source, code, fmt.Sprintf(format, args...)}}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
func (reply *NumericReply) String(client *Client) string {
|
|
|
|
return fmt.Sprintf(":%s %s %s %s\r\n", reply.source.Id(), reply.code, client.Nick(),
|
|
|
|
reply.message)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:33:09 +01:00
|
|
|
// messaging replies
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func RplPrivMsg(source Identifier, target Identifier, message string) Reply {
|
|
|
|
return NewBasicReply(source, RPL_PRIVMSG, "%s :%s", target, message)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplNick(client *Client, newNick string) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewBasicReply(client, RPL_NICK, newNick)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func RplPrivMsgChannel(channel *Channel, source Identifier, message string) Reply {
|
|
|
|
return NewBasicReply(source, RPL_PRIVMSG, "%s :%s", channel.name, message)
|
2012-12-09 22:47:02 +01:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func RplJoin(channel *Channel, user *User) Reply {
|
|
|
|
return NewBasicReply(user, RPL_JOIN, channel.name)
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func RplPart(channel *Channel, user *User, message string) Reply {
|
|
|
|
return NewBasicReply(user, RPL_PART, "%s :%s", channel.name, message)
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplPong(server *Server) Reply {
|
|
|
|
return NewBasicReply(server, RPL_PONG, server.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
func RplQuit(client *Client, message string) Reply {
|
2012-12-15 23:34:20 +01:00
|
|
|
return NewBasicReply(client, RPL_QUIT, ":%", message)
|
2012-12-09 22:47:02 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:33:09 +01:00
|
|
|
func RplInviteMsg(channel *Channel, inviter *Client) Reply {
|
|
|
|
return NewBasicReply(inviter, RPL_INVITE, channel.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// numeric replies
|
2012-12-09 07:54:58 +01:00
|
|
|
|
|
|
|
func RplWelcome(source Identifier, client *Client) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(source, RPL_WELCOME,
|
2012-12-15 23:34:20 +01:00
|
|
|
"Welcome to the Internet Relay Network %s", client.Id())
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplYourHost(server *Server, target *Client) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(server, RPL_YOURHOST,
|
2012-12-15 23:34:20 +01:00
|
|
|
"Your host is %s, running version %s", server.hostname, VERSION)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplCreated(server *Server) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(server, RPL_CREATED,
|
2012-12-15 23:34:20 +01:00
|
|
|
"This server was created %s", server.ctime.Format(time.RFC1123))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplMyInfo(server *Server) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(server, RPL_MYINFO,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s %s a kn", server.name, VERSION)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplUModeIs(server *Server, client *Client) Reply {
|
2012-12-15 23:34:20 +01:00
|
|
|
return NewNumericReply(server, RPL_UMODEIS,
|
|
|
|
client.UModeString())
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
func RplNoTopic(channel *Channel) Reply {
|
2012-12-15 23:34:20 +01:00
|
|
|
return NewNumericReply(channel.server, RPL_NOTOPIC,
|
|
|
|
"%s :No topic is set", channel.name)
|
2012-12-10 00:20:21 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
func RplTopic(channel *Channel) Reply {
|
2012-12-13 08:34:32 +01:00
|
|
|
return NewNumericReply(channel.server, RPL_TOPIC,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :%s", channel.name, channel.topic)
|
2012-12-10 00:20:21 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:27:17 +01:00
|
|
|
func RplInvitingMsg(channel *Channel, invitee *Client) Reply {
|
|
|
|
return NewNumericReply(channel.server, RPL_INVITING,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s %s", channel.name, invitee.Nick())
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
func RplNamReply(channel *Channel) Reply {
|
2012-12-09 07:54:58 +01:00
|
|
|
// TODO multiple names and splitting based on message size
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(channel.server, RPL_NAMREPLY,
|
2012-12-15 23:34:20 +01:00
|
|
|
"= %s :%s", channel.name, strings.Join(channel.Nicks(), " "))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplEndOfNames(source Identifier) Reply {
|
2012-12-15 23:34:20 +01:00
|
|
|
return NewNumericReply(source, RPL_ENDOFNAMES,
|
|
|
|
":End of NAMES list")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-10 05:24:53 +01:00
|
|
|
func RplYoureOper(server *Server) Reply {
|
2012-12-15 23:34:20 +01:00
|
|
|
return NewNumericReply(server, RPL_YOUREOPER,
|
|
|
|
":You are now an IRC operator")
|
2012-12-10 05:24:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 08:33:09 +01:00
|
|
|
// errors (also numeric)
|
2012-12-09 07:54:58 +01:00
|
|
|
|
|
|
|
func ErrAlreadyRegistered(source Identifier) Reply {
|
2012-12-15 23:34:20 +01:00
|
|
|
return NewNumericReply(source, ERR_ALREADYREGISTRED,
|
|
|
|
":You may not reregister")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNickNameInUse(source Identifier, nick string) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(source, ERR_NICKNAMEINUSE,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :Nickname is already in use", nick)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrUnknownCommand(source Identifier, command string) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(source, ERR_UNKNOWNCOMMAND,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :Unknown command", command)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrUsersDontMatch(source Identifier) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(source, ERR_USERSDONTMATCH,
|
2012-12-09 22:47:02 +01:00
|
|
|
":Cannot change mode for other users")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNeedMoreParams(source Identifier, command string) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(source, ERR_NEEDMOREPARAMS,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :Not enough parameters", command)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNoSuchChannel(source Identifier, channel string) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(source, ERR_NOSUCHCHANNEL,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :No such channel", channel)
|
2012-12-09 22:47:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrUserOnChannel(channel *Channel, member *Client) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(channel.server, ERR_USERONCHANNEL,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s %s :is already on channel", member.nick, channel.name)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNotOnChannel(channel *Channel) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(channel.server, ERR_NOTONCHANNEL,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :You're not on that channel", channel.name)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrInviteOnlyChannel(channel *Channel) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(channel.server, ERR_INVITEONLYCHAN,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :Cannot join channel (+i)", channel.name)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrBadChannelKey(channel *Channel) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(channel.server, ERR_BADCHANNELKEY,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :Cannot join channel (+k)", channel.name)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNoSuchNick(source Identifier, nick string) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(source, ERR_NOSUCHNICK,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :No such nick/channel", nick)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2012-12-10 05:24:53 +01:00
|
|
|
|
|
|
|
func ErrPasswdMismatch(server *Server) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(server, ERR_PASSWDMISMATCH, ":Password incorrect")
|
2012-12-10 05:24:53 +01:00
|
|
|
}
|
2012-12-12 08:04:03 +01:00
|
|
|
|
|
|
|
func ErrNoChanModes(channel *Channel) Reply {
|
2012-12-13 08:27:17 +01:00
|
|
|
return NewNumericReply(channel.server, ERR_NOCHANMODES,
|
2012-12-15 23:34:20 +01:00
|
|
|
"%s :Channel doesn't support modes", channel.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNoPrivileges(server *Server) Reply {
|
|
|
|
return NewNumericReply(server, ERR_NOPRIVILEGES, ":Permission Denied")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrRestricted(server *Server) Reply {
|
|
|
|
return NewNumericReply(server, ERR_RESTRICTED, ":Your connection is restricted!")
|
2012-12-12 08:04:03 +01:00
|
|
|
}
|