source: trunk/server/fedora/config/etc/scripts/prune-mailq @ 2792

Last change on this file since 2792 was 2670, checked in by quentin, 9 years ago
Allow specifying full e-mail addresses for purge-{from,to}
  • Property svn:executable set to *
File size: 4.0 KB
Line 
1#!/bin/sh
2
3set -eu
4shopt -s failglob
5
6usage="Usage:
7    $0 list-from
8    $0 list-to
9    $0 show-rand [from regex|to regex]
10    $0 email lockers...
11    $0 purge-from lockers...
12    $0 purge-to lockers..."
13
14usage() {
15    echo "$usage" >&2;
16    exit 1
17}
18
19clean_locker() {
20    echo "${1%%@scripts.mit.edu}"
21}
22
23canonicalize_address() {
24    if [[ "$1" == *@* ]]; then
25        echo "$1"
26    else
27        echo "$1@scripts.mit.edu"
28    fi
29}
30
31list_from() {
32    echo "Top twenty senders by number of queued messages:"
33    mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { print $7 }' | sort | uniq -c | sort -n | tail -n 20
34}
35
36list_to() {
37    echo "Top twenty recipients by number of queued messages:"
38    mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { print $8 }' | sort | uniq -c | sort -n | tail -n 20
39}
40
41show_rand() {
42    if [[ $# -eq 0 ]]; then
43        files=$(ls /var/spool/postfix/deferred/?/* | shuf | head -n 3)
44    elif [[ $# -eq 2 ]]; then
45        match=$2
46        case "$1" in
47            from) dir=7;;
48            to) dir=8;;
49            *) usage;;
50        esac
51        msgids=$(mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$$dir ~ /$match/) { print \$1 }" | shuf | head -n 3)
52        files=$(for msgid in $msgids; do echo /var/spool/postfix/deferred/${msgid:0:1}/$msgid; done)
53    else
54        usage
55    fi
56    for file in $files; do
57        echo ">>>> $file";
58        postcat "$file"
59        echo;
60    done
61}
62
63tmpl_email() {
64    sender=${SSH_GSSAPI_NAME%%/*}
65    if [[ $# -eq 0 ]]; then
66        echo "Please specify a locker to generate template for." >&2
67        exit 1
68    fi
69    for locker in "$@"; do
70        locker=$(clean_locker "$locker")
71        echo "fs la /mit/$locker/"
72        fs la "/mit/$locker"
73        echo
74        cat <<-EOF
75The scripts.mit.edu servers currently have a large number of email messages destined for the *$locker* account that are not being handled by your account and are being queued. Sufficiently large numbers of queued messages can cause stability issues for the servers, so we would like you to ensure that your account can handle all messages it receives by two weeks from now.
76
77You will be able to process the incoming messages if you sign up for the mail scripts service (http://scripts.mit.edu/mail/). You're welcome to simply forward all incoming mail to another address (the default is to forward it to the mit.edu address of the user who signs up); otherwise, you can configure mail scripts to process the incoming messages in some suitable fashion.
78
79Frequently, large numbers of queued messages are a sign that some wiki, blog, forum, or other site has been spammed. If this is the case, you should apply some appropriate spam-blocking mechanism.
80
81If you have any questions, feel free to contact us.
82
83Thanks,
84scripts.mit.edu team
85scripts@mit.edu --- semi-private
86scripts-root@mit.edu --- service maintainers only
87EOF
88        echo;echo
89    done
90}
91
92purge_from() {
93    if [[ $# -eq 0 ]]; then
94        echo "Please specify a locker to purge emails from" >&2
95        exit 1
96    fi
97    for locker in "$@"; do
98        address=$(canonicalize_address "$locker")
99        echo "$address..."
100        mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$7 == \"$address\") { print \$1 }" | tr -d '*!' | postsuper -d -
101        echo
102    done
103}
104
105purge_to() {
106    if [[ $# -eq 0 ]]; then
107        echo "Please specify a locker to purge emails to" >&2
108        exit 1
109    fi
110    for locker in "$@"; do
111        address=$(canonicalize_address "$locker")
112        echo "$address..."
113        mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$8 == \"$address\" && \$9 == \"\") { print \$1 }" | tr -d '*!' | postsuper -d -
114        echo
115    done
116}
117
118op=${1:-}
119
120# We want to go ahead and show the usage message if there are no args, so
121# don't let the shift fail and end the script because of "set -e"
122shift || :
123
124case "$op" in
125    list-from) list_from;;
126    list-to) list_to;;
127    show-rand) show_rand "$@";;
128    email) tmpl_email "$@";;
129    purge-from) purge_from "$@";;
130    purge-to) purge_to "$@";;
131    *)
132        usage
133        ;;
134esac
135
136# vim: set sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.