view start_bots_from_xml_config.py @ 218:ca22fdea1c17

(irclib) minor fixes for 9e494bf91d13f165af272b7bef9422a4ea9100eb (auto-reconnection improvement) Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 06 Mar 2010 23:37:35 +0100
parents 102f895347ff
children
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-


# *** LICENSE ***
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from xml.dom.minidom import parse
from time import sleep
import sys
import traceback

from admin import Admin
from bot import Bot
import say_levels


try:
	if len(sys.argv) > 1:
		config = parse(sys.argv[1])
	else:
		config = parse('config.xml')
except IOError:
	print '[Error] configuration file is missing or cannot be read'
	quit(1)

bots_jids = []
for bot_el in config.getElementsByTagName('bot'):
	if bot_el.getAttribute('jid') in bots_jids:
		print '[Error] you cannot have two bots using the same JID'
		quit(2)
	bots_jids.append(bot_el.getAttribute('jid'))



bots = []
for bot_el in config.getElementsByTagName('bot'):
	debug = False
	if bot_el.hasAttribute('debug'):
		if bot_el.getAttribute('debug') == 'true':
			debug = True
	
	admins = []
	for admin_el in bot_el.getElementsByTagName('admin'):
		if admin_el.hasAttribute('jid'):
			admin = Admin()
			admin.jid = admin_el.getAttribute('jid')
			if admin_el.hasAttribute('say_level'):
				admin.say_level = say_levels.get(admin_el.getAttribute('say_level'))
			else:
				admin.say_level = say_levels.warning
			admins.append(admin)
	
	bot = Bot(bot_el.getAttribute('jid'), bot_el.getAttribute('password'), bot_el.getAttribute('nickname'), admins=admins, debug=debug)
	bots.append(bot)
	for bridge_el in bot_el.getElementsByTagName('bridge'):
		xmpp_room = bridge_el.getElementsByTagName('xmpp-room')[0]
		irc = bridge_el.getElementsByTagName('irc')[0]
		
		irc_connection_interval = 1
		if irc.hasAttribute('connection_interval'):
			try:
				irc_connection_interval = float(irc.getAttribute('connection_interval'))
			except ValueError:
				print '[Error] the value of connection_interval must be a number'
		if irc.hasAttribute('charsets'):
			irc_charsets = irc.getAttribute('charsets').split()
		else:
			irc_charsets = None
		
		if bridge_el.hasAttribute('say_level'):
			say_level = say_levels.get(bridge_el.getAttribute('say_level'))
		else:
			say_level = say_levels.nothing
		
		if bridge_el.hasAttribute('mode'):
			mode = bridge_el.getAttribute('mode')
		else:
			mode = 'bypass'
		
		bot.new_bridge(xmpp_room.getAttribute('jid'), irc.getAttribute('chan'), irc.getAttribute('server'), mode, say_level, irc_connection_interval=irc_connection_interval, irc_charsets=irc_charsets)

try:
	if len(bots) == 0:
		print 'No bots in the configuration file, exiting ...'
		exit(0)
	
	while True:
		for bot in bots:
			if bot.halt and len(bot.xmpp_connections) == 0:
				bots.remove(bot)
		if len(bots) == 0:
			raise Exception()
		sleep(10)
except:
	if len(bots) == 0:
		print 'All bots have been shut down, exiting ...'
		exit(0)
	
	for bot in bots:
		bots.remove(bot)
		del bot
	traceback.print_exc()
	quit(3)