source: skype/skyped.py @ f932b65

Last change on this file since f932b65 was f932b65, checked in by VMiklos <vmiklos@…>, at 2007-08-20T18:56:46Z

skyped: some cleanup, no encoding is needed when sending messages

  • Property mode set to 100644
File size: 2.2 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
17# well, this is a bit hackish. we store the socket of the last connected client
18# here and notify it. maybe later notify all connected clients?
19conn = None
20
21def sig_handler(signum, frame):
22        mainloop.quit()
23
24def input_handler(fd, io_condition):
25        input = fd.recv(1024)
26        for i in input.split("\n"):
27                if i:
28                        fd.send((skype.send(i.strip()) + "\n").encode(locale.getdefaultlocale()[1]))
29        return True
30
31def server(host, port):
32        sock = socket.socket()
33        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
34        sock.bind((host, port))
35        sock.listen(1)
36        gobject.io_add_watch(sock, gobject.IO_IN, listener)
37
38def listener(sock, *args):
39        global conn
40        conn, addr = sock.accept()
41        fileno = conn.fileno()
42        gobject.io_add_watch(conn, gobject.IO_IN, input_handler)
43        return True
44
45def dprint(msg):
46        if len(sys.argv) > 1 and sys.argv[1] == "-d":
47                print msg
48
49class SkypeApi(dbus.service.Object):
50        def __init__(self):
51                bus = dbus.SessionBus()
52                try:
53                        self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype')
54                except dbus.exceptions.DBusException:
55                        sys.exit("Can't find any Skype instance. Are you sure you have started Skype?")
56
57                reply = self.send('NAME ' + CLIENT_NAME)
58                if reply != 'OK':
59                        sys.exit('Could not bind to Skype client')
60
61                reply = self.send('PROTOCOL 5')
62                dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API')
63
64        # skype -> client (async)
65        @dbus.service.method(dbus_interface='com.Skype.API')
66        def Notify(self, msg_text):
67                global conn
68                dprint('<< ' + msg_text)
69                if conn:
70                        conn.send(msg_text + "\n")
71
72        # client -> skype (sync, 5 sec timeout)
73        def send(self, msg_text):
74                if not len(msg_text):
75                        return
76                dprint('>> ' + msg_text)
77                try:
78                        reply = self.skype_api.Invoke(msg_text)
79                except dbus.exceptions.DBusException, s:
80                        reply = str(s)
81                dprint('<< ' + reply)
82                return reply
83
84if __name__=='__main__':
85        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
86        skype = SkypeApi()
87        signal.signal(signal.SIGINT, sig_handler)
88        mainloop = gobject.MainLoop()
89        server('localhost', 2727)
90        mainloop.run()
Note: See TracBrowser for help on using the repository browser.