Changeset d5a66f8
- Timestamp:
- 2011-04-11T23:26:09Z (14 years ago)
- Branches:
- master
- Children:
- 9c97a03
- Parents:
- 3423be0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
skype/skyped.py
r3423be0 rd5a66f8 3 3 # skyped.py 4 4 # 5 # Copyright (c) 2007, 2008, 2009, 2010 by Miklos Vajna <vmiklos@frugalware.org>5 # Copyright (c) 2007, 2008, 2009, 2010, 2011 by Miklos Vajna <vmiklos@frugalware.org> 6 6 # 7 7 # This program is free software; you can redistribute it and/or modify … … 33 33 from traceback import print_exception 34 34 import ssl 35 import select36 import threading37 35 38 36 __version__ = "0.1.1" 37 38 try: 39 import gobject 40 hasgobject = True 41 except ImportError: 42 import select 43 import threading 44 hasgobject = False 39 45 40 46 def eh(type, value, tb): … … 43 49 if type != KeyboardInterrupt: 44 50 print_exception(type, value, tb) 51 if hasgobject: 52 gobject.MainLoop().quit() 45 53 if options.conn: 46 54 options.conn.close() … … 70 78 return True 71 79 72 def input_handler(fd ):80 def input_handler(fd, io_condition = None): 73 81 global options 74 82 global skype … … 77 85 skype.send(i.strip()) 78 86 options.buf = None 87 if not hasgobject: 88 return True 89 else: 90 if not hasgobject: 91 close_socket = False 92 if wait_for_lock(options.lock, 3, 10, "input_handler"): 93 try: 94 input = fd.recv(1024) 95 options.lock.release() 96 except Exception, s: 97 dprint("Warning, receiving 1024 bytes failed (%s)." % s) 98 fd.close() 99 options.conn = False 100 options.lock.release() 101 return False 102 for i in input.split("\n"): 103 if i.strip() == "SET USERSTATUS OFFLINE": 104 close_socket = True 105 skype.send(i.strip()) 106 return not(close_socket) 107 try: 108 input = fd.recv(1024) 109 except Exception, s: 110 dprint("Warning, receiving 1024 bytes failed (%s)." % s) 111 fd.close() 112 return False 113 for i in input.split("\n"): 114 skype.send(i.strip()) 79 115 return True 80 else:81 close_socket = False82 if wait_for_lock(options.lock, 3, 10, "input_handler"):83 try:84 input = fd.recv(1024)85 options.lock.release()86 except Exception, s:87 dprint("Warning, receiving 1024 bytes failed (%s)." % s)88 fd.close()89 options.conn = False90 options.lock.release()91 return False92 for i in input.split("\n"):93 if i.strip() == "SET USERSTATUS OFFLINE":94 close_socket = True95 skype.send(i.strip())96 return not(close_socket)97 116 98 117 def skype_idle_handler(skype): … … 106 125 def send(sock, txt): 107 126 global options 127 from time import sleep 108 128 count = 1 109 129 done = False 110 while (not done) and (count < 10) and options.conn:111 if wait_for_lock(options.lock, 3, 10, "socket send"):130 if hasgobject: 131 while (not done) and (count < 10): 112 132 try: 113 if options.conn: sock.send(txt) 114 options.lock.release() 133 sock.send(txt) 115 134 done = True 116 135 except Exception, s: 117 options.lock.release()118 136 count += 1 119 137 dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count)) 120 time.sleep(1) 121 if not done: 122 if options.conn: 138 sleep(1) 139 if not done: 123 140 options.conn.close() 124 options.conn = False 125 return done 141 else: 142 while (not done) and (count < 10) and options.conn: 143 if wait_for_lock(options.lock, 3, 10, "socket send"): 144 try: 145 if options.conn: sock.send(txt) 146 options.lock.release() 147 done = True 148 except Exception, s: 149 options.lock.release() 150 count += 1 151 dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count)) 152 sleep(1) 153 if not done: 154 if options.conn: 155 options.conn.close() 156 options.conn = False 157 return done 126 158 127 159 def bitlbee_idle_handler(skype): … … 134 166 except Exception, s: 135 167 dprint("Warning, sending '%s' failed (%s)." % (e, s)) 136 if options.conn: options.conn.close() 137 options.conn = False 138 done = False 139 return done 140 141 def server(host, port, skype): 168 if hasgobject: 169 options.conn.close() 170 else: 171 if options.conn: options.conn.close() 172 options.conn = False 173 done = False 174 if hasgobject: 175 return True 176 else: 177 return done 178 return True 179 180 def server(host, port, skype = None): 142 181 global options 143 182 sock = socket.socket() … … 145 184 sock.bind((host, port)) 146 185 sock.listen(1) 147 dprint("Waiting for connection...") 148 listener(sock, skype) 186 if hasgobject: 187 gobject.io_add_watch(sock, gobject.IO_IN, listener) 188 else: 189 dprint("Waiting for connection...") 190 listener(sock, skype) 149 191 150 192 def listener(sock, skype): 151 193 global options 152 if not(wait_for_lock(options.lock, 3, 10, "listener")): return False 194 if not hasgobject: 195 if not(wait_for_lock(options.lock, 3, 10, "listener")): return False 153 196 rawsock, addr = sock.accept() 154 197 options.conn = ssl.wrap_socket(rawsock, … … 161 204 options.conn.handshake() 162 205 except Exception: 163 options.lock.release() 206 if not hasgobject: 207 options.lock.release() 164 208 dprint("Warning, handshake failed, closing connection.") 165 209 return False … … 175 219 dprint("Warning, receiving 1024 bytes failed (%s)." % s) 176 220 options.conn.close() 177 options.conn = False 178 options.lock.release() 221 if not hasgobject: 222 options.conn = False 223 options.lock.release() 179 224 return False 180 225 if ret == 2: 181 226 dprint("Username and password OK.") 182 227 options.conn.send("PASSWORD OK\n") 183 options.lock.release() 184 serverloop(options, skype) 228 if hasgobject: 229 gobject.io_add_watch(options.conn, gobject.IO_IN, input_handler) 230 else: 231 options.lock.release() 232 serverloop(options, skype) 185 233 return True 186 234 else: 187 235 dprint("Username and/or password WRONG.") 188 236 options.conn.send("PASSWORD KO\n") 189 options.conn.close() 190 options.conn = False 191 options.lock.release() 237 if not hasgobject: 238 options.conn.close() 239 options.conn = False 240 options.lock.release() 192 241 return False 193 242 … … 423 472 else: 424 473 dprint('skyped is started on port %s' % options.port) 474 if hasgobject: 475 server(options.host, options.port) 425 476 try: 426 477 skype = SkypeApi() 427 478 except Skype4Py.SkypeAPIError, s: 428 479 sys.exit("%s. Are you sure you have started Skype?" % s) 429 while 1: 430 options.conn = False 431 options.lock = threading.Lock() 432 server(options.host, options.port, skype) 480 if hasgobject: 481 gobject.timeout_add(2000, skype_idle_handler, skype) 482 gobject.timeout_add(60000, bitlbee_idle_handler, skype) 483 gobject.MainLoop().run() 484 else: 485 while 1: 486 options.conn = False 487 options.lock = threading.Lock() 488 server(options.host, options.port, skype)
Note: See TracChangeset
for help on using the changeset viewer.