[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 | |
---|
[4ddda13] | 27 | import sys |
---|
[8237df5] | 28 | import os |
---|
[4ddda13] | 29 | import signal |
---|
| 30 | import locale |
---|
| 31 | import time |
---|
| 32 | import dbus |
---|
| 33 | import dbus.service |
---|
| 34 | import dbus.mainloop.glib |
---|
| 35 | import gobject |
---|
| 36 | import socket |
---|
[8237df5] | 37 | import getopt |
---|
[4ddda13] | 38 | |
---|
[8237df5] | 39 | __version__ = "0.1.1" |
---|
[4ddda13] | 40 | |
---|
| 41 | SKYPE_SERVICE = 'com.Skype.API' |
---|
| 42 | CLIENT_NAME = 'SkypeApiPythonShell' |
---|
| 43 | |
---|
| 44 | # well, this is a bit hackish. we store the socket of the last connected client |
---|
| 45 | # here and notify it. maybe later notify all connected clients? |
---|
| 46 | conn = None |
---|
| 47 | |
---|
| 48 | def sig_handler(signum, frame): |
---|
[94bd28f] | 49 | mainloop.quit() |
---|
[4ddda13] | 50 | |
---|
| 51 | def input_handler(fd, io_condition): |
---|
[94bd28f] | 52 | input = fd.recv(1024) |
---|
| 53 | for i in input.split("\n"): |
---|
[a316c4e] | 54 | if i: |
---|
[f932b65] | 55 | fd.send((skype.send(i.strip()) + "\n").encode(locale.getdefaultlocale()[1])) |
---|
[94bd28f] | 56 | return True |
---|
[4ddda13] | 57 | |
---|
[a316c4e] | 58 | def server(host, port): |
---|
| 59 | sock = socket.socket() |
---|
| 60 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 61 | sock.bind((host, port)) |
---|
| 62 | sock.listen(1) |
---|
| 63 | gobject.io_add_watch(sock, gobject.IO_IN, listener) |
---|
| 64 | |
---|
| 65 | def listener(sock, *args): |
---|
| 66 | global conn |
---|
| 67 | conn, addr = sock.accept() |
---|
| 68 | fileno = conn.fileno() |
---|
| 69 | gobject.io_add_watch(conn, gobject.IO_IN, input_handler) |
---|
| 70 | return True |
---|
| 71 | |
---|
| 72 | def dprint(msg): |
---|
[8237df5] | 73 | global options |
---|
| 74 | |
---|
| 75 | if options.debug: |
---|
[a316c4e] | 76 | print msg |
---|
| 77 | |
---|
[4ddda13] | 78 | class SkypeApi(dbus.service.Object): |
---|
[94bd28f] | 79 | def __init__(self): |
---|
| 80 | bus = dbus.SessionBus() |
---|
[a316c4e] | 81 | try: |
---|
| 82 | self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype') |
---|
| 83 | except dbus.exceptions.DBusException: |
---|
| 84 | sys.exit("Can't find any Skype instance. Are you sure you have started Skype?") |
---|
[94bd28f] | 85 | |
---|
[a316c4e] | 86 | reply = self.send('NAME ' + CLIENT_NAME) |
---|
| 87 | if reply != 'OK': |
---|
| 88 | sys.exit('Could not bind to Skype client') |
---|
[94bd28f] | 89 | |
---|
[a316c4e] | 90 | reply = self.send('PROTOCOL 5') |
---|
[3953172] | 91 | try: |
---|
| 92 | dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') |
---|
| 93 | except KeyError: |
---|
| 94 | sys.exit() |
---|
[94bd28f] | 95 | |
---|
| 96 | # skype -> client (async) |
---|
| 97 | @dbus.service.method(dbus_interface='com.Skype.API') |
---|
| 98 | def Notify(self, msg_text): |
---|
| 99 | global conn |
---|
[f932b65] | 100 | dprint('<< ' + msg_text) |
---|
[94bd28f] | 101 | if conn: |
---|
| 102 | conn.send(msg_text + "\n") |
---|
| 103 | |
---|
| 104 | # client -> skype (sync, 5 sec timeout) |
---|
| 105 | def send(self, msg_text): |
---|
| 106 | if not len(msg_text): |
---|
| 107 | return |
---|
[a316c4e] | 108 | dprint('>> ' + msg_text) |
---|
[94bd28f] | 109 | try: |
---|
[f932b65] | 110 | reply = self.skype_api.Invoke(msg_text) |
---|
[94bd28f] | 111 | except dbus.exceptions.DBusException, s: |
---|
| 112 | reply = str(s) |
---|
[3953172] | 113 | if(reply.startswith("org.freedesktop.DBus.Error.ServiceUnknown")): |
---|
[43e2e9d] | 114 | try: |
---|
| 115 | self.remove_from_connection(dbus.SessionBus(), "/com/Skype/Client") |
---|
| 116 | except LookupError: |
---|
| 117 | pass |
---|
[3953172] | 118 | mainloop.quit() |
---|
[a316c4e] | 119 | dprint('<< ' + reply) |
---|
[94bd28f] | 120 | return reply |
---|
[4ddda13] | 121 | |
---|
[8237df5] | 122 | class Options: |
---|
| 123 | def __init__(self): |
---|
| 124 | self.daemon = True |
---|
| 125 | self.debug = False |
---|
| 126 | self.help = False |
---|
| 127 | self.port = 2727 |
---|
| 128 | self.version = False |
---|
| 129 | |
---|
| 130 | def usage(self, ret): |
---|
| 131 | print """Usage: skyped [OPTION]... |
---|
| 132 | |
---|
| 133 | skyped is a daemon that acts as a tcp server on top of a Skype instance. |
---|
| 134 | |
---|
| 135 | Options: |
---|
| 136 | -d --debug enable debug messages |
---|
| 137 | -h --help this help |
---|
| 138 | -n --nofork don't run as daemon in the background |
---|
| 139 | -p --port set the tcp port (default: %d) |
---|
| 140 | -v --version display version information""" % self.port |
---|
| 141 | sys.exit(ret) |
---|
| 142 | |
---|
[4ddda13] | 143 | if __name__=='__main__': |
---|
[8237df5] | 144 | options = Options() |
---|
| 145 | try: |
---|
| 146 | opts, args = getopt.getopt(sys.argv[1:], "dhnp:v", ["daemon", "help", "nofork", "port=", "version"]) |
---|
| 147 | except getopt.GetoptError: |
---|
| 148 | options.usage(1) |
---|
| 149 | for opt, arg in opts: |
---|
| 150 | if opt in ("-d", "--debug"): |
---|
| 151 | options.debug = True |
---|
| 152 | elif opt in ("-h", "--help"): |
---|
| 153 | options.help = True |
---|
| 154 | elif opt in ("-n", "--nofork"): |
---|
| 155 | options.daemon = False |
---|
| 156 | elif opt in ("-p", "--port"): |
---|
| 157 | options.port = arg |
---|
| 158 | elif opt in ("-v", "--version"): |
---|
| 159 | options.version = True |
---|
| 160 | if options.help: |
---|
| 161 | options.usage(0) |
---|
| 162 | elif options.version: |
---|
| 163 | print "skyped %s" % __version__ |
---|
| 164 | sys.exit(0) |
---|
| 165 | elif options.daemon: |
---|
| 166 | pid = os.fork() |
---|
| 167 | if pid == 0: |
---|
| 168 | nullin = file('/dev/null', 'r') |
---|
| 169 | nullout = file('/dev/null', 'w') |
---|
| 170 | os.dup2(nullin.fileno(), sys.stdin.fileno()) |
---|
| 171 | os.dup2(nullout.fileno(), sys.stdout.fileno()) |
---|
| 172 | os.dup2(nullout.fileno(), sys.stderr.fileno()) |
---|
| 173 | else: |
---|
| 174 | print 'skyped is started on port %s, pid: %d' % (options.port, pid) |
---|
| 175 | sys.exit(0) |
---|
[a316c4e] | 176 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
---|
| 177 | signal.signal(signal.SIGINT, sig_handler) |
---|
| 178 | mainloop = gobject.MainLoop() |
---|
[8237df5] | 179 | server('localhost', options.port) |
---|
[3953172] | 180 | while True: |
---|
| 181 | skype = SkypeApi() |
---|
| 182 | mainloop.run() |
---|