#!/usr/bin/perl -w 
#
# Copyright (C) 2007, Joshua D. Abraham (jabra@spl0it.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
# use strict;
#
# http_grep - search webpage(s) 
#
# ex: $ ./http_grep.pl -u http://spl0it.org -s security
#
use strict;
use LWP::UserAgent;

use Getopt::Long;

use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;    # Truncate calling path from the prog name


my $AUTH    = 'Joshua D. Abraham';  # author
my $EMAIL    = 'jabra@spl0it.org';  # email
my $VERSION = '1.0';                # version

my %options;
my $search;

#
# help: ->
# display help information
# side effect:  exits program
#
sub help {
    print "Usage: $PROG [Input Type] [General Options]
   -u  --url  [url]         Website 
   -f  --file [url-list]    Website List (one per line)

   -s  --search [text]      Text to search for on pages
   
   -v  --version            Display version
   -h  --help               Display this information

Send Comments to $AUTH ( $EMAIL )\n";
    exit;
}


#
# print_version: ->
# displays version
# side effect: exits program
#
sub print_version {
    print "$PROG version $VERSION by $AUTH ( $EMAIL )\n";
    exit;
}

#
# http_search: URL ->
# searches a page and prints the pages which it is found
#
sub http_search {
    my ($url) = @_;
    my $ua = LWP::UserAgent->new;
    $ua->timeout(10);
    $ua->env_proxy;

    my $response = $ua->get($url);

    if ($response->is_success) {
        if ($response->content =~ /$search/i ){
            print "found $search on\n";
            print "$url\n";
        }
    }
    else {
        die $response->status_line;
    }

    
}

GetOptions(
    \%options,
    'url|u=s','file|f=s','search|s=s',
    'help|h'    => sub { help(); },
    'version|v' => sub { print_version(); },
    )
    or exit 1;
if ( $options{search} ) {
    $search = $options{search};
}
else {
    print "Input error\n";
    help();

}
if ( $options{file} ) {
    if ( -r $options{file} ) {
        open(DATA, $options{file} ) || die("Could not open file!");
        while ( <DATA> ) {
            chomp;
            http_search( $_ );
        }      
    }
}
elsif ( $options{url} ) {
    http_search( $options{url} );
}
else {
    print "Input error\n";
    help();
}
