-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolem.go
More file actions
92 lines (75 loc) · 2.19 KB
/
golem.go
File metadata and controls
92 lines (75 loc) · 2.19 KB
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
package main
import (
"crypto/tls"
"fmt"
"log"
"os"
"os/signal"
"syscall"
irc "github.com/fluffle/goirc/client"
)
type Golem struct {
conn *irc.Conn
}
// FromConfig creates a new Golem instance from the provided Config.
// A client connection will be configured with the provided server, user and channel information.
// A command handler is registered for dispatching commands received as PRIVMSGs.
func newGolem(cfg *Config) *Golem {
ircCfg := irc.NewConfig(cfg.irc.nick)
ircCfg.SSL = true
ircCfg.SSLConfig = &tls.Config{ServerName: cfg.irc.host}
ircCfg.Server = fmt.Sprintf("%s:%v",
cfg.irc.host, cfg.irc.port)
ircCfg.Pass = cfg.irc.pass
client := irc.Client(ircCfg)
client.HandleFunc(irc.CONNECTED,
func(conn *irc.Conn, line *irc.Line) {
log.Println("Connection established")
log.Println("Requesting capabilities...")
conn.Cap("REQ", "twitch.tv/tags")
log.Println("Joining channels", cfg.irc.channels)
for _, channel := range cfg.irc.channels {
conn.Join(channel)
}
})
commandHandler := newCommandHandler(cfg.cmd.triggers, cfg.dsn, client)
if commandHandler == nil {
log.Println("Failed to setup command handler")
return nil
}
client.HandleFunc(irc.PRIVMSG, commandHandler.handleCommand)
client.HandleFunc(irc.JOIN, commandHandler.joinedChannel)
return &Golem{conn: client}
}
// Run a fully configured IRC golem.
// The method blocks as long as the IRC connection remains established.
// Both SIGINT and SIGTERM are handled and will terminate the IRC connection.
func (g *Golem) run() {
quit := make(chan bool, 1)
g.conn.HandleFunc(irc.DISCONNECTED,
func(conn *irc.Conn, line *irc.Line) {
log.Println("Connection closed. Exiting...")
quit <- true
})
if err := g.conn.Connect(); err != nil {
log.Fatalln("Connection error:", err.Error())
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
for {
select {
case sig := <-sigChan:
log.Println("Got signal:", sig)
// if we're not connected for some reason, quit immediately
if g.conn.Connected() {
log.Println("Closing connection...")
g.conn.Quit()
} else {
log.Println("Exiting...")
quit <- true
}
case <-quit:
return
}
}
}