]> Pileus Git - ~andy/fetchmail/blob - contrib/runfetchmail
Minor bug fixes for socket.c
[~andy/fetchmail] / contrib / runfetchmail
1 #!/bin/sh
2 # Runfetchmail 1.1
3 #
4 # Copyright (c) 1997 Doug Muth, Wescosville, Pennsylvania USA
5 # All rights reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 # Please send bug reports, suggestions, and flames to: dmuth@ot.com
26 #
27 # This shell script is used as a "frontend" for running fetchmail.  It will
28 #       start up fetchmail and save the session to disk, generate statistics
29 #       of the e-mail that you downloaded, and tell you how many messages
30 #       you have in various folders.  A copy of these results are also
31 #       e-mailed to you.
32 #
33 # An rc file is also supported.  If the file $HOME/.runfetchmailrc 
34 #       exists, it will be sourced.  This way, you can place runfetchmail
35 #       into /usr/local/bin, and individual users can have their own settings.
36 #
37 # Pre-requisites: You must have procmail, or at least `mailstat', a 
38 #       utility that comes with procmail, running on your system.  You must 
39 #       also have `timer', a shell script written by me, if you would like the 
40 #       total time that the transfer took to be displayed.
41 #
42 # Syntax: runfetchmail [-every]
43 #       -every Downloads all messages from the mailserver, regardless of
44 #               their size and whether they have been previously downloaded.
45 #
46 # Changes in version 1.1: The argument "-every" is supported.  I removed the
47 #       $EXIT_CODE variable since I had problems assigning the exit code from 
48 #       fetchmail to it.
49
50 # Command line to run fetchmail
51 FETCHMAIL="/usr/local/bin/fetchmail"
52
53 # Your procmail logfile
54 LOG=$HOME/procmail/log
55
56 # Do we want to use timer?  Set to 0 to disable.
57 TIMER=1
58
59 # Our path to sendmail with parameters
60 SENDMAIL="/usr/bin/sendmail -oi -t"
61
62 # Who am I?
63 SELF="dmuth@ot.com"
64
65 # The folders that I should check for the number of messages
66 FOLDERS="$MAIL $HOME/mail/lists"
67
68 # Number of seconds to "sleep" for while procmail finishes up, increase
69 #       this if you have a really slow system
70 LATENT=10
71
72 # Do we want to use mailstat?  Set to 0 to disable
73 MAILSTAT=1
74
75 # Do we want to e-mail the output to myself?  Set to 0 to disable.
76 # I strongly suggest doing this so that if you lose your connection to
77 #       the net part of the way through a download, you can see how much 
78 #       progess was made
79 E_MAIL=1
80
81 ###
82 # End of user defined variables
83 ###
84
85 # The temp file, and ensure my privacy!
86 TMP=/tmp/fetchmail.sh.$$
87
88 # The version of this program
89 VERSION="Runfetchmail 1.1"
90
91 # Trap errors
92 trap "rm -f $TMP; echo ""Exiting at user request"" ; \
93 test $TIMER -eq 1 && timer -stop -id $$ >/dev/null; exit 1" \
94 2 3 4 15
95
96 # Source the user's rc file if it exists
97 test -e $HOME/.runfetchmailrc && . $HOME/.runfetchmailrc
98
99 num_mail()
100 { # This procedure tells me how many messages there are in each folder
101 for D in $* 
102 do
103         if test -f $D
104         then
105                 echo "There are `frm $D |wc -l` messages in $D"
106         fi
107 done
108 }
109
110 getmail()
111 { # Fetch the mail!
112
113 test $TIMER -eq 1 && timer -start -id $$ -quiet
114
115 $FETCHMAIL $@
116
117 # pause for a short while
118 echo "Now sleeping for $LATENT seconds..."
119 echo -n "Zzz...Zzz...Zzz..."
120 sleep $LATENT
121 echo "wakeup time! <yawn>"
122 }
123
124 stats()
125 { # Prepare the statistics
126
127 # Ensure we have a log file
128 test ! -e $LOG && touch $LOG  
129
130 echo -e "\n\t\t\t   $VERSION Statistics"
131 test $MAILSTAT -eq 1 && mailstat -k <$LOG
132 echo ""
133 num_mail $FOLDERS
134 test $TIMER -eq 1 && echo -e "\n`timer -stop -id $$ -quiet` have elapsed."
135 }
136
137 prepmail()
138 { # Let's prepare our e-mail
139 cat <<EOF >$TMP
140 From: $LOGNAME ($VERSION)
141 To: $LOGNAME
142 X-Loop: $SELF
143 Subject: Mail stats from `date "+%D %X"`
144
145 EOF
146 }
147
148 ### Main Program
149
150 # Let's have some initial cleanup
151 rm -f $LOG
152 clear
153
154 # Create and secure the temporary file
155 test $E_MAIL -eq 1 && { cat /dev/null >$TMP; chmod 600 $TMP }
156
157 # Prepare the e-mail before the logs are added to it
158 test $E_MAIL -eq 1 && prepmail
159
160 # See if we are downloading every message or not
161 if test "$1" = "-every"
162 then
163         FETCHMAIL="$FETCHMAIL -a -l 0"
164         shift
165 fi
166
167 # Fetch the mail and have the output written to stdout and (optionally) $TMP
168 test $E_MAIL -eq 1 && getmail $@ 2>&1 |tee -a $TMP || getmail $@
169
170 clear
171
172 # Do the same thing with the statistics
173 test $E_MAIL -eq 1 && stats $@ 2>&1 |tee -a $TMP || stats $@
174
175 # Now send $TMP to myself and clean up the mess
176 test $E_MAIL -eq 1 && { cat $TMP |$SENDMAIL; rm -f $TMP }
177
178 # cleanup the log file for next time
179 rm -f $LOG
180
181 # The End
182