/
main.go
116 lines (91 loc) · 2.25 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
irc "github.com/fluffle/goirc/client"
"log"
"os"
"strings"
)
type IRCMessage struct {
Channel string
Msg string
}
type IRCChannels struct {
Key string
}
type IRCConfig struct {
Nick string
Password string
Host string
Ssl bool
Skipsslverify bool
Port int
Channels map[string]IRCChannels
BrainKey string
}
func readConfig(configFile string) IRCConfig {
config := IRCConfig{}
file, _ := os.Open(configFile)
decoder := json.NewDecoder(file)
err := decoder.Decode(&config)
if err != nil {
log.Println("Cannot parse config:", err)
os.Exit(1)
}
return config
}
func Bot(config IRCConfig) {
c := irc.NewConfig(config.Nick, config.Nick, config.Nick)
c.SSL = config.Ssl
c.Server = config.Host
c.SSLConfig = &tls.Config{ServerName: c.Server, InsecureSkipVerify: config.Skipsslverify}
bot := irc.Client(c)
quit := make(chan bool)
// on CONNECTED identify with NickServ, set mode to bot, connect to channels in config
bot.HandleFunc(irc.CONNECTED,
func(conn *irc.Conn, line *irc.Line) {
conn.Mode(conn.Me().Nick, "+B")
bot.Privmsgf("NickServ", "identify %s", config.Password)
fmt.Println(line.Raw)
for channel, cinfo := range config.Channels {
if cinfo.Key != "" {
conn.Join(channel + " " + cinfo.Key)
} else {
conn.Join(channel)
}
}
})
bot.HandleFunc(irc.PRIVMSG,
func(conn *irc.Conn, line *irc.Line) {
log.Println(line.Raw)
switch {
case strings.HasPrefix(line.Args[1], "!quit"):
quit <- true
}
})
bot.HandleFunc(irc.DISCONNECTED,
func(conn *irc.Conn, line *irc.Line) { quit <- true })
if err := bot.Connect(); err != nil {
log.Printf("Connection error: %s\n", err.Error())
}
brain := initializeBrain(config.BrainKey)
go danceActions(bot, config.Channels)
go brainActions(bot, brain, config.Channels)
<-quit
}
// sends IRC messages within routines
func sendMsg(bot *irc.Conn, c chan IRCMessage) {
for item := range c {
bot.Privmsg(item.Channel, item.Msg)
log.Println(item.Channel, item.Msg)
}
}
func main() {
configFile := flag.String("config", "config.json", "path to config file")
flag.Parse()
config := readConfig(*configFile)
Bot(config)
}