Ticket #8285: 8285.patch

File 8285.patch, 2.6 KB (added by sukhbir, 7 years ago)

patch

  • facilitator/facilitator-email-poller

    From df60528576963adca9924c4bb08bc42dfed73f6f Mon Sep 17 00:00:00 2001
    From: Sukhbir Singh <sukhbir.in@gmail.com>
    Date: Sun, 24 Mar 2013 18:01:46 -0400
    Subject: [PATCH] Don't process registrations that are too old (fixes #8285)
    
    ---
     facilitator/facilitator-email-poller |   26 +++++++++++++++++++++++++-
     1 files changed, 25 insertions(+), 1 deletions(-)
    
    diff --git a/facilitator/facilitator-email-poller b/facilitator/facilitator-email-poller
    index e2a794e..358f768 100755
    a b import stat 
    1212import sys
    1313import tempfile
    1414import time
     15import datetime
    1516
    1617import fac
    1718
    1819from hashlib import sha1
    1920from M2Crypto import X509
    2021
     22from dateutil import parser
     23from dateutil import tz
     24
    2125DEFAULT_IMAP_HOST = "imap.gmail.com"
    2226DEFAULT_IMAP_PORT = 993
    2327DEFAULT_EMAIL_ADDRESS = "flashproxyreg.a@gmail.com"
    DEFAULT_LOG_FILENAME = "facilitator-email-poller.log" 
    2529
    2630POLL_INTERVAL = 60
    2731
     32REGISTRATION_LIMIT = 30
     33
    2834FACILITATOR_ADDR = ("127.0.0.1", 9002)
    2935
    3036# We trust no other CA certificate than this.
    def imap_get_uid(imap, index): 
    232238        raise imaplib.IMAP4.abort("Can't find UID in %s" % repr(data[0]))
    233239    return m.group(1)
    234240
     241def accept_message(header):
     242    """Check if a message is newer than REGISTRATION_LIMIT."""
     243    # Convert the header to UTC for comparison.
     244    message_time = parser.parse(header.splitlines()[-1]).astimezone(tz.tzutc())
     245    # Get the current time, also in UTC.
     246    current_time = datetime.datetime.now(tz.tzutc())
     247
     248    time_offset = (current_time - message_time).seconds / 60
     249    # If the message is newer than the limit, return True.
     250    if time_offset < REGISTRATION_LIMIT:
     251        return True
     252    else:
     253        return False
     254
    235255# Gmail's IMAP folders are funny: they are not real folders, but actually views
    236256# of messages having a particular label. INBOX consists of messages having the
    237257# INBOX label, for example. Deleting a message from a folder just removes its
    def imap_loop(imap): 
    271291
    272292                try:
    273293                    msg = email.message_from_string(msg_text)
    274                     handle_message(msg)
     294                    received_header = msg["X-Received"]
     295                    check_message = accept_message(received_header)
     296                    # The message is new, so let's handle it.
     297                    if check_message:
     298                        handle_message(msg)
    275299                except Exception, e:
    276300                    log("Error processing message, deleting anyway: %s" % str(e))
    277301