First public revision
[imapami.git] / imapami / mail.py
1 #!/usr/bin/env python
2
3 #
4 # Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 #     * Redistributions of source code must retain the above copyright
10 #       notice, this list of conditions and the following disclaimer.
11 #     * Redistributions in binary form must reproduce the above copyright
12 #       notice, this list of conditions and the following disclaimer in the
13 #       documentation and/or other materials provided with the distribution.
14 #     * Neither the name of the University of California, Berkeley nor the
15 #       names of its contributors may be used to endorse or promote products
16 #       derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #
29
30 import email
31
32 class ImapamiMail(object):
33     """
34     An email, fetched from the IMAP server
35
36     Depending on the fetch level, fields may be filled or set to None.
37     """
38     def __init__(self, item, inbox, flags=None, internal_date=None, date=None,
39                  headers=None, body_part1=None, body_all=None):
40         """
41         :arg str item:
42           The IMAP id of the mail
43         :arg str inbox:
44           The IMAP directory where the mail is located
45         :arg str flags:
46           The IMAP flags of the mail
47         :arg str internal_date:
48           The IMAP date of the mail
49         :arg time.struct_time date:
50           The date of the mail, converted in python format
51         :arg str headers:
52           The headers of the mail
53         :arg str body_part1:
54           The first part of the body of the mail
55         :arg str body_all:
56           The whole body of the mail
57         """
58         # always filled
59         self.item = item
60         self.inbox = inbox
61         msg = ''
62         if headers is not None:
63             msg += headers
64         self.msghdrs = email.message_from_string(msg)
65         if body_all is not None:
66             msg += body_all
67         elif body_part1 is not None:
68             msg += body_part1
69         self.msg = email.message_from_string(msg)
70         # fetch level >= headers
71         self.flags = flags
72         self.internal_date = internal_date
73         self.date = date
74         self.headers = headers
75         # fetch level >= part1
76         self.body_part1 = body_part1
77         # fetch level == all
78         self.body_all = body_all