Changeset View
Changeset View
Standalone View
Standalone View
wallace/module_footer.py
Show All 12 Lines | |||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | # GNU General Public License for more details. | ||||
# You should have received a copy of the GNU General Public License | # You should have received a copy of the GNU General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | # | ||||
import os | import os | ||||
import re | |||||
import tempfile | import tempfile | ||||
import time | import time | ||||
from email import message_from_file | from email import message_from_file | ||||
from email.encoders import encode_quopri | from email.encoders import encode_quopri | ||||
import modules | import modules | ||||
import pykolab | import pykolab | ||||
from pykolab.translate import _ | from pykolab.translate import _ | ||||
log = pykolab.getLogger('pykolab.wallace/footer') | log = pykolab.getLogger('pykolab.wallace/footer') | ||||
extra_log_params = {'qid': '-'} | extra_log_params = {'qid': '-'} | ||||
log = pykolab.logger.LoggerAdapter(log, extra_log_params) | log = pykolab.logger.LoggerAdapter(log, extra_log_params) | ||||
conf = pykolab.getConf() | conf = pykolab.getConf() | ||||
mybasepath = '/var/spool/pykolab/wallace/footer/' | mybasepath = '/var/spool/pykolab/wallace/footer/' | ||||
def __init__(): | def __init__(): | ||||
modules.register('footer', execute, description=description()) | modules.register('footer', execute, description=description()) | ||||
def description(): | def description(): | ||||
return """Append a footer to messages.""" | return """Append a footer to messages.""" | ||||
def append_footer(content, footer, position=None, isHtml=False): | |||||
if (isHtml): | |||||
append = "\n<!-- footer appended by Wallace -->\n" + footer + "\n<!-- footer end -->\n" | |||||
if position == 'top': | |||||
match = re.search('(<body[^>]*>)', content, re.IGNORECASE | re.DOTALL) | |||||
if match: | |||||
content = content.replace(match.group(0), match.group(0) + append) | |||||
else: | |||||
content = "<html><body>" + append + content + "</body></html>" | |||||
else: | |||||
match = re.search('(</body>)', content, re.IGNORECASE | re.DOTALL) | |||||
if match: | |||||
content = content.replace(match.group(0), append + match.group(0)) | |||||
else: | |||||
content = "<html><body>" + content + append + "</body></html>" | |||||
else: | |||||
if position == 'top': | |||||
content = footer + "\n\n" + content | |||||
else: | |||||
content += "\n\n-- \n" + footer | |||||
return content | |||||
def set_part_content(part, content): | def set_part_content(part, content): | ||||
# Reset old encoding and use quoted-printable (#5414) | # Reset old encoding and use quoted-printable (#5414) | ||||
del part['Content-Transfer-Encoding'] | del part['Content-Transfer-Encoding'] | ||||
part.set_payload(content) | part.set_payload(content) | ||||
encode_quopri(part) | encode_quopri(part) | ||||
return True | return True | ||||
Show All 29 Lines | def execute(*args, **kw): | ||||
# parse message | # parse message | ||||
message = message_from_file(open(filepath, 'r')) | message = message_from_file(open(filepath, 'r')) | ||||
# Possible footer answers are limited to ACCEPT only | # Possible footer answers are limited to ACCEPT only | ||||
answers = [ 'ACCEPT' ] | answers = [ 'ACCEPT' ] | ||||
footer = {} | footer = {} | ||||
footer_position = conf.get('wallace', 'footer_position') | |||||
footer_html_file = conf.get('wallace', 'footer_html') | footer_html_file = conf.get('wallace', 'footer_html') | ||||
footer_text_file = conf.get('wallace', 'footer_text') | footer_text_file = conf.get('wallace', 'footer_text') | ||||
if not os.path.isfile(footer_text_file) and not os.path.isfile(footer_html_file): | if not os.path.isfile(footer_text_file) and not os.path.isfile(footer_html_file): | ||||
log.warning(_("No contents configured for footer module")) | log.warning(_("No contents configured for footer module")) | ||||
exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', filepath)) | exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', filepath)) | ||||
return | return | ||||
Show All 37 Lines | for part in message.walk(): | ||||
log.debug("Walking message part: %s; disposition = %r" % (content_type, disposition), level=8) | log.debug("Walking message part: %s; disposition = %r" % (content_type, disposition), level=8) | ||||
if disposition is not None: | if disposition is not None: | ||||
continue | continue | ||||
if content_type == "text/plain": | if content_type == "text/plain": | ||||
content = part.get_payload(decode=True) | content = part.get_payload(decode=True) | ||||
content += "\n\n-- \n%s" % (footer['plain']) | content = append_footer(content, footer['plain'], footer_position, false) | ||||
footer_added = set_part_content(part, content) | footer_added = set_part_content(part, content) | ||||
elif content_type == "text/html": | elif content_type == "text/html": | ||||
content = part.get_payload(decode=True) | content = part.get_payload(decode=True) | ||||
append = "\n<!-- footer appended by Wallace -->\n" + footer['html'] | content = append_footer(content, footer['html'], footer_position, true) | ||||
if "</body>" in content: | |||||
content = content.replace("</body>", append + "</body>") | |||||
else: | |||||
content = "<html><body>" + content + append + "</body></html>" | |||||
footer_added = set_part_content(part, content) | footer_added = set_part_content(part, content) | ||||
if footer_added: | if footer_added: | ||||
log.debug("Footer attached.") | log.debug("Footer attached.") | ||||
message.add_header("X-Wallace-Footer", "YES") | message.add_header("X-Wallace-Footer", "YES") | ||||
(fp, new_filepath) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/footer/ACCEPT") | (fp, new_filepath) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/footer/ACCEPT") | ||||
os.write(fp, message.as_string()) | os.write(fp, message.as_string()) | ||||
os.close(fp) | os.close(fp) | ||||
os.unlink(filepath) | os.unlink(filepath) | ||||
exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', new_filepath)) | exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', new_filepath)) |