/
server.go
135 lines (128 loc) · 3.17 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package ircslack
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net"
"strings"
"github.com/slack-go/slack"
)
// Server is the server object that exposes the Slack API with an IRC interface.
type Server struct {
Name string
LocalAddr net.Addr
Listener net.Listener
SlackAPIKey string
SlackDebug bool
ChunkSize int
FileDownloadLocation string
FileProxyPrefix string
Pagination int
TLSConfig *tls.Config
}
// Start runs the IRC server
func (s Server) Start() error {
var err error
if s.TLSConfig != nil {
s.Listener, err = tls.Listen("tcp", s.LocalAddr.String(), s.TLSConfig)
} else {
s.Listener, err = net.Listen("tcp", s.LocalAddr.String())
}
if err != nil {
return err
}
defer s.Listener.Close()
log.Infof("Listening on %v", s.LocalAddr)
for {
conn, err := s.Listener.Accept()
if err != nil {
return fmt.Errorf("Error accepting: %v", err)
}
go s.HandleRequest(conn)
}
}
// HandleRequest handle IRC client connections
func (s Server) HandleRequest(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
for {
line, err := reader.ReadString('\n')
if err != nil {
// clean up this client's state
delete(UserContexts, conn.RemoteAddr())
if err == io.EOF {
log.Warningf("Client %v disconnected", conn.RemoteAddr())
break
}
log.Warningf("Error handling connection from %v: %v", conn.RemoteAddr(), err)
break
}
s.HandleMsg(conn, string(line))
}
}
// HandleMsg handles raw IRC messages
func (s *Server) HandleMsg(conn net.Conn, msg string) {
if strings.HasPrefix(msg, "PASS ") {
log.Debugf("%v: PASS ***** (redacted for privacy)", conn.RemoteAddr())
} else {
log.Debugf("%v: %v", conn.RemoteAddr(), msg)
}
if len(msg) < 1 {
log.Warningf("Invalid message: '%v'", msg)
return
}
var (
prefix, data string
)
if msg[0] == ':' {
prefix = strings.SplitN(msg[1:], " ", 1)[0]
data = msg[len(prefix)+1:]
} else {
prefix = ""
data = msg
}
if !strings.HasSuffix(data, "\r\n") {
log.Warning("Invalid data: not terminated with <CR><LF>")
return
}
data = data[:len(data)-2]
tokens := strings.Split(data, " ")
cmd := tokens[0]
args := tokens[1:]
var trailing string
for idx, arg := range args {
if strings.HasPrefix(arg, ":") {
trailing = strings.Join(args[idx:], " ")[1:]
args = args[:idx]
break
}
}
handler, ok := IrcCommandHandlers[cmd]
if !ok {
log.Warningf("No handler found for %v", cmd)
return
}
ctx, ok := UserContexts[conn.RemoteAddr()]
if !ok || ctx == nil {
ctx = &IrcContext{
Conn: conn,
ServerName: s.Name,
SlackAPIKey: s.SlackAPIKey,
SlackDebug: s.SlackDebug,
ChunkSize: s.ChunkSize,
postMessage: make(chan SlackPostMessage),
conversationCache: make(map[string]*slack.Channel),
FileHandler: &FileHandler{
SlackAPIKey: s.SlackAPIKey,
FileDownloadLocation: s.FileDownloadLocation,
ProxyPrefix: s.FileProxyPrefix,
},
Users: NewUsers(s.Pagination),
Channels: NewChannels(s.Pagination),
}
go ctx.Start()
UserContexts[conn.RemoteAddr()] = ctx
}
handler(ctx, prefix, cmd, args, trailing)
}