Exemplo n.º 1
0
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()