2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
4 import email
.FeedParser
5 from email
.MIMEText
import MIMEText
6 from email
.Utils
import parseaddr
18 FROM_ADDRESS
= "tp@hurts.ca"
19 SMTP_SERVER
= "127.0.0.1"
22 syslog
.syslog(syslog
.LOG_INFO
, string
)
24 def sendMessage(msg
, to
):
25 msg
['From'] = FROM_ADDRESS
28 s
= smtplib
.SMTP(SMTP_SERVER
)
29 s
.sendmail(parseaddr(FROM_ADDRESS
)[1], [parseaddr(msg
['To'])[1]],
33 def sendError(e
, replyto
):
34 sendMessage(MIMEText(e
.args
[0]), replyto
)
36 def handleMessage(msg
, replyto
):
38 if msg
.is_multipart():
39 raise Exception("Sorry, fancy multipart messages aren't supported.")
41 for line
in msg
.get_payload().splitlines():
45 cmd
= CommandParser
.CommandParser(s
).cmd
49 logIt("Empty command from %s" % replyto
)
52 # Leave in 3 minutes if time is unspecified.
54 cmd
.time
= PlanTime
.PlanTime(time
.time() + 180,
55 PlanTime
.MUST_LEAVE_AFTER
)
57 itin
= Planner
.plan(cmd
.start
, cmd
.end
, cmd
.time
)
58 sf
= ShortFormatter
.ShortFormatter(itin
.entries
)
60 text
= "\n".join(sf
.lines
)
61 if itin
.anyUnparsed():
62 text
+= "\nWarning: This itinerary display may be incomplete!"
64 logIt("%s: success, %d entries" % (s
, len(itin
.entries
)))
65 sendMessage(MIMEText(text
), replyto
)
67 logIt("Exception: %s" % e
)
68 logIt(traceback
.format_exc())
71 # Always return success. Otherwise Postfix will send a bounce message.
75 fp
= email
.FeedParser
.FeedParser()
76 for line
in sys
.stdin
:
80 # if these headers are here, we can reply semi-intelligently with errors
81 replyto
= msg
["reply-to"] or msg
["from"]
82 return handleMessage(msg
, replyto
)
84 if __name__
== '__main__':
85 syslog
.openlog(sys
.argv
[0], syslog
.LOG_DAEMON
)