source: skype/skyped.py @ 94bd28f

Last change on this file since 94bd28f was 94bd28f, checked in by VMiklos <vmiklos@…>, at 2007-08-20T14:26:05Z

skyped: use tabs
bitlbee uses tabs, too

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#!/usr/bin/env python
2""" GPL """
3import sys
4import signal
5import locale
6import time
7import dbus
8import dbus.service
9import dbus.mainloop.glib
10import gobject
11import socket
12
13
14SKYPE_SERVICE = 'com.Skype.API'
15CLIENT_NAME = 'SkypeApiPythonShell'
16
17local_encoding = locale.getdefaultlocale()[1]
18need_conv = (local_encoding != 'utf-8')
19
20# well, this is a bit hackish. we store the socket of the last connected client
21# here and notify it. maybe later notify all connected clients?
22conn = None
23
24def utf8_decode(utf8_str):
25        if need_conv:
26                return utf8_str.decode('utf-8').encode(local_encoding, 'replace')
27        else:
28                return utf8_str
29
30def utf8_encode(local_str):
31        if need_conv:
32                return local_str.decode(local_encoding).encode('utf-8')
33        else:
34                return local_str
35
36def sig_handler(signum, frame):
37        print 'Caught signal %d, exiting.' % signum
38        mainloop.quit()
39
40def input_handler(fd, io_condition):
41        input = fd.recv(1024)
42        for i in input.split("\n"):
43        if i:
44                fd.send(skype.send(i.strip()) + "\n")
45        return True
46
47class SkypeApi(dbus.service.Object):
48        def __init__(self):
49                bus = dbus.SessionBus()
50
51        try:
52                self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype')
53        except dbus.exceptions.DBusException:
54                sys.exit("Can't find any Skype instance. Are you sure you have started Skype?")
55
56        reply = self.send('NAME ' + CLIENT_NAME)
57        if reply != 'OK':
58                sys.exit('Could not bind to Skype client')
59
60        reply = self.send('PROTOCOL 5')
61        dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API')
62
63
64        # skype -> client (async)
65        @dbus.service.method(dbus_interface='com.Skype.API')
66        def Notify(self, msg_text):
67                global conn
68                text = utf8_decode(msg_text)
69                print '<<', text
70                if conn:
71                        conn.send(msg_text + "\n")
72
73        # client -> skype (sync, 5 sec timeout)
74        def send(self, msg_text):
75                if not len(msg_text):
76                        return
77                print '>> ', msg_text
78                try:
79                        reply = utf8_decode(self.skype_api.Invoke(utf8_encode(msg_text)))
80                except dbus.exceptions.DBusException, s:
81                        reply = str(s)
82                print '<< ', reply
83                return reply
84
85dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
86skype = SkypeApi()
87signal.signal(signal.SIGINT, sig_handler)
88
89mainloop = gobject.MainLoop()
90
91def server(host, port):
92        '''Initialize server and start listening.'''
93        sock = socket.socket()
94        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
95        sock.bind((host, port))
96        sock.listen(1)
97        gobject.io_add_watch(sock, gobject.IO_IN, listener)
98def listener(sock, *args):
99        '''Asynchronous connection listener. Starts a handler for each connection.'''
100        global conn
101        conn, addr = sock.accept()
102        fileno = conn.fileno()
103        gobject.io_add_watch(conn, gobject.IO_IN, input_handler)
104        return True
105if __name__=='__main__':
106        server('localhost', 2727)
107        mainloop.run()
Note: See TracBrowser for help on using the repository browser.