| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Bot::Net::Util; | ||
| 5 | |||
| 6 | use Bot::Net; | ||
| 7 | use Carp; | ||
| 8 | use Regexp::Common qw/ delimited /; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Bot::Net::Util - miscellaneous utility functions | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | my @args = Bot::Net::Util->parse_bot_command($message); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Provides utility functions for use elsewhere. | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | =head2 parse_bot_command MESSAGE | ||
| 25 | |||
| 26 | 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. | ||
| 27 | |||
| 28 | Quoted strings may contain quotes by containing a double-quote to escape it. For example, | ||
| 29 | |||
| 30 | tell 'bob''s friend' """Hello World""" | ||
| 31 | |||
| 32 | would become: | ||
| 33 | |||
| 34 | ('tell', "bob's friend", '"Hello World"') | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub parse_bot_command { | ||
| 39 | my $class = shift; | ||
| 40 | local $_ = shift; | ||
| 41 | |||
| 42 | unless (defined $_) { | ||
| 43 | carp "No string given to parse. Returning an empty list."; | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | my @args = m/ | ||
| 48 | ( $RE{delimited}{-delim=>'"'}{-esc=>'"'} | ||
| 49 | | $RE{delimited}{-delim=>"'"}{-esc=>"'"} | ||
| 50 | | \S+ | ||
| 51 | ) | ||
| 52 | /gx; | ||
| 53 | |||
| 54 | # Strip the quotes and unescape the "" and '' escapes | ||
| 55 | map { | ||
| 56 | /^"/ && /"$/ && s/^"// && s/"$// && s/""/"/g; | ||
| 57 | /^'/ && /'$/ && s/^'// && s/'$// && s/''/'/g; | ||
| 58 | } @args; | ||
| 59 | |||
| 60 | return @args; | ||
| 61 | } | ||
| 62 | |||
| 63 | =head1 AUTHORS | ||
| 64 | |||
| 65 | Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >> | ||
| 66 | |||
| 67 | =head1 COPYRIGHT AND LICENSE | ||
| 68 | |||
| 69 | Copyright 2007 Boomer Consulting, Inc. All Rights Reserved. | ||
| 70 | |||
| 71 | This program is free software and may be modified and distributed under the same terms as Perl itself. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | 1; |