2015-05-01 20:59:46 +00:00
|
|
|
package server
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2017-04-19 23:51:55 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-03-01 00:51:26 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2017-04-20 03:32:22 +00:00
|
|
|
"github.com/kjk/betterguid"
|
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
|
|
|
|
2017-04-19 23:51:55 +00:00
|
|
|
func newWSHandler(conn *websocket.Conn, session *Session, r *http.Request) *wsHandler {
|
2015-06-16 22:46:58 +00:00
|
|
|
h := &wsHandler{
|
2016-01-15 01:27:30 +00:00
|
|
|
ws: newWSConn(conn),
|
|
|
|
session: session,
|
|
|
|
addr: conn.RemoteAddr().String(),
|
2015-06-16 22:46:58 +00:00
|
|
|
}
|
2017-04-19 23:51:55 +00:00
|
|
|
h.init(r)
|
2016-01-26 21:10:44 +00:00
|
|
|
h.initHandlers()
|
2015-06-16 22:46:58 +00:00
|
|
|
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
|
|
|
|
2017-04-19 23:51:55 +00:00
|
|
|
func (h *wsHandler) init(r *http.Request) {
|
2016-01-15 01:27:30 +00:00
|
|
|
h.session.setWS(h.addr, h.ws)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
log.Println(h.addr, "[Session] User ID:", h.session.user.ID, "|",
|
|
|
|
h.session.numIRC(), "IRC connections |",
|
|
|
|
h.session.numWS(), "WebSocket connections")
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
channels := h.session.user.GetChannels()
|
2017-06-13 02:25:59 +00:00
|
|
|
path := r.URL.EscapedPath()[3:]
|
|
|
|
pathServer, pathChannel := getTabFromPath(path)
|
|
|
|
cookieServer, cookieChannel := parseTabCookie(r, path)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
for _, channel := range channels {
|
2017-06-13 02:25:59 +00:00
|
|
|
if (channel.Server == pathServer && channel.Name == pathChannel) ||
|
|
|
|
(channel.Server == cookieServer && channel.Name == cookieChannel) {
|
2017-04-19 23:51:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
h.session.sendJSON("users", Userlist{
|
|
|
|
Server: channel.Server,
|
|
|
|
Channel: channel.Name,
|
|
|
|
Users: channelStore.GetUsers(channel.Server, channel.Name),
|
|
|
|
})
|
2017-04-19 23:51:55 +00:00
|
|
|
|
2017-05-20 21:22:56 +00:00
|
|
|
h.session.sendLastMessages(channel.Server, channel.Name, 50)
|
2015-06-16 22:46:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) connect(b []byte) {
|
2017-07-02 01:31:00 +00:00
|
|
|
var data Server
|
2015-06-16 22:46:58 +00:00
|
|
|
json.Unmarshal(b, &data)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2017-07-02 01:31:00 +00:00
|
|
|
if _, ok := h.session.getIRC(data.Host); !ok {
|
2016-01-15 01:27:30 +00:00
|
|
|
log.Println(h.addr, "[IRC] Add server", data.Server)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2017-07-02 01:31:00 +00:00
|
|
|
connectIRC(data.Server, h.session)
|
2016-01-27 17:08:20 +00:00
|
|
|
|
2017-07-02 01:31:00 +00:00
|
|
|
go h.session.user.AddServer(data.Server)
|
2015-06-16 22:46:58 +00:00
|
|
|
} else {
|
2017-07-02 01:31:00 +00:00
|
|
|
log.Println(h.addr, "[IRC]", data.Host, "already added")
|
2015-06-16 22:46:58 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-07-03 05:35:38 +00:00
|
|
|
log.Println(h.addr, "[IRC] Remove server", data.Server)
|
2015-06-16 22:46:58 +00:00
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
h.session.deleteIRC(data.Server)
|
2017-07-03 05:35:38 +00:00
|
|
|
i.Quit()
|
2015-06-16 22:46:58 +00:00
|
|
|
}
|
2017-07-03 05:35:38 +00:00
|
|
|
|
|
|
|
go h.session.user.RemoveServer(data.Server)
|
2015-06-16 22:46:58 +00:00
|
|
|
}
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2017-04-17 20:36:37 +00:00
|
|
|
func (h *wsHandler) message(b []byte) {
|
|
|
|
var data Message
|
2015-06-16 22:46:58 +00:00
|
|
|
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 {
|
2017-04-17 20:36:37 +00:00
|
|
|
i.Privmsg(data.To, data.Content)
|
2017-04-20 03:32:22 +00:00
|
|
|
|
|
|
|
go h.session.user.LogMessage(betterguid.New(),
|
|
|
|
data.Server, i.GetNick(), data.To, data.Content)
|
2015-06-16 22:46:58 +00:00
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 01:25:28 +00:00
|
|
|
|
2017-05-28 05:20:43 +00:00
|
|
|
func (h *wsHandler) topic(b []byte) {
|
|
|
|
var data Topic
|
|
|
|
json.Unmarshal(b, &data)
|
|
|
|
|
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Topic(data.Channel, data.Topic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-01-27 19:48:47 +00:00
|
|
|
func (h *wsHandler) raw(b []byte) {
|
|
|
|
var data Raw
|
|
|
|
json.Unmarshal(b, &data)
|
|
|
|
|
|
|
|
if i, ok := h.session.getIRC(data.Server); ok {
|
|
|
|
i.Write(data.Message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
func (h *wsHandler) fetchMessages(b []byte) {
|
|
|
|
var data FetchMessages
|
|
|
|
json.Unmarshal(b, &data)
|
|
|
|
|
2017-05-20 21:22:56 +00:00
|
|
|
h.session.sendMessages(data.Server, data.Channel, 200, data.Next)
|
2017-05-02 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 04:18:32 +00:00
|
|
|
func (h *wsHandler) setServerName(b []byte) {
|
2017-07-02 01:31:00 +00:00
|
|
|
var data ServerName
|
2017-06-12 04:18:32 +00:00
|
|
|
json.Unmarshal(b, &data)
|
|
|
|
|
|
|
|
if isValidServerName(data.Name) {
|
|
|
|
h.session.user.SetServerName(data.Name, data.Server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 22:46:58 +00:00
|
|
|
func (h *wsHandler) initHandlers() {
|
|
|
|
h.handlers = map[string]func([]byte){
|
2017-06-12 04:18:32 +00:00
|
|
|
"connect": h.connect,
|
|
|
|
"join": h.join,
|
|
|
|
"part": h.part,
|
|
|
|
"quit": h.quit,
|
|
|
|
"message": h.message,
|
|
|
|
"nick": h.nick,
|
|
|
|
"topic": h.topic,
|
|
|
|
"invite": h.invite,
|
|
|
|
"kick": h.kick,
|
|
|
|
"whois": h.whois,
|
|
|
|
"away": h.away,
|
|
|
|
"raw": h.raw,
|
|
|
|
"search": h.search,
|
|
|
|
"cert": h.cert,
|
|
|
|
"fetch_messages": h.fetchMessages,
|
|
|
|
"set_server_name": h.setServerName,
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-12 04:18:32 +00:00
|
|
|
|
|
|
|
func isValidServerName(name string) bool {
|
|
|
|
return strings.TrimSpace(name) != ""
|
|
|
|
}
|