web: Add resources for campaign page.
[gnupg-doc.git] / tools / append-to-donors.sh
1 #!/bin/sh
2 # append-to-donors.sh
3 # Append new names from the payproc journal to the donors file
4 # and send a Thank You mail.
5
6 pgm="append-to-donors.sh"
7 set -e
8
9 # We temporary need the next line due to an libgpg-error update
10 LD_LIBRARY_PATH=/usr/local/lib
11 export LD_LIBRARY_PATH
12
13 PATH=/usr/local/bin:$PATH
14 SENDMAIL="/usr/sbin/sendmail"
15 LC_ALL=C
16 LC_CTYPE=C
17 RFCDATE="$(date -R)"
18 SIGDELIM="-- "
19
20 htdocs="/var/www/www/www.gnupg.org/htdocs"
21
22 donors="$htdocs/donate/donors.dat"
23 donations="$htdocs/donate/donations.dat"
24
25 journal_dir="/var/log/payproc"
26 LOCKFILE="$donors.lock"
27
28 if [ ! -f "$donors" ]; then
29   echo "$pgm: '$donors' not found" >&2;
30   exit 1
31 fi
32
33 if [ x"$(idn --quiet wk@gnupg.org)" != x"wk@gnupg.org" ]; then
34   echo  "$pgm: idn(1) tool not installed or not working"
35   exit 1
36 fi
37 if [ x"$(mu-tool 2047 -c utf-8 '<wk@gnupg.org>')" \
38       != x"=?utf-8?Q?<wk@gnupg.org>?=" ]; then
39   echo "$pgm: mu-tool(1) tool not installed or not working"
40   exit 1
41 fi
42
43
44 if ! lockfile -l 7200 -r 2 $LOCKFILE; then
45     echo "$pgm: another instance is still running"
46     exit 0
47 fi
48 trap "rm -f $LOCKFILE $donors.tmp $donors.stamp" 0
49
50
51 # Send a thank you mail
52 # Uses these variables:
53 #  amount   - The amount of the donation
54 #  currency - The currency for the amount
55 #  euro     - The amount cinvertet to Euro
56 #  xmail    - The mailbox
57 #  name     - The name or empty for an anonymous donation
58 #  message  - The message to us or empty
59 # Used scratch variables:
60 #  upcurrency
61 #  ineuro
62 #  xamount
63 #
64 # FIXME: Clean message and name and use an appropriate encoding.
65 #        The second mail should actually be encrypted.  In fact
66 #        we would better try to encrypt also the first mail.  Add a
67 #        pubkey field to the donation page?
68 #
69 send_thanks () {
70     upcurrency=$(echo $currency | tr [a-z] [A-Z])
71     if [ "$upcurrency" = EUR ]; then
72         ineuro=
73     else
74         ineuro=" (about $(echo $euro| awk '{print int($0 + 0.5)}') EUR)"
75     fi
76     xamount="$(echo $amount| awk '{print int($0 + 0.5)}')"
77     if [ -n "$xmail" ]; then
78       xidnmail=$(CHARSET=UTF-8 idn --no-tld --quiet "$xmail")
79     else
80       xidnmail=""
81     fi
82     if [ x"$xidnmail" = x"$xmail" ]; then
83       xqpmail="$xmail"
84     else
85       xqpmail=$(mu-tool 2047 -c utf-8 "$xmail")
86     fi
87     ( cat <<EOF
88 From: donations@gnupg.org
89 To: $xqpmail
90 Subject: Thank you for supporting GnuPG
91 Date: $RFCDATE
92 Mime-Version: 1.0
93 Content-Type: text/plain
94 X-Loop: gnupg-donations-thanks.gnupg.org
95
96 Dear ${name:-Anonymous},
97
98 we received $xamount $upcurrency$ineuro as a donation to the GnuPG project.
99 Your donation helps us to develop and maintain GnuPG and related software.
100
101 Thank you.
102
103   Werner
104
105
106 $SIGDELIM
107 GnuPG - helping to keep private data private
108 EOF
109     ) | $SENDMAIL -oi donations@gnupg.org "$xidnmail"
110
111
112 if [ -n "$message" ]; then
113     ( cat <<EOF
114 From: donations@gnupg.org
115 To: donations@gnupg.org
116 Reply-To: $xqpmail
117 Subject: Message from GnuPG donor
118 Date: $RFCDATE
119 Mime-Version: 1.0
120 Content-Type: text/plain
121 X-Loop: gnupg-donations-thanks.gnupg.org
122
123 Name ..: ${name:-Anonymous}
124 Amount : $amount $upcurrency $ineuro
125 Message: $message
126 EOF
127     ) | $SENDMAIL -oi donations@gnupg.org
128 fi
129
130 }
131
132
133 # Find the last entry in donors which we have put in.
134 tmp=$(awk -F: <$donors '
135     /^(#.*)?$/ {next}
136     $5!="" { date=$2;lineno=$5 }
137     END {gsub(/-/,"",date)
138          print date ":" lineno}
139 ')
140 lastdate=$(echo $tmp | cut -d: -f1 | sed 's/T.*//')
141 lastline=$(echo $tmp | cut -d: -f2)
142 [ -z "$lastdate" ] && lastdate=19700101
143 [ -z "$lastline" ] && lastline=0
144
145
146 [ -f "$donors".stamp ] && rm "$donors".stamp
147 cat "$donors" > "$donors.tmp"
148 find $journal_dir -type f -name 'journal-????????.log' -print \
149      | sort | while read fname; do
150     fname=$(basename "$fname")
151     jdate=${fname%.log}
152     jdate=${jdate#journal-}
153     jyear=$(echo $jdate |sed 's/\(....\).*/\1/')
154     if [ "$jdate" -ge "$lastdate" ]; then
155         [ "$jdate" -gt "$lastdate" ] && lastline=0
156         payproc-jrnl -F_lnr -Fdate -F'[name]' -F'[message]' \
157                      -Fmail -Famount -Fcurrency -Feuro\
158            -S "_lnr > $lastline" -Stype=C -Saccount==1 \
159            --html --print "$journal_dir/journal-$jdate.log" \
160          | while IFS=: read lnr datestr name message \
161                             xmail amount currency euro rest; do
162             name=$(echo "$name" | tr \`\$: ...)
163             message=$(echo "$message" | tr \`\$ ..)
164             xmail=$(echo "$xmail" | tr \`\$ .. | sed 's/\.$//')
165             # Note that we removed colons from $name
166             echo "$jyear:$datestr:$name::$lnr:" >> "$donors.tmp"
167             touch "$donors".stamp
168             send_thanks
169          done
170     fi
171 done
172
173 # If we have any new records update the files.
174 if [ -f "$donors".stamp ]; then
175
176   if ! mv "$donors.tmp" "$donors"; then
177     echo "$pgm: error updating $donors" >&2
178     exit 1
179   fi
180
181   if [ -f "$donations" ]; then
182     payproc-stat -u "$donations" -- > "$donations".tmp  \
183       $(find /var/log/payproc -type f -name 'journal-????????.log' -print|sort)
184     if ! mv "$donations".tmp "$donations"; then
185         echo "$pgm: error updating $donations" >&2
186         exit 1
187     fi
188   else
189     payproc-stat -u "$donations" -- > "$donations"  \
190       $(find /var/log/payproc -type f -name 'journal-????????.log' -print|sort)
191   fi
192 fi