#!perl -T
# 
# everything works for now
# designed to run in Irssi
# 09/01/08
# i could use some help cleaning this up
#


#use strict;
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.4';
%IRSSI = (
	author		=> 'Soul_keeper',
	contact		=> '#linuxsociety',
	name		=> 'lsquote',
	descriotion	=> 'A better 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/  */ /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);
	#if (!/^indices/i) { return 0; }

# These options will be for the portfolio tracking functions when i get around to setting them up
	if (/^!in/i) {
		$data = $msg;
		$data =~ s/^!in //;
		my $store = "echo $data > /tmp/portfolio$nick";
		$store = system($store);

		my $symbol = "echo $data | tr ' ' ':' | cut -d: -f 1 > /tmp/symbol$nick";
		$symbol = system($symbol);
		my $price = "echo $data | tr ' ' ':' | cut -d: -f 2  > /tmp/price$nick";
		$price = system($price);

		#$symbol = $data;
		#$price = $data;
		#$_ = $data;
		#($first, $second, $third) = /x(.+?)y/g;
		#$symbol ; 
		#$price =~ s/^[a-z] //;
		#$price =~ s/^\w+ //;
		#$symbol =~ /^(.+)(e|r)(.*)$/;
		#$price = /[c-q]//s(.+);
		#$symbol = s///;
		#$price = ^*\s//$/;
		#$symbol =~ s/^!in //;
		#$price =~ s/^!in //;
		#$data =~ s/^!in //;
		#$data =~ /^\s*(\w+)\s+(\w+)\s*$/;
		#$fh = new IO::File;

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

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

               my $fh = new IO::File;
                if ($fh->open("< /tmp/symbol$nick")) {
                        #print <$fh>;
                        $symbol = <$fh>;
                        $fh->close;   
                        }
		
		my $words = "to your portfolio at ";
              	$server->command('msg '.$target.' '.$nick.'Portfolio: '.$portfolio);
		$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 //;

                my $store2 = "echo $data > /tmp/portout$nick";   
                $store2 = system($store2);

                my $symbol2 = "echo $data | tr ' ' ':' | cut -d: -f 1 > /tmp/symbolout$nick";
                $symbol2 = system($symbol2);
                my $price2 = "echo $data | tr ' ' ':' | cut -d: -f 2  > /tmp/priceout$nick";
                $price2 = system($price2);

#                my $fh = new IO::File;
#                if ($fh->open("< /tmp/portout")) {
#                        #print <$fh>;
#                        my $portout = <$fh>;
#                        $fh->close;   
#                        }
                
               my $fh = new IO::File;
                if ($fh->open("< /tmp/price$nick")) {
                        #print <$fh>;
                        $price = <$fh>;
                        $fh->close;
                        }

               my $fh = new IO::File;
                if ($fh->open("< /tmp/priceout$nick")) {
                        #print <$fh>;
                        $price2 = <$fh>;
                        $fh->close;
                        }


               my $fh = new IO::File;
                if ($fh->open("< /tmp/symbolout$nick")) {
                        #print <$fh>;
                        $symbol2 = <$fh>;
			}

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


#                $data =~ /^\s*(\w+)\s+(\w+)\s*$/;
#		
#                #$fh = new IO::File;
                $server->command('msg '.$target.' '.$nick.' Percentage gain/loss: '.$return);		
		}
#        if (/^!portfolio/i) {
#                $data = $msg;
#                $data =~ s/^!portfolio //;
#		$fh = new IO::File;
#		
#		              $server->command('msg '.$target.' '.$nick.'portfolio: '.$data);		
#
#		}

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

        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);
                }

        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=PLv08.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=ZGv08.CBT&f=s0l1t1cn";

                my $site5=get "http://finance.yahoo.com/d?s=PAv08.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=ZIv08.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=HGv08.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=ALv08.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=CLv08.NYM&f=s0l1t1cn";
                my $site2=get "http://finance.yahoo.com/d?s=NGv08.NYM&f=s0l1t1cn";
                my $site3=get "http://finance.yahoo.com/d?s=PNv08.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=get "http://finance.yahoo.com/d?s=CRv08.NYB&f=s0l1t1cn";
                my $site2=get "http://finance.yahoo.com/d?s=^DJS2&f=s0l1t1cn";
                my $site3=get "http://finance.yahoo.com/d?s=^DJC&f=s0l1t1cn";
                my $site4=get "http://finance.yahoo.com/d?s=^XAU&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);
		}

#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);

		}


}

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