cobalt.py

Tue, 04 Nov 2014 20:35:25 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 04 Nov 2014 20:35:25 +0200
changeset 61
8d1098095dec
parent 60
b2db21672012
child 62
052a8a1e3d7d
permissions
-rwxr-xr-x

- added commits.txt to hgignore

#!/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 json
import urllib
import urllib2
import hgapi
import os
import suds
import math
from datetime import datetime

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()

print 'Loading configuration...'
try:
	with open ('cobalt.json', 'r') as fp:
		g_config = json.loads (fp.read())
except IOError as e:
	print 'couldn\'t open cobalt.json: %s' % e
	quit()

g_admins = g_config['admins']
g_mynick = g_config['nickname']

g_idgamesSearchURL = 'http://www.doomworld.com/idgames/api/api.php?action=search&query=%s&type=title&sort=date&out=json'
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 save_config():
	with open ('cobalt.json', 'w') as fp:
		json.dump (g_config, fp, sort_keys = True, indent = 4)

def cfg (key, default):
	if not hasattr (g_config, key):
		g_config[key] = default
		save_config()
		return default
	return g_config[key]

def bt_updatechecktimeout():
	global btannounce_timeout
	btannounce_timeout = time.time() + (cfg ('btlatest_checkinterval', 5) * 60)

def bool_from_string (value):
	if value != 'true' and value != 'false':
		raise logical_exception ('expected true or false for value')
	return True if value == 'true' else False

if suds_active:
	try:
		sys.stdout.write ('Retrieving latest tracker ticket... ')
		btannounce_id = suds_client.service.mc_issue_get_biggest_id (g_config['trackeruser'], g_config['trackerpassword'], 0)
		btannounce_active = True
		bt_updatechecktimeout()
		print btannounce_id
	except Exception as e:
		pass

def bt_getissue(ticket):
	global suds_client
	global g_config
	return suds_client.service.mc_issue_get (g_config['trackeruser'], g_config['trackerpassword'], ticket)

def bt_checklatest():
	global btannounce_timeout
	global btannounce_id

	if time.time() >= btannounce_timeout:
		bt_updatechecktimeout()
		newid = btannounce_id
		try:
			newid = suds_client.service.mc_issue_get_biggest_id (g_config['trackeruser'], g_config['trackerpassword'], 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['logchannel']:
				client.write ("PRIVMSG %s :%s" % (channel['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(date, "%y%m%d-%H%M")}\n')
	f = open ('commits.txt', 'w')
	f.write (data)
	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 = {'zandronum':(time.time()) + 15, 'zandronum-stable':(time.time() + 15), 'zandronum-sandbox':(time.time()) + 15, 'zandronum-sandbox-stable':(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 g_config['developer_emails'].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 (repo_name):
	global repocheck_timeout
	global suds_client
	global g_config
	global g_clients

	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

	if time.time() < repocheck_timeout[repo_name]:
		return

	repocheck_timeout[repo_name] = time.time() + (cfg ('hg_checkinterval', 15) * 60)
	zanrepo = hgapi.Repo (repo_name)
	commit_data = []
	delimeter = '@@@@@@@@@@'

	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]

		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 'btprivate' in channel and channel['btprivate'] == True:
							irc_client.privmsg (channel['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['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 (g_config['trackeruser'],
					g_config['trackerpassword'], 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.cfg['channels']:
					if 'btannounce' in channel and channel['btannounce'] == True:
						irc_client.privmsg (channel['name'],
							"%s: commit %s fixes issue %d: %s"
							% (repo_name, commit_node, ticket_id, commit_message))
						irc_client.privmsg (channel['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 (g_config['trackeruser'], g_config['trackerpassword'], ticket_id, ticket_data)
			#fi

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

	return num_commits > 0
#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['name']
		self.host = cfg['address']
		self.port = cfg['port']
		self.password = cfg['password'] if 'password' in cfg else ''
		self.channels = cfg['channels']
		self.flags = flags
		self.send_buffer = list()
		self.umode = cfg['umode'] if 'umode' in cfg else ''
		self.cfg = cfg
		self.mynick = ''
		self.verbose = g_config['verbose'] if 'verbose' in g_config else False
		self.commandprefix = g_config['commandprefix'][0] if 'commandprefix' in g_config else '.'

		for channel in self.channels:
			if not 'logchannel' in channel:
				channel['logchannel'] = False
			channel['namesdone'] = True
			#channel['haslinkbot'] = False

		if not 'conflictsuffix' in self.cfg:
			self.cfg['conflictsuffix'] = '`'

		self.desired_name = self.cfg['nickname'] if 'nickname' in self.cfg else g_config['nickname']
		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 = self.cfg['ident'] if 'ident' in self.cfg else g_config['ident']
		gecos = self.cfg['gecos'] if 'gecos' in self.cfg else g_config['gecos']
		if 'password' in self.cfg:
			self.write ("PASS %s" % self.cfg['password'])
		self.write ("USER %s * * :%s" % (ident, gecos))
		self.write ("NICK %s" % self.mynick)

	def handle_connect (self):
		self.mynick = self.desired_name
		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.cfg['channels']:
							self.write ("JOIN %s %s" % (channel['name'], channel['password'] if 'password' in channel else ''))

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

						#if match and match.group(1).toLower() == 'linkbot':
							#channel_by_name (match.group(4))['haslinkbot'] = True
					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)

						#if match and match.group(1).toLower() == 'linkbot':
							#for channel in self.channels:
								#channels['haslinkbot'] = False
					elif words[1] == "433":
						#:irc.localhost 433 * cobalt :Nickname is already in use.
						self.mynick = '%s%s' % (self.mynick, self.cfg['conflictsuffix'])
						self.write ("NICK %s" % self.mynick)

			# Check for new issues on the bugtracker
			bt_checklatest()

			# Check for new commits in the repositories
			for n in ['zandronum-stable', 'zandronum', 'zandronum-sandbox', 'zandronum-sandbox-stable']:
				process_zan_repo_updates (n)

	def channel_by_name (self, name):
		for channel in self.channels:
			if channel['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
			http_regex = re.compile (r'.*http(s?)://%s/view\.php\?id=([0-9]+).*' % g_config['trackerurl'])
			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)
				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):
		return 'https://%s/view.php?id=%s' % (g_config['trackerurl'], 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['name'] == replyto and 'btprivate' in channel and channel['btprivate'] == True:
						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

	# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	#
	#	Process an IRC command
	#
	def handle_command (self, sender, ident, host, replyto, command, args):
		if command == "raw":
			check_admin (sender, ident, host, command)
			self.write (" ".join (args))
		elif command == "msg":
			check_admin (sender, ident, host, command)
			if len(args) < 2:
				raise logical_exception ("usage: .%s <target> <message...>" % command)
			self.privmsg (args[0], " ".join (args[1:]))
		elif 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 == 'multierror':
			raise logical_exception ('a\nb\nc\n')
		elif command == 'idgames':
			try:
				if len(args) < 1:
					raise logical_exception ('usage: .%s <keywords>' % command)

				url = g_idgamesSearchURL % urllib.quote (" ".join (args[0:]))
				response = urllib2.urlopen (url).read()
				data = json.loads (response)

				if 'content' in data and 'file' in data['content']:
					if type (data['content']['file']) is list:
						files = data['content']['file']
					else:
						files = [data['content']['file']]

					i = 0
					for filedata in files:
						if i >= 5:
							break

						self.privmsg (replyto, '- %s: \'%s\' by \'%s\', rating: %s: %s' % \
							(filedata['filename'], filedata['title'], filedata['author'], filedata['rating'], filedata['url']))

						i += 1
					self.privmsg (replyto, "(%d / %d results posted)" % (i, len(files)))
				elif 'warning' in data and 'message' in data['warning']:
					raise logical_exception (data['warning']['message'])
				elif 'error' in data and 'message' in data['error']:
					raise logical_exception (data['error']['message'])
				else:
					raise logical_exception ("Incomplete JSON response from doomworld.com/idgames")
			except logical_exception as e:
				raise e
			except Exception as e:
				raise logical_exception ('Search failed: %s' % `e`)
		elif command == 'restart':
			check_admin (sender, ident, host, command)
			excepterm('')
		elif command == 'update':
			check_admin (sender, ident, host, command)

			try:
				repo = hgapi.Repo ('.')
				r1 = repo.hg_id()
				repo.hg_pull()
				repo.hg_update('tip', True)
				r2 = repo.hg_id()
				if r1 != r2:
					self.privmsg (replyto, 'Updated to %s, restarting...' % r2)
					excepterm('')
				else:
					self.privmsg (replyto, 'Up to date at %s.' % r2)
			except hgapi.HgException as e:
				raise logical_exception ('Update failed: %s' % str (e))
		elif command == 'addchan':
			check_admin (sender, ident, host, command)
			if len(args) != 1:
				raise logical_exception ("usage: .%s <channel>" % command)

			for channel in self.channels:
				if channel['name'].upper() == args[0].upper():
					raise logical_exception ('I already know of %s!' % args[0])

			chan = {}
			chan['name'] = args[0]
			chan['logchannel'] = False
			self.channels.append (chan)
			self.write ('JOIN ' + chan['name'])
			save_config()
		elif command == 'delchan':
			check_admin (sender, ident, host, command)
			if len(args) != 1:
				raise logical_exception ("usage: .%s <channel>" % command)

			for channel in self.channels:
				if channel['name'].upper() == args[0].upper():
					break;
			else:
				raise logical_exception ('unknown channel ' + args[0])

			self.channels.remove (channel)
			self.write ('PART ' + args[0])
			save_config()
		elif command == 'chanattr':
			check_admin (sender, ident, host, command)

			if len(args) < 1:
				raise logical_exception ("usage: .%s <attribute> [value...]" % command)

			for channel in self.channels:
				if channel['name'] == replyto:
					break
			else:
				raise logical_exception ('I don\'t know of a channel named ' + replyto)

			key = args[0]

			if len(args) < 2:
				try:
					self.privmsg (replyto, '%s = %s' % (key, channel[key]))
				except KeyError:
					self.privmsg (replyto, 'attribute %s is not set' % key)
			else:
				value = " ".join (args[1:])
				if key == 'name':
					if replyto == channel['name']:
						replyto = value

					self.write ('PART ' + channel['name'])
					channel['name'] = value
					self.write ('JOIN ' + channel['name'] + ' ' + (channel['password'] if hasattr (channel, 'password') else ''))
				elif key == 'password':
					channel['password'] = value
				elif key == 'btannounce':
					channel['btannounce'] = bool_from_string (value)
				elif key == 'btprivate':
					channel['btprivate'] = bool_from_string (value)
				elif key == 'logchannel':
					channel['logchannel'] = bool_from_string (value)
				else:
					raise logical_exception ('unknown key ' + key)

				self.privmsg (replyto, '%s is now %s' % (key, channel[key]))

			save_config()
		elif command == 'devemail':
			check_admin (sender, ident, host, command)

			if len(args) < 2:
				raise logical_exception ("usage: .%s <user> <email>" % command)
			#fi

			if not 'developer_emails' in g_config:
				g_config['developer_emails'] = {}
			#fi

			user = ' '.join (args[0:-1])

			if args[0] in g_config['developer_emails']:
				g_config['developer_emails'][user].append (args[-1])
			else:
				g_config['developer_emails'][user] = [args[-1]]
			#fi

			self.privmsg (replyto, 'Developer emails for %s are now %s' %
				(user, ', '.join (g_config['developer_emails'][user])))
			save_config()
		elif command == 'deldevemail':
			check_admin (sender, ident, host, command)

			if len(args) < 2:
				raise logical_exception ("usage: .%s <user> <email>" % command)
			#fi

			if not 'developer_emails' in g_config:
				g_config['developer_emails'] = {}
			#fi

			user = ' '.join (args[0:-1])

			if user in g_config['developer_emails']:
				try:
					g_config['developer_emails'][user].remove (args[-1])
				except:
					pass
				#tried

				if len (g_config['developer_emails'][user]) == 0:
					g_config['developer_emails'].pop (user)
					self.privmsg (replyto, 'No more developer emails for %s' % user)
				else:
					self.privmsg (replyto, 'Developer emails for %s are now %s' %
						(user, ', '.join (g_config['developer_emails'][user])))
				#fi
				save_config()
			else:
				self.privmsg (replyto, 'There is no developer \'%s\'' % user)
			#fi
		elif command == 'listdevemails':
			check_admin (sender, ident, host, command)

			if 'developer_emails' in g_config:
				for dev, emails in g_config['developer_emails'].iteritems():
					self.privmsg (replyto, 'Emails for %s: %s' % (dev, ', '.join (emails)))
				#done
			else:
				self.privmsg (replyto, 'No dev emails.')
			#fi
		elif command == 'checkhg':
			check_admin (sender, ident, host, command)
			global repocheck_timeout
			repocheck_timeout = {'zandronum':0, 'zandronum-stable':0, 'zandronum-sandbox':0, 'zandronum-sandbox-stable':0}

			for n in ['zandronum-stable', 'zandronum', 'zandronum-sandbox', 'zandronum-sandbox-stable']:
				numcommits = process_zan_repo_updates (n)

				if numcommits == 0:
					self.privmsg (replyto, 'No new commits in ' + n)
				#fi
			#done
		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(date, '%y%m%d-%H%M')}@@@@@@@{date(date, '%s')}")
				data = data.split ('@@@@@@@')

				node = data[0]
				message = data[1]
				author = data[2]
				diffstat = data[3]
				dateversion = data[4]
				date = datetime.fromtimestamp (int (data[5]))
				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, dateversion, 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.cfg['channels']:
			if 'btannounce' in channel and channel['btannounce'] == True:
				if not isprivate or ('btprivate' in channel and channel['btprivate'] == True):
					self.write ("PRIVMSG %s :[%s] New issue %s, reported by %s: %s: %s" % \
						(channel['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 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:
	for aconn in g_config['autoconnect']:
		for conndata in g_config['connections']:
			if conndata['name'] == aconn:
				irc_client (conndata, 0)
				break
		else:
			raise logical_exception ("unknown autoconnect entry %s" % (aconn))

	g_BotActive = True
	asyncore.loop()
except KeyboardInterrupt:
	for client in g_clients:
		client.keyboardinterrupt()
	quit()

mercurial