cobalt.py

Sun, 16 Aug 2015 19:27:14 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 16 Aug 2015 19:27:14 +0300
changeset 153
497b7290977d
parent 152
1b734faab67a
child 156
5747c959c293
permissions
-rwxr-xr-x

More Python 3 rework

#!/usr/bin/env python
'''
	Copyright 2014-2015 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.
'''

from __future__ import print_function
import asyncore
import sys
import traceback
import os
import modulecore as ModuleCore
import configfile as ConfigFile
from configfile import Config
import hgpoll as HgPoll
import bt as Bt
import irc as Irc
import rest

if __name__ != '__main__':
	raise ImportError ('cobalt may not be imported as a module')

if sys.version_info < (3, 0):
	print ('''cobalt requires Python 3 to run''', file=sys.stderr)
	quit (1)

g_BotActive = False

#
# 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
			Irc.broadcast (line)

	for client in Irc.all_clients:
		if data:
			client.exceptdie()
		else:
			client.quit_irc()

	if g_BotActive:
		restart_self()
	else:
		quit()

def restart_self():
	os.execl (sys.executable, sys.executable, * sys.argv)

def main():
	global g_BotActive
	sys.excepthook = handle_exception
	all_clients = []
	g_BotActive = False

	try:
		if os.geteuid() == 0 and input ('Do you seriously want to run cobalt as root? [y/N] ') != 'y':
			quit()
	except:
		pass

	ConfigFile.init()
	ModuleCore.init_data()
	Bt.init()
	HgPoll.init()
	
	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.irc_client (conndata, 0)
					break
			else:
				raise ValueError ('unknown autoconnect entry "%s"' % (aconn))

		if Config.get_node ('rest').get_value ('active', True):
			rest.RestServer()
		
		g_BotActive = True
		asyncore.loop()
	except KeyboardInterrupt:
		for client in Irc.all_clients:
			client.keyboardinterrupt()
		quit()
	except Irc.RestartError as e:
		excepterm (e.message)

main()

mercurial