#!/usr/bin/perl
# -------------------------------------------------------------------------
# idio-audio-links.cgi - Based on www.syntac.net/idio-audio/links.html
#        1. Please make sure that the control structure is not altered.
#        2. Save the edited file in text form.
#        3. Transfer this file to the WWW server in ASCII mode.
#        4. Set its protection to world read, execute (e.g. chmod 755 filename)
# -------------------------------------------------------------------------
# (To see if the script works as intended: Type 'thisfile.pl < test', where test is a text file containing
#  the item identifiers as, Item1=string&Item2=string&Item3=...
#  Alternatively, you may like to install the CGI and HTML and test via the form.)
# -------------------------------------------------------------------------

# -------------------------------------------------------------------------
# Start of the Perl script:
# -------------------------------------------------------------------------
# Procedure to send mail
sub do_mail
{
  local($filename) = "/tmp/cf-$$";
  open(TFILE,">$filename");

# >>>>> Start of User configurable section 1 of 2 >>>>>
# The mail subject will be at the top of the message.
   print TFILE "**************************************************\n";
   print TFILE "Title: Suggested URL for the IDIO-AUDIO Links Page\n";
   print TFILE "**************************************************\n\n";

  if ($url ne "")
  {
   print TFILE "url:	$url\n";
  }

  close(TFILE);
 `/usr/bin/mail  $mailreceiver < $filename`;
  $mailstatus = $?;
  unlink("$filename");
}
# <<<<< End of User configurable section 1 of 2 <<<<<
# >>>>> Do not alter anything in the following section >>>>>
# convert %20 to a space and other %dd to a character
sub reformat
{
  local($tmp) = $_[0] ;
  $tmp =~ s/\+/ /g ;
  while ($tmp =~ /%([0-9A-Fa-f][0-9A-Fa-f])/)
{
      $num = $1;
      $dec = hex($num);
      $chr = pack("c",$dec);
      $chr =~ s/&/and/g;  # Replace if it is the & char.
      $tmp =~ s/%$num/$chr/g;
  }
  return($tmp);
}

# Main body of the script
sub do_main
{
  $cl = $ENV{'CONTENT_LENGTH'};
  if ($cl > 0)
  {
   read(STDIN, $_, $cl);
   $_ .= "&"; # Append an & char so that the last item is not ignored
   $pquery = &reformat($_);

   while ($pquery =~ /url=([^&]*)&/) {
      if (!$url) {
         $url = $1;
      }
      else {
         $url = $url."\n        ".$1;
      }
      $pquery =~ s/url=([^&]*)//;
   }
   $url = &reformat($url);

# <<<<< Do not alter anything in the above section <<<<<
# >>>>> Start of User configurable section 2 of 2 >>>>>
  print "Content-type: text/html\n\n";
  print "<HEAD>\n<TITLE>Your Suggested URL has been sent...</TITLE>\n</HEAD>\n";
  print "<body bgcolor=#000000 text=#FF8000 link=#FFFFFF alink=#FF0000 vlink=808080>\n";

  print "<p align=center>\n";
  print "<tt><b><font size=+4 color=#800040>IDIO-AUDIO</font></b></tt>\n";
  print "<br>\n";
  print "<font size=+1 color=#404080><b>Independent And Experimental Music Online</b></font></p>\n";

  print "<p align=center>\n";
  print "<kbd><b>\n";
  print "<font size=+2>\n";
  print "<font color=#800040><sub>&#186;01</sub>0</font><font color=#404080><sup>01</sup>0</font><font color=#800040><sub>01</sub>0</font><font color=#404080><sup>10</sup>0<sub>&#186;</sub></font></font></b></kbd></p>\n";

  print "<blockquote><tt>\n";

  print "<p><a href=#f>STATUS</a><p>\n";

  print "<font color=#FF0000><h1>Acknowledgement</h1></font>\n";
  print "<h2>Thank you for your participation.</h2><h3>Your Suggested URL has been accepted for our concurrence.</h3>\n";
  print "<hr><h2>The following data has been sent to IDIO-AUDIO. </h2>\n";
  print "<pre>";

  if ($url ne "")
  {
   print "URL:		: $url\n";
  }

  print "</pre>";
  print "<hr>\n";
  &do_mail;
  print "<h3>Sent at $misterwolf from $ENV{'REMOTE_HOST'}</h3>\n";
   if ($mailstatus == 0)
    {
     print "<P><h3>Transmission Successful!</h3>\n";
    }
    else
    {
     print "<P><h3><Blink>An error occurred during transmission. </Blink></h3><br>\n";
     print "The error status was $mailstatus).\n";
    }
     print "</tt></blockquote><br><a name=f><p align=center><a href=#start><img src=http://www.syntac.net/idio-audio/images/sun.gif width=16 height=16 alt=[up] border=0></a><br><br><kbd><b><font size=+2><font color=#800040><sub>&#186;01</sub>0</font><font

 color=#404080><sup>01</sup>0</font><font
color=#800040><sub>01</sub>0</font><font
color=#404080><sup>10</sup>0<sub>&#186;</sub></font></font></b></kbd><br><b><sub><font
size=+4><a
href=http://www.syntac.net/idio-audio/links.html>&#171;</a></font></sub><kbd><font
size=+1><sub><a
href=http://www.syntac.net/idio-audio/index.html>start</a></sub><sup><a
href=http://www.syntac.net/idio-audio/philes.html>listen</a></sup><sub><a
href=http://www.syntac.net/idio-audio/text.html>read</a></sub><sup><a
href=http://www.syntac.net/idio-audio/list.html>join</a></sup><sub><a href=http://www.syntac.net/idio-audio/live.html>witness</a></sub><sup><a href=http://www.syntac.net/idio-audio/links.html>stop</a></sup></font></kbd><sub><font size=+4><a href=http://www.syntac.net/idio-audio/index.html>&#187;</a></font></sub></b></p>\n";
	print "<p align=center>.<br>.<br>.</p>\n";
	print "<p align=center><img src=http://www.syntac.net/teleport/images/idiosyntactix.gif width=102 height=12 hspace=0 vspace=0 border=0 alt=(.) Idiosyntactix></p>\n";
  }
}
# <<<<< End of User configurable section 2 of 2 <<<<<

$misterwolf = `/bin/date`; $misterwolf =~ s/\n//g ;
$mailreceiver = "idio-audio-links\@syntac.net";

$|=1;
eval '&do_main';
sleep(1);

# ***** End of the Perl Script file. *****
