Changeset 5245e9d for skype/skyped.py
- Timestamp:
- 2008-01-12T01:36:54Z (17 years ago)
- Branches:
- master
- Children:
- a3262d1e
- Parents:
- a0b206b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
skype/skyped.py
ra0b206b r5245e9d 35 35 import Skype4Py 36 36 import threading 37 import sha 38 from ConfigParser import ConfigParser 37 39 38 40 __version__ = "0.1.1" … … 41 43 CLIENT_NAME = 'SkypeApiPythonShell' 42 44 43 # well, this is a bit hackish. we store the socket of the last connected client44 # here and notify it. maybe later notify all connected clients?45 conn = None46 47 45 def 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 52 56 53 57 def idle_handler(skype): … … 70 74 71 75 def 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 77 98 78 99 def dprint(msg): … … 89 110 90 111 def recv(self, msg_text): 91 global conn112 global options 92 113 if msg_text == "PONG": 93 114 return … … 110 131 e = i.encode('UTF-8') 111 132 dprint('<< ' + e) 112 if conn:133 if options.conn: 113 134 try: 114 conn.send(e + "\n")135 options.conn.send(e + "\n") 115 136 except IOError, s: 116 137 dprint("Warning, sending '%s' failed (%s)." % (e, s)) … … 132 153 class Options: 133 154 def __init__(self): 155 self.cfgpath = "/etc/skyped.conf" 134 156 self.daemon = True 135 157 self.debug = False … … 138 160 self.port = 2727 139 161 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 140 168 141 169 def usage(self, ret): … … 145 173 146 174 Options: 175 -c --config path to configuration file (default: %s) 147 176 -d --debug enable debug messages 148 177 -h --help this help … … 150 179 -n --nofork don't run as daemon in the background 151 180 -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) 153 182 sys.exit(ret) 154 183 … … 156 185 options = Options() 157 186 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"]) 159 188 except getopt.GetoptError: 160 189 options.usage(1) 161 190 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"): 163 194 options.debug = True 164 195 elif opt in ("-h", "--help"): … … 177 208 print "skyped %s" % __version__ 178 209 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: 180 221 pid = os.fork() 181 222 if pid == 0:
Note: See TracChangeset
for help on using the changeset viewer.