Changeset 38fa988 for skype


Ignore:
Timestamp:
2007-08-20T14:12:32Z (17 years ago)
Author:
VMiklos <vmiklos@…>
Branches:
master
Children:
cb35add
Parents:
4ddda13
Message:

bitlbeed: some cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • skype/bitlbeed.py

    r4ddda13 r38fa988  
    1515CLIENT_NAME = 'SkypeApiPythonShell'
    1616
    17 USER_PROPS = ('FULLNAME', 'SEX', 'LANGUAGE', 'COUNTRY', 'CITY', 'ABOUT',
    18              'ISAUTHORIZED', 'BUDDYSTATUS')
    19 
    20 
    21 local_encoding = "latin2"
     17local_encoding = locale.getdefaultlocale()[1]
    2218need_conv = (local_encoding != 'utf-8')
    2319
     
    3834       return local_str
    3935
    40 def ts():
    41    return time.strftime('[%H:%M:%S]')
    42 
    43 
    4436def sig_handler(signum, frame):
    45    print '### caught signal %d, exiting' % signum
     37   print 'Caught signal %d, exiting.' % signum
    4638   mainloop.quit()
    47    #sys.exit()
    48 
    49 
    50 def excepthook(type, value, traceback):
    51    mainloop.quit()
    52    return sys.__excepthook__(type, value, traceback)
    53 
    5439
    5540def input_handler(fd, io_condition):
    56    #print '### fd=%d cond=%d' % (fd.fileno(), io_condition)
    5741   input = fd.recv(1024)
    58    #if len(input) == 0:     # EOF
    59    #    mainloop.quit()
    60    #    return 0
    6142   for i in input.split("\n"):
    62        do_command(i, fd)
     43       if i:
     44           fd.send(skype.send(i.strip()) + "\n")
    6345   return True
    64 
    65 
    66 def do_command(input, fd):
    67    line = input.strip()
    68    argv = line.split(None, 1)
    69    if len(argv) == 0:      # empty command
    70        return 1
    71    #print '###', argv
    72 
    73    cmd = argv[0]
    74    if cmd == 'q':
    75        mainloop.quit()
    76        return 0
    77    elif commands.has_key(cmd):
    78        commands[cmd](argv)
    79    else:
    80        # send as-is
    81        print fd
    82        fd.send(skype.send(line) + "\n")
    83    return 1
    84 
    85 
    86 def cmd_help(argv):
    87    """help"""
    88    print 'q - quit'
    89    for cmd, handler in commands.items():
    90        if handler.__doc__:
    91            print cmd, '-', handler.__doc__
    92    print '<cmd> - send API <cmd> to skype'
    93 
    94 
    95 def cmd_list_users(argv):
    96    """list user status"""
    97    reply = skype.send('SEARCH FRIENDS')
    98    if reply.startswith('USERS '):
    99        user_list = reply[6:].split(', ')
    100        online = {}
    101        for user in user_list:
    102            reply = skype.send('GET USER %s ONLINESTATUS' % user)
    103            status = reply.split()[3]
    104            if status != 'SKYPEOUT':
    105                online[user] = status;
    106        for user, status in online.items():
    107            print '%-16s [%s]' % (user, status)
    108    else:
    109        print reply
    110 
    111 
    112 def cmd_who(argv):
    113    """list who's online"""
    114    reply = skype.send('SEARCH FRIENDS')
    115    if reply.startswith('USERS '):
    116        user_list = reply[6:].split(', ')
    117        who = {}
    118        for user in user_list:
    119            reply = skype.send('GET USER %s ONLINESTATUS' % user)
    120            status = reply.split()[3]
    121            if status != 'SKYPEOUT' and status != 'OFFLINE':
    122                who[user] = status;
    123        for user, status in who.items():
    124            print '%-16s [%s]' % (user, status)
    125    else:
    126        print reply
    127 
    128 
    129 def cmd_message(argv):
    130    """send message"""
    131    if len(argv) < 2 or argv[1].find(' ') == -1:
    132        print 'usage: m user text...'
    133    else:
    134        (user, text) = argv[1].split(None, 1)
    135        print skype.send(' '.join((skype.msg_cmd, user, text)))
    136 
    137 
    138 def cmd_userinfo(argv):
    139    """show user info"""
    140    if len(argv) == 1:
    141        print 'usage: i user'
    142    else:
    143        user = argv[1]
    144        for prop in USER_PROPS:
    145            reply = skype.send('GET USER %s %s' % (user, prop))
    146            if reply.startswith('USER '):
    147                res = reply.split(None, 3)
    148                if len(res) > 3:
    149                    print '%-13s: %s' % (prop.title(), res[3])
    150            else:
    151                print reply
    152 
    153 
    154 def cmd_test(argv):
    155    """test"""
    156    print skype.send("MESSAGE echo123 one two three")
    157 
    158 
    159 def cb_message(argv):
    160    args = argv[1].split(None, 3)
    161    msg_cmd = argv[0]
    162    msg_id = args[0]
    163    if args[1] == 'STATUS' and args[2] == 'READ':
    164        reply = skype.send('GET %s %s PARTNER_HANDLE' % (msg_cmd, msg_id))
    165        user = reply.split(None, 3)[3]
    166        reply = skype.send('GET %s %s BODY' % (msg_cmd, msg_id))
    167        res = reply.split(None, 3)
    168        print ts(), user, '>', res[3]
    169 
    170 
    171 def cb_call(argv):
    172    args = argv[1].split(None, 3)
    173    call_id = args[0]
    174    if args[1] == 'STATUS':
    175        if args[2] == 'RINGING':
    176            reply = skype.send('GET CALL %s PARTNER_HANDLE' % call_id)
    177            user = reply.split()[3]
    178            reply = skype.send('GET CALL %s TYPE' % call_id)
    179            call_type = reply.split()[3]
    180            call_media = call_type.split('_')[1]
    181            if call_type.startswith('INCOMING'):
    182                print ts(), '*** Incoming', call_media, 'call from', user
    183            elif call_type.startswith('OUTGOING'):
    184                print ts(), '*** Outgoing', call_media, 'call to', user
    185        elif args[2] == 'MISSED':
    186            reply = skype.send('GET CALL %s PARTNER_HANDLE' % call_id)
    187            user = reply.split()[3]
    188            print ts(), '*** missed call from', user
    189 
    190 
    191 def cb_user(argv):
    192    args = argv[1].split(None, 2)
    193    user = args[0]
    194    if args[1] == 'ONLINESTATUS' and args[2] != 'SKYPEOUT':
    195        print ts(), '***', user, 'is', args[2]
    19646
    19747
     
    20353           self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype')
    20454       except dbus.exceptions.DBusException:
    205            print "Can't find any Skype instance. Are you sure you have started Skype?"
    206            sys.exit(0)
     55           sys.exit("Can't find any Skype instance. Are you sure you have started Skype?")
    20756
    20857       reply = self.send('NAME ' + CLIENT_NAME)
     
    21160
    21261       reply = self.send('PROTOCOL 5')
    213        #if reply != 'PROTOCOL 5':
    214        #    sys.exit('This test program only supports Skype API protocol version 1')
    215        self.msg_cmd = 'MESSAGE'
    216 
    217        self.callbacks = {'MESSAGE' : cb_message,
    218                          'CHATMESSAGE' : cb_message,
    219                          'USER' : cb_user,
    220                          'CALL' : cb_call}
    221        
    22262       dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API')
    22363
     
    22868       global conn
    22969       text = utf8_decode(msg_text)
    230        print ts(), '<<<', text
     70       print '<<', text
    23171       if conn:
    23272           conn.send(msg_text + "\n")
    233        argv = text.split(None, 1)
    234        if self.callbacks.has_key(argv[0]):
    235            self.callbacks[argv[0]](argv)
    23673
    23774   # client -> skype (sync, 5 sec timeout)
    23875   def send(self, msg_text):
     76       if not len(msg_text):
     77           return
    23978       print '>> ', msg_text
    24079       try:
     
    24685
    24786
    248 commands = {'?' : cmd_help,
    249            'l' : cmd_list_users,
    250            'w' : cmd_who,
    251            'm' : cmd_message,
    252            'i' : cmd_userinfo,
    253            't' : cmd_test}
    254 
    25587dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    25688skype = SkypeApi()
    257 
    258 #print skype.send('GET SKYPEVERSION')
    259 #print skype.send('GET USERSTATUS')
    260 
    26189signal.signal(signal.SIGINT, sig_handler)
    262 #gobject.io_add_watch(sys.stdin,
    263 #                    gobject.IO_IN | gobject.IO_ERR | gobject.IO_HUP,
    264 #                    input_handler)
    265 #cmd_help(None)
    26690
    26791mainloop = gobject.MainLoop()
    268 sys.excepthook = excepthook
    26992
    27093def server(host, port):
Note: See TracChangeset for help on using the changeset viewer.