-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.py
executable file
·316 lines (262 loc) · 9.21 KB
/
bot.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of tofbot, a friendly IRC bot.
# You may redistribute it under the Simplified BSD License.
# If we meet some day, and you think this stuff is worth it,
# you can buy us a beer in return.
#
# Copyright (c) 2011,2016 Etienne Millon <[email protected]>
# Martin Kirchgessner <[email protected]>
# Nicolas Dumazet <[email protected]>
# Quentin Sabah <[email protected]>
# Christophe-Marie Duquesne <[email protected]>
# Guillaume Piolat <[email protected]>
"""
Usage: ./bot.py
This bot is entirely configured by environment variables. See README.md
for an exaustive list.
"""
from irc import Bot
import random
import sys
import os
import plugins
import types
import json
import atexit
import traceback
import threading
import time
import signal
from toflib import _simple_dispatch, urls_in, Cron, CronEvent
import plugins.help
import plugins.getset
import plugins.ping
import plugins.context
import plugins.euler
import plugins.lolrate
import plugins.donnezmoi
import plugins.jokes
import plugins.twitter
import plugins.dassin
import plugins.eightball
import plugins.sed
import plugins.rick
import plugins.expand
import plugins.like
import plugins.elle
import plugins.lag
import plugins.risoli
import plugins.lisp
import plugins.amour
import plugins.bar
random.seed()
class AutosaveEvent(CronEvent):
def __init__(self, bot, filename):
CronEvent.__init__(self, None)
self.filename = filename
self.bot = bot
def fire(self):
self.bot.save(self.filename)
class Tofbot(Bot):
# Those attributes are published and can be changed by irc users
# value is a str to object converter. It could do sanitization:
# if value is incorrect, raise ValueError
_mutable_attributes = {
"TGtime": int,
"memoryDepth": int
}
def __init__(self, nick=None, name=None, channels=None, password=None,
debug=True):
Bot.__init__(self, nick, name, channels, password)
self.joined = False
self.autoTofadeThreshold = 98
self.riddleMaxDist = 2
self.debug = debug
self.TGtime = 5
self.lolRateDepth = 8
self.cron = Cron()
self.lastTGtofbot = 0
self.memoryDepth = 50
self.last_interaction = int(time.time())
self.plugins = self.load_plugins()
def idle_time(self):
return int(time.time() - self.last_interaction)
def load_plugins(self):
d = os.path.dirname(__file__)
plugindir = os.path.join(d, 'plugins')
plugin_instances = {}
for m in dir(plugins):
if isinstance(m, types.ModuleType):
continue
plugin = getattr(plugins, m)
for n in dir(plugin):
c = getattr(plugin, n)
if type(c) not in [type]:
continue
name = c.__name__
if name.startswith('Plugin'):
instance = c(self)
plugin_name = name[6:].lower()
plugin_instances[plugin_name] = instance
return plugin_instances
# line-feed-safe
def msg(self, chan, msg):
for m in msg.split("\n"):
Bot.msg(self, chan, m)
def log(self, msg):
if self.debug:
print(msg)
def try_join(self, args):
if args[0] in ("End of /MOTD command.",
"This server was created ... I don't know"):
for chan in self.channels:
self.write(('JOIN', chan))
self.joined = True
def dispatch(self, origin, args):
# Useful: https://www.alien.net.au/irc/irc2numerics.html
self.last_interaction = time.time()
self.log("o=%s n=%s a=%s" % (origin.sender, origin.nick, args))
sender_nick = origin.nick
command_type = args[1]
if not self.joined:
self.try_join(args)
return
if command_type == 'JOIN':
for p in self.plugins.values():
p.on_join(args[0], sender_nick)
elif command_type == 'KICK' and args[3] == self.nick:
reason = args[0]
chan = args[2]
self.write(('JOIN', chan))
for p in self.plugins.values():
p.on_kick(chan, reason)
elif command_type == 'PRIVMSG':
self.cron.tick()
msg_text = args[0]
chan = args[2]
urls = urls_in(msg_text)
# filter empty messages
if len(msg_text.strip()) == 0:
return
# dispatch to plugins
for p in self.plugins.values():
p.handle_msg(msg_text, chan, sender_nick)
for url in urls:
p.on_url(url)
# dispatch commands
if msg_text.strip().startswith("!"):
tokens = msg_text.strip().split(" ")
cmd = tokens[0][1:]
chan = self.channels[0]
if cmd in _simple_dispatch:
act = self.find_cmd_action("cmd_" + cmd)
act(chan, tokens[1:], sender_nick)
elif command_type == 'PING':
self.log('PING received in bot.py')
elif command_type == 'ERROR':
traceback.print_exc(file=sys.stdout)
elif command_type == 'PART':
chan = args[2]
for p in self.plugins.values():
p.on_leave(chan, sender_nick)
elif command_type == 'QUIT':
for p in self.plugins.values():
p.on_quit(sender_nick)
elif command_type == '353': # Reply to NAMES
names = set([n.lstrip('@') for n in args[0].split(' ')
if n.lstrip('@') != self.nick])
# act like if everybody just joined
for n in names:
for p in self.plugins:
if p != "jokes":
self.plugins[p].on_join(args[-1], n)
else: # Unknown command type
self.log('Unknown command type : %s' % command_type)
def find_cmd_action(self, cmd_name):
targets = list(self.plugins.values())
targets.insert(0, self)
for t in targets:
if (hasattr(t, cmd_name)):
action = getattr(t, cmd_name)
return action
def nop(self, chan, args):
pass
return nop
def safe_getattr(self, key):
if key not in self._mutable_attributes:
return None
if not hasattr(self, key):
return "(None)"
else:
return str(getattr(self, key))
def safe_setattr(self, key, value):
try:
converter = self._mutable_attributes.get(key)
if converter is None:
return False
value = converter(value)
setattr(self, key, value)
return True
except ValueError:
pass
def load(self, filename):
try:
with open(filename) as f:
state = json.load(f)
if state['version'] != 1:
return False
for name, plugin_state in state['plugins'].items():
try:
plugin = self.plugins[name]
plugin.load(plugin_state)
except KeyError:
pass
except IOError as e:
print("Can't load state. Error: {0}".format(e))
def save(self, filename):
try:
with open(filename, 'w') as f:
state = {'version': 1, 'plugins': {}}
for name, plugin in self.plugins.items():
plugin_state = plugin.save()
state['plugins'][name] = plugin_state
json.dump(state, indent=4, fp=f)
except IOError as e:
print("Can't save state. Error: {0}".format(e))
def kill_if_disconnected(bot, timeout):
while True:
time.sleep(timeout)
if bot.idle_time() > timeout:
bot.log("Idle for more than %ds. Exiting..." % timeout)
os.kill(os.getpid(), signal.SIGTERM)
break
def main():
host = os.getenv("TOFBOT_SERVER", "irc.freenode.net")
port = int(os.getenv("TOFBOT_PORT", "6667"))
chan = os.getenv("TOFBOT_CHAN", "#soulakdev").split(",")
nick = os.getenv("TOFBOT_NICK", "tofbot")
password = os.getenv("TOFBOT_PASSWD", None)
name = os.getenv("TOFBOT_NAME", "tofbot")
debug = bool(os.getenv("TOFBOT_DEBUG", ""))
timeout = int(os.getenv("TOFBOT_TIMEOUT", "240"))
b = Tofbot(nick, name, chan, password, debug)
# Restore serialized data
state_file = "state.json"
b.load(state_file)
# Perform auto-save periodically
autosaveEvent = AutosaveEvent(b, state_file)
b.cron.schedule(autosaveEvent)
# ... and save at exit
@atexit.register
def save_atexit():
print("Exiting, saving state...")
b.save(state_file)
print("Done !")
monitor = threading.Thread(target=kill_if_disconnected, args=(b, timeout))
monitor.daemon = True
monitor.start()
b.run(host, port)
if __name__ == "__main__":
main()