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.send("PING") |
---|
55 | try: |
---|
56 | time.sleep(10) |
---|
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._API.Handlers.append(Skype4Py.utils.WeakCallableRef(self.recv)) |
---|
85 | self.skype._API._Handler = self.recv |
---|
86 | self.skype.Attach() |
---|
87 | |
---|
88 | def recv(self, mode, msg_text): |
---|
89 | global conn |
---|
90 | if mode != "rece_api": |
---|
91 | return |
---|
92 | if "\n" in msg_text: |
---|
93 | # crappy skype prefixes only the first line for |
---|
94 | # multiline messages so we need to do so for the other |
---|
95 | # lines, too. this is something like: |
---|
96 | # 'CHATMESSAGE id BODY first line\nsecond line' -> |
---|
97 | # 'CHATMESSAGE id BODY first line\nCHATMESSAGE id BODY second line' |
---|
98 | prefix = " ".join(msg_text.split(" ")[:3]) |
---|
99 | msg_text = ["%s %s" % (prefix, i) for i in " ".join(msg_text.split(" ")[3:]).split("\n")] |
---|
100 | else: |
---|
101 | msg_text = [msg_text] |
---|
102 | for i in msg_text: |
---|
103 | dprint('<< ' + i) |
---|
104 | if conn: |
---|
105 | conn.send(i + "\n") |
---|
106 | |
---|
107 | def send(self, msg_text): |
---|
108 | if not len(msg_text): |
---|
109 | return |
---|
110 | dprint('>> ' + msg_text) |
---|
111 | try: |
---|
112 | self.skype._DoCommand(msg_text) |
---|
113 | except Skype4Py.ISkypeError: |
---|
114 | pass |
---|
115 | |
---|
116 | class Options: |
---|
117 | def __init__(self): |
---|
118 | self.daemon = True |
---|
119 | self.debug = False |
---|
120 | self.help = False |
---|
121 | self.port = 2727 |
---|
122 | self.version = False |
---|
123 | |
---|
124 | def usage(self, ret): |
---|
125 | print """Usage: skyped [OPTION]... |
---|
126 | |
---|
127 | skyped is a daemon that acts as a tcp server on top of a Skype instance. |
---|
128 | |
---|
129 | Options: |
---|
130 | -d --debug enable debug messages |
---|
131 | -h --help this help |
---|
132 | -n --nofork don't run as daemon in the background |
---|
133 | -p --port set the tcp port (default: %d) |
---|
134 | -v --version display version information""" % self.port |
---|
135 | sys.exit(ret) |
---|
136 | |
---|
137 | if __name__=='__main__': |
---|
138 | options = Options() |
---|
139 | try: |
---|
140 | opts, args = getopt.getopt(sys.argv[1:], "dhnp:v", ["daemon", "help", "nofork", "port=", "version"]) |
---|
141 | except getopt.GetoptError: |
---|
142 | options.usage(1) |
---|
143 | for opt, arg in opts: |
---|
144 | if opt in ("-d", "--debug"): |
---|
145 | options.debug = True |
---|
146 | elif opt in ("-h", "--help"): |
---|
147 | options.help = True |
---|
148 | elif opt in ("-n", "--nofork"): |
---|
149 | options.daemon = False |
---|
150 | elif opt in ("-p", "--port"): |
---|
151 | options.port = arg |
---|
152 | elif opt in ("-v", "--version"): |
---|
153 | options.version = True |
---|
154 | if options.help: |
---|
155 | options.usage(0) |
---|
156 | elif options.version: |
---|
157 | print "skyped %s" % __version__ |
---|
158 | sys.exit(0) |
---|
159 | elif options.daemon: |
---|
160 | pid = os.fork() |
---|
161 | if pid == 0: |
---|
162 | nullin = file('/dev/null', 'r') |
---|
163 | nullout = file('/dev/null', 'w') |
---|
164 | os.dup2(nullin.fileno(), sys.stdin.fileno()) |
---|
165 | os.dup2(nullout.fileno(), sys.stdout.fileno()) |
---|
166 | os.dup2(nullout.fileno(), sys.stderr.fileno()) |
---|
167 | else: |
---|
168 | print 'skyped is started on port %s, pid: %d' % (options.port, pid) |
---|
169 | sys.exit(0) |
---|
170 | server('0.0.0.0', options.port) |
---|
171 | try: |
---|
172 | skype = SkypeApi() |
---|
173 | except Skype4Py.errors.ISkypeAPIError, s: |
---|
174 | sys.exit("%s. Are you sure you have started Skype?" % s) |
---|
175 | gobject.idle_add(idle_handler, skype) |
---|
176 | gobject.MainLoop().run() |
---|