This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
94 lines (77 loc) · 2.09 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"crypto/tls"
"fmt"
"strings"
"github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"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/plugins/cleverbot"
_ "github.com/depado/go-b0tsec/pluginsinit"
)
func main() {
var err error
cnf := configuration.Config
// Argument parsing
configuration.ConfPath = flag.StringP("config", "c", "conf.yml", "Local path to configuration file.")
flag.Parse()
// Load the configuration of the bot
configuration.Load()
if err = cleverbot.Clever.Initialize(); err != nil {
logrus.WithError(err).Fatal("Couldn't connect cleverbot")
}
// Storage initialization and defering Close
if err := database.BotStorage.Open(); err != nil {
logrus.WithError(err).Fatal("Couldn't connect to the database")
}
defer database.BotStorage.Close()
// Plugins initialization and defering Stop
plugins.Start()
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 {
logrus.WithError(err).Fatal("Couldn't connect to IRC server")
}
// Callback on 'Connected' event
ib.AddCallback("001", func(e *irc.Event) {
ib.Join(cnf.Channel)
})
// Callback on 'Invite' event
ib.AddCallback("INVITE", 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 {
if command == "anon" {
fmt.Println("That's the anon command")
}
p.Get(ib, from, to, args)
fmt.Println("Done !")
}
}
}
})
ib.Loop()
}