Revision 1 (by moose, 2006/07/20 11:34:56) initial import
#!/usr/bin/perl

use lib ($ENV{HOME} . '/PerlLib');
use PerlLib;

use warnings;
use diagnostics;
use strict;

use Fcntl;   # For O_RDWR, O_CREAT, etc.
use MLDBM qw(SDBM_File Storable);

our $pl = new PerlLib();

#tie(%h, 'MLDBM', $pl->file_path('cache/.mldbm'), O_RDWR|O_CREAT, 0666)
#		or $pl->die ("Couldn't tie MLDBM file '" . $pl->file_path('cache/.mldbm') . "': $!; aborting");

our $irc_serv = "irc.freenode.org";
our $irc_port = 6667;
our $main_channel = "tomasu";
our $bot_nick = "Moosie";

our %events;

use Socket;
use IO::Socket::INET;

use Math::BigFloat;

our $sock = IO::Socket::INET->new(
        PeerAddr => $irc_serv, 
        PeerPort => $irc_port,
		  ReuseAddr => 1,
		  Type => SOCK_STREAM,
#		  Blocking => 0,
        Proto => 'tcp' ) or die "could not make the connection";

our $line;

our %users = (
	'Inphernic' => [qw/Quack/],
);

our %command_message_handlers = (
	"addalias" => sub {  },
	"remalias" => sub {  },
);
	
our %message_handlers = (

);

our %command_handlers = (
	"PING" => sub {  },
	"NICK" => sub {  },
	"JOIN" => sub {  },
);

#while(defined ($line = <$sock>)){
#	print "LINE: $line";
#	if($line =~ /(NOTICE AUTH).*(checking ident)/i){
		print $sock "NICK $bot_nick\n";
		print $sock "USER bot 0 0 :just a bot\n";
#		last;
#	}
#}

#while(defined($line = <$sock>)){
#	print "LINE: $line";   
	#use next line if the server asks for a ping
#	if($line =~ /^PING/){
#		print $sock "PONG :" . (split(/ :/, $line))[1] . "\n";
#	}
#	if($line =~ /(376|422)/i){
#		#print $sock "NICKSERV :identify me\n";
#		last;
#	}
#}

#sleep 3;
print $sock "JOIN #$main_channel\n";

while (defined($line = <$sock>) and $sock->connected) {
	print "LINE: $line";
	
	my ($info, $text) = split(/ :/, $line);   #$text is the stuff from the ping or the text from the server
	
	if (substr($info, 0, 4) eq "PING") {
		#while there is a line break - many different ways to do this
		#while ( (index($text,"\r") >= 0) || (index($text,"\n") >= 0) ){ chop($text); }
		#$text =~ s/[\r\n]+//g;
		
		print "PONG $text\n";
		print $sock "PONG $text\n";
		
		next;
	}
	
	#done with ping handling
	
	my ($user, $command, $channel) = split(" ", $info);
	
	my ($nick,$hostname) = split(/!/, $user); #split by ! to get nick and hostname seperate
        
	$nick =~ s/://; #remove :'s
	$text =~ s/://;

	#get rid of all line breaks.  Again, many different way of doing this.
	#$/ = "\r\n";
	#while($text =~ m#$/$#){ chomp($text); }
	$text =~ s/[\r\n]+//g;

	#print "Command: '$command', Chan: $channel, Nick: $nick, Host: $hostname\n";
	
	if($command eq "PRIVMSG" && $channel eq "#$main_channel") {
			print "<$nick> $text\n";
                
			if($text eq "moose"){
				$sock->send( "PRIVMSG #$main_channel :Moo.\n" );
				print "PRIVMSG #$main_channel :Moo.\n";
			}
	}
#	elsif($command eq "NICK" {
		
#	}

}

#sub reigster_message_handler {
	
#}

#sub cpy_hash {
#	my %hash = @_;
#	return \%hash;
#}