All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
5 # Wrote by copycat-killer on a rainy day of august 2013
8 # https://github.com/copycat-killer/lain
10 import sys, getopt, locale, imaplib
13 usage = "usage: checkmail -s <imapserver> -u <usermail> -p <password> [--port <port>] [--encoding <encoding>] [--cut]"
19 encoding = locale.getdefaultlocale()[1]
23 opts, args = getopt.getopt(argv, "hs:u:p:", ["port=", "encoding=", "cut"])
24 except getopt.GetoptError:
46 elif opt == "--encoding":
50 mail = imaplib.IMAP4_SSL(server, port)
51 mail.login(user, password)
52 except imaplib.IMAP4.error:
53 print("CheckMailError: invalid credentials")
56 status, counts = mail.status("Inbox","(MESSAGES UNSEEN)")
58 unread = int(counts[0].split()[4][:-1])
60 if status == "OK" and unread:
61 mail.select("Inbox", readonly = 1)
62 ret, messages = mail.uid("search", None, "(UNSEEN)")
65 latest_email_uid = messages[0].split()[-1]
67 ret_header, new_mail_header = mail.uid("fetch", latest_email_uid,
68 "(BODY.PEEK[HEADER.FIELDS (SUBJECT FROM)])")
69 ret_text, new_mail_text = mail.uid("fetch", latest_email_uid, "(BODY[TEXT])")
71 if ret_header == "OK" and ret_text == "OK":
72 try: # not all the servers like this, that's why we try
73 mail.store(latest_email_uid, "-FLAGS", "\\Seen")
74 except imaplib.IMAP4.error:
75 # this simply means the server refused to
76 # toggle Seen flag from mail
79 nm_header = new_mail_header[0][1].decode(encoding, "replace").strip()
80 nm_text = new_mail_text[0][1].decode(encoding, "replace").strip()
83 print(user, "has 1 new message\n")
85 print(user, "has", unread, "new messages\n")
86 nm_header.replace("From:", "Latest from:", 1)
88 print(nm_header, "\n")
91 if len(nm_text) <= 100:
99 print("No new messages")
103 if __name__ == "__main__":