def test_oversize_protections():
    # first just make an oversize limited queue
    overq = queue.Queue("run/queue", pop_limit=10)
    overq.clear()

    for i in range(5):
        overq.push("HELLO" * 100)

    assert_equal(overq.count(), 5)

    key, msg = overq.pop()

    assert not key and not msg, "Should get no messages."
    assert_equal(overq.count(), 0)

    # now make sure that oversize mail is moved to the overq
    setup()
    overq = queue.Queue("run/queue",
                        pop_limit=10,
                        oversize_dir="run/big_queue")
    moveq = queue.Queue("run/big_queue")

    for i in range(5):
        overq.push("HELLO" * 100)

    key, msg = overq.pop()

    assert not key and not msg, "Should get no messages."
    assert_equal(overq.count(), 0)
    assert_equal(moveq.count(), 5)

    moveq.clear()
    overq.clear()
Esempio n. 2
0
def queue_command(pop=False,
                  get=False,
                  keys=False,
                  remove=False,
                  count=False,
                  clear=False,
                  name="run/queue"):
    """
    Let's you do most of the operations available to a queue.

    lamson queue (-pop | -get | -remove | -count | -clear | -keys) -name run/queue
    """
    print "Using queue: %r" % name

    inq = queue.Queue(name)

    if pop:
        key, msg = inq.pop()
        if key:
            print "KEY: ", key
            print msg
    elif get:
        print inq.get(get)
    elif remove:
        inq.remove(remove)
    elif count:
        print "Queue %s contains %d messages" % (name, inq.count())
    elif clear:
        inq.clear()
    elif keys:
        print "\n".join(inq.keys())
    else:
        print "Give something to do.  Try lamson help -for queue to find out what."
        sys.exit(1)
        return  # for unit tests mocking sys.exit
Esempio n. 3
0
    def start(self, one_shot=False):
        """
        Start simply loops indefinitely sleeping and pulling messages
        off for processing when they are available.

        If you give one_shot=True it will run once rather than do a big
        while loop with a sleep.
        """

        logging.info("Queue receiver started on queue dir %s" %
                     (self.queue_dir))
        logging.debug("Sleeping for %d seconds..." % self.sleep)

        inq = queue.Queue(self.queue_dir)

        while True:
            keys = inq.keys()

            for key in keys:
                msg = inq.get(key)

                if msg:
                    logging.debug("Pulled message with key: %r off", key)
                    self.process_message(msg)
                    logging.debug("Removed %r key from queue.", key)

                inq.remove(key)

            if one_shot:
                return
            else:
                time.sleep(self.sleep)
Esempio n. 4
0
def POSTING(message, post_name=None, host=None, action=None):
    name, address = parseaddr(message['from'])

    if not action:
        post.post(post_name, address, host, message)
        msg = view.respond(locals(),
                           'mail/page_ready.msg',
                           From="noreply@%(host)s",
                           To=message['from'],
                           Subject="Your page '%(post_name)s' is ready.")
        relay.deliver(msg)

        # first real message, now we can index it
        index_q = queue.Queue("run/posts")
        index_q.push(message)
    elif action == "delete":
        post.delete(post_name, address)

        msg = view.respond(locals(),
                           'mail/deleted.msg',
                           From="noreply@%(host)s",
                           To=message['from'],
                           Subject="Your page '%(post_name)s' was deleted.")

        relay.deliver(msg)
    else:
        logging.debug("Invalid action: %r", action)

    return POSTING
Esempio n. 5
0
def START(message, to=None, host=None):
    """
    @stateless and routes however handlers.log.START routes (everything).
    Has @nolocking, but that's alright since it's just writing to a maildir.
    """
    q = queue.Queue('run/queue')
    q.push(message)
Esempio n. 6
0
 def __init__(self, queue_dir, sleep=10, size_limit=0, oversize_dir=None):
     """
     The router should be fully configured and ready to work, the
     queue_dir can be a fully qualified path or relative.
     """
     self.queue = queue.Queue(queue_dir, pop_limit=size_limit,
                              oversize_dir=oversize_dir)
     self.queue_dir = queue_dir
     self.sleep = sleep
Esempio n. 7
0
def enqueue(list_name, message):
    qpath = store_path(list_name, 'queue')
    pending = queue.Queue(qpath, safe=True)
    white_list_cleanse(message)

    key = pending.push(message)
    fix_permissions(qpath)

    update_json(list_name, key, message)
    return key
Esempio n. 8
0
def test_queue_receiver():
    receiver = server.QueueReceiver('run/queue')
    run_queue = queue.Queue('run/queue')
    run_queue.push(str(test_mail_response_plain_text()))
    assert run_queue.count() > 0
    receiver.start(one_shot=True)
    assert run_queue.count() == 0

    routing.Router.deliver.side_effect = raises_exception
    receiver.process_message(mail.MailRequest('localhost', 'test@localhost',
                                              'test@localhost', 'Fake body.'))
Esempio n. 9
0
    def __init__(self, pending_queue, storage):
        """
        The pending_queue should be a string with the path to the lamson.queue.Queue 
        that will store pending messages.  These messages are the originals the user
        sent when they tried to confirm.

        Storage should be something that is like ConfirmationStorage so that this
        can store things for later verification.
        """
        self.pending = queue.Queue(pending_queue)
        self.storage = storage
Esempio n. 10
0
 def START(message, to=None, host=None):
     q = queue.Queue(spampot.pathOfQueue)
     '''
 lamson has been found missing delivering mails to recipients in 'cc' and 'bcc'.
 Upto this point, lamson perfectly determines recipients combining 'to' and 'host' variables but always pushes 'message' in queue.
 The issue is, 'message' contains just the original 'To' recipent. Hence, though lamson successfully determines 'cc'/'bcc', the 'message' misses that.
 Using following dirty regex, 'To' field is replaced with next 'cc'/'bcc' recipient with each iteration.
 If, including 'To', there are 4 recipient in 'cc'/'bcc', total 5 mails would be pushed in queue.
 
 '''
     email = "%s@%s" % (to, host)
     message = str(message).replace("%", "%%")
     new_msg = re.sub(r'(?m)^\To:.*\n?', 'To: %s\n', message, 1) % (email, )
     q.push(new_msg)
def test_push():
    q = queue.Queue("run/queue", safe=USE_SAFE)
    q.clear()

    # the queue doesn't really care if its a request or response, as long
    # as the object answers to str(msg)
    msg = mail.MailResponse(To="test@localhost",
                            From="test@localhost",
                            Subject="Test",
                            Body="Test")
    key = q.push(msg)
    assert key, "Didn't get a key for test_get push."

    return q
Esempio n. 12
0
def test_ConfirmationEngine_send():
    queue.Queue('run/queue').clear()
    engine.clear()

    list_name = 'testing'
    action = 'subscribing to'
    host = 'localhost'

    message = mail.MailRequest('fakepeer', 'somedude@localhost',
                               'testing-subscribe@localhost', 'Fake body.')

    engine.send(relay(port=8899), 'testing', message, 'confirmation.msg',
                locals())

    confirm = delivered('confirm')
    assert delivered('somedude', to_queue=engine.pending)
    assert confirm

    return confirm
Esempio n. 13
0
def _settings_loader():
    from lamson.routing import Router
    from lamson.server import Relay, SMTPReceiver
    from lamson import view, queue
    import logging
    import logging.config
    import jinja2

    settings = Settings()
    for attr_name in dir(django_settings):
        if attr_name.startswith("LAMSON_"):
            setattr(settings,
                    attr_name.split("LAMSON_")[1].lower(),
                    getattr(django_settings, attr_name))

    #logging.config.fileConfig("logging.conf")

    # the relay host to actually send the final message to
    #TODO make debug a parameter to the command
    if hasattr(settings, 'relay_config'):
        settings.relay = Relay(host=settings.relay_config['host'],
                               port=settings.relay_config['port'],
                               debug=1)

    # where to listen for incoming messages
    settings.receiver = SMTPReceiver(settings.receiver_config['host'],
                                     settings.receiver_config['port'])

    Router.defaults(**settings.router_defaults)
    Router.load(settings.handlers)
    #TODO make this a parameter to the command
    Router.RELOAD = True
    #TODO make run a parameter to the command
    Router.UNDELIVERABLE_QUEUE = queue.Queue("run/undeliverable")

    if hasattr(settings, 'template_config'):
        view.LOADER = jinja2.Environment(
            loader=jinja2.PackageLoader(settings.template_config['dir'],
                                        settings.template_config['module']))

    return settings
Esempio n. 14
0
 def enqueue_as_spam(self, message):
     """Drops the message into the configured spam queue."""
     outq = queue.Queue(self.spam_queue)
     outq.push(str(message))
Esempio n. 15
0
def SPAMMING(message, **options):
    spam = queue.Queue(SPAM['queue'])
    spam.push(message)
    return SPAMMING
Esempio n. 16
0
def archive_bounce(message):
    qu = queue.Queue(settings.BOUNCE_ARCHIVE)
    qu.push(message)
Esempio n. 17
0
from config import settings
from lamson.routing import Router
from lamson.server import Relay, SMTPReceiver, QueueReceiver
from lamson import view, queue
import logging
import logging.config
import jinja2

logging.config.fileConfig("config/logging.conf")

# the relay host to actually send the final message to
settings.relay = Relay(host=settings.relay_config['host'],
                       port=settings.relay_config['port'],
                       debug=1)

# where to listen for incoming messages
#settings.receiver = SMTPReceiver(settings.receiver_config['host'],
#                                 settings.receiver_config['port'])

# b0nd - include the maildir option we've set in settings.py
settings.receiver = QueueReceiver(settings.receiver_config['maildir'])

Router.defaults(**settings.router_defaults)
Router.load(settings.handlers)
Router.RELOAD = True
Router.UNDELIVERABLE_QUEUE = queue.Queue("run/undeliverable")

view.LOADER = jinja2.Environment(loader=jinja2.PackageLoader(
    settings.template_config['dir'], settings.template_config['module']))
def get_user_post_queue(user_dir):
    queue_dir = "%s/posts_queue" % (user_dir)
    return queue.Queue(queue_dir)
Esempio n. 19
0
def IGNORE_BOUNCE(message):
    bounces = queue.Queue(BOUNCES)
    bounces.push(message)
    return START
Esempio n. 20
0
def defer_to_queue(message):
    index_q = queue.Queue("run/posts")  # use a diff queue?
    index_q.push(message)
    print "run/posts count after dever", index_q.count()