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"
|
|
|
|
|
2015-06-05 22:34:13 +00:00
|
|
|
"github.com/khlieng/name_pending/irc"
|
2015-01-21 21:59:08 +00:00
|
|
|
"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) {
|
2015-02-10 01:25:28 +00:00
|
|
|
var whois WhoisReply
|
2015-01-17 01:37:21 +00:00
|
|
|
userBuffers := make(map[string][]string)
|
|
|
|
var motd MOTD
|
|
|
|
|
2015-05-16 01:56:58 +00:00
|
|
|
for {
|
2015-06-05 22:34:13 +00:00
|
|
|
msg, ok := <-client.Messages
|
2015-05-16 01:56:58 +00:00
|
|
|
if !ok {
|
2015-06-05 22:34:13 +00:00
|
|
|
session.deleteIRC(client.Host)
|
2015-05-16 01:56:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
switch msg.Command {
|
2015-06-05 22:34:13 +00:00
|
|
|
case irc.Nick:
|
2015-01-31 22:35:38 +00:00
|
|
|
session.sendJSON("nick", Nick{
|
2015-06-05 22:34:13 +00:00
|
|
|
Server: client.Host,
|
2015-05-31 23:48:07 +00:00
|
|
|
Old: msg.Nick,
|
2015-01-31 22:35:38 +00:00
|
|
|
New: msg.Trailing,
|
|
|
|
})
|
|
|
|
|
2015-06-05 22:34:13 +00:00
|
|
|
channelStore.RenameUser(msg.Nick, msg.Trailing, client.Host)
|
2015-01-31 22:35:38 +00:00
|
|
|
|
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,
|
2015-05-31 23:48:07 +00:00
|
|
|
User: msg.Nick,
|
2015-02-03 22:33:23 +00:00
|
|
|
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,
|
2015-02-03 22:33:23 +00:00
|
|
|
Name: msg.Params[0],
|
2015-01-17 01:37:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:34:13 +00:00
|
|
|
case irc.Part:
|
2015-02-01 00:56:56 +00:00
|
|
|
session.sendJSON("part", Part{
|
|
|
|
Join: Join{
|
2015-06-05 22:34:13 +00:00
|
|
|
Server: client.Host,
|
2015-05-31 23:48:07 +00:00
|
|
|
User: msg.Nick,
|
2015-02-01 00:56:56 +00:00
|
|
|
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:
|
2015-01-25 01:35:46 +00:00
|
|
|
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
|
2015-01-25 01:35:46 +00:00
|
|
|
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-01-25 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2015-05-31 23:48:07 +00:00
|
|
|
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,
|
2015-05-31 23:48:07 +00:00
|
|
|
From: msg.Nick,
|
2015-01-17 01:37:21 +00:00
|
|
|
To: msg.Params[0],
|
|
|
|
Message: msg.Trailing,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
if msg.Params[0] != "*" {
|
2015-06-07 04:16:16 +00:00
|
|
|
go session.user.LogMessage(client.Host, msg.Nick, msg.Params[0], msg.Trailing)
|
2015-04-29 21:54:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 22:34:13 +00:00
|
|
|
case irc.Quit:
|
2015-01-21 02:06:34 +00:00
|
|
|
session.sendJSON("quit", Quit{
|
2015-06-05 22:34:13 +00:00
|
|
|
Server: client.Host,
|
2015-05-31 23:48:07 +00:00
|
|
|
User: msg.Nick,
|
2015-02-01 00:56:56 +00:00
|
|
|
Reason: msg.Trailing,
|
2015-01-21 02:06:34 +00:00
|
|
|
})
|
|
|
|
|
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,
|
2015-05-31 23:48:07 +00:00
|
|
|
From: msg.Nick,
|
2015-02-03 22:33:23 +00:00
|
|
|
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:
|
2015-02-10 01:25:28 +00:00
|
|
|
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:
|
2015-02-10 01:25:28 +00:00
|
|
|
whois.Server = msg.Params[2]
|
|
|
|
|
2015-06-05 22:34:13 +00:00
|
|
|
case irc.ReplyWhoisChannels:
|
2015-02-10 01:25:28 +00:00
|
|
|
whois.Channels = append(whois.Channels, strings.Split(strings.TrimRight(msg.Trailing, " "), " ")...)
|
|
|
|
|
2015-06-05 22:34:13 +00:00
|
|
|
case irc.ReplyEndOfWhois:
|
2015-02-10 01:25:28 +00:00
|
|
|
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-01-21 02:06:34 +00:00
|
|
|
|
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:
|
2015-02-01 01:08:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|