#!perl -T
# 
# everything works for now
# designed to run in Irssi
# 11/24/08
#
#


#use strict;
use DB_File;
use Fcntl;
use CGI qw/unescapeHTML/;
use LWP::Simple;
use LWP::UserAgent;
use vars qw($VERSION %IRSSI);
use Irssi qw(command_bind signal_add);
use IO::File;

$VERSION = '0.1.8';
%IRSSI = (
	author		=> 'Soul_keeper',
	contact		=> '#linuxsociety',
	name		=> 'lsquote',
	descriotion	=> 'A functional quote bot i had to write because there were no good ones out there in perl',
	license		=> 'GPL',
);

sub own_question {
	my ($server, $msg, $target) = @_;
	question($server, $msg, "", $target);
}

sub public_question {
	my ($server, $msg, $nick, $address, $target) = @_;
	question($server, $msg, $nick.": ", $target);
}

sub get_data {
    my $data = shift;
    my @params = split / +/, $data;
    my $trigger = shift @params;

    my $count = 1;
    if (defined $params[0] && int($params[0]) > 0) {
        $count = shift @params;
        $count = 10 if $count > 10;
    }

    $data = join ' ', @params;
    $data =~ s/[^[:print:]]/ /g;
    $data =~ s/ using databaseusing databaseusing database */ /g;

    return ($trigger, $count, $data);
}



sub question($server, $msg, $nick, $target) {
	my ($server, $msg, $nick, $target, $url, $data) = @_;
	$_ = $msg;
my $ua = LWP::UserAgent->new(agent => "google suck");
my $result = $ua->get($url);
my $symbol = 1;
my $price = 1;
my @datain = 1;



# These options are for the portfolio tracking functions 
	if (/^!in/i) {
		$data = $msg;
		$data =~ s/^!in //;

		#opening and/or creating database if needed
		tie (%portfolio_db, DB_File, "/tmp/portfolio.db", O_CREAT|O_RDWR, 0644) ||
		die ("Cannot create or open /tmp/portfolio.db");

		#separating out symbol and price variables from data		
		@datain = split(/ /, $data);	
		#my $symbol = $datain[0];
		#my $price = $datain[1];	
		$symbol = shift(@datain);
		$price = pop(@datain);
				
		#opening database
		#tie (%portfolio_db, DB_File, "/tmp/portfolio.db") ||
		#die ("Cannot open /tmp/portfolio.db");

		#reading from
		#my $result = $portfolio_db{"$nick$symbol"};
		if (exists $portfolio_db{"$nick$symbol"}) {
		               $server->command('msg '.$target.' '.$nick.' A position with this ticker already exists in your portfolio, try !out');
			}
		else {
			#my $entryprice = $portfolio_db{"$nick$symbol"};
		
		#writing to database, adding new position
		$portfolio_db{"$nick$symbol"} = "$price";

		#close database
		untie(%portfolio_db);

		#print output
		$server->command('msg '.$target.' '.$nick.'Added: '.$symbol); 
		$server->command('msg '.$target.' '.$nick.'With Price: '.$price);
			}
		}

	if (/^!out/i) {
		$data = $msg;
#               #$symbol =~ s/^!in //;
#                #$price =~ s/^!in //;
                 $data =~ s/^!out //;


                #creating database if needed
                #tie (%portfolio_db, DB_File, "/tmp/portfolio.db", O_CREAT|O_RDWR, 0644) ||
                #die ("Cannot create or open /tmp/portfolio.db");
   
                #separating out symbol and price variables from data
                @datain = split(/ /, $data);
                #my $symbol = $datain[0];
                #my $price = $datain[1];
                $symbol = shift(@datain);
                $price = pop(@datain);
                 
                #opening database
                tie (%portfolio_db, DB_File, "/tmp/portfolio.db") ||
                die ("Cannot open /tmp/portfolio.db");

                if (exists $portfolio_db{"$nick$symbol"}) {

                #reading assosciated data from database 
                my $entryprice = $portfolio_db{"$nick$symbol"};
		                
                #deleting exited position key from database
                delete $portfolio_db{"$nick$symbol"};

		my $return = $price/$entryprice;
		$return = $return-1;
		$return = $return*100;

                #close database
                untie(%portfolio_db); 

		$server->command('msg '.$target.' '.$nick.' Portfolio: removing '.$symbol);		
		#delete $portfolio_db{"Bill Smith"};
                $server->command('msg '.$target.' '.$nick.' Percentage gain/loss: '.$return);		
	        	}
                else {
			$server->command('msg '.$target.' '.$nick.' No position with this ticker exists in your portfolio, try !in');
			}

		}
        if (/^!portfolio/i) {
                #opening database
                tie (%portfolio_db, DB_File, "/tmp/portfolio.db") ||
                die ("Cannot open /tmp/portfolio.db");

		#scan for entries by nick, FIXME printing all right now and key needs parsed
		my $key = 1;

               # my $ticker = $data;
               # my $CGISITE = 'http://finance.yahoo.com';
               # my $CGIROOT = $CGISITE . '/d?s=' . $ticker . '&f=s0l1t1cn';
               # my $content=get "$CGIROOT";
               # my $answer = "";
               # $content =~ tr/"/ /;
               # $answer = "$content";
               # $server->command('msg '.$target.' '.$nick.'yahoo: '.$answer);

		while (($key, $price) = each %portfolio_db) {					
	                $server->command('msg '.$target.' '.$nick.'Portfolio: '.$key.' '.$price); 
			#$server->command('msg '.$target.' '.$nick.'Portfolio: '.$price);
			}

                #close database
                untie(%portfolio_db); 
		}

        if (/^!help/i) {
                $server->command('msg '.$target.' '.$nick.'This is lsquote version '.$VERSION.' use !americas, !asia, !europe, !other, !quote ticker, !metals, !energy, !fut, !forex, !in ticker price, !portfolio, !out ticker price, !spot');
                }

        if (/^!indices/i) {
                $server->command('msg '.$target.' '.$nick.' !indices has been replaced with regional triggers: !americas, !asia, !europe, and !other');
                }
	if (/^!americas/i) {	
		my $site1=get "http://finance.yahoo.com/d?s=^dji&f=s0l1t1cn";
		$site1 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$site1);

		my $site2=get "http://finance.yahoo.com/d?s=^gspc&f=s0l1t1cn";
		$site2 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$site2);

		my $site3=get "http://finance.yahoo.com/d?s=^ixic&f=s0l1t1cn";
		$site3 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$site3);

		my $site4=get "http://finance.yahoo.com/d?s=^MERV&f=s0l1t1cn";
		$site4 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$site4);

		my $site5=get "http://finance.yahoo.com/d?s=^BVSP&f=s0l1t1cn";
		$site5 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$site5);

                my $site6=get "http://finance.yahoo.com/d?s=^GSPTSE&f=s0l1t1cn";
		$site6 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$site6);

                my $site7=get "http://finance.yahoo.com/d?s=^SPCDNX&f=s0l1t1cn";
		$site7 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$site7);

                my $site8=get "http://finance.yahoo.com/d?s=^mxx&f=s0l1t1cn";
                $site8 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'yahoo: '.$site8);

		}


        if (/^!asia/i) {
                my $site1=get "http://finance.yahoo.com/d?s=^aord&f=s0l1t1cn";
                $site1 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'yahoo: '.$site1);
               my $site2=get "http://finance.yahoo.com/d?s=^axjo&f=s0l1t1cn";
               $site2 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site2);
               my $site3=get "http://finance.yahoo.com/d?s=^ssec&f=s0l1t1cn";
               $site3 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site3);
               my $site4=get "http://finance.yahoo.com/d?s=^n225&f=s0l1t1cn";
               $site4 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site4);
               my $site5=get "http://finance.yahoo.com/d?s=^KS11&f=s0l1t1cn";
               $site5 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site5);
               my $site6=get "http://finance.yahoo.com/d?s=^TWII&f=s0l1t1cn";
               $site6 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site6);
               my $site7=get "http://finance.yahoo.com/d?s=^BSESN&f=s0l1t1cn";
               $site7 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site7);
		}

        if (/^!europe/i) {
                my $site1=get "http://finance.yahoo.com/d?s=^FCHI&f=s0l1t1cn";
                $site1 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'yahoo: '.$site1);
               my $site2=get "http://finance.yahoo.com/d?s=^GDAXI&f=s0l1t1cn";
               $site2 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site2);
               my $site3=get "http://finance.yahoo.com/d?s=^SMSI&f=s0l1t1cn";
               $site3 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site3);
               my $site4=get "http://finance.yahoo.com/d?s=^SSMI&f=s0l1t1cn";
               $site4 =~ tr/"/ /;  
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site4);
               my $site5=get "http://finance.yahoo.com/d?s=^FTSE&f=s0l1t1cn";
               $site5 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site5);
               my $site6=get "http://finance.yahoo.com/d?s=^MIBTEL&f=s0l1t1cn";
               $site6 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site6);  
               my $site7=get "http://finance.yahoo.com/d?s=RTS.RS&f=s0l1t1cn";
               $site7 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site7);
               my $site8=get "http://finance.yahoo.com/d?s=^ISEQ&f=s0l1t1cn";
               $site8 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site8);
                }

        if (/^!other/i) {
                my $site1=get "http://finance.yahoo.com/d?s=^CCSI&f=s0l1t1cn";
                $site1 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'yahoo: '.$site1);
               my $site2=get "http://finance.yahoo.com/d?s=^TA100&f=s0l1t1cn";
               $site2 =~ tr/"/ /;
               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site2);
#               my $site3=get "http://finance.yahoo.com/d?s=^SMSI&f=s0l1t1cn";
#               $site3 =~ tr/"/ /;
#               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site3);
#               my $site4=get "http://finance.yahoo.com/d?s=^SSMI&f=s0l1t1cn";
#               $site4 =~ tr/"/ /;
#               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site4);
#               my $site5=get "http://finance.yahoo.com/d?s=^FTSE&f=s0l1t1cn";
#               $site5 =~ tr/"/ /;
#               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site5);
#               my $site6=get "http://finance.yahoo.com/d?s=^MIBTEL&f=s0l1t1cn";
#               $site6 =~ tr/"/ /;  
#               $server->command('msg '.$target.' '.$nick.'yahoo: '.$site6);

}

	if (/^!quote/i) {	

		#  $data needs to be set to whatever follows !quote here 
		#
		$data = $msg;
		$data =~ s/^!quote //;
		
		my $ticker = $data;
		my $CGISITE = 'http://finance.yahoo.com';
		my $CGIROOT = $CGISITE . '/d?s=' . $ticker . '&f=s0l1t1cn';
		my $content=get "$CGIROOT";
 		my $answer = "";
		$content =~ tr/"/ /;
		$answer = "$content";
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$answer);
		}

	if (/^!metals/i) {
#		my $site1=get "http://finance.yahoo.com/d?s=PLg08.NYM&f=s0l1t1cn";
                my $site2=get "http://finance.yahoo.com/d?s=PLf09.NYM&f=s0l1t1cn";

#                my $site3=get "http://finance.yahoo.com/d?s=GCg08.CMX&f=s0l1t1cn";
		my $site4=get "http://finance.yahoo.com/d?s=ZGz08.CBT&f=s0l1t1cn";

                my $site5=get "http://finance.yahoo.com/d?s=PAZ08.NYM&f=s0l1t1cn";
#                my $site4=get "http://finance.yahoo.com/d?s=PAH08.NYM&f=s0l1t1cn";

#               my $site6=get "http://finance.yahoo.com/d?s=SIg08.CMX&f=s0l1t1cn";
	        my $site7=get "http://finance.yahoo.com/d?s=ZIz08.CBT&f=s0l1t1cn";	

#                my $site8=get "http://finance.yahoo.com/d?s=HGg08.CMX&f=s0l1t1cn";
                my $site9=get "http://finance.yahoo.com/d?s=HGz08.CMX&f=s0l1t1cn";

#		my $site10=get "http://finance.yahoo.com/d?s=ALg08.CMX&f=s0l1t1cn";
                my $site11=get "http://finance.yahoo.com/d?s=ALz08.CMX&f=s0l1t1cn";

#		$site1 =~ tr/"/ /;
#		$server->command('msg '.$target.' '.$nick.'futures: '.$site1);
		$site2 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'futures: '.$site2);
#                $site3 =~ tr/"/ /;
#                $server->command('msg '.$target.' '.$nick.'futures: '.$site3);
                $site4 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'futures: '.$site4);
                $site5 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'futures: '.$site5);
#                $site6 =~ tr/"/ /;
#                $server->command('msg '.$target.' '.$nick.'futures: '.$site6);
                $site7 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'futures: '.$site7);
#                $site8 =~ tr/"/ /;
#                $server->command('msg '.$target.' '.$nick.'futures: '.$site8);
                $site9 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'futures: '.$site9);
#                $site10 =~ tr/"/ /;
#                $server->command('msg '.$target.' '.$nick.'futures: '.$site10);
                $site11 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'futures: '.$site11);
		}
	if (/^!energy/i) {
                my $site1=get "http://finance.yahoo.com/d?s=CLz08.NYM&f=s0l1t1cn";
                my $site2=get "http://finance.yahoo.com/d?s=NGz08.NYM&f=s0l1t1cn";
                my $site3=get "http://finance.yahoo.com/d?s=PNz08.NYM&f=s0l1t1cn";
                #my $site4=get "http://finance.yahoo.com/d?s=&f=s0l1t1cn";
		$site1 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'futures: '.$site1);
                $site2 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'futures: '.$site2);
                $site3 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'futures: '.$site3);
                #$site4 =~ tr/"/ /;
                #$server->command('msg '.$target.' '.$nick.'futures: '.$site4);
		}		

        if (/^!fut/i) {
        my $site1="lynx -dump http://sites3.barchart.com/pl/vsn/default.asp  > /tmp/fut.txt";
        $site1=system($site1);
        
        my $site2="cat /tmp/fut.txt|tr '\n' ' '|tr '\133' '\n' |tr '\135' '\n'|grep SPZ8 |head -1 >/tmp/fut2";
	$site2=system($site2);

	my $site3="cat /tmp/fut.txt|tr '\n' ' '|tr '\133' '\n'|tr '\135' '\n' |grep ESZ8 |head -1 >>/tmp/fut2";
	$site3=system($site3); 

	my $site4="cat /tmp/fut.txt|tr '\n' ' '|tr '\133' '\n'|tr '\135' '\n' |grep NDZ8 |head -1>>/tmp/fut2";
	$site4=system($site4); 

	#$site4 =~ s/  \[..\]//;
	my $site5="cat /tmp/fut.txt|tr '\n' ' '|tr '\133' '\n'|tr '\135' '\n' |grep NQZ8 |head -1>>/tmp/fut2";
	$site5=system($site5); 

	my $site6="cat /tmp/fut.txt|tr '\n' ' '|tr '\133' '\n'|tr '\135' '\n' |grep DJZ8 |head -1>>/tmp/fut2";
	$site6=system($site6); 

	my $site7="cat /tmp/fut.txt|tr '\n' ' '|tr '\133' '\n'|tr '\135' '\n' |grep ZDZ8 |head -1>>/tmp/fut2";
	$site7=system($site7); 

	my $site8="cat /tmp/fut.txt|tr '\n' ' '|tr '\133' '\n'|tr '\135' '\n' |grep YMZ8 |head -1 |tr '\n' '\b' >> /tmp/fut2";
	$site8=system($site8); 

	$server->command('msg '.$target.' '.$nick.'barchart.com: Contract Month Open High Low Last Change Time');
        my $fh = new IO::File;
        if ($fh->open("< /tmp/fut2")) {
		my $line = 2;
			while (<$fh>) {
				#$_ =~ s/  ..\]//;
				$server->command('msg '.$target.' '.$nick.'futures: '.$_);
			}
        $fh->close;
        }
	}

#This is a special menu for someone named sp00nge :)
        if (/^!sp00nge/i) {
                my $site1=get "http://finance.yahoo.com/d?s=aun.ax&f=s0l1t1cn";
                my $site2=get "http://finance.yahoo.com/d?s=aeo.ax&f=s0l1t1cn";
                my $site3=get "http://finance.yahoo.com/d?s=iin.ax&f=s0l1t1cn";
                my $site4=get "http://finance.yahoo.com/d?s=cti.ax&f=s0l1t1cn";
                my $site5=get "http://finance.yahoo.com/d?s=spn.ax&f=s0l1t1cn";
                my $site6=get "http://finance.yahoo.com/d?s=sgt.ax&f=s0l1t1cn";

                $site1 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'special menu: '.$site1);
                $site2 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'special menu: '.$site2);
                $site3 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'special menu: '.$site3);
                $site4 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'special menu: '.$site4);
		$site5 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'special menu: '.$site5);
                $site6 =~ tr/"/ /;
                $server->command('msg '.$target.' '.$nick.'special menu: '.$site6);
		}

#These are some forex options that were requested by some people  i just havn't figured out how/where to snag the exchange rate data yet
#Ok the crap works, but i could sure use some help cleaning it up, thanks

        if (/^!forex/i) {
                #my $site1=get "http://secure.efxnow.com/MarketData/ENG/Rates.aspx";
		#my $command=~"links -dump";
		#my $site1= "";
		#my $forexsite=~"http://secure.efxnow.com/MarketData/ENG/Rates.aspx";
		#my $site1=system("links -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx");

		#my $site="lynx -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx  > /tmp/rates.txt ";

		#$site=system($site);

#		my $site1="lynx -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx |grep EUR/USD > /tmp/eurusd";
#		my $site2="lynx -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx |grep USD/JPY > /tmp/usdjpy";
#		my $site3="lynx -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx |grep GBP/USD > /tmp/gbpusd";
#		my $site4="lynx -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx |grep EUR/GBP > /tmp/eurgbp";
#		my $site5="lynx -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx |grep USD/CHF > /tmp/usdchf";
#		my $site6="lynx -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx |grep AUD/USD > /tmp/audusd";
#		my $site7="lynx -dump http://secure.efxnow.com/MarketData/ENG/Rates.aspx |grep USD/CAD > /tmp/usdcad";

#               my $site1="lynx -dump http://www.ac-markets.com/forex-resources/currency-exchange-rates.aspx > /tmp/fxrates

		my $site8="lynx -dump http://www.mgforex.com/eng/forex-tools/content/forex-rates.htm > /tmp/fxrates";
	$site8=system($site8);
		#cat forex-rates.htm |grep USDJPY|head -2 |tail -1
               my $site1="cat /tmp/fxrates |grep EURUSD|head -2 |tail -1 > /tmp/eurusd";
               my $site2="cat /tmp/fxrates |grep USDJPY|head -2 |tail -1 > /tmp/usdjpy";
               my $site3="cat /tmp/fxrates |grep GBPUSD|head -2 |tail -1 > /tmp/gbpusd";
               my $site4="cat /tmp/fxrates |grep EURGBP|head -2 |tail -1 > /tmp/eurgbp";
               my $site5="cat /tmp/fxrates |grep USDCHF|head -2 |tail -1 > /tmp/usdchf";
               my $site6="cat /tmp/fxrates |grep AUDUSD|head -2 |tail -1 > /tmp/audusd";
               my $site7="cat /tmp/fxrates |grep USDCAD|head -2 |tail -1 > /tmp/usdcad";

		$site1=system($site1);
		$site2=system($site2);
		$site3=system($site3);
		$site4=system($site4);
		$site5=system($site5);
		$site6=system($site6);
		$site7=system($site7);
		#$site8=system($site8);
		#$site1= rates.txt IO::File;

                #my $fh = new IO::File;
                #if ($fh->open("< /tmp/fxrates")) {
                #      $site8 = <$fh>;
                #        $fh->close;
                #        }
		my $fh = new IO::File;
		if ($fh->open("< /tmp/eurusd")) {
			
		        #print <$fh>;
			$site1 = <$fh>;
        		$fh->close;
    			}		

               if ($fh->open("< /tmp/usdjpy")) {

                        #print <$fh>;
                        $site2 = <$fh>;
                        $fh->close;
                        }
               if ($fh->open("< /tmp/gbpusd")) {

                        #print <$fh>;
                        $site3 = <$fh>;
                        $fh->close;
                        }
               if ($fh->open("< /tmp/eurgbp")) {

                        #print <$fh>;
                        $site4 = <$fh>;
                        $fh->close;
                        }
               if ($fh->open("< /tmp/usdchf")) {

                        #print <$fh>;
                        $site5 = <$fh>;
                        $fh->close;
                        }
               if ($fh->open("< /tmp/audusd")) {

                        #print <$fh>;
                        $site6 = <$fh>;
                        $fh->close;
                        }
               if ($fh->open("< /tmp/usdcad")) {

                        #print <$fh>;
                        $site7 = <$fh>;
                        $fh->close;
                        }


		#my $site1 = system ("echo $site1");
		#$site1 =~ 'grep USD';
		#$site1 =~ /[0-9]*/ /;
		#$site1 =~ /(?<=<li>|<dd>)(.+?)(?=<br>|<li>|<\/li>|<\/dd>)/ig;
               #$site1 =~ tr/"/ /;
#		$server->command('msg '.$target.' '.$nick.'forex:       pair    rate   spread   high / low');
                $server->command('msg '.$target.' '.$nick.'forex:    Rate   Last   Ystd Close % Chng 2007 Open YTD     MTD'); 
                $server->command('msg '.$target.' '.$nick.'forex: '.$site1);
                $server->command('msg '.$target.' '.$nick.'forex: '.$site2);
                $server->command('msg '.$target.' '.$nick.'forex: '.$site3);
                $server->command('msg '.$target.' '.$nick.'forex: '.$site4);
                $server->command('msg '.$target.' '.$nick.'forex: '.$site5);
                $server->command('msg '.$target.' '.$nick.'forex: '.$site6);
                $server->command('msg '.$target.' '.$nick.'forex: '.$site7);

		}

       if (/^!spot/i) {
       	my $site8="lynx -dump lynx -dump http://www.kitcometals.com/ > /tmp/spot";
       	$site8=system($site8);

        my $site1="cat /tmp/spot |grep Bid|head -1 |tail -1|tr -s ' '|tr '\n' '\b'> /tmp/copper && cat /tmp/spot|grep Low|head -1|tail -1|tr -s ' '|tr '\n' '\b'>> /tmp/copper && cat /tmp/spot |grep Change|head -1 |tail -1 | tr -s ' ' >> /tmp/copper";
        my $site2="cat /tmp/spot |grep Bid|head -2 |tail -1|tr -s ' '|tr '\n' '\b'> /tmp/nickel && cat /tmp/spot|grep Low|head -2|tail -1|tr -s ' '|tr '\n' '\b'>> /tmp/nickel && cat /tmp/spot |grep Change|head -2 |tail -1 | tr -s ' ' >> /tmp/nickel"; 
        my $site3="cat /tmp/spot |grep Bid|head -3 |tail -1|tr -s ' '|tr '\n' '\b'> /tmp/aluminum && cat /tmp/spot|grep Low|head -3|tail -1|tr -s ' '|tr '\n' '\b'>> /tmp/aluminum && cat /tmp/spot |grep Change|head -3 |tail -1 | tr -s ' ' >> /tmp/aluminum"; 
        my $site4="cat /tmp/spot |grep Bid|head -4 |tail -1|tr -s ' '|tr '\n' '\b'> /tmp/zinc && cat /tmp/spot|grep Low|head -4|tail -1|tr -s ' '|tr '\n' '\b'>> /tmp/zinc && cat /tmp/spot |grep Change|head -4 |tail -1 | tr -s ' ' >> /tmp/zinc"; 
        my $site5="cat /tmp/spot |grep Bid|head -5 |tail -1|tr -s ' '|tr '\n' '\b'> /tmp/lead && cat /tmp/spot|grep Low|head -5|tail -1|tr -s ' '|tr '\n' '\b'>> /tmp/lead && cat /tmp/spot |grep Change|head -5 |tail -1 | tr -s ' ' >> /tmp/lead"; 

                $site1=system($site1);
                $site2=system($site2); 
                $site3=system($site3);
                $site4=system($site4);
                $site5=system($site5);

               my $fh = new IO::File;
                if ($fh->open("< /tmp/copper")) {
                        $site1 = <$fh>;
                        $fh->close;   
                        }
                if ($fh->open("< /tmp/nickel")) {
                        $site2 = <$fh>;
                        $fh->close;   
                        }
                if ($fh->open("< /tmp/aluminum")) {
                        $site3 = <$fh>;
                        $fh->close;   
                        }
                if ($fh->open("< /tmp/zinc")) {
                        $site4 = <$fh>;
                        $fh->close;   
                        }
                if ($fh->open("< /tmp/lead")) {
                        $site5 = <$fh>;
                        $fh->close;   
                        }

                $server->command('msg '.$target.' '.$nick.'kitco: spot prices');
                $server->command('msg '.$target.' '.$nick.'kitco: copper  '.$site1);
                $server->command('msg '.$target.' '.$nick.'kitco: nickel  '.$site2);
                $server->command('msg '.$target.' '.$nick.'kitco: aluminum'.$site3);
                $server->command('msg '.$target.' '.$nick.'kitco: zinc    '.$site4);
                $server->command('msg '.$target.' '.$nick.'kitco: lead    '.$site5);


}

}

signal_add("message public", "public_question");
signal_add("message own_public", "own_question");
