-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (120 loc) · 4.48 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"flag"
"fmt"
"log"
"strings"
"github.com/rakyll/globalconf"
"github.com/uovobw/tapiro/commands"
"github.com/uovobw/tapiro/logger"
"github.com/uovobw/tapiro/tumblr"
"github.com/uovobw/tapiro/twitter"
"github.com/whyrusleeping/hellabot"
)
const configFile = "tapiro.cfg"
var (
ircNetwork = flag.String("network", "chat.freenode.net", "The irc channel to connect to")
ircPort = flag.Int("port", 6697, "Port of the host to connect to")
ircSsl = flag.Bool("ssl", true, "Use ssl")
ircNickname = flag.String("nick", "tapiro", "Nickname of the bot")
ircPassword = flag.String("password", "secret", "Password of the bot")
ircChannels = flag.String("channels", "##somechannel", "Comma separated list of channels to join")
debug = flag.Bool("debug", false, "Turn debug on")
tumblrConf = flag.NewFlagSet("tumblr", flag.ExitOnError)
tumblrAppKey = tumblrConf.String("appkey", "_", "Tumblr Application Key")
tumblrAppSecret = tumblrConf.String("appsecret", "_", "Tumblr Application Secret")
tumblrOauthToken = tumblrConf.String("oauthtoken", "_", "Tumblr OAuth Token")
tumblrOauthSecret = tumblrConf.String("oauthsecret", "_", "Tumblr OAuth Token Secret")
tumblrUrl = tumblrConf.String("url", "_", "Tumblr url")
twitterConf = flag.NewFlagSet("twitter", flag.ExitOnError)
twitterAppKey = twitterConf.String("appkey", "_", "Twitter Application Key")
twitterAppSecret = twitterConf.String("appsecret", "_", "Twitter Application Secret")
twitterOauthToken = twitterConf.String("oauthtoken", "_", "Twitter OAuth Token")
twitterOauthSecret = twitterConf.String("oauthsecret", "_", "Twitter OAuth Token Secret")
loggerConf = flag.NewFlagSet("logger", flag.ExitOnError)
loggerLocation = loggerConf.String("location", "_", "Logfile location")
loggerChannel = loggerConf.String("channel", "_", "Channel for which to log messages")
commandsConf = flag.NewFlagSet("commands", flag.ExitOnError)
commandsIdentifier = commandsConf.String("identifier", "_", "Command prefix identifier")
commandsDb = commandsConf.String("db", "_", "Db location")
)
var config globalconf.GlobalConf
var chanmap map[string]*hbot.IrcChannel
func init() {
chanmap = make(map[string]*hbot.IrcChannel)
config, err := globalconf.NewWithOptions(&globalconf.Options{
Filename: configFile,
})
if err != nil {
log.Fatalf("Cannot parse configuration file: %s", err)
}
globalconf.Register("tumblr", tumblrConf)
globalconf.Register("twitter", twitterConf)
globalconf.Register("logger", loggerConf)
globalconf.Register("commands", commandsConf)
config.ParseAll()
}
func main() {
if *debug {
log.Println(*ircNetwork)
log.Println(*ircChannels)
log.Println(*ircPort)
log.Println(*ircSsl)
log.Println(*ircNickname)
log.Println(*ircPassword)
log.Println(*ircChannels)
log.Println(*tumblrAppKey)
log.Println(*tumblrAppSecret)
log.Println(*tumblrOauthToken)
log.Println(*tumblrOauthSecret)
log.Println(*tumblrUrl)
log.Println(*twitterAppKey)
log.Println(*twitterAppSecret)
log.Println(*twitterOauthToken)
log.Println(*twitterOauthSecret)
log.Println(*loggerLocation)
log.Println(*loggerChannel)
log.Println(*commandsDb)
log.Println(*commandsIdentifier)
}
bot, err := hbot.NewIrcConnection(fmt.Sprintf("%s:%d", *ircNetwork, *ircPort), *ircNickname, *ircSsl)
if err != nil {
log.Fatalf("Cannot create irc bot: %s", err)
}
if *ircPassword != "" {
bot.Password = *ircPassword
}
// Configure plugins
err = twitter.Configure(*twitterAppKey, *twitterAppSecret, *twitterOauthToken, *twitterOauthSecret)
if err != nil {
log.Fatalf("Configuration error: %s", err)
}
err = tumblr.Configure(*tumblrAppKey, *tumblrAppSecret, *tumblrOauthToken, *tumblrOauthSecret, *tumblrUrl)
if err != nil {
log.Fatalf("Configuration error: %s", err)
}
err = logger.Configure(*loggerChannel, *loggerLocation)
if err != nil {
log.Fatalf("Configuration error: %s", err)
}
err = commands.Configure(*commandsIdentifier, *commandsDb, *ircNickname, bot)
if err != nil {
log.Fatalf("Configuration error: %s", err)
}
bot.AddTrigger(commands.GetTrigger())
bot.AddTrigger(twitter.GetTrigger())
bot.AddTrigger(tumblr.GetTrigger())
bot.AddTrigger(logger.GetTrigger())
bot.Start()
// parse the list of channels
for _, channel := range strings.Split(*ircChannels, ",") {
log.Printf("Joining channel: %s\n", channel)
chanmap[channel] = bot.Join(channel)
}
for msg := range bot.Incoming {
if msg == nil {
log.Println("Disconnected")
return
}
}
}