source: skype/skyped.py @ 09be265

Last change on this file since 09be265 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
RevLine 
[4ddda13]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):
[94bd28f]22        mainloop.quit()
[4ddda13]23
24def input_handler(fd, io_condition):
[94bd28f]25        input = fd.recv(1024)
26        for i in input.split("\n"):
[a316c4e]27                if i:
[f932b65]28                        fd.send((skype.send(i.strip()) + "\n").encode(locale.getdefaultlocale()[1]))
[94bd28f]29        return True
[4ddda13]30
[a316c4e]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
[4ddda13]49class SkypeApi(dbus.service.Object):
[94bd28f]50        def __init__(self):
51                bus = dbus.SessionBus()
[a316c4e]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?")
[94bd28f]56
[a316c4e]57                reply = self.send('NAME ' + CLIENT_NAME)
58                if reply != 'OK':
59                        sys.exit('Could not bind to Skype client')
[94bd28f]60
[a316c4e]61                reply = self.send('PROTOCOL 5')
62                dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API')
[94bd28f]63
64        # skype -> client (async)
65        @dbus.service.method(dbus_interface='com.Skype.API')
66        def Notify(self, msg_text):
67                global conn
[f932b65]68                dprint('<< ' + msg_text)
[94bd28f]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
[a316c4e]76                dprint('>> ' + msg_text)
[94bd28f]77                try:
[f932b65]78                        reply = self.skype_api.Invoke(msg_text)
[94bd28f]79                except dbus.exceptions.DBusException, s:
80                        reply = str(s)
[a316c4e]81                dprint('<< ' + reply)
[94bd28f]82                return reply
[4ddda13]83
84if __name__=='__main__':
[a316c4e]85        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
86        skype = SkypeApi()
87        signal.signal(signal.SIGINT, sig_handler)
88        mainloop = gobject.MainLoop()
[4ddda13]89        server('localhost', 2727)
90        mainloop.run()
Note: See TracBrowser for help on using the repository browser.