iro/model/message.py
changeset 302 3f4bdea2abbf
parent 294 0e75bd39767d
child 312 42fd5075a5d1
equal deleted inserted replaced
90:eb04ac3a8327 302:3f4bdea2abbf
       
     1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
       
     2 # 
       
     3 # This file is part of Iro.
       
     4 # 
       
     5 # Permission is hereby granted, free of charge, to any person obtaining a copy of
       
     6 # this software and associated documentation files (the "Software"), to deal in
       
     7 # the Software without restriction, including without limitation the rights to use,
       
     8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
       
     9 # #Software, and to permit persons to whom the Software is furnished to do so,
       
    10 # subject to the following conditions:
       
    11 # 
       
    12 # The above copyright notice and this permission notice shall be included in
       
    13 # all copies or substantial portions of the Software.
       
    14 # 
       
    15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
       
    16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
       
    17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
       
    18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    21 
       
    22 # -*- coding: utf-8 -*-
       
    23 """All available message typs to send send.
       
    24 """
       
    25 from email.mime.text import MIMEText
       
    26 from email.header import Header
       
    27 from email.Utils import formatdate
       
    28 
       
    29 class Message:
       
    30     """ Baseclass for all different message typs."""
       
    31     def __init__(self,content, typ="Message"):
       
    32         """Constructor of Message class.
       
    33         
       
    34         :param content: content of the message
       
    35         :param string typ: typ of the message
       
    36 
       
    37         .. automethod:: __eq__
       
    38         .. automethod:: __neq__
       
    39         """
       
    40         self.content=content
       
    41         self.typ = typ
       
    42 
       
    43 
       
    44     def getContent(self):
       
    45         """returns the content of the message"""
       
    46         return self.content
       
    47 
       
    48     def __eq__(self,other):
       
    49         """return ``True`` if **other** has the same content."""
       
    50         return self.content == other.content
       
    51 
       
    52     def __neq__(self,other):
       
    53         """return ``False`` if **other** has the same content."""
       
    54         return not self.__eq__(other)
       
    55 
       
    56 class SMS(Message):
       
    57     """ A representation of one SMS"""
       
    58     def __init__(self, cont, from_ = None):
       
    59         """Constructor of SMS class.
       
    60 
       
    61         :param string cont: SMS content
       
    62         :param string from_: the telnumber from the SMS should be sended.
       
    63         """
       
    64         Message.__init__(self, cont.encode("utf-8"), typ="sms")
       
    65         self.from_ = from_
       
    66 
       
    67 class Fax(Message):
       
    68     """A representation of one fax."""
       
    69     def __init__(self,header,cont,attachments=[]):
       
    70         """Constructor of one fax.
       
    71         
       
    72         :param string header: Headline of fax
       
    73         :param string cont: fax content
       
    74         :param list attachments: attachments of fax
       
    75         """
       
    76         Message.__init__(self,cont.encode("utf-8"),typ="fax")
       
    77         self.header=header
       
    78         self.attachments=attachments
       
    79 
       
    80     def getAttachment(self,i):
       
    81         """returns a attachment
       
    82         :param integer i:  the i-th attachment
       
    83         """
       
    84         return self.attachments[i]
       
    85 
       
    86     def __eq__(self,other):
       
    87         if not  Message.__eq__(self,other):
       
    88             return False
       
    89 
       
    90         if self.header != other.header:
       
    91             return False
       
    92 
       
    93         if len(self.attachments) != len(other.attachments):
       
    94             return False
       
    95 
       
    96         for a in self.attachments:
       
    97             if a not in other.attachments:
       
    98                 return False
       
    99         return True
       
   100 
       
   101 
       
   102 
       
   103 class Mail(Message):
       
   104     """A representation of one Mail"""
       
   105     def __init__(self, subject, body, frm):
       
   106         """Constructor of one mail.
       
   107 
       
   108         :param string subject: subject of the mail
       
   109         :param string body: body of the mail
       
   110         :param string frm: mailaddress to send mail from
       
   111 
       
   112         .. automethod:: __repr__
       
   113         """
       
   114         con = MIMEText(body.encode("utf-8"), _charset='utf-8')
       
   115         sub = Header(subject.encode('utf-8'), 'utf-8')
       
   116         con['Subject'] = sub
       
   117         con['Date'] = formatdate(localtime=True)
       
   118         self.subject = subject
       
   119         self.body = body
       
   120         self.frm=frm
       
   121         Message.__init__(self, con, typ='mail')
       
   122 
       
   123     def as_string(self):
       
   124         """returns created mail"""
       
   125         return self.content.as_string()
       
   126 
       
   127     def getFrom(self):
       
   128         """returns the from mailaddress"""
       
   129         return self.frm
       
   130     
       
   131     def __eq__(self,other):
       
   132         if self.as_string() != other.as_string():
       
   133             return False
       
   134 
       
   135         if self.frm != other.frm:
       
   136             return False
       
   137         
       
   138         return True
       
   139 
       
   140     def __repr__(self):
       
   141         """string representation of the class.
       
   142 
       
   143         :returns: ``<Mail(subject, body, frm)>``
       
   144         """
       
   145         return "<Mail(%s, %s, %s)>"%(self.subject,self.body,self.frm)
       
   146 
       
   147 __all__=["Message", "SMS", "Fax", "Mail"]