Browse Source

Improved matching.

master
Sophie Hirn 3 years ago
parent
commit
c74fbcb4cc
2 changed files with 30 additions and 95 deletions
  1. +13
    -21
      README.md
  2. +17
    -74
      mdod.sh

+ 13
- 21
README.md View File

@@ -42,31 +42,27 @@ As all files are bash scripts, writing your own plugins should be easy.

### Matchers

All Matchers except `has[_no]_flag` are case insensitive.
All matchers take PCREs that match on the entire header line.

* `subject_contains <keyword>`
* `subject <regex>`

* `subject_is <keyword>`
* `from <regex>`

* `from_contains <keyword>`
* `to <regex>`

* `from_is <keyword>`
* `cc <regex>`

* `to_contains <keyword>`
* `bcc <regex>`

* `to_is <keyword>`
* `involves <regex>` (Matches if from to, cc or bcc matches)

* `involves_contains <keyword>` (Matches if either from_contains or to_contains matches)
* `return_path <regex>`

* `involves_is <keyword>` (Matches if either from_is or to_is matches)
These matchers take a date(1) compatible date string.

* `return_path_contains <keyword>`
* `newer_than <date>`

* `return_path_is <keyword>`

* `has_flag <flag>`

* `has_no_flag <flag>`
* `older_than <date>`

### Actions

@@ -74,10 +70,6 @@ All Matchers except `has[_no]_flag` are case insensitive.
the mail box root specified by `box`. Will automatically append
`new`/`tmp`/`cur` to the path.

* `set_flag <flag>`: Set the specified maildir flag.

* `clear_flag <flag>`: Clear the specified maildir flag.

### Example Config

#!/bin/bash
@@ -92,7 +84,7 @@ All Matchers except `has[_no]_flag` are case insensitive.

# Sort mail
box "/home/boonami/.mail/sophie.hirn@wyvernscale.com"
match "INBOX" to_contains "list" move "INBOX/Lists"
match "INBOX" to "list" move "INBOX/Lists"

box "/home/boonami/.mail/boonami@wyvernscale.com"
match "INBOX" to_contains "list" move "INBOX/Lists"
match "INBOX" to "list" move "INBOX/Lists"

+ 17
- 74
mdod.sh View File

@@ -75,70 +75,50 @@ shopt -s nullglob
# Matchers

# Subject
function subject_contains {
function subject {
# Perl snippet from https://superuser.com/a/972248

get_header "$1" \
| grep "^Subject: " \
| perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' \
| grep -q "^Subject: .*${2}.*"
}

function subject_is {
# Perl snippet from https://superuser.com/a/972248

get_header "$1" \
| grep "^Subject: " \
| perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' \
| grep -q "^Subject: ${2}\$"

| grep "^Subject:" \
| grep -qP "$2"
}



# From
function from_contains {
get_header "$1" | grep -qi "^From: .*${2}.*"
}

function from_is {
get_header "$1" | grep -qi "^From: ${2}\$"

function from {
get_header "$1" | grep -i "^From:" | grep -qP "$2"
}



# To
function to_contains {
get_header "$1" | grep -qi "^To: .*${2}.*"
function to {
get_header "$1" | grep -i "^To:" | grep -qP "$2"
}

function to_is {
get_header "$1" | grep -qi "^To: ${2}\$"
function cc {
get_header "$1" | grep -i "^Cc:" | grep -qP "$2"
}

function bcc {
get_header "$1" | grep -i "^Bcc:" | grep -qP "$2"
}



# Involves
function involves_contains {
from_contains $@ || to_contains $@
}

function involves_is {
from_is $@ || to_is $@
function involves {
get_header "$1" | grep -i -e "^From:" -e "^To:" -e "^Cc:" -e "^Bcc:" | grep -qP "$2"
}



# Return-Path
function return_path_contains {
get_header "$1" | grep -qi "^Return-Path: .*${2}.*"
}

function return_path_is {
get_header "$1" | grep -qi "^Return-Path: ${2}\$"

function return_path {
get_header "$1" | grep "^Return-Path:" | grep -qP "$2"
}


@@ -156,17 +136,6 @@ shopt -s nullglob



# Flags
function has_flag {
echo "$1" | grep -q ":2,.*$2.*"
}

function has_no_flag {
echo "$1" | grep -vq ":2,.*$2.*"
}



# ACTIONS

function move {
@@ -208,39 +177,13 @@ shopt -s nullglob



function set_flag {
echo " set_flag $@"
echo " currently disabled."

# if ! has_flag "$1" "$2"; then
# mv "$1" "$1$2"
# fi
}



function clear_flag {
echo " clear_flag $@"

if has_flag "$1" "$2"; then
local new=$(echo "$1" | sed "s_\(.*\:2,.*\)$2\(.*\)_\1\2_")
mv "$1" "$new"
fi
}

function noop {
echo " noop $@"
}



# INTERNAL

function get_header {
while IFS= read l; do
test -z "$l" && break
echo "$l" 2>/dev/null
done <"$1"
done <"$1" | sed ':a;$!N;s/\n[ \t][ \t]*/ /;ta;P;D'
}

function matchHelper {