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 | local_encoding = locale.getdefaultlocale()[1] |
---|
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): |
---|
37 | mainloop.quit() |
---|
38 | |
---|
39 | def input_handler(fd, io_condition): |
---|
40 | input = fd.recv(1024) |
---|
41 | for i in input.split("\n"): |
---|
42 | if i: |
---|
43 | fd.send(skype.send(i.strip()) + "\n") |
---|
44 | return True |
---|
45 | |
---|
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 | |
---|
64 | class SkypeApi(dbus.service.Object): |
---|
65 | def __init__(self): |
---|
66 | bus = dbus.SessionBus() |
---|
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?") |
---|
71 | |
---|
72 | reply = self.send('NAME ' + CLIENT_NAME) |
---|
73 | if reply != 'OK': |
---|
74 | sys.exit('Could not bind to Skype client') |
---|
75 | |
---|
76 | reply = self.send('PROTOCOL 5') |
---|
77 | dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') |
---|
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) |
---|
84 | dprint('<< ' + text) |
---|
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 |
---|
92 | dprint('>> ' + msg_text) |
---|
93 | try: |
---|
94 | reply = utf8_decode(self.skype_api.Invoke(utf8_encode(msg_text))) |
---|
95 | except dbus.exceptions.DBusException, s: |
---|
96 | reply = str(s) |
---|
97 | dprint('<< ' + reply) |
---|
98 | return reply |
---|
99 | |
---|
100 | if __name__=='__main__': |
---|
101 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
---|
102 | skype = SkypeApi() |
---|
103 | signal.signal(signal.SIGINT, sig_handler) |
---|
104 | mainloop = gobject.MainLoop() |
---|
105 | server('localhost', 2727) |
---|
106 | mainloop.run() |
---|