~dricottone/mail-filters

ref: 9302fb18f5020d65665b8177610811c96f999880 mail-filters/src/mailman.awk -rwxr-xr-x 2.3 KiB
9302fb18Dominic Ricottone Initial commit 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/awk -f

# mailman.awk
# ===========
# A filter (as for mutt or aerc) intended to clean & decorate plaintext mail
# from mailman servers, highlighting header information

BEGIN {
  in_todays_topics=0;
  in_header=0;
  do_not_print=0;

  get_boundary=0;
  boundary="";

  dim="\033[2m";
  yellow="\033[33m";
  cyan="\033[36m";
  reset="\033[0m";
}
{
  # get boundary
  if (get_boundary==1) {
    get_boundary=0;
    matched=match($0,/boundary=".*"/);
    if (matched!=0) boundary="--" substr($0,RSTART+10,RLENGTH-11);
    # if failed to extract boundary, resume printing
    else do_not_print=0;
  }

  # skip blocks of non-text (HTML, PGP Signatures, non-text MIME parts)
  if (do_not_print==1 && $0 ~ /(<\/html>|END PGP SIGNATURE)/) do_not_print=0;
  else if (boundary!="" && $0 ~ boundary) do_not_print=0;

  else if ($0 ~ /(<html>|BEGIN PGP SIGNATURE)/) do_not_print=1;
  else if ($0 ~ /^Content-Type:/) {
    in_header=0;
    if ($0 ~ /multipart\/alternative/) { do_not_print=1; get_boundary=1; }
    else if (boundary!="" && $0 ~ /text\/html/) do_not_print=1;
    # for other content types, resume printing
    else do_not_print=0;
  }

  else {
    # identify "Today's Topics" section
    if (in_todays_topics==1 && $0 ~ /^-{5,}/) in_todays_topics=0;
    else if ($0 ~ /^Today's Topics:/) in_todays_topics=1;

    # identify header section
    if (in_header==1 && $0 ~ /^\s*$/) { in_header=0; do_not_print=0; }
    else if ($0 ~ /^(Message|Date|From|To|Subject|Message-ID):/) in_header=1;

    if (in_todays_topics==1) {
      matched=match($0, /\(.+\)/);
      if (matched!=0) {
        original=substr($0, RSTART, RLENGTH);
        replacement=reset original;
        sub(/\(.+\)/,replacement);
      }
      if ($0 ~ /^ +[1-9][0-9]?\./) {
        $1=$1 dim cyan;
        $0=$0 reset;
      }
      else if ($0 !~ /^Today's Topics:/) {
        $0=dim cyan $0 reset
      }
    }
    else if (in_header==1) {
      if ($0 ~ /^(Date|From|Subject):/) {
        do_not_print=0;
        $1=$1 dim cyan;
        $0=$0 reset;
      }
      else if ($0 ~ /^Message:/) {
        do_not_print=0;
        $1=$1 yellow;
        $0=$0 reset;
      }
      else if ($0 ~ /^(To|Message-ID):/) {
        do_not_print=1;
      }
      else {
        $0=dim cyan $0 reset;
      }
    }

    if (do_not_print==0) print $0;
  }
}