- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry

Tue, 13 May 2014 23:29:37 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Tue, 13 May 2014 23:29:37 +0300
changeset 1
29c7e9d13a30
parent 0
4904744b40eb
child 2
1a24dd2d598e

- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry

cobalt.py file | annotate | diff | comparison | revisions
template.json file | annotate | diff | comparison | revisions
--- a/cobalt.py	Tue May 13 23:17:01 2014 +0300
+++ b/cobalt.py	Tue May 13 23:29:37 2014 +0300
@@ -99,10 +99,19 @@
 
 sys.excepthook = handle_exception
 
-def check_admin (sender, ident, host):
-	if not "%s!%s@%s" % (sender, user, host) in g_admins:
-		raise ".%s requires admin access" % command
+def check_admin (sender, ident, host, command):
+	if not "%s!%s@%s" % (sender, 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
+
+#
+# Main IRC client class
+#
 class irc_client (asyncore.dispatcher):
 	def __init__ (self, cfg, flags):
 		self.name = cfg['name']
@@ -196,9 +205,9 @@
 				command = stuff[0]
 				args = stuff[1:]
 				try:
-					handle_command (sender, user, host, replyto, command, args)
-				except str as msg:
-					privmsg (replyto, "error: %s" % msg)
+					self.handle_command (sender, user, host, replyto, command, args)
+				except logical_exception as msg:
+					self.privmsg (replyto, "error: %s" % msg)
 			elif http_match:
 				self.get_ticket_data (replyto, http_match.group (2), False)
 		else:
@@ -223,21 +232,21 @@
 			if withlink:
 				self.privmsg (replyto, "Read all about it here: https://%s/view.php?id=%s" % (g_config['trackerurl'], ticket))
 
-	def handle_command (self, sender, user, host, replyto, command, args):
+	def handle_command (self, sender, ident, host, replyto, command, args):
 		if command == "raw":
-			check_admin (sender, ident, host)
+			check_admin (sender, ident, host, command)
 			self.write (" ".join (args))
 		elif command == "msg":
-			check_admin (sender, ident, host)
+			check_admin (sender, ident, host, command)
 			if len(args) < 2:
-				raise "usage: .%s <target> <message...>" % command
+				raise logical_exception ("usage: .%s <target> <message...>" % command)
 			self.privmsg (args[0], " ".join (args[1:]))
 		elif command == "ticket":
 			if len(args) != 1:
-				raise "usage: .%s <ticket>" % command
+				raise logical_exception ("usage: .%s <ticket>" % command)
 			self.get_ticket_data (replyto, args[0], True)
 		else:
-			self.privmsg (replyto, "unknown command `.%s`" % command)
+			raise logical_exception ("unknown command `.%s`" % command)
 
 	def handle_error(self):
 		excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
@@ -257,9 +266,13 @@
 		self.send_all_now()
 		self.close()
 
+#
+# Main procedure:
+#
 try:
 	for conndata in g_config['connections']:
-		irc_client (conndata, CLIF_CONTROL)
+		if conndata['name'] in g_config['autoconnect']:
+			irc_client (conndata, CLIF_CONTROL if conndata['control'] else 0)
 	asyncore.loop()
 except KeyboardInterrupt:
 	for client in g_clients:
--- a/template.json	Tue May 13 23:17:01 2014 +0300
+++ b/template.json	Tue May 13 23:29:37 2014 +0300
@@ -12,6 +12,7 @@
 		"admin!admin@admin",
 		"admin!admin@admin",
 	],
+	"autoconnect": [ "local" ],
 	"connections":
 	[
 		{
@@ -19,6 +20,7 @@
 			"address": "127.0.0.1",
 			"port": 6667,
 			"umode": "+iw",
+			"control": true,
 			"channels":
 			[
 				{

mercurial