forked from fridim/cabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
189 lines (167 loc) · 3.45 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
)
type Bot struct {
Server string
ssl bool
Conn net.Conn
Reader *bufio.Reader
plugins []*Plugin
mutex *sync.Mutex
reconnectMutex *sync.Mutex
pluginsMutex *sync.Mutex
toConn chan string
toStderr chan string
}
func dispatcher(bot *Bot, line string) {
logIn.Print(line)
bot.pluginsMutex.Lock()
for _, plugin := range bot.plugins {
if plugin != nil {
fmt.Fprintln(plugin.stdin, line)
}
}
bot.pluginsMutex.Unlock()
}
func (bot *Bot) connect() error {
if bot.ssl {
conn, err := tls.Dial("tcp", bot.Server, nil)
if err != nil {
return err
}
bot.Conn = conn
bot.Reader = bufio.NewReader(conn)
} else {
conn, err := net.Dial("tcp", bot.Server)
if err != nil {
return err
}
bot.Conn = conn
bot.Reader = bufio.NewReader(conn)
}
return nil
}
func (bot *Bot) disconnect() error {
bot.Reader = nil
bot.Conn.Close()
return nil
}
var reconnecting bool = false
func (bot *Bot) reconnect() {
if reconnecting {
// already reconnecting, just wait
bot.reconnectMutex.Lock()
bot.reconnectMutex.Unlock()
return
}
bot.reconnectMutex.Lock()
reconnecting = true
log.Println("reconnecting...")
bot.disconnect()
delay := 2 * time.Second
for {
err := bot.connect()
if err == nil {
break
}
logErr.Println(err)
log.Printf("Reconnecting in %s seconds...\n", delay)
time.Sleep(delay)
delay = delay * 2
}
reconnecting = false
bot.reconnectMutex.Unlock()
}
var logErr *log.Logger
var logIn *log.Logger
var logOut *log.Logger
func (bot *Bot) consume() {
defer wg.Done()
for {
select {
case line := <-bot.toConn:
bot.mutex.Lock()
fmt.Fprint(bot.Conn, line)
// Do not output passwords
if !strings.Contains(line, "PRIVMSG Nickserv :identify") {
logOut.Print(line)
}
bot.mutex.Unlock()
case line := <-bot.toStderr:
logErr.Print(line)
}
}
}
func main() {
logErr = log.New(os.Stderr, "!!! ", log.LstdFlags)
logOut = log.New(os.Stdout, "--> ", log.LstdFlags)
logIn = log.New(os.Stdout, "<-- ", log.LstdFlags)
log.SetPrefix("iii ")
server := flag.String("server", "chat.freenode.net:6667", "IRC server address to connect to")
ssl := flag.Bool("ssl", false, "use SSL")
flag.Parse()
bot := &Bot{
Server: *server,
ssl: *ssl,
mutex: &sync.Mutex{},
reconnectMutex: &sync.Mutex{},
pluginsMutex: &sync.Mutex{},
}
bot.toConn = make(chan string)
bot.toStderr = make(chan string)
bot.connect()
// consume chan toConn and toStderr
go bot.consume()
// signals
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGHUP, syscall.SIGUSR1)
go func() {
for {
sig := <-sigs
switch sig {
case os.Interrupt:
case syscall.SIGHUP:
fmt.Println("Signal HUP received")
bot.reloadAllPlugins()
case syscall.SIGUSR1:
fmt.Println("Signal USR1 received")
bot.reconnect()
default:
fmt.Println("unhandled signal")
}
}
}()
bot.LoadAllPlugins()
// Stdin loop: allow user to chat with server directly
// (useful for debugging)
go func() {
stdin := bufio.NewReader(os.Stdin)
for {
line, _ := stdin.ReadString('\n')
bot.toConn <- line
}
}()
// Main loop
for {
line, err := bot.Reader.ReadString('\n')
if err != nil {
bot.reconnect()
fmt.Println(err)
time.Sleep(2 * time.Second)
} else {
dispatcher(bot, line)
}
}
}