Changeset e530abd
- Timestamp:
- 2010-12-20T20:51:49Z (14 years ago)
- Branches:
- master
- Children:
- 9c51166
- Parents:
- 5a54ec8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
skype/skyped.py
r5a54ec8 re530abd 42 42 if type != KeyboardInterrupt: 43 43 print_exception(type, value, tb) 44 options.conn.close()44 if options.conn: options.conn.close() 45 45 # shut down client if it's running 46 46 try: … … 52 52 sys.excepthook = eh 53 53 54 def input_handler(fd, io_condition): 55 global options 54 def input_handler(fd): 55 global options 56 global skype 56 57 if options.buf: 57 58 for i in options.buf: 58 59 skype.send(i.strip()) 59 60 options.buf = None 61 return True 60 62 else: 61 63 try: … … 64 66 dprint("Warning, receiving 1024 bytes failed (%s)." % s) 65 67 fd.close() 68 options.conn = False 66 69 return False 67 70 for i in input.split("\n"): … … 79 82 80 83 def send(sock, txt): 81 from time import sleep82 84 count = 1 83 85 done = False … … 89 91 count += 1 90 92 dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count)) 91 sleep(1)93 time.sleep(1) 92 94 if not done: 93 options.conn.close() 95 if options.conn: options.conn.close() 96 options.conn = False 97 return done 94 98 95 99 def bitlbee_idle_handler(skype): 96 100 global options 101 done = False 97 102 if options.conn: 98 103 try: 99 104 e = "PING" 100 send(options.conn, "%s\n" % e)105 done = send(options.conn, "%s\n" % e) 101 106 dprint("... pinged Bitlbee") 102 107 except Exception, s: 103 108 dprint("Warning, sending '%s' failed (%s)." % (e, s)) 104 options.conn.close() 105 return True 109 if options.conn: options.conn.close() 110 options.conn = False 111 done = False 112 return done 106 113 107 114 def server(host, port, skype): … … 139 146 dprint("Warning, receiving 1024 bytes failed (%s)." % s) 140 147 options.conn.close() 148 options.conn = False 141 149 return False 142 150 if ret == 2: … … 148 156 dprint("Username and/or password WRONG.") 149 157 options.conn.send("PASSWORD KO\n") 158 options.conn.close() 159 options.conn = False 150 160 return False 151 161 … … 191 201 # everybody will be happy 192 202 e = i.encode('UTF-8') 193 dprint('<< ' + e)194 203 if options.conn: 204 dprint('<< ' + e) 195 205 try: 196 206 # I called the send function really_send … … 198 208 except Exception, s: 199 209 dprint("Warning, sending '%s' failed (%s)." % (e, s)) 200 options.conn.close() 210 if options.conn: options.conn.close() 211 options.conn = False 212 else: 213 dprint('---' + e) 201 214 202 215 def send(self, msg_text): 203 216 if not len(msg_text) or msg_text == "PONG": 217 if msg_text == "PONG": options.last_bitlbee_pong = time.time() 204 218 return 205 219 try: … … 260 274 timeout = 1; # in seconds 261 275 skype_ping_period = 5 262 bitlbee_ping_period = 30 263 skype_ping_start_time = time.time() 264 bitlbee_ping_start_time = time.time() 265 while 1: 276 bitlbee_ping_period = 10 277 bitlbee_pong_timeout = 30 278 now = time.time() 279 skype_ping_start_time = now 280 bitlbee_ping_start_time = now 281 options.last_bitlbee_pong = now 282 in_error = [] 283 handler_ok = True 284 while (len(in_error) == 0) and handler_ok and options.conn: 266 285 ready_to_read, ready_to_write, in_error = \ 267 286 select.select([options.conn], [], [], timeout) 268 287 now = time.time() 288 handler_ok = True 269 289 if len(ready_to_read) == 1: 270 input_handler(ready_to_read.pop(), options)290 handler_ok = input_handler(ready_to_read.pop()) 271 291 # don't ping bitlbee/skype if they already received data 272 292 bitlbee_ping_start_time = now 273 293 skype_ping_start_time = now 274 if now - skype_ping_period > skype_ping_start_time:275 skype_idle_handler(skype)294 if (now - skype_ping_period > skype_ping_start_time) and handler_ok: 295 handler_ok = skype_idle_handler(skype) 276 296 skype_ping_start_time = now 277 297 if now - bitlbee_ping_period > bitlbee_ping_start_time: 278 bitlbee_idle_handler(skype)298 handler_ok = bitlbee_idle_handler(skype) 279 299 bitlbee_ping_start_time = now 300 if options.last_bitlbee_pong: 301 if (now - options.last_bitlbee_pong) > bitlbee_pong_timeout: 302 dprint("Bitlbee pong timeout") 303 # TODO is following line necessary? Should there be a options.conn.unwrap() somewhere? 304 # options.conn.shutdown() 305 if options.conn: options.conn.close() 306 options.conn = False 307 else: 308 dprint("%f seconds since last PONG" % (now - options.last_bitlbee_pong)) 309 else: 310 options.last_bitlbee_pong = now 311 dprint("Serverloop done") 280 312 281 313 if __name__=='__main__': … … 347 379 except Skype4Py.SkypeAPIError, s: 348 380 sys.exit("%s. Are you sure you have started Skype?" % s) 349 server(options.host, options.port, skype) 381 while 1: 382 options.conn = False 383 server(options.host, options.port, skype)
Note: See TracChangeset
for help on using the changeset viewer.