コード例 #1
0
ファイル: autoresponder.py プロジェクト: Samdney/bridgedb
def createResponseBody(lines, context, client, lang='en'):
    """Parse the **lines** from an incoming email request and determine how to
    respond.

    :param list lines: The list of lines from the original request sent by the
        client.
    :type context: :class:`bridgedb.email.server.MailServerContext`
    :param context: The context which contains settings for the email server.
    :type client: :api:`twisted.mail.smtp.Address`
    :param client: The client's email address which should be in the
        ``'To:'`` header of the response email.
    :param str lang: The 2-5 character locale code to use for translating the
        email. This is obtained from a client sending a email to a valid plus
        address which includes the translation desired, i.e. by sending an
        email to `[email protected]
        <mailto:[email protected]>`__, the client should receive a
        response in Farsi.
    :rtype: str
    :returns: ``None`` if we shouldn't respond to the client (i.e., if they
        have already received a rate-limiting warning email). Otherwise,
        returns a string containing the (optionally translated) body for the
        email response which we should send out.
    """
    translator = translations.installTranslations(lang)
    bridges = None
    try:
        bridgeRequest = request.determineBridgeRequestOptions(lines)
        bridgeRequest.client = str(client)

        # The request was invalid, respond with a help email which explains
        # valid email commands:
        if not bridgeRequest.isValid():
            raise EmailRequestedHelp("Email request from '%s' was invalid."
                                     % str(client))

        # Otherwise they must have requested bridges:
        interval = context.schedule.intervalStart(time.time())
        bridges = context.distributor.getBridges(bridgeRequest, interval)
    except EmailRequestedHelp as error:
        logging.info(error)
        return templates.buildWelcomeText(translator, client)
    except EmailRequestedKey as error:
        logging.info(error)
        return templates.buildKeyMessage(translator, client)
    except TooSoonEmail as error:
        logging.info("Got a mail too frequently: %s." % error)
        return templates.buildSpamWarning(translator, client)
    except (IgnoreEmail, addr.BadEmail) as error:
        logging.info(error)
        # Don't generate a response if their email address is unparsable or
        # invalid, or if we've already warned them about rate-limiting:
        return None
    else:
        answer = "(no bridges currently available)\r\n"
        if bridges:
            transport = bridgeRequest.justOnePTType()
            answer = "".join("  %s\r\n" % b.getBridgeLine(
                bridgeRequest, context.includeFingerprints) for b in bridges)
        return templates.buildAnswerMessage(translator, client, answer)
コード例 #2
0
 def test_templates_buildSpamWarning(self):
     text = templates.buildSpamWarning(self.t, self.client)
     self.shouldIncludeGreeting(text)
     self.shouldIncludeAutomationNotice(text)
     self.shouldIncludeFooter(text)
コード例 #3
0
ファイル: server.py プロジェクト: mmaker/bridgedb
def createResponseBody(lines, context, clientAddress, lang='en'):
    """Parse the **lines** from an incoming email request and determine how to
    respond.

    :param list lines: The list of lines from the original request sent by the
        client.
    :type context: class:`MailContext`
    :param context: The context which contains settings for the email server.
    :type clientAddress: :api:`twisted.mail.smtp.Address`
    :param clientAddress: The client's email address which should be in the
        :header:`To:` header of the response email.
    :param str lang: The 2-5 character locale code to use for translating the
        email. This is obtained from a client sending a email to a valid plus
        address which includes the translation desired, i.e. by sending an
        email to ``[email protected]``, the client should receive a
        response in Farsi.
    :rtype: None or str
    :returns: None if we shouldn't respond to the client (i.e., if they have
        already received a rate-limiting warning email). Otherwise, returns a
        string containing the (optionally translated) body for the email
        response which we should send out.
    """
    clientAddr = '@'.join([clientAddress.local, clientAddress.domain])
    t = translations.installTranslations(lang)

    bridges = None
    try:
        bridgeRequest = request.determineBridgeRequestOptions(lines)

        # The request was invalid, respond with a help email which explains
        # valid email commands:
        if not bridgeRequest.isValid():
            raise EmailRequestedHelp("Email request from %r was invalid."
                                     % clientAddr)

        # Otherwise they must have requested bridges:
        interval = context.schedule.getInterval(time.time())
        bridges = context.distributor.getBridgesForEmail(
            clientAddr,
            interval,
            context.nBridges,
            countryCode=None,
            bridgeFilterRules=bridgeRequest.filters)
    except EmailRequestedHelp as error:
        logging.info(error)
        return templates.buildWelcomeText(t, clientAddress)
    except EmailRequestedKey as error:
        logging.info(error)
        return templates.buildKeyMessage(t, clientAddress)
    except TooSoonEmail as error:
        logging.info("Got a mail too frequently: %s." % error)
        return templates.buildSpamWarning(t, clientAddress)
    except (IgnoreEmail, BadEmail) as error:
        logging.info(error)
        # Don't generate a response if their email address is unparsable or
        # invalid, or if we've already warned them about rate-limiting:
        return None
    else:
        answer = "(no bridges currently available)\r\n"
        if bridges:
            transport = bridgeRequest.justOnePTType()
            answer = "".join("  %s\r\n" % b.getConfigLine(
                includeFingerprint=context.includeFingerprints,
                addressClass=bridgeRequest.addressClass,
                transport=transport,
                request=clientAddr) for b in bridges)
        return templates.buildAnswerMessage(t, clientAddress, answer)
コード例 #4
0
 def test_templates_buildSpamWarning(self):
     text = templates.buildSpamWarning(self.t, self.client)
     self.shouldIncludeGreeting(text)
     self.shouldIncludeAutomationNotice(text)
     self.shouldIncludeFooter(text)