mail-alias-paste-service - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       mail-alias-paste-service (1965B)
       ---
            1 #!/bin/sh
            2 
            3 d="/home/www/domains/www.codemadness.org/htdocs/mailpaste"
            4 tmpmsg=$(mktemp)
            5 tmpmail=$(mktemp)
            6 
            7 cleanup() {
            8         rm -f "$tmpmail" "$tmpmsg"
            9 }
           10 
           11 # store whole mail from stdin temporarily, on exit remove temporary file.
           12 trap "cleanup" EXIT
           13 cat > "$tmpmail"
           14 
           15 # mblaze: don't store mail sequence.
           16 MAILSEQ=/dev/null
           17 export MAILSEQ
           18 
           19 # get from address (without display name).
           20 from=$(maddr -a -h 'From' /dev/stdin < "$tmpmail")
           21 
           22 # check if allowed or not.
           23 case "$from" in
           24 "hiltjo@codemadness.org")
           25         ;;
           26 *)
           27         exit 0;;
           28 esac
           29 
           30 # prevent mail loop.
           31 if printf '%s' "$from" | grep -q "pasty@"; then
           32         exit 0
           33 fi
           34 
           35 echo "Thank you for using the enterprise paste service." > "$tmpmsg"
           36 echo "" >> "$tmpmsg"
           37 echo "Your file(s) are available at:" >> "$tmpmsg"
           38 echo "" >> "$tmpmsg"
           39 
           40 # process each attachment.
           41 mshow -n -q -t /dev/stdin < "$tmpmail" | sed -nE 's@.*name="(.*)".*@\1@p' | while read -r name; do
           42         test "$name" = "" && continue
           43 
           44         # extract attachment.
           45         tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX)
           46         mshow -n -O /dev/stdin "$name" < "$tmpmail" > "$tmpfile"
           47 
           48         # use file extension.
           49         ext="${name##*/}"
           50         case "$ext" in
           51         *.tar.*)
           52                 # special case: support .tar.gz, tar.bz2, etc.
           53                 ext="tar.${ext##*.}";;
           54         *.*)
           55                 ext="${ext##*.}";;
           56         *)
           57                 ext="";;
           58         esac
           59         ext="${ext%%*.}"
           60 
           61         # use file extension if it is set.
           62         outputfile="$tmpfile"
           63         if test "$ext" != ""; then
           64                 outputfile="$tmpfile.$ext"
           65         fi
           66         mv "$tmpfile" "$outputfile"
           67         b=$(basename "$outputfile")
           68 
           69         chmod 666 "$outputfile"
           70         url="gopher://codemadness.org/9/mailpaste/$b"
           71 
           72         echo "$name:" >> "$tmpmsg"
           73         echo "        Text   file: gopher://codemadness.org/0/mailpaste/$b" >> "$tmpmsg"
           74         echo "        Image  file: gopher://codemadness.org/I/mailpaste/$b" >> "$tmpmsg"
           75         echo "        Binary file: gopher://codemadness.org/9/mailpaste/$b" >> "$tmpmsg"
           76         echo "" >> "$tmpmsg"
           77 done
           78 
           79 echo "" >> "$tmpmsg"
           80 echo "Sincerely," >> "$tmpmsg"
           81 echo "Your friendly paste_bot" >> "$tmpmsg"
           82 
           83 # mail back the user.
           84 mail -r "$from" -s "Your files" "$from" < "$tmpmsg"
           85 
           86 cleanup