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

Last change on this file since 2628 was 2609, checked in by mitchb, 9 years ago
Spammers get you coming and going Enhance prune-mailq to be able to give you a list of top offenders, either by sender or by first recipient, and to allow you to purge all mail from a specific sender or all mail solely destined for a specific recipient. Also fix a tiny bit of grammar.
  • Property svn:executable set to *
File size: 3.5 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
10    $0 email lockers...
11    $0 purge-from lockers...
12    $0 purge-to lockers..."
13
14clean_locker() {
15    echo "${1%%@scripts.mit.edu}"
16}
17
18list_from() {
19    echo "Top twenty senders by number of queued messages:"
20    mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { print $7 }' | sort | uniq -c | sort -n | tail -n 20
21}
22
23list_to() {
24    echo "Top twenty recipients by number of queued messages:"
25    mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { print $8 }' | sort | uniq -c | sort -n | tail -n 20
26}
27
28show_rand() {
29    files=$(ls /var/spool/postfix/deferred/?/* | shuf | head -n 3)
30    for file in $files; do
31        echo ">>>> $file";
32        strings "$file"
33        echo;
34    done
35}
36
37tmpl_email() {
38    sender=${SSH_GSSAPI_NAME%%/*}
39    if [[ $# -eq 0 ]]; then
40        echo "Please specify a locker to generate template for." >&2
41        exit 1
42    fi
43    for locker in "$@"; do
44        locker=$(clean_locker "$locker")
45        echo "fs la /mit/$locker/"
46        fs la "/mit/$locker"
47        echo
48        cat <<-EOF
49The 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.
50
51You 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.
52
53Frequently, 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.
54
55If you have any questions, feel free to contact us.
56
57Thanks,
58scripts.mit.edu team
59scripts@mit.edu --- semi-private
60scripts-root@mit.edu --- service maintainers only
61EOF
62        echo;echo
63    done
64}
65
66purge_from() {
67    if [[ $# -eq 0 ]]; then
68        echo "Please specify a locker to purge emails from" >&2
69        exit 1
70    fi
71    for locker in "$@"; do
72        locker=$(clean_locker "$locker")
73        echo "$locker..."
74        mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$7 == \"$locker@scripts.mit.edu\") { print \$1 }" | tr -d '*!' | postsuper -d -
75        echo
76    done
77}
78
79purge_to() {
80    if [[ $# -eq 0 ]]; then
81        echo "Please specify a locker to purge emails to" >&2
82        exit 1
83    fi
84    for locker in "$@"; do
85        locker=$(clean_locker "$locker")
86        echo "$locker..."
87        mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$8 == \"$locker@scripts.mit.edu\" && \$9 == \"\") { print \$1 }" | tr -d '*!' | postsuper -d -
88        echo
89    done
90}
91
92op=${1:-}
93
94# We want to go ahead and show the usage message if there are no args, so
95# don't let the shift fail and end the script because of "set -e"
96shift || :
97
98case "$op" in
99    list-from) list_from;;
100    list-to) list_to;;
101    show-rand) show_rand;;
102    email) tmpl_email "$@";;
103    purge-from) purge_from "$@";;
104    purge-to) purge_to "$@";;
105    *)
106        echo "$usage" >&2;
107        exit 1
108        ;;
109esac
110
111# vim: set sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.