-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcabot.py
365 lines (290 loc) · 12 KB
/
lcabot.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/python
# Copyright (c) Twisted Matrix Laboratories.
# Copyright (c) Michael Still 2013
# See LICENSE for details.
"""
A simple LCA2013 irc bot.
Run this script with two arguments, the channel name the bot should
connect to, and file to log to, e.g.:
$ python lcabot.py
will log channel #test to the file 'test.log'.
"""
# twisted imports
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log
import datetime
import gzip
import imp
import os
import re
import sys
import time
import yaml
class MessageLogger:
"""
An independent logger class (because separation of application
and protocol logic is a good thing).
"""
def __init__(self, file):
self.file = file
def log(self, message):
"""Write a message to the file."""
timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
self.file.write('%s %s\n' % (timestamp, message))
self.file.flush()
def close(self):
self.file.close()
class Lcabot(irc.IRCClient):
"""A logging IRC bot."""
def __init__(self, factory):
self.plugins = []
self.verbs = {}
self.last_heartbeat = time.time()
self.current_topic = None
self.factory = factory
self.nickname = self.factory.conf['nickname']
self.lineRate = None
def _writeLog(self, msg):
try:
self.logger.log(msg)
except:
pass
log.msg(msg)
def _msg(self, channel, msg):
self._writeLog('%s >> <%s> %s' %(channel, self.nickname, msg))
self.msg(channel, msg)
def _topic(self, channel, topic):
self.topic(channel, topic)
self._writeLog('%s >> Set topic to "%s"' %(channel, topic))
def _describe(self, channel, action):
self.describe(channel, action)
self._writeLog('%s >> /me %s' %(channel, action))
def _loadPlugins(self):
self._unloadPlugins()
self._writeLog('[Loading plugins]')
self.plugins = []
self.verbs = {}
plugin_directory = 'commands'
re_plugin = re.compile('[^.].*\.py$')
for plugin_file in os.listdir(plugin_directory):
if re_plugin.match(plugin_file):
name = plugin_file[:-3]
self._writeLog('>> %s' % name)
try:
plugin_info = imp.find_module(name, [plugin_directory])
plugin = imp.load_module(name, *plugin_info)
for module in plugin.Init(self._writeLog,
self.factory.conf):
self.plugins.append(module)
yield module
for verb in module.Verbs():
self.verbs[verb] = module
self._writeLog(' implements verb %s' % verb)
except Exception, e:
self._writeLog('Exception from %s: %s' %(module, e))
def _unloadPlugins(self):
self._writeLog('[Unloading plugins]')
for module in self.plugins:
try:
module.Cleanup()
except Exception, e:
self._writeLog('Exception from %s: %s' %(module, e))
def _handleResponse(self, responses):
for resp in responses:
if resp:
self._writeLog('Response: %s' %(repr(resp)))
channel, kind, body = resp
if channel is None:
channel = '#%s' % self.factory.conf['channels'][0]['name']
if kind == 'msg':
self._msg(channel, body)
elif kind == 'topic':
if body != self.current_topic:
self._topic(channel, body)
else:
self._writeLog('[Skipping topic update, no change]')
else:
self._writeLog('Unknown response type')
def _doHeartbeat(self):
"""Heartbeat our modules."""
self.last_heartbeat = time.time()
for module in self.plugins:
try:
self._writeLog('[Heartbeat sent to %s]' % module.Name())
self._handleResponse(list(module.HeartBeat()))
except Exception, e:
self._writeLog('Exception from %s: %s' %(module, e))
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.logger = MessageLogger(gzip.open(self.factory.filename, "a"))
self._writeLog("[connected at %s]" %
time.asctime(time.localtime(time.time())))
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
self._writeLog("[disconnected at %s]" %
time.asctime(time.localtime(time.time())))
self.logger.close()
###########
# callbacks for events
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self._writeLog("[I have signed on]")
if self.factory.conf.get('password', None):
self._msg('nickserv',
'identify %s %s' %(self.nickname,
self.factory.conf['password']))
self.join(self.factory.conf['channels'][0]['name'],
self.factory.conf['channels'][0]['password'])
def joined(self, channel):
"""This will get called when the bot joins the channel."""
self._writeLog('[I have joined %s]' % channel)
for module in self._loadPlugins():
self._writeLog('[Loaded %s]' % module.Name())
self._writeLog('[Setup complete]')
# Do some opping
self._msg('chanserv', 'op %s %s' %(channel, self.nickname))
for op in self.factory.conf['operators']:
self._msg('chanserv', 'op %s %s' %(channel, op))
def topicUpdated(self, user, channel, topic):
"""Called when the topic changes or we join a channel."""
self._writeLog('%s >> topic set by %s is "%s"'
%(channel, user, topic))
self.current_topic = topic
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
self._writeLog('channel: %s, user: %s, msg: %s' % (channel, user, msg))
# We don't reply to ourselves
if user == self.nickname:
return
# If this is a privmsg, then we need to handle channels. Ignore in
# channel messages which aren't addressed to us
outchannel = channel
if channel == self.nickname:
outchannel = user
elif not msg.startswith(self.nickname + ':'):
return
# Determine the command
command = msg.rstrip()
if msg.startswith(self.nickname + ':'):
command = ':'.join(command.split(':')[1:]).lstrip()
elems = command.split(' ')
self._writeLog('* %s *' % repr(elems))
# Execute commands
if elems[0] == 'reload':
if user in self.factory.conf.get('operators', []):
for module in self._loadPlugins():
self._msg(outchannel,
'%s: Loaded %s' %(user, module.Name()))
else:
self._msg(outchannel, 'Damn kids today')
elif elems[0] == 'heartbeat':
if user in self.factory.conf.get('operators', []):
self._doHeartbeat()
else:
self._msg(outchannel,
'I\'m sorry sonny, I can\'t quite hear you')
elif elems[0] in self.verbs:
module = self.verbs[elems[0]]
try:
args = command[len(elems[0]):].lstrip()
self._writeLog('[Command sent to %s]' % module.Name())
self._handleResponse(list(module.Command(outchannel,
elems[0],
args)))
except Exception, e:
self._writeLog('Exception from %s: %s' %(module, e))
self._msg(outchannel, 'Error while handling command')
else:
if channel == self.nickname:
if elems[0] == 'help' and len(elems) > 1:
module = self.verbs.get(elems[1])
if module:
self._msg(user, module.Help(elems[1]))
else:
self._mg(user, 'That command is not registered')
else:
sorted_verbs = self.verbs.keys()
sorted_verbs.sort()
self._msg(user, ('I understand the following commands: %s'
% ', '.join(sorted_verbs)))
else:
self._describe(outchannel, 'is confused')
self._msg(outchannel, ('%s: I am the linux.conf.au bot. PM me '
'for help.' % user))
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
user = user.split('!', 1)[0]
self._writeLog('%s >> * %s %s' % (channel, user, msg))
def userJoined(self, user, channel):
"""This is called when a user joins a channel."""
for module in self.plugins:
try:
self._writeLog('[Join event for %s on %s sent to %s]'
%(user, channel, module.Name()))
self._handleResponse(list(module.NoticeUser(channel, user)))
except Exception, e:
self._writeLog('Exception from %s: %s' %(module, e))
# irc callbacks
def irc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
old_nick = prefix.split('!')[0]
new_nick = params[0]
self._writeLog("%s is now known as %s" % (old_nick, new_nick))
# For fun, override the method that determines how a nickname is changed on
# collisions. The default method appends an underscore.
def alterCollidedNick(self, nickname):
"""
Generate an altered version of a nickname that caused a collision in an
effort to create an unused related name for subsequent registration.
"""
return nickname + '^'
# Scary inner goo
def dataReceived(self, data):
"""Log all incoming data."""
if self.factory.conf['verbose']:
for l in data.replace('\r', '').split('\n'):
self._writeLog('IN: %s' % l)
irc.IRCClient.dataReceived(self, data)
if data.find(' PONG ') != -1:
self._writeLog('Considering heartbeat')
if time.time() - self.last_heartbeat > 60:
self._doHeartbeat()
def sendLine(self, line):
"""Log all outgoing data."""
if self.factory.conf['verbose']:
for l in line.replace('\r', '').split('\n'):
self._writeLog('OUT: %s' % l)
irc.IRCClient.sendLine(self, line)
class LcaBotFactory(protocol.ClientFactory):
"""A factory for LcaBots.
A new protocol instance will be created each time we connect to the server.
"""
def __init__(self, conf, filename):
self.conf = conf
self.filename = filename
def buildProtocol(self, addr):
p = Lcabot(self)
return p
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason
reactor.stop()
if __name__ == '__main__':
# Initialize logging
log.startLogging(sys.stdout)
# Load config
conf_file = open('bot.yaml')
conf = yaml.load(conf_file.read())
conf_file.close()
print conf
# Create factory protocol and application
logfile = datetime.datetime.now().strftime('%Y%m%d-%H%M%S.log.gz')
f = LcaBotFactory(conf, logfile)
# Connect factory to this host and port
reactor.connectTCP("irc.freenode.net", 6667, f)
# Run bot
reactor.run()