Browse Source

Move sendmail function out of class since it doesn't need any data from it an it was a static method anyway

develop
Jonathan Golder 8 years ago
parent
commit
800cc46574
  1. 3
      __init__.py
  2. 116
      jogobot.py

3
__init__.py

@ -24,4 +24,5 @@
"""
Scripts for our redundances bot
"""
from jogobot.jogobot import output # noqa
# noqa needed to prevent pyflakes from warning about unused imports
from jogobot.jogobot import ( output, sendmail ) # noqa

116
jogobot.py

@ -99,6 +99,64 @@ def pywikibot_output( text, decoder=None, newline=True,
pywikibot.output = pywikibot_output
def sendmail( Subject, Body, To=None, CC=None, BCC=None,
From="JogoBot <tools.jogobot@tools.wmflabs.org>" ):
"""
Provides a simple wrapper for exim (MTA) on tool labs
Params should be formated according related fields in RFC 5322
@param subject Mail subject
@type subject str
@param body Mail body as (formated) string
@type body unicode-str
@param to Mail-Recipiends (comma-separeded)
@type str
@param from Mail-Sender
@type str
"""
# Create mail body as MIME-Object
msg = MIMEText(Body)
# Set up mail header
msg['Subject'] = Subject
msg['From'] = From
if To:
msg['To'] = To
if CC:
msg['CC'] = CC
if BCC:
msg['BCC'] = BCC
msg['Content-Type'] = 'text/plain; charset=utf-8'
# Make sure we have a recipient
if not( To or CC or BCC):
raise JogoBotMailError( "No recipient was provided!" )
# Send the message via exim
with Popen( ["/usr/sbin/exim", "-odf", "-i", "-t"],
stdin=PIPE, universal_newlines=True) as MTA:
MTA.communicate(msg.as_string())
# Try to get returncode of MTA
# Process is not terminated until timeout, set returncode to None
try:
returncode = MTA.wait(timeout=30)
except TimeoutExpired:
returncode = None
# Catch MTA errors
if returncode:
raise JogoBotMailError( "/usr/sbin/exim terminated with " +
"returncode != 0. Returncode was " +
str( returncode ) )
class JogoBot:
"""
Basic bot framework
@ -205,64 +263,6 @@ class JogoBot:
with open(disable_file, 'a'):
pass
@staticmethod
def sendmail( Subject, Body, To=None, CC=None, BCC=None,
From="JogoBot <tools.jogobot@tools.wmflabs.org>" ):
"""
Provides a simple wrapper for exim (MTA) on tool labs
Params should be formated according related fields in RFC 5322
@param subject Mail subject
@type subject str
@param body Mail body as (formated) string
@type body unicode-str
@param to Mail-Recipiends (comma-separeded)
@type str
@param from Mail-Sender
@type str
"""
# Create mail body as MIME-Object
msg = MIMEText(Body)
# Set up mail header
msg['Subject'] = Subject
msg['From'] = From
if To:
msg['To'] = To
if CC:
msg['CC'] = CC
if BCC:
msg['BCC'] = BCC
msg['Content-Type'] = 'text/plain; charset=utf-8'
# Make sure we have a recipient
if not( To or CC or BCC):
raise JogoBotMailError( "No recipient was provided!" )
# Send the message via exim
with Popen( ["/usr/sbin/exim", "-odf", "-i", "-t"],
stdin=PIPE, universal_newlines=True) as MTA:
MTA.communicate(msg.as_string())
# Try to get returncode of MTA
# Process is not terminated until timeout, set returncode to None
try:
returncode = MTA.wait(timeout=30)
except TimeoutExpired:
returncode = None
# Catch MTA errors
if returncode:
raise JogoBotMailError( "/usr/sbin/exim terminated with " +
"returncode != 0. Returncode was " +
str( returncode ) )
class JogoBotMailError( Exception ):
"""

Loading…
Cancel
Save