source: skype/skyped.py @ 944a941

Last change on this file since 944a941 was 944a941, checked in by Miklos Vajna <vmiklos@…>, at 2008-04-06T18:39:32Z

a fix for python-2.4 compatibility

  • interesting, it has been reported this is the only problem, though ideally skype4py doesn't work with python-2.4 either ;)
  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[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#   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 gobject
29import socket
[8237df5]30import getopt
[c15f71a]31import Skype4Py
[5245e9d]32import sha
33from ConfigParser import ConfigParser
[c7304b2]34from OpenSSL import SSL
[eeeb30e]35from traceback import print_exception
[4ddda13]36
[8237df5]37__version__ = "0.1.1"
[4ddda13]38
39SKYPE_SERVICE = 'com.Skype.API'
40CLIENT_NAME = 'SkypeApiPythonShell'
41
[eeeb30e]42def eh(type, value, tb):
[3a2a0b2]43        if type != KeyboardInterrupt:
44                print_exception(type, value, tb)
45        gobject.MainLoop().quit()
46        skype.skype.Client.Shutdown()
47        sys.exit("Exiting.")
[eeeb30e]48
49sys.excepthook = eh
50
[4ddda13]51def input_handler(fd, io_condition):
[5245e9d]52        global options
53        if options.buf:
54                for i in options.buf:
55                        skype.send(i.strip())
56                options.buf = None
57        else:
[2eb4b1f]58                try:
59                        input = fd.recv(1024)
[239b036]60                except SSL.SysCallError:
[2eb4b1f]61                        return True
[5245e9d]62                for i in input.split("\n"):
63                        skype.send(i.strip())
64                return True
[c15f71a]65
66def idle_handler(skype):
[3922d44]67        try:
[6af541d]68                c = skype.skype.Command("PING", Block=True)
69                skype.skype.SendCommand(c)
[3922d44]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]75def 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
87def 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
107def dprint(msg):
[8237df5]108        global options
109
110        if options.debug:
[a316c4e]111                print msg
112
[944a941]113class SkypeApi:
[94bd28f]114        def __init__(self):
[c15f71a]115                self.skype = Skype4Py.Skype()
[5268bd7]116                self.skype.OnNotify = self.recv
[6af541d]117                self.skype.Client.Start()
[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]161class 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
180skyped is a daemon that acts as a tcp server on top of a Skype instance.
181
182Options:
[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]192if __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()
Note: See TracBrowser for help on using the repository browser.