source: skype/skyped.py @ a316c4e

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

skyped: finish cleanup
hopefully no later cosmetics changes will be necessary now

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