# Copyright (C) 2018 by the Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. """This is a custom handler that will examine the size of a message and take action based on various mm_cfg.py settings. The settings are as follows GLOBAL_MAX_MESSAGE_SIZE if set to a number > 0, that is the maximum number of KB for a message to be accepted GLOBAL_MAX_MESSAGE_ACTION if GLOBAL_MAX_MESSAGE_SIZE > 0 and a message is larger than that size take action as follows: = 0, hold the message = 1, reject the message = any value other than 0 or 1, discard the message This handler should be installed as Mailman/Handlers/GlobalTooBig.py and added to the pipeline between Moderate and Hold by putting something like GLOBAL_PIPELINE.insert(GLOBAL_PIPELINE.index('Hold'), 'GlobalTooBig') in mm_cfg.py. You also must define GLOBAL_MAX_MESSAGE_SIZE in mm_cfg.py and if it is > 0 you also must define GLOBAL_MAX_MESSAGE_ACTION. """ import email from Mailman import mm_cfg from Mailman import Errors from Mailman.Handlers import Hold, Moderate def process(mlist, msg, msgdata): # Are we doing anything? if mm_cfg.GLOBAL_MAX_MESSAGE_SIZE <= 0: return # Get the message size. bodylen = 0 for line in email.Iterators.body_line_iterator(msg): bodylen += len(line) for part in msg.walk(): if part.preamble: bodylen += len(part.preamble) if part.epilogue: bodylen += len(part.epilogue) if bodylen/1024.0 <= mm_cfg.GLOBAL_MAX_MESSAGE_SIZE: return if mm_cfg.GLOBAL_MAX_MESSAGE_ACTION == 0: # Hold the message Hold.hold_for_approval(mlist, msg, msgdata, Hold.MessageTooBig(bodylen, mm_cfg.GLOBAL_MAX_MESSAGE_SIZE)) # No return elif mm_cfg.GLOBAL_MAX_MESSAGE_ACTION == 1: raise Errors.RejectMessage, Hold.MessageTooBig(bodylen, mm_cfg.GLOBAL_MAX_MESSAGE_SIZE).rejection_notice(mlist) else: # This will honor the list's forward_auto_discards setting. Moderate.do_discard(mlist, msg) # Doesn't return.