source: skype/skyped.py @ 8b8d1bed

Last change on this file since 8b8d1bed was 53eb75c, checked in by Miklos Vajna <vmiklos@…>, at 2010-12-29T01:49:32Z

Clean up some leftover debug messages

  • Property mode set to 100644
File size: 12.3 KB
RevLine 
[47c590c]1#!/usr/bin/env python2.7
[cd3022c]2#
3#   skyped.py
4
[7cf146f]5#   Copyright (c) 2007, 2008, 2009, 2010 by Miklos Vajna <vmiklos@frugalware.org>
[cd3022c]6#
7#   This program is free software; you can redistribute it and/or modify
8#   it under the terms of the GNU General Public License as published by
9#   the Free Software Foundation; either version 2 of the License, or
10#   (at your option) any later version.
11#
12#   This program is distributed in the hope that it will be useful,
13#   but WITHOUT ANY WARRANTY; without even the implied warranty of
14#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#   GNU General Public License for more details.
16
17#   You should have received a copy of the GNU General Public License
18#   along with this program; if not, write to the Free Software
19#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20#   USA.
21#
22
[4ddda13]23import sys
[8237df5]24import os
[4ddda13]25import signal
26import locale
27import time
28import socket
[8237df5]29import getopt
[c15f71a]30import Skype4Py
[8edfc90]31import hashlib
[d891915]32from ConfigParser import ConfigParser, NoOptionError
[eeeb30e]33from traceback import print_exception
[c7000bb]34import ssl
[eeab8bc]35import select
[d45adcf]36import threading
[4ddda13]37
[8237df5]38__version__ = "0.1.1"
[4ddda13]39
[eeeb30e]40def eh(type, value, tb):
[a618ea6]41        global options
42
[3a2a0b2]43        if type != KeyboardInterrupt:
44                print_exception(type, value, tb)
[53eb75c]45        if options.conn:
46                options.conn.close()
[7415989]47        # shut down client if it's running
48        try:
49                skype.skype.Client.Shutdown()
50        except NameError:
51                pass
[3a2a0b2]52        sys.exit("Exiting.")
[eeeb30e]53
54sys.excepthook = eh
55
[d45adcf]56def wait_for_lock(lock, timeout_to_print, timeout, msg):
57        start = time.time()
58        locked = lock.acquire(0)
59        while not(locked):
60                time.sleep(0.5)
61                if timeout_to_print and (time.time() - timeout_to_print > start):
62                        dprint("%s: Waited %f seconds" % \
63                                        (msg, time.time() - start))
64                        timeout_to_print = False
65                if timeout and (time.time() - timeout > start):
66                        dprint("%s: Waited %f seconds, giving up" % \
67                                        (msg, time.time() - start))
68                        return False
69                locked = lock.acquire(0)
70        return True
71
[e530abd]72def input_handler(fd):
[5245e9d]73        global options
[e530abd]74        global skype
[5245e9d]75        if options.buf:
76                for i in options.buf:
77                        skype.send(i.strip())
78                options.buf = None
[e530abd]79                return True
[5245e9d]80        else:
[f85837a]81                close_socket = False
[d45adcf]82                if wait_for_lock(options.lock, 3, 10, "input_handler"):
83                        try:
84                                        input = fd.recv(1024)
85                                        options.lock.release()
86                        except Exception, s:
87                                dprint("Warning, receiving 1024 bytes failed (%s)." % s)
88                                fd.close()
89                                options.conn = False
90                                options.lock.release()
91                                return False
92                        for i in input.split("\n"):
[f85837a]93                                if i.strip() == "SET USERSTATUS OFFLINE":
94                                        close_socket = True
[d45adcf]95                                skype.send(i.strip())
[f85837a]96                return not(close_socket)
[c15f71a]97
[4b0092e]98def skype_idle_handler(skype):
[3922d44]99        try:
[6af541d]100                c = skype.skype.Command("PING", Block=True)
101                skype.skype.SendCommand(c)
[3922d44]102        except Skype4Py.SkypeAPIError, s:
103                dprint("Warning, pinging Skype failed (%s)." % (s))
[94bd28f]104        return True
[4ddda13]105
[a618ea6]106def send(sock, txt):
[1130561]107        global options
[a618ea6]108        count = 1
109        done = False
[1130561]110        while (not done) and (count < 10) and options.conn:
[d45adcf]111                if wait_for_lock(options.lock, 3, 10, "socket send"):
112                        try:
[1130561]113                                if options.conn: sock.send(txt)
[d45adcf]114                                options.lock.release()
115                                done = True
116                        except Exception, s:
117                                options.lock.release()
118                                count += 1
119                                dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count))
120                                time.sleep(1)
[a618ea6]121        if not done:
[53eb75c]122                if options.conn:
123                        options.conn.close()
[e530abd]124                options.conn = False
125        return done
[a618ea6]126
[4b0092e]127def bitlbee_idle_handler(skype):
[eeab8bc]128        global options
[e530abd]129        done = False
[4b0092e]130        if options.conn:
131                try:
132                        e = "PING"
[e530abd]133                        done = send(options.conn, "%s\n" % e)
[4b0092e]134                except Exception, s:
135                        dprint("Warning, sending '%s' failed (%s)." % (e, s))
[e530abd]136                        if options.conn: options.conn.close()
137                        options.conn = False
138                        done = False
139        return done
[4b0092e]140
[eeab8bc]141def server(host, port, skype):
[c7304b2]142        global options
[c7000bb]143        sock = socket.socket()
[a316c4e]144        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
145        sock.bind((host, port))
146        sock.listen(1)
[eeab8bc]147        dprint("Waiting for connection...")
148        listener(sock, skype)
[a316c4e]149
[eeab8bc]150def listener(sock, skype):
[5245e9d]151        global options
[d45adcf]152        if not(wait_for_lock(options.lock, 3, 10, "listener")): return False
[c7000bb]153        rawsock, addr = sock.accept()
154        options.conn = ssl.wrap_socket(rawsock,
155                server_side=True,
156                certfile=options.config.sslcert,
157                keyfile=options.config.sslkey,
158                ssl_version=ssl.PROTOCOL_TLSv1)
[b0d40f5]159        if hasattr(options.conn, 'handshake'):
[5588f7c4]160                try:
161                        options.conn.handshake()
162                except Exception:
[d45adcf]163                        options.lock.release()
[5588f7c4]164                        dprint("Warning, handshake failed, closing connection.")
165                        return False
[5245e9d]166        ret = 0
[6b9cab1]167        try:
168                line = options.conn.recv(1024)
169                if line.startswith("USERNAME") and line.split(' ')[1].strip() == options.config.username:
170                        ret += 1
171                line = options.conn.recv(1024)
[8edfc90]172                if line.startswith("PASSWORD") and hashlib.sha1(line.split(' ')[1].strip()).hexdigest() == options.config.password:
[6b9cab1]173                        ret += 1
174        except Exception, s:
175                dprint("Warning, receiving 1024 bytes failed (%s)." % s)
176                options.conn.close()
[e530abd]177                options.conn = False
[d45adcf]178                options.lock.release()
[6b9cab1]179                return False
[5245e9d]180        if ret == 2:
181                dprint("Username and password OK.")
[c7304b2]182                options.conn.send("PASSWORD OK\n")
[d45adcf]183                options.lock.release()
[eeab8bc]184                serverloop(options, skype)
[5245e9d]185                return True
186        else:
187                dprint("Username and/or password WRONG.")
[c7304b2]188                options.conn.send("PASSWORD KO\n")
[e530abd]189                options.conn.close()
190                options.conn = False
[d45adcf]191                options.lock.release()
[5245e9d]192                return False
[a316c4e]193
194def dprint(msg):
[ffd078a]195        from time import strftime
[8237df5]196        global options
197
[ffd078a]198        now = strftime("%Y-%m-%d %H:%M:%S")
199
[8237df5]200        if options.debug:
[ffd078a]201                print now + ": " + msg
[a618ea6]202                sys.stdout.flush()
[bcdc24b]203        if options.log:
204                sock = open(options.log, "a")
[ea1d796]205                sock.write("%s: %s\n" % (now, msg))
[bcdc24b]206                sock.close()
[a316c4e]207
[944a941]208class SkypeApi:
[94bd28f]209        def __init__(self):
[c15f71a]210                self.skype = Skype4Py.Skype()
[5268bd7]211                self.skype.OnNotify = self.recv
[6af541d]212                self.skype.Client.Start()
[94bd28f]213
[5268bd7]214        def recv(self, msg_text):
[5245e9d]215                global options
[d86dfb1]216                if msg_text == "PONG":
217                        return
[c15f71a]218                if "\n" in msg_text:
[7613670]219                        # crappy skype prefixes only the first line for
220                        # multiline messages so we need to do so for the other
221                        # lines, too. this is something like:
222                        # 'CHATMESSAGE id BODY first line\nsecond line' ->
223                        # 'CHATMESSAGE id BODY first line\nCHATMESSAGE id BODY second line'
[c15f71a]224                        prefix = " ".join(msg_text.split(" ")[:3])
225                        msg_text = ["%s %s" % (prefix, i) for i in " ".join(msg_text.split(" ")[3:]).split("\n")]
[7613670]226                else:
[c15f71a]227                        msg_text = [msg_text]
228                for i in msg_text:
[a75f2a7]229                        # use utf-8 here to solve the following problem:
230                        # people use env vars like LC_ALL=en_US (latin1) then
231                        # they complain about why can't they receive latin2
232                        # messages.. so here it is: always use utf-8 then
233                        # everybody will be happy
234                        e = i.encode('UTF-8')
[5245e9d]235                        if options.conn:
[e530abd]236                                dprint('<< ' + e)
[af8675f]237                                try:
[eeab8bc]238                                        # I called the send function really_send
[a618ea6]239                                        send(options.conn, e + "\n")
[80dfdce]240                                except Exception, s:
[a75f2a7]241                                        dprint("Warning, sending '%s' failed (%s)." % (e, s))
[e530abd]242                                        if options.conn: options.conn.close()
243                                        options.conn = False
244                        else:
[53eb75c]245                                dprint('-- ' + e)
[c15f71a]246
247        def send(self, msg_text):
[4b0092e]248                if not len(msg_text) or msg_text == "PONG":
[53eb75c]249                        if msg_text == "PONG":
250                                options.last_bitlbee_pong = time.time()
[c15f71a]251                        return
[885e563e]252                try:
[072c0fe]253                        encoding = locale.getdefaultlocale()[1]
254                        if not encoding:
255                                raise ValueError
256                        e = msg_text.decode(encoding)
[885e563e]257                except ValueError:
258                        e = msg_text.decode('UTF-8')
[52d779e]259                dprint('>> ' + e)
[c15f71a]260                try:
[05cf927]261                        c = self.skype.Command(e, Block=True)
262                        self.skype.SendCommand(c)
263                        self.recv(c.Reply)
264                except Skype4Py.SkypeError:
[c15f71a]265                        pass
[8b3beef]266                except Skype4Py.SkypeAPIError, s:
[a75f2a7]267                        dprint("Warning, sending '%s' failed (%s)." % (e, s))
[4ddda13]268
[8237df5]269class Options:
270        def __init__(self):
[1b48afb]271                self.cfgpath = os.path.join(os.environ['HOME'], ".skyped", "skyped.conf")
[1a575f69]272                # for backwards compatibility
273                self.syscfgpath = "/usr/local/etc/skyped/skyped.conf"
274                if os.path.exists(self.syscfgpath):
275                        self.cfgpath = self.syscfgpath
[8237df5]276                self.daemon = True
277                self.debug = False
278                self.help = False
[7e450c3]279                self.host = "0.0.0.0"
[bcdc24b]280                self.log = None
[d891915]281                self.port = None
[8237df5]282                self.version = False
[5245e9d]283                # well, this is a bit hackish. we store the socket of the last connected client
284                # here and notify it. maybe later notify all connected clients?
285                self.conn = None
286                # this will be read first by the input handler
287                self.buf = None
288
[8237df5]289
290        def usage(self, ret):
291                print """Usage: skyped [OPTION]...
292
293skyped is a daemon that acts as a tcp server on top of a Skype instance.
294
295Options:
[5245e9d]296        -c      --config        path to configuration file (default: %s)
[8237df5]297        -d      --debug         enable debug messages
298        -h      --help          this help
[7e450c3]299        -H      --host          set the tcp host (default: %s)
[bcdc24b]300        -l      --log           set the log file in background mode (default: none)
[8237df5]301        -n      --nofork        don't run as daemon in the background
[a349932]302        -p      --port          set the tcp port (default: %s)
[5245e9d]303        -v      --version       display version information""" % (self.cfgpath, self.host, self.port)
[8237df5]304                sys.exit(ret)
305
[eeab8bc]306def serverloop(options, skype):
307        timeout = 1; # in seconds
308        skype_ping_period = 5
[e530abd]309        bitlbee_ping_period = 10
310        bitlbee_pong_timeout = 30
311        now = time.time()
312        skype_ping_start_time = now
313        bitlbee_ping_start_time = now
314        options.last_bitlbee_pong = now
315        in_error = []
316        handler_ok = True
317        while (len(in_error) == 0) and handler_ok and options.conn:
[eeab8bc]318                ready_to_read, ready_to_write, in_error = \
[9c51166]319                        select.select([options.conn], [], [options.conn], \
320                                timeout)
[eeab8bc]321                now = time.time()
[9c51166]322                handler_ok = len(in_error) == 0
323                if (len(ready_to_read) == 1) and handler_ok:
[e530abd]324                        handler_ok = input_handler(ready_to_read.pop())
[eeab8bc]325                        # don't ping bitlbee/skype if they already received data
[9c51166]326                        now = time.time() # allow for the input_handler to take some time
[eeab8bc]327                        bitlbee_ping_start_time = now
328                        skype_ping_start_time = now
[9c51166]329                        options.last_bitlbee_pong = now
[e530abd]330                if (now - skype_ping_period > skype_ping_start_time) and handler_ok:
331                        handler_ok = skype_idle_handler(skype)
[eeab8bc]332                        skype_ping_start_time = now
333                if now - bitlbee_ping_period > bitlbee_ping_start_time:
[e530abd]334                        handler_ok = bitlbee_idle_handler(skype)
[eeab8bc]335                        bitlbee_ping_start_time = now
[e530abd]336                        if options.last_bitlbee_pong:
337                                if (now - options.last_bitlbee_pong) > bitlbee_pong_timeout:
338                                        dprint("Bitlbee pong timeout")
339                                        # TODO is following line necessary? Should there be a options.conn.unwrap() somewhere?
340                                        # options.conn.shutdown()
[53eb75c]341                                        if options.conn:
342                                                options.conn.close()
[e530abd]343                                        options.conn = False
344                        else:
345                                options.last_bitlbee_pong = now
[eeab8bc]346
[4ddda13]347if __name__=='__main__':
[8237df5]348        options = Options()
349        try:
[a985369]350                opts, args = getopt.getopt(sys.argv[1:], "c:dhH:l:np:v", ["config=", "debug", "help", "host=", "log=", "nofork", "port=", "version"])
[8237df5]351        except getopt.GetoptError:
352                options.usage(1)
353        for opt, arg in opts:
[5245e9d]354                if opt in ("-c", "--config"):
355                        options.cfgpath = arg
356                elif opt in ("-d", "--debug"):
[8237df5]357                        options.debug = True
358                elif opt in ("-h", "--help"):
359                        options.help = True
[7e450c3]360                elif opt in ("-H", "--host"):
361                        options.host = arg
[bcdc24b]362                elif opt in ("-l", "--log"):
363                        options.log = arg
[8237df5]364                elif opt in ("-n", "--nofork"):
365                        options.daemon = False
366                elif opt in ("-p", "--port"):
[d891915]367                        options.port = int(arg)
[8237df5]368                elif opt in ("-v", "--version"):
369                        options.version = True
370        if options.help:
371                options.usage(0)
372        elif options.version:
373                print "skyped %s" % __version__
374                sys.exit(0)
[5245e9d]375        # parse our config
376        if not os.path.exists(options.cfgpath):
377                print "Can't find configuration file at '%s'." % options.cfgpath
378                print "Use the -c option to specify an alternate one."
379                sys.exit(1)
380        options.config = ConfigParser()
381        options.config.read(options.cfgpath)
382        options.config.username = options.config.get('skyped', 'username').split('#')[0]
383        options.config.password = options.config.get('skyped', 'password').split('#')[0]
[7cc2c1e]384        options.config.sslkey = os.path.expanduser(options.config.get('skyped', 'key').split('#')[0])
385        options.config.sslcert = os.path.expanduser(options.config.get('skyped', 'cert').split('#')[0])
[d891915]386        # hack: we have to parse the parameters first to locate the
387        # config file but the -p option should overwrite the value from
388        # the config file
389        try:
390                options.config.port = int(options.config.get('skyped', 'port').split('#')[0])
391                if not options.port:
392                        options.port = options.config.port
393        except NoOptionError:
394                pass
395        if not options.port:
396                options.port = 2727
[5245e9d]397        dprint("Parsing config file '%s' done, username is '%s'." % (options.cfgpath, options.config.username))
398        if options.daemon:
[8237df5]399                pid = os.fork()
400                if pid == 0:
[56ae398]401                        nullin = file(os.devnull, 'r')
402                        nullout = file(os.devnull, 'w')
[8237df5]403                        os.dup2(nullin.fileno(), sys.stdin.fileno())
404                        os.dup2(nullout.fileno(), sys.stdout.fileno())
405                        os.dup2(nullout.fileno(), sys.stderr.fileno())
406                else:
407                        print 'skyped is started on port %s, pid: %d' % (options.port, pid)
408                        sys.exit(0)
[d891915]409        else:
410                dprint('skyped is started on port %s' % options.port)
[c15f71a]411        try:
[3953172]412                skype = SkypeApi()
[8b3beef]413        except Skype4Py.SkypeAPIError, s:
[c15f71a]414                sys.exit("%s. Are you sure you have started Skype?" % s)
[e530abd]415        while 1:
416                options.conn = False
[d45adcf]417                options.lock = threading.Lock()
[e530abd]418                server(options.host, options.port, skype)
Note: See TracBrowser for help on using the repository browser.