2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
4 import email
.FeedParser
6 from email
.MIMEText
import MIMEText
7 from email
.Utils
import parseaddr
19 FROM_ADDRESS
= "oc@hurts.ca"
20 SMTP_SERVER
= "127.0.0.1"
22 HELP_TEXT
= """Examples:
23 31 antares to 81 florence
24 stop 6097 to bayshore at 830 pm
25 merivale and baseline to 6097 by 9"""
28 syslog
.syslog(syslog
.LOG_INFO
, string
)
30 def sendMessage(msg
, to
):
31 msg
['From'] = FROM_ADDRESS
34 s
= smtplib
.SMTP(SMTP_SERVER
)
35 s
.sendmail(parseaddr(FROM_ADDRESS
)[1], [parseaddr(msg
['To'])[1]],
39 def sendError(e
, replyto
):
40 sendMessage(MIMEText(e
.args
[0]), replyto
)
42 def sendHelp(replyto
):
43 sendMessage(MIMEText(HELP_TEXT
), replyto
)
45 def findFirstLine(msg
):
46 for line
in msg
.get_payload(None, True).splitlines():
52 def handleMessage(msg
, replyto
):
56 # Look for a text/plain part.
57 for subpart
in email
.iterators
.typed_subpart_iterator(msg
, "text",
59 s
= findFirstLine(subpart
)
63 # No text/plain sub-parts, or the message body was empty
64 raise Exception("Your message was empty, or was not in plain "
66 + "For help, send the word 'help' by itself.\n")
69 logIt("Empty command from %s" % replyto
)
74 if s
.lower().startswith("help"):
78 cmd
= CommandParser
.CommandParser(s
).cmd
80 # Leave in 3 minutes if time is unspecified.
82 cmd
.time
= PlanTime
.PlanTime(time
.time() + 180,
83 PlanTime
.MUST_LEAVE_AFTER
)
85 itin
= Planner
.plan(cmd
.start
, cmd
.end
, cmd
.time
)
86 sf
= ShortFormatter
.ShortFormatter(itin
.entries
)
88 text
= "\n".join(sf
.lines
)
89 if itin
.anyUnparsed():
90 text
+= "\nWarning: This itinerary display may be incomplete!"
92 logIt("success, %d entries" % len(itin
.entries
))
93 sendMessage(MIMEText(text
), replyto
)
95 logIt("Exception: %s" % e
)
96 logIt(traceback
.format_exc())
99 # Always return success. Otherwise Postfix will send a bounce message.
103 fp
= email
.FeedParser
.FeedParser()
104 for line
in sys
.stdin
:
108 # if these headers are here, we can reply semi-intelligently with errors
109 replyto
= msg
["reply-to"] or msg
["from"]
110 return handleMessage(msg
, replyto
)
112 if __name__
== '__main__':
113 syslog
.openlog(sys
.argv
[0], syslog
.LOG_DAEMON
)