#!perl -T
# 
# everything works for now
# designed to run in Irssi, i deleted my initial xchat version
# 08/05/07 b
# added !forex, 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.0.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;
#		#$symbol =~ s/^!in //;
#		#$price =~ s/^!in //;
#		$data =~ s/^!in //;
#		$data =~ /^\s*(\w+)\s+(\w+)\s*$/;
#		#$fh = new IO::File;
#		$server->command('msg '.$target.' '.$nick.'Added '.$1.' at '.$2. ' to your portfolio');
#		}
#	if (/^!out/i) {
#		$data = $msg;
#               #$symbol =~ s/^!in //;
#                #$price =~ s/^!in //;
#                $data =~ s/^!in //;
#                $data =~ /^\s*(\w+)\s+(\w+)\s*$/;
#		
#                #$fh = new IO::File;
#                $server->command('msg '.$target.' '.$nick.'You've exited '.$1.' at '.$2. ' for a ' .$return 'return');		
#		}
#        if (/^!portfolio/i) {
#                $data = $msg;
#                $data =~ s/^!portfolio //;
#		
#
#		}

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

	if (/^!indices/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=^aord&f=s0l1t1cn";
		$site4 =~ tr/"/ /;
		$server->command('msg '.$target.' '.$nick.'yahoo: '.$site4);

		my $site5=get "http://finance.yahoo.com/d?s=^axjo&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);
		}
	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=PLN07.NYM&f=s0l1t1cn";
                my $site2=get "http://finance.yahoo.com/d?s=GCN07.CMX&f=s0l1t1cn";
                my $site3=get "http://finance.yahoo.com/d?s=PAN07.NYM&f=s0l1t1cn";
                my $site4=get "http://finance.yahoo.com/d?s=SIN07.CMX&f=s0l1t1cn";
                my $site5=get "http://finance.yahoo.com/d?s=HGN07.CMX&f=s0l1t1cn";
		my $site6=get "http://finance.yahoo.com/d?s=ALN07.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);
		}
	if (/^!energy/i) {
                my $site1=get "http://finance.yahoo.com/d?s=CLU07.NYM&f=s0l1t1cn";
                my $site2=get "http://finance.yahoo.com/d?s=NGQ07.NYM&f=s0l1t1cn";
                my $site3=get "http://finance.yahoo.com/d?s=PNQ07.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=CRU07.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";
		$site1=system($site1);
		$site2=system($site2);
		$site3=system($site3);
		$site4=system($site4);
		$site5=system($site5);
		$site6=system($site6);
		$site7=system($site7);

		#$site1= rates.txt IO::File;
		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.com:       pair    rate   spread   high / low');
                $server->command('msg '.$target.' '.$nick.'forex.com: '.$site1);
                $server->command('msg '.$target.' '.$nick.'forex.com: '.$site2);
                $server->command('msg '.$target.' '.$nick.'forex.com: '.$site3);
                $server->command('msg '.$target.' '.$nick.'forex.com: '.$site4);
                $server->command('msg '.$target.' '.$nick.'forex.com: '.$site5);
                $server->command('msg '.$target.' '.$nick.'forex.com: '.$site6);
                $server->command('msg '.$target.' '.$nick.'forex.com: '.$site7);

		}


}

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