1	# =====================================================================
     2	# notifyPageUpdates.awk: W-TW page update notifier.
     3	#
     4	# Copyright (c) 2009 Carlo Strozzi
     5	#
     6	# This program is free software; you can redistribute it and/or modify
     7	# it under the terms of the GNU General Public License as published by
     8	# the Free Software Foundation; version 2 dated June, 1991.
     9	#
    10	# This program is distributed in the hope that it will be useful,
    11	# but WITHOUT ANY WARRANTY; without even the implied warranty of
    12	# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13	# GNU General Public License for more details.
    14	#
    15	# You should have received a copy of the GNU General Public License
    16	# along with this program; if not, write to the Free Software
    17	# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    18	#
    19	# =====================================================================
    20	
    21	BEGIN {
    22	
    23	  hdr = body = ""; i = score = 0; delete a
    24	
    25	  cmd = ENVIRON["CSA_CMD_SENDMAIL_"]
    26	
    27	  if (cmd == "") cmd = "/usr/lib/sendmail -t"	# default
    28	
    29	  max_bcc = ENVIRON["TNS_EMAIL_RCPT_MAX"]/1
    30	
    31	  if (max_bcc < 25) max_bcc = 25	# set reasonable default.
    32	}
    33	
    34	{ sub(/\r+$/,"") }			# remove any trailing CRs
    35	
    36	# First line is expected to contain the list of recipients.
    37	NR == 1 {
    38	   if ($0 !~ /^[^ ]+@/) exit
    39	   if ((i=split($0,a," ")) < max_bcc) max_bcc = i
    40	   next
    41	}
    42	
    43	NR > 2 && /^From / { sub (/^/,">") }	# escape bogus 'From ' lines.
    44	
    45	/^(((From|To): [^ ]+)|$)/ { score++ }	# perform basic sanity checks.
    46	
    47	/^Bcc: / && score < 3 { next }		# ignore unwanted Bcc headers.
    48	
    49	score < 3 {
    50	  if (hdr != "") hdr = hdr "\n"
    51	  hdr = hdr $0
    52	}
    53	
    54	score >= 3 { body = body "\n" $0 }
    55	
    56	END {
    57	
    58	    if (score < 3 || i < 1) exit(1)		# invalid message.
    59	
    60	    # Send one copy of the message every max_bcc recipients.
    61	
    62	    k = 1
    63	    while (k <= i) {
    64	      bcc = ""
    65	      for (j=1; j<=max_bcc; j++) {
    66		  if (k > i) break
    67		  if (j > 1) bcc = bcc ", "
    68		  # Remove any notification gateway info from the address,
    69		  # as only e-mail is supported at the moment.
    70		  sub(/.*\^/,"",a[k])
    71		  bcc = bcc a[k++]
    72	      }
    73	      print hdr "\nBcc: " bcc body | cmd
    74	      close(cmd)
    75	    }
    76	}
    77	
    78	# EOF