forked from brachiel/rotcbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotcbot_jabber.py
514 lines (392 loc) · 16.6 KB
/
rotcbot_jabber.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/python
# vim: set expandtab tabstop=4
"""A jabber bot that scans the TGE master server for games and annouces them."""
__author__ = 'Wanja Chresta <wanja dot chresta at gmail dot com>'
__version__ = '0.1'
__website__ = 'http://github.com/brachiel/rotcbot'
__license__ = 'GPLv3 or later'
from jabberbot import JabberBot, botcmd
from RotcWatcher import Watcher
import time
import os # just needed to get the HOME variable
import json
test = False
class RotcBot(JabberBot):
def __init__(self, jid, password, res="main", debug=False, json_output=None):
JabberBot.__init__(self, jid, password, res, debug)
self.w = Watcher()
self.server_ids = [] # enumerate keys of server_list
self.last_rotc_update = 0
self.roster = None
self.json_output = json_output
self.EVENTS = { 'new_game': 'The first player joins a server.',
'game_close': 'The last player leaves a server, or a server which had players is closed.',
'player_change': 'A change in player numbers on a server.',
'new_server': 'A new server was created (any number of players, even 0).',
'server_close': 'A server was closed (even if it had no players)' }
@botcmd(hidden=True)
def l(self, mess, args):
return self.list(mess, args)
@botcmd
def list(self, mess, args):
"""Displays the list of open rotc servers. Alias: l"""
if len(self.server_ids) == 0:
return "There are no open rotc servers. Maybe you want to host one?"
retn = ["List of open rotc ethernet servers:"]
for i in range(len(self.server_ids)):
addr = self.server_ids[i]
try:
name = self.w.server_info[addr]['server_name']
players = self.w.server_info[addr]['player_count']
map = self.w.server_info[addr]['mission_name']
retn.append("[%i] %s has %s players" % (i, name, players))
except KeyError:
retn.append("[%i] %s is in an unknown state" % (i, addr))
return '\n'.join(retn)
@botcmd(hidden=True)
def d(self, mess, args):
return self.details(mess, args)
@botcmd
def details(self, mess, args):
"""Displays the details of a given rotc server. Alias: d"""
if args in self.server_ids:
addr = args
det = self.w.get_server_details(addr)
else:
try:
i = int(args)
except ValueError:
return "Unknown id. Enter either id from list or IP"
if i < len(self.server_ids) and self.server_ids[i] != None:
addr = self.server_ids[i]
det = self.w.get_server_details(addr)
else:
return "There is no server with this id."
retn = ["Details for rotc ethernet server with address %s:" % addr]
for key, value in det.items():
retn.append("%s: %s" % (key, value))
return '\n'.join(retn)
@botcmd(hidden=True)
def broadcast(self, mess, args):
"""ADMIN - broadcast a message to all users of rotcbot."""
jid = str(mess.getFrom()).split('/')[0]
if jid == control_room:
self._broadcast(''.join(args))
else:
self.error("User %s wanted to broadcast, but he's not in the control room" % jid)
@botcmd(hidden=True)
def msg(self, mess, args):
"""sends a message to a jid."""
jid = str(mess.getFrom()).split('/')[0]
if jid == control_room:
try:
target, msg = args.split(' ', 1)
self.send(target, msg)
except ValueError:
self.error("The correct format is: send jid message")
else:
self.error("User %s wanted to use msg, but he's not in the control room" % jid)
@botcmd(hidden=True)
def restart(self, mess, args):
jid = str(mess.getFrom()).split('/')[0]
if jid == control_room:
self.quit() # we will be restarted by the supervisor
else:
self.error("User %s wanted to use restart, but he's not in the control room" % jid)
@botcmd(hidden=True)
def shutdown(self, mess=None, args=None):
if mess is None and args is None:
JabberBot.shutdown(self)
return
jid = str(mess.getFrom()).split('/')[0]
if jid == control_room:
self.log("Bang, Bang, %s shot me down, bang bang..." % jid)
raise Exception("Shutdown rotcbot")
else:
self.error("User %s wanted to shutdown rotbot, but he's not in the control room" % jid)
@botcmd(hidden=True)
def show_contacts(self, mess, args):
jid = str(mess.getFrom()).split('/')[0]
if jid == control_room:
contacts = "List of all users:\n"
for contact in self.roster.getItems():
try:
groups = self.roster.getGroups(contact)
except e:
groups = "ERR " + str(e)
contacts += '> %s - %s\n' % (str(contact), groups)
#self.send(control_room, contacts, message_type='groupchat')
self.log(contacts)
else:
self.error("User %s wanted to use show_contacts, but he's not in the control room" % jid)
#@botcmd
#def raise_error(self, mess, args):
# """Tells the bot to die"""
#
# raise Exception('killed', mess)
def idle_proc(self):
if self.last_rotc_update + 7 < int(time.time()):
self._build_server_list()
self.last_rotc_update = int(time.time())
if self.json_output:
self.json_output()
def json_output(self):
try:
with open(self.json_output, 'w') as out:
data = {'time':self.last_rotc_update,
'server_info':self.w.server_info}
json.dump(data, out)
except IOError, e:
self.error('Failed to write to json file: %s' % str(e))
return False
return True
def _build_server_list(self):
self.w.update_server_list()
# after this is called, the RotcWatcher module will call RotcWatcher.callback_* functions which must be connected to functions that do something
#print "I",self.w.server_info
server_list = [ x for x,y in self.w.server_info.items() if 'new' not in y.keys() ]
#print "L",server_list
# update the id-list of servers to use with the "list" command
new_ids = [None,] * len(self.server_ids) # presume all the servers are gone
for addr in server_list:
if addr in self.server_ids:
i = self.server_ids.index(addr)
new_ids[i] = addr
else:
new_ids.append(addr)
self.server_ids = new_ids
@botcmd(hidden=True)
def s(self, mess, args):
return self.show_events(mess, args)
@botcmd(hidden=True)
def show(self, mess, args):
"""Same as show_events."""
return self.show_events(mess, args)
@botcmd
def show_events(self, mess, args):
"""Shows the events you've subscribed. Aliases: s, show"""
jid = str(mess.getFrom()).split('/')[0]
groups = self.roster.getGroups(jid)
if len(groups) == 0:
return "You haven't subscribed any events. To do so, use sub_event"
msg = []
for event,desc in self.EVENTS.items():
if event in groups:
msg.append('%s - %s' % (event, desc))
return "The events you've currently subscribed:\n" + '\n'.join(msg)
@botcmd(hidden=True)
def sub(self, mess, args):
"""Same as sub_event."""
return self.sub_event(mess, args)
@botcmd
def sub_event(self, mess, args):
"""Subscribe an event (new_game, game_close, player_change, new_server, server_close). Aliases: sub"""
jid = str(mess.getFrom()).split('/')[0]
if args not in self.EVENTS.keys():
return "%s is not a valid event. Possibilities are: %s" % (args, ', '.join(self.EVENTS.keys()))
groups = self.roster.getGroups(jid)
if args not in groups:
groups.append(args)
self.roster.setItem(jid, groups=groups)
self.log("%s subscribed to %s" % (jid, args))
return "You have subscribed to the %s event" % args
else:
return "You've already subscribed to the %s event. Nothing changed." % args
@botcmd(hidden=True)
def un(self, mess, args):
return self.unsub_event(mess, args)
@botcmd(hidden=True)
def unsub(self, mess, args):
"""Same as unsub_event."""
return self.unsub_event(mess, args)
@botcmd
def unsub_event(self, mess, args):
"""Unsubscribe an event. Aliases: un, unsub"""
jid = str(mess.getFrom()).split('/')[0]
groups = self.roster.getGroups(jid)
# if args not in self.EVENTS.keys():
# return "%s is not a valid event. Possibilities are: %s" % (args, ', '.join(self.EVENTS.keys()))
if args not in groups:
return "You are not subscribed to the event '%s'" % args
while args in groups:
groups.remove(args)
self.roster.setItem(jid, groups=groups)
self.log("%s unsubscribed from %s" % (jid, args))
return "You've been unsubscribed to the event '%s'" % args
def _broadcast(self, msg):
self._broadcast_event(None, msg)
def _broadcast_event(self, event, msg):
#for jid in self.roster.getItems():
# build a list of unique jids
jid_status = {}
for jid, (show, status) in self._JabberBot__seen.items():
jid = str(jid).split('/')[0]
if show is self.AVAILABLE:
jid_status[jid] = show
for jid in jid_status.keys():
groups = self.roster.getGroups(jid)
if groups and (event == None or event in groups): # None is broadcast to all
print "%s: %s < %s" % (event, jid, msg)
self.send(jid, msg, message_type='chat')
# level = self.notice_level[jid]
# for jid, level in self.notice_level.items():
# if show is self.AVAILABLE:
# if level >= min_level:
# self.send(jid, msg, message_type='chat')
def _announce_player_change(self, addr, from_players, to_players):
if addr not in self.w.server_info:
return
name = self.w.server_info[addr]['server_name']
msg = "Server '%s' has now %s player(s)" % (name, to_players)
print "Server '%s' went from %s players to %s players." % (name, from_players, to_players)
self._broadcast_event('player_change', msg)
if from_players == 0 and to_players >= 1: # first player joins
self._broadcast_event('new_game', msg)
elif from_players >= 1 and to_players == 0: # last player leaves
self._broadcast_event('game_close', msg)
def _announce_new_server(self, addr):
if addr not in self.w.server_info:
self._broadcast_event('new_server', "A new server was created. Details are unknown")
return
name = self.w.server_info[addr]['server_name']
players = self.w.server_info[addr]['player_count']
msg = "The new Server '%s' has %s player(s)" % (name, players)
self._broadcast_event('new_server', msg)
if players > 0:
self._broadcast_event('new_game', msg)
def _announce_server_close(self, addr):
if addr not in self.w.server_info.keys():
self._broadcast_event('server_close', "The server %s was closed." % addr)
return
if 'server_name' in self.w.server_info.keys():
name = self.w.server_info[addr]['server_name']
else:
name = addr
if 'player_count' in self.w.server_info.keys():
players = self.w.server_info[addr]['player_count']
else:
players = None
if players != None:
msg = "The Server '%s' was closed having %s player(s)" % (name, players)
else:
msg = "The Server '%s' was closed" % name
self._broadcast_event('server_close', msg)
if players:
self._broadcast_event('game_close', msg)
###############################################
# get login informations
option = dict()
home_dir = os.getenv('HOME')
config_file = home_dir + '/.rotcbot_jabber'
# TODO: This should be replaced by the argparse module
try:
f = open(config_file, 'r')
except IOError, e:
print "Failed to open config file '%s'" % config_file
raise e
for line in f.readlines():
line = line.rstrip('\n')
try:
key, value = line.split(' = ')
except ValueError:
key = line
value = 'true'
option[key] = value
# if irc transport is configured, we connect to the irc conference channel,
# and accept queries from users of that irc server
try:
irc_transport = option['irc_transport']
irc_server = option['irc_server']
irc_channel = option['irc_channel']
irc_jid = "%s%%%s@%s" % (irc_channel, irc_server, irc_transport)
irc = True
print "Found irc configuration."
except KeyError:
irc = False
print "No irc configuration found"
# Check if json output is configured
try:
json_output = option['json_output']
except KeyError:
json_output = None
print "No json output configuration found"
# control_room is a conference room on a jabber server, where the log is posted
# and special commands can be called by whoever is in that room.
# Thus, it would be wise to lock the channel.
try:
control_room = option['control_room']
except KeyError:
control_room = None
try:
bot = RotcBot(option['username'], option['password'], res='prod', json_output=json_output)
except KeyError, e:
print "You need to define username and password for the bot in the config file."
raise e
# define handler functions
# TODO: These should be defined in a subclass
def detail_change(addr, key, oldval, newval):
global bot
if key == 'player_count':
bot._announce_player_change(addr, int(oldval), int(newval))
def new_server(addr):
global bot
bot._announce_new_server(addr)
def server_close(addr):
global bot
bot._announce_server_close(addr)
if addr in bot.server_ids:
bot.server_ids.remove(addr)
def log(s):
global bot, control_room
print s
if control_room:
bot.send(control_room, s, message_type='groupchat')
def error_handler(error):
log("ERR: %s" % error)
def unknown_command(mess, cmd, args):
"""Print the message of the user to the control room."""
if mess.getType() == "groupchat":
return None
jid = str(mess.getFrom()).split('/')[0]
bot.send(control_room, "%s: %s %s" % (jid, cmd, ''.join(args)), message_type='groupchat')
return """Sorry, I don't know that command - but I forwarded your message to the admins, so they might fix that. Use "help" for available commands."""
def was_subscribed(jid):
"""By default, new users get subscribed to the new_game event"""
jid = str(jid)
print "New user subscribed: %s" % jid
groups = bot.roster.getGroups(jid)
groups.append("new_game")
bot.roster.setItem(jid, groups=groups)
bot.send(jid, "Hi! I'll tell you about new rotc games. You can subscribe to various server events, like get notified, when a new empty server is created, or the player count of a server changes. Use the help command, to get more information.")
# End method definitions. Now hook them
bot.error = error_handler
bot.unknown_command = unknown_command
bot.was_subscribed = was_subscribed
bot.log = log
# run the build once before connecting, to avoid anouncing of long open servers
# if the bot crashes
bot._build_server_list()
if not test:
bot.w.callback_new_server = new_server
bot.w.callback_detail_change = detail_change
bot.w.callback_server_close = server_close
# we are started by a supervisor script
# if it gives us previous errors, we'll send them to the admin
def connected():
global bot, irc_jid, control_room
if irc:
print "Joining IRC Channel %s" % irc_jid
bot.join_room(irc_jid)
# join control room (XMPP MUC) if configured
if control_room:
print "Joining Control Room %s" % control_room
bot.join_room(control_room)
try:
supervisor_errors # will raise NameError if it's not defined
log("ERR: The supervisor had to restart the script; here are the messages:")
for err in supervisor_errors:
log("ERR: %s" % str(err))
except NameError:
pass
#### All preparations done. Now, start the bot
bot.serve_forever(connected)