aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/runfetchmail
blob: 2b40f5112f9de6bbbaca81a2ef3a2c1de272d7d5 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh
# Runfetchmail 1.1
#
# Copyright (c) 1997 Doug Muth, Wescosville, Pennsylvania USA
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Please send bug reports, suggestions, and flames to: dmuth@ot.com
#
# This shell script is used as a "frontend" for running fetchmail.  It will
#	start up fetchmail and save the session to disk, generate statistics
#	of the e-mail that you downloaded, and tell you how many messages
#	you have in various folders.  A copy of these results are also
#	e-mailed to you.
#
# An rc file is also supported.  If the file $HOME/.runfetchmailrc 
#	exists, it will be sourced.  This way, you can place runfetchmail
#	into /usr/local/bin, and individual users can have their own settings.
#
# Pre-requisites: You must have procmail, or at least `mailstat', a 
#	utility that comes with procmail, running on your system.  You must 
#	also have `timer', a shell script written by me, if you would like the 
#	total time that the transfer took to be displayed.
#
# Syntax: runfetchmail [-every]
#	-every Downloads all messages from the mailserver, regardless of
#		their size and whether they have been previously downloaded.
#
# Changes in version 1.1: The argument "-every" is supported.  I removed the
#	$EXIT_CODE variable since I had problems assigning the exit code from 
#	fetchmail to it.

# Command line to run fetchmail
FETCHMAIL="/usr/local/bin/fetchmail"

# Your procmail logfile
LOG=$HOME/procmail/log

# Do we want to use timer?  Set to 0 to disable.
TIMER=1

# Our path to sendmail with parameters
SENDMAIL="/usr/bin/sendmail -oi -t"

# Who am I?
SELF="dmuth@ot.com"

# The folders that I should check for the number of messages
FOLDERS="$MAIL $HOME/mail/lists"

# Number of seconds to "sleep" for while procmail finishes up, increase
# 	this if you have a really slow system
LATENT=10

# Do we want to use mailstat?  Set to 0 to disable
MAILSTAT=1

# Do we want to e-mail the output to myself?  Set to 0 to disable.
# I strongly suggest doing this so that if you lose your connection to
#	the net part of the way through a download, you can see how much 
#	progess was made
E_MAIL=1

###
# End of user defined variables
###

# The temp file, and ensure my privacy!
TMP=/tmp/fetchmail.sh.$$

# The version of this program
VERSION="Runfetchmail 1.1"

# Trap errors
trap "rm -f $TMP; echo ""Exiting at user request"" ; \
test $TIMER -eq 1 && timer -stop -id $$ >/dev/null; exit 1" \
2 3 4 15

# Source the user's rc file if it exists
test -e $HOME/.runfetchmailrc && . $HOME/.runfetchmailrc

num_mail()
{ # This procedure tells me how many messages there are in each folder
for D in $* 
do
	if test -f $D
	then
		echo "There are `frm $D |wc -l` messages in $D"
	fi
done
}

getmail()
{ # Fetch the mail!

test $TIMER -eq 1 && timer -start -id $$ -quiet

$FETCHMAIL $@

# pause for a short while
echo "Now sleeping for $LATENT seconds..."
echo -n "Zzz...Zzz...Zzz..."
sleep $LATENT
echo "wakeup time! <yawn>"
}

stats()
{ # Prepare the statistics

# Ensure we have a log file
test ! -e $LOG && touch $LOG  

echo -e "\n\t\t\t   $VERSION Statistics"
test $MAILSTAT -eq 1 && mailstat -k <$LOG
echo ""
num_mail $FOLDERS
test $TIMER -eq 1 && echo -e "\n`timer -stop -id $$ -quiet` have elapsed."
}

prepmail()
{ # Let's prepare our e-mail
cat <<EOF >$TMP
From: $LOGNAME ($VERSION)
To: $LOGNAME
X-Loop: $SELF
Subject: Mail stats from `date "+%D %X"`

EOF
}

### Main Program

# Let's have some initial cleanup
rm -f $LOG
clear

# Create and secure the temporary file
test $E_MAIL -eq 1 && { cat /dev/null >$TMP; chmod 600 $TMP }

# Prepare the e-mail before the logs are added to it
test $E_MAIL -eq 1 && prepmail

# See if we are downloading every message or not
if test "$1" = "-every"
then
	FETCHMAIL="$FETCHMAIL -a -l 0"
	shift
fi

# Fetch the mail and have the output written to stdout and (optionally) $TMP
test $E_MAIL -eq 1 && getmail $@ 2>&1 |tee -a $TMP || getmail $@

clear

# Do the same thing with the statistics
test $E_MAIL -eq 1 && stats $@ 2>&1 |tee -a $TMP || stats $@

# Now send $TMP to myself and clean up the mess
test $E_MAIL -eq 1 && { cat $TMP |$SENDMAIL; rm -f $TMP }

# cleanup the log file for next time
rm -f $LOG

# The End