1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # skyped.py |
---|
4 | # |
---|
5 | # Copyright (c) 2007 by Miklos Vajna <vmiklos@frugalware.org> |
---|
6 | # |
---|
7 | # It uses several code from a very basic python CLI interface, available at: |
---|
8 | # |
---|
9 | # http://forum.skype.com/index.php?showtopic=42640 |
---|
10 | # |
---|
11 | # This program is free software; you can redistribute it and/or modify |
---|
12 | # it under the terms of the GNU General Public License as published by |
---|
13 | # the Free Software Foundation; either version 2 of the License, or |
---|
14 | # (at your option) any later version. |
---|
15 | # |
---|
16 | # This program is distributed in the hope that it will be useful, |
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | # GNU General Public License for more details. |
---|
20 | # |
---|
21 | # You should have received a copy of the GNU General Public License |
---|
22 | # along with this program; if not, write to the Free Software |
---|
23 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
---|
24 | # USA. |
---|
25 | # |
---|
26 | |
---|
27 | # makepkg configuration |
---|
28 | """ GPL """ |
---|
29 | import sys |
---|
30 | import signal |
---|
31 | import locale |
---|
32 | import time |
---|
33 | import dbus |
---|
34 | import dbus.service |
---|
35 | import dbus.mainloop.glib |
---|
36 | import gobject |
---|
37 | import socket |
---|
38 | |
---|
39 | |
---|
40 | SKYPE_SERVICE = 'com.Skype.API' |
---|
41 | CLIENT_NAME = 'SkypeApiPythonShell' |
---|
42 | |
---|
43 | # well, this is a bit hackish. we store the socket of the last connected client |
---|
44 | # here and notify it. maybe later notify all connected clients? |
---|
45 | conn = None |
---|
46 | |
---|
47 | def sig_handler(signum, frame): |
---|
48 | mainloop.quit() |
---|
49 | |
---|
50 | def input_handler(fd, io_condition): |
---|
51 | input = fd.recv(1024) |
---|
52 | for i in input.split("\n"): |
---|
53 | if i: |
---|
54 | fd.send((skype.send(i.strip()) + "\n").encode(locale.getdefaultlocale()[1])) |
---|
55 | return True |
---|
56 | |
---|
57 | def server(host, port): |
---|
58 | sock = socket.socket() |
---|
59 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
60 | sock.bind((host, port)) |
---|
61 | sock.listen(1) |
---|
62 | gobject.io_add_watch(sock, gobject.IO_IN, listener) |
---|
63 | |
---|
64 | def listener(sock, *args): |
---|
65 | global conn |
---|
66 | conn, addr = sock.accept() |
---|
67 | fileno = conn.fileno() |
---|
68 | gobject.io_add_watch(conn, gobject.IO_IN, input_handler) |
---|
69 | return True |
---|
70 | |
---|
71 | def dprint(msg): |
---|
72 | if len(sys.argv) > 1 and sys.argv[1] == "-d": |
---|
73 | print msg |
---|
74 | |
---|
75 | class SkypeApi(dbus.service.Object): |
---|
76 | def __init__(self): |
---|
77 | bus = dbus.SessionBus() |
---|
78 | try: |
---|
79 | self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype') |
---|
80 | except dbus.exceptions.DBusException: |
---|
81 | sys.exit("Can't find any Skype instance. Are you sure you have started Skype?") |
---|
82 | |
---|
83 | reply = self.send('NAME ' + CLIENT_NAME) |
---|
84 | if reply != 'OK': |
---|
85 | sys.exit('Could not bind to Skype client') |
---|
86 | |
---|
87 | reply = self.send('PROTOCOL 5') |
---|
88 | try: |
---|
89 | dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') |
---|
90 | except KeyError: |
---|
91 | sys.exit() |
---|
92 | |
---|
93 | # skype -> client (async) |
---|
94 | @dbus.service.method(dbus_interface='com.Skype.API') |
---|
95 | def Notify(self, msg_text): |
---|
96 | global conn |
---|
97 | dprint('<< ' + msg_text) |
---|
98 | if conn: |
---|
99 | conn.send(msg_text + "\n") |
---|
100 | |
---|
101 | # client -> skype (sync, 5 sec timeout) |
---|
102 | def send(self, msg_text): |
---|
103 | if not len(msg_text): |
---|
104 | return |
---|
105 | dprint('>> ' + msg_text) |
---|
106 | try: |
---|
107 | reply = self.skype_api.Invoke(msg_text) |
---|
108 | except dbus.exceptions.DBusException, s: |
---|
109 | reply = str(s) |
---|
110 | if(reply.startswith("org.freedesktop.DBus.Error.ServiceUnknown")): |
---|
111 | self.remove_from_connection(dbus.SessionBus(), "/com/Skype/Client") |
---|
112 | mainloop.quit() |
---|
113 | dprint('<< ' + reply) |
---|
114 | return reply |
---|
115 | |
---|
116 | if __name__=='__main__': |
---|
117 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
---|
118 | signal.signal(signal.SIGINT, sig_handler) |
---|
119 | mainloop = gobject.MainLoop() |
---|
120 | server('localhost', 2727) |
---|
121 | while True: |
---|
122 | skype = SkypeApi() |
---|
123 | mainloop.run() |
---|