Do you like writing bots for irc? Well, if you have ever had trouble parsing the lines sent from the server into data you can use, I have a solution. Just use this simple OO module, which exports two methods: new, and parse_line ... new creates an object, and parse_line splits up each line from the irc server into usable data that your bot can then react to.
Have fun!
package Mind;
use strict;
use diagnostics;
use warnings;
use Data::Dumper;
sub new {
my $class = shift;
my $self = {
NICK => undef,
HOST => undef,
TYPE => undef,
CHAN => undef,
TEXT => undef
};
bless $self, $class;
return $self;
}
sub parse_line {
my $self = shift;
chop (my $line = shift);
my ($i_nick, $i_host, $i_type, $i_chan, $i_text);
($i_chan, $i_text) = split(/ :/, $line);
($i_nick, $i_type, $i_chan) = split(/ /, $line);
($i_nick, $i_host) = split(/!/, $i_nick);
$i_nick =~ s/://;
$self->{NICK} = $i_nick;
$self->{HOST} = $i_host;
$self->{TYPE} = $i_type;
$self->{CHAN} = $i_chan;
$self->{TEXT} = $i_text;
#print Dumper($self); #test
return $self;
}
#add your own method here
#to allow for your bot to
#react to things that happen
#in your channel like so:
sub interact {
#test ...
my $self = shift;
if ($self->{TEXT} =~ /hi shrax/gi && $self->{NICK} =~ /kenny/g
+i) {
my $self = "$self->{TYPE} $self->{CHAN} :$self-
+>{NICK}, it works!";
return $self;
}
if ($self->{TEXT} =~ /how are you shrax/gi && $self->{NICK} =~
+ /kenny/gi) {
my $self = "$self->{TYPE} $self->{CHAN} :$self-
+>{NICK}, I'm fine thank you!";
return $self;
}
}
1;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.