forked from jrk/remindify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handle_new_email.py
79 lines (65 loc) · 2.78 KB
/
handle_new_email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import logging, email
from google.appengine.ext import webapp
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail
from google.appengine.api.urlfetch import fetch
from models import *
def get_word(text):
words = text.split()
characters = -1
for word in words:
characters += len(word)
if characters >= 0:
return word
class NewPingHandler(InboundMailHandler):
def receive(self, msg):
logging.info( "New handler received a message from %s at %s" % ( msg.sender, msg.date ) )
acct = account_for_sender( msg.sender )
# Unauthorized
if not acct:
logging.info( "Sender %s unauthorized. Dropping." % msg.sender )
mail.send_mail( sender=from_field( 'p' ), to=msg.sender,
subject='Re: '+msg.subject,
body="I don't know you." )
return
candidates = [ msg.subject ]
#try:
# candidates.extend( msg.bodies( content_type='text/plain' )[0].splitlines() )
#except:
# logging.error( 'Found no body of type text/plain' )
# Extract all the non-nil matches
#matches = [ date_string_re.match( c ) for c in candidates ]
#cmds = [ m.groups() for m in filter( lambda m: m, matches) ]
cmds = candidates
if not cmds:
logging.info( "Message included no recognized command strings" )
return
failedCmds = []
for cmd in cmds:
# try agenda first
if cmd == "!agenda":
# Send all reminders
send_agenda( msg, acct.tz, acct.user )
elif cmd == "!today":
# Send reminders scheduled for today
send_agenda_today( msg, acct.tz, acct.user )
else:
# if failed, try reminder
try:
reminder = create_reminder( cmd, acct.tz, acct.user )
if not reminder:
failedCmds.append( cmd )
except Exception, e:
logging.info( 'Creating reminder failed: "%s"' % (str(e)) )
if failedCmds:
errMsg = 'I failed to parse the following commands:\n\n%s' % '\n\n'.join( failedCmds )
logging.error( 'Replying: ' + errMsg )
mail.send_mail( sender=from_field( 'p' ), to=msg.sender,
subject='Re: '+msg.subject,
body=errMsg)
def main():
application = webapp.WSGIApplication([NewPingHandler.mapping()], debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
main()