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