#!/usr/bin/perl

use warnings;
use Term::ANSIColor;

$ENV{PATH} = '/usr/bin:/bin';
$ENV{ENV} = '';

print "\nFor help on how to use this system, enter help.\nTo return to the menu hit <Enter>\n\n";

for(;;) {
	print colored("finger> ", "RED BOLD", "on_black");
	$| = 1;
	chomp($_ = lc(<STDIN>));
	print "\n";
	exit if /^exit$|^quit$/;
	last unless /\S/;
	
	if(/^help$/){
		print <<'EOF';
The email terminal's user information lookup system can look up the information
of a particular user.  The information given includes the last time the user
checked their email, if there are any unread emails and the full name of the
user.

You can enter a CRSid (username), initials or a surname/forname to look up
a user's information.  All valid CRSids will return a result; searching with
initials and surname/forname will not produce a result if the user in question
has removed themselves from the search database.

The system uses a program called finger, so if you wish to use the system to
look up information on a different computer add an "@" followed by the domain name or IP address of the computer.

EOF
	} elsif(/(^[a-z]+\d+$)/) {
		my $crsid = $1;

		# hermes
		$hermes = `finger -l $crsid\@hermes.cam.ac.uk`;
		$hermesaccount = 1;
		if ($hermes =~ m/\nLogin name:/) {
			$hermes = pstrip($hermes, "1");
		} elsif ($hermes =~ /^\[hermes.cam.ac.uk\]\n$/) {
			$hermes = "$crsid has an account on hermes, but prevented information from bing given by hermes.";
		} else {
			$hermesaccount = 0;
			$hermes = "$crsid has no account on Hermes (University Email Service).";
		}

		# zeus
		$zeus = `finger -l $crsid\@192.168.11.10`;
		$zeusaccount = 1;
		if ($zeus=~ m/\nLogin:/) {
			$zeus = pstrip($zeus, "1");
		} elsif ($zeus =~ /\]\n$/) {
         $zeus = "$crsid has an account on zeus, but prevented infermation from being given by zeus.";
		} else {
			$zeusaccount = 0;
			$zeus = "$crsid has no account on Zeus (JCN server).\nThis might be because they are not a member of Jesus College.\nhttp://www-jcsu.jesus.cam.ac.uk";
		}

		# srcf
		$srcf = `finger -l $crsid\@student.cusu.cam.ac.uk`;
		$srcfaccount = 1;
		if ($srcf =~ /\nUsername:/) {
			$srcf = pstrip($srcf, "1");
		} elsif ($srcf =~ /cam.ac.uk\]\n$/) {
			$srcf = "$crsid has an account on the SRCF, but prevented information from being given by the srcf.";
		} else {
			$srcfaccount = 0;
			$srcf = "$crsid has no account on the SRCF (Student-Run Computing Facility).\nhttp://www.srcf.ucam.org/";
		}

		# valid crsid?
		if ($hermesaccount + $zeusaccount + $srcfaccount == 0) {
			print <<"EOF";
$crsid is likely to be an invalid CRSid, since there are no accounts with this
username on Hermes, Zeus or the SRCF.

EOF
		} else {
			$colour = "YELLOW BOLD on_black";
			print colored("$crsid\@hermes.cam.ac.uk\n", "$colour") . $hermes . "\n\n";
			print colored("$crsid\@zeus.jesus.cam.ac.uk\n", "$colour") . $zeus . "\n\n";
			print colored("$crsid\@kern.srcf.ucam.org\n", "$colour") . $srcf . "\n\n";
		}

#Fingering hermes (only letters), no results returned on other servers
	} elsif(/(^[a-z]+$)/){
		$search = $1;
		my $hermes = `finger -pm $search\@hermes.cam.ac.uk`;
		$hermes =~ s/^\[hermes.cam.ac.uk\]\n//;
		# Check for case where no users match
		if ($hermes =~ /^finger: /) {
			print "No users found matching $search\n\n";
		} elsif ($hermes =~ /^$/) {
			print "You can not search using $search, since $search is a valid username on hermes.cam.ac.uk.\n\n";
		} else {
			foreach (split /\n\nLogin name:/, $hermes) {
				unless (/^Login name:/) {$_ = "Login name:" . $_;}
				$_ = pstrip($_, "0");
				/^(Login name:.*?\n)/;
				my $temp = colored($1, 'GREEN BOLD');
				$_ =~ s/^(Login name:.*?\n)/$temp/;
				print $_ . "\n\n";
			}
		}
#fingering a single or different machine
	} elsif(/(^[\w\-\.]+\@[a-z0-9\-\.]+$)/){
		print `finger -l $1`;
	} else{
		print "Unrecognied input, see help for more information\n";
		print "To return to the menu hit <Enter>\n\n";
	}
}

sub pstrip {
	# Strip project / plan  / pgp key
	my (@stripped_lines, $naked);
	my @lines = split /\n/, shift @_;
	my $strip_first_line = shift @_;

	# Optionally remove first line
	if ($strip_first_line) {shift @lines;}

	foreach my $line (@lines) {
		last if ($line =~ /^(Project|(No )?Plan|PGP (Public )?[Kk]ey|Mail forwarded to)(\.|:| )/);
		push @stripped_lines, $line;
	}
	$naked = join "\n", @stripped_lines;
	$naked =~ s/\n\n/\n/;
	chomp $naked;
	return "$naked";
}