class VBTS: """ initialize the object """ def __init__(self, to_be_handled): self.app = Yate() self.app.__Yatecall__ = self.yatecall self.to_be_handled = to_be_handled self.log = logging.getLogger("libvbts.yate.VBTS_SMS_Route.VBTS") self.ym = YateMessenger.YateMessenger() def yatecall(self, d): if d == "": self.app.Output("VBTS SMS_Route event: empty") elif d == "incoming": res = self.ym.parse(self.app.params) for (tag, re) in self.regexs: if (not res.has_key(tag) or not re.match(res[tag])): self.app.Output("VBTS SMS_Route %s did not match" % (tag,)) return self.app.Output("VBTS SMS_Route received: " + self.app.name + " id: " + self.app.id) self.log.info("VBTS SMS_Route received: " + self.app.name + " id: " + self.app.id) self.app.handled = True self.app.retval = "202" self.app.Acknowledge() self.ym.send_smqueue_sms(self.app, res["vbts_tp_dest_address"], "%s <sip:%s@%s>" % (res["caller"], res["caller"], res["address"]), res["vbts_text"]) elif d == "answer": self.app.Output("VBTS SMS_Route Answered: " + self.app.name + " id: " + self.app.id) elif d == "installed": self.app.Output("VBTS SMS_Route Installed: " + self.app.name ) elif d == "uninstalled": self.app.Output("VBTS SMS_Route Uninstalled: " + self.app.name ) else: self.app.Output("VBTS SMS_Route event: " + self.app.type ) def uninstall(self): for (msg, pri) in self.to_be_handled: self.app.Uninstall(msg) def main(self, priority, regexs): self.regexs = regexs try: self.app.Output("VBTS SMS_Route Starting") for msg in to_be_handled: self.app.Output("VBTS SMS_Route Installing %s at %d" % (msg, priority)) self.log.info("Installing %s at %d" % (msg, priority)) self.app.Install(msg, priority) while True: self.app.flush() time.sleep(0.1) except: self.app.Output("Unexpected error:" + str(sys.exc_info()[0])) self.close() def close(self): self.uninstall() self.app.close()
class Provisioner: """ initialize the object """ def __init__(self, to_be_handled): self.app = Yate() self.app.__Yatecall__ = self.yatecall self.log = logging.getLogger( "libvbts.yate.VBTS_Call_Provisioning.Provisioner") self.ym = YateMessenger.YateMessenger() self.to_be_handled = to_be_handled self.state = "call" self.ourcallid = "provisioner/" + uniqid(10) self.partycallid = "" self.last_dtmf = (None, 0.0) self.user_num = "" self.minnum = int(self.ym.smqueue_get("SC.Register.Digits.Min")) self.maxnum = int(self.ym.smqueue_get("SC.Register.Digits.Max")) self.log.info("Starting with min=%d and max=%d" % (self.minnum, self.maxnum)) def __play(self, fileloc): self.app.Yate("chan.attach") self.app.params = [] self.app.params.append(["source", "wave/play/" + fileloc]) self.app.params.append(["notify", self.ourcallid]) self.app.Dispatch() #not sure if this is needed self.app.Yate("chan.attach") self.app.params = [] self.app.params.append(["consumer", "wave/record/-"]) self.app.params.append(["maxlen", "320000"]) self.app.params.append(["notify", self.ourcallid]) self.app.Dispatch() self.transition = (0, [], "intro") return def __next(self, index, files, nextState): self.transition = (index, files, nextState) def setState(self, state): self.app.Output("setState('%s') state: %s" % (self.state, state)) if (state == ""): self.close() if (state == "intro"): self.state = state self.__play(INTRO_FILE) self.__next(1, [INTRO_FILE], "input") #if we get this, replay the prompt elif (state == "input"): self.state = state self.__play(INPUT_FILE) self.__next(1, [INPUT_FILE], "input") elif (state == "taken"): self.state = state self.user_num = "" self.__play(TAKEN_FILE) self.__next(1, [TAKEN_FILE], "input") elif (state == "invalid"): self.state = state self.user_num = "" self.__play(INVALID_FILE) self.__next(1, [INVALID_FILE], "input") elif (state == "exit"): self.state = state self.user_num = "" self.__play(EXIT_FILE) self.__next(1, [EXIT_FILE], "goodbye") elif (state == "error"): self.state = state self.user_num = "" self.__play(ERROR_FILE) self.__next(1, [ERROR_FILE], "input") elif (state == "verify"): #number taken self.app.Output( str(self.ym.SR_get("name", ("callerid", self.user_num)))) if (self.ym.SR_get("name", ("callerid", self.user_num))): #don't set the state, transfer to taken instead self.setState("taken") return else: #want to play chosens and numbers here self.__play(VERIFY_START_FILE) audio = [VERIFY_START_FILE] for i in self.user_num: audio.append(NUMBER_FILES[i]) audio.append(VERIFY_END_FILE) self.app.Output("Want to verify %s" % (self.user_num, )) self.state = state self.__next(1, audio, "verify") elif (state == "goodbye"): self.state = state self.app.Yate("chan.attach") self.app.params = [] self.app.params.append(["source", "tone/congestion"]) self.app.params.append(["consumer", "wave/record/-"]) self.app.params.append(["maxlen", 32000]) self.app.params.append(["notify", self.ourcallid]) self.app.Dispatch() def gotNotify(self, reason): self.app.Output("gotNotify() state: %s" % (self.state, )) if (reason == "replace"): return elif (reason == "goodbye"): self.setState("") elif (reason == "record" or reason == "play"): self.setState("input") elif (reason == "eof"): (index, files, nextState) = self.transition if (index >= len(files)): self.setState(nextState) else: self.__play(files[index]) index += 1 self.transition = (index, files, nextState) def gotDTMF(self, text): #if it's too recent, skip the dtmf burst if (text == self.last_dtmf[0] and time.time() < self.last_dtmf[1] + DTMF_TIME): self.last_dtmf = (text, time.time()) return else: self.last_dtmf = (text, time.time()) self.app.Output("gotDTMF('%s') state: %s" % (text, self.state)) #else if we're in the verify step, should only get # or * if (self.state == "verify"): if (text == "#"): self.app.Output( "Provisoning %s %s %s %s" % (self.name, self.user_num, self.ipaddr, self.port)) self.log.info( "Provisoning %s %s %s %s" % (self.name, self.user_num, self.ipaddr, self.port)) if (self.ym.SR_provision(self.name, self.user_num, self.ipaddr, self.port)): self.ym.send_smqueue_sms( self.app, self.user_num, "101 <sip:[email protected]>", "Your Papa Legba Burning Man number is %s" % (self.user_num, )) self.setState("exit") else: self.setState("error") elif (text == "*"): self.user_num = "" self.setState("input") else: self.setState("verify") #still inputting elif (self.state == "input"): if (text in string.digits): self.user_num += text elif (text == "#"): if ((len(self.user_num) >= self.minnum) and (len(self.user_num) <= self.maxnum)): self.setState("verify") else: self.log.info("Number %s is invalid" % self.user_num) self.setState("invalid") elif (text == "*"): #generate number for them self.user_num = random_num(self.minnum, self.maxnum) while (self.ym.SR_get("name", ("callerid", self.user_num))): self.user_num = random_num(self.minnum, self.maxnum) self.setState("verify") #we'll figure this out later else: self.user_num = "" self.setState("input") def yatecall(self, d): if d == "": self.app.Output("VBTS Provisioner event: empty") elif d == "incoming": self.app.Output("VBTS Provisioner Incoming: " + self.app.name + " id: " + self.app.id) if (self.app.name == "call.execute"): if (not self.ym.is_imsi( self.ym.get_param("caller", self.app.params))): self.app.Acknowledge() return self.name = self.ym.get_param("caller", self.app.params) self.ipaddr = self.ym.get_param("ip_host", self.app.params) self.port = self.ym.get_param("ip_port", self.app.params) self.partycallid = self.ym.get_param("id", self.app.params) self.ym.add_param("targetid", self.ourcallid, self.app.params) self.app.handled = True #if this is being originated, we have to create next link target = self.ym.get_param("vbts_target", self.app.params) if (target): callername = self.ym.get_param("vbts_from", self.app.params) if not (callername): callername = "101" old_yate = self.app self.app.Yate("call.execute") self.app.params = [] self.ym.add_param("id", self.partycallid, self.app.params) self.ym.add_param("callto", target, self.app.params) self.ym.add_param("caller", callername, self.app.params) #self.ym.add_param("tonedetect_out", "true", self.app.params) self.app.Dispatch() #finish with old message old_yate.Acknowledge() else: self.app.Acknowledge() #then respond self.app.Yate("call.answered") self.app.params = [] self.ym.add_param("id", self.ourcallid, self.app.params) self.ym.add_param("targetid", self.partycallid, self.app.params) self.app.Dispatch() self.setState("intro") return elif (self.app.name == "chan.notify"): self.app.Output("VBTS Provisioner Notify: " + self.app.name + " id: " + self.app.id) if (self.ym.get_param("targetid", self.app.params) in [self.ourcallid, self.partycallid]): self.gotNotify(self.ym.get_param("reason", self.app.params)) self.app.handled = True self.app.Acknowledge() return elif (self.app.name == "chan.dtmf"): if (self.ym.get_param("targetid", self.app.params) in [self.ourcallid, self.partycallid]): text = self.ym.get_param("text", self.app.params) for t in text: self.gotDTMF(t) self.app.handled = True self.app.Acknowledge() return else: self.app.Output("WTF? " + self.app.name) self.app.Acknowledge() return elif d == "answer": self.app.Output("VBTS Provisioner Answered: " + self.app.name + " id: " + self.app.id) elif d == "installed": self.app.Output("VBTS Provisioner Installed: " + self.app.name) elif d == "uninstalled": self.app.Output("VBTS Provisioner Uninstalled: " + self.app.name) else: self.app.Output("VBTS Provisioner event: " + self.app.type) def uninstall(self): for msg in self.to_be_handled: self.app.Uninstall(msg) def main(self): for msg in to_be_handled: self.app.Output("VBTS Provisioner Installing %s" % (msg, )) self.log.info("Installing %s" % (msg, )) self.app.Install(msg) try: while True: self.app.flush() time.sleep(0.1) except Exception as e: self.log.debug(str(e)) self.close() def close(self): self.uninstall() self.app.close() exit(0)
class VBTS_Provisioning: """ initialize the object """ def __init__(self, to_be_handled): self.app = Yate() self.app.__Yatecall__ = self.yatecall self.log = logging.getLogger( "libvbts.yate.VBTS_SMS_Provisioning.VBTS_Provisioning") self.ym = YateMessenger.YateMessenger() self.to_be_handled = to_be_handled def yatecall(self, d): if d == "": self.app.Output("VBTS Provisioning event: empty") elif d == "incoming": res = self.ym.parse(self.app.params) Output( self.app, self.log, "VBTS Provisioning received: " + self.app.name + " id: " + self.app.id) sender_name = res["caller"] #look up their number to set to return sender_num = self.ym.SR_get("callerid", ("name", sender_name)) #they're in the system, pass on it if (sender_num): self.app.Acknowledge() #otherwise redirect to provisioning script else: self.app.handled = True self.app.retval = "202" self.app.Acknowledge() #originate a new call self.ym.originate(self.app, res["caller"], "101", "external/nodata/VBTS_Call_Provisioning.py", ipaddr=res["ip_host"], port=res["ip_port"]) elif d == "answer": self.app.Output("VBTS Provisioning Answered: " + self.app.name + " id: " + self.app.id) elif d == "installed": self.app.Output("VBTS Provisioning Installed: " + self.app.name) elif d == "uninstalled": self.app.Output("VBTS Provisioning Uninstalled: " + self.app.name) else: self.app.Output("VBTS Provisioning event: " + self.app.type) def uninstall(self): for (msg, pri) in self.to_be_handled: self.app.Uninstall(msg) def main(self, priority): try: self.app.Output("VBTS Provisioning Starting") for msg in to_be_handled: self.app.Output("VBTS Provisioning_SMS Installing %s at %d" % (msg, priority)) self.log.info("Installing %s at %d" % (msg, priority)) self.app.Install(msg, priority) while True: self.app.flush() time.sleep(0.1) except: self.app.Output("Unexpected error:" + str(sys.exc_info()[0])) self.close() def close(self): self.uninstall() self.app.close()
class IPBX: def __init__(self): self.app = Yate() self.app.__Yatecall__ = self.retenv def retenv(self, ev): if ev == "": self.app.Output("Event is Empty") elif ev == "incoming": self.app.Output("Message: " + self.app.name + " id: " + self.app.id) if self.app.name == "engine.debug": self.app.Output("ENGINE DEBUG") elif self.app.name == "engine.command": self.app.Output("ENGINE COMMAND") elif self.app.name == "engine.status": self.app.Output("ENGINE STATUS") elif self.app.name == "engine.timer": self.app.Output("ENGINE TIMER") elif self.app.name == "user.notify": self.app.Output("USER NOTIFY") elif self.app.name == "user.auth": self.app.Output("USER AUTH") elif self.app.name == "user.register": self.app.Output("USER REGISTER") elif self.app.name == "user.unregister": self.app.Output("USER UNREGISTER") elif self.app.name == "call.route": self.app.Output("CALL ROUTE") elif self.app.name == "call.answered": self.app.Output("CALL ANSWERED") elif self.app.name == "chan.hangup": self.app.Output("CHAN HANGUP") elif self.app.name == "chan.disconnected": self.app.Output("CHAN DISCONNECTED") elif self.app.name == "call.cdr": self.app.Output("CALL CDR") else: self.app.Acknowledge() self.app.Acknowledge() self.count = self.count + 1 elif ev == "answer": self.app.Output("IPBX Answered: " + self.app.name + " id: " + self.app.id) elif ev == "installed": self.app.Output("IPBX Installed: " + self.app.name) elif ev == "uninstalled": self.app.Output("IPBX Uninstalled: " + self.app.name) else: self.app.Output("IPBX event: " + self.app.type) def uninstall(self): self.app.Uninstall("engine.timer") def main(self): self.count = 0 self.app.Install("engine.timer", 10) self.app.Install("user.register", 10); self.app.Install("user.unregister", 10); self.app.Install("user.auth", 10); self.app.Install("call.route", 10); self.app.Install("call.cdr", 10); self.app.Install("call.answered",50); self.app.Install("chan.disconnected",50); self.app.Install("chan.hangup", 10); self.app.Install("user.notify", 10); self.app.Install("engine.status"); self.app.Install("engine.command"); self.app.Install("engine.debug"); while True: self.app.flush() if self.count == 10: self.uninstall() break self.app.Output("PYTHON: Bye!") # close file descriptors self.app.close()
class Route_Local: """ initialize the object """ def __init__(self, to_be_handled): self.app = Yate() self.app.__Yatecall__ = self.yatecall self.log = logging.getLogger( "libvbts.yate.VBTS_Route_Local.Route_Local") self.ym = YateMessenger.YateMessenger() self.to_be_handled = to_be_handled def yatecall(self, d): if d == "": self.app.Output("VBTS Route Local event: empty") elif d == "incoming": #ensure it's an IMSI if (not self.ym.is_imsi( self.ym.get_param("caller", self.app.params))): self.app.Acknowledge() return self.app.Output("VBTS Route Local received: " + self.app.name + " id: " + self.app.id) self.log.info("VBTS Route Local received: " + self.app.name + " id: " + self.app.id) #get the destination name target = self.ym.SR_dialdata_get( "dial", ("exten", self.ym.get_param("called", self.app.params))) #and the caller's number caller_num = self.ym.SR_get( "callerid", ("name", self.ym.get_param("callername", self.app.params))) if (target and caller_num): target_ip = self.ym.SR_get("ipaddr", ("name", target)) target_port = self.ym.SR_get("port", ("name", target)) self.app.retval = "sip/%s@%s:%s" % (target, target_ip, target_port) self.ym.add_param("caller", caller_num, self.app.params) self.ym.add_param("callername", caller_num, self.app.params) self.app.handled = True #ack regardless self.app.Acknowledge() elif d == "answer": self.app.Output("VBTS Route Local Answered: " + self.app.name + " id: " + self.app.id) elif d == "installed": self.app.Output("VBTS Route Local Installed: " + self.app.name) elif d == "uninstalled": self.app.Output("VBTS Route Local Uninstalled: " + self.app.name) else: self.app.Output("VBTS Route Local event: " + self.app.type) def uninstall(self): for (msg, pri) in self.to_be_handled: self.app.Uninstall(msg) def main(self, priority, regexs): self.regexs = regexs try: self.app.Output("VBTS Route Local Starting") for msg in to_be_handled: self.app.Output("VBTS Route Local Installing %s at %d" % (msg, priority)) self.log.info("Installing %s at %d" % (msg, priority)) self.app.Install(msg, priority) while True: self.app.flush() time.sleep(0.1) except: self.app.Output("Unexpected error:" + str(sys.exc_info()[0])) self.close() def close(self): self.uninstall() self.app.close()
class Route_Provisioning: """ initialize the object """ def __init__(self, to_be_handled): self.app = Yate() self.app.__Yatecall__ = self.yatecall self.log = logging.getLogger( "libvbts.yate.VBTS_Route_Provisioning.Route_Provisioning") self.ym = YateMessenger.YateMessenger() self.to_be_handled = to_be_handled def yatecall(self, d): if d == "": self.app.Output("VBTS Route Provisioning event: empty") elif d == "incoming": #ensure it's an IMSI if (not self.ym.is_imsi( self.ym.get_param("caller", self.app.params))): self.app.Acknowledge() return self.app.Output("VBTS Route Provisioning received: " + self.app.name + " id: " + self.app.id) self.log.info("VBTS Route Provisioning received: " + self.app.name + " id: " + self.app.id) #check if the caller exists caller_num = self.ym.SR_get( "callerid", ("name", self.ym.get_param("callername", self.app.params))) #if they don't, route them to the provisioning script if (not caller_num): self.app.retval = "external/nodata/VBTS_Call_Provisioning.py" self.app.handled = True #ack regardless self.app.Acknowledge() elif d == "answer": self.app.Output("VBTS Route Provisioning Answered: " + self.app.name + " id: " + self.app.id) elif d == "installed": self.app.Output("VBTS Route Provisioning Installed: " + self.app.name) elif d == "uninstalled": self.app.Output("VBTS Route Provisioning Uninstalled: " + self.app.name) else: self.app.Output("VBTS Route Provisioning event: " + self.app.type) def uninstall(self): for (msg, pri) in self.to_be_handled: self.app.Uninstall(msg) def main(self, priority, regexs): self.regexs = regexs try: self.app.Output("VBTS Echo Starting") for msg in to_be_handled: self.app.Output("VBTS Echo_SMS Installing %s at %d" % (msg, priority)) self.log.info("Installing %s at %d" % (msg, priority)) self.app.Install(msg, priority) while True: self.app.flush() time.sleep(0.1) except: self.app.Output("Unexpected error:" + str(sys.exc_info()[0])) self.close() def close(self): self.uninstall() self.app.close()
class IVR: """ initialize the object """ def __init__(self, to_be_handled): self.app = Yate() self.app.__Yatecall__ = self.yatecall self.log = logging.getLogger("libvbts.yate.playrec.IVR") self.ym = YateMessenger.YateMessenger() self.to_be_handled = to_be_handled self.state = "call" self.ourcallid = "playrec/" + uniqid(10) self.partycallid = "" self.dir = "/tmp" self.last_dtmf = (None, 0.0) def setState(self, state): self.app.Output("setState('%s') state: %s" % (self.state, state)) if (state == ""): self.close() #if we get this, replay the prompt if (state == "prompt"): self.state = state self.app.Yate("chan.attach") self.app.params = [] self.app.params.append(["source", "wave/play//tmp/test.gsm"]) self.app.Dispatch() self.app.Yate("chan.attach") self.app.params = [] self.app.params.append(["consumer", "wave/record/-"]) self.app.params.append(["maxlen", "320000"]) self.app.params.append(["notify", self.ourcallid]) self.app.Dispatch() return if (state == self.state): return if (state == "record"): self.app.Yate("chan.attach") self.app.params = [] self.app.params.append(["source", "wave/play/-"]) self.app.params.append( ["consumer", "wave/record/" + self.dir + "/playrec.gsm"]) self.app.params.append(["maxlen", "80000"]) self.app.params.append(["notify", self.ourcallid]) self.app.Dispatch() elif (state == "play"): self.app.Yate("chan.attach") self.app.params = [] self.app.params.append( ["source", "wave/play/" + self.dir + "/playrec.gsm"]) self.app.params.append(["consumer", "wave/record/-"]) self.app.params.append(["maxlen", "480000"]) self.app.params.append(["notify", self.ourcallid]) self.app.Dispatch() elif (state == "goodbye"): self.app.Yate("chan.attach") self.app.params = [] self.app.params.append(["source", "tone/congestion"]) self.app.params.append(["consumer", "wave/record/-"]) self.app.params.append(["maxlen", 32000]) self.app.params.append(["notify", self.ourcallid]) self.app.Dispatch() #update state self.state = state def gotNotify(self, reason): self.app.Output("gotNotify() state: %s" % (self.state, )) if (reason == "replace"): return elif (reason == "goodbye"): self.setState("") elif (reason == "prompt"): self.setState("goodbye") elif (reason == "record" or reason == "play"): self.setState("prompt") def gotDTMF(self, text): #if it's too recent, skip the dtmf burst if (text == self.last_dtmf[0] and time.time() < self.last_dtmf[1] + DTMF_TIME): self.last_dtmf = (text, time.time()) return else: self.last_dtmf = (text, time.time()) self.app.Output("gotDTMF('%s') state: %s" % (text, self.state)) if (text == "1"): self.setState("record") elif (text == "2"): self.setState("play") elif (text == "3"): self.setState("") elif (text == "#"): self.setState("prompt") def yatecall(self, d): if d == "": self.app.Output("VBTS IVR event: empty") elif d == "incoming": self.app.Output("VBTS IVR Incoming: " + self.app.name + " id: " + self.app.id) if (self.app.name == "call.execute"): self.partycallid = self.ym.get_param("id", self.app.params) self.ym.add_param("targetid", self.ourcallid, self.app.params) self.app.handled = True self.app.Acknowledge() self.app.Yate("call.answered") self.app.params = [] self.ym.add_param("id", self.ourcallid, self.app.params) self.ym.add_param("targetid", self.partycallid, self.app.params) self.app.Dispatch() self.setState("prompt") return elif (self.app.name == "chan.notify"): if (self.ym.get_param("targetid", self.app.params) == self.ourcallid): self.gotNotify(self.ym.get_param("reason", self.app.params)) self.app.handled = True self.app.Acknowledge() return elif (self.app.name == "chan.dtmf"): if (self.ym.get_param("targetid", self.app.params) == self.ourcallid): text = self.ym.get_param("text", self.app.params) for t in text: self.gotDTMF(t) self.app.handled = True self.app.Acknowledge() return elif d == "answer": self.app.Output("VBTS IVR Answered: " + self.app.name + " id: " + self.app.id) elif d == "installed": self.app.Output("VBTS IVR Installed: " + self.app.name) elif d == "uninstalled": self.app.Output("VBTS IVR Uninstalled: " + self.app.name) else: self.app.Output("VBTS IVR event: " + self.app.type) def uninstall(self): for msg in self.to_be_handled: self.app.Uninstall(msg) def main(self): for msg in to_be_handled: self.app.Output("VBTS IVR Installing %s" % (msg, )) self.log.info("Installing %s" % (msg, )) self.app.Install(msg) while True: self.app.flush() time.sleep(0.1) def close(self): self.uninstall() self.app.close() exit(0)
class YateApp: """ initialize the object """ def __init__(self): # call the Yate object self.app = Yate() # declare the event intercetion method self.app.__Yatecall__ = self.retenv # this function will manage the imput from Yate. # param "d" is the Yate.type variable for incoming message def retenv(self, d): if d == "": self.app.Output("PYTHON event: empty") elif d == "incoming": self.app.Output("PYTHON message: " + self.app.name + " id: " + self.app.id) self.app.Acknowledge() self.count = self.count + 1 elif d == "answer": self.app.Output("PYTHON Answered: " + self.app.name + " id: " + self.app.id) elif d == "installed": self.app.Output("PYTHON Installed: " + self.app.name ) elif d == "uninstalled": self.app.Output("PYTHON Uninstalled: " + self.app.name ) else: self.app.Output("PYTHON event: " + self.app.type ) # clean shutdown of our application... def uninstall(self): self.app.Uninstall("engine.timer") def main(self): # create and dispatch an initial test message to Yate self.app.Yate("test") self.app.retval = "ret_value" self.app.params = [ ['param1', 'value1'], ['param2', 'value2'] ] self.app.Dispatch() # reset the counter self.count = 0 # install engine.timer self.app.Install("engine.timer", 10) # start the main loop while True: # check for events self.app.flush() # uninstall engine.timer after 5 messages from Yate if self.count == 5: self.uninstall() break # ok, now a second example with a threaded asyncore loop! self.count = 0 self.app.Install("engine.timer", 10) self.app.th_loop() # wow, amazing, the loop still running and we can continue in the execution! # dispatch a final test message after the main loop exit self.app.Yate("final_test") self.app.retval = "ret_value" self.app.params = [ ['param1', 'value1'] ] self.app.Dispatch() # now wait for 10 message from yate, and then uninstall the engine.timer and exit while self.count != 10: pass self.uninstall() self.app.th_stop() # bye! self.app.Output("PYTHON: Bye!") # close file descriptors self.app.close()
class VBTS_Echo: """ initialize the object """ def __init__(self, to_be_handled): self.app = Yate() self.app.__Yatecall__ = self.yatecall self.log = logging.getLogger("libvbts.yate.VBTS_SMS_Echo.VBTS_Echo") self.ym = YateMessenger.YateMessenger() self.to_be_handled = to_be_handled def yatecall(self, d): if d == "": self.app.Output("VBTS ECHO event: empty") elif d == "incoming": res = self.ym.parse(self.app.params) for (tag, re) in self.regexs: if (not res.has_key(tag) or not re.match(res[tag])): self.app.Output("VBTS ECHO %s did not match" % (tag, )) self.app.Acknowledge() return self.app.Output("VBTS ECHO received: " + self.app.name + " id: " + self.app.id) self.log.info("VBTS ECHO received: " + self.app.name + " id: " + self.app.id) self.app.handled = True self.app.retval = "202" self.app.Acknowledge() sender_name = res["caller"] #look up their number to set to return sender_num = self.ym.SR_get("callerid", ("name", sender_name)) self.ym.send_openbts_sms(self.app, sender_name, "<sip:%[email protected]>" % (sender_num, ), res["vbts_text"]) elif d == "answer": self.app.Output("VBTS ECHO Answered: " + self.app.name + " id: " + self.app.id) elif d == "installed": self.app.Output("VBTS ECHO Installed: " + self.app.name) elif d == "uninstalled": self.app.Output("VBTS ECHO Uninstalled: " + self.app.name) else: self.app.Output("VBTS ECHO event: " + self.app.type) def uninstall(self): for (msg, pri) in self.to_be_handled: self.app.Uninstall(msg) def main(self, priority, regexs): self.regexs = regexs try: self.app.Output("VBTS Echo Starting") for msg in to_be_handled: self.app.Output("VBTS Echo_SMS Installing %s at %d" % (msg, priority)) self.log.info("Installing %s at %d" % (msg, priority)) self.app.Install(msg, priority) while True: self.app.flush() time.sleep(0.1) except: self.app.Output("Unexpected error:" + str(sys.exc_info()[0])) self.close() def close(self): self.uninstall() self.app.close()