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 | skype.skype.SendCommand(skype.skype.Command(-1, "PING")) |
---|
55 | try: |
---|
56 | time.sleep(2) |
---|
57 | except KeyboardInterrupt: |
---|
58 | sys.exit("Exiting.") |
---|
59 | return True |
---|
60 | |
---|
61 | def server(host, port): |
---|
62 | sock = socket.socket() |
---|
63 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
---|
64 | sock.bind((host, port)) |
---|
65 | sock.listen(1) |
---|
66 | gobject.io_add_watch(sock, gobject.IO_IN, listener) |
---|
67 | |
---|
68 | def listener(sock, *args): |
---|
69 | global conn |
---|
70 | conn, addr = sock.accept() |
---|
71 | fileno = conn.fileno() |
---|
72 | gobject.io_add_watch(conn, gobject.IO_IN, input_handler) |
---|
73 | return True |
---|
74 | |
---|
75 | def dprint(msg): |
---|
76 | global options |
---|
77 | |
---|
78 | if options.debug: |
---|
79 | print msg |
---|
80 | |
---|
81 | class SkypeApi(): |
---|
82 | def __init__(self): |
---|
83 | self.skype = Skype4Py.Skype() |
---|
84 | self.skype.OnNotify = self.recv |
---|
85 | self.skype.Attach() |
---|
86 | |
---|
87 | def recv(self, msg_text): |
---|
88 | global conn |
---|
89 | if msg_text == "PONG": |
---|
90 | return |
---|
91 | if "\n" in msg_text: |
---|
92 | # crappy skype prefixes only the first line for |
---|
93 | # multiline messages so we need to do so for the other |
---|
94 | # lines, too. this is something like: |
---|
95 | # 'CHATMESSAGE id BODY first line\nsecond line' -> |
---|
96 | # 'CHATMESSAGE id BODY first line\nCHATMESSAGE id BODY second line' |
---|
97 | prefix = " ".join(msg_text.split(" ")[:3]) |
---|
98 | msg_text = ["%s %s" % (prefix, i) for i in " ".join(msg_text.split(" ")[3:]).split("\n")] |
---|
99 | else: |
---|
100 | msg_text = [msg_text] |
---|
101 | for i in msg_text: |
---|
102 | e = i.encode(locale.getdefaultlocale()[1]) |
---|
103 | dprint('<< ' + e) |
---|
104 | if conn: |
---|
105 | try: |
---|
106 | conn.send(e + "\n") |
---|
107 | except IOError, s: |
---|
108 | dprint("Warning, seding '%s' failed (%s)." % (e, s)) |
---|
109 | |
---|
110 | def send(self, msg_text): |
---|
111 | if not len(msg_text): |
---|
112 | return |
---|
113 | e = msg_text.decode(locale.getdefaultlocale()[1]) |
---|
114 | dprint('>> ' + e) |
---|
115 | try: |
---|
116 | self.skype.SendCommand(self.skype.Command(-1, e)) |
---|
117 | except Skype4Py.ISkypeError: |
---|
118 | pass |
---|
119 | except Skype4Py.SkypeAPIError, s: |
---|
120 | dprint("Warning, seding '%s' failed (%s)." % (e, s)) |
---|
121 | |
---|
122 | class Options: |
---|
123 | def __init__(self): |
---|
124 | self.daemon = True |
---|
125 | self.debug = False |
---|
126 | self.help = False |
---|
127 | self.port = 2727 |
---|
128 | self.version = False |
---|
129 | |
---|
130 | def usage(self, ret): |
---|
131 | print """Usage: skyped [OPTION]... |
---|
132 | |
---|
133 | skyped is a daemon that acts as a tcp server on top of a Skype instance. |
---|
134 | |
---|
135 | Options: |
---|
136 | -d --debug enable debug messages |
---|
137 | -h --help this help |
---|
138 | -n --nofork don't run as daemon in the background |
---|
139 | -p --port set the tcp port (default: %d) |
---|
140 | -v --version display version information""" % self.port |
---|
141 | sys.exit(ret) |
---|
142 | |
---|
143 | if __name__=='__main__': |
---|
144 | options = Options() |
---|
145 | try: |
---|
146 | opts, args = getopt.getopt(sys.argv[1:], "dhnp:v", ["daemon", "help", "nofork", "port=", "version"]) |
---|
147 | except getopt.GetoptError: |
---|
148 | options.usage(1) |
---|
149 | for opt, arg in opts: |
---|
150 | if opt in ("-d", "--debug"): |
---|
151 | options.debug = True |
---|
152 | elif opt in ("-h", "--help"): |
---|
153 | options.help = True |
---|
154 | elif opt in ("-n", "--nofork"): |
---|
155 | options.daemon = False |
---|
156 | elif opt in ("-p", "--port"): |
---|
157 | options.port = arg |
---|
158 | elif opt in ("-v", "--version"): |
---|
159 | options.version = True |
---|
160 | if options.help: |
---|
161 | options.usage(0) |
---|
162 | elif options.version: |
---|
163 | print "skyped %s" % __version__ |
---|
164 | sys.exit(0) |
---|
165 | elif options.daemon: |
---|
166 | pid = os.fork() |
---|
167 | if pid == 0: |
---|
168 | nullin = file('/dev/null', 'r') |
---|
169 | nullout = file('/dev/null', 'w') |
---|
170 | os.dup2(nullin.fileno(), sys.stdin.fileno()) |
---|
171 | os.dup2(nullout.fileno(), sys.stdout.fileno()) |
---|
172 | os.dup2(nullout.fileno(), sys.stderr.fileno()) |
---|
173 | else: |
---|
174 | print 'skyped is started on port %s, pid: %d' % (options.port, pid) |
---|
175 | sys.exit(0) |
---|
176 | server('0.0.0.0', options.port) |
---|
177 | try: |
---|
178 | skype = SkypeApi() |
---|
179 | except Skype4Py.SkypeAPIError, s: |
---|
180 | sys.exit("%s. Are you sure you have started Skype?" % s) |
---|
181 | gobject.idle_add(idle_handler, skype) |
---|
182 | gobject.MainLoop().run() |
---|