"Fossies" - the Fresh Open Source Software Archive

Member "mailman-3.3.7/src/mailman/rules/administrivia.py" (10 Nov 2022, 3773 Bytes) of package /linux/misc/mailman-3.3.7.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "administrivia.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.3.5_vs_3.3.6.

    1 # Copyright (C) 2007-2022 by the Free Software Foundation, Inc.
    2 #
    3 # This file is part of GNU Mailman.
    4 #
    5 # GNU Mailman is free software: you can redistribute it and/or modify it under
    6 # the terms of the GNU General Public License as published by the Free
    7 # Software Foundation, either version 3 of the License, or (at your option)
    8 # any later version.
    9 #
   10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
   11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   13 # more details.
   14 #
   15 # You should have received a copy of the GNU General Public License along with
   16 # GNU Mailman.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 """The administrivia rule."""
   19 
   20 from email.iterators import typed_subpart_iterator
   21 from mailman.config import config
   22 from mailman.core.i18n import _
   23 from mailman.interfaces.rules import IRule
   24 from public import public
   25 from zope.interface import implementer
   26 
   27 
   28 # The list of email commands we search for in the Subject header and payload.
   29 # We probably should get this information from the actual implemented
   30 # commands.
   31 EMAIL_COMMANDS = {
   32     # keyword: (minimum #args, maximum #args)
   33     'confirm':     (1, 1),
   34     'help':        (0, 0),
   35     'info':        (0, 0),
   36     'lists':       (0, 0),
   37     'options':     (0, 0),
   38     'password':    (2, 2),
   39     'remove':      (0, 0),
   40     'set':         (3, 3),
   41     'subscribe':   (0, 3),
   42     'unsubscribe': (0, 1),
   43     'who':         (0, 2),
   44     }
   45 
   46 
   47 @public
   48 @implementer(IRule)
   49 class Administrivia:
   50     """The administrivia rule."""
   51 
   52     name = 'administrivia'
   53     description = _('Catch mis-addressed email commands.')
   54     record = True
   55 
   56     def check(self, mlist, msg, msgdata):
   57         """See `IRule`."""
   58         # The list must have the administrivia check enabled.
   59         if not mlist.administrivia:
   60             return False
   61         # First check the Subject text.
   62         lines_to_check = []
   63         subject = str(msg.get('subject', ''))
   64         if subject != '':
   65             lines_to_check.append(subject)
   66         # Search only the first text/plain subpart of the message.  There's
   67         # really no good way to find email commands in any other content type.
   68         for part in typed_subpart_iterator(msg, 'text', 'plain'):
   69             payload = part.get_payload()
   70             lines = payload.splitlines()
   71             # Count lines without using enumerate() because blank lines in the
   72             # payload don't count against the maximum examined.
   73             lineno = 0
   74             for line in lines:
   75                 line = line.strip()
   76                 if len(line) == 0:
   77                     continue
   78                 lineno += 1
   79                 if lineno > int(config.mailman.email_commands_max_lines):
   80                     break
   81                 lines_to_check.append(line)
   82             # Only look at the first text/plain part.
   83             break
   84         # For each line we're checking, split the line into words.  Then see
   85         # if it looks like a command with the min-to-max number of arguments.
   86         for line in lines_to_check:
   87             words = [word.lower() for word in line.split()]
   88             if words[0] not in EMAIL_COMMANDS:
   89                 # This is not an administrivia command.
   90                 continue
   91             minargs, maxargs = EMAIL_COMMANDS[words[0]]
   92             if minargs <= len(words) - 1 <= maxargs:
   93                 msgdata['moderation_sender'] = msg.sender
   94                 with _.defer_translation():
   95                     # This will be translated at the point of use.
   96                     msgdata.setdefault('moderation_reasons', []).append(
   97                         _('Message contains administrivia'))
   98                 return True
   99         return False