#!/usr/bin/env python #/** # * Copyright (C) 2005 # * Author: psychedelys # * # * May break everything, just provided like this with out guaranty. # * # * This program is free software; you can redistribute it and/or modify # * it under the terms of the GNU General Public License as published by # * the Free Software Foundation; either version 2 of the License, or # * (at your option) any later version. # * # * This program is distributed in the hope that it will be useful, # * but WITHOUT ANY WARRANTY; without even the implied warranty of # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # * GNU General Public License for more details. # * # * You should have received a copy of the GNU General Public License # * along with this program; if not, write to the Free Software # * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # * # * BTW, We are using the Skype API, so the global licence of the interface, # * is under the more restrictive licence, and I don't spend time to check, # * I need to correct it. # */ # #TODO : # - grep TODO # - CLEAR UP THE MATTER ABOUT OCCUPATION/FREE for PSTN-USB # (what a mess in my following code) # - Switch to "Not available" when our PSTN is in used, # so we can't be disturbe while using the other line # - didn't manage HOLD status (from skype) # #Functionality: # - read the code, ... # import pygtk pygtk.require('2.0') import gtk import gobject import dbus, sys,socket class skypeUSBB2K: usbb2k_state="standby" usbb2k_current_port_status="USB" skype_current_call=0 skype_status_away="DND" skype_status_available="ONLINE" skype_calling_number="" s=None def write_textbuffer(self, direction, message): if direction=="GET": print " w_tb get :",message msg=message.split() print msg # if msg[0]=="CALL": if msg[2]=="STATUS": if msg[3]=="RINGING": print "Ring, ring, ring, ring, status:",self.usbb2k_state self.skype_current_call=msg[1] if self.usbb2k_state=="standby": #IF PSTN IS FREE, SWITCH to USB print "Start Ring state" print "USBB2K SEND DATA: RING 1" self.s.send("RING 1") self.ring_state=True self.usbb2k_state="Ring" #ELSE # REFUSED else: set_msg="SET CALL "+str(self.skype_current_call)+" STATUS REFUSED" self.api_object.send_message(set_msg) elif msg[3]=="MISSED": if self.usbb2k_state=="Ring": print "End Ring state" self.usbb2k_state="standby" print "USBB2K SEND DATA: RING 0" self.s.send("RING 0") self.ring_state=False #we missed the call and the status was not Ring, strange, reinitialize else: print "Oups, strange 'missed' status was : ", self.usbb2k_state self.usbb2k_state="standby" elif msg[3]=="INPROGRESS": print "We take the call" self.usbb2k_state="Chat" elif msg[3]=="FINISHED": print "The CALL end" self.usbb2k_state="standby" elif msg[3]=="FAILLED": print "The CALL Failed" self.usbb2k_state="standby" elif direction=="SEND": print " w_tb send :",message else: print "skypeUSBB2K.write_textbuffer unknow direction: ",direction, message #self.skypeUSBB2K.s.send(message) def KeyTimeOut(self): print "KeyTimeOut" def PstnUsb(self,widget,data=None): #if not data: if widget.get_active(): if data=="USB": print "USBB2K USB active buton" self.usbb2k_current_port_status="USB" print "USBB2K SEND DATA: SWITCH USB" self.s.send("SWITCH USB") self.button_r1.set_active(True) self.button_r2.set_active(False) else: print "USBB2K PSTN active buton" self.usbb2k_current_port_status="PSTN" print "USBB2K SEND DATA: SWITCH PSTN" self.s.send("SWITCH PSTN") self.button_r1.set_active(False) self.button_r2.set_active(True) def usbb2k_interrupt(self,sock,mode): data=sock.recv(100) if not data: sys.exit("Error usbb2k sock\n") usbb2k_event=data.splitlines() # TODO manage the case where we get more than 1 instruction in the same time # like at start up print "USBB2K GET DATA: ",usbb2k_event usbb2k_type=usbb2k_event[0].split() if usbb2k_type[0]=='KEY': if self.usbb2k_state=="WaitNum": if usbb2k_type[1]=='0b': if self.skype_calling_number=="": print "USBB2K GET: KEY *, switch PSTN/USB" #self.PstnUsb(self.button_r1) elif usbb2k_type[1]=='0b': #'#' print "Calling this skype nb: ",self.skype_calling_number # # send instruction to skype else: self.skype_calling_number=self.skype_calling_number+"usbb2k_type[1]" print usbb2k_type[1] if usbb2k_type[0]=='HANDSET': if usbb2k_type[1]=='OFF': print "USBB2K GET: HANDSET OFF" if self.usbb2k_state=="Chat": print "end Call" set_msg="SET CALL "+str(self.skype_current_call)+" STATUS FINISHED" self.api_object.send_message(set_msg) self.usbb2k_state="standby" set_msg="SET USERSTATUS "+self.skype_status_available self.api_object.send_message(set_msg) self.skype_calling_number="" if self.usbb2k_state=="WaitNum": self.usbb2k_state="standby" set_msg="SET USERSTATUS "+self.skype_status_available self.api_object.send_message(set_msg) self.skype_calling_number="" if usbb2k_type[1]=='ON': print "USBB2K GET: HANDSET ON and port ",self.usbb2k_current_port_status if self.usbb2k_current_port_status=="USB": if self.usbb2k_state=="Ring": print "accept Call" self.usbb2k_state="Chat" set_msg="SET CALL "+str(self.skype_current_call)+" STATUS INPROGRESS" self.api_object.send_message(set_msg) else: print "wait number" self.usbb2k_state="WaitNum" #get first the current status set_msg="SET USERSTATUS "+self.skype_status_away self.api_object.send_message(set_msg) return True def destroy(self,widget,data=None): gtk.main_quit() def delete_event(self,widget,data=None): gtk.main_quit() def __init__(self): #init dbus for skype #bus = dbus.SessionBus() #end init dbus component self.ring_state=False self.s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) self.s.connect("/tmp/usbb2k.sock") if self.s: th_usbb2k_read=gobject.io_add_watch(self.s,gobject.IO_IN,self.usbb2k_interrupt) self.window=gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_title("Skype <--> USBB2K-API TEST") self.window.connect("destroy",self.destroy) self.window.set_resizable(False) self.window.set_border_width(10) self.vbox = gtk.VBox(True, 2) self.window.add(self.vbox) self.vbox.show() self.button_r1 = gtk.RadioButton(None, "USB", False) self.button_r1.connect("toggled", self.PstnUsb, "USB") self.button_r2 = gtk.RadioButton(self.button_r1, "PSTN") self.button_r2.connect("toggled", self.PstnUsb, "PSTN") self.button = gtk.Button(None, gtk.STOCK_QUIT) self.button.connect("clicked", self.destroy) self.vbox.pack_start(self.button_r1, True, True, 5) self.vbox.pack_start(self.button_r2, True, True, 5) self.vbox.pack_start(self.button, True, True, 5) self.window.show_all() # don't find how to ask the box about the active port # so initialize the default as USB...as I don't used PSTN self.PstnUsb(self.button_r1,"USB") #objects self.api_object = Api_obj(self) self.callback_obj = Callback_obj(dbus.Service("com.Skype.API", dbus.SystemBus()), self) class Callback_obj(dbus.Object): # Skype -> Client messages def Notify(self, message, message_text): print "SKYPE GET1: ",message_text self.mainwindow.write_textbuffer("GET",message_text) def __init__(self, service, mw): self.mainwindow = mw dbus.Object.__init__(self, "/com/Skype/Client", service, [self.Notify]) class Api_obj: # Client -> Skype def send_message(self, message): print "SKYPE SEND: ",message self.mainwindow.write_textbuffer("SEND",message) answer = self.skype_api_object.Invoke(message) print "SKYPE GET: ",answer self.mainwindow.write_textbuffer("GET",answer) return answer def __init__(self, mw): self.mainwindow = mw remote_bus = dbus.SystemBus() system_service_list = remote_bus.get_service('org.freedesktop.DBus').get_object('/org/freedesktop/DBus', 'org.freedesktop.DBus').ListServices() skype_api_found = 0 for service in system_service_list: print "available dbus service:",service for service in system_service_list: if service=='com.Skype.API': skype_api_found = 1 break if not skype_api_found: sys.exit('No running API-capable Skype found') skype_service = remote_bus.get_service('com.Skype.API') self.skype_api_object = skype_service.get_object('/com/Skype','com.Skype.API') answer = self.send_message('NAME SkypeApiPythonTestClient') if answer != 'OK': sys.exit('Could not bind to Skype client') answer = self.send_message('PROTOCOL 1') if answer != 'PROTOCOL 1': sys.exit('This test program only supports Skype API protocol version 1') def main(): usbb2k=skypeUSBB2K() gtk.main() return 0 if __name__=="__main__": main()