[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): |
---|
| 25 | if need_conv: |
---|
| 26 | return utf8_str.decode('utf-8').encode(local_encoding, 'replace') |
---|
| 27 | else: |
---|
| 28 | return utf8_str |
---|
| 29 | |
---|
| 30 | def utf8_encode(local_str): |
---|
| 31 | if need_conv: |
---|
| 32 | return local_str.decode(local_encoding).encode('utf-8') |
---|
| 33 | else: |
---|
| 34 | return local_str |
---|
| 35 | |
---|
| 36 | def sig_handler(signum, frame): |
---|
[38fa988] | 37 | print 'Caught signal %d, exiting.' % signum |
---|
[4ddda13] | 38 | mainloop.quit() |
---|
| 39 | |
---|
| 40 | def input_handler(fd, io_condition): |
---|
| 41 | input = fd.recv(1024) |
---|
| 42 | for i in input.split("\n"): |
---|
[38fa988] | 43 | if i: |
---|
| 44 | fd.send(skype.send(i.strip()) + "\n") |
---|
[4ddda13] | 45 | return True |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | class SkypeApi(dbus.service.Object): |
---|
| 49 | def __init__(self): |
---|
| 50 | bus = dbus.SessionBus() |
---|
| 51 | |
---|
| 52 | try: |
---|
| 53 | self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype') |
---|
| 54 | except dbus.exceptions.DBusException: |
---|
[38fa988] | 55 | sys.exit("Can't find any Skype instance. Are you sure you have started Skype?") |
---|
[4ddda13] | 56 | |
---|
| 57 | reply = self.send('NAME ' + CLIENT_NAME) |
---|
| 58 | if reply != 'OK': |
---|
| 59 | sys.exit('Could not bind to Skype client') |
---|
| 60 | |
---|
| 61 | reply = self.send('PROTOCOL 5') |
---|
| 62 | dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | # skype -> client (async) |
---|
| 66 | @dbus.service.method(dbus_interface='com.Skype.API') |
---|
| 67 | def Notify(self, msg_text): |
---|
| 68 | global conn |
---|
| 69 | text = utf8_decode(msg_text) |
---|
[38fa988] | 70 | print '<<', text |
---|
[4ddda13] | 71 | if conn: |
---|
| 72 | conn.send(msg_text + "\n") |
---|
| 73 | |
---|
| 74 | # client -> skype (sync, 5 sec timeout) |
---|
| 75 | def send(self, msg_text): |
---|
[38fa988] | 76 | if not len(msg_text): |
---|
| 77 | return |
---|
[4ddda13] | 78 | print '>> ', msg_text |
---|
| 79 | try: |
---|
| 80 | reply = utf8_decode(self.skype_api.Invoke(utf8_encode(msg_text))) |
---|
| 81 | except dbus.exceptions.DBusException, s: |
---|
| 82 | reply = str(s) |
---|
| 83 | print '<< ', reply |
---|
| 84 | return reply |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
---|
| 88 | skype = SkypeApi() |
---|
| 89 | signal.signal(signal.SIGINT, sig_handler) |
---|
| 90 | |
---|
| 91 | mainloop = gobject.MainLoop() |
---|
| 92 | |
---|
| 93 | def server(host, port): |
---|
| 94 | '''Initialize server and start listening.''' |
---|
| 95 | sock = socket.socket() |
---|
| 96 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 97 | sock.bind((host, port)) |
---|
| 98 | sock.listen(1) |
---|
| 99 | gobject.io_add_watch(sock, gobject.IO_IN, listener) |
---|
| 100 | def listener(sock, *args): |
---|
| 101 | '''Asynchronous connection listener. Starts a handler for each connection.''' |
---|
| 102 | global conn |
---|
| 103 | conn, addr = sock.accept() |
---|
| 104 | fileno = conn.fileno() |
---|
| 105 | gobject.io_add_watch(conn, gobject.IO_IN, input_handler) |
---|
| 106 | return True |
---|
| 107 | if __name__=='__main__': |
---|
| 108 | server('localhost', 2727) |
---|
| 109 | mainloop.run() |
---|