source: skype/skyped.py @ dfec37e

Last change on this file since dfec37e was cd3022c, checked in by VMiklos <vmiklos@…>, at 2007-08-21T18:18:08Z

skyped: added copyright header

  • Property mode set to 100644
File size: 3.2 KB
Line 
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# makepkg configuration
28""" GPL """
29import sys
30import signal
31import locale
32import time
33import dbus
34import dbus.service
35import dbus.mainloop.glib
36import gobject
37import socket
38
39
40SKYPE_SERVICE = 'com.Skype.API'
41CLIENT_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?
45conn = None
46
47def sig_handler(signum, frame):
48        mainloop.quit()
49
50def input_handler(fd, io_condition):
51        input = fd.recv(1024)
52        for i in input.split("\n"):
53                if i:
54                        fd.send((skype.send(i.strip()) + "\n").encode(locale.getdefaultlocale()[1]))
55        return True
56
57def server(host, port):
58        sock = socket.socket()
59        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
60        sock.bind((host, port))
61        sock.listen(1)
62        gobject.io_add_watch(sock, gobject.IO_IN, listener)
63
64def listener(sock, *args):
65        global conn
66        conn, addr = sock.accept()
67        fileno = conn.fileno()
68        gobject.io_add_watch(conn, gobject.IO_IN, input_handler)
69        return True
70
71def dprint(msg):
72        if len(sys.argv) > 1 and sys.argv[1] == "-d":
73                print msg
74
75class SkypeApi(dbus.service.Object):
76        def __init__(self):
77                bus = dbus.SessionBus()
78                try:
79                        self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype')
80                except dbus.exceptions.DBusException:
81                        sys.exit("Can't find any Skype instance. Are you sure you have started Skype?")
82
83                reply = self.send('NAME ' + CLIENT_NAME)
84                if reply != 'OK':
85                        sys.exit('Could not bind to Skype client')
86
87                reply = self.send('PROTOCOL 5')
88                dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API')
89
90        # skype -> client (async)
91        @dbus.service.method(dbus_interface='com.Skype.API')
92        def Notify(self, msg_text):
93                global conn
94                dprint('<< ' + msg_text)
95                if conn:
96                        conn.send(msg_text + "\n")
97
98        # client -> skype (sync, 5 sec timeout)
99        def send(self, msg_text):
100                if not len(msg_text):
101                        return
102                dprint('>> ' + msg_text)
103                try:
104                        reply = self.skype_api.Invoke(msg_text)
105                except dbus.exceptions.DBusException, s:
106                        reply = str(s)
107                dprint('<< ' + reply)
108                return reply
109
110if __name__=='__main__':
111        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
112        skype = SkypeApi()
113        signal.signal(signal.SIGINT, sig_handler)
114        mainloop = gobject.MainLoop()
115        server('localhost', 2727)
116        mainloop.run()
Note: See TracBrowser for help on using the repository browser.