aboutsummaryrefslogtreecommitdiffstats
path: root/skype/skyped.py
blob: ff275a15ebbb009596c604ba7ce1bf2d5fb0f96f (plain)
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
#!/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.
#

# makepkg configuration
""" GPL """
import sys
import signal
import locale
import time
import dbus
import dbus.service
import dbus.mainloop.glib
import gobject
import socket


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 sig_handler(signum, frame):
	mainloop.quit()

def input_handler(fd, io_condition):
	input = fd.recv(1024)
	for i in input.split("\n"):
		if i:
			fd.send((skype.send(i.strip()) + "\n").encode(locale.getdefaultlocale()[1]))
	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):
	if len(sys.argv) > 1 and sys.argv[1] == "-d":
		print msg

class SkypeApi(dbus.service.Object):
	def __init__(self):
		bus = dbus.SessionBus()
		try:
			self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype')
		except dbus.exceptions.DBusException:
			sys.exit("Can't find any Skype instance. Are you sure you have started Skype?")

		reply = self.send('NAME ' + CLIENT_NAME)
		if reply != 'OK':
			sys.exit('Could not bind to Skype client')

		reply = self.send('PROTOCOL 5')
		dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API')

	# skype -> client (async)
	@dbus.service.method(dbus_interface='com.Skype.API')
	def Notify(self, msg_text):
		global conn
		dprint('<< ' + msg_text)
		if conn:
			conn.send(msg_text + "\n")

	# client -> skype (sync, 5 sec timeout)
	def send(self, msg_text):
		if not len(msg_text):
			return
		dprint('>> ' + msg_text)
		try:
			reply = self.skype_api.Invoke(msg_text)
		except dbus.exceptions.DBusException, s:
			reply = str(s)
		dprint('<< ' + reply)
		return reply

if __name__=='__main__':
	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
	skype = SkypeApi()
	signal.signal(signal.SIGINT, sig_handler)
	mainloop = gobject.MainLoop()
	server('localhost', 2727)
	mainloop.run()