[4ddda13] | 1 | #!/usr/bin/env python |
---|
[cd3022c] | 2 | # |
---|
| 3 | # skyped.py |
---|
| 4 | # |
---|
[a74f5b5] | 5 | # Copyright (c) 2007, 2008 by Miklos Vajna <vmiklos@frugalware.org> |
---|
[cd3022c] | 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 gobject |
---|
| 33 | import socket |
---|
[8237df5] | 34 | import getopt |
---|
[c15f71a] | 35 | import Skype4Py |
---|
[5245e9d] | 36 | import sha |
---|
| 37 | from ConfigParser import ConfigParser |
---|
[c7304b2] | 38 | from OpenSSL import SSL |
---|
[eeeb30e] | 39 | from traceback import print_exception |
---|
| 40 | #from exceptions import KeyboardInterrupt |
---|
[4ddda13] | 41 | |
---|
[8237df5] | 42 | __version__ = "0.1.1" |
---|
[4ddda13] | 43 | |
---|
| 44 | SKYPE_SERVICE = 'com.Skype.API' |
---|
| 45 | CLIENT_NAME = 'SkypeApiPythonShell' |
---|
| 46 | |
---|
[eeeb30e] | 47 | def eh(type, value, tb): |
---|
| 48 | if type == KeyboardInterrupt: |
---|
| 49 | sys.exit("Exiting.") |
---|
| 50 | print_exception(type, value, tb) |
---|
| 51 | sys.exit(1) |
---|
| 52 | |
---|
| 53 | sys.excepthook = eh |
---|
| 54 | |
---|
[4ddda13] | 55 | def input_handler(fd, io_condition): |
---|
[5245e9d] | 56 | global options |
---|
| 57 | if options.buf: |
---|
| 58 | for i in options.buf: |
---|
| 59 | skype.send(i.strip()) |
---|
| 60 | options.buf = None |
---|
| 61 | else: |
---|
| 62 | input = fd.recv(1024) |
---|
| 63 | for i in input.split("\n"): |
---|
| 64 | skype.send(i.strip()) |
---|
| 65 | return True |
---|
[c15f71a] | 66 | |
---|
| 67 | def idle_handler(skype): |
---|
[3922d44] | 68 | try: |
---|
| 69 | skype.skype.SendCommand(skype.skype.Command(-1, "PING")) |
---|
| 70 | except Skype4Py.SkypeAPIError, s: |
---|
| 71 | dprint("Warning, pinging Skype failed (%s)." % (s)) |
---|
[d86dfb1] | 72 | time.sleep(2) |
---|
[94bd28f] | 73 | return True |
---|
[4ddda13] | 74 | |
---|
[a316c4e] | 75 | def server(host, port): |
---|
[c7304b2] | 76 | global options |
---|
| 77 | |
---|
| 78 | ctx = SSL.Context(SSL.TLSv1_METHOD) |
---|
| 79 | ctx.use_privatekey_file(options.config.sslkey) |
---|
| 80 | ctx.use_certificate_file(options.config.sslcert) |
---|
| 81 | sock = SSL.Connection(ctx, socket.socket()) |
---|
[a316c4e] | 82 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 83 | sock.bind((host, port)) |
---|
| 84 | sock.listen(1) |
---|
| 85 | gobject.io_add_watch(sock, gobject.IO_IN, listener) |
---|
| 86 | |
---|
| 87 | def listener(sock, *args): |
---|
[5245e9d] | 88 | global options |
---|
| 89 | options.conn, addr = sock.accept() |
---|
| 90 | ret = 0 |
---|
[c7304b2] | 91 | line = options.conn.recv(1024) |
---|
| 92 | if line.startswith("USERNAME") and line.split(' ')[1].strip() == options.config.username: |
---|
| 93 | ret += 1 |
---|
| 94 | line = options.conn.recv(1024) |
---|
| 95 | if line.startswith("PASSWORD") and sha.sha(line.split(' ')[1].strip()).hexdigest() == options.config.password: |
---|
| 96 | ret += 1 |
---|
[5245e9d] | 97 | if ret == 2: |
---|
| 98 | dprint("Username and password OK.") |
---|
[c7304b2] | 99 | options.conn.send("PASSWORD OK\n") |
---|
[5245e9d] | 100 | gobject.io_add_watch(options.conn, gobject.IO_IN, input_handler) |
---|
| 101 | return True |
---|
| 102 | else: |
---|
| 103 | dprint("Username and/or password WRONG.") |
---|
[c7304b2] | 104 | options.conn.send("PASSWORD KO\n") |
---|
[5245e9d] | 105 | return False |
---|
[a316c4e] | 106 | |
---|
| 107 | def dprint(msg): |
---|
[8237df5] | 108 | global options |
---|
| 109 | |
---|
| 110 | if options.debug: |
---|
[a316c4e] | 111 | print msg |
---|
| 112 | |
---|
[c15f71a] | 113 | class SkypeApi(): |
---|
[94bd28f] | 114 | def __init__(self): |
---|
[c15f71a] | 115 | self.skype = Skype4Py.Skype() |
---|
[5268bd7] | 116 | self.skype.OnNotify = self.recv |
---|
[c15f71a] | 117 | self.skype.Attach() |
---|
[94bd28f] | 118 | |
---|
[5268bd7] | 119 | def recv(self, msg_text): |
---|
[5245e9d] | 120 | global options |
---|
[d86dfb1] | 121 | if msg_text == "PONG": |
---|
| 122 | return |
---|
[c15f71a] | 123 | if "\n" in msg_text: |
---|
[7613670] | 124 | # crappy skype prefixes only the first line for |
---|
| 125 | # multiline messages so we need to do so for the other |
---|
| 126 | # lines, too. this is something like: |
---|
| 127 | # 'CHATMESSAGE id BODY first line\nsecond line' -> |
---|
| 128 | # 'CHATMESSAGE id BODY first line\nCHATMESSAGE id BODY second line' |
---|
[c15f71a] | 129 | prefix = " ".join(msg_text.split(" ")[:3]) |
---|
| 130 | msg_text = ["%s %s" % (prefix, i) for i in " ".join(msg_text.split(" ")[3:]).split("\n")] |
---|
[7613670] | 131 | else: |
---|
[c15f71a] | 132 | msg_text = [msg_text] |
---|
| 133 | for i in msg_text: |
---|
[a75f2a7] | 134 | # use utf-8 here to solve the following problem: |
---|
| 135 | # people use env vars like LC_ALL=en_US (latin1) then |
---|
| 136 | # they complain about why can't they receive latin2 |
---|
| 137 | # messages.. so here it is: always use utf-8 then |
---|
| 138 | # everybody will be happy |
---|
| 139 | e = i.encode('UTF-8') |
---|
[52d779e] | 140 | dprint('<< ' + e) |
---|
[5245e9d] | 141 | if options.conn: |
---|
[af8675f] | 142 | try: |
---|
[5245e9d] | 143 | options.conn.send(e + "\n") |
---|
[af8675f] | 144 | except IOError, s: |
---|
[a75f2a7] | 145 | dprint("Warning, sending '%s' failed (%s)." % (e, s)) |
---|
[c15f71a] | 146 | |
---|
| 147 | def send(self, msg_text): |
---|
| 148 | if not len(msg_text): |
---|
| 149 | return |
---|
[52d779e] | 150 | e = msg_text.decode(locale.getdefaultlocale()[1]) |
---|
| 151 | dprint('>> ' + e) |
---|
[c15f71a] | 152 | try: |
---|
[05cf927] | 153 | c = self.skype.Command(e, Block=True) |
---|
| 154 | self.skype.SendCommand(c) |
---|
| 155 | self.recv(c.Reply) |
---|
| 156 | except Skype4Py.SkypeError: |
---|
[c15f71a] | 157 | pass |
---|
[8b3beef] | 158 | except Skype4Py.SkypeAPIError, s: |
---|
[a75f2a7] | 159 | dprint("Warning, sending '%s' failed (%s)." % (e, s)) |
---|
[4ddda13] | 160 | |
---|
[8237df5] | 161 | class Options: |
---|
| 162 | def __init__(self): |
---|
[c7304b2] | 163 | self.cfgpath = "/usr/local/etc/skyped/skyped.conf" |
---|
[8237df5] | 164 | self.daemon = True |
---|
| 165 | self.debug = False |
---|
| 166 | self.help = False |
---|
[7e450c3] | 167 | self.host = "0.0.0.0" |
---|
[8237df5] | 168 | self.port = 2727 |
---|
| 169 | self.version = False |
---|
[5245e9d] | 170 | # well, this is a bit hackish. we store the socket of the last connected client |
---|
| 171 | # here and notify it. maybe later notify all connected clients? |
---|
| 172 | self.conn = None |
---|
| 173 | # this will be read first by the input handler |
---|
| 174 | self.buf = None |
---|
| 175 | |
---|
[8237df5] | 176 | |
---|
| 177 | def usage(self, ret): |
---|
| 178 | print """Usage: skyped [OPTION]... |
---|
| 179 | |
---|
| 180 | skyped is a daemon that acts as a tcp server on top of a Skype instance. |
---|
| 181 | |
---|
| 182 | Options: |
---|
[5245e9d] | 183 | -c --config path to configuration file (default: %s) |
---|
[8237df5] | 184 | -d --debug enable debug messages |
---|
| 185 | -h --help this help |
---|
[7e450c3] | 186 | -H --host set the tcp host (default: %s) |
---|
[8237df5] | 187 | -n --nofork don't run as daemon in the background |
---|
| 188 | -p --port set the tcp port (default: %d) |
---|
[5245e9d] | 189 | -v --version display version information""" % (self.cfgpath, self.host, self.port) |
---|
[8237df5] | 190 | sys.exit(ret) |
---|
| 191 | |
---|
[4ddda13] | 192 | if __name__=='__main__': |
---|
[8237df5] | 193 | options = Options() |
---|
| 194 | try: |
---|
[5245e9d] | 195 | opts, args = getopt.getopt(sys.argv[1:], "c:dhH:np:v", ["config=", "daemon", "help", "host=", "nofork", "port=", "version"]) |
---|
[8237df5] | 196 | except getopt.GetoptError: |
---|
| 197 | options.usage(1) |
---|
| 198 | for opt, arg in opts: |
---|
[5245e9d] | 199 | if opt in ("-c", "--config"): |
---|
| 200 | options.cfgpath = arg |
---|
| 201 | elif opt in ("-d", "--debug"): |
---|
[8237df5] | 202 | options.debug = True |
---|
| 203 | elif opt in ("-h", "--help"): |
---|
| 204 | options.help = True |
---|
[7e450c3] | 205 | elif opt in ("-H", "--host"): |
---|
| 206 | options.host = arg |
---|
[8237df5] | 207 | elif opt in ("-n", "--nofork"): |
---|
| 208 | options.daemon = False |
---|
| 209 | elif opt in ("-p", "--port"): |
---|
| 210 | options.port = arg |
---|
| 211 | elif opt in ("-v", "--version"): |
---|
| 212 | options.version = True |
---|
| 213 | if options.help: |
---|
| 214 | options.usage(0) |
---|
| 215 | elif options.version: |
---|
| 216 | print "skyped %s" % __version__ |
---|
| 217 | sys.exit(0) |
---|
[5245e9d] | 218 | # parse our config |
---|
| 219 | if not os.path.exists(options.cfgpath): |
---|
| 220 | print "Can't find configuration file at '%s'." % options.cfgpath |
---|
| 221 | print "Use the -c option to specify an alternate one." |
---|
| 222 | sys.exit(1) |
---|
| 223 | options.config = ConfigParser() |
---|
| 224 | options.config.read(options.cfgpath) |
---|
| 225 | options.config.username = options.config.get('skyped', 'username').split('#')[0] |
---|
| 226 | options.config.password = options.config.get('skyped', 'password').split('#')[0] |
---|
[c7304b2] | 227 | options.config.sslkey = options.config.get('skyped', 'key').split('#')[0] |
---|
| 228 | options.config.sslcert = options.config.get('skyped', 'cert').split('#')[0] |
---|
[5245e9d] | 229 | dprint("Parsing config file '%s' done, username is '%s'." % (options.cfgpath, options.config.username)) |
---|
| 230 | if options.daemon: |
---|
[8237df5] | 231 | pid = os.fork() |
---|
| 232 | if pid == 0: |
---|
| 233 | nullin = file('/dev/null', 'r') |
---|
| 234 | nullout = file('/dev/null', 'w') |
---|
| 235 | os.dup2(nullin.fileno(), sys.stdin.fileno()) |
---|
| 236 | os.dup2(nullout.fileno(), sys.stdout.fileno()) |
---|
| 237 | os.dup2(nullout.fileno(), sys.stderr.fileno()) |
---|
| 238 | else: |
---|
| 239 | print 'skyped is started on port %s, pid: %d' % (options.port, pid) |
---|
| 240 | sys.exit(0) |
---|
[7e450c3] | 241 | server(options.host, options.port) |
---|
[c15f71a] | 242 | try: |
---|
[3953172] | 243 | skype = SkypeApi() |
---|
[8b3beef] | 244 | except Skype4Py.SkypeAPIError, s: |
---|
[c15f71a] | 245 | sys.exit("%s. Are you sure you have started Skype?" % s) |
---|
| 246 | gobject.idle_add(idle_handler, skype) |
---|
| 247 | gobject.MainLoop().run() |
---|