-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.go
95 lines (80 loc) · 2.4 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
package main
import (
"github.com/golang/protobuf/proto"
"flag"
"github.com/thoj/go-ircevent"
"io/ioutil"
"log"
"net/http"
)
func logErrors(c chan error) {
var err error
for err = range c {
log.Print("IRC error: ", err)
}
}
func launchhttpserver(bindto string) {
var err error
err = http.ListenAndServe(bindto, nil)
if err != nil {
log.Fatal("Error starting http server: ", err)
}
}
func main() {
var myircbot *irc.Connection
var channelname string
var err error
var extractor *URLTitleExtractor
var github *GitHubAdapter
var configpath string // path of config file
var configdata []byte
var config IRCBotConfig
var msgbuffer *MessageBuffer
flag.StringVar(&configpath, "config", "", "Specify the path to the configuration file.")
flag.Parse()
configdata, err = ioutil.ReadFile(configpath)
if err == nil {
err = proto.Unmarshal(configdata, &config)
if err != nil {
err = proto.UnmarshalText(string(configdata), &config)
}
if err != nil {
log.Fatal("Error parsing config: ", err)
}
} else {
var nerr = proto.UnmarshalText(configpath, &config)
if nerr != nil {
log.Print("Error parsing config: ", nerr)
log.Fatal("Error reading config file: ", err)
}
}
myircbot = irc.IRC(config.GetBotName(), config.GetBotName())
if myircbot == nil {
log.Fatal("Error calling IRC(nick, user string) *Connection. Nick or User empty.")
}
go logErrors(myircbot.ErrorChan()) // collect irc errors and log
// 1: RPL_WELCOME "Welcome to the Internet Relay Network
// <nick>!<user>@<host>"
myircbot.AddCallback("001", func(e *irc.Event) {
//Join all channels.
for _, channelname = range config.GetIrcChannel() {
myircbot.Join(channelname)
}
})
if err = myircbot.Connect(config.GetServerAddress()); err != nil {
log.Fatal("Error connecting to server: ", err)
}
msgbuffer = NewMessageBuffer(myircbot, config.GetSendQueueLength())
extractor = &URLTitleExtractor{
msgbuffer: msgbuffer,
}
github = NewGitHubAdapter(msgbuffer, config.GetGithub())
//Event handling
myircbot.AddCallback("PRIVMSG", logprivmsgs)
myircbot.AddCallback("PRIVMSG", extractor.WriteURLTitle)
// Write GitHub status messages to the specified channels
http.Handle("/github", github)
// Start http server in a new thread
go launchhttpserver(config.GetHttpServerAddress())
myircbot.Loop()
}