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

212 lines
5.8 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
PublicId() string
}
type Reply interface {
String(client *Client) string
}
type BasicReply struct {
source Identifier
code string
message string
}
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}
}
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
}
func NewNumericReply(source Identifier, code string, format string, args ...interface{}) *NumericReply {
return &NumericReply{&BasicReply{source, code, fmt.Sprintf(format, args...)}}
}
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-13 08:33:09 +01:00
// messaging replies
func RplPrivMsg(source Identifier, target Identifier, message string) Reply {
return NewBasicReply(source, RPL_PRIVMSG, "%s :%s", target, message)
}
func RplNick(client *Client, newNick string) Reply {
2012-12-13 08:27:17 +01:00
return NewBasicReply(client, RPL_NICK, newNick)
}
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
}
func RplJoin(channel *Channel, user *User) Reply {
return NewBasicReply(user, RPL_JOIN, channel.name)
2012-12-13 08:27:17 +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 {
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
func RplWelcome(source Identifier, client *Client) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, RPL_WELCOME,
"Welcome to the Internet Relay Network %s", client.Id())
}
func RplYourHost(server *Server, target *Client) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(server, RPL_YOURHOST,
"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,
"This server was created %s", server.ctime.Format(time.RFC1123))
}
func RplMyInfo(server *Server) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(server, RPL_MYINFO,
"%s %s a kn", server.name, VERSION)
}
func RplUModeIs(server *Server, client *Client) Reply {
return NewNumericReply(server, RPL_UMODEIS,
client.UModeString())
}
2012-12-13 08:27:17 +01:00
func RplNoTopic(channel *Channel) Reply {
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,
"%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,
"%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,
"= %s :%s", channel.name, strings.Join(channel.Nicks(), " "))
}
func RplEndOfNames(source Identifier) Reply {
return NewNumericReply(source, RPL_ENDOFNAMES,
":End of NAMES list")
}
2012-12-10 05:24:53 +01:00
func RplYoureOper(server *Server) Reply {
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)
func ErrAlreadyRegistered(source Identifier) Reply {
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,
"%s :Nickname is already in use", nick)
}
func ErrUnknownCommand(source Identifier, command string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_UNKNOWNCOMMAND,
"%s :Unknown command", 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,
"%s :Not enough parameters", command)
}
func ErrNoSuchChannel(source Identifier, channel string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_NOSUCHCHANNEL,
"%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,
"%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,
"%s :You're not on that channel", channel.name)
}
func ErrInviteOnlyChannel(channel *Channel) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(channel.server, ERR_INVITEONLYCHAN,
"%s :Cannot join channel (+i)", channel.name)
}
func ErrBadChannelKey(channel *Channel) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(channel.server, ERR_BADCHANNELKEY,
"%s :Cannot join channel (+k)", channel.name)
}
func ErrNoSuchNick(source Identifier, nick string) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_NOSUCHNICK,
"%s :No such nick/channel", nick)
}
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,
"%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!")
}