1.1 --- a/imiptools/__init__.py Wed Oct 22 15:46:50 2014 +0200
1.2 +++ b/imiptools/__init__.py Thu Oct 23 22:24:17 2014 +0200
1.3 @@ -3,7 +3,7 @@
1.4 from email import message_from_file
1.5 from email.mime.multipart import MIMEMultipart
1.6 from email.mime.text import MIMEText
1.7 -from smtplib import SMTP
1.8 +from smtplib import LMTP, SMTP
1.9 from imiptools.content import handle_itip_part
1.10 import sys
1.11
1.12 @@ -28,8 +28,11 @@
1.13
1.14 # Sending of outgoing messages.
1.15
1.16 -def sendmail(sender, recipients, data):
1.17 - smtp = SMTP("localhost")
1.18 +def sendmail(sender, recipients, data, lmtp_socket=None):
1.19 + if lmtp_socket:
1.20 + smtp = LMTP(lmtp_socket)
1.21 + else:
1.22 + smtp = SMTP("localhost")
1.23 smtp.sendmail(sender, recipients, data)
1.24 smtp.quit()
1.25
1.26 @@ -50,6 +53,7 @@
1.27 self.sender = sender or MESSAGE_SENDER
1.28 self.subject = subject or MESSAGE_SUBJECT
1.29 self.body_text = body_text or MESSAGE_TEXT
1.30 + self.lmtp_socket = None
1.31
1.32 def process(self, f, original_recipients, recipients):
1.33
1.34 @@ -64,25 +68,55 @@
1.35
1.36 # Handle messages with iTIP parts.
1.37
1.38 - all_parts = []
1.39 + all_responses = []
1.40 + handled = False
1.41
1.42 for part in msg.walk():
1.43 if part.get_content_type() in itip_content_types and \
1.44 part.get_param("method"):
1.45
1.46 - all_parts += handle_itip_part(part, original_recipients, self.handlers)
1.47 + all_responses += handle_itip_part(part, original_recipients, self.handlers)
1.48 + handled = True
1.49
1.50 # Pack any returned parts into a single message.
1.51
1.52 - if all_parts:
1.53 - message = self.make_message(all_parts, senders)
1.54 + if all_responses:
1.55 + outgoing_parts = []
1.56 + forwarded_parts = []
1.57 +
1.58 + for outgoing, part in all_responses:
1.59 + if outgoing:
1.60 + outgoing_parts.append(part)
1.61 + else:
1.62 + forwarded_parts.append(part)
1.63 +
1.64 + # Reply using any outgoing parts in a new message.
1.65 +
1.66 + if outgoing_parts:
1.67 + message = self.make_message(outgoing_parts, senders)
1.68
1.69 + if "-d" in sys.argv:
1.70 + print message
1.71 + else:
1.72 + sendmail(self.sender, senders, message.as_string())
1.73 +
1.74 + # Forward messages to their recipients using the existing message.
1.75 +
1.76 + if forwarded_parts:
1.77 + msg.get_payload()[:0] = forwarded_parts
1.78 +
1.79 + if "-d" in sys.argv:
1.80 + print msg
1.81 + elif self.lmtp_socket:
1.82 + sendmail(self.sender, original_recipients, msg.as_string(), self.lmtp_socket)
1.83 +
1.84 + # Unhandled messages are delivered as they are.
1.85 +
1.86 + if not handled:
1.87 if "-d" in sys.argv:
1.88 - print message
1.89 - else:
1.90 - sendmail(self.sender, senders, message.as_string())
1.91 -
1.92 - # Forward messages to their recipients.
1.93 + print msg
1.94 + elif self.lmtp_socket:
1.95 + sendmail(self.sender, original_recipients, msg.as_string(), self.lmtp_socket)
1.96
1.97 def make_message(self, parts, recipients):
1.98
1.99 @@ -112,6 +146,7 @@
1.100 original_recipients = []
1.101 recipients = []
1.102 senders = []
1.103 + lmtp = []
1.104
1.105 l = []
1.106
1.107 @@ -129,6 +164,11 @@
1.108 elif arg == "-s":
1.109 l = senders
1.110
1.111 + # Switch to getting the LMTP socket.
1.112 +
1.113 + elif arg == "-l":
1.114 + l = lmtp
1.115 +
1.116 # Ignore debugging options.
1.117
1.118 elif arg == "-d":
1.119 @@ -137,6 +177,7 @@
1.120 l.append(arg)
1.121
1.122 self.sender = senders and senders[0] or self.sender
1.123 + self.lmtp_socket = lmtp and lmtp[0] or None
1.124 self.process(stream, original_recipients, recipients)
1.125
1.126 def __call__(self):
1.127 @@ -156,6 +197,8 @@
1.128 except SystemExit, value:
1.129 sys.exit(value)
1.130 except Exception, exc:
1.131 + if "-v" in args:
1.132 + raise
1.133 type, value, tb = sys.exc_info()
1.134 print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno)
1.135 sys.exit(EX_TEMPFAIL)