# 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
[docs]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
[docs] def getContent(self):
"""returns the content of the message"""
return self.content
[docs] def __eq__(self,other):
"""return ``True`` if **other** has the same content."""
return self.content == other.content
[docs] def __neq__(self,other):
"""return ``False`` if **other** has the same content."""
return not self.__eq__(other)
[docs]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_
[docs]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
[docs] 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
[docs]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')
[docs] def as_string(self):
"""returns created mail"""
return self.content.as_string()
[docs] 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
[docs] 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"]