On Wed, Sep 30, 2020 at 03:20:50PM +0100, Sam Kuper wrote:
> On Wed, Sep 30, 2020 at 02:18:08PM +0100, Chris Green wrote:
> 
> > I've attached them here anyway.
> 
> Thanks :)  Would you be willing to mention a license?
> 
> Without a license, your scripts are technically non-free software, i.e.
> others don't have the right to distribute them, modify them, or share
> their modifications.
> 
> I'd suggest AGPLv3 as a good default Free Software license
> https://www.gnu.org/licenses/license-list.html#AGPLv3.0 ; but given that
> these are small (<300LOC) programs, you might prefer a "pushover"
> license like Apache v2:
> https://www.gnu.org/licenses/license-recommendations.html#small
> https://www.gnu.org/licenses/license-list.html#apache2 .
> 
OK, I've gone with Apache v2, attached again here.

-- 
Chris Green
#!/usr/bin/python3
#
#
# license    Apache v2 (http://www.apache.org/licenses/LICENSE-2.0) 
# author     Chris Green - ch...@isbd.co.uk
# 
#
#
# Mail filtering script
#
import mailbox
import os
import sys
import time
import mailLib
import shlex
#
#
# Redirect any exceptions to a file
#
sys.stderr = open("/home/chris/tmp/mail.err", 'a')
#
#
# Some constants (i.e. configuration)
#
home = "/home/chris"
logfile = home + "/tmp/mail.log"
filtfile = home + "/.mutt/filter"
mldir = home + "/mail/"
indir = mldir + "In/"
judir = mldir + "Ju/"
#
#
# Set to log to mail.log in ~/tmp with name 'filter' and the envelope/from
#
log = mailLib.initLog("filter")
#
#
# Initialise destination mailbox name to empty
#
dest = ""
#
#
# Read the message from standard input and make a message object from it
#
msg = mailbox.MaildirMessage(sys.stdin.buffer.read())
#
#
# Extract the To:, Cc: and Subject: headers and the envelope/from
#
msgcc = msg.get("Cc", "unknown").lower()
msgto = msg.get("To", "unknown").lower()
msgsb = msg.get("Subject", "unknown")
msgfm = msg.get("From", "unknown").lower()
#
#
# See if it's in our filter file
#
f = open(filtfile, 'r')
for ln in f:                    # for each line in filter
    if ln[0] == '#':            # ignore comments
        continue
    #
    #
    # split the line into fields, shlex.split() does quoted strings, add a field
    # to create a dummy fourth field if there isn't one in the filter file
    #
    fld = shlex.split(ln)
    fld.append("XXXXYYYYZZZZ")
    #
    #
    # copy the fields into better named variables
    #
    nm = fld[0]             # name/alias
    dd = fld[1] + "/"       # destination directory
    tocc = fld[2].lower()   # list address
    sbstrip = '[' + fld[3] + ']'        # string to match in and/or strip out of subject
    #
    #
    # see if the filter To/CC column matches the message To: or Cc: or if sbstrip is in Subject:
    #
    if (tocc in msgcc or tocc in msgto or sbstrip in msgsb):
        #
        #
        # set the destination directory
        #
        dest = mldir + dd + nm
        #
        #
        # Strip out list name (4th field) from subject if it's there
        #
        if sbstrip in msgsb:
            msg.replace_header("Subject", msgsb.replace(sbstrip, ''))
        #
        #
        # we've found a match so assume we won't get another
        #
        break
#
#
# if destination mb name hasn't been set yet then set to In/default
# (mail with 'chris' in destination will get to 'inbox')
#
if dest == "":
    dest = indir + "default"

mailLib.deliverMdMsg(dest, msg, log)
#!/usr/bin/python
#
#
# license    Apache v2 (http://www.apache.org/licenses/LICENSE-2.0) 
# author     Chris Green - ch...@isbd.co.uk
# 
import sys
#
#
#
#
home = "/home/chris"
filtfile = home + "/.mutt/filter"
#
#
# Get mailing lists from filter file
#
f = open(filtfile, 'r')
for ln in f:
    if ln[0] == '#':            # ignore comments
        continue
    #
    #
    # split the line into fields
    #
    fld = ln.split()
    tocc = fld[2]

    if (":x" in fld[1]):
        continue
    sys.stdout.write("alias ")
    sys.stdout.write(fld[0] + " ")
    sys.stdout.write(tocc + "\n")
#!/usr/bin/python
#
#
# license    Apache v2 (http://www.apache.org/licenses/LICENSE-2.0) 
# author     Chris Green - ch...@isbd.co.uk
# 
import sys
#
#
#
#
home = "/home/chris"
filtfile = home + "/.mutt/filter"

#
#
# Get mailing lists from filter file
#
f = open(filtfile, 'r')
for ln in f:
    if ln[0] == '#':            # ignore comments
        continue
    #
    #
    # split the line into fields
    #
    fld = ln.split()
    if (fld[1][0:4] == "Li:x"):
        continue                # don't output if there's an x flag
    if (fld[1][0:2] == "Li"):
        sys.stdout.write(fld[2] + " ")    # output the list address
#
#
# license    Apache v2 (http://www.apache.org/licenses/LICENSE-2.0) 
# author     Chris Green - ch...@isbd.co.uk
# 
import mailbox
import logging
import logging.handlers
import os
import time
#
#
# log a message
#
def initLog(name):
    log = logging.getLogger(name)
    log.setLevel(logging.DEBUG)
    f = logging.handlers.RotatingFileHandler("/home/chris/tmp/mail.log", 'a', 1000000, 4)
    f.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    f.setFormatter(formatter)
    log.addHandler(f)
    return log
#
#
# Deliver a message to a local maildir
#
def deliverMdMsg(dest, msg, log):
    #
    #
    # Create the destination maildir instance
    #
    md = mailbox.Maildir(dest, factory=None)

    log.info("From: " + msg.get("From", "unknown"))
    log.info("Destination is: " + dest)
    #
    #
    # Put the incoming message in the appropriate maildir
    # No need to lock, it's a maildir
    #
    try:
        md.add(msg)
    except exception as e:
        log.info("Failed to store message:" + e)

    return

Reply via email to