#!/usr/bin/perl
#############################################################################
# Author: Timothy Mills
# Date  : 03 - 04 - 2013
# File  : wiscanv2.pl
#
# wiscanv2.pl takes the output of 'iwlist scan' and makes it in an easier
# to read format for those of us who still deal with the terminal :)
#############################################################################


helpCheck();

#Run the command and suppress error output
$iwlistOut = `iwlist scan 2> /dev/null`; 

@myLines = split("\n", $iwlistOut);
@cellArr = [];

printHeading();
printFin("#", "Name", "Quality","Sig Lvl", "Enc", "Enc T.", "Auth T.", "Chan/Freq");
printHeading();

$currNum = 0;
varInit();

foreach (@myLines)
{
    if ($_ =~ "Cell [0-9][0-9]")
    {
	if ($currNum > 0)
	{
	    printFin($currNum, $name, $quality, $sigLvl, $enc, $encType,
		$authType, $chanFreq);

	    varInit();
	}
	$currNum++;
    }

    $cellArr[$currNum] = $cellArr[$currNum]."$_"."\n";

    if ($_ =~ "ESSID")
    {
	handleName($_);
    }
    elsif($_ =~ "Encryption key")
    {
	handleKey($_);
    }
    elsif($_ =~ "Quality=[0-9]+/[0-9]+")
    {
	handleQuality($_);
    }
    elsif($_ =~ "IE: WPA .+ [0-9]")
    {
	handleEncType($_);
    }
    elsif($_ =~ "Authentication Suites")
    {
	handleAuthType($_);
    }
    elsif($_ =~ "Frequency")
    {
	handleChanFreq($_);
    }
}


if ($currNum > 0)
{
    printFin($currNum, $name, $quality, $sigLvl, $enc, $encType, $authType, $chanFreq);
    printHeading();
}
else
{
    print("No scan results :'( \n");
}

if (scalar @ARGV == 1)
{
    $myNum = ($ARGV[0] =~ "[0-9]+");
    
    print $cellArr[$ARGV[0]] if($myNum && ($ARGV[0]>0 && $ARGV[0] <= $currNum));
    
}

sub helpCheck ()
{
    foreach(@ARGV)
    {
	helpMsg() if ($_ =~ "--help");
    }

    if (scalar @ARGV == 1 && !($ARGV[0] =~ "[0-9]+"))
    {
	helpMsg();
    }
    elsif (scalar @ARGV > 1)
    {
	helpMsg();
    }
}

sub helpMsg ()
{
    printf("Usage: wiscan.pl [network number] \n\n");
    printf("wiscan is used to make the output of 'iwlist scan' be more readable"
	   ."\n");

    printf("from within a terminal. By using the optional argument you may \n");
    printf("get the full output for a network from 'iwlist scan'\n\n");

    printf("EXAMPLES: \n");
    printf("1: ./wiscan.pl   -- Lists all wireless networks within range\n");
    printf("2: ./wiscan.pl 2 -- Shows full output for second wireless network entry \n                    in the output of wiscan.pl\n");

    
    exit(0);
}
    

sub printFin()
{
    printf("| %-3s | %-22s | %-7s | %-8s | %-3s | %-6s | %-9s | %11s |\n", $_[0],
	   $_[1], $_[2], $_[3], $_[4], $_[5], $_[6], $_[7]);
}

sub printHeading()
{
    printf("+ %-3s + %-22s + %-7s + %-8s + %-3s + %-6s + %-9s + %-11s +",
	"---", "----------------------", "-------", "--------", "---", "------", "---------", "-----------");
    
    
    printf("\n");
}

sub varInit()
{
    $name = "";
    $quality = "";
    $sigLvl = "";
    $enc = "";
    $encType = "WEP";
    $authType = "N/A";
    $chanFreq = "";
}

sub handleEncType ()
{
    $encType = "WPA v".($_ =~ ".*[0-9]");
}

sub handleQuality ()
{
    $qual = $_[0];

    @qualArr = split(" ", $qual);

    $quality = (split("=", $qualArr[0]))[1];
    $sigLvl = (split("=", $qualArr[2]))[1]." dBm";
}

sub handleKey ()
{
    $enc = (split(":", $_[0]))[1];
}

sub handleName ()
{
    $name = (split(":", $_[0]))[1];
}

sub handleAuthType ()
{
    $authType = (split(" ", $_[0]))[4];
}

sub handleChanFreq()
{
    my $chan = (split(" ", $_[0]))[3];

    if(length($chan) == 3) {$chan = substr($chan, 0, 2);}
    else                   {$chan = substr($chan, 0, 1);}

    my $frequency = (split(":",(split(" ", $_[0]))[0]))[1];

    $chanFreq = $chan . "/" . $frequency . "GHz";	
}