Back to index

moin  1.9.0~rc2
bot.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: iso-8859-1 -*-
00003 """
00004     MoinMoin - jabber bot main file
00005 
00006     @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import logging, os, sys, time
00011 from Queue import Queue
00012 
00013 from jabberbot.config import BotConfig
00014 from jabberbot.i18n import init_i18n
00015 from jabberbot.xmppbot import XMPPBot
00016 from jabberbot.xmlrpcbot import XMLRPCServer, XMLRPCClient
00017 
00018 
00019 def _check_xmpp_version():
00020     """Checks if available version of pyxmpp is recent enough
00021 
00022     Since __revision__ is broken in current trunk, we can't rely on it.
00023     Therefore a simple check for known problems is used to determine if
00024     we can start the bot with it.
00025 
00026     """
00027     import pyxmpp
00028 
00029     msg = pyxmpp.message.Message()
00030     form = pyxmpp.jabber.dataforms.Form()
00031 
00032     try:
00033         msg.add_content(form)
00034     except TypeError:
00035         print 'Your version of pyxmpp is too old!'
00036         print 'You need a least revision 665 to run this bot. Exiting...'
00037         sys.exit(1)
00038 
00039 def main():
00040     """Starts the jabber bot"""
00041 
00042     _check_xmpp_version()
00043 
00044     args = sys.argv
00045     if "--help" in args:
00046         print """MoinMoin notification bot
00047 
00048         Usage: %(myname)s [--server server] [--xmpp_port port] [--user user] [--resource resource] [--password pass] [--xmlrpc_host host] [--xmlrpc_port port]
00049         """ % {"myname": os.path.basename(args[0])}
00050 
00051         raise SystemExit
00052 
00053     log = logging.getLogger(__name__)
00054     log.setLevel(logging.DEBUG)
00055     log.addHandler(logging.StreamHandler())
00056 
00057     init_i18n(BotConfig)
00058 
00059     # TODO: actually accept options from the help string
00060     commands_from_xmpp = Queue()
00061     commands_to_xmpp = Queue()
00062 
00063     xmpp_bot = None
00064     xmlrpc_client = None
00065     xmlrpc_server = None
00066 
00067     while True:
00068         try:
00069             if not xmpp_bot or not xmpp_bot.isAlive():
00070                 log.info("(Re)starting XMPP thread...")
00071                 xmpp_bot = XMPPBot(BotConfig, commands_from_xmpp, commands_to_xmpp)
00072                 xmpp_bot.setDaemon(True)
00073                 xmpp_bot.start()
00074 
00075             if not xmlrpc_client or not xmlrpc_client.isAlive():
00076                 log.info("(Re)starting XMLRPC client thread...")
00077                 xmlrpc_client = XMLRPCClient(BotConfig, commands_from_xmpp, commands_to_xmpp)
00078                 xmlrpc_client.setDaemon(True)
00079                 xmlrpc_client.start()
00080 
00081             if not xmlrpc_server or not xmlrpc_server.isAlive():
00082                 log.info("(Re)starting XMLRPC server thread...")
00083                 xmlrpc_server = XMLRPCServer(BotConfig, commands_to_xmpp)
00084                 xmlrpc_server.setDaemon(True)
00085                 xmlrpc_server.start()
00086 
00087             time.sleep(5)
00088 
00089         except KeyboardInterrupt, i:
00090             xmpp_bot.stop()
00091             xmlrpc_client.stop()
00092 
00093             log.info("Stopping XMPP bot thread, please wait...")
00094             xmpp_bot.join(5)
00095             log.info("Stopping XMLRPC client thread, please wait...")
00096             xmlrpc_client.join(5)
00097 
00098             sys.exit(0)
00099 
00100 
00101 if __name__ == "__main__":
00102     main()