2015-05-01 20:59:46 +00:00
|
|
|
package server
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
import (
|
2016-01-11 20:04:57 +00:00
|
|
|
"crypto/tls"
|
2015-01-17 01:37:21 +00:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2015-04-29 21:54:44 +00:00
|
|
|
"strings"
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/gorilla/websocket"
|
2015-05-01 22:20:22 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
type wsHandler struct {
|
2015-12-29 21:23:07 +00:00
|
|
|
ws *wsConn
|
|
|
|
session *Session
|
|
|
|
addr string
|
2015-06-16 22:46:58 +00:00
|
|
|
handlers map[string]func([]byte)
|
|
|
|
}
|
2015-06-05 22:34:13 +00:00
|
|
|
|
2015-12-29 21:23:07 +00:00
|
|
|
func newWSHandler(conn *websocket.Conn, uuid string) *wsHandler {
|
2015-06-16 22:46:58 +00:00
|
|
|
h := &wsHandler{
|
|
|
|
ws: newWSConn(conn),
|
|
|
|
addr: conn.RemoteAddr().String(),
|
|
|
|
}
|
2015-12-29 21:23:07 +00:00
|
|
|
h.init(uuid)
|
2015-06-16 22:46:58 +00:00
|
|
|
h.initHandlers()
|
|
|
|
return h
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) run() {
|
|
|
|
defer h.ws.close()
|
|
|
|
go h.ws.send()
|
|
|
|
go h.ws.recv()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
for {
|
2015-06-16 22:46:58 +00:00
|
|
|
req, ok := <-h.ws.in
|
2015-06-05 22:34:13 +00:00
|
|
|
if !ok {
|
2015-06-16 22:46:58 +00:00
|
|
|
if h.session != nil {
|
|
|
|
h.session.deleteWS(h.addr)
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
h.dispatchRequest(req)
|
|
|
|
}
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) dispatchRequest(req WSRequest) {
|
|
|
|
if handler, ok := h.handlers[req.Type]; ok {
|
|
|
|
handler(req.Data)
|
|
|
|
}
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-12-29 21:23:07 +00:00
|
|
|
func (h *wsHandler) init(uuid string) {
|
|
|
|
log.Println(h.addr, "set UUID", uuid)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
sessionLock.Lock()
|
2015-12-29 21:23:07 +00:00
|
|
|
if storedSession, exists := sessions[uuid]; exists {
|
2015-06-16 22:46:58 +00:00
|
|
|
sessionLock.Unlock()
|
|
|
|
h.session = storedSession
|
|
|
|
h.session.setWS(h.addr, h.ws)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
log.Println(h.addr, "attached to", h.session.numIRC(), "existing IRC connections")
|
2015-05-04 21:52:34 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
channels := h.session.user.GetChannels()
|
|
|
|
for i, channel := range channels {
|
|
|
|
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
h.session.sendJSON("channels", channels)
|
|
|
|
h.session.sendJSON("servers", h.session.user.GetServers())
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
for _, channel := range channels {
|
|
|
|
h.session.sendJSON("users", Userlist{
|
|
|
|
Server: channel.Server,
|
|
|
|
Channel: channel.Name,
|
|
|
|
Users: channelStore.GetUsers(channel.Server, channel.Name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
h.session = NewSession()
|
2015-12-29 21:23:07 +00:00
|
|
|
h.session.user = storage.NewUser(uuid)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-12-29 21:23:07 +00:00
|
|
|
sessions[uuid] = h.session
|
2015-06-16 22:46:58 +00:00
|
|
|
sessionLock.Unlock()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
h.session.setWS(h.addr, h.ws)
|
|
|
|
h.session.sendJSON("servers", nil)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
go h.session.write()
|
|
|
|
}
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) connect(b []byte) {
|
|
|
|
var data Connect
|
|
|
|
json.Unmarshal(b, &data)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if _, ok := h.session.getIRC(data.Server); !ok {
|
|
|
|
log.Println(h.addr, "connecting to", data.Server)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
i := irc.NewClient(data.Nick, data.Username)
|
|
|
|
i.TLS = data.TLS
|
|
|
|
i.Password = data.Password
|
|
|
|
i.Realname = data.Realname
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2016-01-11 20:04:57 +00:00
|
|
|
if h.session.user.Certificate != nil {
|
|
|
|
i.TLSConfig = &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{*h.session.user.Certificate},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if idx := strings.Index(data.Server, ":"); idx < 0 {
|
|
|
|
h.session.setIRC(data.Server, i)
|
|
|
|
} else {
|
|
|
|
h.session.setIRC(data.Server[:idx], i)
|
|
|
|
}
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
i.Connect(data.Server)
|
|
|
|
go newIRCHandler(i, h.session).run()
|
|
|
|
|
|
|
|
h.session.user.AddServer(storage.Server{
|
|
|
|
Name: data.Name,
|
|
|
|
Address: i.Host,
|
|
|
|
TLS: data.TLS,
|
|
|
|
Password: data.Password,
|
|
|
|
Nick: data.Nick,
|
|
|
|
Username: data.Username,
|
|
|
|
Realname: data.Realname,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log.Println(h.addr, "already connected to", data.Server)
|
|
|
|
}
|
|
|
|
}
|
2015-01-21 02:06:34 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) join(b []byte) {
|
|
|
|
var data Join
|
|
|
|
json.Unmarshal(b, &data)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Join(data.Channels...)
|
|
|
|
}
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) part(b []byte) {
|
|
|
|
var data Part
|
|
|
|
json.Unmarshal(b, &data)
|
2015-02-04 01:43:49 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Part(data.Channels...)
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 01:43:49 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) quit(b []byte) {
|
|
|
|
var data Quit
|
|
|
|
json.Unmarshal(b, &data)
|
2015-02-04 01:43:49 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Quit()
|
|
|
|
h.session.deleteIRC(data.Server)
|
|
|
|
channelStore.RemoveUserAll(i.GetNick(), data.Server)
|
|
|
|
h.session.user.RemoveServer(data.Server)
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) chat(b []byte) {
|
|
|
|
var data Chat
|
|
|
|
json.Unmarshal(b, &data)
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Privmsg(data.To, data.Message)
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) nick(b []byte) {
|
|
|
|
var data Nick
|
|
|
|
json.Unmarshal(b, &data)
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Nick(data.New)
|
|
|
|
h.session.user.SetNick(data.New, data.Server)
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) invite(b []byte) {
|
|
|
|
var data Invite
|
|
|
|
json.Unmarshal(b, &data)
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Invite(data.User, data.Channel)
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) kick(b []byte) {
|
|
|
|
var data Invite
|
|
|
|
json.Unmarshal(b, &data)
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Kick(data.Channel, data.User)
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) whois(b []byte) {
|
|
|
|
var data Whois
|
|
|
|
json.Unmarshal(b, &data)
|
2015-02-21 12:06:05 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Whois(data.User)
|
|
|
|
}
|
|
|
|
}
|
2015-02-21 12:06:05 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) away(b []byte) {
|
|
|
|
var data Away
|
|
|
|
json.Unmarshal(b, &data)
|
2015-02-21 12:06:05 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Away(data.Message)
|
|
|
|
}
|
|
|
|
}
|
2015-05-10 23:09:59 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) search(b []byte) {
|
|
|
|
go func() {
|
|
|
|
var data SearchRequest
|
|
|
|
json.Unmarshal(b, &data)
|
2015-05-10 23:09:59 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
results, err := h.session.user.SearchMessages(data.Server, data.Channel, data.Phrase)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2015-05-10 23:09:59 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
h.session.sendJSON("search", SearchResult{
|
|
|
|
Server: data.Server,
|
|
|
|
Channel: data.Channel,
|
|
|
|
Results: results,
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
2015-05-10 23:09:59 +00:00
|
|
|
|
2016-01-11 20:04:57 +00:00
|
|
|
func (h *wsHandler) cert(b []byte) {
|
|
|
|
var data ClientCert
|
|
|
|
json.Unmarshal(b, &data)
|
|
|
|
|
|
|
|
err := h.session.user.SetCertificate(data.Cert, data.Key)
|
|
|
|
if err != nil {
|
|
|
|
h.session.sendJSON("cert_fail", Error{Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.session.sendJSON("cert_success", nil)
|
|
|
|
}
|
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) initHandlers() {
|
|
|
|
h.handlers = map[string]func([]byte){
|
|
|
|
"connect": h.connect,
|
|
|
|
"join": h.join,
|
|
|
|
"part": h.part,
|
|
|
|
"quit": h.quit,
|
|
|
|
"chat": h.chat,
|
|
|
|
"nick": h.nick,
|
|
|
|
"invite": h.invite,
|
|
|
|
"kick": h.kick,
|
|
|
|
"whois": h.whois,
|
|
|
|
"away": h.away,
|
|
|
|
"search": h.search,
|
2016-01-11 20:04:57 +00:00
|
|
|
"cert": h.cert,
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
}
|