[Mailman-Users] Add spam filter regex rule via command line

Mark Sapiro mark at msapiro.net
Sun May 28 14:38:00 EDT 2017


On 05/26/2017 07:32 AM, Dang Tran wrote:
> 
> I’d like to add a spam filter rule:
> 
> X-Spam-Status: Yes
> 
> Action:  hold
> 
> If I have 5-10 lists, I can goto list admin page and add this manually.  However if I have 500 lists (not saying I do), it would be impractical to perform this manually.
> 
> Please could someone help me with a script to iterate through all my list and add this settings?


You can use Mailman's bin/config_list for this. Do do this, you would
first make a file with contents like

header_filter_rules = [(u'X-Spam-Status: Yes', 7, False)]

The rules are a list of tuples of the form (regexp, action, flag) where
regexp is the regular expression(s) as a unicode string, action is a
dumber as defined in Defaults.py and flag is always False. There can be
more than one regexp in the string separated by new-lines, e.g.
u'X-Spam-Status: Yes\nsecond regexp'. The actions are defined as

# Actions
DEFER = 0
APPROVE = 1
REJECT = 2
DISCARD = 3
SUBSCRIBE = 4
UNSUBSCRIBE = 5
ACCEPT = 6
HOLD = 7

APPROVE, SUBSCRIBE and UNSUBSCRIBE aren't relevant here. The flag is
used by the web UI and is only True for a just inserted empty rule.

You could then run

#!/bin/bash
for list in `bin/list_lists --bare; do
  bin/config_list -i path/to/file $list;
done

This will only work if you want to set header_filter_rules to a
particular rule or list of rules. If you want to append your rule to
possibly existing header_filter_rules you could use a withlist script
something like

import mm_cfg
def add_rule(mlist):
    if not mlist.Locked():
        mlist.Lock()
    mlist.header_filter_rules.append((u'X-Spam-Status: Yes', 7, False))
    mlist.Save()
    mlist.Unlock()

and save that in Mailman's bin directory as add_rule.py and run

bin/withlist -a -r add_rule

to append the rule to header_filter_rules for all lists. To insert the
rule as the first rule, you would change

    ...append((u'X-Spam-Status: Yes', 7, False))

to

    ...insert(0, (u'X-Spam-Status: Yes', 7, False))

-- 
Mark Sapiro <mark at msapiro.net>        The highway is for gamblers,
San Francisco Bay Area, California    better use your sense - B. Dylan


More information about the Mailman-Users mailing list