cobalt.py

Sun, 09 Nov 2014 22:35:46 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 09 Nov 2014 22:35:46 +0200
changeset 70
3fd78ed9a19f
parent 69
1b84de3e0d29
parent 68
ecc161d4a6fa
permissions
-rwxr-xr-x

- merged the two fixes

#!/usr/bin/env python
'''
	Copyright 2014 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. The name of the author may not be used to endorse or promote products
	   derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''

import asyncore
import socket
import time
import sys
import traceback
import re
import urllib
import urllib2
import hgapi
import os
import suds
import math
import json
from datetime import datetime
import modulecore as ModuleCore
import configfile
from configfile import Config

ModuleCore.init_data()

try:
	uid = os.geteuid()
except:
	uid = -1

if uid == 0 and raw_input ('Do you seriously want to run cobalt as root? [y/N] ') != 'y':
	quit()

configfile.init()
g_admins = Config.get_value ('admins', default=[])
g_mynick = Config.get_value ('nickname', default='cobalt')
g_BotActive = False
g_needCommitsTxtRebuild = True

#
# SOAP stuff
#
suds_active = False

try:
	print 'Initializing MantisBT connection...'
	suds_import = suds.xsd.doctor.Import ('http://schemas.xmlsoap.org/soap/encoding/', 'http://schemas.xmlsoap.org/soap/encoding/')
	suds_client = suds.client.Client ('https://zandronum.com/tracker/api/soap/mantisconnect.php?wsdl', plugins=[suds.xsd.doctor.ImportDoctor (suds_import)])
	suds_active = True
except Exception as e:
	print 'Failed to establish MantisBT connection: ' + `e`
	pass

btannounce_active = False
btannounce_timeout = 0

def bt_updatechecktimeout():
	global btannounce_timeout
	btannounce_timeout = time.time() + (Config.get_node ('bt').get_value ('checkinterval', default=5) * 60)

def bt_credentials():
	bt = Config.get_node ('bt')
	user = bt.get_value ('username', '')
	password = bt.get_value ('password', '')
	return [user, password]

if suds_active:
	sys.stdout.write ('Retrieving latest tracker ticket... ')
	user, password = bt_credentials()
	btannounce_id = suds_client.service.mc_issue_get_biggest_id (user, password, 0)
	btannounce_active = True
	bt_updatechecktimeout()
	print btannounce_id

def bt_getissue(ticket):
	global suds_client
	user, password = bt_credentials()
	return suds_client.service.mc_issue_get (user, password, ticket)

def bt_checklatest():
	global btannounce_timeout
	global btannounce_id

	if time.time() >= btannounce_timeout:
		bt_updatechecktimeout()
		newid = btannounce_id
		try:
			user, password = bt_credentials()
			newid = suds_client.service.mc_issue_get_biggest_id (user, password, 0)
		except Exception as e:
			pass

		while newid > btannounce_id:
			try:
				btannounce_id += 1
				data = bt_getissue (btannounce_id)

				for client in g_clients:
					client.announce_ticket (data)
			except Exception as e:
				pass

#
# irc_client flags
#
CLIF_CONNECTED = (1 << 1)

#
# List of all clients
#
g_clients = []

class channel (object):
	name = ""
	password = ""

	def __init__ (self, name):
		self.name = name

#
# Prints a line to log channel(s)
#
def chanlog (line):
	for client in g_clients:
		if not client.flags & CLIF_CONNECTED:
			continue

		for channel in client.channels:
			if channel.get_value ('logchannel', default=False):
				client.write ("PRIVMSG %s :%s" % (channel.get_value ('name'), line))

#
# Exception handling
#
def handle_exception(excType, excValue, trace):
	excepterm (traceback.format_exception(excType, excValue, trace))

def excepterm(data):
	for segment in data:
		for line in segment.splitlines():
			print line
			chanlog (line)

	for client in g_clients:
		if len(data) > 0:
			client.exceptdie()
		else:
			client.quit_irc()

	if g_BotActive:
		restart_self()
	else:
		quit()

sys.excepthook = handle_exception

def check_admin (sender, ident, host, command):
	if not "%s@%s" % (ident, host) in g_admins:
		raise logical_exception (".%s requires admin access" % command)

class logical_exception (Exception):
	def __init__ (self, value):
		self.value = value
	def __str__ (self):
		return self.value

# from http://www.daniweb.com/software-development/python/code/260268/restart-your-python-program
def restart_self():
	python = sys.executable
	os.execl (python, python, * sys.argv)

def make_commits_txt():
	global g_needCommitsTxtRebuild

	if g_needCommitsTxtRebuild == False:
		return

	print 'Building commits.txt...'
	# Update zandronum-everything
	repo = hgapi.Repo ('zandronum-everything')
	repo.hg_command ('pull', '../zandronum-sandbox')
	repo.hg_command ('pull', '../zandronum-sandbox-stable')

	data = repo.hg_command ('log', '--template', '{node} {date|hgdate}\n')

	f = open ('commits.txt', 'w')

	for line in data.split ('\n'):
		if line == '':
			continue

		words = line.split (' ')
		timestamp = int (words[1])
		f.write ('%s %s\n' % (words[0], datetime.utcfromtimestamp (timestamp).strftime ('%y%m%d-%H%M')))
	f.close()
	g_needCommitsTxtRebuild = False
#enddef

' Check if a repository exists '
def check_repo_exists (repo_name, repo_owner):
	print 'Checking that %s exists...' % repo_name
	repo_url = 'https://bitbucket.org/%s/%s' % (repo_owner, repo_name)
	zanrepo = hgapi.Repo (repo_name)

	try:
		zanrepo.hg_command ('id', '.')
	except hgapi.hgapi.HgException:
		# If the repo does not exist, clone it. zandronum-everything can be spawned off other repos though
		if repo_name == 'zandronum-everything':
			if not os.path.exists (repo_name):
				os.makedirs (repo_name)

			global g_needCommitsTxtRebuild
			g_needCommitsTxtRebuild = True
			print 'Init %s' % repo_name
			zanrepo.hg_command ('init')
			print 'Cloning zandronum-sandbox into %s' % repo_name
			zanrepo.hg_command ('pull', '../zandronum-sandbox')
			print 'Cloning zandronum-sandbox-stable into %s' % repo_name
			zanrepo.hg_command ('pull', '../zandronum-sandbox-stable')
			print 'Done'
			make_commits_txt()
			return
		#fi

		try:
			print 'Cloning %s...' % repo_name
			zanrepo.hg_clone (repo_url, repo_name)
			print 'Cloning done.'
		except Exception as e:
			print 'Unable to clone %s from %s: %s' % (repo_name, repo_url, str (`e`))
			quit(1)
	#tried
#enddef

check_repo_exists ('zandronum', 'Torr_Samaho')
check_repo_exists ('zandronum-stable', 'Torr_Samaho')
check_repo_exists ('zandronum-sandbox', 'crimsondusk')
check_repo_exists ('zandronum-sandbox-stable', 'crimsondusk')
check_repo_exists ('zandronum-everything', '')

repocheck_timeout = (time.time()) + 15

def get_commit_data (zanrepo, rev, template):
	return zanrepo.hg_command ('log', '-l', '1', '-r', rev, '--template', template)
#enddef

def decipher_hgapi_error (e):
	# Blah, hgapi, why must your error messages be so mangled?
	try:
		rawmsg = e.message.replace('\n', '').replace('" +','').replace('\t','')
		errmsg = re.compile (r'.*: tErr: (.*)Out:.*').match (rawmsg).group (1)
		return [True, errmsg]
	except:
		return [False, '']
	#endtry
#enddef

def bbcodify(commit_diffstat):
	result=''
	for line in commit_diffstat.split('\n'):
		# Add green color-tags for the ++++++++++ stream
		rex = re.compile (r'^(.*)\|(.*) (\+*)(-*)(.*)$')
		match = rex.match (line)
		if match:
			line = '%s|%s [color=#5F7]%s[/color][color=#F53]%s[/color]%s\n' \
				% (match.group (1), match.group (2), match.group (3), match.group (4), match.group (5))

			# Tracker doesn't seem to like empty color tags
			line = line.replace ('[color=#5F7][/color]', '').replace ('[color=#F53][/color]', '')
		#else:
			#rex = re.compile (r'^(.*) ([0-9]+) insertions\(\+\), ([0-9]+) deletions\(\-\)$')
			#match = rex.match (line)
			#if match:
				#line = '%s [b][color=green]%s[/color][/b] insertions, [b][color=red]%s[/color][/b] deletions\n' \
					#% (match.group (1), match.group (2), match.group (3))

		result += line
	#done

	return result
#enddef

def find_developer_by_email (commit_email):
	for developer, emails in Config.get_value ('developer_emails', default={}).iteritems():
		for email in emails:
			if commit_email == email:
				return developer
			#fi
		#done
	#done

	return ''
#enddef

' Retrieves and processes commits for zandronum repositories '
' Ensure both repositories are OK before using this! '
def process_zan_repo_updates():
	global repocheck_timeout
	if time.time() < repocheck_timeout:
		return
	
	for n in ['zandronum-stable', 'zandronum', 'zandronum-sandbox', 'zandronum-sandbox-stable']:
		process_one_repo (n)
	
	hgns = Config.get_node ('hg')
	repocheck_timeout = time.time() + hgns.get_value ('checkinterval', default=15) * 60

def process_one_repo (repo_name):
	global repocheck_timeout
	global suds_client
	global g_clients

	hgns = Config.get_node ('hg')
	
	if not hgns.get_value ('track', default=True):
		return

	usestable = repo_name == 'zandronum-stable'
	usesandbox = repo_name == 'zandronum-sandbox' or repo_name == 'zandronum-sandbox-stable'
	repo_owner = 'Torr_Samaho' if not usesandbox else 'crimsondusk'
	repo_url = 'https://bitbucket.org/%s/%s' % (repo_owner, repo_name)
	num_commits = 0
	btuser, btpassword = bt_credentials()
	zanrepo = hgapi.Repo (repo_name)
	commit_data = []
	delimeter = '@@@@@@@@@@'
	print 'Checking %s for updates' % repo_name

	try:
		data = zanrepo.hg_command ('incoming', '--quiet', '--template',
			'{node|short} {desc}' + delimeter)
	except hgapi.hgapi.HgException as e:
		deciphered = decipher_hgapi_error (e)

		if deciphered[0] and len(deciphered[1]) > 0:
			chanlog ("error while using hg import on %s: %s" % (repo_name, deciphered[1]))
		#fi

		return
	except Exception as e:
		chanlog ("%s" % `e`)
		return
	#tried

	for line in data.split (delimeter):
		if line == '':
			continue
		#fi

		rex = re.compile (r'([^ ]+) (.+)')
		match = rex.match (line)
		failed = False

		if not match:
			chanlog ('malformed hg data: %s' % line)
			continue
		#fi

		commit_node = match.group (1)
		commit_message = match.group (2)
		commit_data.append ([commit_node, commit_message])
	#done

	if len (commit_data) > 0:
		pull_args = [];

		for commit in commit_data:
			pull_args.append ('-r');
			pull_args.append (commit[0]);
		#done

		try:
			zanrepo.hg_command ('pull', *pull_args)

			# Also pull these commits to the zandronum main repository
			if usestable:
				devrepo = hgapi.Repo ('zandronum')
				devrepo.hg_command ('pull', '../zandronum-stable', *pull_args)
			#fi

			# Pull everything into sandboxes too
			if not usesandbox:
				devrepo = hgapi.Repo ('zandronum-sandbox')
				devrepo.hg_command ('pull', '../%s' % repo_name, *pull_args)

				devrepo = hgapi.Repo ('zandronum-sandbox-stable')
				devrepo.hg_command ('pull', '../%s' % repo_name, *pull_args)
			#fi

			devrepo = hgapi.Repo ('zandronum-everything')
			devrepo.hg_command ('pull', '../%s' % repo_name, *pull_args)

			global g_needCommitsTxtRebuild
			g_needCommitsTxtRebuild = True
		except Exception as e:
			chanlog ('Warning: unable to pull: %s' % `e`)
			return
		#tried
	#fi

	for commit in commit_data:
		commit_node = commit[0]
		commit_message = commit[1]
		print 'Processing new commit: %s' % commit_node

		try:
			if usesandbox:
				commit_author = get_commit_data (zanrepo, commit_node, '{author}')
				commit_url = '%s/commits/%s' % (repo_url, commit_node)
				commit_email = ''

				# Remove the email address from the author if possible
				rex = re.compile (r'^(.+) <([^>]+)>$.*')
				match = rex.match (commit_author)
				if match:
					commit_author = match.group (1)
					commit_email = match.group (2)
				#fi

				commit_trackeruser = find_developer_by_email (commit_email)
				committer = commit_trackeruser if commit_trackeruser != '' else commit_author

				for irc_client in g_clients:
					for channel in irc_client.cfg['channels']:
						if channel.get_value ('btprivate', False):
							irc_client.privmsg (channel.get_value ('name'),
								"%s: new commit %s by %s: %s"
								% (repo_name, commit_node, committer, commit_url))

							for line in commit_message.split ('\n'):
								irc_client.privmsg (channel.get_value ('name'), line)
						#fi
					#done
				#done

				num_commits += 1
				continue
			#fi

			rex = re.compile (r'^.*(fixes|resolves|addresses|should fix) ([0-9]+).*$')
			match = rex.match (commit_message)

			if not match:
				continue # no "fixes" message in the commit
			#fi

			ticket_id = int (match.group (2))

			# Acquire additional data
			moredata = get_commit_data (zanrepo, commit_node,
				'{author|nonempty}\n{date(date, \'%A %d %B %Y %T\')}').split('\n')

			if len (moredata) != 2:
				chanlog ('error while processing %s: malformed hg data' % commit_node)
				continue
			#fi

			commit_author = moredata[0]
			commit_date = moredata[1]
			commit_email = ""

			try:
				ticket_data = suds_client.service.mc_issue_get (btuser, btpassword, ticket_id)
			except Exception as e:
				chanlog ('error while processing %s: %s' % (commit_node, `e`))
				continue
			#tried

			# Remove the email address from the author if possible
			rex = re.compile (r'^(.+) <([^>]+)>$.*')
			match = rex.match (commit_author)
			if match:
				commit_author = match.group (1)
				commit_email = match.group (2)
			#fi

			commit_diffstat = zanrepo.hg_command ('diff', '--change', commit_node, '--stat')

			if len(commit_diffstat) > 0:
				# commit_diffstat = 'Changes in files:\n[code]\n' + commit_diffstat + '\n[/code]'
				commit_diffstat = 'Changes in files:\n' + bbcodify(commit_diffstat)
			else:
				commit_diffstat = 'No changes in files.'

			# Compare the email addresses against known developer usernames
			commit_trackeruser = find_developer_by_email (commit_email)

			if commit_trackeruser != '':
				commit_author += ' [%s]' % commit_trackeruser
			#fi

			message = 'Issue addressed by commit %s: [b][url=%s/commits/%s]%s[/url][/b]' \
				% (commit_node, repo_url, commit_node, commit_message)
			message += "\nCommitted by %s on %s\n\n%s" \
					% (commit_author, commit_date, commit_diffstat)

			need_update = False

			# If not already set, set handler
			if not 'handler' in ticket_data:
				ticket_data['handler'] = {'name': commit_trackeruser}
				need_update = True
			#fi

			# Find out the status level of the ticket
			needs_testing_level = 70

			if ticket_data['status']['id'] < needs_testing_level:
				ticket_data.status['id'] = needs_testing_level
				need_update = True
			#fi

			# Set target version if not set
			if not 'target_version' in ticket_data:
				ticket_data['target_version'] = '1.4' if repo_name == 'zandronum-stable' else '2.0'
				need_update = True
			elif (ticket_data['target_version'] == '2.0' or ticket_data['target_version'] == '2.0-beta') \
			and repo_name == 'zandronum-stable':
				# Target version was 2.0 but this was just committed to zandronum-stable, adjust
				ticket_data['target_version'] = '1.4'
				need_update = True
			elif ticket_data['target_version'] == '2.0-beta':
				# Fix target version from 2.0-beta to 2.0
				ticket_data['target_version'] = '2.0'
				need_update = True
			#fi

			# Announce on IRC
			for irc_client in g_clients:
				for channel in irc_client.channels:
					if channel.get_value ('btannounce', default=True):
						irc_client.privmsg (channel.get_value ('name'),
							"%s: commit %s fixes issue %d: %s"
							% (repo_name, commit_node, ticket_id, commit_message))
						irc_client.privmsg (channel.get_value ('name'),
							"Read all about it here: " + irc_client.get_ticket_url (ticket_id))
					#fi
				#done
			#done

			if need_update:
				# We need to remove the note data, otherwise the ticket notes
				# will get unnecessary updates. WTF, MantisBT?
				ticket_data.notes = []
				suds_client.service.mc_issue_update (btuser, btpassword, ticket_id, ticket_data)
			#fi

			suds_client.service.mc_issue_note_add (btuser, btpassword, ticket_id, { 'text': message })
			num_commits += 1
		except Exception as e:
			chanlog ('Error while processing %s: %s' % (commit_node, `e`))
			continue
		#tried
	#done
#enddef

def plural (a):
	return '' if a == 1 else 's'

#
# Main IRC client class
#
class irc_client (asyncore.dispatcher):
	def __init__ (self, cfg, flags):
		self.name = cfg.get_value ('name')
		self.host = cfg.get_value ('address')
		self.port = cfg.get_value ('port', default=6667)
		self.password = cfg.get_value ('password', default='')
		self.channels = cfg.get_nodelist ('channels')
		self.flags = flags
		self.send_buffer = []
		self.umode = cfg.get_value ('umode', default='')
		self.cfg = cfg
		self.desired_name = Config.get_value ('nickname', default='cobalt')
		self.mynick = self.desired_name
		self.verbose = Config.get_value ('verbose', default=False)
		self.commandprefix = Config.get_value ('commandprefix', default='.')

		g_clients.append (self)
		asyncore.dispatcher.__init__ (self)
		self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
		self.connect ((self.host, self.port))

	def register_to_irc (self):
		ident = Config.get_value ('ident', default='cobalt')
		gecos = Config.get_value ('gecos', default='cobalt')
		self.write ("PASS %s" % self.password)
		self.write ("USER %s * * :%s" % (ident, gecos))
		self.write ("NICK %s" % self.mynick)

	def handle_connect (self):
		print "Connected to [%s] %s:%d" % (self.name, self.host, self.port)
		self.register_to_irc()

	def write (self, utfdata):
		try:
			self.send_buffer.append ("%s" % utfdata.decode("utf-8","ignore").encode("ascii","ignore"))
		except UnicodeEncodeError:
			pass

	def handle_close (self):
		print "Connection to [%s] %s:%d terminated." % (self.name, self.host, self.port)
		self.close()

	def handle_write (self):
		self.send_all_now()

	def readable (self):
		return True

	def writable (self):
		return len (self.send_buffer) > 0

	def send_all_now (self):
		for line in self.send_buffer:
			if self.verbose:
				print "[%s] <- %s" % (self.name, line)
			self.send ("%s\n" % line)
		self.send_buffer = []

	def handle_read (self):
		lines = self.recv (4096).splitlines()
		for utfline in lines:
			try:
				line = utfline.decode("utf-8","ignore").encode("ascii","ignore")
			except UnicodeDecodeError:
				continue

			if self.verbose:
				print "[%s] -> %s" % (self.name, line)

			if line.startswith ("PING :"):
				self.write ("PONG :%s" % line[6:])
			else:
				words = line.split(" ")
				if len(words) >= 2:
					if words[1] == "001":
						self.flags |= CLIF_CONNECTED

						for channel in self.channels:
							self.write ("JOIN %s %s" % (channel.get_value ('name'), channel.get_value ('password', default='')))

						umode = self.cfg.get_value ('umode', '')
						
						if umode != '':
							self.write ('MODE %s %s' % (self.mynick, self.cfg.get_value ('umode', '')))
					elif words[1] == "PRIVMSG":
						self.handle_privmsg (line)
					elif words[1] == 'QUIT':
						rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) QUIT')
						match = rex.match (line)

						# Try reclaim our nickname if possible
						if match and match.group(1) == self.desired_name:
							self.mynick = self.desired_name
							self.write ("NICK %s" % self.mynick)
					elif words[1] == "433":
						#:irc.localhost 433 * cobalt :Nickname is already in use.
						self.mynick += self.cfg.get_value ('conflictsuffix', default='`')
						self.write ("NICK " + self.mynick)

			# Check for new issues on the bugtracker
			bt_checklatest()

			# Check for new commits in the repositories
			process_zan_repo_updates()

	def channel_by_name (self, name):
		for channel in self.channels:
			if channel.get_value ('name').upper() == args[0].upper():
				return channel
		else:
			raise logical_exception ('unknown channel ' + args[0])

	# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	#
	#	Handle a PRIVMSG line from the IRC server
	#
	def handle_privmsg (self, line):
		rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$')
		match = rex.match (line)
		if match:
			sender = match.group (1)
			user = match.group (2)
			host = match.group (3)
			channel = match.group (4)
			message = match.group (5)
			replyto = channel if channel != g_mynick else sender

			# Check for tracker url in the message
			url = Config.get_node ('bt').get_value ('url')
			http_regex = re.compile (r'.*http(s?)://%s/view\.php\?id=([0-9]+).*' % url)
			http_match = http_regex.match (line)

			# Check for command.
			if len(message) >= 2 and message[0] == self.commandprefix and message[1] != self.commandprefix:
				stuff = message[1:].split(' ')
				command = stuff[0]
				args = stuff[1:]
				try:
					self.handle_command (sender, user, host, replyto, command, args, message)
				except logical_exception as e:
					for line in e.value.split ('\n'):
						if len(line) > 0:
							self.privmsg (replyto, "error: %s" % line)
			elif http_match:
				self.get_ticket_data (replyto, http_match.group (2), False)
		else:
			chanlog ("Recieved bad PRIVMSG: %s" % line)

	# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	#
	#	Get the URL for a specified ticket
	#
	def get_ticket_url (self, ticket):
		url = Config.get_node ('bt').get_value ('url')
		return 'https://%s/view.php?id=%s' % (url, ticket)

	# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	#
	#	Retrieve a ticket from mantisbt
	#
	def get_ticket_data (self, replyto, ticket, withlink):
		if suds_active == False:
			return

		data = {}
		try:
			data = bt_getissue (ticket)
		except Exception, e:
			self.privmsg (replyto, "Failed to get info for issue %s: %s" % (ticket, `e`))

		if data:
			if data['view_state']['name'] == 'private':
				allowprivate = False

				for channel in self.channels:
					if channel.get_value ('name') == replyto and channel.get_value ('btprivate', False):
						allowprivate = True
						break
					#fi
				#done

				if not allowprivate:
					self.privmsg (replyto, 'Error: ticket %s is private' % ticket)
					return
				#fi
			#fi

			self.privmsg (replyto, "Issue %s: %s: Reporter: %s, assigned to: %s, status: %s (%s)" % \
				(ticket, \
				data.summary, \
				data.reporter.name if hasattr (data.reporter, 'name') else "<unknown>", \
				data.handler.name if hasattr (data, 'handler') else "nobody", \
				data.status.name, \
				data.resolution.name))

			if withlink:
				self.privmsg (replyto, "Read all about it here: " + self.get_ticket_url (ticket))
			#fi
		#fi
	#enddef

	def is_admin (self, ident, host):
		return ("%s@%s" % (ident, host)) in g_admins

	# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	#
	#	Process an IRC command
	#
	def handle_command (self, sender, ident, host, replyto, command, args, message):
		kvargs = {'sender': sender, 'ident': ident, 'host': host, 'replyto': replyto, 'cmdname': command, 'message': message}

		try:
			result = ModuleCore.call_command (self, **kvargs)

			if result:
				return
		except ModuleCore.CommandError as e:
			lines = str (e).split ('\n')
			self.privmsg (replyto, 'error: %s' % lines[0])

			for line in lines[1:]:
				self.privmsg (replyto, '  ' + line)
			return
		#tried

		if command == 'ticket':
			if len(args) != 1:
				raise logical_exception ("usage: .%s <ticket>" % command)
			self.get_ticket_data (replyto, args[0], True)
		elif command == 'testannounce':
			check_admin (sender, ident, host, command)
			if len(args) != 1:
				raise logical_exception ("usage: .%s <ticket>" % command)
			self.announce_ticket (bt_getissue (args[0]))
		elif command == 'checkhg':
			check_admin (sender, ident, host, command)
			global repocheck_timeout
			repocheck_timeout = 0
			process_zan_repo_updates()
		elif command == 'die':
			check_admin (sender, ident, host, command)
			quit()
		elif command == 'convert':
			if len(args) != 3 or args[1] != 'as':
				raise logical_exception ("usage: .%s <value> as <valuetype>" % command)

			value = float (args[0])
			valuetype = args[2]

			if valuetype in ['radians', 'degrees']:
				if valuetype == 'radians':
					radvalue = value
					degvalue = (value * 180.) / math.pi
				else:
					radvalue = (value * math.pi) / 180.
					degvalue = value

				self.privmsg (replyto, '%s radians, %s degrees (%s)' %(radvalue, degvalue, degvalue % 360.))
			elif valuetype in ['celsius', 'fahrenheit']:
				if valuetype == 'celsius':
					celvalue = value
					fahrvalue = value * 1.8 + 32
				else:
					celvalue = (value - 32) / 1.8
					fahrvalue = value

				self.privmsg (replyto, '%s degrees celsius, %s degrees fahrenheit' %(celvalue, fahrvalue))
			else:
				raise logical_exception ('unknown valuetype, expected one of: degrees, radians (angle conversion), ' +
										 'celsius, fahrenheit (temperature conversion)')
		elif command == 'urban' or command == 'ud':
			try:
				if len(args) < 1:
					raise logical_exception ('usage: %s <word>' % command)

				url = 'http://api.urbandictionary.com/v0/define?term=%s' % ('%20'.join (args))
				response = urllib2.urlopen (url).read()
				data = json.loads (response)

				if 'list' in data and len(data['list']) > 0 and 'word' in data['list'][0] and 'definition' in data['list'][0]:
					word = data['list'][0]['word']
					definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace ('  ', ' ')
					up = data['list'][0]['thumbs_up']
					down = data['list'][0]['thumbs_down']
					self.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down))
				else:	
					self.privmsg (replyto, "couldn't find a definition of \002%s\002" % args[0])
			except logical_exception as e:
				raise e
			except Exception as e:
				raise logical_exception ('Urban dictionary lookup failed: %s' % `e`)
		elif command == 'hg':
			check_admin (sender, ident, host, command)

			if len(args) < 2:
				raise logical_exception ('usage: %s <repo> <command...>' % command)

			try:
				repo = hgapi.Repo (args[0])
				result = repo.hg_command (*args[1:])
				self.privmsg (replyto, result)
			except hgapi.hgapi.HgException as e:
				result = decipher_hgapi_error (e)

				if result[0]:
					self.privmsg (replyto, 'error: %s' % result[1])
				else:
					self.privmsg (replyto, 'error: %s' % `e`)
				#fi
			#tried
		elif command == 'changeset' or command == 'cset' or command == 'rev':
			if len(args) != 1:
				raise logical_exception ('usage: %s <changeset>' % command)

			repo = hgapi.Repo ('zandronum-everything')
			data = ""
			node = args[0]

			# Possibly we're passed a date version instead. Try find the node for this.
			try:
				datetime.strptime (args[0], '%y%m%d-%H%M')
				make_commits_txt()
				commits_txt = open ('commits.txt', 'r')

				for line in commits_txt:
					data = line.replace ('\n', '').split (' ')
					if data[1] == args[0]:
						node = data[0]
						break
				else:
					self.privmsg (replyto, 'couldn\'t find changset for date %s' % args[0])
					return
				#done
			except ValueError:
				pass
			#tried

			# The sandboxes contain all revisions in zandronum and zandronum-stable.
			# Thus we only need to try find the revision in the sandbox repos.
			try:
				repo.hg_command ("log", "-r", node, "--template", " ")
			except hgapi.hgapi.HgException:
				self.privmsg (replyto, 'couldn\'t find changeset %s' % (node))
				return
			#tried

			try:
				data = repo.hg_command ("log", "-r", node, "--template",
					"{node|short}@@@@@@@{desc}@@@@@@@{author}@@@@@@@{diffstat}@@@@@@@{date|hgdate}")
				data = data.split ('@@@@@@@')

				node = data[0]
				message = data[1]
				author = data[2]
				diffstat = data[3]
				date = datetime.utcfromtimestamp (int (data[4].split (' ')[0]))
				delta = datetime.now() - date
				datestring = ''

				# Remove the email address from the author if possible
				match = re.compile (r'^(.+) <([^>]+)>$.*').match (author)
				if match:
					author = match.group (1)
					email = match.group (2)

				username = find_developer_by_email (email)

				if username != '':
					author = username

				if delta.days < 4:
					if delta.days == 0:
						if delta.seconds < 60:
							datestring = 'just now'
						elif delta.seconds < 3600:
							minutes = delta.seconds / 60
							datestring = '%d minute%s ago' % (minutes, plural (minutes))
						else:
							hours = delta.seconds / 3600
							datestring = '%d hour%s ago' % (hours, plural (hours))
					else:
						datestring = '%d day%s ago' % (delta.days, plural (delta.days))
				else:
					datestring = 'on %s' % (str (date))
				#fi

				self.privmsg (replyto, 'changeset %s (%s): committed by %s %s (%s)' % \
					(node, date.strftime ('%y%m%d-%H%M'), author, datestring, diffstat))

				for line in message.split ('\n'):
					self.privmsg (replyto, '    ' + line)
			except hgapi.hgapi.HgException as e:
				result = decipher_hgapi_error (e)

				if result[0]:
					self.privmsg (replyto, 'error: %s' % result[1])
				else:
					self.privmsg (replyto, 'error: %s' % `e`)
			#tried
#		else:
#			raise logical_exception ("unknown command `.%s`" % command)

	#
	# Print a ticket announce to appropriate channels
	#
	def announce_ticket (self, data):
		idstring = "%d" % data.id
		while len(idstring) < 7:
			idstring = "0" + idstring

		isprivate = data['view_state']['name'] == 'private'
		reporter = data['reporter']['name'] if hasattr (data['reporter'], 'name') else '<nobody>'

		for channel in self.channels:
			if channel.get_value ('btannounce', False):
				if not isprivate or (channel.get_value ('btprivate', False)):
					self.write ("PRIVMSG %s :[%s] New issue %s, reported by %s: %s: %s" % \
						(channel.get_value ('name'), data['project']['name'], idstring, reporter,
						data['summary'], self.get_ticket_url (idstring)))
				#fi
			#fi
		#done

	def handle_error(self):
		excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))

	def restart(self):
		excepterm('')

	def privmsg (self, channel, msg):
		self.write ("PRIVMSG %s :%s" % (channel, msg))

	def close_connection (self, message):
		if self.flags & CLIF_CONNECTED:
			self.write ("QUIT :" + message)
		self.send_all_now()
		self.close()

	def quit_irc (self):
		self.close_connection ('Leaving')

	def exceptdie (self):
		self.close_connection ('Caught exception')

	def keyboardinterrupt (self):
		self.close_connection ('KeyboardInterrupt')

#
# Main procedure:
#
try:
	autoconnects = Config.get_value ('autoconnect', [])
	
	if len (autoconnects) == 0:
		print "Nowhere to connect."
		quit()
	
	for aconn in autoconnects:
		for conndata in Config.get_nodelist ('connections'):
			if conndata.get_value ('name') == aconn:
				irc_client (conndata, 0)
				break
		else:
			raise ValueError ("unknown autoconnect entry %s" % (aconn))
	
	g_BotActive = True
	asyncore.loop()
except KeyboardInterrupt:
	for client in g_clients:
		client.keyboardinterrupt()
	quit()

mercurial