#!/usr/bin/perl -w
use strict;
# A handy-dandy locatory-thingy

# v2.0 rewritten to use massfinger
# v2.1 colour-coding added
# v2.2 included error stream output in /tmp/scanforfnords, so it isn't shown
#      remembered to blat temporary file after execution (oops!)
# v2.3 used instances of a new class "Fnord" to represent each fnord
# H I  fnords are now no longer hard-coded, and instead load from a file
#  A L added a "--help" argument so that it explains what the fuck it is
# E I  stuck it on CSLib, now that it's usable by other people
#  R S   -- LionsPhil (incl. all previous versions)

# TODO: De-shonk the way it detects fnords. Temp files and greppage == bad.


# Right, do the poor widdle users want to RTFM?
if((@ARGV) && (shift eq "--help"))
{
	print "[7m scanforfnords v2.3 [0m\n";
	print "Works with [1mmassfinger[0m to detect friends (fnords) logged into ECS machines.\n";
	print "[1mscanforfnords[0m is called without arguments. All of the configuration is handled  in .scanforfnordsrc in your home directory.\n";
	print "This file should be a list of fnords to detect: each one seperated by a new     line, and the three parts of each one seperated by a tab:\n";
	print "  The [4musername[0m (this is what's looked for)\n";
	print "  The [4mdisplay name[0m (this is what's shown)\n";
	print "  The [4mcolour[0m (either an ANSI fragment (e.g. \"31;1\") or leave it off)\n";
	print "    (You can look up ANSI colour codes using [1mshowcols[0m)\n";
	print "\nExample .scanforfnordsrc:\n";
	print "prb102\tLionsPhil\t33\njg102\tt3h Jimz0r\t32;1\n";
	print "Also see [1m~prb102/.scanforfnordsrc[0m\n";
	print "\n(It's called scanfor[4mfnords[0m for historic reasons. And because Phil is very       strange.)\n";
	exit 0;
}

# Peruse the configuration file
open(FNORDLIST, $ENV{HOME}."/.scanforfnordsrc") or die("Reading ".$ENV{HOME}."/.scanforfnordsrc: $!\nTry running with the '--help' command line option for information.\n[31;1mFatal config file error[0m");
my @rawfnords = <FNORDLIST>; # My raw fnords! All mine! You can't have any!
close(FNORDLIST);

# Transmogrify the input into instanciential goodness
my @fnords = ();
chomp(@rawfnords);
foreach (@rawfnords)
{
	my $fu=""; my $fn=""; my $fc="";
	($fu, $fn, $fc) = split(/\t+/, $_);
	push (@fnords, new Fnord($fu, $fn, "[".$fc."m"));
}

# Make baby Larry Wall cry with temporary file abuse. Oh, the humanity!
`massfinger >& /tmp/scanforfnords`;

# Now make him suicidal by grepping for each fnord in turn...joy!
my $cruftycruftyindex = 0;
foreach (@fnords)
{
	my $fnordu = $_->{'username'};
	print $fnords[$cruftycruftyindex]->{'colour'} . `grep $fnordu /tmp/scanforfnords | grep linux` . "[0m";
	$cruftycruftyindex++;
}

# Say bye bye, mister temporary file.
unlink "/tmp/scanforfnords";

# Stop running this script before it gives you cancer or something.
exit 0;


# Perl wins the "world's ugliest implementation of OOP" award. Yuck, yuck, YUCK!
package Fnord;
sub new {
	my $package = shift;
	my $reftohash = {};
	bless($reftohash, $package); # Can only be performed by popes.
	                             # Thankfully, all users are popes.
	$reftohash->{'username'} = shift; # First (real) arg: username
	$reftohash->{'realname'} = shift; # Second       arg: real name
	$reftohash->{'colour'}   = shift; # Third        arg: colour ("31;2")
	return $reftohash;
}

# -><- HAIL ERIS -><-, and stop reading my goddessdamn comments. Weirdo.
