[4ddda13] | 1 | #!/usr/bin/env python |
---|
[cd3022c] | 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 |
---|
[4ddda13] | 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): |
---|
[94bd28f] | 48 | mainloop.quit() |
---|
[4ddda13] | 49 | |
---|
| 50 | def input_handler(fd, io_condition): |
---|
[94bd28f] | 51 | input = fd.recv(1024) |
---|
| 52 | for i in input.split("\n"): |
---|
[a316c4e] | 53 | if i: |
---|
[f932b65] | 54 | fd.send((skype.send(i.strip()) + "\n").encode(locale.getdefaultlocale()[1])) |
---|
[94bd28f] | 55 | return True |
---|
[4ddda13] | 56 | |
---|
[a316c4e] | 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 | |
---|
[4ddda13] | 75 | class SkypeApi(dbus.service.Object): |
---|
[94bd28f] | 76 | def __init__(self): |
---|
| 77 | bus = dbus.SessionBus() |
---|
[a316c4e] | 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?") |
---|
[94bd28f] | 82 | |
---|
[a316c4e] | 83 | reply = self.send('NAME ' + CLIENT_NAME) |
---|
| 84 | if reply != 'OK': |
---|
| 85 | sys.exit('Could not bind to Skype client') |
---|
[94bd28f] | 86 | |
---|
[a316c4e] | 87 | reply = self.send('PROTOCOL 5') |
---|
| 88 | dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') |
---|
[94bd28f] | 89 | |
---|
| 90 | # skype -> client (async) |
---|
| 91 | @dbus.service.method(dbus_interface='com.Skype.API') |
---|
| 92 | def Notify(self, msg_text): |
---|
| 93 | global conn |
---|
[f932b65] | 94 | dprint('<< ' + msg_text) |
---|
[94bd28f] | 95 | if conn: |
---|
| 96 | conn.send(msg_text + "\n") |
---|
| 97 | |
---|
| 98 | # client -> skype (sync, 5 sec timeout) |
---|
| 99 | def send(self, msg_text): |
---|
| 100 | if not len(msg_text): |
---|
| 101 | return |
---|
[a316c4e] | 102 | dprint('>> ' + msg_text) |
---|
[94bd28f] | 103 | try: |
---|
[f932b65] | 104 | reply = self.skype_api.Invoke(msg_text) |
---|
[94bd28f] | 105 | except dbus.exceptions.DBusException, s: |
---|
| 106 | reply = str(s) |
---|
[a316c4e] | 107 | dprint('<< ' + reply) |
---|
[94bd28f] | 108 | return reply |
---|
[4ddda13] | 109 | |
---|
| 110 | if __name__=='__main__': |
---|
[a316c4e] | 111 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
---|
| 112 | skype = SkypeApi() |
---|
| 113 | signal.signal(signal.SIGINT, sig_handler) |
---|
| 114 | mainloop = gobject.MainLoop() |
---|
[4ddda13] | 115 | server('localhost', 2727) |
---|
| 116 | mainloop.run() |
---|