3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00
ergo/src/irc/reply.go

198 lines
5.5 KiB
Go
Raw Normal View History

package irc
import (
"fmt"
2012-12-09 21:51:50 +01:00
"strings"
"time"
)
type Identifier interface {
Id() string
}
type Reply interface {
String(client *Client) string
}
type BasicReply struct {
source Identifier
code string
message string
}
2012-12-13 08:27:17 +01:00
func NewBasicReply(source Identifier, code string, message string) *BasicReply {
fullMessage := fmt.Sprintf(":%s %s %s\r\n", source.Id(), code, message)
return &BasicReply{source, code, fullMessage}
}
func (reply *BasicReply) String(client *Client) string {
2012-12-13 08:27:17 +01:00
return reply.message
}
2012-12-13 08:27:17 +01:00
type NumericReply struct {
*BasicReply
}
2012-12-13 08:27:17 +01:00
func NewNumericReply(source Identifier, code string, message string) *NumericReply {
return &NumericReply{&BasicReply{source, code, message}}
}
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)
}
// messaging
func RplPrivMsg(source *Client, message string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, RPL_PRIVMSG, ":"+message)
}
func RplNick(client *Client, newNick string) Reply {
2012-12-13 08:27:17 +01:00
return NewBasicReply(client, RPL_NICK, newNick)
}
2012-12-13 08:27:17 +01:00
func RplPrivMsgChannel(channel *Channel, source *Client, message string) Reply {
return NewBasicReply(source, RPL_PRIVMSG, fmt.Sprintf("%s :%s", channel.name, message))
2012-12-09 22:47:02 +01:00
}
2012-12-13 08:27:17 +01:00
func RplJoin(channel *Channel, client *Client) Reply {
return NewBasicReply(client, RPL_JOIN, channel.name)
}
func RplPart(channel *Channel, client *Client, message string) Reply {
return NewBasicReply(client, RPL_PART, fmt.Sprintf("%s :%s", channel.name, message))
}
func RplPong(server *Server) Reply {
return NewBasicReply(server, RPL_PONG, server.Id())
}
func RplQuit(client *Client, message string) Reply {
return NewBasicReply(client, RPL_QUIT, ":"+message)
2012-12-09 22:47:02 +01:00
}
// Server Info
func RplWelcome(source Identifier, client *Client) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, RPL_WELCOME,
2012-12-09 22:47:02 +01:00
"Welcome to the Internet Relay Network "+client.Id())
}
func RplYourHost(server *Server, target *Client) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(server, RPL_YOURHOST,
2012-12-09 22:47:02 +01:00
fmt.Sprintf("Your host is %s, running version %s", server.hostname, VERSION))
}
func RplCreated(server *Server) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(server, RPL_CREATED,
2012-12-09 22:47:02 +01:00
"This server was created "+server.ctime.Format(time.RFC1123))
}
func RplMyInfo(server *Server) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(server, RPL_MYINFO,
fmt.Sprintf("%s %s w kn", server.name, VERSION))
}
func RplUModeIs(server *Server, client *Client) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(server, RPL_UMODEIS, client.UModeString())
}
2012-12-13 08:27:17 +01:00
// numeric replies
2012-12-13 08:27:17 +01:00
func RplNoTopic(channel *Channel) Reply {
return NewNumericReply(channel.server, RPL_NOTOPIC, channel.name+" :No topic is set")
2012-12-10 00:20:21 +01:00
}
2012-12-13 08:27:17 +01:00
func RplTopic(channel *Channel) Reply {
return NewNumericReply(channel.server, RPL_TOPIC, fmt.Sprintf("%s :%s", channel.name, channel.topic))
2012-12-10 00:20:21 +01:00
}
2012-12-13 08:27:17 +01:00
func RplInviteMsg(channel *Channel, inviter *Client) Reply {
return NewNumericReply(inviter, RPL_INVITE, channel.name)
}
2012-12-13 08:27:17 +01:00
func RplInvitingMsg(channel *Channel, invitee *Client) Reply {
return NewNumericReply(channel.server, RPL_INVITING,
fmt.Sprintf("%s %s", channel.name, invitee.Nick()))
}
2012-12-09 21:51:50 +01:00
func RplNamReply(channel *Channel) Reply {
// 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-09 22:47:02 +01:00
fmt.Sprintf("= %s :%s", channel.name, strings.Join(channel.Nicks(), " ")))
}
func RplEndOfNames(source Identifier) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, RPL_ENDOFNAMES, ":End of NAMES list")
}
2012-12-10 05:24:53 +01:00
func RplYoureOper(server *Server) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(server, RPL_YOUREOPER, ":You are now an IRC operator")
2012-12-10 05:24:53 +01:00
}
// errors
func ErrAlreadyRegistered(source Identifier) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_ALREADYREGISTRED, ":You may not reregister")
}
func ErrNickNameInUse(source Identifier, nick string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_NICKNAMEINUSE,
2012-12-09 22:47:02 +01:00
nick+" :Nickname is already in use")
}
func ErrUnknownCommand(source Identifier, command string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_UNKNOWNCOMMAND,
2012-12-09 22:47:02 +01:00
command+" :Unknown command")
}
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")
}
func ErrNeedMoreParams(source Identifier, command string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_NEEDMOREPARAMS,
2012-12-09 22:47:02 +01:00
command+"%s :Not enough parameters")
}
func ErrNoSuchChannel(source Identifier, channel string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_NOSUCHCHANNEL,
2012-12-09 22:47:02 +01:00
channel+" :No such channel")
}
func ErrUserOnChannel(channel *Channel, member *Client) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(channel.server, ERR_USERONCHANNEL,
2012-12-09 22:47:02 +01:00
fmt.Sprintf("%s %s :is already on channel", member.nick, channel.name))
}
func ErrNotOnChannel(channel *Channel) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(channel.server, ERR_NOTONCHANNEL,
2012-12-09 22:47:02 +01:00
channel.name+" :You're not on that channel")
}
func ErrInviteOnlyChannel(channel *Channel) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(channel.server, ERR_INVITEONLYCHAN,
2012-12-09 22:47:02 +01:00
channel.name+" :Cannot join channel (+i)")
}
func ErrBadChannelKey(channel *Channel) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(channel.server, ERR_BADCHANNELKEY,
2012-12-09 22:47:02 +01:00
channel.name+" :Cannot join channel (+k)")
}
func ErrNoSuchNick(source Identifier, nick string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_NOSUCHNICK,
2012-12-09 22:47:02 +01:00
nick+" :No such nick/channel")
}
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
}
func ErrNoChanModes(channel *Channel) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(channel.server, ERR_NOCHANMODES,
channel.name+" :Channel doesn't support modes")
}