[4ddda13] | 1 | #!/usr/bin/env python |
---|
[cd3022c] | 2 | # |
---|
| 3 | # skyped.py |
---|
| 4 | # |
---|
| 5 | # Copyright (c) 2007 by Miklos Vajna <vmiklos@frugalware.org> |
---|
| 6 | # |
---|
| 7 | # It uses several code from a very basic python CLI interface, available at: |
---|
| 8 | # |
---|
| 9 | # http://forum.skype.com/index.php?showtopic=42640 |
---|
| 10 | # |
---|
| 11 | # This program is free software; you can redistribute it and/or modify |
---|
| 12 | # it under the terms of the GNU General Public License as published by |
---|
| 13 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 14 | # (at your option) any later version. |
---|
| 15 | # |
---|
| 16 | # This program is distributed in the hope that it will be useful, |
---|
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 19 | # GNU General Public License for more details. |
---|
| 20 | # |
---|
| 21 | # You should have received a copy of the GNU General Public License |
---|
| 22 | # along with this program; if not, write to the Free Software |
---|
| 23 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
---|
| 24 | # USA. |
---|
| 25 | # |
---|
| 26 | |
---|
[4ddda13] | 27 | import sys |
---|
[8237df5] | 28 | import os |
---|
[4ddda13] | 29 | import signal |
---|
| 30 | import locale |
---|
| 31 | import time |
---|
| 32 | import gobject |
---|
| 33 | import socket |
---|
[8237df5] | 34 | import getopt |
---|
[c15f71a] | 35 | import Skype4Py |
---|
| 36 | import threading |
---|
[4ddda13] | 37 | |
---|
[8237df5] | 38 | __version__ = "0.1.1" |
---|
[4ddda13] | 39 | |
---|
| 40 | SKYPE_SERVICE = 'com.Skype.API' |
---|
| 41 | CLIENT_NAME = 'SkypeApiPythonShell' |
---|
| 42 | |
---|
| 43 | # well, this is a bit hackish. we store the socket of the last connected client |
---|
| 44 | # here and notify it. maybe later notify all connected clients? |
---|
| 45 | conn = None |
---|
| 46 | |
---|
| 47 | def input_handler(fd, io_condition): |
---|
[94bd28f] | 48 | input = fd.recv(1024) |
---|
| 49 | for i in input.split("\n"): |
---|
[c15f71a] | 50 | skype.send(i.strip()) |
---|
| 51 | return True |
---|
| 52 | |
---|
| 53 | def idle_handler(skype): |
---|
[3922d44] | 54 | try: |
---|
| 55 | skype.skype.SendCommand(skype.skype.Command(-1, "PING")) |
---|
| 56 | except Skype4Py.SkypeAPIError, s: |
---|
| 57 | dprint("Warning, pinging Skype failed (%s)." % (s)) |
---|
[c15f71a] | 58 | try: |
---|
[d86dfb1] | 59 | time.sleep(2) |
---|
[c15f71a] | 60 | except KeyboardInterrupt: |
---|
| 61 | sys.exit("Exiting.") |
---|
[94bd28f] | 62 | return True |
---|
[4ddda13] | 63 | |
---|
[a316c4e] | 64 | def server(host, port): |
---|
| 65 | sock = socket.socket() |
---|
| 66 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
| 67 | sock.bind((host, port)) |
---|
| 68 | sock.listen(1) |
---|
| 69 | gobject.io_add_watch(sock, gobject.IO_IN, listener) |
---|
| 70 | |
---|
| 71 | 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 |
---|
| 77 | |
---|
| 78 | def dprint(msg): |
---|
[8237df5] | 79 | global options |
---|
| 80 | |
---|
| 81 | if options.debug: |
---|
[a316c4e] | 82 | print msg |
---|
| 83 | |
---|
[c15f71a] | 84 | class SkypeApi(): |
---|
[94bd28f] | 85 | def __init__(self): |
---|
[c15f71a] | 86 | self.skype = Skype4Py.Skype() |
---|
[5268bd7] | 87 | self.skype.OnNotify = self.recv |
---|
[c15f71a] | 88 | self.skype.Attach() |
---|
[94bd28f] | 89 | |
---|
[5268bd7] | 90 | def recv(self, msg_text): |
---|
[94bd28f] | 91 | global conn |
---|
[d86dfb1] | 92 | if msg_text == "PONG": |
---|
| 93 | return |
---|
[c15f71a] | 94 | if "\n" in msg_text: |
---|
[7613670] | 95 | # crappy skype prefixes only the first line for |
---|
| 96 | # multiline messages so we need to do so for the other |
---|
| 97 | # lines, too. this is something like: |
---|
| 98 | # 'CHATMESSAGE id BODY first line\nsecond line' -> |
---|
| 99 | # 'CHATMESSAGE id BODY first line\nCHATMESSAGE id BODY second line' |
---|
[c15f71a] | 100 | prefix = " ".join(msg_text.split(" ")[:3]) |
---|
| 101 | msg_text = ["%s %s" % (prefix, i) for i in " ".join(msg_text.split(" ")[3:]).split("\n")] |
---|
[7613670] | 102 | else: |
---|
[c15f71a] | 103 | msg_text = [msg_text] |
---|
| 104 | for i in msg_text: |
---|
[a75f2a7] | 105 | # use utf-8 here to solve the following problem: |
---|
| 106 | # people use env vars like LC_ALL=en_US (latin1) then |
---|
| 107 | # they complain about why can't they receive latin2 |
---|
| 108 | # messages.. so here it is: always use utf-8 then |
---|
| 109 | # everybody will be happy |
---|
| 110 | e = i.encode('UTF-8') |
---|
[52d779e] | 111 | dprint('<< ' + e) |
---|
[c15f71a] | 112 | if conn: |
---|
[af8675f] | 113 | try: |
---|
| 114 | conn.send(e + "\n") |
---|
| 115 | except IOError, s: |
---|
[a75f2a7] | 116 | dprint("Warning, sending '%s' failed (%s)." % (e, s)) |
---|
[c15f71a] | 117 | |
---|
| 118 | def send(self, msg_text): |
---|
| 119 | if not len(msg_text): |
---|
| 120 | return |
---|
[52d779e] | 121 | e = msg_text.decode(locale.getdefaultlocale()[1]) |
---|
| 122 | dprint('>> ' + e) |
---|
[c15f71a] | 123 | try: |
---|
[05cf927] | 124 | c = self.skype.Command(e, Block=True) |
---|
| 125 | self.skype.SendCommand(c) |
---|
| 126 | self.recv(c.Reply) |
---|
| 127 | except Skype4Py.SkypeError: |
---|
[c15f71a] | 128 | pass |
---|
[8b3beef] | 129 | except Skype4Py.SkypeAPIError, s: |
---|
[a75f2a7] | 130 | dprint("Warning, sending '%s' failed (%s)." % (e, s)) |
---|
[4ddda13] | 131 | |
---|
[8237df5] | 132 | class Options: |
---|
| 133 | def __init__(self): |
---|
| 134 | self.daemon = True |
---|
| 135 | self.debug = False |
---|
| 136 | self.help = False |
---|
| 137 | self.port = 2727 |
---|
| 138 | self.version = False |
---|
| 139 | |
---|
| 140 | def usage(self, ret): |
---|
| 141 | print """Usage: skyped [OPTION]... |
---|
| 142 | |
---|
| 143 | skyped is a daemon that acts as a tcp server on top of a Skype instance. |
---|
| 144 | |
---|
| 145 | Options: |
---|
| 146 | -d --debug enable debug messages |
---|
| 147 | -h --help this help |
---|
| 148 | -n --nofork don't run as daemon in the background |
---|
| 149 | -p --port set the tcp port (default: %d) |
---|
| 150 | -v --version display version information""" % self.port |
---|
| 151 | sys.exit(ret) |
---|
| 152 | |
---|
[4ddda13] | 153 | if __name__=='__main__': |
---|
[8237df5] | 154 | options = Options() |
---|
| 155 | try: |
---|
| 156 | opts, args = getopt.getopt(sys.argv[1:], "dhnp:v", ["daemon", "help", "nofork", "port=", "version"]) |
---|
| 157 | except getopt.GetoptError: |
---|
| 158 | options.usage(1) |
---|
| 159 | for opt, arg in opts: |
---|
| 160 | if opt in ("-d", "--debug"): |
---|
| 161 | options.debug = True |
---|
| 162 | elif opt in ("-h", "--help"): |
---|
| 163 | options.help = True |
---|
| 164 | elif opt in ("-n", "--nofork"): |
---|
| 165 | options.daemon = False |
---|
| 166 | elif opt in ("-p", "--port"): |
---|
| 167 | options.port = arg |
---|
| 168 | elif opt in ("-v", "--version"): |
---|
| 169 | options.version = True |
---|
| 170 | if options.help: |
---|
| 171 | options.usage(0) |
---|
| 172 | elif options.version: |
---|
| 173 | print "skyped %s" % __version__ |
---|
| 174 | sys.exit(0) |
---|
| 175 | elif options.daemon: |
---|
| 176 | pid = os.fork() |
---|
| 177 | if pid == 0: |
---|
| 178 | nullin = file('/dev/null', 'r') |
---|
| 179 | nullout = file('/dev/null', 'w') |
---|
| 180 | os.dup2(nullin.fileno(), sys.stdin.fileno()) |
---|
| 181 | os.dup2(nullout.fileno(), sys.stdout.fileno()) |
---|
| 182 | os.dup2(nullout.fileno(), sys.stderr.fileno()) |
---|
| 183 | else: |
---|
| 184 | print 'skyped is started on port %s, pid: %d' % (options.port, pid) |
---|
| 185 | sys.exit(0) |
---|
[53b71d3] | 186 | server('0.0.0.0', options.port) |
---|
[c15f71a] | 187 | try: |
---|
[3953172] | 188 | skype = SkypeApi() |
---|
[8b3beef] | 189 | except Skype4Py.SkypeAPIError, s: |
---|
[c15f71a] | 190 | sys.exit("%s. Are you sure you have started Skype?" % s) |
---|
| 191 | gobject.idle_add(idle_handler, skype) |
---|
| 192 | gobject.MainLoop().run() |
---|