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

format replies to strings instead of using a Reply struct

This commit is contained in:
Jeremy Latt 2014-02-19 22:20:34 -08:00
parent 25ebab37d3
commit 9960089226
6 changed files with 292 additions and 420 deletions

View File

@ -1,9 +1,5 @@
package irc package irc
import (
"log"
)
type Channel struct { type Channel struct {
banList []UserMask banList []UserMask
flags ChannelModeSet flags ChannelModeSet
@ -31,26 +27,14 @@ func NewChannel(s *Server, name string) *Channel {
return channel return channel
} }
func (channel *Channel) Reply(reply Reply) {
if DEBUG_CHANNEL {
log.Printf("%s ← %s %s", channel, reply.Source(), reply)
}
for client := range channel.members {
if (reply.Code() == ReplyCode(PRIVMSG)) &&
(reply.Source() == client.Id()) {
continue
}
client.Reply(reply)
}
}
func (channel *Channel) IsEmpty() bool { func (channel *Channel) IsEmpty() bool {
return len(channel.members) == 0 return len(channel.members) == 0
} }
func (channel *Channel) Names(client *Client) { func (channel *Channel) Names(client *Client) {
client.Reply(NewNamesReply(channel)) client.MultilineReply(channel.Nicks(), RPL_NAMREPLY,
"= %s :%s", channel.name)
client.RplEndOfNames(channel)
} }
func (channel *Channel) ClientIsOperator(client *Client) bool { func (channel *Channel) ClientIsOperator(client *Client) bool {
@ -109,7 +93,7 @@ func (channel *Channel) ModeString() (str string) {
func (channel *Channel) Join(client *Client, key string) { func (channel *Channel) Join(client *Client, key string) {
if (channel.key != "") && (channel.key != key) { if (channel.key != "") && (channel.key != key) {
client.Reply(ErrBadChannelKey(channel)) client.ErrBadChannelKey(channel)
return return
} }
@ -120,18 +104,23 @@ func (channel *Channel) Join(client *Client, key string) {
channel.members[client][ChannelOperator] = true channel.members[client][ChannelOperator] = true
} }
channel.Reply(RplJoin(client, channel)) reply := RplJoin(client, channel)
for member := range channel.members {
member.replies <- reply
}
channel.GetTopic(client) channel.GetTopic(client)
channel.Names(client) channel.Names(client)
} }
func (channel *Channel) Part(client *Client, message string) { func (channel *Channel) Part(client *Client, message string) {
if !channel.members.Has(client) { if !channel.members.Has(client) {
client.Reply(ErrNotOnChannel(channel)) client.ErrNotOnChannel(channel)
return return
} }
channel.Reply(RplPart(client, channel, message)) for member := range channel.members {
member.replies <- RplPart(member, channel, message)
}
channel.Quit(client) channel.Quit(client)
if channel.IsEmpty() { if channel.IsEmpty() {
@ -141,7 +130,7 @@ func (channel *Channel) Part(client *Client, message string) {
func (channel *Channel) GetTopic(client *Client) { func (channel *Channel) GetTopic(client *Client) {
if !channel.members.Has(client) { if !channel.members.Has(client) {
client.Reply(ErrNotOnChannel(channel)) client.ErrNotOnChannel(channel)
return return
} }
@ -151,35 +140,42 @@ func (channel *Channel) GetTopic(client *Client) {
return return
} }
client.Reply(RplTopic(channel)) client.RplTopic(channel)
} }
func (channel *Channel) SetTopic(client *Client, topic string) { func (channel *Channel) SetTopic(client *Client, topic string) {
if !channel.members.Has(client) { if !channel.members.Has(client) {
client.Reply(ErrNotOnChannel(channel)) client.ErrNotOnChannel(channel)
return return
} }
if channel.flags[OpOnlyTopic] && !channel.members[client][ChannelOperator] { if channel.flags[OpOnlyTopic] && !channel.members[client][ChannelOperator] {
client.Reply(ErrChanOPrivIsNeeded(channel)) client.ErrChanOPrivIsNeeded(channel)
return return
} }
channel.topic = topic channel.topic = topic
channel.Reply(RplTopicMsg(client, channel)) for member := range channel.members {
member.replies <- RplTopicMsg(client, channel)
}
} }
func (channel *Channel) PrivMsg(client *Client, message string) { func (channel *Channel) PrivMsg(client *Client, message string) {
if channel.flags[NoOutside] && !channel.members.Has(client) { if channel.flags[NoOutside] && !channel.members.Has(client) {
client.Reply(ErrCannotSendToChan(channel)) client.ErrCannotSendToChan(channel)
return return
} }
channel.Reply(RplPrivMsg(client, channel, message)) for member := range channel.members {
if member == client {
continue
}
member.replies <- RplPrivMsg(client, channel, message)
}
} }
func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) { func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
if len(changes) == 0 { if len(changes) == 0 {
client.Reply(RplChannelModeIs(channel)) client.RplChannelModeIs(channel)
return return
} }
@ -191,13 +187,13 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
// TODO add/remove // TODO add/remove
for _, banMask := range channel.banList { for _, banMask := range channel.banList {
client.Reply(RplBanList(channel, banMask)) client.RplBanList(channel, banMask)
} }
client.Reply(RplEndOfBanList(channel)) client.RplEndOfBanList(channel)
case NoOutside, Private, Secret, OpOnlyTopic: case NoOutside, Private, Secret, OpOnlyTopic:
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.Reply(ErrChanOPrivIsNeeded(channel)) client.ErrChanOPrivIsNeeded(channel)
continue continue
} }
@ -213,7 +209,7 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
case Key: case Key:
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.Reply(ErrChanOPrivIsNeeded(channel)) client.ErrChanOPrivIsNeeded(channel)
continue continue
} }
@ -234,7 +230,7 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
case ChannelOperator, Voice: case ChannelOperator, Voice:
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.Reply(ErrChanOPrivIsNeeded(channel)) client.ErrChanOPrivIsNeeded(channel)
continue continue
} }
@ -267,16 +263,23 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
} }
if len(applied) > 0 { if len(applied) > 0 {
channel.Reply(RplChannelMode(client, channel, applied)) for member := range channel.members {
member.replies <- RplChannelMode(client, channel, applied)
}
} }
} }
func (channel *Channel) Notice(client *Client, message string) { func (channel *Channel) Notice(client *Client, message string) {
if channel.flags[NoOutside] && !channel.members.Has(client) { if channel.flags[NoOutside] && !channel.members.Has(client) {
client.Reply(ErrCannotSendToChan(channel)) client.ErrCannotSendToChan(channel)
return return
} }
channel.Reply(RplNotice(client, channel, message)) for member := range channel.members {
if member == client {
continue
}
member.replies <- RplNotice(client, channel, message)
}
} }
func (channel *Channel) Quit(client *Client) { func (channel *Channel) Quit(client *Client) {
@ -286,18 +289,20 @@ func (channel *Channel) Quit(client *Client) {
func (channel *Channel) Kick(client *Client, target *Client, comment string) { func (channel *Channel) Kick(client *Client, target *Client, comment string) {
if !client.flags[Operator] && !channel.members.Has(client) { if !client.flags[Operator] && !channel.members.Has(client) {
client.Reply(ErrNotOnChannel(channel)) client.ErrNotOnChannel(channel)
return return
} }
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.Reply(ErrChanOPrivIsNeeded(channel)) client.ErrChanOPrivIsNeeded(channel)
return return
} }
if !channel.members.Has(target) { if !channel.members.Has(target) {
client.Reply(ErrUserNotInChannel(channel, target)) client.ErrUserNotInChannel(channel, target)
return return
} }
channel.Reply(RplKick(channel, client, target, comment)) for member := range channel.members {
member.replies <- RplKick(channel, client, target, comment)
}
channel.Quit(target) channel.Quit(target)
} }

View File

@ -28,7 +28,7 @@ type Client struct {
phase Phase phase Phase
quitTimer *time.Timer quitTimer *time.Timer
realname string realname string
replies chan Reply replies chan string
server *Server server *Server
socket *Socket socket *Socket
username string username string
@ -46,7 +46,7 @@ func NewClient(server *Server, conn net.Conn) *Client {
phase: server.InitPhase(), phase: server.InitPhase(),
server: server, server: server,
socket: NewSocket(conn), socket: NewSocket(conn),
replies: make(chan Reply), replies: make(chan string),
} }
client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.connectionTimeout) client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.connectionTimeout)
@ -71,7 +71,7 @@ func (client *Client) readCommands() {
switch err { switch err {
case NotEnoughArgsError: case NotEnoughArgsError:
parts := strings.SplitN(line, " ", 2) parts := strings.SplitN(line, " ", 2)
client.Reply(ErrNeedMoreParams(client.server, parts[0])) client.ErrNeedMoreParams(parts[0])
} }
continue continue
} }
@ -97,7 +97,7 @@ func (client *Client) connectionClosed() {
func (client *Client) writeReplies() { func (client *Client) writeReplies() {
for reply := range client.replies { for reply := range client.replies {
client.socket.Write(reply.Format(client)...) client.socket.Write(reply)
} }
client.socket.Close() client.socket.Close()
client.doneWriting <- true client.doneWriting <- true
@ -144,7 +144,7 @@ func (client *Client) Touch() {
} }
func (client *Client) Idle() { func (client *Client) Idle() {
client.Reply(RplPing(client.server, client)) client.replies <- RplPing(client.server, client)
if client.quitTimer == nil { if client.quitTimer == nil {
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout) client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
@ -188,16 +188,6 @@ func (client *Client) destroy() {
} }
} }
func (client *Client) Reply(reply Reply) {
if client.hasQuit {
if DEBUG_CLIENT {
log.Printf("%s dropping %s", client, reply)
}
return
}
client.replies <- reply
}
func (client *Client) IdleTime() time.Duration { func (client *Client) IdleTime() time.Duration {
return time.Since(client.atime) return time.Since(client.atime)
} }
@ -276,7 +266,7 @@ func (client *Client) ChangeNickname(nickname string) {
client.nick = nickname client.nick = nickname
client.server.clients.Add(client) client.server.clients.Add(client)
for friend := range client.Friends() { for friend := range client.Friends() {
friend.Reply(reply) friend.replies <- reply
} }
} }
@ -285,7 +275,7 @@ func (client *Client) Quit(message string) {
return return
} }
client.Reply(RplError(client.server, client.Nick())) client.replies <- RplError(client.server, "connection closed")
client.hasQuit = true client.hasQuit = true
friends := client.Friends() friends := client.Friends()
@ -295,7 +285,7 @@ func (client *Client) Quit(message string) {
if len(friends) > 0 { if len(friends) > 0 {
reply := RplQuit(client, message) reply := RplQuit(client, message)
for friend := range friends { for friend := range friends {
friend.Reply(reply) friend.replies <- reply
} }
} }
} }

View File

@ -70,10 +70,6 @@ func (command *BaseCommand) Source() Identifier {
return command.Client() return command.Client()
} }
func (command *BaseCommand) Reply(reply Reply) {
command.client.Reply(reply)
}
func ParseCommand(line string) (cmd editableCommand, err error) { func ParseCommand(line string) (cmd editableCommand, err error) {
code, args := parseLine(line) code, args := parseLine(line)
constructor := parseCommandFuncs[code] constructor := parseCommandFuncs[code]

View File

@ -6,81 +6,29 @@ import (
"time" "time"
) )
type BaseReply struct {
code ReplyCode
id string
message string
}
func (reply *BaseReply) Code() ReplyCode {
return reply.code
}
func (reply *BaseReply) SetSource(source Identifier) {
reply.id = source.Id()
}
func (reply *BaseReply) Source() string {
return reply.id
}
type StringReply struct {
BaseReply
}
func NewStringReply(source Identifier, code StringCode, func NewStringReply(source Identifier, code StringCode,
format string, args ...interface{}) *StringReply { format string, args ...interface{}) string {
reply := &StringReply{} header := fmt.Sprintf(":%s %s ", source, code)
reply.code = code message := fmt.Sprintf(format, args...)
reply.message = fmt.Sprintf(format, args...) return header + message + CRLF
reply.SetSource(source)
return reply
} }
func (reply *StringReply) Format(client *Client) []string { func NewNumericReply(target *Client, code NumericCode,
message := fmt.Sprintf(":%s %s %s%s", format string, args ...interface{}) string {
reply.id, reply.code, reply.message, CRLF) header := fmt.Sprintf(":%s %s %s ", target.server.Id(), code, target.Nick())
return []string{message} message := fmt.Sprintf(format, args...)
return header + message + CRLF
} }
func (reply *StringReply) String() string { func (target *Client) NumericReply(code NumericCode,
return fmt.Sprintf("Reply(source=%s, code=%s, message=%s)", format string, args ...interface{}) {
reply.id, reply.code, reply.message) target.replies <- NewNumericReply(target, code, format, args...)
}
type NumericReply struct {
BaseReply
}
func NewNumericReply(source Identifier, code NumericCode, format string,
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 {
message := fmt.Sprintf(":%s %s %s %s%s",
reply.id, reply.code, client.Nick(), reply.message, CRLF)
return []string{message}
}
func (reply *NumericReply) String() string {
return fmt.Sprintf("Reply(source=%s, code=%d, message=%s)",
reply.id, reply.code, reply.message)
} }
// //
// multiline replies // multiline replies
// //
type MultilineReply interface {
formatLine(*Client, []string) string
names() []string
}
func joinedLen(names []string) int { func joinedLen(names []string) int {
var l = len(names) - 1 // " " between names var l = len(names) - 1 // " " between names
for _, name := range names { for _, name := range names {
@ -89,258 +37,175 @@ func joinedLen(names []string) int {
return l return l
} }
func multilineFormat(reply MultilineReply, client *Client) []string { func (target *Client) MultilineReply(names []string, code NumericCode, format string,
lines := make([]string, 0) args ...interface{}) {
baseLen := len(reply.formatLine(client, []string{})) baseLen := len(NewNumericReply(target, code, format))
tooLong := func(names []string) bool { tooLong := func(names []string) bool {
return (baseLen + joinedLen(names)) > MAX_REPLY_LEN return (baseLen + joinedLen(names)) > MAX_REPLY_LEN
} }
argsAndNames := func(names []string) []interface{} {
return append(args, strings.Join(names, " "))
}
from, to := 0, 1 from, to := 0, 1
names := reply.names()
for to < len(names) { for to < len(names) {
if (from < (to - 1)) && tooLong(names[from:to]) { if (from < (to - 1)) && tooLong(names[from:to]) {
lines = append(lines, reply.formatLine(client, names[from:to-1])) target.NumericReply(code, format, argsAndNames(names[from:to-1])...)
from, to = to-1, to from, to = to-1, to
} else { } else {
to += 1 to += 1
} }
} }
if from < len(names) { if from < len(names) {
lines = append(lines, reply.formatLine(client, names[from:])) target.NumericReply(code, format, argsAndNames(names[from:])...)
} }
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
}
func (reply *NamesReply) String() string {
return fmt.Sprintf("NamesReply(channel=%s, names=%s)",
reply.channel, reply.channel.Nicks())
}
// 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)
} }
// //
// messaging replies // messaging replies
// //
func RplPrivMsg(source Identifier, target Identifier, message string) Reply { func RplPrivMsg(source Identifier, target Identifier, message string) string {
return NewStringReply(source, PRIVMSG, "%s :%s", target.Nick(), message) return NewStringReply(source, PRIVMSG, "%s :%s", target.Nick(), message)
} }
func RplNotice(source Identifier, target Identifier, message string) Reply { func RplNotice(source Identifier, target Identifier, message string) string {
return NewStringReply(source, NOTICE, "%s :%s", target.Nick(), message) return NewStringReply(source, NOTICE, "%s :%s", target.Nick(), message)
} }
func RplNick(source Identifier, newNick string) Reply { func RplNick(source Identifier, newNick string) string {
return NewStringReply(source, NICK, newNick) return NewStringReply(source, NICK, newNick)
} }
func RplJoin(client *Client, channel *Channel) Reply { func RplJoin(client *Client, channel *Channel) string {
return NewStringReply(client, JOIN, channel.name) return NewStringReply(client, JOIN, channel.name)
} }
func RplPart(client *Client, channel *Channel, message string) Reply { func RplPart(client *Client, channel *Channel, message string) string {
return NewStringReply(client, PART, "%s :%s", channel, message) return NewStringReply(client, PART, "%s :%s", channel, message)
} }
// TODO separate source and target func RplMode(client *Client, target *Client, changes ModeChanges) string {
func RplMode(client *Client, changes ModeChanges) Reply { return NewStringReply(client, MODE, "%s :%s", target.Nick(), changes)
return NewStringReply(client, MODE, "%s :%s", client.Nick(), changes)
} }
func RplChannelMode(client *Client, channel *Channel, func RplChannelMode(client *Client, channel *Channel,
changes ChannelModeChanges) Reply { changes ChannelModeChanges) string {
return NewStringReply(client, MODE, "%s %s", channel, changes) return NewStringReply(client, MODE, "%s %s", channel, changes)
} }
func RplTopicMsg(source Identifier, channel *Channel) Reply { func RplTopicMsg(source Identifier, channel *Channel) string {
return NewStringReply(source, TOPIC, "%s :%s", channel, channel.topic) return NewStringReply(source, TOPIC, "%s :%s", channel, channel.topic)
} }
func RplPing(server *Server, target Identifier) Reply { func RplPing(server *Server, target Identifier) string {
return NewStringReply(server, PING, target.Nick()) return NewStringReply(server, PING, target.Nick())
} }
func RplPong(server *Server, client *Client) Reply { func RplPong(server *Server, client *Client) string {
return NewStringReply(server, PONG, client.Nick()) return NewStringReply(server, PONG, client.Nick())
} }
func RplQuit(client *Client, message string) Reply { func RplQuit(client *Client, message string) string {
return NewStringReply(client, QUIT, ":%s", message) return NewStringReply(client, QUIT, ":%s", message)
} }
func RplError(server *Server, message string) Reply { func RplError(server *Server, message string) string {
return NewStringReply(server, ERROR, message) return NewStringReply(server, ERROR, ":%s", message)
} }
func RplInviteMsg(channel *Channel, inviter *Client) Reply { func RplInviteMsg(channel *Channel, inviter *Client) string {
return NewStringReply(inviter, INVITE, channel.name) return NewStringReply(inviter, INVITE, channel.name)
} }
func RplKick(channel *Channel, client *Client, target *Client, comment string) Reply { func RplKick(channel *Channel, client *Client, target *Client, comment string) string {
return NewStringReply(client, KICK, "%s %s :%s", return NewStringReply(client, KICK, "%s %s :%s",
channel, target.Nick(), comment) channel, target.Nick(), comment)
} }
// numeric replies // numeric replies
func RplWelcome(source Identifier, client *Client) Reply { func (target *Client) RplWelcome() {
return NewNumericReply(source, RPL_WELCOME, target.NumericReply(RPL_WELCOME,
":Welcome to the Internet Relay Network %s", client.Id()) ":Welcome to the Internet Relay Network %s", target.Id())
} }
func RplYourHost(server *Server) Reply { func (target *Client) RplYourHost() {
return NewNumericReply(server, RPL_YOURHOST, target.NumericReply(RPL_YOURHOST,
":Your host is %s, running version %s", server.name, VERSION) ":Your host is %s, running version %s", target.server.name, VERSION)
} }
func RplCreated(server *Server) Reply { func (target *Client) RplCreated() {
return NewNumericReply(server, RPL_CREATED, target.NumericReply(RPL_CREATED,
":This server was created %s", server.ctime.Format(time.RFC1123)) ":This server was created %s", target.server.ctime.Format(time.RFC1123))
} }
func RplMyInfo(server *Server) Reply { func (target *Client) RplMyInfo() {
return NewNumericReply(server, RPL_MYINFO, target.NumericReply(RPL_MYINFO,
"%s %s aiOorsw abeIikmntpqrsl", server.name, VERSION) "%s %s aiOorsw abeIikmntpqrsl", target.server.name, VERSION)
} }
func RplUModeIs(server *Server, client *Client) Reply { func (target *Client) RplUModeIs(client *Client) {
return NewNumericReply(server, RPL_UMODEIS, client.ModeString()) target.NumericReply(RPL_UMODEIS, client.ModeString())
} }
func RplNoTopic(channel *Channel) Reply { func (target *Client) RplNoTopic(channel *Channel) {
return NewNumericReply(channel.server, RPL_NOTOPIC, target.NumericReply(RPL_NOTOPIC,
"%s :No topic is set", channel.name) "%s :No topic is set", channel.name)
} }
func RplTopic(channel *Channel) Reply { func (target *Client) RplTopic(channel *Channel) {
return NewNumericReply(channel.server, RPL_TOPIC, target.NumericReply(RPL_TOPIC,
"%s :%s", channel.name, channel.topic) "%s :%s", channel.name, channel.topic)
} }
// <nick> <channel> // <nick> <channel>
// NB: correction in errata // NB: correction in errata
func RplInvitingMsg(channel *Channel, invitee *Client) Reply { func (target *Client) RplInvitingMsg(channel *Channel, invitee *Client) {
return NewNumericReply(channel.server, RPL_INVITING, target.NumericReply(RPL_INVITING,
"%s %s", invitee.Nick(), channel.name) "%s %s", invitee.Nick(), channel.name)
} }
func RplNamReply(channel *Channel, names []string) *NumericReply { func (target *Client) RplEndOfNames(channel *Channel) {
return NewNumericReply(channel.server, RPL_NAMREPLY, "= %s :%s", target.NumericReply(RPL_ENDOFNAMES,
channel.name, strings.Join(names, " "))
}
func RplEndOfNames(channel *Channel) Reply {
return NewNumericReply(channel.server, RPL_ENDOFNAMES,
"%s :End of NAMES list", channel.name) "%s :End of NAMES list", channel.name)
} }
// :You are now an IRC operator // :You are now an IRC operator
func RplYoureOper(server *Server) Reply { func (target *Client) RplYoureOper() {
return NewNumericReply(server, RPL_YOUREOPER, ":You are now an IRC operator") target.NumericReply(RPL_YOUREOPER,
":You are now an IRC operator")
} }
func RplWhoisUser(client *Client) Reply { func (target *Client) RplWhoisUser(client *Client) {
return NewNumericReply(client.server, RPL_WHOISUSER, "%s %s %s * :%s", target.NumericReply(RPL_WHOISUSER,
client.Nick(), client.username, client.hostname, client.realname) "%s %s %s * :%s", client.Nick(), client.username, client.hostname,
client.realname)
} }
func RplWhoisOperator(client *Client) Reply { func (target *Client) RplWhoisOperator(client *Client) {
return NewNumericReply(client.server, RPL_WHOISOPERATOR, target.NumericReply(RPL_WHOISOPERATOR,
"%s :is an IRC operator", client.Nick()) "%s :is an IRC operator", client.Nick())
} }
func RplWhoisIdle(client *Client) Reply { func (target *Client) RplWhoisIdle(client *Client) {
return NewNumericReply(client.server, RPL_WHOISIDLE, target.NumericReply(RPL_WHOISIDLE,
"%s %d %d :seconds idle, signon time", "%s %d %d :seconds idle, signon time",
client.Nick(), client.IdleSeconds(), client.SignonTime()) client.Nick(), client.IdleSeconds(), client.SignonTime())
} }
func RplWhoisChannels(client *Client, chstrs []string) Reply { func (target *Client) RplEndOfWhois() {
return NewNumericReply(client.server, RPL_WHOISCHANNELS, target.NumericReply(RPL_ENDOFWHOIS,
"%s :%s", client.Nick(), strings.Join(chstrs, " ")) ":End of WHOIS list")
} }
func RplEndOfWhois(server *Server) Reply { func (target *Client) RplChannelModeIs(channel *Channel) {
return NewNumericReply(server, RPL_ENDOFWHOIS, ":End of WHOIS list") target.NumericReply(RPL_CHANNELMODEIS,
} "%s %s", channel, channel.ModeString())
func RplChannelModeIs(channel *Channel) Reply {
return NewNumericReply(channel.server, RPL_CHANNELMODEIS, "%s %s",
channel, channel.ModeString())
} }
// <channel> <user> <host> <server> <nick> ( "H" / "G" ) ["*"] [ ( "@" / "+" ) ] // <channel> <user> <host> <server> <nick> ( "H" / "G" ) ["*"] [ ( "@" / "+" ) ]
// :<hopcount> <real name> // :<hopcount> <real name>
func RplWhoReply(channel *Channel, client *Client) Reply { func (target *Client) RplWhoReply(channel *Channel, client *Client) {
channelName := "*" channelName := "*"
flags := "" flags := ""
@ -362,175 +227,177 @@ func RplWhoReply(channel *Channel, client *Client) Reply {
flags += "+" flags += "+"
} }
} }
return NewNumericReply(client.server, RPL_WHOREPLY, target.NumericReply(RPL_WHOREPLY,
"%s %s %s %s %s %s :%d %s", "%s %s %s %s %s %s :%d %s", channelName, client.username, client.hostname,
channelName, client.username, client.hostname, client.server.name, client.server.name, client.Nick(), flags, client.hops, client.realname)
client.Nick(), flags, client.hops, client.realname)
} }
// <name> :End of WHO list // <name> :End of WHO list
func RplEndOfWho(server *Server, name string) Reply { func (target *Client) RplEndOfWho(name string) {
return NewNumericReply(server, RPL_ENDOFWHO, "%s :End of WHO list", name) target.NumericReply(RPL_ENDOFWHO,
"%s :End of WHO list", name)
} }
func RplBanList(channel *Channel, ban UserMask) Reply { func (target *Client) RplBanList(channel *Channel, ban UserMask) {
return NewNumericReply(channel.server, RPL_BANLIST, "%s %s", channel.name, ban) target.NumericReply(RPL_BANLIST,
"%s %s", channel.name, ban)
} }
func RplEndOfBanList(channel *Channel) Reply { func (target *Client) RplEndOfBanList(channel *Channel) {
return NewNumericReply(channel.server, RPL_ENDOFBANLIST, target.NumericReply(RPL_ENDOFBANLIST,
"%s :End of channel ban list", channel.name) "%s :End of channel ban list", channel.name)
} }
func RplNowAway(server *Server) Reply { func (target *Client) RplNowAway() {
return NewNumericReply(server, RPL_NOWAWAY, target.NumericReply(RPL_NOWAWAY,
":You have been marked as being away") ":You have been marked as being away")
} }
func RplUnAway(server *Server) Reply { func (target *Client) RplUnAway() {
return NewNumericReply(server, RPL_UNAWAY, target.NumericReply(RPL_UNAWAY,
":You are no longer marked as being away") ":You are no longer marked as being away")
} }
func RplAway(server *Server, client *Client) Reply { func (target *Client) RplAway(client *Client) {
return NewNumericReply(server, RPL_AWAY, target.NumericReply(RPL_AWAY,
"%s :%s", client.Nick(), client.awayMessage) "%s :%s", client.Nick(), client.awayMessage)
} }
func RplIsOn(server *Server, nicks []string) Reply { func (target *Client) RplIsOn(nicks []string) {
return NewNumericReply(server, RPL_ISON, target.NumericReply(RPL_ISON,
":%s", strings.Join(nicks, " ")) ":%s", strings.Join(nicks, " "))
} }
func RplMOTDStart(server *Server) Reply { func (target *Client) RplMOTDStart() {
return NewNumericReply(server, RPL_MOTDSTART, target.NumericReply(RPL_MOTDSTART,
":- %s Message of the day - ", server.name) ":- %s Message of the day - ", target.server.name)
} }
func RplMOTD(server *Server, line string) Reply { func (target *Client) RplMOTD(line string) {
return NewNumericReply(server, RPL_MOTD, target.NumericReply(RPL_MOTD,
":- %s", line) ":- %s", line)
} }
func RplMOTDEnd(server *Server) Reply { func (target *Client) RplMOTDEnd() {
return NewNumericReply(server, RPL_ENDOFMOTD, target.NumericReply(RPL_ENDOFMOTD,
":End of MOTD command") ":End of MOTD command")
} }
func RplList(channel *Channel) Reply { func (target *Client) RplList(channel *Channel) {
return NewNumericReply(channel.server, RPL_LIST, "%s %d :%s", target.NumericReply(RPL_LIST,
channel, len(channel.members), channel.topic) "%s %d :%s", channel, len(channel.members), channel.topic)
} }
func RplListEnd(server *Server) Reply { func (target *Client) RplListEnd(server *Server) {
return NewNumericReply(server, RPL_LISTEND, ":End of LIST") target.NumericReply(RPL_LISTEND,
":End of LIST")
} }
// //
// errors (also numeric) // errors (also numeric)
// //
func ErrAlreadyRegistered(source Identifier) Reply { func (target *Client) ErrAlreadyRegistered() {
return NewNumericReply(source, ERR_ALREADYREGISTRED, target.NumericReply(ERR_ALREADYREGISTRED,
":You may not reregister") ":You may not reregister")
} }
func ErrNickNameInUse(source Identifier, nick string) Reply { func (target *Client) ErrNickNameInUse(nick string) {
return NewNumericReply(source, ERR_NICKNAMEINUSE, target.NumericReply(ERR_NICKNAMEINUSE,
"%s :Nickname is already in use", nick) "%s :Nickname is already in use", nick)
} }
func ErrUnknownCommand(source Identifier, code StringCode) Reply { func (target *Client) ErrUnknownCommand(code StringCode) {
return NewNumericReply(source, ERR_UNKNOWNCOMMAND, target.NumericReply(ERR_UNKNOWNCOMMAND,
"%s :Unknown command", code) "%s :Unknown command", code)
} }
func ErrUsersDontMatch(source Identifier) Reply { func (target *Client) ErrUsersDontMatch() {
return NewNumericReply(source, ERR_USERSDONTMATCH, target.NumericReply(ERR_USERSDONTMATCH,
":Cannot change mode for other users") ":Cannot change mode for other users")
} }
func ErrNeedMoreParams(source Identifier, command string) Reply { func (target *Client) ErrNeedMoreParams(command string) {
return NewNumericReply(source, ERR_NEEDMOREPARAMS, target.NumericReply(ERR_NEEDMOREPARAMS,
"%s :Not enough parameters", command) "%s :Not enough parameters", command)
} }
func ErrNoSuchChannel(server *Server, channel string) Reply { func (target *Client) ErrNoSuchChannel(channel string) {
return NewNumericReply(server, ERR_NOSUCHCHANNEL, target.NumericReply(ERR_NOSUCHCHANNEL,
"%s :No such channel", channel) "%s :No such channel", channel)
} }
func ErrUserOnChannel(channel *Channel, member *Client) Reply { func (target *Client) ErrUserOnChannel(channel *Channel, member *Client) {
return NewNumericReply(channel.server, ERR_USERONCHANNEL, target.NumericReply(ERR_USERONCHANNEL,
"%s %s :is already on channel", member.Nick(), channel.name) "%s %s :is already on channel", member.Nick(), channel.name)
} }
func ErrNotOnChannel(channel *Channel) Reply { func (target *Client) ErrNotOnChannel(channel *Channel) {
return NewNumericReply(channel.server, ERR_NOTONCHANNEL, target.NumericReply(ERR_NOTONCHANNEL,
"%s :You're not on that channel", channel.name) "%s :You're not on that channel", channel.name)
} }
func ErrInviteOnlyChannel(channel *Channel) Reply { func (target *Client) ErrInviteOnlyChannel(channel *Channel) {
return NewNumericReply(channel.server, ERR_INVITEONLYCHAN, target.NumericReply(ERR_INVITEONLYCHAN,
"%s :Cannot join channel (+i)", channel.name) "%s :Cannot join channel (+i)", channel.name)
} }
func ErrBadChannelKey(channel *Channel) Reply { func (target *Client) ErrBadChannelKey(channel *Channel) {
return NewNumericReply(channel.server, ERR_BADCHANNELKEY, target.NumericReply(ERR_BADCHANNELKEY,
"%s :Cannot join channel (+k)", channel.name) "%s :Cannot join channel (+k)", channel.name)
} }
func ErrNoSuchNick(source Identifier, nick string) Reply { func (target *Client) ErrNoSuchNick(nick string) {
return NewNumericReply(source, ERR_NOSUCHNICK, target.NumericReply(ERR_NOSUCHNICK,
"%s :No such nick/channel", nick) "%s :No such nick/channel", nick)
} }
func ErrPasswdMismatch(server *Server) Reply { func (target *Client) ErrPasswdMismatch() {
return NewNumericReply(server, ERR_PASSWDMISMATCH, ":Password incorrect") target.NumericReply(ERR_PASSWDMISMATCH, ":Password incorrect")
} }
func ErrNoChanModes(channel *Channel) Reply { func (target *Client) ErrNoChanModes(channel *Channel) {
return NewNumericReply(channel.server, ERR_NOCHANMODES, target.NumericReply(ERR_NOCHANMODES,
"%s :Channel doesn't support modes", channel) "%s :Channel doesn't support modes", channel)
} }
func ErrNoPrivileges(server *Server) Reply { func (target *Client) ErrNoPrivileges() {
return NewNumericReply(server, ERR_NOPRIVILEGES, ":Permission Denied") target.NumericReply(ERR_NOPRIVILEGES, ":Permission Denied")
} }
func ErrRestricted(server *Server) Reply { func (target *Client) ErrRestricted() {
return NewNumericReply(server, ERR_RESTRICTED, ":Your connection is restricted!") target.NumericReply(ERR_RESTRICTED, ":Your connection is restricted!")
} }
func ErrNoSuchServer(server *Server, target string) Reply { func (target *Client) ErrNoSuchServer(server string) {
return NewNumericReply(server, ERR_NOSUCHSERVER, "%s :No such server", target) target.NumericReply(ERR_NOSUCHSERVER, "%s :No such server", server)
} }
func ErrUserNotInChannel(channel *Channel, client *Client) Reply { func (target *Client) ErrUserNotInChannel(channel *Channel, client *Client) {
return NewNumericReply(channel.server, ERR_USERNOTINCHANNEL, target.NumericReply(ERR_USERNOTINCHANNEL,
"%s %s :They aren't on that channel", client.Nick(), channel) "%s %s :They aren't on that channel", client.Nick(), channel)
} }
func ErrCannotSendToChan(channel *Channel) Reply { func (target *Client) ErrCannotSendToChan(channel *Channel) {
return NewNumericReply(channel.server, ERR_CANNOTSENDTOCHAN, target.NumericReply(ERR_CANNOTSENDTOCHAN,
"%s :Cannot send to channel", channel) "%s :Cannot send to channel", channel)
} }
// <channel> :You're not channel operator // <channel> :You're not channel operator
func ErrChanOPrivIsNeeded(channel *Channel) Reply { func (target *Client) ErrChanOPrivIsNeeded(channel *Channel) {
return NewNumericReply(channel.server, ERR_CHANOPRIVSNEEDED, target.NumericReply(ERR_CHANOPRIVSNEEDED,
"%s :You're not channel operator", channel) "%s :You're not channel operator", channel)
} }
func ErrNoMOTD(server *Server) Reply { func (target *Client) ErrNoMOTD() {
return NewNumericReply(server, ERR_NOMOTD, ":MOTD File is missing") target.NumericReply(ERR_NOMOTD, ":MOTD File is missing")
} }
func ErrNoNicknameGiven(server *Server) Reply { func (target *Client) ErrNoNicknameGiven() {
return NewNumericReply(server, ERR_NONICKNAMEGIVEN, ":No nickname given") target.NumericReply(ERR_NONICKNAMEGIVEN, ":No nickname given")
} }
func ErrErroneusNickname(server *Server, nick string) Reply { func (target *Client) ErrErroneusNickname(nick string) {
return NewNumericReply(server, ERR_ERRONEUSNICKNAME, target.NumericReply(ERR_ERRONEUSNICKNAME,
"%s :Erroneous nickname", nick) "%s :Erroneous nickname", nick)
} }

View File

@ -86,7 +86,7 @@ func (server *Server) ReceiveCommands() {
default: default:
srvCmd, ok := cmd.(ServerCommand) srvCmd, ok := cmd.(ServerCommand)
if !ok { if !ok {
client.Reply(ErrUnknownCommand(server, cmd.Code())) client.ErrUnknownCommand(cmd.Code())
continue continue
} }
switch srvCmd.(type) { switch srvCmd.(type) {
@ -192,28 +192,28 @@ func (s *Server) GenerateGuestNick() string {
func (s *Server) tryRegister(c *Client) { func (s *Server) tryRegister(c *Client) {
if c.HasNick() && c.HasUsername() { if c.HasNick() && c.HasUsername() {
c.Register() c.Register()
c.Reply(RplWelcome(s, c)) c.RplWelcome()
c.Reply(RplYourHost(s)) c.RplYourHost()
c.Reply(RplCreated(s)) c.RplCreated()
c.Reply(RplMyInfo(s)) c.RplMyInfo()
s.MOTD(c) s.MOTD(c)
} }
} }
func (server *Server) MOTD(client *Client) { func (server *Server) MOTD(client *Client) {
if server.motdFile == "" { if server.motdFile == "" {
client.Reply(ErrNoMOTD(server)) client.ErrNoMOTD()
return return
} }
file, err := os.Open(server.motdFile) file, err := os.Open(server.motdFile)
if err != nil { if err != nil {
client.Reply(ErrNoMOTD(server)) client.ErrNoMOTD()
return return
} }
defer file.Close() defer file.Close()
client.Reply(RplMOTDStart(server)) client.RplMOTDStart()
reader := bufio.NewReader(file) reader := bufio.NewReader(file)
for { for {
line, err := reader.ReadString('\n') line, err := reader.ReadString('\n')
@ -224,17 +224,17 @@ func (server *Server) MOTD(client *Client) {
if len(line) > 80 { if len(line) > 80 {
for len(line) > 80 { for len(line) > 80 {
client.Reply(RplMOTD(server, line[0:80])) client.RplMOTD(line[0:80])
line = line[80:] line = line[80:]
} }
if len(line) > 0 { if len(line) > 0 {
client.Reply(RplMOTD(server, line)) client.RplMOTD(line)
} }
} else { } else {
client.Reply(RplMOTD(server, line)) client.RplMOTD(line)
} }
} }
client.Reply(RplMOTDEnd(server)) client.RplMOTDEnd()
} }
func (s *Server) Id() string { func (s *Server) Id() string {
@ -266,7 +266,7 @@ func (m *PassCommand) HandleAuthServer(s *Server) {
client := m.Client() client := m.Client()
if s.password != m.password { if s.password != m.password {
client.Reply(ErrPasswdMismatch(s)) client.ErrPasswdMismatch()
client.socket.Close() client.socket.Close()
return return
} }
@ -286,17 +286,17 @@ func (m *NickCommand) HandleRegServer(s *Server) {
client := m.Client() client := m.Client()
if m.nickname == "" { if m.nickname == "" {
client.Reply(ErrNoNicknameGiven(s)) client.ErrNoNicknameGiven()
return return
} }
if s.clients.Get(m.nickname) != nil { if s.clients.Get(m.nickname) != nil {
client.Reply(ErrNickNameInUse(s, m.nickname)) client.ErrNickNameInUse(m.nickname)
return return
} }
if !IsNickname(m.nickname) { if !IsNickname(m.nickname) {
client.Reply(ErrErroneusNickname(s, m.nickname)) client.ErrErroneusNickname(m.nickname)
return return
} }
@ -315,7 +315,7 @@ func (msg *RFC2812UserCommand) HandleRegServer(server *Server) {
for _, mode := range msg.Flags() { for _, mode := range msg.Flags() {
client.flags[mode] = true client.flags[mode] = true
} }
client.Reply(RplUModeIs(server, client)) client.RplUModeIs(client)
} }
msg.HandleRegServer2(server) msg.HandleRegServer2(server)
} }
@ -335,11 +335,11 @@ func (msg *QuitCommand) HandleRegServer(server *Server) {
// //
func (m *PassCommand) HandleServer(s *Server) { func (m *PassCommand) HandleServer(s *Server) {
m.Client().Reply(ErrAlreadyRegistered(s)) m.Client().ErrAlreadyRegistered()
} }
func (m *PingCommand) HandleServer(s *Server) { func (m *PingCommand) HandleServer(s *Server) {
m.Client().Reply(RplPong(s, m.Client())) m.Client().replies <- RplPong(s, m.Client())
} }
func (m *PongCommand) HandleServer(s *Server) { func (m *PongCommand) HandleServer(s *Server) {
@ -350,12 +350,12 @@ func (msg *NickCommand) HandleServer(server *Server) {
client := msg.Client() client := msg.Client()
if msg.nickname == "" { if msg.nickname == "" {
client.Reply(ErrNoNicknameGiven(server)) client.ErrNoNicknameGiven()
return return
} }
if server.clients.Get(msg.nickname) != nil { if server.clients.Get(msg.nickname) != nil {
client.Reply(ErrNickNameInUse(server, msg.nickname)) client.ErrNickNameInUse(msg.nickname)
return return
} }
@ -363,7 +363,7 @@ func (msg *NickCommand) HandleServer(server *Server) {
} }
func (m *UserCommand) HandleServer(s *Server) { func (m *UserCommand) HandleServer(s *Server) {
m.Client().Reply(ErrAlreadyRegistered(s)) m.Client().ErrAlreadyRegistered()
} }
func (msg *QuitCommand) HandleServer(server *Server) { func (msg *QuitCommand) HandleServer(server *Server) {
@ -392,7 +392,7 @@ func (m *PartCommand) HandleServer(server *Server) {
channel := server.channels[chname] channel := server.channels[chname]
if channel == nil { if channel == nil {
m.Client().Reply(ErrNoSuchChannel(server, chname)) m.Client().ErrNoSuchChannel(chname)
continue continue
} }
@ -404,7 +404,7 @@ func (msg *TopicCommand) HandleServer(server *Server) {
client := msg.Client() client := msg.Client()
channel := server.channels[msg.channel] channel := server.channels[msg.channel]
if channel == nil { if channel == nil {
client.Reply(ErrNoSuchChannel(server, msg.channel)) client.ErrNoSuchChannel(msg.channel)
return return
} }
@ -420,7 +420,7 @@ func (msg *PrivMsgCommand) HandleServer(server *Server) {
if IsChannel(msg.target) { if IsChannel(msg.target) {
channel := server.channels[msg.target] channel := server.channels[msg.target]
if channel == nil { if channel == nil {
client.Reply(ErrNoSuchChannel(server, msg.target)) client.ErrNoSuchChannel(msg.target)
return return
} }
@ -430,12 +430,12 @@ func (msg *PrivMsgCommand) HandleServer(server *Server) {
target := server.clients[msg.target] target := server.clients[msg.target]
if target == nil { if target == nil {
client.Reply(ErrNoSuchNick(server, msg.target)) client.ErrNoSuchNick(msg.target)
return return
} }
target.Reply(RplPrivMsg(client, target, msg.message)) target.replies <- RplPrivMsg(client, target, msg.message)
if target.flags[Away] { if target.flags[Away] {
client.Reply(RplAway(server, target)) target.RplAway(client)
} }
} }
@ -444,12 +444,12 @@ func (m *ModeCommand) HandleServer(s *Server) {
target := s.clients.Get(m.nickname) target := s.clients.Get(m.nickname)
if target == nil { if target == nil {
client.Reply(ErrNoSuchNick(s, m.nickname)) client.ErrNoSuchNick(m.nickname)
return return
} }
if client != target && !client.flags[Operator] { if client != target && !client.flags[Operator] {
client.Reply(ErrUsersDontMatch(s)) client.ErrUsersDontMatch()
return return
} }
@ -460,27 +460,47 @@ func (m *ModeCommand) HandleServer(s *Server) {
case Invisible, ServerNotice, WallOps: case Invisible, ServerNotice, WallOps:
switch change.op { switch change.op {
case Add: case Add:
client.flags[change.mode] = true target.flags[change.mode] = true
changes = append(changes, change) changes = append(changes, change)
case Remove: case Remove:
delete(client.flags, change.mode) delete(target.flags, change.mode)
changes = append(changes, change) changes = append(changes, change)
} }
case Operator, LocalOperator: case Operator, LocalOperator:
if change.op == Remove { if change.op == Remove {
delete(client.flags, change.mode) delete(target.flags, change.mode)
changes = append(changes, change) changes = append(changes, change)
} }
} }
} }
// Who should get these replies?
if len(changes) > 0 { if len(changes) > 0 {
client.Reply(RplMode(client, changes)) client.replies <- RplMode(client, target, changes)
} }
} }
func (client *Client) WhoisChannelsNames() []string {
chstrs := make([]string, len(client.channels))
index := 0
for channel := range client.channels {
switch {
case channel.members[client][ChannelOperator]:
chstrs[index] = "@" + channel.name
case channel.members[client][Voice]:
chstrs[index] = "+" + channel.name
default:
chstrs[index] = channel.name
}
index += 1
}
return chstrs
}
func (m *WhoisCommand) HandleServer(server *Server) { func (m *WhoisCommand) HandleServer(server *Server) {
client := m.Client() client := m.Client()
@ -490,16 +510,17 @@ func (m *WhoisCommand) HandleServer(server *Server) {
// TODO implement wildcard matching // TODO implement wildcard matching
mclient := server.clients.Get(mask) mclient := server.clients.Get(mask)
if mclient == nil { if mclient == nil {
client.Reply(ErrNoSuchNick(server, mask)) client.ErrNoSuchNick(mask)
continue continue
} }
client.Reply(RplWhoisUser(mclient)) client.RplWhoisUser(mclient)
if client.flags[Operator] { if client.flags[Operator] {
client.Reply(RplWhoisOperator(mclient)) client.RplWhoisOperator(mclient)
} }
client.Reply(RplWhoisIdle(mclient)) client.RplWhoisIdle(mclient)
client.Reply(NewWhoisChannelsReply(mclient)) client.MultilineReply(client.WhoisChannelsNames(), RPL_WHOISCHANNELS,
client.Reply(RplEndOfWhois(server)) "%s :%s", client.Nick())
client.RplEndOfWhois()
} }
} }
@ -507,7 +528,7 @@ func (msg *ChannelModeCommand) HandleServer(server *Server) {
client := msg.Client() client := msg.Client()
channel := server.channels[msg.channel] channel := server.channels[msg.channel]
if channel == nil { if channel == nil {
client.Reply(ErrNoSuchChannel(server, msg.channel)) client.ErrNoSuchChannel(msg.channel)
return return
} }
@ -517,15 +538,15 @@ func (msg *ChannelModeCommand) HandleServer(server *Server) {
func whoChannel(client *Client, channel *Channel) { func whoChannel(client *Client, channel *Channel) {
for member := range channel.members { for member := range channel.members {
if !client.flags[Invisible] { if !client.flags[Invisible] {
client.Reply(RplWhoReply(channel, member)) client.RplWhoReply(channel, member)
} }
} }
} }
func (msg *WhoCommand) HandleServer(server *Server) { func (msg *WhoCommand) HandleServer(server *Server) {
client := msg.Client() client := msg.Client()
// TODO implement wildcard matching
// TODO implement wildcard matching
mask := string(msg.mask) mask := string(msg.mask)
if mask == "" { if mask == "" {
for _, channel := range server.channels { for _, channel := range server.channels {
@ -539,25 +560,25 @@ func (msg *WhoCommand) HandleServer(server *Server) {
} else { } else {
mclient := server.clients[mask] mclient := server.clients[mask]
if mclient != nil && !mclient.flags[Invisible] { if mclient != nil && !mclient.flags[Invisible] {
client.Reply(RplWhoReply(nil, mclient)) client.RplWhoReply(nil, mclient)
} }
} }
client.Reply(RplEndOfWho(server, mask)) client.RplEndOfWho(mask)
} }
func (msg *OperCommand) HandleServer(server *Server) { func (msg *OperCommand) HandleServer(server *Server) {
client := msg.Client() client := msg.Client()
if server.operators[msg.name] != msg.password { if server.operators[msg.name] != msg.password {
client.Reply(ErrPasswdMismatch(server)) client.ErrPasswdMismatch()
return return
} }
client.flags[Operator] = true client.flags[Operator] = true
client.Reply(RplYoureOper(server)) client.RplYoureOper()
client.Reply(RplUModeIs(server, client)) client.RplUModeIs(client)
} }
func (msg *AwayCommand) HandleServer(server *Server) { func (msg *AwayCommand) HandleServer(server *Server) {
@ -570,9 +591,9 @@ func (msg *AwayCommand) HandleServer(server *Server) {
client.awayMessage = msg.text client.awayMessage = msg.text
if client.flags[Away] { if client.flags[Away] {
client.Reply(RplNowAway(server)) client.RplNowAway()
} else { } else {
client.Reply(RplUnAway(server)) client.RplUnAway()
} }
} }
@ -586,7 +607,7 @@ func (msg *IsOnCommand) HandleServer(server *Server) {
} }
} }
client.Reply(RplIsOn(server, ison)) client.RplIsOn(ison)
} }
func (msg *MOTDCommand) HandleServer(server *Server) { func (msg *MOTDCommand) HandleServer(server *Server) {
@ -598,7 +619,7 @@ func (msg *NoticeCommand) HandleServer(server *Server) {
if IsChannel(msg.target) { if IsChannel(msg.target) {
channel := server.channels[msg.target] channel := server.channels[msg.target]
if channel == nil { if channel == nil {
client.Reply(ErrNoSuchChannel(server, msg.target)) client.ErrNoSuchChannel(msg.target)
return return
} }
@ -608,10 +629,10 @@ func (msg *NoticeCommand) HandleServer(server *Server) {
target := server.clients.Get(msg.target) target := server.clients.Get(msg.target)
if target == nil { if target == nil {
client.Reply(ErrNoSuchNick(server, msg.target)) client.ErrNoSuchNick(msg.target)
return return
} }
target.Reply(RplNotice(client, target, msg.message)) target.replies <- RplNotice(client, target, msg.message)
} }
func (msg *KickCommand) HandleServer(server *Server) { func (msg *KickCommand) HandleServer(server *Server) {
@ -619,13 +640,13 @@ func (msg *KickCommand) HandleServer(server *Server) {
for chname, nickname := range msg.kicks { for chname, nickname := range msg.kicks {
channel := server.channels[chname] channel := server.channels[chname]
if channel == nil { if channel == nil {
client.Reply(ErrNoSuchChannel(server, chname)) client.ErrNoSuchChannel(chname)
continue continue
} }
target := server.clients[nickname] target := server.clients[nickname]
if target == nil { if target == nil {
client.Reply(ErrNoSuchNick(server, nickname)) client.ErrNoSuchNick(nickname)
continue continue
} }
@ -638,7 +659,7 @@ func (msg *ListCommand) HandleServer(server *Server) {
// TODO target server // TODO target server
if msg.target != "" { if msg.target != "" {
client.Reply(ErrNoSuchServer(server, msg.target)) client.ErrNoSuchServer(msg.target)
return return
} }
@ -648,20 +669,20 @@ func (msg *ListCommand) HandleServer(server *Server) {
(channel.flags[Secret] || channel.flags[Private]) { (channel.flags[Secret] || channel.flags[Private]) {
continue continue
} }
client.Reply(RplList(channel)) client.RplList(channel)
} }
} else { } else {
for _, chname := range msg.channels { for _, chname := range msg.channels {
channel := server.channels[chname] channel := server.channels[chname]
if channel == nil || (!client.flags[Operator] && if channel == nil || (!client.flags[Operator] &&
(channel.flags[Secret] || channel.flags[Private])) { (channel.flags[Secret] || channel.flags[Private])) {
client.Reply(ErrNoSuchChannel(server, chname)) client.ErrNoSuchChannel(chname)
continue continue
} }
client.Reply(RplList(channel)) client.RplList(channel)
} }
} }
client.Reply(RplListEnd(server)) client.RplListEnd(server)
} }
func (msg *NamesCommand) HandleServer(server *Server) { func (msg *NamesCommand) HandleServer(server *Server) {
@ -676,7 +697,7 @@ func (msg *NamesCommand) HandleServer(server *Server) {
for _, chname := range msg.channels { for _, chname := range msg.channels {
channel := server.channels[chname] channel := server.channels[chname]
if channel == nil { if channel == nil {
client.Reply(ErrNoSuchChannel(server, chname)) client.ErrNoSuchChannel(chname)
continue continue
} }
channel.Names(client) channel.Names(client)

View File

@ -170,20 +170,13 @@ type Identifier interface {
} }
type Replier interface { type Replier interface {
Reply(Reply) Reply(...string)
}
type Reply interface {
Code() ReplyCode
Format(*Client) []string
Source() string
} }
type Command interface { type Command interface {
Code() StringCode Code() StringCode
Client() *Client Client() *Client
Source() Identifier Source() Identifier
Reply(Reply)
} }
type ServerCommand interface { type ServerCommand interface {