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