Skip to content

Commit

Permalink
fix: cached and sieve filter generation
Browse files Browse the repository at this point in the history
added more mblaze deps, fixes too long sieve filter bug
  • Loading branch information
jaromil committed Dec 17, 2023
1 parent fd96bdd commit 91e0bb8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 53 deletions.
9 changes: 9 additions & 0 deletions src/jaro
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ main() {
option_subcommands[help]=""

option_subcommands[update]=""
option_subcommands[sieve]=""

option_subcommands[stat]=""

Expand Down Expand Up @@ -631,6 +632,14 @@ EOF
notice "Initialization completed in $MAILDIRS"
;;

sieve)
read_account
mblaze_setup
update_filters
update_sieve
notice "Sieve filters updated in $MAILDIRS"
;;

help) usage ;;

index) nm_setup
Expand Down
133 changes: 80 additions & 53 deletions src/zlibs/filters
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ update_filters() {
local ff="$MAILDIRS/cache/filters"
${=mkdir} "$MAILDIRS/cache"

[[ -r "$ff" ]] && { rm -f "$ff" }
[[ -r "$ff" ]] && rm -f "$ff"
newlock "$ff"
cat <<EOF >> "$ff"
# automatically generated by jaromail
# generated on `date`
typeset -Al filter_from
typeset -alU filter_own
typeset -Al filter_to
Expand All @@ -121,7 +121,7 @@ EOF
/^./ { print $1 ";" $2 ";" $3 ";" $4 }' "$MAILDIRS/Filters.txt"`

#
func "insert filter rules in the cache"
act "insert filter rules in the cache"

for f in ${(f)ffilters}; do
header="${f[(ws:;:)1]}"
Expand Down Expand Up @@ -156,8 +156,8 @@ EOF
# }

#
func "compile the list of own addresses and aliases"

act "generate the list of own addresses and aliases"
print "\n# own accounts" >> "$ff"
for i in `awk '
/^#/ { next }
/^$/ { next }
Expand All @@ -167,6 +167,7 @@ EOF
filter_own+=($i)
EOF
done
print "\n# own aliases" >> "$ff"
test -r $MAILDIRS/Aliases.txt && {
for i in `awk '
/^#/ { next }
Expand All @@ -178,31 +179,29 @@ EOF
done
}

func "insert addressbook whitelist in the cache"
act "insert addressbook whitelist in the cache"
ffilters=''
ADDRESSBOOK=whitelist.abook
ffilters=`search_addressbook | getemail`
ffilters=`all_addressbook_emails`
print "\n# own whitelist addressbook" >> "$ff"
for f in ${(f)ffilters}; do
cat <<EOF >> "$ff"
filter_from+=("${f}" "known")
EOF
print "filter_whitelist+=(\"${f}\")" >> "$ff"
done

func "insert addressbook blacklist in the cache"
act "insert addressbook blacklist in the cache"
ffilters=''
ADDRESSBOOK=blacklist.abook
ffilters=`search_addressbook | getemail`
ffilters=`all_addressbook_emails`
print "\n# own blacklist addressbook" >> "$ff"
for f in ${(f)ffilters}; do
cat <<EOF >> "$ff"
filter_from+=("${f}" "zz.blacklist")
EOF
print "filter_blacklist+=(\"${f}\")" >> "$ff"
done

#
func "unlocking and compiling the cache"

act "unlocking and compiling the cache"
unlock "$ff"
zcompile "$ff"
func "recursive reload"
source "$WORKDIR/zlibs/filters"
func "reload"
source "$MAILDIRS/cache/filters"
return 0
}

Expand Down Expand Up @@ -240,7 +239,11 @@ filter_maildir() {
return 1
}

mails=`${=find} "$MAILDIRS/$mdinput" -maxdepth 2 -type f`
if command -v mlist > /dev/null; then
mails=`mlist "$mdinput"`
else
mails=`${=find} "$mdinput" -maxdepth 2 -type f`
fi
numm=`print $mails | wc -l`

[[ $numm = 0 ]] && {
Expand Down Expand Up @@ -291,13 +294,9 @@ filter_maildir() {
# run all filter regexps on the from: field
_dest=""
e_addr=()
local femail=""
if command -v maddr > /dev/null; then
femail=`maddr -h from "$m" | getemail`
else
hdr "$m" | e_parse From
femail="${(k)e_addr}" # e_parse From hit is always one
fi
local femail=`maddr -h from "$m" | getemail`

# fuzzy match if in own Filters.txt
for exp in ${(k)filter_from}; do
local found=0
if [[ "$femail" == "$exp" ]]; then
Expand Down Expand Up @@ -336,6 +335,9 @@ filter_maildir() {
}

_dest=""
# TODO: use mblaze here instead of hdr and
# fetchaddr. also: honor SIGQUIT during filtering for a
# clean quit on ctrl-c
# recompile the array of destination addresses
e_addr=()
hdr "$m" | e_parse To
Expand Down Expand Up @@ -391,6 +393,23 @@ filter_maildir() {

} # own filters

# simple match if in whitelist
if ((${filter_whitelist[(Ie)$femail]})); then
[[ "known" == "$mdinput" ]] && {
func "$c\t/ $numm"
continue
}
act "$c\t/ $numm\t-> known\t(from $femail)"
[[ $DRYRUN = 1 ]] || {
print "$m" | mrefile "${MAILDIRS}/known"
[[ $? = 0 ]] || {
error "Error filtering to maildir: known"
error "File: $m"
}
}
continue
fi

hdr "$m" | awk '/X-Spam-Flag.*YES/ { exit 1 }'
{ test $? = 0 } || {
[[ "$mdinput" = "Spam" ]] && {
Expand All @@ -405,6 +424,25 @@ filter_maildir() {
}
}

# simple match if in blacklist
[[ -d "${MAILDIRS}/zz.blacklist" ]] ||
mmkdir "${MAILDIRS}/zz.blacklist"
if ((${filter_blacklist[(Ie)$femail]})); then
[[ "zz.blacklist" == "$mdinput" ]] && {
func "$c\t/ $numm"
continue
}
act "$c\t/ $numm\t-> zz.blacklist\t(from $femail)"
[[ $DRYRUN = 1 ]] || {
print "$m" | mrefile "${MAILDIRS}/zz.blacklist"
[[ $? = 0 ]] || {
error "Error filtering to maildir: zz.blacklist"
error "File: $m"
}
}
continue
fi

# parse own email and aliases
match=0
for f in ${(k)e_addr}; do
Expand All @@ -426,7 +464,8 @@ filter_maildir() {
}
}
done
[[ $match = 1 ]] && continue

[[ $match == 1 ]] && continue

# if here then file to unsorted
if [ "$mdinput" = "unsorted" ]; then
Expand Down Expand Up @@ -499,7 +538,7 @@ update_sieve() {

#######
# SIEVE
act "generating sieve filter rules"
act "generate sieve filter rules"
id=`datestamp`.$RANDOM
newlock "$MAILDIRS/Filters.sieve"
rm -f "$MAILDIRS/Filters.sieve"
Expand All @@ -513,19 +552,13 @@ EOF

# blacklist
[[ -r "$MAILDIRS"/blacklist.abook ]] && {
sieve_filter_array=()

for i in `awk -F'=' '
/^email/ { print $2 }
' "$MAILDIRS"/blacklist.abook`; do
sieve_filter_array+=("$i")
done

{ test "${#sieve_filter_array}" = "0" } || {
sieve_filter_array=($filter_blacklist)
[[ "${#sieve_filter_array}" = "0" ]] || {
sieve_filter \
'if header :contains "From"' \
zz.blacklist
}
sieve_filter_array=()
}

# bounces
Expand All @@ -547,29 +580,23 @@ EOF
set -A sieve_filter_map ${(kv)filter_from}
sieve_complex_filter 'if header :contains [ "From","Sender" ] '




##############################################################
# if the sender is known (whitelist) then put mail in
# high priority 'known' maildir or INBOX (sieve)

act "compiling whitelist rules from addressbook"
act "generate sieve rules for addressbooks"
func "generating whitelist for sieve filters"

[[ -r "$MAILDIRS"/whitelist.abook ]] && {
sieve_filter_array=()
for i in `awk -F'=' '
/^email/ { print $2 }
' "$MAILDIRS"/whitelist.abook`; do
sieve_filter_array+=("$i")
done
sieve_filter \
'if header :contains [ "From","Sender" ]' \
INBOX
sieve_filter_array=($filter_whitelist)
[[ "${#sieve_filter_array}" = "0" ]] || {
sieve_filter \
'if header :contains [ "From","Sender" ]' \
INBOX
}
}

cat <<EOF >> "$MAILDIRS/Filters.sieve"
cat <<EOF >> "$MAILDIRS/Filters.sieve"
# spam
if header :is "X-Spam-Flag" "YES" {
fileinto :create "Spam"; stop;
Expand Down

0 comments on commit 91e0bb8

Please sign in to comment.