/
weierbot.go
303 lines (268 loc) · 7.37 KB
/
weierbot.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) 2012 by The Weierbot Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
irc "github.com/fluffle/goirc/client"
"html/template"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
)
var (
timezone = time.UTC
wolframURL = "http://www.wolframalpha.com/input/?i=%s"
mathbinPreviewURL = "http://mathbin.net/preview.cgi?body=%s"
mathbinURL = "http://mathbin.net/%s"
reMathbin = regexp.MustCompile(`equation_previews/[\d_]+\.png`)
)
func init() {
tz, err := time.LoadLocation("Europe/Vienna")
if err != nil {
log.Fatal(err)
}
timezone = tz
}
var commands map[string]func(*WeierBot, string, Message)
var tmpl = template.Must(template.New("weierbot").Parse(`<!doctype html>
<html>
<head>
<title>{{.Title}} - Weierbot</title>
<style>a { color: #111; }</style>
</head>
<body>
<div style="float:right;">
{{if .Prev}}<a style="color:#FF1A00;" href="/{{.Prev}}">Previous</a>{{end}}
{{if .Next}}<a style="color:#FF1A00;" href="/{{.Next}}">Next</a>{{end}}
</div>
<h1 style="color:#111; font-size: 1.2em; margin:0;">Weierbot</h1>
<h2 style="margin: .1em 0 .5em; font-size: 1.8em;">
<a style="color:#FF1A00; text-decoration: none;" href="/{{.Title}}">{{.Title}}</a>
</h2>
<pre style="white-space:pre-wrap;">{{.Chatlog}}</pre>
<p class="margin-top: 1em; color:#888;">Proudly powered by
<a href="https://github.com/nomatter01/weierbot">weierbot</a></p>
</body>
</html>`))
var reURL = regexp.MustCompile(`https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]`)
var reTime = regexp.MustCompile(`(?m)^\[\S+\]`)
type WeierBot struct {
server string
client *irc.Conn
log chan Message
disconnect chan bool
}
func NewWeierBot(server, nick, password string, channels []string) *WeierBot {
w := &WeierBot{
server: server,
client: irc.SimpleClient(nick),
log: make(chan Message, 512),
disconnect: make(chan bool),
}
w.client.Me.Name = nick
w.client.Me.Ident = nick
w.client.AddHandler("connected", func(conn *irc.Conn, line *irc.Line) {
if password != "" {
conn.Privmsg("NickServ", "identify "+password)
}
for _, ch := range channels {
log.Printf("Joining %s\n", ch)
conn.Join(ch)
}
})
w.client.AddHandler("disconnected", func(conn *irc.Conn, line *irc.Line) {
w.disconnect <- true
})
w.client.AddHandler("join", func(conn *irc.Conn, line *irc.Line) {
if len(line.Args) >= 1 && strings.HasPrefix(line.Args[0], "#") {
conn.Privmsg(line.Nick, fmt.Sprintf("Hallo %s, willkommen auf %s. "+
"Dieser Channel wird unter http://weierbot.tux21b.org/ mitgespeichert.",
line.Nick, line.Args[0]))
}
})
w.client.AddHandler("privmsg", func(conn *irc.Conn, line *irc.Line) {
if len(line.Args) < 2 {
return
}
w.handleMessage(line.Args[0], Message{
Nick: line.Nick,
Time: line.Time,
Message: line.Args[1],
})
})
go w.writeLog()
return w
}
func createMathbinImage(latexcode string) string {
resp, err := http.Get(fmt.Sprintf(mathbinPreviewURL, url.QueryEscape("[EQ]"+latexcode+"[/EQ]")))
if err != nil {
return err.Error()
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
result := reMathbin.FindString(string(body))
return fmt.Sprintf(mathbinURL, result)
}
func (bot *WeierBot) handleMessage(target string, msg Message) {
if strings.HasPrefix(target, "#") {
bot.log <- msg
} else {
target = msg.Nick
}
command, ok := commands[msg.Message]
if ok {
command(bot, target, msg)
}
switch {
case strings.HasPrefix(msg.Message, "!wolfram "):
bot.send(target, fmt.Sprintf(wolframURL, url.QueryEscape(msg.Message[9:])))
case msg.Message == "!log":
bot.send(msg.Nick, "http://weierbot.tux21b.org/")
case strings.HasPrefix(msg.Message, "!mathbin "):
imgurl := createMathbinImage(msg.Message[9:])
bot.send(target, fmt.Sprintf(imgurl))
}
}
func (bot *WeierBot) send(target, message string) {
bot.client.Privmsg(target, message)
if strings.HasPrefix(target, "#") {
bot.log <- Message{Nick: bot.client.Me.Nick, Time: time.Now(),
Message: message}
}
}
func (bot *WeierBot) writeLog() {
var (
file *os.File
format = "2006-01-02.log"
filename string
)
defer func() {
if file != nil {
file.Close()
}
}()
for msg := range bot.log {
msg.Time = msg.Time.In(timezone)
current := msg.Time.Format(format)
if file == nil || current != filename {
if file != nil {
file.Close()
}
f, err := os.OpenFile(current,
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
log.Println(err)
} else {
file, filename = f, current
}
}
fmt.Fprintf(file, "%s\n", msg)
}
}
func (bot *WeierBot) ServeIRC() {
tries := 0
for {
tries++
if err := bot.client.Connect(bot.server); err != nil {
log.Println(err)
if tries >= 5 {
log.Fatal("maximum number of tries exceeded")
}
<-time.After(5 * time.Second)
continue
}
tries = 0
log.Printf("Connected to %s\n", bot.server)
<-bot.disconnect
}
}
func (bot *WeierBot) ServeHTTP(w http.ResponseWriter, r *http.Request) {
files, err := filepath.Glob("*.log")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sort.Strings(files)
if r.URL.Path == "/" && len(files) > 0 {
http.Redirect(w, r, "/"+filepath.Base(files[len(files)-1]),
http.StatusTemporaryRedirect)
}
index := sort.SearchStrings(files, r.URL.Path[1:])
if index < 0 || index >= len(files) {
http.NotFound(w, r)
return
}
chatlog, err := ioutil.ReadFile(files[index])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
chatlog = []byte(template.HTMLEscapeString(string(chatlog)))
chatlog = reURL.ReplaceAll(chatlog, []byte(`<a href="$0">$0</a>`))
chatlog = reTime.ReplaceAll(chatlog, []byte(`<span style="color: #888;">$0</span>`))
data := struct {
Title string
Prev, Next string
Chatlog template.HTML
}{
Title: filepath.Base(files[index]),
Chatlog: template.HTML(chatlog),
}
if index > 0 {
data.Prev = files[index-1]
}
if index < len(files)-1 {
data.Next = files[index+1]
}
if err := tmpl.Execute(w, data); err != nil {
log.Println(err.Error())
}
}
type Message struct {
Time time.Time
Nick string
Message string
}
func (m Message) String() string {
return fmt.Sprintf("[%s] %s: %s", m.Time.Format(time.Kitchen), m.Nick, m.Message)
}
var (
flagServer = flag.String("server", "irc.euirc.net", "address of the irc server")
flagNick = flag.String("nick", "weierbot", "irc nickname of the bot")
flagPassword = flag.String("password", "", "NickServ password")
flagChannels = flag.String("channels", "#tm", "irc channels (comma separated)")
flagListen = flag.String("http", ":8005", "http listen address")
)
func main() {
rand.Seed(time.Now().UnixNano())
flag.Parse()
commands = make(map[string]func(*WeierBot, string, Message))
commands["!coin"] = func(bot *WeierBot, target string, msg Message) {
if rand.Intn(2) == 0 {
bot.send(target, "head")
} else {
bot.send(target, "tail")
}
}
commands["!proof"] = func(bot *WeierBot, target string, msg Message) {
bot.send(target, buildProof())
}
bot := NewWeierBot(*flagServer, *flagNick, *flagPassword,
strings.Split(*flagChannels, ","))
go func() {
if err := http.ListenAndServe(*flagListen, bot); err != nil {
log.Fatal(err)
}
}()
bot.ServeIRC()
}