[4ddda13] | 1 | #!/usr/bin/env python |
---|
| 2 | """ GPL """ |
---|
| 3 | import sys |
---|
| 4 | import signal |
---|
| 5 | import locale |
---|
| 6 | import time |
---|
| 7 | import dbus |
---|
| 8 | import dbus.service |
---|
| 9 | import dbus.mainloop.glib |
---|
| 10 | import gobject |
---|
| 11 | import socket |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | SKYPE_SERVICE = 'com.Skype.API' |
---|
| 15 | CLIENT_NAME = 'SkypeApiPythonShell' |
---|
| 16 | |
---|
[38fa988] | 17 | local_encoding = locale.getdefaultlocale()[1] |
---|
[4ddda13] | 18 | need_conv = (local_encoding != 'utf-8') |
---|
| 19 | |
---|
| 20 | # well, this is a bit hackish. we store the socket of the last connected client |
---|
| 21 | # here and notify it. maybe later notify all connected clients? |
---|
| 22 | conn = None |
---|
| 23 | |
---|
| 24 | def utf8_decode(utf8_str): |
---|
[94bd28f] | 25 | if need_conv: |
---|
| 26 | return utf8_str.decode('utf-8').encode(local_encoding, 'replace') |
---|
| 27 | else: |
---|
| 28 | return utf8_str |
---|
[4ddda13] | 29 | |
---|
| 30 | def utf8_encode(local_str): |
---|
[94bd28f] | 31 | if need_conv: |
---|
| 32 | return local_str.decode(local_encoding).encode('utf-8') |
---|
| 33 | else: |
---|
| 34 | return local_str |
---|
[4ddda13] | 35 | |
---|
| 36 | def sig_handler(signum, frame): |
---|
[94bd28f] | 37 | mainloop.quit() |
---|
[4ddda13] | 38 | |
---|
| 39 | def input_handler(fd, io_condition): |
---|
[94bd28f] | 40 | input = fd.recv(1024) |
---|
| 41 | for i in input.split("\n"): |
---|
[a316c4e] | 42 | if i: |
---|
| 43 | fd.send(skype.send(i.strip()) + "\n") |
---|
[94bd28f] | 44 | return True |
---|
[4ddda13] | 45 | |
---|
[a316c4e] | 46 | def server(host, port): |
---|
| 47 | sock = socket.socket() |
---|
| 48 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 49 | sock.bind((host, port)) |
---|
| 50 | sock.listen(1) |
---|
| 51 | gobject.io_add_watch(sock, gobject.IO_IN, listener) |
---|
| 52 | |
---|
| 53 | def listener(sock, *args): |
---|
| 54 | global conn |
---|
| 55 | conn, addr = sock.accept() |
---|
| 56 | fileno = conn.fileno() |
---|
| 57 | gobject.io_add_watch(conn, gobject.IO_IN, input_handler) |
---|
| 58 | return True |
---|
| 59 | |
---|
| 60 | def dprint(msg): |
---|
| 61 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
---|
| 62 | print msg |
---|
| 63 | |
---|
[4ddda13] | 64 | class SkypeApi(dbus.service.Object): |
---|
[94bd28f] | 65 | def __init__(self): |
---|
| 66 | bus = dbus.SessionBus() |
---|
[a316c4e] | 67 | try: |
---|
| 68 | self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype') |
---|
| 69 | except dbus.exceptions.DBusException: |
---|
| 70 | sys.exit("Can't find any Skype instance. Are you sure you have started Skype?") |
---|
[94bd28f] | 71 | |
---|
[a316c4e] | 72 | reply = self.send('NAME ' + CLIENT_NAME) |
---|
| 73 | if reply != 'OK': |
---|
| 74 | sys.exit('Could not bind to Skype client') |
---|
[94bd28f] | 75 | |
---|
[a316c4e] | 76 | reply = self.send('PROTOCOL 5') |
---|
| 77 | dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') |
---|
[94bd28f] | 78 | |
---|
| 79 | # skype -> client (async) |
---|
| 80 | @dbus.service.method(dbus_interface='com.Skype.API') |
---|
| 81 | def Notify(self, msg_text): |
---|
| 82 | global conn |
---|
| 83 | text = utf8_decode(msg_text) |
---|
[a316c4e] | 84 | dprint('<< ' + text) |
---|
[94bd28f] | 85 | if conn: |
---|
| 86 | conn.send(msg_text + "\n") |
---|
| 87 | |
---|
| 88 | # client -> skype (sync, 5 sec timeout) |
---|
| 89 | def send(self, msg_text): |
---|
| 90 | if not len(msg_text): |
---|
| 91 | return |
---|
[a316c4e] | 92 | dprint('>> ' + msg_text) |
---|
[94bd28f] | 93 | try: |
---|
| 94 | reply = utf8_decode(self.skype_api.Invoke(utf8_encode(msg_text))) |
---|
| 95 | except dbus.exceptions.DBusException, s: |
---|
| 96 | reply = str(s) |
---|
[a316c4e] | 97 | dprint('<< ' + reply) |
---|
[94bd28f] | 98 | return reply |
---|
[4ddda13] | 99 | |
---|
| 100 | if __name__=='__main__': |
---|
[a316c4e] | 101 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
---|
| 102 | skype = SkypeApi() |
---|
| 103 | signal.signal(signal.SIGINT, sig_handler) |
---|
| 104 | mainloop = gobject.MainLoop() |
---|
[4ddda13] | 105 | server('localhost', 2727) |
---|
| 106 | mainloop.run() |
---|