Download this file
#!/usr/bin/perl -w

# (c) Matthieu Weber 2004
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/copyleft/gpl.html

$cmd_file = "$ENV{HOME}/.mailing_list_options";

sub mail {
  my ($dest, $subj) = @_;
	open(MAIL, "|mutt -s '$subj' '$dest' < /dev/stdin");
	print MAIL "$subj";
	close(MAIL);
	#print "mail to $dest with subj $subj\n";
}

sub url {
  my $url = shift;
	open(VIEW, "|urlview");
	print VIEW "$url";
	close (VIEW);
}

sub prompt_user {
  print "\nMailing list $list_name ($list_type) commands:\n";
	foreach $c (sort keys(%command)) {
	  print "  $c\n";
	}
	print "\nYour choice: ";
	$ans = <>;
	$ans =~ s/\r?\n//;
	if ($ans eq "") {
    print "Abort.\n";
		return 1;
	}
	if (exists $command{$ans}) {
	  if (exists $command{$ans}->{subject}) {
		  my $subject = $command{$ans}->{subject};
			if ($subject =~ /%p/) {
			  print "Password for list $list_name: ";
	      $pass = <>;
	      $pass =~ s/\r?\n//;
				$subject =~ s/%p/$pass/e;
			}
	    &mail($destination, $subject);
		}
		return 0;
	}
	else {
    print "No such command. Abort\n";
		return 1;

	}
}

sub write_commands {
  if (! defined $destination || $destination eq "") {
    print "Parsing failed. Is this a mailing-list message?\n";
		return 1;
	}
  open F, ">$cmd_file";
	chmod 0600, "$cmd_file";
 	print F "\$destination = '$destination';\n";
 	print F "\$list_name = '$list_name';\n";
 	print F "\$list_type = '$list_type';\n";
 	print F "\%command = (\n";
	foreach $k (keys(%command)) {
	  print F "'$k' => {";
		foreach $sk (keys(%{$command{$k}})) {
		  print F "'$sk' => '$command{$k}->{$sk}', ";
		}
		print F "},\n";
	}
	print F ");\n";
	print F "1;\n";
	close F;
	return 0;
}

if (! -f $cmd_file) {
  $/="\n\n";
  $h = <>;
  $/="\n";
  $h =~ s/\n\s+/ /gs;
  $h =~ s/^From (.*)\n//gm;
  $h =~ s/^([^:]+): /$1\n/gm;
  %hdr = split /\n/, $h;
	
  if (exists $hdr{"List-software"} && $hdr{"List-software"} =~ /Ecartis/) {
    # Ecartis
		($destination) = ($hdr{"List-help"} =~ /mailto:([^\>\?]+)/);
		$list_name = $hdr{"List-Id"};
		$list_name =~ s/ .*$//;

		$list_type = "Listar";
		%command = (
		"subscribe"      => { "subject" => "subscribe $list_name" },
		"unsubscribe"    => { "subject" => "unsubscribe $list_name" },
		"help"           => { "subject" => "help" },
		"set_vacation"   => { "subject" => "set $list_name vacation" },
		"unset_vacation" => { "subject" => "unset $list_name vacation" },
		"status"         => { "subject" => "stats $list_name" },
		);
	}
	elsif (exists $hdr{"X-Mailman-Version"}) {
		# Mailman
		($destination) = ($hdr{"List-Help"} =~ /mailto:([^\>\?]+)/);
		($list_name) = ($hdr{"List-Id"} =~ /<([^\.]+)/);

		$list_type = "Mailman";
		%command = (
		"subscribe"      => { "subject" => "subscribe \%p" },
		"unsubscribe"    => { "subject" => "unsubscribe \%p" },
		"help"           => { "subject" => "help" },
		"set_vacation"   => { "subject" => "set nomail on \%p" },
		"unset_vacation" => { "subject" => "set nomail off \%p" },
		"options"        => { "subject" => "options" },
		);
	}
	elsif (exists $hdr{"X-Mailing-List"} && $hdr{"X-Mailing-List"} =~ /korppi\.jyu\.fi/) {
		($destination) = ($hdr{"List-Help"} =~ /mailto:([^\>\?]+)/);
		($list_name) = ($hdr{"List-Help"} =~ /mailto:([^@]+)/);
	  print "$destination\n";
    
		$list_type = "Korppi";
		%command = (
		"subscribe"      => { "subject" => "subscribe" },
		"unsubscribe"    => { "subject" => "unsubscribe" },
		"help"           => { "subject" => "help" },
		"archive_help"   => { "subject" => "archive help" },
		);
	}
	$exit_code = &write_commands();
}
else {
  require "$cmd_file";
  $exit_code = &prompt_user();
  unlink "$cmd_file";
}

exit $exit_code;