import os
+import sys
from smtplib import SMTP_SSL
import email
import datetime
from imapclient import IMAPClient, SEEN
from dateutil.parser import parse
from cron_descriptor import get_description
+from croniter import croniter
IMAP_USER = os.environ["REMINDME_IMAP_USER"]
USAGE = ""
-
__iclient = None
return make_reply(reminder, f"Received Reminder: {reminder['subject']}", reply_body)
-def remind(reminder):
+def remind(last_time, start_time, reminder):
f_line = reminder.get_payload().strip().splitlines()[0]
if f_line.lower().startswith("repeat"):
- return False, None
+ cron_payload = f_line[6:].strip()
+ it = croniter(cron_payload, last_time)
+ next_time = it.get_next(datetime.datetime)
+ if next_time > last_time and next_time <= start_time:
+ return (
+ True,
+ make_reply(
+ reminder, f"Re: {reminder['subject']} now", reminder.get_payload()
+ ),
+ )
+ else:
+ return False, None
else:
try:
parsed_time = parse(f_line, fuzzy=True)
)
-def main():
+def main(last_time, start_time):
msgs = iclient().search(["ALL"])
new_rems, old_rems, metas, to_delete, to_send = [], [], [], [], []
for uid, data in iclient().fetch(msgs, ["BODY.PEEK[]", "FLAGS"]).items():
to_send.append(ack_reminder(uid, reminder))
for uid, reminder in old_rems + new_rems:
- done, reply = remind(reminder)
+ done, reply = remind(last_time, start_time, reminder)
if reply:
to_send.append(reply)
if done:
if __name__ == "__main__":
- main()
+ last_time = datetime.datetime.fromtimestamp(
+ float(sys.argv[1]), datetime.timezone.utc
+ )
+ start_time = datetime.datetime.now(datetime.timezone.utc)
+ main(last_time, start_time)
+ print(start_time.timestamp())