#!/bin/bash # $Id: chkmail,v 1.224 2016/02/08 18:59:01 fulford Exp $ # $Source: /src/merlin/usr/local/etc/RCS/chkmail,v $ # $Revision: 1.224 $ # Author C W Fulford. # Copyright 2014-2015 (c) C W Fulford. # Licensed for public use under the LGPL, . # For assistance contact fulford@fulford.net 0709 229 5385 ######################################################################## cmd=`basename $0` [ $cmd == "debrianail" ] && expunge=":" tmp=/var/tmp/$cmd.$$ case $cmd in chkmail) syntax="$cmd [-a <addr>] [-b <mbox> ] [-c <config>] [-d] [-i <imap_server>] [-L] [-l] [-m <#max>] [-n] [-P <password>] [-p <port> [-u <username>] [-v] [<config_id>|ALL]|[-V]";; debrianail) syntax="$cmd [-c <config>] [-d] [-i <imap_server>] [-l] [-m <mbox>] [-n] [-P <password>] [-p <port> [-v] [-w <#>] <mail_address>|[-V]";; esac while [ $# -gt 0 ] ;do case $1 in -a) [ $cmd = "debrianail" ]&&{ echo $syntax>&2; exit 1 } echo "addr=$2">>$tmp;shift 2;; -b) echo "mbox=$2">>$tmp;shift 2;; -c) config=$2;shift;; -d) debug=:;set -x;shift;; -i) echo "srvr=$2">$tmp;shift 2;; -L) [ $cmd = "debrianail" ]&&{ echo $syntax>&2; exit 1 } list=":";shift;; -l) log=:;shift;; -m) echo "max=$2">>$tmp;shift 2;; -n) notice=:;shift;; -P) echo "passwd=pasSword4u -p) port=$2>>$tmp;shift 2;; -u) echo "user=$2">>$tmp;shift 2;; -v) verbose=:;nl="\\n";shift;; -V) echo "$cmd $Revision: 1.224 $ $Date: 2016/02/08 18:59:01 $"|awk '{print $1,$3,$6}';exit;; -*) echo "syntax: $syntax">&2;exit 1;; *) idlist=$1;shift;; esac done config=${config:="/usr/local/etc/chkmail.cf"} [ -n $idlist ] ||{ echo "syntax: $syntax" >&2;exit 1;} [ $idlist == ALL -a $cmd == "chkmail" ]&&{ [ "$verbose" ] && echo $cmd: checking all ids in $config >&2 idlist=`sed <$config -ne "/^[^ #@]*:/s/:/ /p"` } [ -n "$idlist" ] ||{ echo "syntax: $syntax" >&2;exit 1;} cnt_msgs=/usr/local/etc/msg_cnt lst_mbxs=/usr/local/etc/lst_mbxs del_msgs=/usr/local/etc/msg_del for id in $idlist ;do unset addr alert archives afolders max mbox po srvr user [ -r "$config" -a -n "$id" -a $id != "ALL" ] && { if grep "$id:">/dev/null $config;then eval `sed -ne '/^'$id':/,/^$/{/^[ ][^#]/p}' $config` else echo "$cmd: $id not found in $config" >&2 exit 1 fi } [ -f $tmp ] && . $tmp case $port in 22) proto=ssh;; 143) proto=imap;; 993) proto=ssl;; "") port=22;proto=ssh; [ "$verbose" ] && echo "$cmd: using port 22." >&2;; *) echo "$cmd: proceedure for $port not known." >&2;exit;; esac proto=${proto:-"ssl"} # set defaults if nothing found addr=${addr:-"`id -nu`@`hostname -d`"} alert=${alert:-$addr} max=${max:-100} mbox=${mbox:-"INBOX"} po=`echo $addr|awk -F@ '{split($2,a,".");print a[1]}'` srvr=${srvr:-"imap.`hostname -d`"} user=${user:-$addr} if [ "$expunge" ];then [ "$verbose" ] &&{ echo "$cmd: Warning messages in excess of $max will be deleted" } fi if [ "$list" ];then expect -f $lst_mbxs $srvr $proto $addr $mbox "$passwd"| awk '/^* LIST/{gsub(/"/,"",$5);print $5}' exit fi #Check if host name resolves getent hosts $srvr >/dev/null||{ echo -e "${nl}$cmd: Can't find host \"$srvr\".">&2 exit 1 } [ "$mbox" == "$folders" ] && folders="" nc -zw 2 $srvr $port </dev/null || { echo -e "${nl}Port $port unavailable on $srvr ($user).">&2 exit 1 } for n in "$folders" ;do echo $folders|egrep -wq '.Archive' && archives=: done if [ "$archives" ];then ay=2012 while [ $ay -lt `date "+%Y"` ];do afolders="$afolders INBOX.Archive.$ay" ay=`expr $ay + 1` done folders=`echo $folders|sed -e "s/\.Archive/$afolders/"` fi for m in $mbox $folders ;do #[ "$verbose" ] && printf "%s%8s%s""chkmail:",${user},"'s",$m,"on",$srvr [ "$verbose" ] && printf "%s%-39s%-18s%s" "chkmail:" "$user's " $m " on $srvr:" if [ $port -eq 22 ];then mdomain=`echo $addr|awk -F@ '{print $2}'` vuser=`echo $addr|awk -F@ '{print $1}'` f=`echo $m|awk '{sub(/INBOX/,"");print}'` mdir=/var/qmail/mailnames/$mdomain/$vuser/Maildir/$f total=`ssh $srvr "sudo find $mdir/{cur,new} -type f" 2>/dev/null|wc -l` else suser=`echo $user|sed -ne 's/^\(.*\)@.*/\1/p'` total=`expect -f $cnt_msgs $srvr $proto $addr $m "$passwd"|\ awk '/EXISTS/{print $2}'` fi if [ -n "$total" ];then if [ "$verbose" ]; then #printf "%6d\t$m\n" $total printf "%6d\n" $total elif [ ${total:-0} -ge $max -a $m == "INBOX" ];then echo "$cmd: Warning: ${user}'s $m on $srvr has $total messages" >&2 fi [ "$log" ] && { logger -p mail.notice -t $cmd "$total messages in $user $m at $srvr." } [ ${total:-0} -gt "$max" -a "$notice" ] && { fname=`echo $alert|awk -F@ '{split($1,a,".");print a[1]}'` fname=`echo $fname|awk -F@ '{sub(".",substr((toupper($1)),1,1),$1);print}'` mailx -s "Your $mbox at $po" $alert <<- . $fname You have $total messages in your mail folder $m at $po. Please delete or archive old messages. If messages need to be kept on-line move them to other folders. Try to keep your inbox to under $max messages. Clifford W Fulford Mail Administrator . } if [ "$expunge" ];then if [ $total -lt $max ];then echo "$cmd: Total messages ($total) less then maximum ($max)">&2 exit 1 else del=`expr $total - $max` expect -f $del_msgs $srvr $proto $addr $m "$passwd" $del total=`expr $total - $del` [ "$log" ] &&{ logger -p mail.notice -t $cmd "$del messages deleted for $user $m at $srvr." logger -p mail.notice -t $cmd "$total messages in $user $m at $srvr." } [ $verbose ] &&{ s=s;[ $del -eq 1 ] && s="" echo "$cmd: $del message$s deleted for $user $m at $srvr." echo "$cmd: $total message$s remaining for $user $m at $srvr." } fi fi else [ "$verbose" ] && echo "$cmd: $user $mbx not found." [ "$log" ] && { logger -p mail.notice -t $cmd "$user $m at $srvr. Not found!" } fi done done [ -f "$tmp" ] && rm $tmp ###################################################################### # This program is free software: you can redistribute it and or # # modify it under the terms of the Lesser GNU General Public License # # as published by the Free Software Foundation, either version 3 of # # the License, or (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # A copy of the Lesser GNU General Public License and the GNU # # General Public License is available at # # <http://www.gnu.org/licenses/>. # ######################################################################
Copyright
© 2003-2017
Clifford W Fulford.
Fulford Consulting Ltd.
Regd. Co. 4250037 in England & Wales.
Regd. office 162, Edward Rd. Nottingham NG2 5GF, England, UK.
Related web sites: City Linux | Flare Support | West Bridgford | Fulford Portal | Joan Mary Fulford (Nottingham Writer) | Fulford Gallery | Amharic Interpreter | Arabic Interpreter | Tigrinya Interpreter
The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.