#!/usr/bin/perl
#
# By Stefan Seiz (www.stefanseiz.com)
# This is a sample Perl CGI you can use as a frontend
# to a Mailing List hosted with CommuniGate Pro.
#
# This script requires the following Modules
# CGI, File::Copy, CLI.pm (by Stalker/CommuniGate Systems)

# enter your server's IP Address below
my $CGServerAddress = "127.0.0.1";

# enter the username of someone with sufficient user-rights to manage lists below
my $PostmasterLogin = "Postmaster";
# enter the password for the above username
my $PostmasterPassword = "secret";

BEGIN
{
 use FindBin qw($Bin);
 use lib "$Bin";
}#This allows the web server to find the CLI.pm file in the current directory 

use strict;
use CLI;
use CGI qw(:standard);
use File::Copy;

print header;				# Print "Context-type: text/html"
print "<html>\n<head>\n<title>List Subscription</title>\n";

# Uncomment next line in order to use a CSS Stylesheet. Also modify name of Stylesheet accordingly
#print '<link rel="STYLESHEET" type="text/css" href="/style.css"></head>';
print "\n\n";
print "<body>\n";

if($PostmasterPassword eq "") {			# You didn't specify domain/login/password
  errormsg("You have to modify the script file to specify the CGPro Server address and Postmaster password");
} elsif(param()) {						# The form is already filled
  # Read the parameters from the form
  my $Account = param("account");
  my $RealName = param("realname");
  my $ListName = param("listname");
  my $ListAction = param("listaction");
  my $Mode = "";
  $Mode = param("mode");
  my $CompanyName = param("company");

  if ($Account eq "") {
	errormsg("No Email specified");
  }
  if ($RealName eq "") {
	errormsg("No Name specified");
  }
  if ($CompanyName eq "") {
	errormsg("No Company specified");
  }
  if ($Mode ne "noconfirm") {
	$Mode = "confirm";
  }
  ############################################
  # Don't send conf requests if form passes "noconfirm" in param "mode"
  ############################################
  if ($Mode eq "noconfirm") { 
	$Mode = "silently";
  }

  my $cli = new CGP::CLI( { PeerAddr => $CGServerAddress,
							PeerPort => 106,
							login	 => $PostmasterLogin,
							password => $PostmasterPassword } );
  unless($cli) {
	errormsg("Can't login to CGPro: ".$CGP::ERR_STRING);
  }

  # Stitch Realname and email together in a Variable for easyer handling in line after next Line (QUOTES)
  my $Subscriber = sprintf("\"%s\" <%s>",$RealName,$Account);
	
  if($cli->List($ListName, $ListAction, $Subscriber, $Mode)){
  
	# Log Company Name etc. to a file
	(my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time);

	$mon=$mon+1; $year=$year+1900;
	my $myDate = "$mday.$mon.$year $hour:$min:$sec";

	# Change path below to your to your liking. 
	my $filename = ">>/var/log/subscribers.xls";
	open(FH,$filename) or die "ERROR: $!";
	printf FH "$RealName\t$CompanyName\t$Account\t$ListAction\t$ListName\t$myDate\n";
	close(FH);

	print h2("Account ".$ListAction."d.");
	
	if ($Mode eq "confirm"){
		print "You will be getting a confirmation email shortly. <p>";
		print "You have to reply to this email as instructed in order to succesfully subscribe/unsubscribe.";
		print "<br>If we wouldn't do this, anyone could subscribe you to a list and clutter your mailbox.";
		print "<p><!-- $ENV{'REMOTE_ADDR'} -->";
		}
  } else{
	errormsg(" ".$cli->getErrMessage);
		last MAIN;
  }
  $cli->Logout;
			   
} else {						   # The form is empty
  print h2("Warning! "),hr();
  print p("No direct calls to this CGI allowed.");

}


print end_html();  # print"</BODY></HTML>"

sub errormsg {	#a procedure to output error message in HTML format and exit
 my ($msg) = @_;
 if ( $msg eq " address cannot be subscribed because it cannot be routed"){ 
	print h3("ERROR: your Email-Address is invalid!");
 }
 else{
	print h3("ERROR: $msg");
 }
 print "<ul><b>Please go ";
 print '<a href="javascript:history.go(-1)">back</a>';
 print " and complete the form!</b>";
 print '<p>Or <a href="mailto:webmaster@example.com?Subject=ERROR: ';
 print $msg;
 print '">notify</a> our Webmaster about the error</ul>';

 print end_html();
 exit;
}

