def test_policies(self): p1 = self.mox.CreateMock(QueuePolicy) p2 = self.mox.CreateMock(QueuePolicy) p1.apply(self.env) p2.apply(self.env) self.mox.ReplayAll() queue = Queue(self.store, self.relay) queue.add_policy(p1) queue.add_policy(p2) self.assertRaises(TypeError, queue.add_policy, None) queue._run_policies(self.env)
def test_policies(self): p1 = self.mox.CreateMock(QueuePolicy) p2 = self.mox.CreateMock(QueuePolicy) p1.apply(self.env) p2.apply(self.env) self.mox.ReplayAll() queue = Queue(self.store, self.relay) queue.add_policy(p1) queue.add_policy(p2) self.assertRaises(TypeError, queue.add_policy, None) queue._run_policies(self.env)
def test_enqueue_wait_splitpolicy(self): splitpolicy1 = self.mox.CreateMock(QueuePolicy) splitpolicy2 = self.mox.CreateMock(QueuePolicy) regpolicy = self.mox.CreateMock(QueuePolicy) env1 = Envelope('*****@*****.**', ['*****@*****.**']) env2 = Envelope('*****@*****.**', ['*****@*****.**']) env3 = Envelope('*****@*****.**', ['*****@*****.**']) splitpolicy1.apply(self.env).AndReturn([env1, env2]) regpolicy.apply(env1) splitpolicy2.apply(env1) regpolicy.apply(env2) splitpolicy2.apply(env2).AndReturn([env2, env3]) self.store.write(env1, IsA(float)).AndReturn('1234') self.store.write(env2, IsA(float)).AndReturn('5678') self.store.write(env3, IsA(float)).AndReturn('90AB') self.relay._attempt(env1, 0).InAnyOrder('relay') self.relay._attempt(env2, 0).InAnyOrder('relay') self.relay._attempt(env3, 0).InAnyOrder('relay') self.store.remove('1234').InAnyOrder('relay') self.store.remove('5678').InAnyOrder('relay') self.store.remove('90AB').InAnyOrder('relay') self.mox.ReplayAll() queue = Queue(self.store, self.relay, relay_pool=5) queue.add_policy(splitpolicy1) queue.add_policy(regpolicy) queue.add_policy(splitpolicy2) self.assertEqual([(env1, '1234'), (env2, '5678'), (env3, '90AB')], queue.enqueue(self.env)) queue.relay_pool.join()
def test_enqueue_wait_splitpolicy(self): splitpolicy1 = self.mox.CreateMock(QueuePolicy) splitpolicy2 = self.mox.CreateMock(QueuePolicy) regpolicy = self.mox.CreateMock(QueuePolicy) env1 = Envelope('*****@*****.**', ['*****@*****.**']) env2 = Envelope('*****@*****.**', ['*****@*****.**']) env3 = Envelope('*****@*****.**', ['*****@*****.**']) splitpolicy1.apply(self.env).AndReturn([env1, env2]) regpolicy.apply(env1) splitpolicy2.apply(env1) regpolicy.apply(env2) splitpolicy2.apply(env2).AndReturn([env2, env3]) self.store.write(env1, IsA(float)).AndReturn('1234') self.store.write(env2, IsA(float)).AndReturn('5678') self.store.write(env3, IsA(float)).AndReturn('90AB') self.relay._attempt(env1, 0).InAnyOrder('relay') self.relay._attempt(env2, 0).InAnyOrder('relay') self.relay._attempt(env3, 0).InAnyOrder('relay') self.store.remove('1234').InAnyOrder('relay') self.store.remove('5678').InAnyOrder('relay') self.store.remove('90AB').InAnyOrder('relay') self.mox.ReplayAll() queue = Queue(self.store, self.relay, relay_pool=5) queue.add_policy(splitpolicy1) queue.add_policy(regpolicy) queue.add_policy(splitpolicy2) self.assertEqual([(env1, '1234'), (env2, '5678'), (env3, '90AB')], queue.enqueue(self.env)) queue.relay_pool.join()
class MDA(MTA): def __init__(self, msa, mail_list_url, mda_domain, list_subject_prefix=None): self.msa = msa # Relay: relay = DovecotLdaRelay(config['LDA']['dovecot_path'], timeout=10.0) # Queue: #env_db = shelve.open('envelope') #meta_db = shelve.open('meta') #storage = DictStorage(env_db, meta_db) # !!! replace with DiskStorage! (installed via pip install python-slimta-diskstorage) storage = DiskStorage(config['MDA']['ds_env'], config['MDA']['ds_meta']) self.queue = Queue( storage, relay) # no backoff - just fail local delivery immediately self.queue.start() # Headers: self.queue.add_policy(AddDateHeader()) self.queue.add_policy(AddMessageIdHeader()) self.queue.add_policy(AddReceivedHeader()) # Mailing List: self.queue.add_policy( MailingListDistribution(self.msa, mail_list_url, mda_domain, list_subject_prefix)) # SpamAssassin: #self.queue.add_policy(SpamAssassin()) # Edge: #tls_args = {'keyfile': '/home/jmcaine/dev/temp/slimta/tls/key.pem', 'certfile': '/home/jmcaine/dev/temp/slimta/tls/certificate.pem'} -- gone, see https://docs.slimta.org/en/latest/blog/2016-11-14.html ssl = SSLContext(PROTOCOL_SSLv23) ssl.load_cert_chain(config['SSL']['certificate_path'], config['SSL']['key_path']) self.edge = SmtpEdge(('0.0.0.0', 25), self.queue, validator_class=MDA_Validators, hostname=mda_domain, context=ssl) self.edge.start()
def _start_outbound_queue(args, relay, inbound_queue): from slimta.queue.dict import DictStorage from slimta.queue import Queue from slimta.policy.headers import AddDateHeader, \ AddMessageIdHeader, AddReceivedHeader from slimta.policy.split import RecipientDomainSplit envelope_db = {} meta_db = {} storage = DictStorage(envelope_db, meta_db) queue = Queue(storage, relay, bounce_queue=inbound_queue) queue.start() queue.add_policy(AddDateHeader()) queue.add_policy(AddMessageIdHeader()) queue.add_policy(AddReceivedHeader()) queue.add_policy(RecipientDomainSplit()) return queue
def _start_outbound_queue(args, relay, inbound_queue): from slimta.queue.dict import DictStorage from slimta.queue import Queue from slimta.policy.headers import AddDateHeader, \ AddMessageIdHeader, AddReceivedHeader from slimta.policy.split import RecipientDomainSplit envelope_db = {} meta_db = {} storage = DictStorage(envelope_db, meta_db) queue = Queue(storage, relay, bounce_queue=inbound_queue) queue.start() queue.add_policy(AddDateHeader()) queue.add_policy(AddMessageIdHeader()) queue.add_policy(AddReceivedHeader()) queue.add_policy(RecipientDomainSplit()) return queue
def _start_inbound_queue(args, relay): from slimta.queue.dict import DictStorage from slimta.queue import Queue from slimta.policy.headers import AddDateHeader, \ AddMessageIdHeader, AddReceivedHeader from slimta.policy.spamassassin import SpamAssassin import shelve envelope_db = shelve.open(args.envelope_db) meta_db = shelve.open(args.meta_db) storage = DictStorage(envelope_db, meta_db) queue = Queue(storage, relay) queue.start() queue.add_policy(AddDateHeader()) queue.add_policy(AddMessageIdHeader()) queue.add_policy(AddReceivedHeader()) queue.add_policy(SpamAssassin()) return queue
def _start_inbound_queue(args, relay): from slimta.queue.dict import DictStorage from slimta.queue import Queue from slimta.policy.headers import AddDateHeader, \ AddMessageIdHeader, AddReceivedHeader from slimta.policy.spamassassin import SpamAssassin envelope_db = {} meta_db = {} storage = DictStorage(envelope_db, meta_db) queue = Queue(storage, relay) queue.start() queue.add_policy(AddDateHeader()) queue.add_policy(AddMessageIdHeader()) queue.add_policy(AddReceivedHeader()) if args.spamassassin: queue.add_policy(SpamAssassin()) return queue
class MSA(MTA): def __init__(self): # Relay: ssl = SSLContext(PROTOCOL_SSLv23) ssl.load_cert_chain(config['SSL']['certificate_path'], config['SSL']['key_path']) self.relay = MxSmtpRelay(context=ssl, connect_timeout=20, command_timeout=10, data_timeout=20, idle_timeout=30) # Queue: #env_db = shelve.open('msa_envelope') #meta_db = shelve.open('msa_meta') #storage = DictStorage(env_db, meta_db) # !!! replace with DiskStorage! (installed via pip install python-slimta-diskstorage) storage = DiskStorage(config['MSA']['ds_env'], config['MSA']['ds_meta']) def retry_backoff(envelope, attempts): if attempts < 10: return 60 * attempts * attempts # try again at increasingly long intervals; give up after 10 tries (100 minutes) return None self.queue = Queue(storage, self.relay, backoff=retry_backoff) self.queue.start() # Headers: self.queue.add_policy(AddDateHeader()) self.queue.add_policy(AddMessageIdHeader()) self.queue.add_policy(AddReceivedHeader()) self.queue.add_policy(RecipientDomainSplit()) # !!! Add Forward policy here, to manage general forwarding (but not list distribution - do that in mda!) # Edge: self.edge = SmtpEdge( ('localhost', 587), self.queue, auth=False ) #, auth=True, validator_class=MSA_Validators) # ?!!! context=ssl, tls_immediately=True, self.edge.start()