~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to jabberbot/bot.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: iso-8859-1 -*-
 
3
"""
 
4
    MoinMoin - jabber bot main file
 
5
 
 
6
    @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
 
7
    @license: GNU GPL, see COPYING for details.
 
8
"""
 
9
 
 
10
import logging, os, sys, time
 
11
from Queue import Queue
 
12
 
 
13
from jabberbot.config import BotConfig
 
14
from jabberbot.i18n import init_i18n
 
15
from jabberbot.xmppbot import XMPPBot
 
16
from jabberbot.xmlrpcbot import XMLRPCServer, XMLRPCClient
 
17
 
 
18
 
 
19
def _check_xmpp_version():
 
20
    """Checks if available version of pyxmpp is recent enough
 
21
 
 
22
    Since __revision__ is broken in current trunk, we can't rely on it.
 
23
    Therefore a simple check for known problems is used to determine if
 
24
    we can start the bot with it.
 
25
 
 
26
    """
 
27
    import pyxmpp
 
28
 
 
29
    msg = pyxmpp.message.Message()
 
30
    form = pyxmpp.jabber.dataforms.Form()
 
31
 
 
32
    try:
 
33
        msg.add_content(form)
 
34
    except TypeError:
 
35
        print 'Your version of pyxmpp is too old!'
 
36
        print 'You need a least revision 665 to run this bot. Exiting...'
 
37
        sys.exit(1)
 
38
 
 
39
def main():
 
40
    """Starts the jabber bot"""
 
41
 
 
42
    _check_xmpp_version()
 
43
 
 
44
    args = sys.argv
 
45
    if "--help" in args:
 
46
        print """MoinMoin notification bot
 
47
 
 
48
        Usage: %(myname)s [--server server] [--xmpp_port port] [--user user] [--resource resource] [--password pass] [--xmlrpc_host host] [--xmlrpc_port port]
 
49
        """ % {"myname": os.path.basename(args[0])}
 
50
 
 
51
        raise SystemExit
 
52
 
 
53
    log = logging.getLogger(__name__)
 
54
    log.setLevel(logging.DEBUG)
 
55
    log.addHandler(logging.StreamHandler())
 
56
 
 
57
    init_i18n(BotConfig)
 
58
 
 
59
    # TODO: actually accept options from the help string
 
60
    commands_from_xmpp = Queue()
 
61
    commands_to_xmpp = Queue()
 
62
 
 
63
    xmpp_bot = None
 
64
    xmlrpc_client = None
 
65
    xmlrpc_server = None
 
66
 
 
67
    while True:
 
68
        try:
 
69
            if not xmpp_bot or not xmpp_bot.isAlive():
 
70
                log.info("(Re)starting XMPP thread...")
 
71
                xmpp_bot = XMPPBot(BotConfig, commands_from_xmpp, commands_to_xmpp)
 
72
                xmpp_bot.setDaemon(True)
 
73
                xmpp_bot.start()
 
74
 
 
75
            if not xmlrpc_client or not xmlrpc_client.isAlive():
 
76
                log.info("(Re)starting XMLRPC client thread...")
 
77
                xmlrpc_client = XMLRPCClient(BotConfig, commands_from_xmpp, commands_to_xmpp)
 
78
                xmlrpc_client.setDaemon(True)
 
79
                xmlrpc_client.start()
 
80
 
 
81
            if not xmlrpc_server or not xmlrpc_server.isAlive():
 
82
                log.info("(Re)starting XMLRPC server thread...")
 
83
                xmlrpc_server = XMLRPCServer(BotConfig, commands_to_xmpp)
 
84
                xmlrpc_server.setDaemon(True)
 
85
                xmlrpc_server.start()
 
86
 
 
87
            time.sleep(5)
 
88
 
 
89
        except KeyboardInterrupt, i:
 
90
            xmpp_bot.stop()
 
91
            xmlrpc_client.stop()
 
92
 
 
93
            log.info("Stopping XMPP bot thread, please wait...")
 
94
            xmpp_bot.join(5)
 
95
            log.info("Stopping XMLRPC client thread, please wait...")
 
96
            xmlrpc_client.join(5)
 
97
 
 
98
            sys.exit(0)
 
99
 
 
100
 
 
101
if __name__ == "__main__":
 
102
    main()