- argrrgh

Fri, 28 Nov 2014 22:45:55 -0500

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Fri, 28 Nov 2014 22:45:55 -0500
changeset 94
2c93636202a5
parent 93
125f3c97071e
child 95
af912f53649c

- argrrgh

cobalt.py file | annotate | diff | comparison | revisions
irc.py file | annotate | diff | comparison | revisions
rest.py file | annotate | diff | comparison | revisions
--- a/cobalt.py	Sat Nov 29 05:07:41 2014 +0200
+++ b/cobalt.py	Fri Nov 28 22:45:55 2014 -0500
@@ -53,7 +53,7 @@
 def excepterm (data):
 	for segment in data:
 		for line in segment.splitlines():
-			print line
+			#print line
 			Irc.broadcast (line)
 
 	for client in Irc.all_clients:
@@ -62,10 +62,11 @@
 		else:
 			client.quit_irc()
 
-	if g_BotActive:
-		restart_self()
-	else:
-		quit()
+	quit()
+	#if g_BotActive:
+	#	restart_self()
+	#else:
+	#	quit()
 
 def restart_self():
 	os.execl (sys.executable, sys.executable, * sys.argv)
@@ -99,7 +100,7 @@
 		for aconn in autoconnects:
 			for conndata in Config.get_nodelist ('connections'):
 				if conndata.get_value ('name') == aconn:
-					Irc.irc_client (conndata, 0)
+					#Irc.irc_client (conndata, 0)
 					break
 			else:
 				raise ValueError ("unknown autoconnect entry %s" % (aconn))
--- a/irc.py	Sat Nov 29 05:07:41 2014 +0200
+++ b/irc.py	Fri Nov 28 22:45:55 2014 -0500
@@ -22,6 +22,7 @@
 # Prints a line to log channel(s)
 #
 def broadcast (line):
+	print line
 	for client in all_clients:
 		if not client.flags & CLIF_CONNECTED:
 			continue
--- a/rest.py	Sat Nov 29 05:07:41 2014 +0200
+++ b/rest.py	Fri Nov 28 22:45:55 2014 -0500
@@ -7,7 +7,7 @@
 import re
 import json
 import urllib
-import irc
+import irc as Irc
 from datetime import datetime, timedelta
 from configfile import Config
 
@@ -24,15 +24,15 @@
 
 	while i < len (g_throttle):
 		if g_throttle[i][1] <= datetime.utcnow():
-			print 'Throttle of %s expired' % g_throttle[i][0][0]
+			print 'Throttle of %s expired' % g_throttle[i][0]
 			item = g_throttle.pop (i) # expired
 
-			if item[0][0] == address[0]:
+			if item[0] == address:
 				return False # this address expired
 
 			continue
 
-		if g_throttle[i][0][0] == address[0]:
+		if g_throttle[i][0] == address:
 			return True # is throttled
 
 		i += 1
@@ -53,7 +53,7 @@
 
 def handle_rest_http (data, address):
 	global g_credentials
-	displayaddress = '%s:%s' % address
+	displayaddress = address
 	authrex = re.compile (r'^authorization: Basic (.+)$')
 	payloadrex = re.compile (r'^payload=(.+)$')
 	authenticated = False
@@ -66,7 +66,7 @@
 	for line in data:
 		match = authrex.match (line)
 
-		if match and match.group (1) == g_credentials)
+		if match and match.group (1) == g_credentials:
 			authenticated = True
 			continue
 
@@ -84,7 +84,7 @@
 	try:
 		jsondata = json.loads (jsonstring)
 		repodata = jsondata['repository']
-		repopath = '%s/%s' % (repodata['owner'], repodata['name']
+		repopath = '%s/%s' % (repodata['owner'], repodata['name'])
 
 		if repopath not in valid_repos:
 			raise ValueError ('unknown repository %s' % repopath)
@@ -108,8 +108,8 @@
 
 class RESTConnection (asyncore.dispatcher):
 	httpdata = ''
-	address=None
 	writebuffer = ''
+	address = None
 
 	def __init__ (self, conn, address):
 		asyncore.dispatcher.__init__ (self, conn)
@@ -117,6 +117,7 @@
 			certfile='cert.pem', do_handshake_on_connect=False)
 		self.socket.setblocking (0)
 		self.address = address
+		print 'Initialized connection handler for %s' % type (address)
 
 		while True:
 			try:
@@ -128,8 +129,8 @@
 				elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
 					select.select([], [self.socket], [])
 				else:
-					Irc.broadcast ('%s:%s: SSL error: %s' % (self.address[0], self.address[1], err))
-					throttle (self.address)
+					Irc.broadcast ('%s:%s: SSL error: %s' % (self.address[0], err))
+					throttle (self.address[0])
 					self.close()
 					return
 
@@ -149,7 +150,7 @@
 		while 1:
 			try:
 				data = self.recv (4096)
-			except ssl.SSLError, err:
+			except:
 				# EOF
 				self.finish()
 				return
@@ -157,7 +158,7 @@
 			self.httpdata += data.replace ('\r', '')
 
 	def finish (self):
-		handle_rest_http (self.httpdata.split ('\n'), self.address)
+		handle_rest_http (self.httpdata.split ('\n'), self.address[0])
 		self.close()
 
 	def handle_write (self):
@@ -176,6 +177,7 @@
 
 		asyncore.dispatcher.__init__ (self)
 		self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
+		self.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 		self.bind (('', g_portnumber))
 		self.listen (5)
 		print 'REST server initialized'
@@ -189,7 +191,10 @@
 			return
 
 		Irc.broadcast ('REST connection from %s:%s' % address)
-		RESTConnection (sock, address)
+		conn = RESTConnection (sock, address)
+
+		if conn.address == None:
+			conn.__init__ (sock, address) # eh?
 
 	def handle_error (self):
 		return

mercurial