Several friends of mine and myself are writing small utility programs for our napster network in the form of "bots" (almost exactly like their IRC equivalent). Since we're all capable perl programmers, I thought it would be best if we could share our code, and move functions from one bot to the other, or write a new function and have it work in another bot. Write once, run everywhere, right?

So I am writing this API for a MP3::Napster bot. Pretty simple code, but I am trying to make it robust and a pleasure to write code for. No use in making something _else_ we dont want to code in.

I'd like it if a few monks could look over the code and make some observations before I plow into the real guts of the API.

Thanks,
brother dep.

package Bot; require 5.6; # update or die. use warnings; use strict; use Carp; use Data::Dumper; use MP3::Napster; use POSIX; ######################################################### ## data, mkay? ## ######################################################### # warnings and debug level, respectively... our ($squawk, $dl); $squawk = 1; $dl = 1; # this is where we are leaving subs we import. our %importedMethods = (); # information on the bot our %botInfo = (); # information for our hackers our %messages = (); # our bot. our $bot; ######################################################### ## subs, mkay? ## ######################################################### # change the debug level sub debug { $dl = shift; } # convert and verify the hash that the programmer gives us for install +ation # into the bot. sub convertAttributes { my %allowedAttributes = ( bot_name => qr/[:alnum:]{1,16}/, prefix => qr/[:alnum:]{1}/, server => qr/[:alnum:]+/, password => qr/[:alnum:]{32}/, port => qr/\d{1,5}/, channel => qr/#?[:alnum:]{1,16}/, ); my %incoming = %{ shift() }; my @badAttributes = grep { $incoming{$_} !~ $allowedAttributes{$_} } keys %incoming; if (scalar @badAttributes == 0) { %botInfo = %incoming; return 1; } else { return 0; } } # handle public messages sub pubHandler { my $botCommands = join '|', keys %importedMethods; $botCommands = qr{($botCommands)}; my $self = shift; my ($ec, $message) = @_; my ($channel, $nick, $msg) = $message =~ m[^(\S+) (\S+) (.*)]; if (my ($command) = $msg =~ m[$botInfo{prefix}($botCommands)]) { # we've been issued a command my $rval = $importedMethods{$command} -> ( $nick, $msg ); if ($rval) { sendPub( $rval ) } } else { return 0 } } # get the bot up and running or die. sub stage { $bot = MP3::Napster -> new ( "$botInfo{server}:$botInfo{port}" ) or die "Could not connect to $botInfo{server}\n"; $bot -> login( $botInfo{bot_name}, $botInfo{password}, 'LINK_UNKNOWN +' ) or die "$botInfo{bot_name} could not log in.\n"; $bot -> join_channel( $botInfo{channel} ) or die "$botInfo{bot_name} could not join $botInfo{channel}\n"; } # verify that the code we are being given is good code. sub checkFuncSyntax { my $codeBlock = shift; { no strict; local $^W = 0; eval "sub {\n$codeBlock\n}"; } die "bad code submitted, $@\n" if $@; return $@ || 0; } ######################################################### ## object constructor, mkay? ## ######################################################### sub new { my $package = shift; my %attributes = %{ shift() }; my ($bot_name, $prefix, $server, $password, $port, $channel); $bot_name = $attributes{name}; $prefix = $attributes{prefix}; $server = $attributes{server}; $password = $attributes{password}; $port = $attributes{port}; $channel = $attributes{channel}; for ($bot_name, $prefix, $server, $password, $port, $channel) { die "Incomplete attribute list" unless $_; } convertAttributes( \%attributes ) or die "Attribute list malformatted"; bless { %botInfo }, $package; } ######################################################### ## methods, mkay? ## ######################################################### # add a new function to the bot. sub addFunction { my $self = shift; my ($funcName, $func) = (@_); if ( my $rval = checkFuncSyntax( $func ) ) { $importedMethods{$funcName} = $func; return 1; } else { warn "function $funcName not imported: $rval\n"; return 0; } } # we do this after we're connected to get the bot running # and install our methods. sub botRun { if (! $bot ) { warn "Bot not connected, or bot not running...\n"; return 0; } $bot -> callback(PUBLIC_MESSAGE, \&pubHandler); $bot -> callback(PRIVATE_MESSAGE, \&msgHandler); return 1; } # send a public message. sub sendMsg { my $self = shift; my $user = shift; my $msg = shift; $self -> private_message( $user, $msg ); return 1; } =cut =pod =head1 synposis my $bot = Bot -> new( server => 'localhost', port => '8888', bot_name => 'bot', password => 'secret', channel = 'bots', prefix => ':' ); $bot -> stage(); $bot -> botRun(); while ($bot) { $bot -> addFunction( 'execute', sub { my $out = qx/shift/; $out } );

--
Laziness, Impatience, Hubris, and Generosity.


In reply to A code review if you please (code) by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.