3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-14 16:09:32 +01:00
ergo/irc/reply.go

538 lines
14 KiB
Go
Raw Normal View History

package irc
import (
"fmt"
2012-12-09 21:51:50 +01:00
"strings"
"time"
)
type BaseReply struct {
code ReplyCode
id string
message string
source Identifier
}
func (reply *BaseReply) Code() ReplyCode {
return reply.code
}
func (reply *BaseReply) SetSource(source Identifier) {
reply.id = source.Id()
reply.source = source
}
func (reply *BaseReply) Source() Identifier {
return reply.source
2012-12-13 08:27:17 +01:00
}
type StringReply struct {
BaseReply
}
func NewStringReply(source Identifier, code StringCode,
format string, args ...interface{}) *StringReply {
reply := &StringReply{}
reply.code = code
reply.message = fmt.Sprintf(format, args...)
reply.SetSource(source)
return reply
}
func (reply *StringReply) Format(client *Client) []string {
2014-02-15 04:25:14 +01:00
message := fmt.Sprintf(":%s %s %s",
reply.id, reply.code, reply.message)
return []string{message}
}
func (reply *StringReply) String() string {
return fmt.Sprintf("Reply(source=%s, code=%s, message=%s)",
reply.id, reply.code, reply.message)
}
2012-12-13 08:27:17 +01:00
type NumericReply struct {
BaseReply
}
func NewNumericReply(source Identifier, code NumericCode, format string,
2013-05-12 20:20:55 +02:00
args ...interface{}) *NumericReply {
reply := &NumericReply{}
reply.code = code
reply.message = fmt.Sprintf(format, args...)
reply.SetSource(source)
return reply
}
func (reply *NumericReply) Format(client *Client) []string {
2014-02-15 04:25:14 +01:00
message := fmt.Sprintf(":%s %s %s %s",
reply.id, reply.code, client.Nick(), reply.message)
return []string{message}
}
func (reply *NumericReply) String() string {
return fmt.Sprintf("Reply(source=%s, code=%d, message=%s)",
reply.id, reply.code, reply.message)
}
2014-02-18 05:47:41 +01:00
//
// multiline replies
//
2013-06-03 07:07:50 +02:00
2014-02-18 05:47:41 +01:00
type MultilineReply interface {
formatLine(*Client, []string) string
names() []string
2013-06-03 07:07:50 +02:00
}
2014-02-18 05:47:41 +01:00
func joinedLen(names []string) int {
var l = len(names) - 1 // " " between names
for _, name := range names {
l += len(name)
2013-06-03 07:07:50 +02:00
}
2014-02-18 05:47:41 +01:00
return l
2013-06-03 07:07:50 +02:00
}
2014-02-18 05:47:41 +01:00
func multilineFormat(reply MultilineReply, client *Client) []string {
lines := make([]string, 0)
2014-02-18 05:47:41 +01:00
baseLen := len(reply.formatLine(client, []string{}))
2013-06-03 07:07:50 +02:00
tooLong := func(names []string) bool {
return (baseLen + joinedLen(names)) > MAX_REPLY_LEN
}
2014-02-08 22:18:11 +01:00
from, to := 0, 1
2014-02-18 05:47:41 +01:00
names := reply.names()
for to < len(names) {
if (from < (to - 1)) && tooLong(names[from:to]) {
lines = append(lines, reply.formatLine(client, names[from:to-1]))
2014-02-08 22:18:11 +01:00
from, to = to-1, to
} else {
to += 1
2013-06-03 07:07:50 +02:00
}
}
2014-02-18 05:47:41 +01:00
if from < len(names) {
lines = append(lines, reply.formatLine(client, names[from:]))
2013-06-03 07:07:50 +02:00
}
2014-02-18 05:47:41 +01:00
return lines
}
// names
type NamesReply struct {
BaseReply
channel *Channel
}
func NewNamesReply(channel *Channel) Reply {
reply := &NamesReply{
channel: channel,
}
reply.SetSource(channel.server)
return reply
}
func (reply *NamesReply) names() []string {
return reply.channel.Nicks()
}
func (reply *NamesReply) formatLine(client *Client, names []string) string {
return RplNamReply(reply.channel, names).Format(client)[0]
}
func (reply *NamesReply) Format(client *Client) []string {
lines := multilineFormat(reply, client)
lines = append(lines, RplEndOfNames(reply.channel).Format(client)...)
return lines
2013-06-03 07:07:50 +02:00
}
2014-02-08 22:18:11 +01:00
func (reply *NamesReply) String() string {
return fmt.Sprintf("NamesReply(channel=%s, names=%s)",
reply.channel, reply.channel.Nicks())
}
2014-02-18 05:47:41 +01:00
// whois channels
type WhoisChannelsReply struct {
BaseReply
client *Client
}
func NewWhoisChannelsReply(client *Client) *WhoisChannelsReply {
reply := &WhoisChannelsReply{
client: client,
}
reply.SetSource(client.server)
return reply
}
func (reply *WhoisChannelsReply) names() []string {
chstrs := make([]string, len(reply.client.channels))
index := 0
for channel := range reply.client.channels {
switch {
case channel.members[reply.client][ChannelOperator]:
chstrs[index] = "@" + channel.name
case channel.members[reply.client][Voice]:
chstrs[index] = "+" + channel.name
default:
chstrs[index] = channel.name
}
index += 1
}
return chstrs
}
func (reply *WhoisChannelsReply) formatLine(client *Client, names []string) string {
return RplWhoisChannels(reply.client, names).Format(client)[0]
}
func (reply *WhoisChannelsReply) Format(client *Client) []string {
return multilineFormat(reply, client)
}
//
2012-12-13 08:33:09 +01:00
// messaging replies
2014-02-18 05:47:41 +01:00
//
func RplPrivMsg(source Identifier, target Identifier, message string) Reply {
return NewStringReply(source, PRIVMSG, "%s :%s", target.Nick(), message)
}
2014-02-12 02:11:59 +01:00
func RplNotice(source Identifier, target Identifier, message string) Reply {
return NewStringReply(source, NOTICE, "%s :%s", target.Nick(), message)
2014-02-12 02:11:59 +01:00
}
2013-05-12 20:20:55 +02:00
func RplNick(source Identifier, newNick string) Reply {
return NewStringReply(source, NICK, newNick)
}
2014-02-09 02:53:06 +01:00
func RplJoin(client *Client, channel *Channel) Reply {
return NewStringReply(client, JOIN, channel.name)
2012-12-13 08:27:17 +01:00
}
2014-02-09 02:53:06 +01:00
func RplPart(client *Client, channel *Channel, message string) Reply {
return NewStringReply(client, PART, "%s :%s", channel, message)
}
func RplMode(client *Client, changes ModeChanges) Reply {
return NewStringReply(client, MODE, "%s :%s", client.Nick(), changes)
}
func RplChannelMode(client *Client, channel *Channel,
changes ChannelModeChanges) Reply {
return NewStringReply(client, MODE, "%s %s", channel, changes)
}
func RplTopicMsg(source Identifier, channel *Channel) Reply {
return NewStringReply(source, TOPIC, "%s :%s", channel, channel.topic)
2012-12-13 08:27:17 +01:00
}
2014-02-09 21:13:09 +01:00
func RplPing(server *Server, target Identifier) Reply {
return NewStringReply(server, PING, target.Nick())
2014-02-09 21:13:09 +01:00
}
func RplPong(server *Server, client *Client) Reply {
return NewStringReply(server, PONG, client.Nick())
2012-12-13 08:27:17 +01:00
}
func RplQuit(client *Client, message string) Reply {
return NewStringReply(client, QUIT, ":%s", message)
2012-12-09 22:47:02 +01:00
}
2014-02-18 22:25:21 +01:00
func RplError(server *Server, message string) Reply {
return NewStringReply(server, ERROR, message)
}
2012-12-13 08:33:09 +01:00
func RplInviteMsg(channel *Channel, inviter *Client) Reply {
return NewStringReply(inviter, INVITE, channel.name)
2012-12-13 08:33:09 +01:00
}
2014-02-17 08:29:11 +01:00
func RplKick(channel *Channel, client *Client, target *Client, comment string) Reply {
return NewStringReply(client, KICK, "%s %s :%s",
channel, target.Nick(), comment)
}
2012-12-13 08:33:09 +01:00
// 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())
}
2013-06-03 07:07:50 +02:00
func RplYourHost(server *Server) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(server, RPL_YOURHOST,
":Your host is %s, running version %s", server.name, 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,
2014-02-09 08:15:05 +01:00
"%s %s aiOorsw abeIikmntpqrsl", server.name, VERSION)
}
func RplUModeIs(server *Server, client *Client) Reply {
2014-02-09 17:53:06 +01:00
return NewNumericReply(server, RPL_UMODEIS, client.ModeString())
}
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
}
// <nick> <channel>
// NB: correction in errata
2012-12-13 08:27:17 +01:00
func RplInvitingMsg(channel *Channel, invitee *Client) Reply {
return NewNumericReply(channel.server, RPL_INVITING,
"%s %s", invitee.Nick(), channel.name)
}
2013-06-03 07:07:50 +02:00
func RplNamReply(channel *Channel, names []string) *NumericReply {
return NewNumericReply(channel.server, RPL_NAMREPLY, "= %s :%s",
channel.name, strings.Join(names, " "))
}
2014-02-08 22:18:11 +01:00
func RplEndOfNames(channel *Channel) Reply {
2014-02-15 04:38:01 +01:00
return NewNumericReply(channel.server, RPL_ENDOFNAMES,
2014-02-08 22:18:11 +01:00
"%s :End of NAMES list", channel.name)
}
2014-02-09 19:07:40 +01:00
// :You are now an IRC operator
2012-12-10 05:24:53 +01:00
func RplYoureOper(server *Server) Reply {
2014-02-09 19:07:40 +01:00
return NewNumericReply(server, RPL_YOUREOPER, ":You are now an IRC operator")
2012-12-10 05:24:53 +01:00
}
2014-02-18 04:08:57 +01:00
func RplWhoisUser(client *Client) Reply {
return NewNumericReply(client.server, RPL_WHOISUSER, "%s %s %s * :%s",
client.Nick(), client.username, client.hostname, client.realname)
2014-02-09 02:43:59 +01:00
}
2014-02-18 04:08:57 +01:00
func RplWhoisOperator(client *Client) Reply {
return NewNumericReply(client.server, RPL_WHOISOPERATOR,
"%s :is an IRC operator", client.Nick())
}
func RplWhoisIdle(client *Client) Reply {
return NewNumericReply(client.server, RPL_WHOISIDLE,
2014-02-18 04:56:06 +01:00
"%s %d %d :seconds idle, signon time",
client.Nick(), client.IdleSeconds(), client.SignonTime())
2014-02-18 04:08:57 +01:00
}
2014-02-18 05:47:41 +01:00
func RplWhoisChannels(client *Client, chstrs []string) Reply {
2014-02-18 04:08:57 +01:00
return NewNumericReply(client.server, RPL_WHOISCHANNELS,
"%s :%s", client.Nick(), strings.Join(chstrs, " "))
}
2014-02-09 02:43:59 +01:00
func RplEndOfWhois(server *Server) Reply {
return NewNumericReply(server, RPL_ENDOFWHOIS, ":End of WHOIS list")
}
func RplChannelModeIs(channel *Channel) Reply {
return NewNumericReply(channel.server, RPL_CHANNELMODEIS, "%s %s",
channel, channel.ModeString())
2014-02-09 03:14:39 +01:00
}
2014-02-09 03:49:52 +01:00
// <channel> <user> <host> <server> <nick> ( "H" / "G" ) ["*"] [ ( "@" / "+" ) ]
// :<hopcount> <real name>
2014-02-18 06:30:14 +01:00
func RplWhoReply(channel *Channel, client *Client) Reply {
2014-02-11 18:09:16 +01:00
channelName := "*"
2014-02-18 06:30:14 +01:00
flags := ""
if client.flags[Away] {
flags = "G"
} else {
flags = "H"
}
if client.flags[Operator] {
flags += "*"
}
2014-02-11 18:09:16 +01:00
if channel != nil {
channelName = channel.name
2014-02-18 06:30:14 +01:00
if channel.members[client][ChannelOperator] {
flags += "@"
} else if channel.members[client][Voice] {
flags += "+"
}
2014-02-11 18:09:16 +01:00
}
2014-02-18 06:30:14 +01:00
return NewNumericReply(client.server, RPL_WHOREPLY,
"%s %s %s %s %s %s :%d %s",
channelName, client.username, client.hostname, client.server.name,
client.Nick(), flags, client.hops, client.realname)
2014-02-09 03:49:52 +01:00
}
// <name> :End of WHO list
func RplEndOfWho(server *Server, name string) Reply {
return NewNumericReply(server, RPL_ENDOFWHO, "%s :End of WHO list", name)
}
func RplBanList(channel *Channel, ban UserMask) Reply {
return NewNumericReply(channel.server, RPL_BANLIST, "%s %s", channel.name, ban)
}
func RplEndOfBanList(channel *Channel) Reply {
2014-02-12 00:44:58 +01:00
return NewNumericReply(channel.server, RPL_ENDOFBANLIST,
"%s :End of channel ban list", channel.name)
}
func RplNowAway(server *Server) Reply {
return NewNumericReply(server, RPL_NOWAWAY,
":You have been marked as being away")
}
func RplUnAway(server *Server) Reply {
return NewNumericReply(server, RPL_UNAWAY,
":You are no longer marked as being away")
}
func RplAway(server *Server, client *Client) Reply {
return NewNumericReply(server, RPL_AWAY,
"%s :%s", client.Nick(), client.awayMessage)
}
2014-02-12 00:58:54 +01:00
func RplIsOn(server *Server, nicks []string) Reply {
return NewNumericReply(server, RPL_ISON,
":%s", strings.Join(nicks, " "))
}
2014-02-12 01:35:32 +01:00
func RplMOTDStart(server *Server) Reply {
return NewNumericReply(server, RPL_MOTDSTART,
":- %s Message of the day - ", server.name)
}
func RplMOTD(server *Server, line string) Reply {
return NewNumericReply(server, RPL_MOTD,
":- %s", line)
}
func RplMOTDEnd(server *Server) Reply {
return NewNumericReply(server, RPL_ENDOFMOTD,
":End of MOTD command")
}
2014-02-17 08:51:27 +01:00
func RplList(channel *Channel) Reply {
return NewNumericReply(channel.server, RPL_LIST, "%s %d :%s",
channel, len(channel.members), channel.topic)
}
func RplListEnd(server *Server) Reply {
return NewNumericReply(server, RPL_LISTEND, ":End of LIST")
}
2014-02-12 00:58:54 +01:00
//
2012-12-13 08:33:09 +01:00
// errors (also numeric)
2014-02-12 00:58:54 +01:00
//
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)
}
2014-02-17 08:29:11 +01:00
func ErrUnknownCommand(source Identifier, code StringCode) Reply {
2012-12-13 08:27:17 +01:00
return NewNumericReply(source, ERR_UNKNOWNCOMMAND,
2014-02-17 08:29:11 +01:00
"%s :Unknown command", code)
}
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)
}
2014-02-09 03:14:39 +01:00
func ErrNoSuchChannel(server *Server, channel string) Reply {
return NewNumericReply(server, 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,
2014-02-17 08:29:11 +01:00
"%s :Channel doesn't support modes", channel)
}
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!")
}
2014-02-09 02:43:59 +01:00
func ErrNoSuchServer(server *Server, target string) Reply {
return NewNumericReply(server, ERR_NOSUCHSERVER, "%s :No such server", target)
}
2014-02-09 03:14:39 +01:00
2014-02-17 08:29:11 +01:00
func ErrUserNotInChannel(channel *Channel, client *Client) Reply {
return NewNumericReply(channel.server, ERR_USERNOTINCHANNEL,
"%s %s :They aren't on that channel", client.Nick(), channel)
2014-02-09 03:14:39 +01:00
}
2014-02-09 08:33:56 +01:00
func ErrCannotSendToChan(channel *Channel) Reply {
return NewNumericReply(channel.server, ERR_CANNOTSENDTOCHAN,
2014-02-17 08:29:11 +01:00
"%s :Cannot send to channel", channel)
2014-02-09 08:33:56 +01:00
}
// <channel> :You're not channel operator
func ErrChanOPrivIsNeeded(channel *Channel) Reply {
return NewNumericReply(channel.server, ERR_CHANOPRIVSNEEDED,
2014-02-17 08:29:11 +01:00
"%s :You're not channel operator", channel)
}
2014-02-12 00:33:02 +01:00
func ErrNoMOTD(server *Server) Reply {
return NewNumericReply(server, ERR_NOMOTD, ":MOTD File is missing")
}
func ErrNoNicknameGiven(server *Server) Reply {
return NewNumericReply(server, ERR_NONICKNAMEGIVEN, ":No nickname given")
}
func ErrErroneusNickname(server *Server, nick string) Reply {
return NewNumericReply(server, ERR_ERRONEUSNICKNAME,
"%s :Erroneous nickname", nick)
}