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
|
|
|
|
}
|
|
|
|
|
|
|
|
type Reply interface {
|
|
|
|
String(client *Client) string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BasicReply struct {
|
|
|
|
source Identifier
|
|
|
|
code string
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reply *BasicReply) String(client *Client) string {
|
2012-12-12 07:34:22 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelReply struct {
|
|
|
|
*BasicReply
|
|
|
|
channel *Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reply *ChannelReply) String(client *Client) string {
|
2012-12-12 07:34:22 +01:00
|
|
|
return fmt.Sprintf(":%s %s %s %s\r\n", reply.source.Id(), reply.code, reply.channel.name,
|
|
|
|
reply.message)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewReply(source Identifier, code string, message string) *BasicReply {
|
|
|
|
return &BasicReply{source, code, message}
|
|
|
|
}
|
|
|
|
|
|
|
|
// messaging
|
|
|
|
|
|
|
|
func RplPrivMsg(source *Client, message string) Reply {
|
|
|
|
return NewReply(source, RPL_PRIVMSG, ":"+message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RplNick(client *Client, newNick string) Reply {
|
|
|
|
return NewReply(client, RPL_NICK, ":"+newNick)
|
|
|
|
}
|
|
|
|
|
2012-12-09 22:47:02 +01:00
|
|
|
func RplInviteMsg(channel *Channel, inviter *Client) Reply {
|
|
|
|
return NewReply(inviter, RPL_INVITE, channel.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RplInvitingMsg(channel *Channel, invitee *Client) Reply {
|
|
|
|
return NewReply(channel.server, RPL_INVITING,
|
|
|
|
fmt.Sprintf("%s %s", channel.name, invitee.Nick()))
|
|
|
|
}
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
// Server Info
|
|
|
|
|
|
|
|
func RplWelcome(source Identifier, client *Client) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(source, RPL_WELCOME,
|
|
|
|
"Welcome to the Internet Relay Network "+client.Id())
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplYourHost(server *Server, target *Client) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(server, RPL_YOURHOST,
|
|
|
|
fmt.Sprintf("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-09 22:47:02 +01:00
|
|
|
return NewReply(server, RPL_CREATED,
|
|
|
|
"This server was created "+server.ctime.Format(time.RFC1123))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplMyInfo(server *Server) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(server, RPL_MYINFO,
|
2012-12-10 05:24:53 +01:00
|
|
|
fmt.Sprintf("%s %s iwroO ik", server.name, VERSION))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplUModeIs(server *Server, client *Client) Reply {
|
|
|
|
return NewReply(server, RPL_UMODEIS, client.UModeString())
|
|
|
|
}
|
|
|
|
|
|
|
|
// channel operations
|
|
|
|
|
2012-12-10 00:20:21 +01:00
|
|
|
func RplPrivMsgChannel(channel *Channel, source *Client, message string) Reply {
|
|
|
|
return &ChannelReply{NewReply(source, RPL_PRIVMSG, ":"+message), channel}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RplJoin(channel *Channel, client *Client) Reply {
|
|
|
|
return &ChannelReply{NewReply(client, RPL_JOIN, ""), channel}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RplPart(channel *Channel, client *Client, message string) Reply {
|
|
|
|
return &ChannelReply{NewReply(client, RPL_PART, ":"+message), channel}
|
|
|
|
}
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
func RplNoTopic(channel *Channel) Reply {
|
2012-12-12 07:34:22 +01:00
|
|
|
return NewReply(channel.server, RPL_NOTOPIC, channel.name+" :No topic is set")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplTopic(channel *Channel) Reply {
|
2012-12-12 07:34:22 +01:00
|
|
|
return NewReply(channel.server, RPL_TOPIC, fmt.Sprintf("%s :%s", channel.name, channel.topic))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-10 00:20:21 +01:00
|
|
|
// server info
|
|
|
|
|
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-09 22:47:02 +01:00
|
|
|
return NewReply(channel.server, RPL_NAMREPLY,
|
|
|
|
fmt.Sprintf("= %s :%s", channel.name, strings.Join(channel.Nicks(), " ")))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplEndOfNames(source Identifier) Reply {
|
2012-12-10 00:20:21 +01:00
|
|
|
return NewReply(source, RPL_ENDOFNAMES, ":End of NAMES list")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RplPong(server *Server) Reply {
|
2012-12-12 07:34:22 +01:00
|
|
|
return NewReply(server, RPL_PONG, server.Id())
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-10 05:24:53 +01:00
|
|
|
// server functions
|
|
|
|
|
|
|
|
func RplYoureOper(server *Server) Reply {
|
|
|
|
return NewReply(server, RPL_YOUREOPER, ":You are now an IRC operator")
|
|
|
|
}
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
// errors
|
|
|
|
|
|
|
|
func ErrAlreadyRegistered(source Identifier) Reply {
|
2012-12-10 00:20:21 +01:00
|
|
|
return NewReply(source, ERR_ALREADYREGISTRED, ":You may not reregister")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNickNameInUse(source Identifier, nick string) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(source, ERR_NICKNAMEINUSE,
|
|
|
|
nick+" :Nickname is already in use")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrUnknownCommand(source Identifier, command string) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(source, ERR_UNKNOWNCOMMAND,
|
|
|
|
command+" :Unknown command")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrUsersDontMatch(source Identifier) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(source, ERR_USERSDONTMATCH,
|
|
|
|
":Cannot change mode for other users")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNeedMoreParams(source Identifier, command string) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(source, ERR_NEEDMOREPARAMS,
|
|
|
|
command+"%s :Not enough parameters")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNoSuchChannel(source Identifier, channel string) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(source, ERR_NOSUCHCHANNEL,
|
|
|
|
channel+" :No such channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrUserOnChannel(channel *Channel, member *Client) Reply {
|
|
|
|
return NewReply(channel.server, ERR_USERONCHANNEL,
|
|
|
|
fmt.Sprintf("%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-09 22:47:02 +01:00
|
|
|
return NewReply(channel.server, ERR_NOTONCHANNEL,
|
|
|
|
channel.name+" :You're not on that channel")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrInviteOnlyChannel(channel *Channel) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(channel.server, ERR_INVITEONLYCHAN,
|
|
|
|
channel.name+" :Cannot join channel (+i)")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrBadChannelKey(channel *Channel) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(channel.server, ERR_BADCHANNELKEY,
|
|
|
|
channel.name+" :Cannot join channel (+k)")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ErrNoSuchNick(source Identifier, nick string) Reply {
|
2012-12-09 22:47:02 +01:00
|
|
|
return NewReply(source, ERR_NOSUCHNICK,
|
|
|
|
nick+" :No such nick/channel")
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2012-12-10 05:24:53 +01:00
|
|
|
|
|
|
|
func ErrPasswdMismatch(server *Server) Reply {
|
|
|
|
return NewReply(server, ERR_PASSWDMISMATCH, ":Password incorrect")
|
|
|
|
}
|