[47c590c] | 1 | #!/usr/bin/env python2.7 |
---|
[cd3022c] | 2 | # |
---|
| 3 | # skyped.py |
---|
| 4 | # |
---|
[d5a66f8] | 5 | # Copyright (c) 2007, 2008, 2009, 2010, 2011 by Miklos Vajna <vmiklos@frugalware.org> |
---|
[cd3022c] | 6 | # |
---|
| 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
---|
| 20 | # USA. |
---|
| 21 | # |
---|
| 22 | |
---|
[4ddda13] | 23 | import sys |
---|
[8237df5] | 24 | import os |
---|
[4ddda13] | 25 | import signal |
---|
| 26 | import locale |
---|
| 27 | import time |
---|
| 28 | import socket |
---|
[8237df5] | 29 | import getopt |
---|
[c15f71a] | 30 | import Skype4Py |
---|
[8edfc90] | 31 | import hashlib |
---|
[d891915] | 32 | from ConfigParser import ConfigParser, NoOptionError |
---|
[eeeb30e] | 33 | from traceback import print_exception |
---|
[c7000bb] | 34 | import ssl |
---|
[4ddda13] | 35 | |
---|
[8237df5] | 36 | __version__ = "0.1.1" |
---|
[4ddda13] | 37 | |
---|
[d5a66f8] | 38 | try: |
---|
| 39 | import gobject |
---|
| 40 | hasgobject = True |
---|
| 41 | except ImportError: |
---|
| 42 | import select |
---|
| 43 | import threading |
---|
| 44 | hasgobject = False |
---|
| 45 | |
---|
[eeeb30e] | 46 | def eh(type, value, tb): |
---|
[a618ea6] | 47 | global options |
---|
| 48 | |
---|
[3a2a0b2] | 49 | if type != KeyboardInterrupt: |
---|
| 50 | print_exception(type, value, tb) |
---|
[d5a66f8] | 51 | if hasgobject: |
---|
| 52 | gobject.MainLoop().quit() |
---|
[53eb75c] | 53 | if options.conn: |
---|
| 54 | options.conn.close() |
---|
[7415989] | 55 | # shut down client if it's running |
---|
| 56 | try: |
---|
| 57 | skype.skype.Client.Shutdown() |
---|
| 58 | except NameError: |
---|
| 59 | pass |
---|
[3a2a0b2] | 60 | sys.exit("Exiting.") |
---|
[eeeb30e] | 61 | |
---|
| 62 | sys.excepthook = eh |
---|
| 63 | |
---|
[d45adcf] | 64 | def wait_for_lock(lock, timeout_to_print, timeout, msg): |
---|
| 65 | start = time.time() |
---|
| 66 | locked = lock.acquire(0) |
---|
| 67 | while not(locked): |
---|
| 68 | time.sleep(0.5) |
---|
| 69 | if timeout_to_print and (time.time() - timeout_to_print > start): |
---|
| 70 | dprint("%s: Waited %f seconds" % \ |
---|
| 71 | (msg, time.time() - start)) |
---|
| 72 | timeout_to_print = False |
---|
| 73 | if timeout and (time.time() - timeout > start): |
---|
| 74 | dprint("%s: Waited %f seconds, giving up" % \ |
---|
| 75 | (msg, time.time() - start)) |
---|
| 76 | return False |
---|
| 77 | locked = lock.acquire(0) |
---|
| 78 | return True |
---|
| 79 | |
---|
[d5a66f8] | 80 | def input_handler(fd, io_condition = None): |
---|
[5245e9d] | 81 | global options |
---|
[e530abd] | 82 | global skype |
---|
[5245e9d] | 83 | if options.buf: |
---|
| 84 | for i in options.buf: |
---|
| 85 | skype.send(i.strip()) |
---|
| 86 | options.buf = None |
---|
[d5a66f8] | 87 | if not hasgobject: |
---|
| 88 | return True |
---|
[5245e9d] | 89 | else: |
---|
[d5a66f8] | 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 |
---|
[d45adcf] | 100 | options.lock.release() |
---|
[d5a66f8] | 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()) |
---|
| 115 | return True |
---|
[c15f71a] | 116 | |
---|
[4b0092e] | 117 | def skype_idle_handler(skype): |
---|
[3922d44] | 118 | try: |
---|
[6af541d] | 119 | c = skype.skype.Command("PING", Block=True) |
---|
| 120 | skype.skype.SendCommand(c) |
---|
[3922d44] | 121 | except Skype4Py.SkypeAPIError, s: |
---|
| 122 | dprint("Warning, pinging Skype failed (%s)." % (s)) |
---|
[94bd28f] | 123 | return True |
---|
[4ddda13] | 124 | |
---|
[a618ea6] | 125 | def send(sock, txt): |
---|
[1130561] | 126 | global options |
---|
[d5a66f8] | 127 | from time import sleep |
---|
[a618ea6] | 128 | count = 1 |
---|
| 129 | done = False |
---|
[d5a66f8] | 130 | if hasgobject: |
---|
| 131 | while (not done) and (count < 10): |
---|
[d45adcf] | 132 | try: |
---|
[d5a66f8] | 133 | sock.send(txt) |
---|
[d45adcf] | 134 | done = True |
---|
| 135 | except Exception, s: |
---|
| 136 | count += 1 |
---|
| 137 | dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count)) |
---|
[d5a66f8] | 138 | sleep(1) |
---|
| 139 | if not done: |
---|
[53eb75c] | 140 | options.conn.close() |
---|
[d5a66f8] | 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 |
---|
[a618ea6] | 158 | |
---|
[4b0092e] | 159 | def bitlbee_idle_handler(skype): |
---|
[eeab8bc] | 160 | global options |
---|
[e530abd] | 161 | done = False |
---|
[4b0092e] | 162 | if options.conn: |
---|
| 163 | try: |
---|
| 164 | e = "PING" |
---|
[e530abd] | 165 | done = send(options.conn, "%s\n" % e) |
---|
[4b0092e] | 166 | except Exception, s: |
---|
| 167 | dprint("Warning, sending '%s' failed (%s)." % (e, s)) |
---|
[d5a66f8] | 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 |
---|
[4b0092e] | 179 | |
---|
[d5a66f8] | 180 | def server(host, port, skype = None): |
---|
[c7304b2] | 181 | global options |
---|
[05d964c] | 182 | if ":" in host: |
---|
| 183 | sock = socket.socket(socket.AF_INET6) |
---|
| 184 | else: |
---|
| 185 | sock = socket.socket() |
---|
[a316c4e] | 186 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 187 | sock.bind((host, port)) |
---|
| 188 | sock.listen(1) |
---|
[d5a66f8] | 189 | if hasgobject: |
---|
| 190 | gobject.io_add_watch(sock, gobject.IO_IN, listener) |
---|
| 191 | else: |
---|
| 192 | dprint("Waiting for connection...") |
---|
| 193 | listener(sock, skype) |
---|
[a316c4e] | 194 | |
---|
[eeab8bc] | 195 | def listener(sock, skype): |
---|
[5245e9d] | 196 | global options |
---|
[d5a66f8] | 197 | if not hasgobject: |
---|
| 198 | if not(wait_for_lock(options.lock, 3, 10, "listener")): return False |
---|
[c7000bb] | 199 | rawsock, addr = sock.accept() |
---|
| 200 | options.conn = ssl.wrap_socket(rawsock, |
---|
| 201 | server_side=True, |
---|
| 202 | certfile=options.config.sslcert, |
---|
| 203 | keyfile=options.config.sslkey, |
---|
| 204 | ssl_version=ssl.PROTOCOL_TLSv1) |
---|
[b0d40f5] | 205 | if hasattr(options.conn, 'handshake'): |
---|
[5588f7c4] | 206 | try: |
---|
| 207 | options.conn.handshake() |
---|
| 208 | except Exception: |
---|
[d5a66f8] | 209 | if not hasgobject: |
---|
| 210 | options.lock.release() |
---|
[5588f7c4] | 211 | dprint("Warning, handshake failed, closing connection.") |
---|
| 212 | return False |
---|
[5245e9d] | 213 | ret = 0 |
---|
[6b9cab1] | 214 | try: |
---|
| 215 | line = options.conn.recv(1024) |
---|
| 216 | if line.startswith("USERNAME") and line.split(' ')[1].strip() == options.config.username: |
---|
| 217 | ret += 1 |
---|
| 218 | line = options.conn.recv(1024) |
---|
[8edfc90] | 219 | if line.startswith("PASSWORD") and hashlib.sha1(line.split(' ')[1].strip()).hexdigest() == options.config.password: |
---|
[6b9cab1] | 220 | ret += 1 |
---|
| 221 | except Exception, s: |
---|
| 222 | dprint("Warning, receiving 1024 bytes failed (%s)." % s) |
---|
| 223 | options.conn.close() |
---|
[d5a66f8] | 224 | if not hasgobject: |
---|
| 225 | options.conn = False |
---|
| 226 | options.lock.release() |
---|
[6b9cab1] | 227 | return False |
---|
[5245e9d] | 228 | if ret == 2: |
---|
| 229 | dprint("Username and password OK.") |
---|
[c7304b2] | 230 | options.conn.send("PASSWORD OK\n") |
---|
[d5a66f8] | 231 | if hasgobject: |
---|
| 232 | gobject.io_add_watch(options.conn, gobject.IO_IN, input_handler) |
---|
| 233 | else: |
---|
| 234 | options.lock.release() |
---|
| 235 | serverloop(options, skype) |
---|
[5245e9d] | 236 | return True |
---|
| 237 | else: |
---|
| 238 | dprint("Username and/or password WRONG.") |
---|
[c7304b2] | 239 | options.conn.send("PASSWORD KO\n") |
---|
[d5a66f8] | 240 | if not hasgobject: |
---|
| 241 | options.conn.close() |
---|
| 242 | options.conn = False |
---|
| 243 | options.lock.release() |
---|
[5245e9d] | 244 | return False |
---|
[a316c4e] | 245 | |
---|
| 246 | def dprint(msg): |
---|
[ffd078a] | 247 | from time import strftime |
---|
[8237df5] | 248 | global options |
---|
| 249 | |
---|
[ffd078a] | 250 | now = strftime("%Y-%m-%d %H:%M:%S") |
---|
| 251 | |
---|
[8237df5] | 252 | if options.debug: |
---|
[f503585] | 253 | try: |
---|
| 254 | print now + ": " + msg |
---|
| 255 | except Exception, s: |
---|
| 256 | try: |
---|
| 257 | sanitized = msg.encode("ascii", "backslashreplace") |
---|
| 258 | except Error, s: |
---|
| 259 | try: |
---|
| 260 | sanitized = "hex [" + msg.encode("hex") + "]" |
---|
| 261 | except Error, s: |
---|
| 262 | sanitized = "[unable to print debug message]" |
---|
| 263 | print now + "~=" + sanitized |
---|
[a618ea6] | 264 | sys.stdout.flush() |
---|
[bcdc24b] | 265 | if options.log: |
---|
| 266 | sock = open(options.log, "a") |
---|
[ea1d796] | 267 | sock.write("%s: %s\n" % (now, msg)) |
---|
[bcdc24b] | 268 | sock.close() |
---|
[a316c4e] | 269 | |
---|
[944a941] | 270 | class SkypeApi: |
---|
[94bd28f] | 271 | def __init__(self): |
---|
[c15f71a] | 272 | self.skype = Skype4Py.Skype() |
---|
[5268bd7] | 273 | self.skype.OnNotify = self.recv |
---|
[6af541d] | 274 | self.skype.Client.Start() |
---|
[94bd28f] | 275 | |
---|
[5268bd7] | 276 | def recv(self, msg_text): |
---|
[5245e9d] | 277 | global options |
---|
[d86dfb1] | 278 | if msg_text == "PONG": |
---|
| 279 | return |
---|
[c15f71a] | 280 | if "\n" in msg_text: |
---|
[7613670] | 281 | # crappy skype prefixes only the first line for |
---|
| 282 | # multiline messages so we need to do so for the other |
---|
| 283 | # lines, too. this is something like: |
---|
| 284 | # 'CHATMESSAGE id BODY first line\nsecond line' -> |
---|
| 285 | # 'CHATMESSAGE id BODY first line\nCHATMESSAGE id BODY second line' |
---|
[c15f71a] | 286 | prefix = " ".join(msg_text.split(" ")[:3]) |
---|
| 287 | msg_text = ["%s %s" % (prefix, i) for i in " ".join(msg_text.split(" ")[3:]).split("\n")] |
---|
[7613670] | 288 | else: |
---|
[c15f71a] | 289 | msg_text = [msg_text] |
---|
| 290 | for i in msg_text: |
---|
[3423be0] | 291 | try: |
---|
| 292 | # Internally, BitlBee always uses UTF-8 and encodes/decodes as |
---|
| 293 | # necessary to communicate with the IRC client; thus send the |
---|
| 294 | # UTF-8 it expects |
---|
| 295 | e = i.encode('UTF-8') |
---|
| 296 | except: |
---|
| 297 | # Should never happen, but it's better to send difficult to |
---|
| 298 | # read data than crash because some message couldn't be encoded |
---|
| 299 | e = i.encode('ascii', 'backslashreplace') |
---|
[5245e9d] | 300 | if options.conn: |
---|
[e530abd] | 301 | dprint('<< ' + e) |
---|
[af8675f] | 302 | try: |
---|
[a618ea6] | 303 | send(options.conn, e + "\n") |
---|
[80dfdce] | 304 | except Exception, s: |
---|
[a75f2a7] | 305 | dprint("Warning, sending '%s' failed (%s)." % (e, s)) |
---|
[e530abd] | 306 | if options.conn: options.conn.close() |
---|
| 307 | options.conn = False |
---|
| 308 | else: |
---|
[53eb75c] | 309 | dprint('-- ' + e) |
---|
[c15f71a] | 310 | |
---|
| 311 | def send(self, msg_text): |
---|
[4b0092e] | 312 | if not len(msg_text) or msg_text == "PONG": |
---|
[53eb75c] | 313 | if msg_text == "PONG": |
---|
| 314 | options.last_bitlbee_pong = time.time() |
---|
[c15f71a] | 315 | return |
---|
[885e563e] | 316 | try: |
---|
[3423be0] | 317 | # Internally, BitlBee always uses UTF-8 and encodes/decodes as |
---|
| 318 | # necessary to communicate with the IRC client; thus decode the |
---|
| 319 | # UTF-8 it sent us |
---|
[885e563e] | 320 | e = msg_text.decode('UTF-8') |
---|
[3423be0] | 321 | except: |
---|
| 322 | # Should never happen, but it's better to send difficult to read |
---|
| 323 | # data to Skype than to crash |
---|
| 324 | e = msg_text.decode('ascii', 'backslashreplace') |
---|
[52d779e] | 325 | dprint('>> ' + e) |
---|
[c15f71a] | 326 | try: |
---|
[05cf927] | 327 | c = self.skype.Command(e, Block=True) |
---|
| 328 | self.skype.SendCommand(c) |
---|
| 329 | self.recv(c.Reply) |
---|
| 330 | except Skype4Py.SkypeError: |
---|
[c15f71a] | 331 | pass |
---|
[8b3beef] | 332 | except Skype4Py.SkypeAPIError, s: |
---|
[a75f2a7] | 333 | dprint("Warning, sending '%s' failed (%s)." % (e, s)) |
---|
[4ddda13] | 334 | |
---|
[8237df5] | 335 | class Options: |
---|
| 336 | def __init__(self): |
---|
[1b48afb] | 337 | self.cfgpath = os.path.join(os.environ['HOME'], ".skyped", "skyped.conf") |
---|
[2ff0f37] | 338 | # fall back to system-wide settings |
---|
[1a575f69] | 339 | self.syscfgpath = "/usr/local/etc/skyped/skyped.conf" |
---|
[2ff0f37] | 340 | if os.path.exists(self.syscfgpath) and not os.path.exists(self.cfgpath): |
---|
[1a575f69] | 341 | self.cfgpath = self.syscfgpath |
---|
[8237df5] | 342 | self.daemon = True |
---|
| 343 | self.debug = False |
---|
| 344 | self.help = False |
---|
[7e450c3] | 345 | self.host = "0.0.0.0" |
---|
[bcdc24b] | 346 | self.log = None |
---|
[d891915] | 347 | self.port = None |
---|
[8237df5] | 348 | self.version = False |
---|
[5245e9d] | 349 | # well, this is a bit hackish. we store the socket of the last connected client |
---|
| 350 | # here and notify it. maybe later notify all connected clients? |
---|
| 351 | self.conn = None |
---|
| 352 | # this will be read first by the input handler |
---|
| 353 | self.buf = None |
---|
| 354 | |
---|
[8237df5] | 355 | |
---|
| 356 | def usage(self, ret): |
---|
| 357 | print """Usage: skyped [OPTION]... |
---|
| 358 | |
---|
| 359 | skyped is a daemon that acts as a tcp server on top of a Skype instance. |
---|
| 360 | |
---|
| 361 | Options: |
---|
[5245e9d] | 362 | -c --config path to configuration file (default: %s) |
---|
[8237df5] | 363 | -d --debug enable debug messages |
---|
| 364 | -h --help this help |
---|
[05d964c] | 365 | -H --host set the tcp host, supports IPv4 and IPv6 (default: %s) |
---|
[bcdc24b] | 366 | -l --log set the log file in background mode (default: none) |
---|
[8237df5] | 367 | -n --nofork don't run as daemon in the background |
---|
[a349932] | 368 | -p --port set the tcp port (default: %s) |
---|
[5245e9d] | 369 | -v --version display version information""" % (self.cfgpath, self.host, self.port) |
---|
[8237df5] | 370 | sys.exit(ret) |
---|
| 371 | |
---|
[eeab8bc] | 372 | def serverloop(options, skype): |
---|
| 373 | timeout = 1; # in seconds |
---|
| 374 | skype_ping_period = 5 |
---|
[e530abd] | 375 | bitlbee_ping_period = 10 |
---|
| 376 | bitlbee_pong_timeout = 30 |
---|
| 377 | now = time.time() |
---|
| 378 | skype_ping_start_time = now |
---|
| 379 | bitlbee_ping_start_time = now |
---|
| 380 | options.last_bitlbee_pong = now |
---|
| 381 | in_error = [] |
---|
| 382 | handler_ok = True |
---|
| 383 | while (len(in_error) == 0) and handler_ok and options.conn: |
---|
[eeab8bc] | 384 | ready_to_read, ready_to_write, in_error = \ |
---|
[9c51166] | 385 | select.select([options.conn], [], [options.conn], \ |
---|
| 386 | timeout) |
---|
[eeab8bc] | 387 | now = time.time() |
---|
[9c51166] | 388 | handler_ok = len(in_error) == 0 |
---|
| 389 | if (len(ready_to_read) == 1) and handler_ok: |
---|
[e530abd] | 390 | handler_ok = input_handler(ready_to_read.pop()) |
---|
[eeab8bc] | 391 | # don't ping bitlbee/skype if they already received data |
---|
[9c51166] | 392 | now = time.time() # allow for the input_handler to take some time |
---|
[eeab8bc] | 393 | bitlbee_ping_start_time = now |
---|
| 394 | skype_ping_start_time = now |
---|
[9c51166] | 395 | options.last_bitlbee_pong = now |
---|
[e530abd] | 396 | if (now - skype_ping_period > skype_ping_start_time) and handler_ok: |
---|
| 397 | handler_ok = skype_idle_handler(skype) |
---|
[eeab8bc] | 398 | skype_ping_start_time = now |
---|
| 399 | if now - bitlbee_ping_period > bitlbee_ping_start_time: |
---|
[e530abd] | 400 | handler_ok = bitlbee_idle_handler(skype) |
---|
[eeab8bc] | 401 | bitlbee_ping_start_time = now |
---|
[e530abd] | 402 | if options.last_bitlbee_pong: |
---|
| 403 | if (now - options.last_bitlbee_pong) > bitlbee_pong_timeout: |
---|
| 404 | dprint("Bitlbee pong timeout") |
---|
| 405 | # TODO is following line necessary? Should there be a options.conn.unwrap() somewhere? |
---|
| 406 | # options.conn.shutdown() |
---|
[53eb75c] | 407 | if options.conn: |
---|
| 408 | options.conn.close() |
---|
[e530abd] | 409 | options.conn = False |
---|
| 410 | else: |
---|
| 411 | options.last_bitlbee_pong = now |
---|
[eeab8bc] | 412 | |
---|
[4ddda13] | 413 | if __name__=='__main__': |
---|
[8237df5] | 414 | options = Options() |
---|
| 415 | try: |
---|
[a985369] | 416 | opts, args = getopt.getopt(sys.argv[1:], "c:dhH:l:np:v", ["config=", "debug", "help", "host=", "log=", "nofork", "port=", "version"]) |
---|
[8237df5] | 417 | except getopt.GetoptError: |
---|
| 418 | options.usage(1) |
---|
| 419 | for opt, arg in opts: |
---|
[5245e9d] | 420 | if opt in ("-c", "--config"): |
---|
| 421 | options.cfgpath = arg |
---|
| 422 | elif opt in ("-d", "--debug"): |
---|
[8237df5] | 423 | options.debug = True |
---|
| 424 | elif opt in ("-h", "--help"): |
---|
| 425 | options.help = True |
---|
[7e450c3] | 426 | elif opt in ("-H", "--host"): |
---|
| 427 | options.host = arg |
---|
[bcdc24b] | 428 | elif opt in ("-l", "--log"): |
---|
| 429 | options.log = arg |
---|
[8237df5] | 430 | elif opt in ("-n", "--nofork"): |
---|
| 431 | options.daemon = False |
---|
| 432 | elif opt in ("-p", "--port"): |
---|
[d891915] | 433 | options.port = int(arg) |
---|
[8237df5] | 434 | elif opt in ("-v", "--version"): |
---|
| 435 | options.version = True |
---|
| 436 | if options.help: |
---|
| 437 | options.usage(0) |
---|
| 438 | elif options.version: |
---|
| 439 | print "skyped %s" % __version__ |
---|
| 440 | sys.exit(0) |
---|
[5245e9d] | 441 | # parse our config |
---|
| 442 | if not os.path.exists(options.cfgpath): |
---|
| 443 | print "Can't find configuration file at '%s'." % options.cfgpath |
---|
| 444 | print "Use the -c option to specify an alternate one." |
---|
| 445 | sys.exit(1) |
---|
| 446 | options.config = ConfigParser() |
---|
| 447 | options.config.read(options.cfgpath) |
---|
| 448 | options.config.username = options.config.get('skyped', 'username').split('#')[0] |
---|
| 449 | options.config.password = options.config.get('skyped', 'password').split('#')[0] |
---|
[7cc2c1e] | 450 | options.config.sslkey = os.path.expanduser(options.config.get('skyped', 'key').split('#')[0]) |
---|
| 451 | options.config.sslcert = os.path.expanduser(options.config.get('skyped', 'cert').split('#')[0]) |
---|
[d891915] | 452 | # hack: we have to parse the parameters first to locate the |
---|
| 453 | # config file but the -p option should overwrite the value from |
---|
| 454 | # the config file |
---|
| 455 | try: |
---|
| 456 | options.config.port = int(options.config.get('skyped', 'port').split('#')[0]) |
---|
| 457 | if not options.port: |
---|
| 458 | options.port = options.config.port |
---|
| 459 | except NoOptionError: |
---|
| 460 | pass |
---|
| 461 | if not options.port: |
---|
| 462 | options.port = 2727 |
---|
[5245e9d] | 463 | dprint("Parsing config file '%s' done, username is '%s'." % (options.cfgpath, options.config.username)) |
---|
| 464 | if options.daemon: |
---|
[8237df5] | 465 | pid = os.fork() |
---|
| 466 | if pid == 0: |
---|
[56ae398] | 467 | nullin = file(os.devnull, 'r') |
---|
| 468 | nullout = file(os.devnull, 'w') |
---|
[8237df5] | 469 | os.dup2(nullin.fileno(), sys.stdin.fileno()) |
---|
| 470 | os.dup2(nullout.fileno(), sys.stdout.fileno()) |
---|
| 471 | os.dup2(nullout.fileno(), sys.stderr.fileno()) |
---|
| 472 | else: |
---|
| 473 | print 'skyped is started on port %s, pid: %d' % (options.port, pid) |
---|
| 474 | sys.exit(0) |
---|
[d891915] | 475 | else: |
---|
| 476 | dprint('skyped is started on port %s' % options.port) |
---|
[d5a66f8] | 477 | if hasgobject: |
---|
| 478 | server(options.host, options.port) |
---|
[c15f71a] | 479 | try: |
---|
[3953172] | 480 | skype = SkypeApi() |
---|
[8b3beef] | 481 | except Skype4Py.SkypeAPIError, s: |
---|
[c15f71a] | 482 | sys.exit("%s. Are you sure you have started Skype?" % s) |
---|
[d5a66f8] | 483 | if hasgobject: |
---|
| 484 | gobject.timeout_add(2000, skype_idle_handler, skype) |
---|
| 485 | gobject.timeout_add(60000, bitlbee_idle_handler, skype) |
---|
| 486 | gobject.MainLoop().run() |
---|
| 487 | else: |
---|
| 488 | while 1: |
---|
| 489 | options.conn = False |
---|
| 490 | options.lock = threading.Lock() |
---|
| 491 | server(options.host, options.port, skype) |
---|