dispatch/server/irc_handler.go

209 lines
4.4 KiB
Go
Raw Normal View History

package server
2015-01-17 01:37:21 +00:00
import (
"log"
2015-01-17 01:37:21 +00:00
"strings"
2015-06-05 22:34:13 +00:00
"github.com/khlieng/name_pending/irc"
"github.com/khlieng/name_pending/storage"
2015-01-17 01:37:21 +00:00
)
2015-06-05 22:34:13 +00:00
func handleIRC(client *irc.Client, session *Session) {
var whois WhoisReply
2015-01-17 01:37:21 +00:00
userBuffers := make(map[string][]string)
var motd MOTD
for {
2015-06-05 22:34:13 +00:00
msg, ok := <-client.Messages
if !ok {
2015-06-05 22:34:13 +00:00
session.deleteIRC(client.Host)
return
}
2015-01-17 01:37:21 +00:00
switch msg.Command {
2015-06-05 22:34:13 +00:00
case irc.Nick:
session.sendJSON("nick", Nick{
2015-06-05 22:34:13 +00:00
Server: client.Host,
Old: msg.Nick,
New: msg.Trailing,
})
2015-06-05 22:34:13 +00:00
channelStore.RenameUser(msg.Nick, msg.Trailing, client.Host)
2015-06-05 22:34:13 +00:00
case irc.Join:
2015-01-17 01:37:21 +00:00
session.sendJSON("join", Join{
2015-06-05 22:34:13 +00:00
Server: client.Host,
User: msg.Nick,
Channels: msg.Params,
2015-01-17 01:37:21 +00:00
})
2015-06-05 22:34:13 +00:00
channelStore.AddUser(msg.Nick, client.Host, msg.Params[0])
2015-01-17 01:37:21 +00:00
2015-06-05 22:34:13 +00:00
if msg.Nick == client.GetNick() {
2015-01-17 01:37:21 +00:00
session.user.AddChannel(storage.Channel{
2015-06-05 22:34:13 +00:00
Server: client.Host,
Name: msg.Params[0],
2015-01-17 01:37:21 +00:00
})
}
2015-06-05 22:34:13 +00:00
case irc.Part:
session.sendJSON("part", Part{
Join: Join{
2015-06-05 22:34:13 +00:00
Server: client.Host,
User: msg.Nick,
Channels: msg.Params,
},
Reason: msg.Trailing,
2015-01-17 01:37:21 +00:00
})
2015-06-05 22:34:13 +00:00
channelStore.RemoveUser(msg.Nick, client.Host, msg.Params[0])
2015-01-17 01:37:21 +00:00
2015-06-05 22:34:13 +00:00
if msg.Nick == client.GetNick() {
session.user.RemoveChannel(client.Host, msg.Params[0])
2015-01-17 01:37:21 +00:00
}
2015-06-05 22:34:13 +00:00
case irc.Mode:
target := msg.Params[0]
if len(msg.Params) > 2 && isChannel(target) {
mode := parseMode(msg.Params[1])
2015-06-05 22:34:13 +00:00
mode.Server = client.Host
mode.Channel = target
mode.User = msg.Params[2]
session.sendJSON("mode", mode)
2015-06-05 22:34:13 +00:00
channelStore.SetMode(client.Host, target, msg.Params[2], mode.Add, mode.Remove)
}
2015-06-05 22:34:13 +00:00
case irc.Privmsg, irc.Notice:
if msg.Params[0] == client.GetNick() {
2015-01-17 01:37:21 +00:00
session.sendJSON("pm", Chat{
2015-06-05 22:34:13 +00:00
Server: client.Host,
From: msg.Nick,
2015-01-17 01:37:21 +00:00
Message: msg.Trailing,
})
} else {
session.sendJSON("message", Chat{
2015-06-05 22:34:13 +00:00
Server: client.Host,
From: msg.Nick,
2015-01-17 01:37:21 +00:00
To: msg.Params[0],
Message: msg.Trailing,
})
}
if msg.Params[0] != "*" {
go session.user.LogMessage(client.Host, msg.Nick, msg.Params[0], msg.Trailing)
}
2015-06-05 22:34:13 +00:00
case irc.Quit:
session.sendJSON("quit", Quit{
2015-06-05 22:34:13 +00:00
Server: client.Host,
User: msg.Nick,
Reason: msg.Trailing,
})
2015-06-05 22:34:13 +00:00
channelStore.RemoveUserAll(msg.Nick, client.Host)
2015-01-17 01:37:21 +00:00
2015-06-05 22:34:13 +00:00
case irc.ReplyWelcome,
irc.ReplyYourHost,
irc.ReplyCreated,
irc.ReplyLUserClient,
irc.ReplyLUserOp,
irc.ReplyLUserUnknown,
irc.ReplyLUserChannels,
irc.ReplyLUserMe:
2015-01-17 01:37:21 +00:00
session.sendJSON("pm", Chat{
2015-06-05 22:34:13 +00:00
Server: client.Host,
From: msg.Nick,
Message: strings.Join(msg.Params[1:], " "),
2015-01-17 01:37:21 +00:00
})
2015-06-05 22:34:13 +00:00
case irc.ReplyWhoisUser:
whois.Nick = msg.Params[1]
whois.Username = msg.Params[2]
whois.Host = msg.Params[3]
whois.Realname = msg.Params[5]
2015-06-05 22:34:13 +00:00
case irc.ReplyWhoisServer:
whois.Server = msg.Params[2]
2015-06-05 22:34:13 +00:00
case irc.ReplyWhoisChannels:
whois.Channels = append(whois.Channels, strings.Split(strings.TrimRight(msg.Trailing, " "), " ")...)
2015-06-05 22:34:13 +00:00
case irc.ReplyEndOfWhois:
session.sendJSON("whois", whois)
whois = WhoisReply{}
2015-06-05 22:34:13 +00:00
case irc.ReplyTopic:
2015-01-17 01:37:21 +00:00
session.sendJSON("topic", Topic{
2015-06-05 22:34:13 +00:00
Server: client.Host,
2015-01-17 01:37:21 +00:00
Channel: msg.Params[1],
Topic: msg.Trailing,
})
2015-06-05 22:34:13 +00:00
channelStore.SetTopic(msg.Trailing, client.Host, msg.Params[1])
2015-06-05 22:34:13 +00:00
case irc.ReplyNamReply:
2015-01-17 01:37:21 +00:00
users := strings.Split(msg.Trailing, " ")
userBuffer := userBuffers[msg.Params[2]]
userBuffers[msg.Params[2]] = append(userBuffer, users...)
2015-06-05 22:34:13 +00:00
case irc.ReplyEndOfNames:
2015-01-17 01:37:21 +00:00
channel := msg.Params[1]
users := userBuffers[channel]
session.sendJSON("users", Userlist{
2015-06-05 22:34:13 +00:00
Server: client.Host,
2015-01-17 01:37:21 +00:00
Channel: channel,
Users: users,
})
2015-06-05 22:34:13 +00:00
channelStore.SetUsers(users, client.Host, channel)
2015-01-17 01:37:21 +00:00
delete(userBuffers, channel)
2015-06-05 22:34:13 +00:00
case irc.ReplyMotdStart:
motd.Server = client.Host
2015-01-17 01:37:21 +00:00
motd.Title = msg.Trailing
2015-06-05 22:34:13 +00:00
case irc.ReplyMotd:
motd.Content = append(motd.Content, msg.Trailing)
2015-01-17 01:37:21 +00:00
2015-06-05 22:34:13 +00:00
case irc.ReplyEndOfMotd:
2015-01-17 01:37:21 +00:00
session.sendJSON("motd", motd)
motd = MOTD{}
default:
2015-06-05 22:34:13 +00:00
printMessage(msg, client)
2015-01-17 01:37:21 +00:00
}
}
}
func parseMode(mode string) *Mode {
m := Mode{}
add := false
for _, c := range mode {
if c == '+' {
add = true
} else if c == '-' {
add = false
} else if add {
m.Add += string(c)
} else {
m.Remove += string(c)
}
}
return &m
}
func isChannel(s string) bool {
return strings.IndexAny(s, "&#+!") == 0
}
2015-06-05 22:34:13 +00:00
func printMessage(msg *irc.Message, i *irc.Client) {
log.Println(i.GetNick()+":", msg.Prefix, msg.Command, msg.Params, msg.Trailing)
2015-01-17 01:37:21 +00:00
}