C
Chris
I am new to perl. and have got stuck on a error that occurs in my
program. The error that i get is the following
~/test]# perl girbot.pl
Global symbol "$latest" requires explicit package name at girbot.pl
line 148.
Global symbol "$rss" requires explicit package name at girbot.pl line
151.
Global symbol "$latest" requires explicit package name at girbot.pl
line 159.
Execution of girbot.pl aborted due to compilation errors.
My program looks like this. The bit that i am having problems with is
the bit that generates the rss file. Also i am not exactly sure if my
regex are corrent. In theory they should search for ed2k:// in $arg and
if it is found, save $arg to the rss file. When I get it working i will
use substr to just pull out the ed2k:// link. But first i have to get
around those nast errors that are above.
can anybody help?
thanks
chris
#!/usr/bin/perl
# Usage: perl girbot.pl <config file, optional>
# <setting>=<value>
use strict;
use Net::IRC;
use XML::RSS;
sub GetSetting
{
my ($setting,$config_file)=@_;
open(CFGFILE,"<$config_file")
or die "Can't open configuration file ($config_file)";
my @slist=<CFGFILE>;
foreach my $selem (@slist)
{
if (index($selem,"#")==0) { next; }
my @ln=split("=",$selem);
if ($ln[0] =~ /$setting/i)
{
chomp $ln[1];
return $ln[1];
}
}
close CFGFILE;
}
# =============
# MAIN BOT CODE
# =============
# Set our configuration file
my $configuration_file = "gir.cfg";
# You can start the bot with a config file as a
# commandline argument. Without the argument,
# the bot loads its settings from "gir.cfg", in
# the same directory as the bot.
if($#ARGV==0) { $configuration_file=$ARGV[0]; }
# Now, we can load in our script's settings
my $cfg_nick=GetSetting("nick",$configuration_file);
my $cfg_altnick=GetSetting("altnick",$configuration_file);
my $cfg_ident=GetSetting("ident",$configuration_file);
my $cfg_port=GetSetting("port",$configuration_file);
my $cfg_server=GetSetting("server",$configuration_file);
my $cfg_channel=GetSetting("channel", $configuration_file);
# Just about all of the settings are "strings", except
# for the "port". Let's make sure that that setting
# is numerical, and if not, set it to the most common
# port, 6667:
if($cfg_port=~/\D/) { $cfg_port=6667; }
# Now that all of our settings are loaded in,
# let's create the IRC object
my $irc = new Net::IRC;
print "Creating connection to IRC server...";
my $conn = $irc->newconn(Server => "$cfg_server",
Port => $cfg_port,
Nick => "$cfg_nick",
Ircname => "$cfg_ident",
Username => "$cfg_ident")
or die "Can't connect to IRC server.";
print "done!\n";
$conn->{channel} = '#sonic';
# With that out of the way, let's create
# some subs for our object handlers
# What our bot will do when it finishes
# connecting to the IRC server
sub on_connect {
my $self = shift;
print "*** Connected to IRC.\n";
$conn->join("$cfg_channel");
print "*** Joined Channel: $cfg_channel.\n";
}
# This sub will print various
# incoming date while we're still
# connecting to IRC
sub on_init {
my ($self, $event) = @_;
my (@args) = ($event->args);
shift (@args);
print "*** @args\n";
}
# This sub will handle what happens when the
# bot receives public (channel) text.
sub on_public {
my ($self, $event) = @_;
my @to = $event->to;
my ($nick, $mynick) = ($event->nick, $self->nick); # Sender text,
Bot nick
my $host=$event->host; # Sender's hostname
my ($arg) = ($event->args); # The message
# Here's where we want to "parse" channel text
print "<$nick> $arg\n";
if ($nick eq "eZebra" && $arg=~m/ed2k:\/\//i)
{
if ($latest == 1)
{
#&add_ed2k();
$rss->add_item(
link => '$arg'
);
}
}
if ($nick eq "eZebra" && $arg=~m/--- Latest links: ---/i)
{
if ($latest != 1)
{
my $latest = 1;
#&start_rss();
my $rss = new XML::RSS(version => '0.91');
$rss->channel(
title => 'Test RSS Feed',
link => 'irc://irc.efnet.net/test',
description => 'Toast'
);
}
}
if ($nick eq "eZebra" && $arg=~m/------/i)
{
#&end_rss();
my $latest = 0;
rss->save("~/feed.rss");
}
}
# This sub will handle what happens when the
# bot receives private message text
sub on_msg {
my ($self, $event) = @_;
my ($nick) = $event->nick; # Message Sender
my ($arg) = ($event->args); # Message Text
my $host=$event->host;
# Here's where we want to "parse" message text
print " - $nick - $arg\n";
}
# This sub will get triggered if our bot's nick
# is taken, setting it to our alternate nick.
sub on_nick_taken {
my ($self) = shift;
$self->nick($cfg_altnick);
}
sub on_join {
# get our connection object and the event object, which is passed
# with this event automatically
my ($conn, $event) = @_;
# this is the nick that just joined
my $nick = $event->{nick};
# say hello to the nick in public
$conn->privmsg($conn->{channel}, "Hello, $nick!");
if ($nick = "testuser")
{
$conn->mode('#sonic', '+o', 'testuser');
print "*** Modifying $nick Mode to: +o\n";
}
}
# Now that all of our handler subs are created,
# let's install them
print "Installing local handlers...";
$conn->add_handler('public', \&on_public);
$conn->add_handler('msg', \&on_msg);
$conn->add_handler('join', \&on_join);
print "done!\nInstalling global handlers...";
$conn->add_global_handler([ 251,252,253,254,302,255 ], \&on_init);
$conn->add_global_handler([376, 422], \&on_connect);
$conn->add_global_handler(433, \&on_nick_taken);
print "done!\n";
# Everything's installed, so there's nothing
# holding up back from starting up!
$irc->start;
program. The error that i get is the following
~/test]# perl girbot.pl
Global symbol "$latest" requires explicit package name at girbot.pl
line 148.
Global symbol "$rss" requires explicit package name at girbot.pl line
151.
Global symbol "$latest" requires explicit package name at girbot.pl
line 159.
Execution of girbot.pl aborted due to compilation errors.
My program looks like this. The bit that i am having problems with is
the bit that generates the rss file. Also i am not exactly sure if my
regex are corrent. In theory they should search for ed2k:// in $arg and
if it is found, save $arg to the rss file. When I get it working i will
use substr to just pull out the ed2k:// link. But first i have to get
around those nast errors that are above.
can anybody help?
thanks
chris
#!/usr/bin/perl
# Usage: perl girbot.pl <config file, optional>
# <setting>=<value>
use strict;
use Net::IRC;
use XML::RSS;
sub GetSetting
{
my ($setting,$config_file)=@_;
open(CFGFILE,"<$config_file")
or die "Can't open configuration file ($config_file)";
my @slist=<CFGFILE>;
foreach my $selem (@slist)
{
if (index($selem,"#")==0) { next; }
my @ln=split("=",$selem);
if ($ln[0] =~ /$setting/i)
{
chomp $ln[1];
return $ln[1];
}
}
close CFGFILE;
}
# =============
# MAIN BOT CODE
# =============
# Set our configuration file
my $configuration_file = "gir.cfg";
# You can start the bot with a config file as a
# commandline argument. Without the argument,
# the bot loads its settings from "gir.cfg", in
# the same directory as the bot.
if($#ARGV==0) { $configuration_file=$ARGV[0]; }
# Now, we can load in our script's settings
my $cfg_nick=GetSetting("nick",$configuration_file);
my $cfg_altnick=GetSetting("altnick",$configuration_file);
my $cfg_ident=GetSetting("ident",$configuration_file);
my $cfg_port=GetSetting("port",$configuration_file);
my $cfg_server=GetSetting("server",$configuration_file);
my $cfg_channel=GetSetting("channel", $configuration_file);
# Just about all of the settings are "strings", except
# for the "port". Let's make sure that that setting
# is numerical, and if not, set it to the most common
# port, 6667:
if($cfg_port=~/\D/) { $cfg_port=6667; }
# Now that all of our settings are loaded in,
# let's create the IRC object
my $irc = new Net::IRC;
print "Creating connection to IRC server...";
my $conn = $irc->newconn(Server => "$cfg_server",
Port => $cfg_port,
Nick => "$cfg_nick",
Ircname => "$cfg_ident",
Username => "$cfg_ident")
or die "Can't connect to IRC server.";
print "done!\n";
$conn->{channel} = '#sonic';
# With that out of the way, let's create
# some subs for our object handlers
# What our bot will do when it finishes
# connecting to the IRC server
sub on_connect {
my $self = shift;
print "*** Connected to IRC.\n";
$conn->join("$cfg_channel");
print "*** Joined Channel: $cfg_channel.\n";
}
# This sub will print various
# incoming date while we're still
# connecting to IRC
sub on_init {
my ($self, $event) = @_;
my (@args) = ($event->args);
shift (@args);
print "*** @args\n";
}
# This sub will handle what happens when the
# bot receives public (channel) text.
sub on_public {
my ($self, $event) = @_;
my @to = $event->to;
my ($nick, $mynick) = ($event->nick, $self->nick); # Sender text,
Bot nick
my $host=$event->host; # Sender's hostname
my ($arg) = ($event->args); # The message
# Here's where we want to "parse" channel text
print "<$nick> $arg\n";
if ($nick eq "eZebra" && $arg=~m/ed2k:\/\//i)
{
if ($latest == 1)
{
#&add_ed2k();
$rss->add_item(
link => '$arg'
);
}
}
if ($nick eq "eZebra" && $arg=~m/--- Latest links: ---/i)
{
if ($latest != 1)
{
my $latest = 1;
#&start_rss();
my $rss = new XML::RSS(version => '0.91');
$rss->channel(
title => 'Test RSS Feed',
link => 'irc://irc.efnet.net/test',
description => 'Toast'
);
}
}
if ($nick eq "eZebra" && $arg=~m/------/i)
{
#&end_rss();
my $latest = 0;
rss->save("~/feed.rss");
}
}
# This sub will handle what happens when the
# bot receives private message text
sub on_msg {
my ($self, $event) = @_;
my ($nick) = $event->nick; # Message Sender
my ($arg) = ($event->args); # Message Text
my $host=$event->host;
# Here's where we want to "parse" message text
print " - $nick - $arg\n";
}
# This sub will get triggered if our bot's nick
# is taken, setting it to our alternate nick.
sub on_nick_taken {
my ($self) = shift;
$self->nick($cfg_altnick);
}
sub on_join {
# get our connection object and the event object, which is passed
# with this event automatically
my ($conn, $event) = @_;
# this is the nick that just joined
my $nick = $event->{nick};
# say hello to the nick in public
$conn->privmsg($conn->{channel}, "Hello, $nick!");
if ($nick = "testuser")
{
$conn->mode('#sonic', '+o', 'testuser');
print "*** Modifying $nick Mode to: +o\n";
}
}
# Now that all of our handler subs are created,
# let's install them
print "Installing local handlers...";
$conn->add_handler('public', \&on_public);
$conn->add_handler('msg', \&on_msg);
$conn->add_handler('join', \&on_join);
print "done!\nInstalling global handlers...";
$conn->add_global_handler([ 251,252,253,254,302,255 ], \&on_init);
$conn->add_global_handler([376, 422], \&on_connect);
$conn->add_global_handler(433, \&on_nick_taken);
print "done!\n";
# Everything's installed, so there's nothing
# holding up back from starting up!
$irc->start;