source: skype/skyped.py @ cb35add

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

bitlbeed -> skyped
i was crazy when i imported the daemon as bitlbeed..

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