diff --git a/tools/append-to-donors.sh b/tools/append-to-donors.sh index b5dc9e6..c49ffd2 100755 --- a/tools/append-to-donors.sh +++ b/tools/append-to-donors.sh @@ -1,301 +1,308 @@ #!/bin/sh # append-to-donors.sh # Append new names from the payproc journal to the donors file # and send a Thank You mail. # # Note that this script does not yet handle subscriptions. Because we # want to verify the mail address anyway, it makes sense to move mai # sending to payprocd. The final plan is to use webhooks to create # charge records and use them to add a Donor reulary to the list of # donors (but may be limited to once a year) pgm="append-to-donors.sh" set -e # We temporary need the next line due to an libgpg-error update LD_LIBRARY_PATH=/usr/local/lib export LD_LIBRARY_PATH PATH=/usr/local/bin:$PATH SENDMAIL="/usr/sbin/sendmail" LC_ALL=C LC_CTYPE=C RFCDATE="$(date -R)" SIGDELIM="-- " usage() { cat <&2 ;; esac shift done if [ $testmode = yes ]; then htdocs="/var/www/www/preview.gnupg.org/htdocs" journal_dir="/var/log/payproc-test" else htdocs="/var/www/www/www.gnupg.org/htdocs" journal_dir="/var/log/payproc" fi donors="$htdocs/donate/donors.dat" donations="$htdocs/donate/donations.dat" LOCKFILE="$donors.lock" if [ ! -f "$donors" ]; then echo "$pgm: '$donors' not found" >&2; exit 1 fi if [ x"$(idn --quiet wk@gnupg.org)" != x"wk@gnupg.org" ]; then echo "$pgm: idn(1) tool not installed or not working" exit 1 fi if [ x"$(mu-tool 2047 -c utf-8 '')" \ != x"=?utf-8?Q??=" ]; then echo "$pgm: mu-tool(1) tool not installed or not working" exit 1 fi if ! lockfile -l 7200 -r 2 $LOCKFILE; then echo "$pgm: another instance is still running" exit 0 fi trap "rm -f $LOCKFILE $donors.tmp $donors.stamp" 0 # Send a thank you mail # Uses these variables: # amount - The amount of the donation # currency - The currency for the amount # euro - The amount cinvertet to Euro # xmail - The mailbox # name - The name or empty for an anonymous donation # message - The message to us or empty # recur - only when called with an arg > 0 # service - 1 = Stripe, 2 = Paypal, 3 = SEPA # Used scratch variables: # upcurrency # ineuro # xamount # recur_text # # FIXME: Clean message and name and use an appropriate encoding. # The second mail should actually be encrypted. In fact # we would better try to encrypt also the first mail. Add a # pubkey field to the donation page? # send_thanks () { recur_text="one-time" + xextratext="" if [ $1 -gt 0 ]; then case "$recur" in 12) recur_text="monthly" ;; 6) recur_text="bi-monthly" ;; 4) recur_text="quarterly" ;; 1) recur_text="yearly" ;; esac + xextratext=" +First Look Media, the parent organization of The Intercept and +Laura Poitras' Field of Vision, will match the first 12 months +of your gift. +" fi case "$service" in 1) service_text="Stripe" ;; 2) service_text="PayPal" ;; 3) service_text="SEPA" ;; *) service_text="$service" ;; esac upcurrency=$(echo $currency | tr [a-z] [A-Z]) if [ "$upcurrency" = EUR ]; then ineuro= else ineuro=" (about $(echo $euro| awk '{print int($0 + 0.5)}') EUR)" fi xamount="$(echo $amount| awk '{print int($0 + 0.5)}')" if [ -n "$xmail" ]; then xidnmail=$(CHARSET=UTF-8 idn --no-tld --quiet "$xmail") else xidnmail="" fi if [ x"$xidnmail" = x"$xmail" ]; then xqpmail="$xmail" else xqpmail=$(mu-tool 2047 -c utf-8 "$xmail") fi if [ $testmode = yes ]; then xisatest="[TEST DONATION] " else xisatest="" fi ( cat < "$donors.tmp" find $journal_dir/ -type f -name 'journal-????????.log' -print \ | sort | while read fname; do fname=$(basename "$fname") jdate=${fname%.log} jdate=${jdate#journal-} jyear=$(echo $jdate |sed 's/\(....\).*/\1/') if [ "$jdate" -ge "$lastdate" ]; then [ "$jdate" -gt "$lastdate" ] && lastline=0 # First for charge records payproc-jrnl -F_lnr -Fdate -F'[name]' -F'[message]' \ -Fmail -Famount -Fcurrency -Feuro -Fservice \ -S "_lnr > $lastline" -Stype=C -Saccount==1 \ --html --print "$journal_dir/journal-$jdate.log" \ | while IFS=: read lnr datestr name message \ xmail amount currency euro service rest; do name=$(echo "$name" | tr \`\$: ...) message=$(echo "$message" | tr \`\$ ..) xmail=$(echo "$xmail" | tr \`\$ .. | sed 's/\.$//') # Note that we removed colons from $name echo "$jyear:$datestr:$name::$lnr:" >> "$donors.tmp" touch "$donors".stamp send_thanks 0 done # Second for new subscriptions payproc-jrnl -F_lnr -Fdate -F'[name]' -F'[message]' \ -Fmail -Famount -Fcurrency -Feuro -F service -Frecur \ -S "_lnr > $lastline" -Stype=S -Saccount==1 \ --html --print "$journal_dir/journal-$jdate.log" \ | while IFS=: read lnr datestr name message \ xmail amount currency euro service recur rest; do name=$(echo "$name" | tr \`\$: ...) message=$(echo "$message" | tr \`\$ ..) xmail=$(echo "$xmail" | tr \`\$ .. | sed 's/\.$//') # Note that we removed colons from $name echo "$jyear:$datestr:$name:S:$lnr:" >> "$donors.tmp" touch "$donors".stamp send_thanks 1 done fi done # If we have any new records update the files. if [ -f "$donors".stamp -o $force = yes ]; then if ! mv "$donors.tmp" "$donors"; then echo "$pgm: error updating $donors" >&2 exit 1 fi if [ -f "$donations" ]; then payproc-stat -u "$donations" -- > "$donations".tmp \ $(find $journal_dir/ -type f -name 'journal-????????.log' -print|sort) if ! mv "$donations".tmp "$donations"; then echo "$pgm: error updating $donations" >&2 exit 1 fi else payproc-stat -u "$donations" -- > "$donations" \ $(find $journal_dir/ -type f -name 'journal-????????.log' -print|sort) fi fi