Index: skype/skyped.py
===================================================================
--- skype/skyped.py	(revision 3423be07392acb960fed3e014141ce5df177c06f)
+++ skype/skyped.py	(revision d5a66f8ae910023efdbfc8343a63dca110b8bf09)
@@ -3,5 +3,5 @@
 #   skyped.py
 #  
-#   Copyright (c) 2007, 2008, 2009, 2010 by Miklos Vajna <vmiklos@frugalware.org>
+#   Copyright (c) 2007, 2008, 2009, 2010, 2011 by Miklos Vajna <vmiklos@frugalware.org>
 #
 #   This program is free software; you can redistribute it and/or modify
@@ -33,8 +33,14 @@
 from traceback import print_exception
 import ssl
-import select
-import threading
 
 __version__ = "0.1.1"
+
+try:
+	import gobject
+	hasgobject = True
+except ImportError:
+	import select
+	import threading
+	hasgobject = False
 
 def eh(type, value, tb):
@@ -43,4 +49,6 @@
 	if type != KeyboardInterrupt:
 		print_exception(type, value, tb)
+	if hasgobject:
+		gobject.MainLoop().quit()
 	if options.conn:
 		options.conn.close()
@@ -70,5 +78,5 @@
 	return True
 
-def input_handler(fd):
+def input_handler(fd, io_condition = None):
 	global options
 	global skype
@@ -77,22 +85,33 @@
 			skype.send(i.strip())
 		options.buf = None
+		if not hasgobject:
+			return True
+	else:
+		if not hasgobject:
+			close_socket = False
+			if wait_for_lock(options.lock, 3, 10, "input_handler"):
+				try:
+						input = fd.recv(1024)
+						options.lock.release()
+				except Exception, s:
+					dprint("Warning, receiving 1024 bytes failed (%s)." % s)
+					fd.close()
+					options.conn = False
+					options.lock.release()
+					return False
+				for i in input.split("\n"):
+					if i.strip() == "SET USERSTATUS OFFLINE":
+						close_socket = True
+					skype.send(i.strip())
+			return not(close_socket)
+		try:
+			input = fd.recv(1024)
+		except Exception, s:
+			dprint("Warning, receiving 1024 bytes failed (%s)." % s)
+			fd.close()
+			return False
+		for i in input.split("\n"):
+			skype.send(i.strip())
 		return True
-	else:
-		close_socket = False
-		if wait_for_lock(options.lock, 3, 10, "input_handler"):
-			try:
-					input = fd.recv(1024)
-					options.lock.release()
-			except Exception, s:
-				dprint("Warning, receiving 1024 bytes failed (%s)." % s)
-				fd.close()
-				options.conn = False
-				options.lock.release()
-				return False
-			for i in input.split("\n"):
-				if i.strip() == "SET USERSTATUS OFFLINE":
-					close_socket = True
-				skype.send(i.strip())
-		return not(close_socket)
 
 def skype_idle_handler(skype):
@@ -106,22 +125,35 @@
 def send(sock, txt):
 	global options
+	from time import sleep
 	count = 1
 	done = False
-	while (not done) and (count < 10) and options.conn:
-		if wait_for_lock(options.lock, 3, 10, "socket send"):
+	if hasgobject:
+		while (not done) and (count < 10):
 			try:
-				if options.conn: sock.send(txt)
-				options.lock.release()
+				sock.send(txt)
 				done = True
 			except Exception, s:
-				options.lock.release()
 				count += 1
 				dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count))
-				time.sleep(1)
-	if not done:
-		if options.conn:
+				sleep(1)
+		if not done:
 			options.conn.close()
-		options.conn = False
-	return done
+	else:
+		while (not done) and (count < 10) and options.conn:
+			if wait_for_lock(options.lock, 3, 10, "socket send"):
+				try:
+					 if options.conn: sock.send(txt)
+					 options.lock.release()
+					 done = True
+				except Exception, s:
+					options.lock.release()
+					count += 1
+					dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count))
+					sleep(1)
+		if not done:
+			if options.conn:
+				options.conn.close()
+			options.conn = False
+		return done
 
 def bitlbee_idle_handler(skype):
@@ -134,10 +166,17 @@
 		except Exception, s:
 			dprint("Warning, sending '%s' failed (%s)." % (e, s))
-			if options.conn: options.conn.close()
-			options.conn = False
-			done = False
-	return done
-
-def server(host, port, skype):
+			if hasgobject:
+				options.conn.close()
+			else:
+				if options.conn: options.conn.close()
+				options.conn = False
+				done = False
+	if hasgobject:
+		return True
+	else:
+		return done
+	return True
+
+def server(host, port, skype = None):
 	global options
 	sock = socket.socket()
@@ -145,10 +184,14 @@
 	sock.bind((host, port))
 	sock.listen(1)
-	dprint("Waiting for connection...")
-	listener(sock, skype)
+	if hasgobject:
+		gobject.io_add_watch(sock, gobject.IO_IN, listener)
+	else:
+		dprint("Waiting for connection...")
+		listener(sock, skype)
 
 def listener(sock, skype):
 	global options
-	if not(wait_for_lock(options.lock, 3, 10, "listener")): return False
+	if not hasgobject:
+		if not(wait_for_lock(options.lock, 3, 10, "listener")): return False
 	rawsock, addr = sock.accept()
 	options.conn = ssl.wrap_socket(rawsock,
@@ -161,5 +204,6 @@
 			options.conn.handshake()
 		except Exception:
-			options.lock.release()
+			if not hasgobject:
+				options.lock.release()
 			dprint("Warning, handshake failed, closing connection.")
 			return False
@@ -175,19 +219,24 @@
 		dprint("Warning, receiving 1024 bytes failed (%s)." % s)
 		options.conn.close()
-		options.conn = False
-		options.lock.release()
+		if not hasgobject:
+			options.conn = False
+			options.lock.release()
 		return False
 	if ret == 2:
 		dprint("Username and password OK.")
 		options.conn.send("PASSWORD OK\n")
-		options.lock.release()
-		serverloop(options, skype)
+		if hasgobject:
+			gobject.io_add_watch(options.conn, gobject.IO_IN, input_handler)
+		else:
+			options.lock.release()
+			serverloop(options, skype)
 		return True
 	else:
 		dprint("Username and/or password WRONG.")
 		options.conn.send("PASSWORD KO\n")
-		options.conn.close()
-		options.conn = False
-		options.lock.release()
+		if not hasgobject:
+			options.conn.close()
+			options.conn = False
+			options.lock.release()
 		return False
 
@@ -423,10 +472,17 @@
 	else:
 		dprint('skyped is started on port %s' % options.port)
+	if hasgobject:
+		server(options.host, options.port)
 	try:
 		skype = SkypeApi()
 	except Skype4Py.SkypeAPIError, s:
 		sys.exit("%s. Are you sure you have started Skype?" % s)
-	while 1:
-		options.conn = False
-		options.lock = threading.Lock()
-		server(options.host, options.port, skype)
+	if hasgobject:
+		gobject.timeout_add(2000, skype_idle_handler, skype)
+		gobject.timeout_add(60000, bitlbee_idle_handler, skype)
+		gobject.MainLoop().run()
+	else:
+		while 1:
+			options.conn = False
+			options.lock = threading.Lock()
+			server(options.host, options.port, skype)
