Changeset 5245e9d


Ignore:
Timestamp:
2008-01-12T01:36:54Z (16 years ago)
Author:
Miklos Vajna <vmiklos@…>
Branches:
master
Children:
a3262d1e
Parents:
a0b206b
Message:

skyped: add authentication support
this is not yet ssl, but better than nothing

Location:
skype
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • skype/skyped.py

    ra0b206b r5245e9d  
    3535import Skype4Py
    3636import threading
     37import sha
     38from ConfigParser import ConfigParser
    3739
    3840__version__ = "0.1.1"
     
    4143CLIENT_NAME = 'SkypeApiPythonShell'
    4244
    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 
    4745def input_handler(fd, io_condition):
    48         input = fd.recv(1024)
    49         for i in input.split("\n"):
    50                 skype.send(i.strip())
    51         return True
     46        global options
     47        if options.buf:
     48                for i in options.buf:
     49                        skype.send(i.strip())
     50                options.buf = None
     51        else:
     52                input = fd.recv(1024)
     53                for i in input.split("\n"):
     54                        skype.send(i.strip())
     55                return True
    5256
    5357def idle_handler(skype):
     
    7074
    7175def listener(sock, *args):
    72         global conn
    73         conn, addr = sock.accept()
    74         fileno = conn.fileno()
    75         gobject.io_add_watch(conn, gobject.IO_IN, input_handler)
    76         return True
     76        global options
     77        options.conn, addr = sock.accept()
     78        lines = options.conn.recv(512).split('\n')
     79        ret = 0
     80        nlines = []
     81        for i in lines:
     82                if i.startswith("USERNAME") and i.split(' ')[1].strip() == options.config.username:
     83                        ret += 1
     84                elif i.startswith("PASSWORD") and sha.sha(i.split(' ')[1].strip()).hexdigest() == options.config.password:
     85                        ret += 1
     86                else:
     87                        nlines.append(i)
     88        del lines
     89        if ret == 2:
     90                dprint("Username and password OK.")
     91                options.buf = nlines
     92                input_handler(None, None)
     93                gobject.io_add_watch(options.conn, gobject.IO_IN, input_handler)
     94                return True
     95        else:
     96                dprint("Username and/or password WRONG.")
     97                return False
    7798
    7899def dprint(msg):
     
    89110
    90111        def recv(self, msg_text):
    91                 global conn
     112                global options
    92113                if msg_text == "PONG":
    93114                        return
     
    110131                        e = i.encode('UTF-8')
    111132                        dprint('<< ' + e)
    112                         if conn:
     133                        if options.conn:
    113134                                try:
    114                                         conn.send(e + "\n")
     135                                        options.conn.send(e + "\n")
    115136                                except IOError, s:
    116137                                        dprint("Warning, sending '%s' failed (%s)." % (e, s))
     
    132153class Options:
    133154        def __init__(self):
     155                self.cfgpath = "/etc/skyped.conf"
    134156                self.daemon = True
    135157                self.debug = False
     
    138160                self.port = 2727
    139161                self.version = False
     162                # well, this is a bit hackish. we store the socket of the last connected client
     163                # here and notify it. maybe later notify all connected clients?
     164                self.conn = None
     165                # this will be read first by the input handler
     166                self.buf = None
     167
    140168
    141169        def usage(self, ret):
     
    145173
    146174Options:
     175        -c      --config        path to configuration file (default: %s)
    147176        -d      --debug         enable debug messages
    148177        -h      --help          this help
     
    150179        -n      --nofork        don't run as daemon in the background
    151180        -p      --port          set the tcp port (default: %d)
    152         -v      --version       display version information""" % (self.host, self.port)
     181        -v      --version       display version information""" % (self.cfgpath, self.host, self.port)
    153182                sys.exit(ret)
    154183
     
    156185        options = Options()
    157186        try:
    158                 opts, args = getopt.getopt(sys.argv[1:], "dhH:np:v", ["daemon", "help", "host=", "nofork", "port=", "version"])
     187                opts, args = getopt.getopt(sys.argv[1:], "c:dhH:np:v", ["config=", "daemon", "help", "host=", "nofork", "port=", "version"])
    159188        except getopt.GetoptError:
    160189                options.usage(1)
    161190        for opt, arg in opts:
    162                 if opt in ("-d", "--debug"):
     191                if opt in ("-c", "--config"):
     192                        options.cfgpath = arg
     193                elif opt in ("-d", "--debug"):
    163194                        options.debug = True
    164195                elif opt in ("-h", "--help"):
     
    177208                print "skyped %s" % __version__
    178209                sys.exit(0)
    179         elif options.daemon:
     210        # parse our config
     211        if not os.path.exists(options.cfgpath):
     212                print "Can't find configuration file at '%s'." % options.cfgpath
     213                print "Use the -c option to specify an alternate one."
     214                sys.exit(1)
     215        options.config = ConfigParser()
     216        options.config.read(options.cfgpath)
     217        options.config.username = options.config.get('skyped', 'username').split('#')[0]
     218        options.config.password = options.config.get('skyped', 'password').split('#')[0]
     219        dprint("Parsing config file '%s' done, username is '%s'." % (options.cfgpath, options.config.username))
     220        if options.daemon:
    180221                pid = os.fork()
    181222                if pid == 0:
Note: See TracChangeset for help on using the changeset viewer.