2015-01-17 01:37:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-21 02:06:34 +00:00
|
|
|
"log"
|
2015-01-17 01:37:21 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-01-21 21:59:08 +00:00
|
|
|
"github.com/khlieng/name_pending/storage"
|
2015-01-17 01:37:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func handleMessages(irc *IRC, 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
|
|
|
|
|
|
|
|
for msg := range irc.Messages {
|
|
|
|
switch msg.Command {
|
2015-01-31 22:35:38 +00:00
|
|
|
case NICK:
|
|
|
|
session.sendJSON("nick", Nick{
|
|
|
|
Server: irc.Host,
|
|
|
|
Old: msg.Prefix,
|
|
|
|
New: msg.Trailing,
|
|
|
|
})
|
|
|
|
|
|
|
|
channelStore.RenameUser(msg.Prefix, msg.Trailing, irc.Host)
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
case JOIN:
|
2015-01-21 02:06:34 +00:00
|
|
|
user := msg.Prefix
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
session.sendJSON("join", Join{
|
|
|
|
Server: irc.Host,
|
|
|
|
User: user,
|
2015-02-03 22:33:23 +00:00
|
|
|
Channels: msg.Params,
|
2015-01-17 01:37:21 +00:00
|
|
|
})
|
|
|
|
|
2015-02-03 22:33:23 +00:00
|
|
|
channelStore.AddUser(user, irc.Host, msg.Params[0])
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-02-06 23:16:51 +00:00
|
|
|
if user == irc.GetNick() {
|
2015-01-17 01:37:21 +00:00
|
|
|
session.user.AddChannel(storage.Channel{
|
|
|
|
Server: irc.Host,
|
2015-02-03 22:33:23 +00:00
|
|
|
Name: msg.Params[0],
|
2015-01-17 01:37:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
case PART:
|
2015-01-21 02:06:34 +00:00
|
|
|
user := msg.Prefix
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-02-01 00:56:56 +00:00
|
|
|
session.sendJSON("part", Part{
|
|
|
|
Join: Join{
|
|
|
|
Server: irc.Host,
|
|
|
|
User: user,
|
|
|
|
Channels: msg.Params,
|
|
|
|
},
|
|
|
|
Reason: msg.Trailing,
|
2015-01-17 01:37:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
channelStore.RemoveUser(user, irc.Host, msg.Params[0])
|
|
|
|
|
2015-02-06 23:16:51 +00:00
|
|
|
if user == irc.GetNick() {
|
2015-01-17 01:37:21 +00:00
|
|
|
session.user.RemoveChannel(irc.Host, msg.Params[0])
|
|
|
|
}
|
|
|
|
|
2015-01-25 01:35:46 +00:00
|
|
|
case MODE:
|
|
|
|
target := msg.Params[0]
|
|
|
|
if len(msg.Params) > 2 && isChannel(target) {
|
|
|
|
mode := parseMode(msg.Params[1])
|
|
|
|
mode.Server = irc.Host
|
|
|
|
mode.Channel = target
|
|
|
|
mode.User = msg.Params[2]
|
|
|
|
|
|
|
|
session.sendJSON("mode", mode)
|
|
|
|
|
|
|
|
channelStore.SetMode(irc.Host, target, msg.Params[2], mode.Add, mode.Remove)
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
case PRIVMSG, NOTICE:
|
2015-02-06 23:16:51 +00:00
|
|
|
if msg.Params[0] == irc.GetNick() {
|
2015-01-17 01:37:21 +00:00
|
|
|
session.sendJSON("pm", Chat{
|
|
|
|
Server: irc.Host,
|
|
|
|
From: msg.Prefix,
|
|
|
|
Message: msg.Trailing,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
session.sendJSON("message", Chat{
|
|
|
|
Server: irc.Host,
|
|
|
|
From: msg.Prefix,
|
|
|
|
To: msg.Params[0],
|
|
|
|
Message: msg.Trailing,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
case QUIT:
|
2015-01-21 02:06:34 +00:00
|
|
|
user := msg.Prefix
|
|
|
|
|
|
|
|
session.sendJSON("quit", Quit{
|
|
|
|
Server: irc.Host,
|
|
|
|
User: user,
|
2015-02-01 00:56:56 +00:00
|
|
|
Reason: msg.Trailing,
|
2015-01-21 02:06:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
channelStore.RemoveUserAll(user, irc.Host)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
case RPL_WELCOME,
|
|
|
|
RPL_YOURHOST,
|
2015-01-21 02:06:34 +00:00
|
|
|
RPL_CREATED,
|
2015-01-17 01:37:21 +00:00
|
|
|
RPL_LUSERCLIENT,
|
|
|
|
RPL_LUSEROP,
|
|
|
|
RPL_LUSERUNKNOWN,
|
|
|
|
RPL_LUSERCHANNELS,
|
|
|
|
RPL_LUSERME:
|
|
|
|
session.sendJSON("pm", Chat{
|
|
|
|
Server: irc.Host,
|
|
|
|
From: msg.Prefix,
|
2015-02-03 22:33:23 +00:00
|
|
|
Message: strings.Join(msg.Params[1:], " "),
|
2015-01-17 01:37:21 +00:00
|
|
|
})
|
|
|
|
|
2015-02-10 01:25:28 +00:00
|
|
|
case RPL_WHOISUSER:
|
|
|
|
whois.Nick = msg.Params[1]
|
|
|
|
whois.Username = msg.Params[2]
|
|
|
|
whois.Host = msg.Params[3]
|
|
|
|
whois.Realname = msg.Params[5]
|
|
|
|
|
|
|
|
case RPL_WHOISSERVER:
|
|
|
|
whois.Server = msg.Params[2]
|
|
|
|
|
|
|
|
case RPL_WHOISCHANNELS:
|
|
|
|
whois.Channels = append(whois.Channels, strings.Split(strings.TrimRight(msg.Trailing, " "), " ")...)
|
|
|
|
|
|
|
|
case RPL_ENDOFWHOIS:
|
|
|
|
session.sendJSON("whois", whois)
|
|
|
|
|
|
|
|
whois = WhoisReply{}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
case RPL_TOPIC:
|
|
|
|
session.sendJSON("topic", Topic{
|
|
|
|
Server: irc.Host,
|
|
|
|
Channel: msg.Params[1],
|
|
|
|
Topic: msg.Trailing,
|
|
|
|
})
|
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
channelStore.SetTopic(msg.Trailing, irc.Host, msg.Params[1])
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
case RPL_NAMREPLY:
|
|
|
|
users := strings.Split(msg.Trailing, " ")
|
|
|
|
|
2015-01-25 01:35:46 +00:00
|
|
|
/*for i, user := range users {
|
2015-01-17 01:37:21 +00:00
|
|
|
users[i] = strings.TrimLeft(user, "@+")
|
2015-01-25 01:35:46 +00:00
|
|
|
}*/
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
userBuffer := userBuffers[msg.Params[2]]
|
|
|
|
userBuffers[msg.Params[2]] = append(userBuffer, users...)
|
|
|
|
|
|
|
|
case RPL_ENDOFNAMES:
|
|
|
|
channel := msg.Params[1]
|
|
|
|
users := userBuffers[channel]
|
|
|
|
|
|
|
|
session.sendJSON("users", Userlist{
|
|
|
|
Server: irc.Host,
|
|
|
|
Channel: channel,
|
|
|
|
Users: users,
|
|
|
|
})
|
|
|
|
|
|
|
|
channelStore.SetUsers(users, irc.Host, channel)
|
|
|
|
delete(userBuffers, channel)
|
|
|
|
|
|
|
|
case RPL_MOTDSTART:
|
|
|
|
motd.Server = irc.Host
|
|
|
|
motd.Title = msg.Trailing
|
|
|
|
|
|
|
|
case RPL_MOTD:
|
2015-02-01 01:08:38 +00:00
|
|
|
motd.Content = append(motd.Content, msg.Trailing)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
case RPL_ENDOFMOTD:
|
|
|
|
session.sendJSON("motd", motd)
|
|
|
|
|
|
|
|
motd = MOTD{}
|
|
|
|
|
|
|
|
default:
|
|
|
|
printMessage(msg, irc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-17 01:37:21 +00:00
|
|
|
func printMessage(msg *Message, irc *IRC) {
|
2015-02-06 23:16:51 +00:00
|
|
|
log.Println(irc.GetNick()+":", msg.Prefix, msg.Command, msg.Params, msg.Trailing)
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|