-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
executable file
·390 lines (332 loc) · 8.68 KB
/
bot.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package ircbot
import (
"bufio"
"crypto/rand"
"crypto/tls"
"errors"
"fmt"
"net"
"net/textproto"
"os"
"os/signal"
"strings"
"syscall"
log "github.com/sirupsen/logrus"
db "github.com/zaibon/ircbot/database"
)
// IrcBot represents the bot in general
type IrcBot struct {
// identity
User string
Nick string
// server info
server string
port uint
channels []string
// tcp communication
conn net.Conn
reader *textproto.Reader
writer *textproto.Writer
// crypto
encrypted bool
config tls.Config
// data flow
//channel to send *IrcMsg to the goroutine that handle input message
//You usualy don't need to send anything there. It's useful when creating custom actionner
ChIn chan *IrcMsg
//channel to send *IrcMsg to the goroutine that handle output message
//every *IrcMsg send in this channel will be send to the server
//You usualy don't need to send anything there. It's useful when creating custom actionner
ChOut chan *IrcMsg
//channel to send *IrcMsg to the goroutine that handle errors
ChError chan error
// exit flag
Exit chan bool
//action handlers
handlersIntern map[string][]Actioner //handler of interanl commands
handlersUser map[string]Actioner // handler of commands fired by user
//database
db *db.DB
}
func NewIrcBot(user, nick, server string, port uint, channels []string, DBPath string) *IrcBot {
bot := IrcBot{
User: user,
Nick: nick,
server: server,
port: port,
channels: channels,
handlersIntern: make(map[string][]Actioner),
handlersUser: make(map[string]Actioner),
ChIn: make(chan *IrcMsg),
ChOut: make(chan *IrcMsg),
ChError: make(chan error),
Exit: make(chan bool),
}
//init database
var err error
bot.db, err = db.Open(DBPath)
if err != nil {
log.WithFields(log.Fields{
"db Path": DBPath,
"error": err,
}).Panicln("unable to open database")
}
return &bot
}
/////////////////
//Public function
/////////////////
// Connect connects the bot to the server and joins the channels
func (b *IrcBot) Connect(password string) error {
//launch a go routine that handle errors
// b.handleError()
log.WithField("address", b.url()).Debugln("Connection")
var conn net.Conn
var err error
if b.encrypted {
cert, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
if err != nil {
log.Errorln("error during certificate loading")
return err
}
config := tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{cert},
ServerName: "hello",
}
config.Rand = rand.Reader
conn, err = tls.Dial("tcp", b.url(), &config)
if err != nil {
log.WithField("address", b.url()).Errorln("Dial server")
return err
}
} else {
conn, err = net.Dial("tcp", b.url())
if err != nil {
log.WithField("address", b.url()).Errorln("Dial server")
return err
}
}
b.conn = conn
r := bufio.NewReader(b.conn)
w := bufio.NewWriter(b.conn)
b.reader = textproto.NewReader(r)
b.writer = textproto.NewWriter(w)
//connect to server
b.writer.PrintfLine("USER %s 8 * :%s", b.Nick, b.Nick)
b.writer.PrintfLine("NICK %s", b.Nick)
//launch go routines that handle requests
b.listen()
b.handleActionIn()
b.handleActionOut()
b.handlerError()
b.identify(password)
return nil
}
// Disconnect sends QUIT command to server and closes connections
func (b *IrcBot) Disconnect() {
log.Debugln("disconnection...")
for _, ch := range b.channels {
log.WithField("channel", ch).Debugln("leaving")
b.Say(ch, "See you soon...")
}
b.writer.PrintfLine("QUIT")
b.conn.Close()
log.Debugln("connection close")
// b.Exit <- true
}
// Say makes the bot say text to channel
func (b *IrcBot) Say(channel string, text string) {
msg := NewIrcMsg()
msg.Command = "PRIVMSG"
msg.CmdParams = []string{channel}
msg.Trailing = []string{":", text}
b.ChOut <- msg
}
// AddInternAction add an action to excecute on internal command (join,connect,...)
// command is the internal command to handle, action is an ActionFunc callback
func (b *IrcBot) AddInternAction(a Actioner) {
addAction(a, b.handlersIntern)
}
// AddUserAction add an action fired by the user to handle
// command is the commands send by user, action is an ActionFunc callback
func (b *IrcBot) AddUserAction(a Actioner) {
for _, cmd := range a.Command() {
b.handlersUser[cmd] = a
}
}
// GetActionnersCmds returns all registred user actioners commands
func (b *IrcBot) GetActionnersCmds() []string {
var cmds []string
for cmd, _ := range b.handlersUser {
fmt.Println(cmd)
cmds = append(cmds, cmd)
}
return cmds
}
// GetActionUsage returns the Actioner from the user actions map or return an error if
// no action if found with this name
// Usefull if you want to access actioner information within other actioner
// see Help actionner for example
func (b *IrcBot) GetActioner(actionName string) (Actioner, error) {
actioner, ok := b.handlersUser[actionName]
if !ok {
log.WithField("action name", actionName).Warningln("action not found")
return nil, errors.New("no action found with that name")
}
return actioner, nil
}
// DBConnection return a new connection do the database. Use it if your custom action need to access the database
func (b *IrcBot) DBConnection() (*db.DB, error) {
return db.Open(b.db.Path())
}
// String implements the Stringer interface
func (b *IrcBot) String() string {
s := fmt.Sprintf("server: %s\n", b.server)
s += fmt.Sprintf("port: %d\n", b.port)
s += fmt.Sprintf("ssl: %t\n", b.encrypted)
if len(b.channels) > 0 {
s += "channels: "
for _, v := range b.channels {
s += fmt.Sprintf("%s ", v)
}
}
return s
}
func (b *IrcBot) url() string {
return fmt.Sprintf("%s:%d", b.server, b.port)
}
func (b *IrcBot) join() {
for _, v := range b.channels {
s := fmt.Sprintf("JOIN %s", v)
log.WithField("channel", v).Debugln("join")
b.writer.PrintfLine(s)
}
}
func (b *IrcBot) identify(password string) {
//idenify with nickserv
if password != "" {
s := fmt.Sprintf("PRIVMSG NickServ :identify %s %s", b.Nick, password)
log.WithFields(log.Fields{
"nick": b.Nick,
"passwd": "*****",
}).Debugln("identify")
b.writer.PrintfLine(s)
}
}
func (b *IrcBot) listen() {
go func() {
for {
//block read line from socket
line, err := b.reader.ReadLine()
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Errorln("error reading socket")
b.ChError <- err
}
//convert line into IrcMsg
msg := ParseLine(line)
// end of MODT
if msg.Command == "376" {
b.join()
}
if msg.Command == "PING" {
out := strings.Replace(line, "PING", "PONG", -1)
b.writer.PrintfLine(out)
log.Debugln(out)
}
if msg.Command == "PRIVMSG" || msg.Command == "JOIN" {
b.ChIn <- msg
}
}
}()
}
func addAction(a Actioner, acts map[string][]Actioner) {
if len(a.Command()) > 1 {
for _, cmd := range a.Command() {
acts[cmd] = append(acts[cmd], a)
}
return
}
acts[a.Command()[0]] = append(acts[a.Command()[0]], a)
}
func (b *IrcBot) handleActionIn() {
go func() {
for {
//receive new message
msg := <-b.ChIn
log.WithFields(log.Fields{
"channel": msg.Channel(),
"nick": msg.Nick(),
"command": msg.Command,
"params": strings.Join(msg.CmdParams, " "),
"message": strings.Join(msg.Trailing, " "),
}).Debugln("receive")
//action fired by user
if len(msg.Trailing) > 0 {
actionUser, ok := b.handlersUser[msg.Trailing[0]]
if ok {
if msg.Channel() == b.Nick {
//query message, respond to user, not channel
msg.CmdParams[0] = msg.Nick()
}
actionUser.Do(b, msg)
}
}
//action fired by event
actionsIntern, ok := b.handlersIntern[msg.Command]
if ok && len(actionsIntern) > 0 {
for _, action := range actionsIntern {
action.Do(b, msg)
}
}
}
}()
}
func (b *IrcBot) handleActionOut() {
go func() {
for {
msg := <-b.ChOut
params := strings.Join(msg.CmdParams, " ")
content := strings.Join(msg.Trailing, " ")
s := fmt.Sprintf("%s %s %s", msg.Command, params, content)
b.writer.PrintfLine(s)
log.WithFields(log.Fields{
"channel": msg.Channel(),
"nick": msg.Nick(),
"command": msg.Command,
"params": strings.Join(msg.CmdParams, " "),
"message": strings.Join(msg.Trailing, " "),
}).Debugln("send")
}
}()
}
func (b *IrcBot) handlerError() {
go func() {
for {
err := <-b.ChError
log.WithField("error", err).Errorln("error")
// if err != nil {
// b.Disconnect()
// log.Fatalln("ChError ocurs :", err)
// }
}
}()
}
func (b *IrcBot) signlalHandler() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM)
for {
select {
case <-c:
fmt.Println("disconnection")
b.Disconnect()
}
}
}
func (b *IrcBot) errChk(err error) {
if err != nil {
b.ChError <- err
}
}