annotate jabberbot/xmlrpcbot.py @ 6073:338348b5d5ad

use proper parameter name in log_attempt() call
author 'Karl O. Pinc' <kop@meme.com>
date Mon, 15 Sep 2014 21:14:44 -0500
parents b397401dda7f
children
rev   line source
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 """
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
3 MoinMoin - a xmlrpc server and client for the notification bot
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
5 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
7 """
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
8
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2502
diff changeset
9 import logging, xmlrpclib, Queue
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2402
diff changeset
10 from SimpleXMLRPCServer import SimpleXMLRPCServer
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
11 from threading import Thread
2088
a99ed13fec1e Introduce a shared secret for wiki-bot communication + some code style fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2084
diff changeset
12
2118
805d56ccbea7 Don't import each command by hand, rather used a shortened module name.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2116
diff changeset
13 import jabberbot.commands as cmd
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
14
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2402
diff changeset
15
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
16 class ConfigurationError(Exception):
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
17
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
18 def __init__(self, message):
2632
584e1714aa05 Fix a missing __init__ argument in ConfigurationError exception.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2629
diff changeset
19 Exception.__init__(self)
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
20 self.message = message
2088
a99ed13fec1e Introduce a shared secret for wiki-bot communication + some code style fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2084
diff changeset
21
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
22
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
23 def _xmlrpc_decorator(function):
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
24 """A decorator function, which adds some maintenance code
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
25
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
26 This function takes care of preparing a MultiCall object and
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
27 an authentication token, and deleting them at the end.
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
28
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
29 """
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
30 def wrapped_func(self, command):
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
31 # Dummy function, so that the string appears in a .po file
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
32 _ = lambda x: x
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
33
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
34 self.token = None
4582
afc6264a134f raised requirement to Python 2.4, removing 2.3 support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3612
diff changeset
35 self.multicall = xmlrpclib.MultiCall(self.connection)
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
36 jid = command.jid
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
37 if type(jid) is not list:
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
38 jid = [jid]
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
39
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
40 try:
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
41 try:
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
42 self.get_auth_token(command.jid)
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
43 if self.token:
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
44 self.multicall.applyAuthToken(self.token)
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
45
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
46 function(self, command)
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
47 self.commands_out.put_nowait(command)
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
48
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
49 except xmlrpclib.Fault, fault:
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
50 msg = _("Your request has failed. The reason is:\n%(error)s")
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
51 self.log.error(str(fault))
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
52 self.report_error(jid, msg, {'error': fault.faultString})
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
53 except xmlrpclib.Error, err:
5887
b397401dda7f fix typos, correct is: 'occurred'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4582
diff changeset
54 msg = _("A serious error occurred while processing your request:\n%(error)s")
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
55 self.log.error(str(err))
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
56 self.report_error(jid, msg, {'error': str(err)})
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
57 except Exception, exc:
5887
b397401dda7f fix typos, correct is: 'occurred'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4582
diff changeset
58 msg = _("An internal error has occurred, please contact the administrator.")
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
59 self.log.critical(str(exc))
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
60 self.report_error(jid, msg)
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
61
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
62 finally:
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
63 del self.token
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
64 del self.multicall
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
65
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
66 return wrapped_func
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
67
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
68 class XMLRPCClient(Thread):
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
69 """XMLRPC Client
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
70
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
71 It's responsible for performing XMLRPC operations on
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
72 a wiki, as inctructed by command objects received from
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
73 the XMPP component"""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
74
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
75 def __init__(self, config, commands_in, commands_out):
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
76 """A constructor
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
77
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
78 @param commands_out: an output command queue (to xmpp)
2402
9c0c2b782737 Typo fix.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2391
diff changeset
79 @param commands_in: an input command queue (from xmpp)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
80
2071
aa761813d7bd Silly changes to test CIA.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2070
diff changeset
81 """
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
82 Thread.__init__(self)
3612
6f06c7d051a4 Setting a more meaningful logger name.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 2957
diff changeset
83 self.log = logging.getLogger(__name__)
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2332 2286
diff changeset
84
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
85 if not config.secret:
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
86 error = "You must set a (long) secret string!"
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
87 self.log.critical(error)
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
88 raise ConfigurationError(error)
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2332 2286
diff changeset
89
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
90 self.commands_in = commands_in
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
91 self.commands_out = commands_out
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2071
diff changeset
92 self.config = config
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
93 self.url = config.wiki_url + "?action=xmlrpc2"
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
94 self.connection = self.create_connection()
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
95 self.token = None
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
96 self.multicall = None
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
97 self.stopping = False
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
98
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
99 self._cmd_handlers = {cmd.GetPage: self.get_page,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
100 cmd.GetPageHTML: self.get_page_html,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
101 cmd.GetPageList: self.get_page_list,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
102 cmd.GetPageInfo: self.get_page_info,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
103 cmd.GetUserLanguage: self.get_language_by_jid,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
104 cmd.Search: self.do_search,
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
105 cmd.RevertPage: self.do_revert}
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
106
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
107 def run(self):
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2071
diff changeset
108 """Starts the server / thread"""
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
109 while True:
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
110 if self.stopping:
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
111 break
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
112
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
113 try:
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
114 command = self.commands_in.get(True, 2)
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
115 self.execute_command(command)
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
116 except Queue.Empty:
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
117 pass
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
118
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
119 def stop(self):
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
120 """Stop the thread"""
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
121 self.stopping = True
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
122
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
123 def create_connection(self):
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
124 return xmlrpclib.ServerProxy(self.url, allow_none=True, verbose=self.config.verbose)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
125
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
126 def execute_command(self, command):
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
127 """Execute commands coming from the XMPP component"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
128
2957
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
129 cmd_name = command.__class__
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
130
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
131 try:
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
132 handler = self._cmd_handlers[cmd_name]
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
133 except KeyError:
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
134 self.log.debug("No such command: " + cmd_name.__name__)
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
135 return
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
136
bc85e5200dd3 GHOP: refactor hardcoded if...elif...else code to use dispatching.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2691
diff changeset
137 handler(command)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
138
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
139 def report_error(self, jid, text, data={}):
2691
64eae2cfb6dc Improve comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2678
diff changeset
140 """Reports an internal error
64eae2cfb6dc Improve comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2678
diff changeset
141
64eae2cfb6dc Improve comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2678
diff changeset
142 @param jid: Jabber ID that should be informed about the error condition
64eae2cfb6dc Improve comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2678
diff changeset
143 @param text: description of the error
64eae2cfb6dc Improve comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2678
diff changeset
144 @param data: dictionary used to substitute strings in translated message
64eae2cfb6dc Improve comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2678
diff changeset
145 @type data: dict
64eae2cfb6dc Improve comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2678
diff changeset
146
64eae2cfb6dc Improve comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2678
diff changeset
147 """
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
148 # Dummy function, so that the string appears in a .po file
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
149 _ = lambda x: x
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
150
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
151 cmddata = {'text': text, 'data': data}
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
152 report = cmd.NotificationCommandI18n(jid, cmddata, msg_type=u"chat", async=False)
2391
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
153 self.commands_out.put_nowait(report)
ddb4c626062d Big checkpoint commit. Details below:
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
154
2123
184975c4b86f Fix GetPage, make it use the token obtained using jid and shared secret.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2121
diff changeset
155 def get_auth_token(self, jid):
2502
d9aef892fda8 Fix xmlrpc commands. How come I've never noticed how b0rked it was?!
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
156 """Get an auth token using user's Jabber ID
d9aef892fda8 Fix xmlrpc commands. How come I've never noticed how b0rked it was?!
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
157
d9aef892fda8 Fix xmlrpc commands. How come I've never noticed how b0rked it was?!
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
158 @type jid: unicode
d9aef892fda8 Fix xmlrpc commands. How come I've never noticed how b0rked it was?!
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
159 """
d9aef892fda8 Fix xmlrpc commands. How come I've never noticed how b0rked it was?!
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
160 # We have to use a bare JID
d9aef892fda8 Fix xmlrpc commands. How come I've never noticed how b0rked it was?!
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
161 jid = jid.split('/')[0]
d9aef892fda8 Fix xmlrpc commands. How come I've never noticed how b0rked it was?!
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2483
diff changeset
162 token = self.connection.getJabberAuthToken(jid, self.config.secret)
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
163 if token:
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
164 self.token = token
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
165
2342
24242585fd70 Warn user if we failed to get a token using his credentials.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
166 def warn_no_credentials(self, jid):
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
167 """Warn a given JID that credentials check failed
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
168
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
169 @param jid: full JID to notify about failure
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
170 @type jid: str
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
171
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
172 """
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
173 # Dummy function, so that the string appears in a .po file
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
174 _ = lambda x: x
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
175
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
176 cmddata = {'text': _("Credentials check failed, you might be unable to see all information.")}
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
177 warning = cmd.NotificationCommandI18n([jid], cmddata, async=False)
2342
24242585fd70 Warn user if we failed to get a token using his credentials.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
178 self.commands_out.put_nowait(warning)
2350
61829d040c63 Don't send html-formatted notifications.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2342
diff changeset
179
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
180 def _get_multicall_result(self, jid):
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
181 """Returns multicall results and issues a warning if there's an auth error
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
182
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
183 @param jid: a full JID to use if there's an error
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
184 @type jid: str
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2653
diff changeset
185
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
186 """
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
187
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
188 if not self.token:
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
189 result = self.multicall()[0]
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
190 token_result = u"FAILURE"
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
191 else:
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
192 token_result, result = self.multicall()
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
193
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
194 if token_result != u"SUCCESS":
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
195 self.warn_no_credentials(jid)
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
196
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
197 return result
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
198
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
199
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
200 def get_page(self, command):
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
201 """Returns a raw page"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
202
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
203 self.multicall.getPage(command.pagename)
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
204 command.data = self._get_multicall_result(command.jid)
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
205
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
206 get_page = _xmlrpc_decorator(get_page)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
207
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
208
2121
ae2134211ac1 XMPP -> wiki XML RPC, the beginning...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2118
diff changeset
209 def get_page_html(self, command):
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
210 """Returns a html-formatted page"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
211
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
212 self.multicall.getPageHTML(command.pagename)
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
213 command.data = self._get_multicall_result(command.jid)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
214
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
215 get_page_html = _xmlrpc_decorator(get_page_html)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
216
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
217
2125
9d5f4ba23a54 Add a GetPageList command. Some code duplication to be refactored later.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2123
diff changeset
218 def get_page_list(self, command):
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
219 """Returns a list of all accesible pages"""
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
220
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
221 # Dummy function, so that the string appears in a .po file
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
222 _ = lambda x: x
2629
5dac44252ca6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2626
diff changeset
223
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
224 cmd_data = {'text': _("This command may take a while to complete, please be patient...")}
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
225 info = cmd.NotificationCommandI18n([command.jid], cmd_data, async=False, msg_type=u"chat")
2125
9d5f4ba23a54 Add a GetPageList command. Some code duplication to be refactored later.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2123
diff changeset
226 self.commands_out.put_nowait(info)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
227
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
228 self.multicall.getAllPages()
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
229 command.data = self._get_multicall_result(command.jid)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
230
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
231 get_page_list = _xmlrpc_decorator(get_page_list)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
232
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
233
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
234 def get_page_info(self, command):
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
235 """Returns detailed information about a given page"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
236
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
237 self.multicall.getPageInfo(command.pagename)
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
238 command.data = self._get_multicall_result(command.jid)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
239
2127
42232c6ac004 Remove code duplication in xmlrpcbot, initial support for getPageInfo()
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2125
diff changeset
240 get_page_info = _xmlrpc_decorator(get_page_info)
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
241
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
242 def do_search(self, command):
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
243 """Performs a search"""
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
244
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
245 # Dummy function, so that the string appears in a .po file
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
246 _ = lambda x: x
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
247
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
248 cmd_data = {'text': _("This command may take a while to complete, please be patient...")}
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
249 info = cmd.NotificationCommandI18n([command.jid], cmd_data, async=False, msg_type=u"chat")
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
250 self.commands_out.put_nowait(info)
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
251
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
252 c = command
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
253 self.multicall.searchPagesEx(c.term, c.search_type, 30, c.case, c.mtime, c.regexp)
2653
11447cada25e Fix layering violation in xmlrpcbot code, as mentioned in TODO.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2650
diff changeset
254 command.data = self._get_multicall_result(command.jid)
2650
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
255
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
256 do_search = _xmlrpc_decorator(do_search)
2b4be3be9835 First implementation of search, will be changed later to optionally use oob.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2647
diff changeset
257
2671
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
258 def do_revert(self, command):
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
259 """Performs a page revert"""
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
260
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
261 # Dummy function, so that the string appears in a .po file
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
262 _ = lambda x: x
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
263
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
264 self.multicall.revertPage(command.pagename, command.revision)
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
265 data = self._get_multicall_result(command.jid)
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
266
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
267 if type(data) == bool and data:
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
268 cmd_data = {'text': _("Page has been reverted.")}
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
269 elif isinstance(str, data) or isinstance(unicode, data):
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
270 cmd_data = {'text': _("Revert failed: %(reason)s" % {'reason': data})}
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
271 else:
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
272 cmd_data = {'text': _("Revert failed.")}
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
273
2678
00b746e6be07 Translate revert notifications.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2671
diff changeset
274 info = cmd.NotificationCommandI18n([command.jid], cmd_data, async=False, msg_type=u"chat")
2671
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
275 self.commands_out.put_nowait(info)
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
276
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
277 do_revert = _xmlrpc_decorator(do_revert)
9bc7d2c14e1f Add suport for reverting pages (text-only for now).
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2659
diff changeset
278
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
279 def get_language_by_jid(self, command):
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
280 """Returns language of the a user identified by the given JID"""
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
281
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
282 server = xmlrpclib.ServerProxy(self.config.wiki_url + "?action=xmlrpc2")
2481
4030f9321319 Fix local variable reference prior to assignment.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2480
diff changeset
283 language = "en"
2483
41e79a4df6b6 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2481
diff changeset
284
2480
b0185066e802 Handle exceptions in get_language_by_jid
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
285 try:
b0185066e802 Handle exceptions in get_language_by_jid
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
286 language = server.getUserLanguageByJID(command.jid)
b0185066e802 Handle exceptions in get_language_by_jid
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
287 except xmlrpclib.Fault, fault:
b0185066e802 Handle exceptions in get_language_by_jid
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
288 self.log.error(str(fault))
b0185066e802 Handle exceptions in get_language_by_jid
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
289 except xmlrpclib.Error, err:
b0185066e802 Handle exceptions in get_language_by_jid
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
290 self.log.error(str(err))
b0185066e802 Handle exceptions in get_language_by_jid
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
291 except Exception, exc:
b0185066e802 Handle exceptions in get_language_by_jid
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
292 self.log.critical(str(exc))
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
293
2481
4030f9321319 Fix local variable reference prior to assignment.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2480
diff changeset
294 command.language = language
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
295 self.commands_out.put_nowait(command)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2413
diff changeset
296
2088
a99ed13fec1e Introduce a shared secret for wiki-bot communication + some code style fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2084
diff changeset
297
2084
5e0c667c79cb Don't inherit SimpleXMLRPCServer, it's dangerous.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
298 class XMLRPCServer(Thread):
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
299 """XMLRPC Server
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
300
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
301 It waits for notifications requests coming from wiki,
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
302 creates command objects and puts them on a queue for
2071
aa761813d7bd Silly changes to test CIA.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2070
diff changeset
303 later processing by the XMPP component
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
304
2071
aa761813d7bd Silly changes to test CIA.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2070
diff changeset
305 @param commands: an input command queue
aa761813d7bd Silly changes to test CIA.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2070
diff changeset
306 """
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
307
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
308 def __init__(self, config, commands):
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
309 Thread.__init__(self)
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
310 self.commands = commands
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
311 self.verbose = config.verbose
3612
6f06c7d051a4 Setting a more meaningful logger name.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 2957
diff changeset
312 self.log = logging.getLogger(__name__)
2647
6e323cf20c64 Fix initialisation of xmlrpcbot.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2645
diff changeset
313 self.config = config
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
314
2319
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
315 if config.secret:
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
316 self.secret = config.secret
10ec0e8d35e1 Make send_notification accept a list of JIDs, instead of one.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2167
diff changeset
317 else:
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
318 error = "You must set a (long) secret string"
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
319 self.log.critical(error)
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2319
diff changeset
320 raise ConfigurationError(error)
2645
3f499601e702 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2641
diff changeset
321
2641
b0a8154e8874 Create SimpleXMLRPCServer when the thread is actually started.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2638
diff changeset
322 self.server = None
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
323
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
324 def run(self):
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
325 """Starts the server / thread"""
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
326
2647
6e323cf20c64 Fix initialisation of xmlrpcbot.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2645
diff changeset
327 self.server = SimpleXMLRPCServer((self.config.xmlrpc_host, self.config.xmlrpc_port))
2645
3f499601e702 Whitespace fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2641
diff changeset
328
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
329 # Register methods having an "export" attribute as XML RPC functions and
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
330 # decorate them with a check for a shared (wiki-bot) secret.
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
331 items = self.__class__.__dict__.items()
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
332 methods = [(name, func) for (name, func) in items if callable(func)
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
333 and "export" in func.__dict__]
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
334
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
335 for name, func in methods:
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
336 self.server.register_function(self.secret_check(func), name)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
337
2084
5e0c667c79cb Don't inherit SimpleXMLRPCServer, it's dangerous.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
338 self.server.serve_forever()
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2167
diff changeset
339
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
340 def secret_check(self, function):
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
341 """Adds a check for a secret to a given function
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
342
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
343 Using this one does not have to worry about checking for the secret
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
344 in every XML RPC function.
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
345 """
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
346 def protected_func(secret, *args):
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
347 if secret != self.secret:
2626
a791fc964e09 Attempt to move i18n from xmlrpc to xmpp thread.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
348 raise xmlrpclib.Fault(1, "You are not allowed to use this bot!")
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
349 else:
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
350 return function(self, *args)
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
351
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
352 return protected_func
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
353
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
354
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
355 def send_notification(self, jids, notification):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
356 """Instructs the XMPP component to send a notification
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
357
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
358 The notification dict has following entries:
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
359 'text' - notification text (REQUIRED)
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
360 'subject' - notification subject
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
361 'url_list' - a list of dicts describing attached URLs
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
362
2332
456545b5acfe Handle errors in get_auth_token() gracefully, fix a comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2322
diff changeset
363 @param jids: a list of JIDs to send a message to (bare JIDs)
456545b5acfe Handle errors in get_auth_token() gracefully, fix a comment.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2322
diff changeset
364 @type jids: a list of str or unicode
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
365 @param notification: dictionary with notification data
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2635
diff changeset
366 @type notification: dict
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
367
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
368 """
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2632
diff changeset
369 command = cmd.NotificationCommand(jids, notification, async=True)
2131
ea879bd7935d Fix some names as pointed out by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2128
diff changeset
370 self.commands.put_nowait(command)
2070
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
371 return True
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
372 send_notification.export = True
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
373
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
374 def addJIDToRoster(self, jid):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
375 """Instructs the XMPP component to add a new JID to its roster
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
376
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
377 @param jid: a jid to add, this must be a bare jid
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
378 @type jid: str or unicode,
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
379
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
380 """
2131
ea879bd7935d Fix some names as pointed out by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2128
diff changeset
381 command = cmd.AddJIDToRosterCommand(jid)
ea879bd7935d Fix some names as pointed out by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2128
diff changeset
382 self.commands.put_nowait(command)
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
383 return True
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
384 addJIDToRoster.export = True
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
385
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
386 def removeJIDFromRoster(self, jid):
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
387 """Instructs the XMPP component to remove a JID from its roster
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
388
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
389 @param jid: a jid to remove, this must be a bare jid
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
390 @type jid: str or unicode
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
391
2116
7f579f5fd44b Improve docstrings. s/type/msgtype/ to avoid name conflict with builtin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
392 """
2131
ea879bd7935d Fix some names as pointed out by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2128
diff changeset
393 command = cmd.RemoveJIDFromRosterCommand(jid)
ea879bd7935d Fix some names as pointed out by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2128
diff changeset
394 self.commands.put_nowait(command)
2096
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
395 return True
b37dd51e566e Use decorators to assure that shared secret is always checked. Add 2 commands.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2089
diff changeset
396 removeJIDFromRoster.export = True