iro/model/message.py
author Sandro Knauß <knauss@netzguerilla.net>
Fri, 24 Aug 2012 01:05:06 +0200
branchdevel
changeset 294 0e75bd39767d
parent 258 0a5eb5aac0be
child 312 42fd5075a5d1
permissions -rw-r--r--
adding LICENSE to all files

# Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
# 
# This file is part of Iro.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# #Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# -*- coding: utf-8 -*-
"""All available message typs to send send.
"""
from email.mime.text import MIMEText
from email.header import Header
from email.Utils import formatdate

class Message:
    """ Baseclass for all different message typs."""
    def __init__(self,content, typ="Message"):
        """Constructor of Message class.
        
        :param content: content of the message
        :param string typ: typ of the message

        .. automethod:: __eq__
        .. automethod:: __neq__
        """
        self.content=content
        self.typ = typ


    def getContent(self):
        """returns the content of the message"""
        return self.content

    def __eq__(self,other):
        """return ``True`` if **other** has the same content."""
        return self.content == other.content

    def __neq__(self,other):
        """return ``False`` if **other** has the same content."""
        return not self.__eq__(other)

class SMS(Message):
    """ A representation of one SMS"""
    def __init__(self, cont, from_ = None):
        """Constructor of SMS class.

        :param string cont: SMS content
        :param string from_: the telnumber from the SMS should be sended.
        """
        Message.__init__(self, cont.encode("utf-8"), typ="sms")
        self.from_ = from_

class Fax(Message):
    """A representation of one fax."""
    def __init__(self,header,cont,attachments=[]):
        """Constructor of one fax.
        
        :param string header: Headline of fax
        :param string cont: fax content
        :param list attachments: attachments of fax
        """
        Message.__init__(self,cont.encode("utf-8"),typ="fax")
        self.header=header
        self.attachments=attachments

    def getAttachment(self,i):
        """returns a attachment
        :param integer i:  the i-th attachment
        """
        return self.attachments[i]

    def __eq__(self,other):
        if not  Message.__eq__(self,other):
            return False

        if self.header != other.header:
            return False

        if len(self.attachments) != len(other.attachments):
            return False

        for a in self.attachments:
            if a not in other.attachments:
                return False
        return True



class Mail(Message):
    """A representation of one Mail"""
    def __init__(self, subject, body, frm):
        """Constructor of one mail.

        :param string subject: subject of the mail
        :param string body: body of the mail
        :param string frm: mailaddress to send mail from

        .. automethod:: __repr__
        """
        con = MIMEText(body.encode("utf-8"), _charset='utf-8')
        sub = Header(subject.encode('utf-8'), 'utf-8')
        con['Subject'] = sub
        con['Date'] = formatdate(localtime=True)
        self.subject = subject
        self.body = body
        self.frm=frm
        Message.__init__(self, con, typ='mail')

    def as_string(self):
        """returns created mail"""
        return self.content.as_string()

    def getFrom(self):
        """returns the from mailaddress"""
        return self.frm
    
    def __eq__(self,other):
        if self.as_string() != other.as_string():
            return False

        if self.frm != other.frm:
            return False
        
        return True

    def __repr__(self):
        """string representation of the class.

        :returns: ``<Mail(subject, body, frm)>``
        """
        return "<Mail(%s, %s, %s)>"%(self.subject,self.body,self.frm)

__all__=["Message", "SMS", "Fax", "Mail"]