-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitania.py
executable file
·440 lines (368 loc) · 15.2 KB
/
titania.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python
# twisted imports
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol, task
from twisted.python import log
from twisted.python.logfile import DailyLogFile
log.startLogging(DailyLogFile.fromFullPath("/var/log/titania.log"))
# system imports
import time, sys, string, os, pprint, socket
from datetime import datetime as dt
from urlparse import urlparse
from urllib import urlencode
import collections
import Queue
import base64
# interface imports
import SpaceAPIClient
import simplejson
import argparse
import oauth.oauth as oauth
from twittytwister import twitter
#Global Vars
HEARTBEAT = 1 ## Heartbeat time in seconds
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()))
#log.msg(message) #Unecessary to log chat messages to debug log
self.file.write('%s %s\n' % (timestamp, message))
self.file.flush()
def close(self):
self.file.close()
def convert(data):
if isinstance(data, unicode):
return str(data)
elif isinstance(data, collections.Mapping):
return dict(map(convert, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(convert, data))
else:
return data
class IRCBot(irc.IRCClient):
"""A logging IRC bot."""
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.logger = MessageLogger(open(self.factory.filename, "a"))
self.logger.log("[connected at %s]" %
time.asctime(time.localtime(time.time())))
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
self.logger.log("[disconnected at %s]" %
time.asctime(time.localtime(time.time())))
self.logger.close()
# Custom functions
def logged_msg(self,target,msg):
msg=msg.encode('ascii','replace')
self.msg(target,msg)
self.logger.log("<%s> %s" % (target,msg))
# callbacks for events
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.factory.channel)
self.msg("Nickserv", "identify %s"%base64.b64decode(self.password))
def joined(self, channel):
"""This will get called when the bot joins the channel."""
self.logger.log("[I have joined %s]" % channel)
#IRC Keepalive`
self.startHeartbeat()
#Custom heartbeat
self.loopcall.start(5.0)
def on_pm(self, user, channel, msg):
if user not in self.space.admins:
response = "Hello, I'm the IRC bot for %s, I'm afraid you're not allowed to do that, %s" % (self.space.name,user)
# Allow authenticated users (see --admin flag) to tweet
elif msg.startswith("tweet"):
txt = "%s said: %s"%(user,string.join(msg.split()[1:]).strip())
self.space.msg_q.add(txt,tweet=True,irc=False)
response = "your message has been queued"
# Allow authenticated users (see --admin flag) to tweet
elif msg.startswith("speak"):
txt = "%s said: %s"%(user,string.join(msg.split()[1:]).strip())
self.space.msg_q.add(txt,tweet=False,irc=True)
response = "your message has been queued"
elif msg.startswith("open door"):
msg = string.join(msg.split()[1:]).strip()
txt = "%s remotely opened the door: %s"%(user,msg)
response = self.space.unlock_door(msg)
self.space.msg_q.add(txt,tweet=False,irc=True)
else:
response = "Successfully authenticated"
self.logged_msg(user,response)
def on_msg(self,user,channel,cmd):
#lose my nick, weird whitespace, and lower for ease of comp
cmd_l = ":".join(cmd.split(':')[1:]).strip().lower()
if cmd_l.startswith("is the space"):
if cmd_l.split()[3].split('?')[0] not in ["open","closed"]:
response = "Stop being a smart arse, the space is either 'open' or 'closed'"
else:
delta = self.space.time_delta_string()
response = "The space is %s, as of %s ago" % (self.space.status(),delta)
elif cmd_l.startswith("is the door"):
if cmd_l.split()[3].split('?')[0] not in ["open","closed"]:
response = "Stop being a smart arse, the door is either 'open' or 'closed'"
else:
door = "closed" if self.space.api.doorClosedState() else "open"
response = "The door is %s" % (door)
elif cmd_l.startswith("what is the board"):
response = "The Status of the board is %s"%self.space.board_status()
else:
response = "Sorry, don't know what to do with that"
msg="%s: %s"%(user,response)
self.logged_msg(channel, msg)
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
self.logger.log("<%s> %s" % (user, msg))
# Check to see if they're sending me a private message
if channel == self.nickname:
self.on_pm(user,channel,msg)
return
# Check to see if they're asking me for help
if msg.startswith(self.nickname + ": help"):
msg = "%s: I'm a little stupid at the minute; current commands I accept are:" % user
self.logged_msg(channel, msg)
msg = "%s: is the space open?" % self.nickname
self.logged_msg(channel, msg)
msg = "%s: what is the board status?" % self.nickname
self.logged_msg(channel, msg)
return
# If its a message directed at me, deal with it
if msg.startswith(self.nickname + ":"):
self.on_msg(user, channel,msg)
return
# Otherwise check to see if it is a message directed at me
if msg.startswith(self.nickname + ":"):
msg = "%s: I am a log bot" % user
msg += ", say \'%s:help\' for more information" % self.nickname
self.logged_msg(channel, msg)
return
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
user = user.split('!', 1)[0]
self.logger.log("* %s %s" % (user, msg))
# 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.logger.log("%s is now known as %s" % (old_nick, new_nick))
def userJoined(self, user, channel):
self.logger.log('%s has joined %s' % (user, channel))
def userLeft(self, user, channel):
self.logger.log('%s has left %s' % (user, channel))
def userQuit(self, user, quitMessage):
self.logger.log('%s has quit. Reason: %s' % (user, quitMessage))
# 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 + '^'
def heartbeat(self):
"""
Periodically executed tasks
"""
#Launch Space heartbeat
delta = self.space.heartbeat()
if delta:
self.topic("%s is %s: %s"%(self.space.name, self.space.status(),
self.space.config['url']))
class IRCBotFactory(protocol.ClientFactory):
"""A factory for IRCBots.
A new protocol instance will be created each time we connect to the server.
"""
def __init__(self, space):
self.channel = space.irc_chan
self.filename = "/var/log/%s.irc.log"%self.channel
self.space=space
def buildProtocol(self, addr):
p = IRCBot()
p.factory = self
p.space = self.space
self.space.irc = p
p.loopcall = task.LoopingCall(p.heartbeat)
p.nickname = self.space.username
p.password = self.space.password
p.realname = self.space.name
log.msg("Built IRC: %s,%s"%(p.realname,p.nickname))
return p
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
connector.connect()
def clientConnectionFailed(self, connector, reason):
log.err("connection failed: %s"%reason)
reactor.stop()
class twitterClient():
"""
A Twitter Client
"""
def __init__(self,auth,params={}):
consumer = oauth.OAuthConsumer(auth['ckey'],auth['csecret'])
token = oauth.OAuthToken(auth['akey'], auth['asecret'])
self.client = twitter.Twitter(consumer=consumer, token=token)
self.params = params
def tweet(self,msg,params={}):
params = dict(self.params.items() + params.items())
log.msg(self.client.update(msg,params))
class broadcast_queues():
"""
Custom Class with multiple queues to handle async twitter/irc
"""
def __init__(self):
self.irc = Queue.Queue()
self.twitter = Queue.Queue()
def add(self,txt,irc=True,tweet=False):
if irc:
self.irc.put(txt)
if tweet:
self.twitter.put(txt)
log.msg("Queued %s;T:%s;I:%s"%(txt,tweet,irc))
class hackerspace():
"""
Space Interface
"""
def __init__(self,args):
self.admins = args.admin
self.msg_q = broadcast_queues()
self.enable_twitter = args.enable_twitter
self.client_init(args)
#overrides
if args.chan is not None:
log.err("Overriding chan:%s with %s due to override"%(self.irc_chan,args.chan))
self.irc_chan="#%s"%args.chan
def error_bcast(self,msg):
self.msg_q.add(msg,tweet=False,irc=True)
log.err(msg)
def client_init(self,args):
if hasattr(args,'auth_file'):
auth=simplejson.load(open(args.auth_file,'r'))
#SpaceAPI
try:
authpair = auth['api_authpair']
base = auth['api_base']
except KeyError as err:
self.error_bcast("Invalid Auth Configuration:%s"%err)
self.api = SpaceAPIClient.client(authpair=authpair,base=base)
self.updateState()
self.load_json_config(self.config)
self.occupied = self.config['open']
#Twitter
self.password = auth['ircreg']
self.admins += auth['admins']
if self.enable_twitter:
try:
self.twitter = twitterClient(auth)
except Exception, err:
self.error_bcast('Twitter could not be loaded (IRC might fail too):%s'%err)
self.twitter = False
else:
self.error_bcast('Twitter explicitly disabled by console flag')
self.twitter = False
else:
self.error_bcast('No Credentials for twitter/spaceapi loaded')
# IRC
self.irc_f = IRCBotFactory(self)
reactor.connectTCP(self.irc_net,
6667,
self.irc_f)
def load_json_config(self,conf):
try:
self.name = conf['space']
if 'botname' in conf:
self.username = conf['botname']
else:
self.username = re.sub(r'\s','',self.name)
irc = urlparse(conf['contact']['irc'])
self.irc_net = irc.netloc
self.irc_chan = string.strip(irc.path,'/')
log.msg("Using Net:%s Chan:%s"%(self.irc_net, self.irc_chan))
except KeyError as err:
self.error_bcast("Cannot Load Space Configuration: %s"%err)
def status(self):
return "open" if self.occupied else "closed"
def updateState(self):
try:
self.config = convert(self.api.spaceState(debug=True)) #Debug used to ensure authentication validity
except AuthenticationException as err:
self.error_bcast("Cannot Authenticate with SpaceAPI service:%s"%err)
raise err
def time_delta_string(self):
self.updateState() ##To fix date bug; farbot was reporting deltas from last change before initialisation as was only reading spacestate once!
s = int(dt.strftime(dt.utcnow(),"%s")) - int(self.config['lastchange'])
hours, remainder = divmod(s, 3600)
minutes, seconds = divmod(remainder, 60)
if hours > 0:
delta = '%s hrs, %s mins' % (hours, minutes)
else:
delta = '%s mins, %s secs' % (minutes, seconds)
return delta
def state_changed(self):
try:
state = self.api.buttonDownState()
except ValueError as err:
log.err("%s"%err)
return False
except socket.error as err:
log.err("%s"%err)
return False
if self.occupied != state:
self.occupied = state
delta = self.time_delta_string()
self.api.set_open_state(self.occupied)
msg = "%s is now %s after %s" % (self.name,self.status(),delta)
log.msg(msg)
self.msg_q.add(msg,tweet=self.enable_twitter,irc=True)
return True
else:
return False
def unlock_door(self,msg):
try:
r=self.api.openDoor()
r=r['response']
except Exception as E:
r= "%s"%E
return r
def heartbeat(self):
self.state_changed()
#Send any queued tweets and post one at a time
if not self.msg_q.twitter.empty() and self.twitter:
self.twitter.tweet(self.msg_q.twitter.get())
self.msg_q.twitter.task_done()
#pick up all queued irc queued messages from the space
if not self.msg_q.irc.empty():
msg=self.msg_q.irc.get_nowait()
self.irc.logged_msg(self.irc_chan,msg)
self.msg_q.irc.task_done()
if __name__ == '__main__':
# initialize logging
log.startLogging(sys.stdout)
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--auth_file', dest='auth_file', action='store',
default=None,
help='Twitter Authentication File')
parser.add_argument('--admin', dest='admin', action='append',
default=[],
help='Nickname of Admin (multiple invocations allowed)')
parser.add_argument('--chan', dest='chan', action='store',
default=None,
help='IRC Channel override')
parser.add_argument('--disable_twitter', dest='enable_twitter', action='store_false',
help='Twitter override')
args = parser.parse_args()
if (args.auth_file is None):
log.err("Need Auth file")
sys.exit()
print args
#Create Hackerspace
s=hackerspace(args)
# run bot
reactor.run()