| 54 | | """This is the actual mailsink server which stores all received messages in |
| 55 | | an internal queue. Do not access the queue directly. All accessor methods |
| 56 | | in this object are sufficiently guarded against race conditions.""" |
| 57 | | |
| 58 | | def __init__( self, *args, **kwargs): |
| 59 | | smtpd.SMTPServer.__init__( self, *args, **kwargs ) |
| 60 | | self.queued_mails = [] |
| 61 | | self.lock = threading.Lock() |
| | 54 | """This is the actual mailsink server which stores all received messages in |
| | 55 | an internal queue. Do not access the queue directly. All accessor methods |
| | 56 | in this object are sufficiently guarded against race conditions.""" |
| 63 | | def has_message(self): |
| 64 | | """Return True if at least one message was received successfully. The |
| 65 | | access to the internal queue is synchronized so the caller will be |
| 66 | | blocked until the necessary lock was aquired.""" |
| 67 | | self.lock.acquire() |
| 68 | | number_messages = len(self.queued_mails) |
| 69 | | self.lock.release() |
| 70 | | return number_messages > 0 |
| | 58 | def __init__( self, *args, **kwargs): |
| | 59 | smtpd.SMTPServer.__init__( self, *args, **kwargs ) |
| | 60 | self.queued_mails = [] |
| | 61 | self.lock = threading.Lock() |
| 72 | | def get_messages(self): |
| 73 | | """Return a copy of the internal queue with all received messages. The |
| 74 | | access to the internal queue is synchronized so the caller will be |
| 75 | | blocked until the necessary lock was aquired.""" |
| 76 | | self.lock.acquire() |
| 77 | | messages = copy.copy(self.queued_mails) |
| 78 | | self.lock.release() |
| 79 | | return messages |
| | 63 | def has_message(self): |
| | 64 | """Return True if at least one message was received successfully. The |
| | 65 | access to the internal queue is synchronized so the caller will be |
| | 66 | blocked until the necessary lock was aquired.""" |
| | 67 | self.lock.acquire() |
| | 68 | number_messages = len(self.queued_mails) |
| | 69 | self.lock.release() |
| | 70 | return number_messages > 0 |
| 81 | | def pop(self, index=None): |
| 82 | | """Return the index'th message in the queue (default=last) which is |
| 83 | | removed from the queue afterwards. Throws IndexError if index is bigger |
| 84 | | than the number of messages in the queue.""" |
| 85 | | item = None |
| 86 | | self.lock.acquire() |
| 87 | | if index == None: |
| 88 | | index = len(self.queued_mails) - 1 |
| 89 | | try: |
| 90 | | item = self.queued_mails.pop(index) |
| 91 | | except Exception: |
| 92 | | print 'pop: before lock release' |
| 93 | | self.lock.release() |
| 94 | | raise |
| 95 | | self.lock.release() |
| 96 | | return item |
| | 72 | def get_messages(self): |
| | 73 | """Return a copy of the internal queue with all received messages. The |
| | 74 | access to the internal queue is synchronized so the caller will be |
| | 75 | blocked until the necessary lock was aquired.""" |
| | 76 | self.lock.acquire() |
| | 77 | messages = copy.copy(self.queued_mails) |
| | 78 | self.lock.release() |
| | 79 | return messages |
| | 80 | |
| | 81 | def pop(self, index=None): |
| | 82 | """Return the index'th message in the queue (default=last) which is |
| | 83 | removed from the queue afterwards. Throws IndexError if index is bigger |
| | 84 | than the number of messages in the queue.""" |
| | 85 | item = None |
| | 86 | self.lock.acquire() |
| | 87 | if index == None: |
| | 88 | index = len(self.queued_mails) - 1 |
| | 89 | try: |
| | 90 | item = self.queued_mails.pop(index) |
| | 91 | except Exception: |
| | 92 | print 'pop: before lock release' |
| | 93 | self.lock.release() |
| | 94 | raise |
| | 95 | self.lock.release() |
| | 96 | return item |
| 99 | | def process_message(self, peer, mailfrom, rcpttos, data): |
| 100 | | "Store a received message in the internal queue. For internal use only!" |
| 101 | | msg = {'client': peer, 'from': mailfrom, 'recipients': rcpttos, |
| 102 | | 'mail': data} |
| 103 | | self.lock.acquire() |
| 104 | | self.queued_mails.append(msg) |
| 105 | | self.lock.release() |
| | 99 | def process_message(self, peer, mailfrom, rcpttos, data): |
| | 100 | "Store a received message in the internal queue. For internal use only!" |
| | 101 | msg = {'client': peer, 'from': mailfrom, 'recipients': rcpttos, |
| | 102 | 'mail': data} |
| | 103 | self.lock.acquire() |
| | 104 | self.queued_mails.append(msg) |
| | 105 | self.lock.release() |
| 112 | | def __init__(self, host='localhost', port=25): |
| 113 | | threading.Thread.__init__(self) |
| 114 | | self.stop_event = threading.Event() |
| 115 | | self.server = SMTPMailsinkServer((host, port), None) |
| | 112 | def __init__(self, host='localhost', port=25): |
| | 113 | threading.Thread.__init__(self) |
| | 114 | self.stop_event = threading.Event() |
| | 115 | self.server = SMTPMailsinkServer((host, port), None) |
| 126 | | def stop(self, timeout_seconds=5.0): |
| 127 | | """Stop the mailsink and shut down this thread. timeout_seconds |
| 128 | | specifies how long the caller should wait for the mailsink server to |
| 129 | | close down (default: 5 seconds). If the server did not stop in time, a |
| 130 | | warning message is printed.""" |
| 131 | | self.stop_event.set() |
| 132 | | self.server.close() |
| 133 | | threading.Thread.join(self, timeout=timeout_seconds) |
| 134 | | if self.isAlive(): |
| 135 | | print "WARNING: Thread still alive. Timeout while waiting for " + \ |
| 136 | | "termination!" |
| 137 | | |
| 138 | | def has_message(self): |
| 139 | | "Return True if at least one message was received successfully." |
| 140 | | return self.server.has_message() |
| 141 | | |
| 142 | | def get_messages(self): |
| 143 | | "Return a copy of the internal queue with all received messages." |
| 144 | | return self.server.get_messages() |
| | 126 | def stop(self, timeout_seconds=5.0): |
| | 127 | """Stop the mailsink and shut down this thread. timeout_seconds |
| | 128 | specifies how long the caller should wait for the mailsink server to |
| | 129 | close down (default: 5 seconds). If the server did not stop in time, a |
| | 130 | warning message is printed.""" |
| | 131 | self.stop_event.set() |
| | 132 | self.server.close() |
| | 133 | threading.Thread.join(self, timeout=timeout_seconds) |
| | 134 | if self.isAlive(): |
| | 135 | print "WARNING: Thread still alive. Timeout while waiting for " + \ |
| | 136 | "termination!" |
| 146 | | def pop(self, index=None): |
| 147 | | """Return the index'th message in the queue (default=last) which is |
| 148 | | removed from the queue afterwards. Throws IndexError if index is bigger |
| 149 | | than the number of messages in the queue.""" |
| 150 | | return self.server.pop(index) |
| | 138 | def has_message(self): |
| | 139 | "Return True if at least one message was received successfully." |
| | 140 | return self.server.has_message() |
| | 141 | |
| | 142 | def get_messages(self): |
| | 143 | "Return a copy of the internal queue with all received messages." |
| | 144 | return self.server.get_messages() |
| | 145 | |
| | 146 | def pop(self, index=None): |
| | 147 | """Return the index'th message in the queue (default=last) which is |
| | 148 | removed from the queue afterwards. Throws IndexError if index is bigger |
| | 149 | than the number of messages in the queue.""" |
| | 150 | return self.server.pop(index) |