sfeed_sendmail

sfeed to sendmail command
git clone git://r-36.net/sfeed_sendmail
Log | Files | Refs | README | LICENSE

sfeed_sendmail (2419B)


      1 #!/bin/sh
      2 
      3 if [ $# -lt 2 ];
      4 then
      5 	printf "usage: %s to-addr tsvfile\n" "$(basename "$0")" >&2
      6 	exit 1
      7 fi
      8 
      9 toaddr="$1"
     10 tsvfile="$2"
     11 
     12 [ -z "$SENDMAILCMD" ] && SENDMAILCMD="sendmail -f"
     13 #[ -z "$SENDMAILCMD" ] && SENDMAILCMD="msmtp -f"
     14 
     15 while IFS=$'\n' read -r line;
     16 do
     17 	timestamp="$(printf "%s\n" "${line}" | cut -f 1)";
     18 	title="$(printf "%s\n" "${line}" | cut -f 2)";
     19 	link="$(printf "%s\n" "${line}" | cut -f 3)";
     20 	content="$(printf "%s\n" "${line}" | cut -f 4)";
     21 	contenttype="$(printf "%s\n" "${line}" | cut -f 5)";
     22 	id="$(printf "%s\n" "${line}" | cut -f 6)";
     23 	author="$(printf "%s\n" "${line}" | cut -f 7)";
     24 	enclosure="$(printf "%s\n" "${line}" | cut -f 8)";
     25 
     26 	[ -z "${timestamp}" ] && timestamp="$(TZ=UTC date +%s)"
     27 	timestamp="$(TZ=UTC date -R -d @${timestamp})"
     28 
     29 	case "${title}" in
     30 	*\;*|*\<\/*|*\=\"*)
     31 		# HTML encoding in title.
     32 		title="$(printf "%s\n" "${title}" \
     33 				| lynx -dump -stdin -nomargins \
     34 				-display_charset="utf-8" \
     35 				-image_links \
     36 				-width=1024 \
     37 				-assume_charset="utf-8" \
     38 				| tr '\n' ' ')"
     39 		;;
     40 	esac
     41 	[ -z "${title}" ] && title="${link}"
     42 	[ -z "${title}" ] && title="$(printf "%s\n" "${content}" | cut -c 10-)"
     43 	[ -z "${title}" ] && title="No Title"
     44 
     45 	case "${author}" in
     46 	*@*)
     47 		fromaddr="${author}"
     48 		;;
     49 	*)
     50 		fromaddr="$(printf "\"%s\" <none@none.no>" "${author}")"
     51 		;;
     52 	esac
     53 
     54 	{
     55 		printf "From: %s\r\n" "${fromaddr}"
     56 		printf "To: %s\r\n" "${toaddr}"
     57 		printf "Date: %s\r\n" "${timestamp}"
     58 		if [ -z "${title}" ];
     59 		then
     60 			printf "Subject: %s\r\n" "$(rputil -e "${title}" 2>/dev/null)"
     61 		fi
     62 		printf "Content-Type: text/plain; charset=\"UTF-8\"\r\n"
     63 		if [ -n "${link}" ];
     64 		then
     65 			printf "X-RSS-URL: %s\r\n" "${link}"
     66 			printf "X-RSS-Feed: %s\r\n" "${link}"
     67 		fi
     68 		printf "X-RSS-File: %s\r\n" "${tsvfile}"
     69 		if [ -n "${id}" ];
     70 		then
     71 			printf "X-RSS-ID: %s\r\n" "${id}"
     72 		fi
     73 		printf "User-Agent: sfeed_sendmail/1.0\r\n"
     74 		printf "\r\n"
     75 
     76 		if [ "${contenttype}" = "html" ];
     77 		then
     78 			printf "%s\n" "${content}" \
     79 				| sed 's,\\n,\n,g; s,\\t,\t,g; s,\\\\,\\,g' \
     80 				| lynx -dump -stdin -nomargins \
     81 				-display_charset="utf-8" \
     82 				-image_links \
     83 				-assume_charset="utf-8"
     84 		else
     85 			printf "%s\n" "${content}" \
     86 				| sed 's,\\n,\n,g; s,\\t,\t,g; s,\\\\,\\,g'
     87 		fi
     88 
     89 		printf "\n"
     90 		[ -n "${link}" ] && printf "URL: %s\n" "${link}"
     91 		[ -n "${enclosure}" ] && printf "Enclosure: %s\n" "${enclosure}"
     92 		
     93 	#} | cat
     94 	} | $SENDMAILCMD "${fromaddr}" "${toaddr}"
     95 done
     96