#!/usr/bin/env python

#
# Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California, Berkeley nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import email

class ImapamiMail(object):
    """
    An email, fetched from the IMAP server

    Depending on the fetch level, fields may be filled or set to None.
    """
    def __init__(self, item, inbox, flags=None, internal_date=None, date=None,
                 headers=None, body_part1=None, body_all=None):
        """
        :arg str item:
          The IMAP id of the mail
        :arg str inbox:
          The IMAP directory where the mail is located
        :arg str flags:
          The IMAP flags of the mail
        :arg str internal_date:
          The IMAP date of the mail
        :arg time.struct_time date:
          The date of the mail, converted in python format
        :arg str headers:
          The headers of the mail
        :arg str body_part1:
          The first part of the body of the mail
        :arg str body_all:
          The whole body of the mail
        """
        # always filled
        self.item = item
        self.inbox = inbox
        msg = ''
        if headers is not None:
            msg += headers
        self.msghdrs = email.message_from_string(msg)
        if body_all is not None:
            msg += body_all
        elif body_part1 is not None:
            msg += body_part1
        self.msg = email.message_from_string(msg)
        # fetch level >= headers
        self.flags = flags
        self.internal_date = internal_date
        self.date = date
        self.headers = headers
        # fetch level >= part1
        self.body_part1 = body_part1
        # fetch level == all
        self.body_all = body_all
