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
section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. -------------------------------------------------------------------------------