Index: skype/skyped.py
===================================================================
--- skype/skyped.py	(revision 761367025bcb4a711f07715a91076769a3297531)
+++ skype/skyped.py	(revision 40d2dc483898d5dc3c57a90ea6d00e07648eea41)
@@ -30,10 +30,9 @@
 import locale
 import time
-import dbus
-import dbus.service
-import dbus.mainloop.glib
 import gobject
 import socket
 import getopt
+import Skype4Py
+import threading
 
 __version__ = "0.1.1"
@@ -46,13 +45,16 @@
 conn = None
 
-def sig_handler(signum, frame):
-	mainloop.quit()
-
 def input_handler(fd, io_condition):
 	input = fd.recv(1024)
 	for i in input.split("\n"):
-		if i:
-			for j in skype.send(i.strip()):
-				fd.send((j + "\n").encode(locale.getdefaultlocale()[1]))
+		skype.send(i.strip())
+	return True
+
+def idle_handler(skype):
+	skype.send("PING")
+	try:
+		time.sleep(10)
+	except KeyboardInterrupt:
+		sys.exit("Exiting.")
 	return True
 
@@ -77,31 +79,30 @@
 		print msg
 
-class SkypeApi(dbus.service.Object):
+class SkypeApi():
 	def __init__(self):
-		bus = dbus.SessionBus()
-		try:
-			self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype')
-		except dbus.exceptions.DBusException:
-			sys.exit("Can't find any Skype instance. Are you sure you have started Skype?")
+		self.skype = Skype4Py.Skype()
+		self.skype._API.Handlers.append(Skype4Py.utils.WeakCallableRef(self.recv))
+		self.skype._API._Handler = self.recv
+		self.skype.Attach()
 
-		reply = self.send('NAME ' + CLIENT_NAME)
-		if reply[0] != 'OK':
-			sys.exit('Could not bind to Skype client')
+	def recv(self, mode, msg_text):
+		global conn
+		if mode != "rece_api":
+			return
+		if "\n" in msg_text:
+			# crappy skype prefixes only the first line for
+			# multiline messages so we need to do so for the other
+			# lines, too. this is something like:
+			# 'CHATMESSAGE id BODY first line\nsecond line' ->
+			# 'CHATMESSAGE id BODY first line\nCHATMESSAGE id BODY second line'
+			prefix = " ".join(msg_text.split(" ")[:3])
+			msg_text = ["%s %s" % (prefix, i) for i in " ".join(msg_text.split(" ")[3:]).split("\n")]
+		else:
+			msg_text = [msg_text]
+		for i in msg_text:
+			dprint('<< ' + i)
+			if conn:
+				conn.send(i + "\n")
 
-		reply = self.send('PROTOCOL 5')
-		try:
-			dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API')
-		except KeyError:
-			sys.exit()
-
-	# skype -> client (async)
-	@dbus.service.method(dbus_interface='com.Skype.API')
-	def Notify(self, msg_text):
-		global conn
-		dprint('<< ' + msg_text)
-		if conn:
-			conn.send(msg_text + "\n")
-
-	# client -> skype (sync, 5 sec timeout)
 	def send(self, msg_text):
 		if not len(msg_text):
@@ -109,26 +110,7 @@
 		dprint('>> ' + msg_text)
 		try:
-			reply = self.skype_api.Invoke(msg_text)
-		except dbus.exceptions.DBusException, s:
-			reply = str(s)
-			if(reply.startswith("org.freedesktop.DBus.Error.ServiceUnknown")):
-				try:
-					self.remove_from_connection(dbus.SessionBus(), "/com/Skype/Client")
-				except LookupError:
-					pass
-				mainloop.quit()
-		if "\n" in reply:
-			# crappy skype prefixes only the first line for
-			# multiline messages so we need to do so for the other
-			# lines, too. this is something like:
-			# 'CHATMESSAGE id BODY first line\nsecond line' ->
-			# 'CHATMESSAGE id BODY first line\nCHATMESSAGE id BODY second line'
-			prefix = " ".join(reply.split(" ")[:3])
-			reply = ["%s %s" % (prefix, i) for i in " ".join(reply.split(" ")[3:]).split("\n")]
-		else:
-			reply = [reply]
-		for i in reply:
-			dprint('<< ' + i)
-		return reply
+			self.skype._DoCommand(msg_text)
+		except Skype4Py.ISkypeError:
+			pass
 
 class Options:
@@ -186,9 +168,9 @@
 			print 'skyped is started on port %s, pid: %d' % (options.port, pid)
 			sys.exit(0)
-	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-	signal.signal(signal.SIGINT, sig_handler)
-	mainloop = gobject.MainLoop()
 	server('0.0.0.0', options.port)
-	while True:
+	try:
 		skype = SkypeApi()
-		mainloop.run()
+	except Skype4Py.errors.ISkypeAPIError, s:
+		sys.exit("%s. Are you sure you have started Skype?" % s)
+	gobject.idle_add(idle_handler, skype)
+	gobject.MainLoop().run()
