~jhonnyc/cgmail/jonathanc-branch

« back to all changes in this revision

Viewing changes to src/lib/imap.py

  • Committer: Marco Ferragina
  • Date: 2007-05-06 15:51:12 UTC
  • Revision ID: marco.ferragina@gmail.com-20070506155112-874uk2m8blrknyuf
Restructured package source dir. Now is much more organized. Implemented right click menu on account window treeview

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import imaplib
 
2
from email.Parser import HeaderParser
 
3
 
 
4
class ImapBoxConnectionError(Exception): pass
 
5
class ImapBoxAuthError(Exception): pass
 
6
 
 
7
class ImapBox:
 
8
        def __init__(self, user, password, 
 
9
                        host, port = 143, ssl = False,
 
10
                        use_default_mbox = True,
 
11
                        mbox_dir = None):
 
12
                self.user = user
 
13
                self.password = password
 
14
                self.port = int(port)
 
15
                self.host = host
 
16
                self.ssl = ssl
 
17
                self.use_default_mbox = use_default_mbox
 
18
                self.mbox_dir = mbox_dir
 
19
 
 
20
                self.mbox = None
 
21
        
 
22
        def __connect(self):
 
23
                print "connecting to the imap server ..."
 
24
                try:
 
25
                        if not self.ssl:
 
26
                                self.mbox = imaplib.IMAP4(self.host, self.port)
 
27
                        else:
 
28
                                self.mbox = imaplib.IMAP4_SSL(self.host, self.port)
 
29
                except Exception:
 
30
                        raise ImapBoxConnectionError()
 
31
                print "... connection done"
 
32
 
 
33
                print "authenticate user ..."
 
34
                try:
 
35
                        self.mbox.login(self.user, self.password)
 
36
                except Exception, e:
 
37
                        raise ImapBoxAuthError()
 
38
                print "...auth done"
 
39
        
 
40
        def get_mails(self):
 
41
                
 
42
                try:
 
43
                        self.__connect()
 
44
                except ImapBoxConnectionError:
 
45
                        raise ImapBoxConnectionError()
 
46
                except ImapBoxAuthError:
 
47
                        raise ImapBoxAuthError()
 
48
 
 
49
                print "getting mails..."
 
50
 
 
51
                mails = []
 
52
                try:
 
53
                        if self.use_default_mbox:
 
54
                                result, message = self.mbox.select(readonly=1)
 
55
                        else:
 
56
                                result, message = self.mbox.select(self.mbox_dir, readonly=1)
 
57
                        if result != 'OK':
 
58
                                raise Exception, message
 
59
 
 
60
                        # retrieve only unseen messages
 
61
                        typ, data = self.mbox.search(None, 'UNSEEN')
 
62
                        for num in data[0].split():
 
63
                                # fetch only needed fields
 
64
                                f = self.mbox.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT FROM MESSAGE-ID)])')
 
65
                                hp = HeaderParser()
 
66
                                m = hp.parsestr(f[1][0][1])
 
67
                                sub = m['subject']
 
68
                                fr = m['from']
 
69
                                sub = sub.replace("<", "&lt;").replace(">", "&gt;")
 
70
                                sub = sub.replace("&", "&amp;")
 
71
                                fr = fr.replace("<", "&lt;").replace(">", "&gt;")
 
72
                                fr.replace("&", "&amp;")
 
73
                                mails.append([sub, fr, m['Message-ID']])
 
74
                except Exception, e:
 
75
                        print e
 
76
 
 
77
                self.mbox.logout()
 
78
                print "...done"
 
79
                return mails
 
80
 
 
81
 
 
82
if __name__ == "__main__":
 
83
        #i = ImapBox("", "", "")
 
84
        i = ImapBox("", "", "")
 
85
        print i.get_mails()