1 | #!/usr/bin/env python |
---|
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 | |
---|
27 | import sys |
---|
28 | import os |
---|
29 | import signal |
---|
30 | import locale |
---|
31 | import time |
---|
32 | import gobject |
---|
33 | import socket |
---|
34 | import getopt |
---|
35 | import Skype4Py |
---|
36 | import threading |
---|
37 | |
---|
38 | __version__ = "0.1.1" |
---|
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): |
---|
48 | input = fd.recv(1024) |
---|
49 | for i in input.split("\n"): |
---|
50 | skype.send(i.strip()) |
---|
51 | return True |
---|
52 | |
---|
53 | def idle_handler(skype): |
---|
54 | try: |
---|
55 | skype.skype.SendCommand(skype.skype.Command(-1, "PING")) |
---|
56 | except Skype4Py.SkypeAPIError, s: |
---|
57 | dprint("Warning, pinging Skype failed (%s)." % (s)) |
---|
58 | try: |
---|
59 | time.sleep(2) |
---|
60 | except KeyboardInterrupt: |
---|
61 | sys.exit("Exiting.") |
---|
62 | return True |
---|
63 | |
---|
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): |
---|
79 | global options |
---|
80 | |
---|
81 | if options.debug: |
---|
82 | print msg |
---|
83 | |
---|
84 | class SkypeApi(): |
---|
85 | def __init__(self): |
---|
86 | self.skype = Skype4Py.Skype() |
---|
87 | self.skype.OnNotify = self.recv |
---|
88 | self.skype.Attach() |
---|
89 | |
---|
90 | def recv(self, msg_text): |
---|
91 | global conn |
---|
92 | if msg_text == "PONG": |
---|
93 | return |
---|
94 | if "\n" in msg_text: |
---|
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' |
---|
100 | prefix = " ".join(msg_text.split(" ")[:3]) |
---|
101 | msg_text = ["%s %s" % (prefix, i) for i in " ".join(msg_text.split(" ")[3:]).split("\n")] |
---|
102 | else: |
---|
103 | msg_text = [msg_text] |
---|
104 | for i in msg_text: |
---|
105 | e = i.encode(locale.getdefaultlocale()[1]) |
---|
106 | dprint('<< ' + e) |
---|
107 | if conn: |
---|
108 | try: |
---|
109 | conn.send(e + "\n") |
---|
110 | except IOError, s: |
---|
111 | dprint("Warning, seding '%s' failed (%s)." % (e, s)) |
---|
112 | |
---|
113 | def send(self, msg_text): |
---|
114 | if not len(msg_text): |
---|
115 | return |
---|
116 | e = msg_text.decode(locale.getdefaultlocale()[1]) |
---|
117 | dprint('>> ' + e) |
---|
118 | try: |
---|
119 | self.skype.SendCommand(self.skype.Command(-1, e)) |
---|
120 | except Skype4Py.ISkypeError: |
---|
121 | pass |
---|
122 | except Skype4Py.SkypeAPIError, s: |
---|
123 | dprint("Warning, seding '%s' failed (%s)." % (e, s)) |
---|
124 | |
---|
125 | class Options: |
---|
126 | def __init__(self): |
---|
127 | self.daemon = True |
---|
128 | self.debug = False |
---|
129 | self.help = False |
---|
130 | self.port = 2727 |
---|
131 | self.version = False |
---|
132 | |
---|
133 | def usage(self, ret): |
---|
134 | print """Usage: skyped [OPTION]... |
---|
135 | |
---|
136 | skyped is a daemon that acts as a tcp server on top of a Skype instance. |
---|
137 | |
---|
138 | Options: |
---|
139 | -d --debug enable debug messages |
---|
140 | -h --help this help |
---|
141 | -n --nofork don't run as daemon in the background |
---|
142 | -p --port set the tcp port (default: %d) |
---|
143 | -v --version display version information""" % self.port |
---|
144 | sys.exit(ret) |
---|
145 | |
---|
146 | if __name__=='__main__': |
---|
147 | options = Options() |
---|
148 | try: |
---|
149 | opts, args = getopt.getopt(sys.argv[1:], "dhnp:v", ["daemon", "help", "nofork", "port=", "version"]) |
---|
150 | except getopt.GetoptError: |
---|
151 | options.usage(1) |
---|
152 | for opt, arg in opts: |
---|
153 | if opt in ("-d", "--debug"): |
---|
154 | options.debug = True |
---|
155 | elif opt in ("-h", "--help"): |
---|
156 | options.help = True |
---|
157 | elif opt in ("-n", "--nofork"): |
---|
158 | options.daemon = False |
---|
159 | elif opt in ("-p", "--port"): |
---|
160 | options.port = arg |
---|
161 | elif opt in ("-v", "--version"): |
---|
162 | options.version = True |
---|
163 | if options.help: |
---|
164 | options.usage(0) |
---|
165 | elif options.version: |
---|
166 | print "skyped %s" % __version__ |
---|
167 | sys.exit(0) |
---|
168 | elif options.daemon: |
---|
169 | pid = os.fork() |
---|
170 | if pid == 0: |
---|
171 | nullin = file('/dev/null', 'r') |
---|
172 | nullout = file('/dev/null', 'w') |
---|
173 | os.dup2(nullin.fileno(), sys.stdin.fileno()) |
---|
174 | os.dup2(nullout.fileno(), sys.stdout.fileno()) |
---|
175 | os.dup2(nullout.fileno(), sys.stderr.fileno()) |
---|
176 | else: |
---|
177 | print 'skyped is started on port %s, pid: %d' % (options.port, pid) |
---|
178 | sys.exit(0) |
---|
179 | server('0.0.0.0', options.port) |
---|
180 | try: |
---|
181 | skype = SkypeApi() |
---|
182 | except Skype4Py.SkypeAPIError, s: |
---|
183 | sys.exit("%s. Are you sure you have started Skype?" % s) |
---|
184 | gobject.idle_add(idle_handler, skype) |
---|
185 | gobject.MainLoop().run() |
---|