2015-05-01 20:59:46 +00:00
|
|
|
package server
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
import (
|
2015-01-21 02:06:34 +00:00
|
|
|
"log"
|
2015-01-17 01:37:21 +00:00
|
|
|
"strings"
|
2017-05-28 05:20:43 +00:00
|
|
|
"unicode"
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2017-04-20 03:32:22 +00:00
|
|
|
"github.com/kjk/betterguid"
|
|
|
|
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/irc"
|
|
|
|
"github.com/khlieng/dispatch/storage"
|
2015-01-17 01:37:21 +00:00
|
|
|
)
|
|
|
|
|
2017-05-28 22:42:00 +00:00
|
|
|
var excludedErrors = []string{
|
|
|
|
irc.ErrNicknameInUse,
|
|
|
|
}
|
|
|
|
|
2015-06-16 21:20:53 +00:00
|
|
|
type ircHandler struct {
|
|
|
|
client *irc.Client
|
|
|
|
session *Session
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 21:20:53 +00:00
|
|
|
whois WhoisReply
|
|
|
|
userBuffers map[string][]string
|
|
|
|
motdBuffer MOTD
|
|
|
|
|
|
|
|
handlers map[string]func(*irc.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIRCHandler(client *irc.Client, session *Session) *ircHandler {
|
|
|
|
i := &ircHandler{
|
|
|
|
client: client,
|
|
|
|
session: session,
|
|
|
|
userBuffers: make(map[string][]string),
|
|
|
|
}
|
|
|
|
i.initHandlers()
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) run() {
|
2015-05-16 01:56:58 +00:00
|
|
|
for {
|
2016-01-13 17:53:54 +00:00
|
|
|
select {
|
|
|
|
case msg, ok := <-i.client.Messages:
|
|
|
|
if !ok {
|
|
|
|
i.session.deleteIRC(i.client.Host)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
i.dispatchMessage(msg)
|
2015-05-16 01:56:58 +00:00
|
|
|
|
2016-01-13 17:53:54 +00:00
|
|
|
case connected := <-i.client.ConnectionChanged:
|
|
|
|
i.session.sendJSON("connection_update", map[string]bool{
|
|
|
|
i.client.Host: connected,
|
|
|
|
})
|
|
|
|
i.session.setConnectionState(i.client.Host, connected)
|
|
|
|
}
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) dispatchMessage(msg *irc.Message) {
|
2017-05-28 22:42:00 +00:00
|
|
|
if msg.Command[0] == '4' && !isExcludedError(msg.Command) {
|
2017-05-28 05:20:43 +00:00
|
|
|
i.session.printError(formatIRCError(msg))
|
|
|
|
}
|
|
|
|
|
2015-06-16 21:20:53 +00:00
|
|
|
if handler, ok := i.handlers[msg.Command]; ok {
|
|
|
|
handler(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) nick(msg *irc.Message) {
|
|
|
|
i.session.sendJSON("nick", Nick{
|
2017-04-11 01:49:52 +00:00
|
|
|
Server: i.client.Host,
|
|
|
|
Old: msg.Nick,
|
|
|
|
New: msg.LastParam(),
|
2015-06-16 21:20:53 +00:00
|
|
|
})
|
|
|
|
|
2016-12-16 23:11:44 +00:00
|
|
|
channelStore.RenameUser(msg.Nick, msg.LastParam(), i.client.Host)
|
2017-04-11 04:04:59 +00:00
|
|
|
|
|
|
|
if msg.LastParam() == i.client.GetNick() {
|
|
|
|
go i.session.user.SetNick(msg.LastParam(), i.client.Host)
|
|
|
|
}
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) join(msg *irc.Message) {
|
|
|
|
i.session.sendJSON("join", Join{
|
|
|
|
Server: i.client.Host,
|
|
|
|
User: msg.Nick,
|
|
|
|
Channels: msg.Params,
|
|
|
|
})
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
channel := msg.Params[0]
|
|
|
|
channelStore.AddUser(msg.Nick, i.client.Host, channel)
|
2015-06-16 21:20:53 +00:00
|
|
|
|
|
|
|
if msg.Nick == i.client.GetNick() {
|
2017-05-28 05:20:43 +00:00
|
|
|
// Incase no topic is set and theres a cached one that needs to be cleared
|
|
|
|
i.client.Topic(channel)
|
|
|
|
|
|
|
|
i.session.sendLastMessages(i.client.Host, channel, 50)
|
2017-05-20 21:22:56 +00:00
|
|
|
|
2016-01-17 22:33:52 +00:00
|
|
|
go i.session.user.AddChannel(storage.Channel{
|
2015-06-16 21:20:53 +00:00
|
|
|
Server: i.client.Host,
|
2017-05-28 05:20:43 +00:00
|
|
|
Name: channel,
|
2015-06-16 21:20:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) part(msg *irc.Message) {
|
2017-04-04 20:57:01 +00:00
|
|
|
part := Part{
|
|
|
|
Server: i.client.Host,
|
|
|
|
User: msg.Nick,
|
|
|
|
Channel: msg.Params[0],
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(msg.Params) == 2 {
|
|
|
|
part.Reason = msg.Params[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
i.session.sendJSON("part", part)
|
2015-06-16 21:20:53 +00:00
|
|
|
|
2017-04-04 20:57:01 +00:00
|
|
|
channelStore.RemoveUser(msg.Nick, i.client.Host, part.Channel)
|
2015-06-16 21:20:53 +00:00
|
|
|
|
|
|
|
if msg.Nick == i.client.GetNick() {
|
2017-04-04 20:57:01 +00:00
|
|
|
go i.session.user.RemoveChannel(i.client.Host, part.Channel)
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) mode(msg *irc.Message) {
|
|
|
|
target := msg.Params[0]
|
|
|
|
if len(msg.Params) > 2 && isChannel(target) {
|
|
|
|
mode := parseMode(msg.Params[1])
|
|
|
|
mode.Server = i.client.Host
|
|
|
|
mode.Channel = target
|
|
|
|
mode.User = msg.Params[2]
|
|
|
|
|
|
|
|
i.session.sendJSON("mode", mode)
|
|
|
|
|
|
|
|
channelStore.SetMode(i.client.Host, target, msg.Params[2], mode.Add, mode.Remove)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) message(msg *irc.Message) {
|
2017-04-17 20:36:37 +00:00
|
|
|
message := Message{
|
2017-04-20 03:32:22 +00:00
|
|
|
ID: betterguid.New(),
|
2015-06-16 21:20:53 +00:00
|
|
|
Server: i.client.Host,
|
|
|
|
From: msg.Nick,
|
2017-04-17 20:36:37 +00:00
|
|
|
Content: msg.LastParam(),
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Params[0] == i.client.GetNick() {
|
|
|
|
i.session.sendJSON("pm", message)
|
|
|
|
} else {
|
|
|
|
message.To = msg.Params[0]
|
|
|
|
i.session.sendJSON("message", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Params[0] != "*" {
|
2017-04-20 03:32:22 +00:00
|
|
|
go i.session.user.LogMessage(message.ID,
|
|
|
|
i.client.Host, msg.Nick, msg.Params[0], msg.LastParam())
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) quit(msg *irc.Message) {
|
|
|
|
i.session.sendJSON("quit", Quit{
|
2017-04-11 01:49:52 +00:00
|
|
|
Server: i.client.Host,
|
|
|
|
User: msg.Nick,
|
|
|
|
Reason: msg.LastParam(),
|
2015-06-16 21:20:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
channelStore.RemoveUserAll(msg.Nick, i.client.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) info(msg *irc.Message) {
|
2017-04-12 23:50:58 +00:00
|
|
|
if msg.Command == irc.ReplyWelcome {
|
|
|
|
i.session.sendJSON("nick", Nick{
|
|
|
|
Server: i.client.Host,
|
|
|
|
New: msg.Params[0],
|
|
|
|
})
|
|
|
|
|
|
|
|
go i.session.user.SetNick(msg.Params[0], i.client.Host)
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:36:37 +00:00
|
|
|
i.session.sendJSON("pm", Message{
|
2015-06-16 21:20:53 +00:00
|
|
|
Server: i.client.Host,
|
|
|
|
From: msg.Nick,
|
2017-04-17 20:36:37 +00:00
|
|
|
Content: strings.Join(msg.Params[1:], " "),
|
2015-06-16 21:20:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) whoisUser(msg *irc.Message) {
|
|
|
|
i.whois.Nick = msg.Params[1]
|
|
|
|
i.whois.Username = msg.Params[2]
|
|
|
|
i.whois.Host = msg.Params[3]
|
|
|
|
i.whois.Realname = msg.Params[5]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) whoisServer(msg *irc.Message) {
|
|
|
|
i.whois.Server = msg.Params[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) whoisChannels(msg *irc.Message) {
|
2016-12-16 23:11:44 +00:00
|
|
|
i.whois.Channels = append(i.whois.Channels, strings.Split(strings.TrimRight(msg.LastParam(), " "), " ")...)
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) whoisEnd(msg *irc.Message) {
|
2017-05-28 22:52:19 +00:00
|
|
|
if i.whois.Nick != "" {
|
|
|
|
i.session.sendJSON("whois", i.whois)
|
|
|
|
}
|
2015-06-16 21:20:53 +00:00
|
|
|
i.whois = WhoisReply{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) topic(msg *irc.Message) {
|
2017-05-28 05:20:43 +00:00
|
|
|
var channel string
|
|
|
|
var nick string
|
|
|
|
|
|
|
|
if msg.Command == irc.Topic {
|
|
|
|
channel = msg.Params[0]
|
|
|
|
nick = msg.Nick
|
|
|
|
} else {
|
|
|
|
channel = msg.Params[1]
|
|
|
|
}
|
|
|
|
|
2015-06-16 21:20:53 +00:00
|
|
|
i.session.sendJSON("topic", Topic{
|
|
|
|
Server: i.client.Host,
|
2017-05-28 05:20:43 +00:00
|
|
|
Channel: channel,
|
2016-12-16 23:11:44 +00:00
|
|
|
Topic: msg.LastParam(),
|
2017-05-28 05:20:43 +00:00
|
|
|
Nick: nick,
|
|
|
|
})
|
|
|
|
|
|
|
|
channelStore.SetTopic(msg.LastParam(), i.client.Host, channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) noTopic(msg *irc.Message) {
|
|
|
|
channel := msg.Params[1]
|
|
|
|
|
|
|
|
i.session.sendJSON("topic", Topic{
|
|
|
|
Server: i.client.Host,
|
|
|
|
Channel: channel,
|
2015-06-16 21:20:53 +00:00
|
|
|
})
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
channelStore.SetTopic("", i.client.Host, channel)
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) names(msg *irc.Message) {
|
2016-12-16 23:11:44 +00:00
|
|
|
users := strings.Split(strings.TrimSuffix(msg.LastParam(), " "), " ")
|
2015-06-16 21:20:53 +00:00
|
|
|
userBuffer := i.userBuffers[msg.Params[2]]
|
|
|
|
i.userBuffers[msg.Params[2]] = append(userBuffer, users...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) namesEnd(msg *irc.Message) {
|
|
|
|
channel := msg.Params[1]
|
|
|
|
users := i.userBuffers[channel]
|
|
|
|
|
|
|
|
i.session.sendJSON("users", Userlist{
|
|
|
|
Server: i.client.Host,
|
|
|
|
Channel: channel,
|
|
|
|
Users: users,
|
|
|
|
})
|
|
|
|
|
|
|
|
channelStore.SetUsers(users, i.client.Host, channel)
|
|
|
|
delete(i.userBuffers, channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) motdStart(msg *irc.Message) {
|
|
|
|
i.motdBuffer.Server = i.client.Host
|
2016-12-16 23:11:44 +00:00
|
|
|
i.motdBuffer.Title = msg.LastParam()
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) motd(msg *irc.Message) {
|
2016-12-16 23:11:44 +00:00
|
|
|
i.motdBuffer.Content = append(i.motdBuffer.Content, msg.LastParam())
|
2015-06-16 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ircHandler) motdEnd(msg *irc.Message) {
|
|
|
|
i.session.sendJSON("motd", i.motdBuffer)
|
|
|
|
i.motdBuffer = MOTD{}
|
|
|
|
}
|
|
|
|
|
2017-06-21 05:23:07 +00:00
|
|
|
func (i *ircHandler) badNick(msg *irc.Message) {
|
|
|
|
i.session.sendJSON("nick_fail", NickFail{
|
|
|
|
Server: i.client.Host,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-16 21:20:53 +00:00
|
|
|
func (i *ircHandler) initHandlers() {
|
|
|
|
i.handlers = map[string]func(*irc.Message){
|
2017-06-21 05:23:07 +00:00
|
|
|
irc.Nick: i.nick,
|
|
|
|
irc.Join: i.join,
|
|
|
|
irc.Part: i.part,
|
|
|
|
irc.Mode: i.mode,
|
|
|
|
irc.Privmsg: i.message,
|
|
|
|
irc.Notice: i.message,
|
|
|
|
irc.Quit: i.quit,
|
|
|
|
irc.Topic: i.topic,
|
|
|
|
irc.ReplyWelcome: i.info,
|
|
|
|
irc.ReplyYourHost: i.info,
|
|
|
|
irc.ReplyCreated: i.info,
|
|
|
|
irc.ReplyLUserClient: i.info,
|
|
|
|
irc.ReplyLUserOp: i.info,
|
|
|
|
irc.ReplyLUserUnknown: i.info,
|
|
|
|
irc.ReplyLUserChannels: i.info,
|
|
|
|
irc.ReplyLUserMe: i.info,
|
|
|
|
irc.ReplyWhoisUser: i.whoisUser,
|
|
|
|
irc.ReplyWhoisServer: i.whoisServer,
|
|
|
|
irc.ReplyWhoisChannels: i.whoisChannels,
|
|
|
|
irc.ReplyEndOfWhois: i.whoisEnd,
|
|
|
|
irc.ReplyNoTopic: i.noTopic,
|
|
|
|
irc.ReplyTopic: i.topic,
|
|
|
|
irc.ReplyNamReply: i.names,
|
|
|
|
irc.ReplyEndOfNames: i.namesEnd,
|
|
|
|
irc.ReplyMotdStart: i.motdStart,
|
|
|
|
irc.ReplyMotd: i.motd,
|
|
|
|
irc.ReplyEndOfMotd: i.motdEnd,
|
|
|
|
irc.ErrErroneousNickname: i.badNick,
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 01:35:46 +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
|
|
|
|
}
|
|
|
|
|
2017-05-28 22:42:00 +00:00
|
|
|
func isExcludedError(cmd string) bool {
|
|
|
|
for _, err := range excludedErrors {
|
|
|
|
if cmd == err {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
func formatIRCError(msg *irc.Message) string {
|
|
|
|
errMsg := strings.TrimSuffix(msg.LastParam(), ".")
|
|
|
|
if len(msg.Params) > 2 {
|
|
|
|
for _, c := range msg.LastParam() {
|
|
|
|
if unicode.IsLower(c) {
|
|
|
|
return msg.Params[1] + " " + errMsg
|
|
|
|
}
|
|
|
|
return msg.Params[1] + ": " + errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errMsg
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:34:13 +00:00
|
|
|
func printMessage(msg *irc.Message, i *irc.Client) {
|
2016-12-16 23:11:44 +00:00
|
|
|
log.Println(i.GetNick()+":", msg.Prefix, msg.Command, msg.Params, msg.LastParam())
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|