import sys import pjsua as pj from time import sleep import string server = "sipgate.de" username = "1289459" password = "MMW9AX" current_call = None incomingCallNumber = "" LOG_LEVEL=2 userInput = "" # Logging callback def log_cb(level, str, len): print str, # Callback to receive events from account class MyAccountCallback(pj.AccountCallback): def __init__(self, account=None): pj.AccountCallback.__init__(self, account) # Notification on incoming call def on_incoming_call(self, call): global current_call if current_call: call.answer(486, "Busy") return incomingCallNumber = call.info().remote_uri print "This number "+incomingCallNumber+" is calling." current_call = call call_cb = MyCallCallback(current_call) current_call.set_callback(call_cb) current_call.answer(180) # Callback to receive events from Call class MyCallCallback(pj.CallCallback): def __init__(self, call=None): pj.CallCallback.__init__(self, call) # Notification when call state has changed def on_state(self): global current_call if self.call.info().state == pj.CallState.CONNECTING: print "CONNECTING" if self.call.info().state == pj.CallState.EARLY: print "RINGING" if self.call.info().state == pj.CallState.CONFIRMED: print "CALL CONFIRMED" if self.call.info().state == pj.CallState.DISCONNECTED: current_call = None print "CURRENT CALL DISCONNECTED" lib = pj.Lib() lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb)) transport = lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(0)) lib.start() lib.set_null_snd_dev() try: acc_cfg = pj.AccountConfig(server,username,password) acc = lib.create_account(acc_cfg, cb=MyAccountCallback()) except ValueError: print "Exception: " + str(e) print "You want to receive a call then." print "Receive another call?" userInput = sys.stdin.readline().rstrip("\r\n") while userInput == "yes": if acc.info().reg_status < 700: if not current_call: continue else: sleep(5) current_call.answer(200) sleep(5) current_call.hangup() sleep(3) print "Receive another call? " userInput = sys.stdin.readline().rstrip("\r\n") else: print "REGISTRATION ERROR" lib.destroy() lib = None acc = None sleep(2) print "LOGGING OUT..." acc.delete() lib.destroy() lib = None acc = None print "LOGOUT WAS SUCCESSFULL!!"