annotate jabberbot/bot.py @ 5816:b661a89ef0be

xmlrpc: new methods xmlrpc_getProcessingInstruction and xmlrpc_getProcessingInstructionVersion
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 25 Dec 2011 23:56:09 +0100
parents be610b894f10
children
rev   line source
2670
8afeb0c659b1 Add a hasbang to make launching of the bot easier.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2669
diff changeset
1 #!/usr/bin/env python
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 # -*- coding: iso-8859-1 -*-
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
3 """
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4 MoinMoin - jabber bot main file
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
5
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
6 @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: 2067
diff changeset
7 @license: GNU GPL, see COPYING for details.
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
8 """
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
9
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
10 import logging, os, sys, time
2108
c0c7c4cd4eee Fix more relative imports.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
11 from Queue import Queue
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
12
2112
a4d95b9d8f37 Fix a relative import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
13 from jabberbot.config import BotConfig
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
14 from jabberbot.i18n import init_i18n
2108
c0c7c4cd4eee Fix more relative imports.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
15 from jabberbot.xmppbot import XMPPBot
c0c7c4cd4eee Fix more relative imports.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
16 from jabberbot.xmlrpcbot import XMLRPCServer, XMLRPCClient
2067
3ca2b7065a05 Jabber bot - the beginning.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
17
2088
a99ed13fec1e Introduce a shared secret for wiki-bot communication + some code style fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
18
2683
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
19 def _check_xmpp_version():
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
20 """Checks if available version of pyxmpp is recent enough
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
21
2685
c960a907690e Spelling fix.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2683
diff changeset
22 Since __revision__ is broken in current trunk, we can't rely on it.
2683
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
23 Therefore a simple check for known problems is used to determine if
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
24 we can start the bot with it.
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
25
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
26 """
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
27 import pyxmpp
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
28
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
29 msg = pyxmpp.message.Message()
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
30 form = pyxmpp.jabber.dataforms.Form()
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
31
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
32 try:
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
33 msg.add_content(form)
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
34 except TypeError:
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
35 print 'Your version of pyxmpp is too old!'
5420
be610b894f10 jabberbot: clarified pyxmpp requirements
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3612
diff changeset
36 print 'You need a least pyxmpp SVN revision 665 to run this bot.'
be610b894f10 jabberbot: clarified pyxmpp requirements
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3612
diff changeset
37 print 'pyxmpp release 1.0.1 works, too.'
be610b894f10 jabberbot: clarified pyxmpp requirements
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3612
diff changeset
38 print 'Exiting...'
2683
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
39 sys.exit(1)
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
40
2083
9c66c85a6a70 Fix some reasonable stuff suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2071
diff changeset
41 def main():
2683
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
42 """Starts the jabber bot"""
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
43
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
44 _check_xmpp_version()
58660fc20599 Add a runtime test for pyxmpp version (detect api incompatibility)
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2670
diff changeset
45
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
46 args = sys.argv
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
47 if "--help" in args:
4e123eb32380 Handle inbound xmlrpc traffic, send notifications. This is only a beginning. ;)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2068
diff changeset
48 print """MoinMoin notification bot
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
49
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
50 Usage: %(myname)s [--server server] [--xmpp_port port] [--user user] [--resource resource] [--password pass] [--xmlrpc_host host] [--xmlrpc_port port]
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
51 """ % {"myname": os.path.basename(args[0])}
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
52
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
53 raise SystemExit
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
54
3612
6f06c7d051a4 Setting a more meaningful logger name.
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 2685
diff changeset
55 log = logging.getLogger(__name__)
2322
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2121
diff changeset
56 log.setLevel(logging.DEBUG)
4b4868ae88f0 Replace prints with stdlib's logging features.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2121
diff changeset
57 log.addHandler(logging.StreamHandler())
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2322 2286
diff changeset
58
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
59 init_i18n(BotConfig)
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
60
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
61 # TODO: actually accept options from the help string
2068
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
62 commands_from_xmpp = Queue()
7e2a9b7d2bc6 XML RPC functionality for the notification bot - the beginning
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2067
diff changeset
63 commands_to_xmpp = Queue()
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
64
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
65 xmpp_bot = None
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
66 xmlrpc_client = None
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
67 xmlrpc_server = None
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
68
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
69 while True:
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
70 try:
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
71 if not xmpp_bot or not xmpp_bot.isAlive():
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
72 log.info("(Re)starting XMPP thread...")
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
73 xmpp_bot = XMPPBot(BotConfig, commands_from_xmpp, commands_to_xmpp)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
74 xmpp_bot.setDaemon(True)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
75 xmpp_bot.start()
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
76
2659
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
77 if not xmlrpc_client or not xmlrpc_client.isAlive():
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
78 log.info("(Re)starting XMLRPC client thread...")
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
79 xmlrpc_client = XMLRPCClient(BotConfig, commands_from_xmpp, commands_to_xmpp)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
80 xmlrpc_client.setDaemon(True)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
81 xmlrpc_client.start()
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
82
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
83 if not xmlrpc_server or not xmlrpc_server.isAlive():
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
84 log.info("(Re)starting XMLRPC server thread...")
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
85 xmlrpc_server = XMLRPCServer(BotConfig, commands_to_xmpp)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
86 xmlrpc_server.setDaemon(True)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
87 xmlrpc_server.start()
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
88
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
89 time.sleep(5)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
90
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
91 except KeyboardInterrupt, i:
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
92 xmpp_bot.stop()
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
93 xmlrpc_client.stop()
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
94
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
95 log.info("Stopping XMPP bot thread, please wait...")
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
96 xmpp_bot.join(5)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
97 log.info("Stopping XMLRPC client thread, please wait...")
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
98 xmlrpc_client.join(5)
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
99
892055d122fb Handle process/thread termination gracefully.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2461
diff changeset
100 sys.exit(0)
2088
a99ed13fec1e Introduce a shared secret for wiki-bot communication + some code style fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2083
diff changeset
101
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
102
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
103 if __name__ == "__main__":
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2121
diff changeset
104 main()