-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
86 lines (77 loc) · 1.82 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
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"github.com/thoj/go-ircevent"
)
var config = flag.String("config", "", "configuration file")
type Config struct {
Irc struct {
Ssl bool `json:"ssl"`
SslVerifySkip bool `json:"ssl_verify_skip"`
Port string `json:"port"`
Nickname string `json:"nickname"`
Channels []string `json:"channels"`
Host string `json:"host"`
Password string `json:"password"`
} `json:"irc"`
Database struct {
Karma string `json:"karma"`
} `json:"database"`
Logging struct {
Location string `json:"location"`
} `json:"logging"`
}
func (c *Config) Load(filename string) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if err := json.Unmarshal(data, &c); err != nil {
return err
}
if c.Irc.Nickname == "" {
c.Irc.Nickname = "derwanksta"
}
if c.Irc.Host == "" {
return errors.New("host is required.")
}
return nil
}
func main() {
flag.Parse()
c := &Config{}
if err := c.Load(*config); err != nil {
log.Fatal(err)
}
ircbot := irc.IRC(c.Irc.Nickname, c.Irc.Nickname)
ircbot.UseTLS = c.Irc.Ssl
if c.Irc.SslVerifySkip {
ircbot.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
ircbot.Password = c.Irc.Password
fmt.Println("connecting...")
err := ircbot.Connect(net.JoinHostPort(c.Irc.Host, c.Irc.Port))
if err != nil {
log.Fatal(err)
}
fmt.Println("connected")
ircbot.AddCallback("001", func(e *irc.Event) {
for _, channel := range c.Irc.Channels {
ircbot.Join(channel)
log.Println(fmt.Sprintf("Joined: %v", channel))
}
})
ircbot.AddCallback("PRIVMSG", func(e *irc.Event) {
channel := e.Arguments[0]
ircbot.Privmsg(channel, e.Message())
log.Println(fmt.Sprintf("Received Msg: %v", e.Message()))
})
ircbot.Loop()
}