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): |
---|
22 | mainloop.quit() |
---|
23 | |
---|
24 | def input_handler(fd, io_condition): |
---|
25 | input = fd.recv(1024) |
---|
26 | for i in input.split("\n"): |
---|
27 | if i: |
---|
28 | fd.send((skype.send(i.strip()) + "\n").encode(locale.getdefaultlocale()[1])) |
---|
29 | return True |
---|
30 | |
---|
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 | |
---|
49 | class SkypeApi(dbus.service.Object): |
---|
50 | def __init__(self): |
---|
51 | bus = dbus.SessionBus() |
---|
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?") |
---|
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 | # skype -> client (async) |
---|
65 | @dbus.service.method(dbus_interface='com.Skype.API') |
---|
66 | def Notify(self, msg_text): |
---|
67 | global conn |
---|
68 | dprint('<< ' + msg_text) |
---|
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 |
---|
76 | dprint('>> ' + msg_text) |
---|
77 | try: |
---|
78 | reply = self.skype_api.Invoke(msg_text) |
---|
79 | except dbus.exceptions.DBusException, s: |
---|
80 | reply = str(s) |
---|
81 | dprint('<< ' + reply) |
---|
82 | return reply |
---|
83 | |
---|
84 | if __name__=='__main__': |
---|
85 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
---|
86 | skype = SkypeApi() |
---|
87 | signal.signal(signal.SIGINT, sig_handler) |
---|
88 | mainloop = gobject.MainLoop() |
---|
89 | server('localhost', 2727) |
---|
90 | mainloop.run() |
---|