Bot::Net::Util - miscellaneous utility functions


Bot-Net documentation Contained in the Bot-Net distribution.

Index


Code Index:

NAME

Top

Bot::Net::Util - miscellaneous utility functions

SYNOPSIS

Top

  my @args = Bot::Net::Util->parse_bot_command($message);

DESCRIPTION

Top

Provides utility functions for use elsewhere.

METHODS

Top

parse_bot_command MESSAGE

Returns an array of words found in the given message. This tries to sensibly break up a command. The string will be split on whitespace or grouped by quotes.

Quoted strings may contain quotes by containing a double-quote to escape it. For example,

  tell 'bob''s friend' """Hello World"""

would become:

  ('tell', "bob's friend", '"Hello World"')

AUTHORS

Top

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

COPYRIGHT AND LICENSE

Top


Bot-Net documentation Contained in the Bot-Net distribution.
use strict;
use warnings;

package Bot::Net::Util;

use Bot::Net;
use Carp;
use Regexp::Common qw/ delimited /;

sub parse_bot_command {
    my $class = shift;
    local $_  = shift;

    unless (defined $_) {
        carp "No string given to parse. Returning an empty list.";
        return;
    }

    my @args = m/
                ( $RE{delimited}{-delim=>'"'}{-esc=>'"'} 
                | $RE{delimited}{-delim=>"'"}{-esc=>"'"}
                | \S+
                )
        /gx;

    # Strip the quotes and unescape the "" and '' escapes
    map {
        /^"/ && /"$/ && s/^"// && s/"$// && s/""/"/g;
        /^'/ && /'$/ && s/^'// && s/'$// && s/''/'/g;
    } @args;

    return @args;
}

1;