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
17 FROM_ADDRESS
= "tp@hurts.ca"
18 LOG_FILE
= "/home/tp/planner.log"
21 fp
= open(LOG_FILE
, "a")
24 def sendMessage(msg
, to
):
25 msg
['From'] = FROM_ADDRESS
28 s
= smtplib
.SMTP("10.42.42.1")
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
51 # Leave in 3 minutes if time is unspecified.
53 cmd
.time
= PlanTime
.PlanTime(time
.time() + 180,
54 PlanTime
.MUST_LEAVE_AFTER
)
56 itin
= Planner
.plan(cmd
.start
, cmd
.end
, cmd
.time
)
57 sf
= ShortFormatter
.ShortFormatter(itin
.entries
)
59 sendMessage(MIMEText("\n".join(sf
.lines
)), replyto
)
64 # Always return success. Otherwise Postfix will send a bounce message.
71 fp
= email
.FeedParser
.FeedParser()
72 for line
in sys
.stdin
:
76 # if these headers are here, we can reply semi-intelligently with errors
77 replyto
= msg
["reply-to"] or msg
["from"]
78 return handleMessage(msg
, replyto
)
80 if __name__
== '__main__':