Changeset 38fa988
- Timestamp:
- 2007-08-20T14:12:32Z (17 years ago)
- Branches:
- master
- Children:
- cb35add
- Parents:
- 4ddda13
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
skype/bitlbeed.py
r4ddda13 r38fa988 15 15 CLIENT_NAME = 'SkypeApiPythonShell' 16 16 17 USER_PROPS = ('FULLNAME', 'SEX', 'LANGUAGE', 'COUNTRY', 'CITY', 'ABOUT', 18 'ISAUTHORIZED', 'BUDDYSTATUS') 19 20 21 local_encoding = "latin2" 17 local_encoding = locale.getdefaultlocale()[1] 22 18 need_conv = (local_encoding != 'utf-8') 23 19 … … 38 34 return local_str 39 35 40 def ts():41 return time.strftime('[%H:%M:%S]')42 43 44 36 def sig_handler(signum, frame): 45 print ' ### caught signal %d, exiting' % signum37 print 'Caught signal %d, exiting.' % signum 46 38 mainloop.quit() 47 #sys.exit()48 49 50 def excepthook(type, value, traceback):51 mainloop.quit()52 return sys.__excepthook__(type, value, traceback)53 54 39 55 40 def input_handler(fd, io_condition): 56 #print '### fd=%d cond=%d' % (fd.fileno(), io_condition)57 41 input = fd.recv(1024) 58 #if len(input) == 0: # EOF59 # mainloop.quit()60 # return 061 42 for i in input.split("\n"): 62 do_command(i, fd) 43 if i: 44 fd.send(skype.send(i.strip()) + "\n") 63 45 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 command70 return 171 #print '###', argv72 73 cmd = argv[0]74 if cmd == 'q':75 mainloop.quit()76 return 077 elif commands.has_key(cmd):78 commands[cmd](argv)79 else:80 # send as-is81 print fd82 fd.send(skype.send(line) + "\n")83 return 184 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 reply110 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 reply127 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 reply152 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', user183 elif call_type.startswith('OUTGOING'):184 print ts(), '*** Outgoing', call_media, 'call to', user185 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', user189 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]196 46 197 47 … … 203 53 self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype') 204 54 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?") 207 56 208 57 reply = self.send('NAME ' + CLIENT_NAME) … … 211 60 212 61 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 222 62 dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') 223 63 … … 228 68 global conn 229 69 text = utf8_decode(msg_text) 230 print ts(), '<<<', text70 print '<<', text 231 71 if conn: 232 72 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)236 73 237 74 # client -> skype (sync, 5 sec timeout) 238 75 def send(self, msg_text): 76 if not len(msg_text): 77 return 239 78 print '>> ', msg_text 240 79 try: … … 246 85 247 86 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 255 87 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 256 88 skype = SkypeApi() 257 258 #print skype.send('GET SKYPEVERSION')259 #print skype.send('GET USERSTATUS')260 261 89 signal.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)266 90 267 91 mainloop = gobject.MainLoop() 268 sys.excepthook = excepthook269 92 270 93 def server(host, port):
Note: See TracChangeset
for help on using the changeset viewer.