Exemplo n.º 1
0
    def setUp(self):
        # Set up Google App Engine testbed
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.testbed.init_app_identity_stub()

        self.contactNumber = "+16135551234"

        # Set up some handy constants
        self._APP_ID = app_identity.get_application_id()
        self.MAIL_SUFFIX = "@" + self._APP_ID + ".appspotmail.com"
        self.XMPP_SUFFIX = "@" + self._APP_ID + ".appspotchat.com"

        self.ownerPhoneNumber = "+16135554444"
        self.ownerJid = "*****@*****.**"
        self.ownerEmailAddress = "*****@*****.**"
        self.owner = Owner(self.ownerPhoneNumber, self.ownerJid, self.ownerEmailAddress)
        self.xmppvoicemail = XmppVoiceMail(self.owner)
        self.communications = self.xmppvoicemail._communications = CommunicationsFixture()

        # Subscribe the default sender.
        defaultSender = Contact.getDefaultSender()
        defaultSender.subscribed = True
        Contact.update(defaultSender)
Exemplo n.º 2
0
def main():
    routes = [
        (r'/recording', PostRecording),
        (r'/call', CallHandler),
        (r'/sms', SMSHandler),
        
        (r'/api/login', LoginHandler),
        (r'/api/admin/contacts', AdminContactsHandler),
        (r'/api/admin/contacts/(.*)', AdminContactsHandler),
        (r'/api/admin/log', AdminLogHandler),
        (r'/api/invite', InviteHandler),
        (r'/api/sendSms', SendSmsHandler),
        
        (r'/_ah/xmpp/message/chat/', XMPPHandler),
        (r'/_ah/xmpp/presence/(available|unavailable)/', XmppPresenceHandler),
        (r'/_ah/xmpp/subscription/(subscribe|subscribed|unsubscribe|unsubscribed)/', XmppSubscribeHandler),
        
        MailHandler.mapping(),
    ]

    webapp2Config = {}
    webapp2Config['webapp2_extras.sessions'] = {
        'secret_key': config.SESSION_SECRET_KEY,
    }
            
    app = webapp2.WSGIApplication(routes=routes, debug=True, config=webapp2Config)
    app.error_handlers[404] = handle_404
    app.error_handlers[500] = handle_500

    # Send an invite for the default sender if the default sender is not subscribed.
    defaultSender = Contact.getDefaultSender()
    if not defaultSender.subscribed:
        xmppVoiceMail.sendXmppInvite(defaultSender.name)
       
    return app
Exemplo n.º 3
0
    def test_incomingXmppFromBadUser(self):
        """
        Test an incoming email to the default sender with a number in the message body.
        """
        with self.assertRaises(PermissionException):
            defaultSender = Contact.getDefaultSender()

            self.xmppvoicemail.handleIncomingXmpp(
                sender="*****@*****.**", to=defaultSender.name + self.XMPP_SUFFIX, messageBody="(613)555-1235: Hello"
            )
Exemplo n.º 4
0
 def getDisplayNameAndContact(self, number):
     displayName = toPrettyNumber(number)
     
     # Find the XMPP user to send this from
     contact = Contact.getByPhoneNumber(number)
     if contact:
         displayName = contact.name
     else:
         contact = Contact.getDefaultSender()
         
     return (displayName, contact)
Exemplo n.º 5
0
    def test_incomingXmppForInvalidNumber(self):
        """
        Test an incoming XMPP to the default sender, with an invalid number
        in the body.
        """
        with self.assertRaises(InvalidParametersException):
            defaultSender = Contact.getDefaultSender()

            self.xmppvoicemail.handleIncomingXmpp(
                sender=self.ownerJid, to=defaultSender.name + self.XMPP_SUFFIX, messageBody="(613)555-123a: Hello"
            )
Exemplo n.º 6
0
    def _sendXMPPMessage(self, message, fromContact=None, fromNumber=None):
        if not fromContact:
            fromContact = Contact.getDefaultSender()

        # Add the fromNumber to the message if this is from the default sender.
        if fromContact.isDefaultSender() and fromNumber:
            message = toPrettyNumber(fromNumber) + ": " + message

        logging.debug("Sending XMPP message to " + self._owner.jid + ": " + message)

        fromJid = fromContact.name + "@" + self._APP_ID + ".appspotchat.com"
        return self._communications.sendXmppMessage(fromJid, self._owner.jid, message)
Exemplo n.º 7
0
 def handleIncomingCall(self, fromNumber, callStatus):
     """Handle an incoming call.
     """
     displayFrom = toPrettyNumber(fromNumber)
     
     # Find the XMPP user to send this from
     contact = Contact.getByPhoneNumber(fromNumber)
     if contact:
         displayFrom = contact.name
     else:
         contact = Contact.getDefaultSender()
         
     self.sendMessageToOwner("Call from: " + displayFrom + " status:" + callStatus, contact, fromNumber)
Exemplo n.º 8
0
    def get(self):
        answer = []

        # Always put the default sender at the top.
        defaultSender = Contact.getDefaultSender()
        answer.append(defaultSender.toDict())

        contacts = Contact.all()
        contacts.filter('__key__ !=', defaultSender.key())
        
        for contact in contacts:
            answer.append(contact.toDict())
            
        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps(answer))
Exemplo n.º 9
0
    def test_incomingSmsMessageFromUnknownUserSubscribed(self):
        """
        Tests an incoming SMS message from an unknown user with xmppVoiceMail subscribed.
        """
        defaultSender = Contact.getDefaultSender()

        # Send the SMS
        self.xmppvoicemail.handleIncomingSms("+16135551234", self.ownerPhoneNumber, "Hello")

        # Should get an XMPP from the default sender.
        self.assertEqual(1, len(self.communications.xmppMessages), "Should have sent an XMPP message")
        message = self.communications.xmppMessages[0]
        self.assertEqual(defaultSender.name + self.XMPP_SUFFIX, message["fromJid"])
        self.assertEqual(self.ownerJid, message["toJid"])
        self.assertEqual("(613)555-1234: Hello", message["message"])
Exemplo n.º 10
0
    def test_incomingXmppForNumber(self):
        """
        Test an incoming XMPP to the default sender, with a number in the body.
        """
        defaultSender = Contact.getDefaultSender()

        self.xmppvoicemail.handleIncomingXmpp(
            sender=self.ownerJid, to=defaultSender.name + self.XMPP_SUFFIX, messageBody="(613)555-1234: Hello"
        )

        # Should send an SMS to 555-1234
        self.assertEqual(1, len(self.communications.sms), "Should have sent an SMS")
        sms = self.communications.sms[0]
        self.assertEqual("+16135551234", sms["toNumber"])
        self.assertEqual("Hello", sms["body"])
Exemplo n.º 11
0
    def test_incomingEmailFromBadUser(self):
        """
        Test an incoming email to the default sender with a number in the message body.
        """
        with self.assertRaises(PermissionException):
            defaultSender = Contact.getDefaultSender()

            self.xmppvoicemail.handleIncomingEmail(
                sender="*****@*****.**",
                to=defaultSender.name + self.XMPP_SUFFIX,
                subject="Foo",
                messageBody="16135551234: Hello",
            )

        # We don't deal with terrorists:
        self.assertEqual(0, len(self.communications.mails))
Exemplo n.º 12
0
    def test_incomingSmsMessageFromUnknownUser(self):
        """
        Tests an incoming SMS message from an unknown user with xmppVoiceMail unsubscribed.
        """
        # Unsubscribe the default sender.
        defaultSender = Contact.getDefaultSender()
        defaultSender.subscribed = False
        Contact.update(defaultSender)

        # Send the SMS
        self.xmppvoicemail.handleIncomingSms("+16135551234", self.ownerPhoneNumber, "Hello")

        # Should get an email, since user is not subscribed to xmppVoiceMail via XMPP.
        self.assertEqual(1, len(self.communications.mails), "Should have sent an email.")
        message = self.communications.mails[0]
        self.assertEqual('"' + defaultSender.name + '" <16135551234' + self.MAIL_SUFFIX + ">", message["sender"])
        self.assertEqual(self.ownerEmailAddress, message["to"])
        self.assertEqual("Hello", message["subject"])
Exemplo n.º 13
0
    def test_incomingEmailForNumber(self):
        """
        Test an incoming email to the default sender with a number in the message body.
        """
        defaultSender = Contact.getDefaultSender()

        self.xmppvoicemail.handleIncomingEmail(
            sender=self.ownerEmailAddress,
            to=defaultSender.name + self.XMPP_SUFFIX,
            subject="Foo",
            messageBody="16135551234: Hello",
        )

        # Should send an SMS to 555-1234
        self.assertEqual(1, len(self.communications.sms), "Should have sent an SMS")
        sms = self.communications.sms[0]
        self.assertEqual("+16135551234", sms["toNumber"])
        self.assertEqual("Hello", sms["body"])
Exemplo n.º 14
0
    def sendMessageToOwner(self, message, contact=None, fromNumber=None):
        """
        Send a message to the user who owns this XmppVoiceMail account.

        contact is the contact to send the message from.
        
        fromNumber is the phone number to send the message from if contact is
        the default sender.

        Returns True on success, False on failure.
        """
        answer = False

        defaultSender = Contact.getDefaultSender()
        if not contact:
            contact = defaultSender

        fromJid = contact.name  + "@" + self._APP_ID + ".appspotchat.com"
        xmppOnline = self._ownerXmppPresent(fromJid)
                
        sendByEmail = self._owner.emailEnabled() and ( (not xmppOnline) or \
                      ((not contact.subscribed) and (not defaultSender.subscribed)) ) 
                
        if sendByEmail:
            self.sendEmailMessageToOwner(
                subject=message,
                fromContact=contact,
                fromNumber=fromNumber)
            answer = True
             
        elif self._owner.xmppEnabled():
            if not contact.subscribed:
                # Need a subscribed contact for XMPP; use the default sender.
                contact = defaultSender

            result = self._sendXMPPMessage(
                message=message,
                fromContact=contact,
                fromNumber=fromNumber)
            answer = result == xmpp.NO_ERROR
                
        return answer
Exemplo n.º 15
0
    def sendEmailMessageToOwner(self, subject, body=None, fromContact=None, fromNumber=None):
        if not body:
            body = ""

        if not fromContact:
            fromContact = Contact.getDefaultSender()

        fromName = fromContact.name
        if fromNumber:
            fromAddress = stripNumber(toNormalizedNumber(fromNumber))
        elif not fromContact.isDefaultSender():
            fromContact.normalizedPhoneNumber
        else:
            fromAddress = fromName
            
        logging.debug("Sending eMail message to " + self._owner.emailAddress + ": " + subject)

        fromAddress = '"' + fromName + '" <' + fromAddress + "@" + self._APP_ID + ".appspotmail.com>"
        self._communications.sendMail(
            sender=fromAddress,
            to=self._owner.emailAddress,
            subject=subject,
            body=body)