forked from Depado/go-b0tsec
/
main.go
79 lines (64 loc) · 1.58 KB
/
main.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
package main
import (
"crypto/tls"
"flag"
"log"
"strings"
"github.com/thoj/go-ircevent"
"github.com/depado/go-b0tsec/configuration"
"github.com/depado/go-b0tsec/database"
"github.com/depado/go-b0tsec/plugins"
_ "github.com/depado/go-b0tsec/pluginsinit"
)
func main() {
var err error
cnf := configuration.Config
// Argument parsing
configuration.ConfPath = flag.String("c", "conf.yml", "Local path to configuration file.")
flag.Parse()
// Load the configuration of the bot
configuration.Load()
// Storage initialization
if err := database.BotStorage.Open(); err != nil {
log.Fatalf("something went wrong with the databse : %v", err)
}
plugins.Start()
// Defer all Close/Stop methods
defer database.BotStorage.Close()
defer plugins.Stop()
// Bot initialization
ib := irc.IRC(cnf.BotName, cnf.BotName)
if cnf.TLS {
ib.UseTLS = true
if cnf.InsecureTLS {
ib.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
}
if err = ib.Connect(cnf.Server); err != nil {
log.Fatal(err)
}
// Callback on 'Connected' event
ib.AddCallback("001", func(e *irc.Event) {
ib.Join(cnf.Channel)
})
// Callback on 'Message' event
ib.AddCallback("PRIVMSG", func(e *irc.Event) {
from := e.Nick
to := e.Arguments[0]
m := e.Message()
for _, c := range plugins.Middlewares {
c.Get(ib, from, to, m)
}
if strings.HasPrefix(m, cnf.CommandCharacter) {
if len(m) > 1 {
splitted := strings.Fields(m[1:])
command := splitted[0]
args := splitted[1:]
if p, ok := plugins.Commands[command]; ok {
p.Get(ib, from, to, args)
}
}
}
})
ib.Loop()
}