|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import sys |
| 4 | +import gtk |
| 5 | +import appindicator |
| 6 | +import pynotify |
| 7 | + |
| 8 | +import imaplib |
| 9 | +import re |
| 10 | +import email.header |
| 11 | +import ezPyCrypto |
| 12 | +import quopri |
| 13 | +import os |
| 14 | + |
| 15 | +PING_FREQUENCY = 10 # seconds |
| 16 | + |
| 17 | +class CheckGMail: |
| 18 | + def __init__(self): |
| 19 | + directory= os.path.dirname(os.path.realpath(__file__)) |
| 20 | + self.ind = appindicator.Indicator("new-gmail-indicator", directory+"/normal.png" , appindicator.CATEGORY_APPLICATION_STATUS) |
| 21 | + self.ind.set_status(appindicator.STATUS_ACTIVE) |
| 22 | + self.ind.set_attention_icon(directory+"/new.png") |
| 23 | + |
| 24 | + self.menu_setup() |
| 25 | + self.ind.set_menu(self.menu) |
| 26 | + self.last_count=0 |
| 27 | + |
| 28 | + fd = open(directory+"/.ex_mykey.priv","rb") |
| 29 | + k = ezPyCrypto.key(fd.read()) |
| 30 | + fd.close() |
| 31 | + |
| 32 | + fd=open(directory+"/email", "rb") |
| 33 | + self.email= k.decString(fd.read()) |
| 34 | + fd.close() |
| 35 | + fd=open(directory+"/pwd", "rb") |
| 36 | + self.pwd= k.decString(fd.read()) |
| 37 | + fd.close() |
| 38 | + |
| 39 | + def menu_setup(self): |
| 40 | + self.menu = gtk.Menu() |
| 41 | + |
| 42 | + self.quit_item = gtk.MenuItem("Quit") |
| 43 | + self.quit_item.connect("activate", self.quit) |
| 44 | + self.quit_item.show() |
| 45 | + self.menu.append(self.quit_item) |
| 46 | + |
| 47 | + def main(self): |
| 48 | + self.check_mail() |
| 49 | + gtk.timeout_add(PING_FREQUENCY * 1000, self.check_mail) |
| 50 | + gtk.main() |
| 51 | + |
| 52 | + def quit(self, widget): |
| 53 | + sys.exit(0) |
| 54 | + |
| 55 | + def unir(self, arreglo): |
| 56 | + res=[] |
| 57 | + for i in arreglo: |
| 58 | + if i[1] != None: |
| 59 | + res.append(i[0].decode(i[1])) |
| 60 | + else: |
| 61 | + res.append(i[0]) |
| 62 | + return ''.join(res) |
| 63 | + |
| 64 | + def get_first_text_part(self, msg): |
| 65 | + maintype = msg.get_content_maintype() |
| 66 | + if maintype == 'multipart': |
| 67 | + for part in msg.get_payload(): |
| 68 | + print part.get_content_charset(part.get_payload()) |
| 69 | + if part.get_content_maintype() == 'text': |
| 70 | + resp= ' ' |
| 71 | + if part['Content-Transfer-Encoding'] == 'quoted-printable': |
| 72 | + resp= quopri.decodestring(part.get_payload()) |
| 73 | + if part.get_content_charset(False): |
| 74 | + resp = part.get_payload().decode(part.get_content_charset()) |
| 75 | + print resp |
| 76 | + return resp |
| 77 | + for part in msg.get_payload(): |
| 78 | + return self.get_first_text_part(part) |
| 79 | + elif maintype == 'text': |
| 80 | + resp= '' |
| 81 | + print msg.get_content_charset(msg.get_payload()) |
| 82 | + if msg['Content-Transfer-Encoding'] == 'quoted-printable': |
| 83 | + resp= quopri.decodestring(msg.get_payload()) |
| 84 | + if msg.get_content_charset(False): |
| 85 | + resp = msg.get_payload().decode(msg.get_content_charset()) |
| 86 | + print resp |
| 87 | + return resp |
| 88 | + else: |
| 89 | + return ' ' |
| 90 | + |
| 91 | + def check_mail(self): |
| 92 | + messages, unread, msg_notify = self.gmail_checker(self.email, self.pwd) |
| 93 | + print ''.join(msg_notify) |
| 94 | + if unread > 0: |
| 95 | + self.ind.set_status(appindicator.STATUS_ATTENTION) |
| 96 | + if unread > self.last_count: |
| 97 | + n = pynotify.Notification(str(unread)+" new e-mail(s)", ''.join(msg_notify), "notification") |
| 98 | + n.show() |
| 99 | + else: |
| 100 | + self.ind.set_status(appindicator.STATUS_ACTIVE) |
| 101 | + self.last_count= unread |
| 102 | + return True |
| 103 | + |
| 104 | + def gmail_checker(self, username, password): |
| 105 | + i = imaplib.IMAP4_SSL('imap.gmail.com') |
| 106 | + i.login(username, password) |
| 107 | + x, y = i.status('INBOX', '(MESSAGES UNSEEN)') |
| 108 | + i.select('INBOX','readonly') |
| 109 | + typ, data = i.search(None,'UNSEEN') |
| 110 | + msg_notify= [] |
| 111 | + messages=0 |
| 112 | + unseen=0 |
| 113 | + if typ == 'OK': |
| 114 | + for msg_num in data[0].split(): |
| 115 | + typ, msg = i.fetch(str(msg_num), "RFC822") |
| 116 | + if typ == 'OK': |
| 117 | + mail_msg = email.message_from_string(msg[0][1]) |
| 118 | + aux= email.header.decode_header(mail_msg['from']) |
| 119 | + fr=self.unir(aux) |
| 120 | + if len(fr) > 40: |
| 121 | + fr= fr[:37]+'...' |
| 122 | + try: |
| 123 | + msg_notify.append('From: '+ fr.encode('utf-8') +'\n') |
| 124 | + except: |
| 125 | + msg_notify.append('From: ##Encode error##\n') |
| 126 | + aux=email.header.decode_header(mail_msg['subject']) |
| 127 | + sub=self.unir(aux) |
| 128 | + if len(sub) > 40: |
| 129 | + sub= sub[:37]+'...' |
| 130 | + try: |
| 131 | + msg_notify.append('Subject: ' + sub.encode('utf-8') +'\n') |
| 132 | + except: |
| 133 | + msg_notify.append('From: ##Encode error##\n') |
| 134 | + bod=self.get_first_text_part(mail_msg) |
| 135 | + print bod.__class__ |
| 136 | + if not isinstance(bod, str) and not type(bod).__name__ == 'unicode': |
| 137 | + bod='(No content)' |
| 138 | + bod=re.sub('[ \t\n\r]+',' ', bod) |
| 139 | + if re.search("<html>", bod.lower()): |
| 140 | + bod=re.sub('<.*?>', '', bod) |
| 141 | + if len(bod) > 40: |
| 142 | + bod=(bod[:37]+'...') |
| 143 | + #print bod |
| 144 | + try: |
| 145 | + msg_notify.append('Msg: '+ bod.encode('utf-8') + '\n') |
| 146 | + except: |
| 147 | + msg_notify.append('Msg: ##Encode error##\n') |
| 148 | + messages = int(re.search('MESSAGES\s+(\d+)', y[0]).group(1)) |
| 149 | + unseen = int(re.search('UNSEEN\s+(\d+)', y[0]).group(1)) |
| 150 | + return (messages, unseen, msg_notify) |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + indicator = CheckGMail() |
| 154 | + indicator.main() |
| 155 | + |
0 commit comments