]> git.madduck.net Git - etc/awesome.git/blob - scripts/checkmail

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

some fixes
[etc/awesome.git] / scripts / checkmail
1 #!/usr/bin/python
2
3 # Simple email checker
4 #
5 # Wrote by copycat-killer on a rainy day of august 2013
6 # to be used in Lain. 
7 #
8 # https://github.com/copycat-killer/lain
9
10 import sys, getopt, locale, imaplib
11
12 def main(argv):
13    usage    = "usage: checkmail -s <imapserver> -u <usermail> -p <password> [--port <port>] [--encoding <encoding>] [--cut]"
14    server   = ""
15    user     = ""
16    password = ""
17    port     = 993
18    cut      = False
19    encoding = locale.getdefaultlocale()[1]
20    output   = ""
21
22    try:
23        opts, args = getopt.getopt(argv, "hs:u:p:", ["port=", "encoding=", "cut"])
24    except getopt.GetoptError:
25       print(usage)
26       sys.exit(2)
27
28    if len(argv) == 0:
29       print(usage)
30       sys.exit()
31
32    for opt, arg in opts:
33       if opt == "-h":
34          print(usage)
35          sys.exit()
36       elif opt == "-s":
37          server = arg
38       elif opt == "-u":
39          user = arg
40       elif opt == "-p":
41          password = arg
42       elif opt == "--port":
43          port = int(arg)
44       elif opt == "--cut":
45          cut = True
46       elif opt == "--encoding":
47          encoding = arg
48
49    try:
50       mail = imaplib.IMAP4_SSL(server, port)
51       mail.login(user, password)
52    except imaplib.IMAP4.error:
53       print("CheckMailError: invalid credentials")
54       sys.exit(2)
55
56    status, counts = mail.status("Inbox","(MESSAGES UNSEEN)")
57
58    unread = int(counts[0].split()[4][:-1])
59
60    if status == "OK" and unread:
61       mail.select("Inbox", readonly = 1)
62       ret, messages = mail.uid("search", None, "(UNSEEN)")
63
64       if ret == "OK":
65           latest_email_uid = messages[0].split()[-1]
66
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])")
70
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
77                   print("[+Seen]\n")
78
79               nm_header = new_mail_header[0][1].decode(encoding, "replace").strip()
80               nm_text = new_mail_text[0][1].decode(encoding, "replace").strip()
81
82               if unread == 1:
83                   print(user, "has 1 new message\n")
84               else:
85                   print(user, "has", unread, "new messages\n")
86                   nm_header.replace("From:", "Latest from:", 1)
87
88               print(nm_header, "\n")
89
90               if cut:
91                   if len(nm_text) <= 100:
92                       print(nm_text)
93                   else:
94                       print(nm_text[0:100])
95                       print("[...]")
96               else:
97                   print(nm_text)
98    else:
99       print("No new messages")
100
101    mail.logout()
102
103 if __name__ == "__main__":
104    main(sys.argv[1:])