-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
335 lines (315 loc) · 7.97 KB
/
main.go
File metadata and controls
335 lines (315 loc) · 7.97 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"./irc" // https://github.com/thoj/go-ircevent
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
type Config struct {
Channels []string
Server string
DefaultTopic string
WebPort int
Prefix string
Nick string
Realname string
Cooldown int64
WebDesign string
Identify bool
Nickserv string
Command string
}
// Start configuration
func config() bool {
c, err := ioutil.ReadFile("config.json")
if err != nil {
fmt.Println(err)
return false
}
err = json.Unmarshal([]byte(c), &f)
return true
}
//Get the config before more damage is done!
var f Config
var channels = []string{"#example", "#example2"}
var server = "irc.esper.net"
var bot = irc.IRC("Mandatch", "Mibbit")
var botnick = "Mandatch"
var default_topic = "default"
var web_port = 80
var prefix = "!"
var cooldown = int64(5)
var webdesign = "default.html"
var identify = false
var nickserv = "Nickserv"
var ncmd = "IDENTIFY mypassword"
// End configuration
var myIP = "0.0.0.0" // do not touch - automatically obtained.
var lastcmd = int64(0)
func NLSplit(str string) []string { // Utility to split a string by newline. Cross-platform.
rstr := strings.Replace(string(str), "\r", "\n", -1)
rstr = strings.Replace(string(rstr), "\n\n", "\n", -1)
st := strings.Split(string(rstr), "\n")
return st
}
func prepForIRC(str string, target string, channel string) string {
dorp := strings.Replace(string(str), "{n}", "\x02"+target+"\x02", -1)
dorp = strings.Replace(string(dorp), "{i}", myIP, -1)
dorp = strings.Replace(string(dorp), "{c}", channel, -1)
return dorp
}
func getAList() []string {
list, err := ioutil.ReadFile("data/access.list")
if err != nil {
fmt.Println(err)
return []string{}
}
return NLSplit(string(list))
}
func isAllowed(user string) bool {
ulist := getAList()
for i := range ulist {
if strings.ToLower(user) == strings.ToLower(string(ulist[i])) {
return true
}
}
return false
}
func isTopic(topic string, channel string) bool {
dirl, err := ioutil.ReadDir("data/topics")
if err != nil {
return false
}
for i := range dirl {
if topic+".txt" == string(dirl[i].Name()) {
return true
}
if channel == string(dirl[i].Name()) {
dirll, err := ioutil.ReadDir("data/topics/" + channel)
if err != nil {
return false
}
for l := range dirll {
if topic+".txt" == string(dirll[l].Name()) {
return true
}
}
}
}
return false
}
func getTopic(topic string, channel string) string {
td, err := ioutil.ReadFile("data/topics/" + topic + ".txt")
if err != nil {
tdd, err := ioutil.ReadFile("data/topics/" + channel + "/" + topic + ".txt")
if err != nil {
return ""
}
return string(tdd)
}
return string(td)
}
func sendMessage(channel string, text string, target string) {
if len(text) == 0 {
return
}
msgs := NLSplit(text)
if len(msgs) >= 3 {
msgs = msgs[0:2]
}
for i := range msgs {
bot.Privmsg(channel, prepForIRC(msgs[i], target, channel))
}
return
}
func onMsg(event *irc.Event) {
fmt.Println("<["+event.Arguments[0]+"]", event.Nick+">", event.Message)
if strings.HasPrefix(event.Message, prefix) {
if isAllowed(string(event.Nick)) {
channel := event.Arguments[0]
msgg := event.Message[len(prefix):]
msg := strings.Split(msgg, " ")
Target := ""
Topic := ""
ct, _ := strconv.ParseInt(time.Now().UTC().Format("20060102150405"), 10, 64)
if ct-cooldown < lastcmd {
return
}
lastcmd = ct
if len(msg) < 1 {
return
}
if msg[0] != "" {
Target = msg[0]
} else {
return
}
if msg[0] == "*" {
Target = "Everyone"
}
if len(msg) == 1 {
Topic = default_topic
}
if len(msg) == 2 {
Topic = msg[1]
}
if Topic == "" {
Topic = default_topic
}
Topic = strings.ToLower(Topic)
if isTopic(string(Topic), channel) {
Topicstr := getTopic(Topic, channel)
sendMessage(channel, Topicstr, Target)
}
}
}
return
}
func onConnect(event *irc.Event) {
//http://res.public-craft.com/myip.php - gets bot's host's IP.
// TODO - Do this without involving my own services.
resp, err := http.Get("http://res.public-craft.com/myip.php")
if err != nil {
myIP = "0.0.0.0"
} else {
dorp, _ := ioutil.ReadAll(resp.Body)
myIP = string(dorp)
}
resp.Body.Close()
if identify {
bot.Privmsg(nickserv, ncmd)
}
for i := range channels {
bot.Join(channels[i])
}
}
func onKick(event *irc.Event) {
if strings.ToLower(event.Message) != "leave" {
if strings.ToLower(event.Arguments[1]) == strings.ToLower(bot.GetNick()) {
bot.Join(event.Arguments[0])
sendMessage(event.Arguments[0], "If you want me to stay out, kick me with the reason 'leave'.", "") // Yep
}
}
}
func renderPage(c string, p string) string {
tpl, err := ioutil.ReadFile("data/html/" + webdesign)
if err != nil {
return "There was an issue loading the template data: File not Found."
}
ret := ""
tpll := ""
for i := range tpl {
tpll = tpll + string(tpl[i])
}
// If somebody has a better solution for converting a []byte to a string, I'd like to hear it.
// Tried, like, everything.
ret = strings.Replace(tpll, "{b}", botnick, -1)
ret = strings.Replace(ret, "{p}", p, -1)
ret = strings.Replace(ret, "{c}", c, -1)
return ret
}
func WebServer() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
page := "Topics"
derp := ""
dirl, err := ioutil.ReadDir("data/topics")
if err != nil {
return
}
for i := range dirl {
if strings.HasSuffix(dirl[i].Name(), ".txt") {
n := dirl[i].Name()[:len(dirl[i].Name())-4]
derp = derp + "<li><a href='/topic/" + n + "'>" + n + "</a></li>"
}
if strings.HasPrefix(dirl[i].Name(), "#") {
ddd := strings.Replace(dirl[i].Name(), "#", ",", -1)
derp = derp + "<li><a href='/topics/" + ddd + "'>" + dirl[i].Name() + "</a></li>"
}
}
fmt.Fprintf(w, renderPage(derp, page))
})
http.HandleFunc("/topics/", func(w http.ResponseWriter, r *http.Request) {
channell := r.URL.Path[len("/topics/"):]
derp := ""
page := ""
if strings.HasPrefix(channell, ",") {
channel := strings.Replace(channell, ",", "#", -1)
derp = derp + "<h2>Topics (" + channel + ")</h2><ul>"
page = "Topics (" + channel + ")"
dirl, err := ioutil.ReadDir("data/topics/" + channel)
if err != nil {
return
}
for i := range dirl {
if strings.HasSuffix(dirl[i].Name(), ".txt") {
n := dirl[i].Name()[:len(dirl[i].Name())-4]
derp = derp + "<li><a href='/topic/" + channell + "/" + n + "'>" + n + "</a></li>"
}
}
}
fmt.Fprintf(w, renderPage(derp, page))
})
http.HandleFunc("/topic/", func(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[len("/topic/"):]
channel := ""
page := ""
if strings.HasPrefix(title, ",") {
ddd := strings.Split(title, "/")
channel = ddd[0]
title = ddd[1]
channel = strings.Replace(channel, ",", "#", -1)
}
if isTopic(string(title), channel) {
derp := ""
page = "Viewing Topic (" + channel + "/" + title + ")"
derp = derp + "<pre>" + getTopic(title, channel) + "</pre>"
fmt.Fprintf(w, renderPage(derp, page))
}
})
http.HandleFunc("/alist", func(w http.ResponseWriter, r *http.Request) {
derp := ""
page := "Access List"
derp = derp + "<ul>"
al := getAList()
for i := range al {
if al[i] == "" {
continue
}
derp = derp + "<li>" + al[i] + "</li>"
}
fmt.Fprintf(w, renderPage(derp, page))
})
if web_port != 0 {
wp := strconv.Itoa(web_port)
fmt.Println("Serving web requests on port " + string(wp))
http.ListenAndServe(":"+string(wp), nil)
}
}
func main() {
if !config() {
fmt.Println("Error in loading config. Aborting.")
return
}
channels = f.Channels
server = f.Server
bot = irc.IRC(f.Nick, f.Realname)
default_topic = f.DefaultTopic
web_port = f.WebPort
prefix = f.Prefix
cooldown = f.Cooldown
webdesign = f.WebDesign
botnick = f.Nick
identify = f.Identify
nickserv = f.Nickserv
ncmd = f.Command
bot.Connect(server)
bot.AddCallback("001", onConnect)
bot.AddCallback("PRIVMSG", onMsg)
bot.AddCallback("KICK", onKick)
WebServer()
bot.Loop()
}