deprecated has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A code review if you please (code)
by chipmunk (Parson) on Jul 02, 2001 at 23:41 UTC | |
|
Re: A code review if you please (code)
by japhy (Canon) on Jul 02, 2001 at 23:15 UTC | |
|
Re: A code review if you please (code)
by Brovnik (Hermit) on Jul 03, 2001 at 13:33 UTC |