Common utilities for irc-related tasks
use strict; use warnings; use IRC::Utils ':ALL'; my $nickname = '^Lame|BOT[moo]'; my $uppercase_nick = uc_irc($nickname); my $lowercase_nick = lc_irc($nickname); print "They're equivalent\n" if eq_irc($uppercase_nick, $lowercase_nick); my $mode_line = 'ov+b-i Bob sue stalin*!*@*'; my $hashref = parse_mode_line($mode_line); my $banmask = 'stalin*'; my $full_banmask = normalize_mask($banmask); if (matches_mask($full_banmask, '[email protected]')) { print "EEK!"; } my $decoded = irc_decode($raw_irc_message); print $decoded, "\n"; if (has_color($message)) { print 'COLOR CODE ALERT!\n"; } my $results_hashref = matches_mask_array(\@masks, \@items_to_match_against); my $nick = parse_user('[email protected]'); my ($nick, $user, $host) = parse_user('[email protected]');
The functions in this module take care of many of the tasks you are faced with when working with \s-1IRC\s0. Mode lines, ban masks, message encoding and formatting, etc.
Takes one mandatory parameter, a string to convert to \s-1IRC\s0 uppercase, and one optional parameter, the casemapping of the ircd (which can be 'rfc1459', 'strict-rfc1459' or 'ascii'. Default is 'rfc1459'). Returns the \s-1IRC\s0 uppercase equivalent of the passed string. Takes one mandatory parameter, a string to convert to \s-1IRC\s0 lowercase, and one optional parameter, the casemapping of the ircd (which can be 'rfc1459', 'strict-rfc1459' or 'ascii'. Default is 'rfc1459'). Returns the \s-1IRC\s0 lowercase equivalent of the passed string. Takes two mandatory parameters, \s-1IRC\s0 strings (channels or nicknames) to compare. A third, optional parameter specifies the casemapping. Returns true if the two strings are equivalent, false otherwise
# long version lc_irc($one, $map) eq lc_irc($two, $map)
# short version eq_irc($one, $two, $map) Takes a list representing an \s-1IRC\s0 mode line. Returns a hashref. Optionally you can also supply an arrayref and a hashref to specify valid channel modes (default: \*(C`[qw(beI k l imnpstaqr)]\*(C') and status modes (default: \*(C`{o => '@', h => '%', v => '+'}\*(C'), respectively.
If the modeline couldn't be parsed the hashref will be empty. On success the following keys will be available in the hashref:
'modes', an arrayref of normalised modes;
'args', an arrayref of applicable arguments to the modes;
Example:
my $hashref = parse_mode_line( 'ov+b-i', 'Bob', 'sue', 'stalin*!*@*' );
# $hashref will be: { modes => [ '+o', '+v', '+b', '-i' ], args => [ 'Bob', 'sue', 'stalin*!*@*' ], } Takes one parameter, a string representing an \s-1IRC\s0 mask. Returns a normalised full mask.
Example:
$fullbanmask = normalize_mask( 'stalin*' );
# $fullbanmask will be: 'stalin*!*@*'; Takes two parameters, a string representing an \s-1IRC\s0 mask and something to match against the \s-1IRC\s0 mask, such as a nick!user@hostname string. Returns a true value if they match, a false value otherwise. Optionally, one may pass the casemapping (see \*(C`uc_irc\*(C'), as this function uses \*(C`uc_irc\*(C' internally. Takes two array references, the first being a list of strings representing \s-1IRC\s0 masks, the second a list of somethings to test against the masks. Returns an empty hashref if there are no matches. Otherwise, the keys will be the masks matched, each value being an arrayref of the strings that matched it. Optionally, one may pass the casemapping (see \*(C`uc_irc\*(C'), as this function uses \*(C`uc_irc\*(C' internally. Takes one argument, a string representing a number of mode changes. Returns a condensed version of the changes.
my $mode_line = unparse_mode_line('+o+o+o-v+v'); $mode_line is now '+ooo-v+v' Takes two arguments, strings representing a set of \s-1IRC\s0 user modes before and after a change. Returns a string representing what changed.
my $mode_change = gen_mode_change('abcde', 'befmZ'); $mode_change is now '-acd+fmZ' Takes one parameter, a string representing a user in the form nick!user@hostname. In a scalar context it returns just the nickname. In a list context it returns a list consisting of the nick, user and hostname, respectively. Takes one argument, a channel name to validate. Returns true or false if the channel name is valid or not. You can supply a second argument, an array of characters of allowed channel prefixes. Defaults to \*(C`['#', '&']\*(C'. Takes one argument, a nickname to validate. Returns true or false if the nickname is valid or not. Takes an \s-1IRC\s0 server numerical reply code (e.g. '001') as an argument, and returns the corresponding name (e.g. '\s-1RPL_WELCOME\s0'). Takes an \s-1IRC\s0 server reply name (e.g. '\s-1RPL_WELCOME\s0') as an argument, and returns the corresponding numerical code (e.g. '001'). Takes one parameter, a string of \s-1IRC\s0 text. Returns true if it contains any \s-1IRC\s0 color codes, false otherwise. Useful if you want your bot to kick users for (ab)using colors. :) Takes one parameter, a string of \s-1IRC\s0 text. Returns true if it contains any \s-1IRC\s0 formatting codes, false otherwise. Takes one parameter, a string of \s-1IRC\s0 text. Returns the string stripped of all \s-1IRC\s0 color codes. Takes one parameter, a string of \s-1IRC\s0 text. Returns the string stripped of all \s-1IRC\s0 formatting codes. This function takes a byte string (i.e. an unmodified \s-1IRC\s0 message) and returns a text string. Since the source encoding might have been \s-1UTF-8\s0, you should store it with \s-1UTF-8\s0 or some other Unicode encoding in your file/database/whatever to be safe. For a more detailed discussion, see \*(L"\s-1ENCODING\s0\*(R".
use IRC::Utils qw(decode_irc);
sub message_handler { my ($nick, $channel, $message) = @_;
# not wise, $message is a byte string of unknown encoding print $message, "\n";
$message = decode_irc($what);
# good, $message is a text string print $message, "\n"; }
Use the following constants to add formatting and mIRC color codes to \s-1IRC\s0 messages.
Normal text:
NORMAL
Formatting:
BOLD UNDERLINE REVERSE ITALIC FIXED
Colors:
WHITE BLACK BLUE GREEN RED BROWN PURPLE ORANGE YELLOW LIGHT_GREEN TEAL LIGHT_CYAN LIGHT_BLUE PINK GREY LIGHT_GREY
Individual non-color formatting codes can be cancelled with their corresponding constant, but you can also cancel all of them at once with \*(C`NORMAL\*(C'. To cancel the effect of color codes, you must use \*(C`NORMAL\*(C'. which of course has the side effect of cancelling all other formatting codes as well.
$msg = 'This word is '.YELLOW.'yellow'.NORMAL.' while this word is'.BOLD.'bold'.BOLD; $msg = UNDERLINE.BOLD.'This sentence is both underlined and bold.'.NORMAL;
The only encoding requirement the \s-1IRC\s0 protocol places on its messages is that they be 8-bits and ASCII-compatible. This has resulted in most of the Western world settling on ASCII-compatible Latin-1 (usually Microsoft's \s-1CP1252\s0, a Latin-1 variant) as a convention. Recently, popular \s-1IRC\s0 clients (mIRC, xchat, certain irssi configurations) have begun sending a mixture of \s-1CP1252\s0 and \s-1UTF-8\s0 over the wire to allow more characters without breaking backward compatibility (too much). They send \s-1CP1252\s0 encoded messages if the characters fit within that encoding, otherwise falling back to \s-1UTF-8\s0, and likewise autodetecting the encoding (\s-1UTF-8\s0 or \s-1CP1252\s0) of incoming messages. Since writing text with mixed encoding to a file, terminal, or database is not a good idea, you need a way to decode messages from \s-1IRC\s0. \*(C`decode_irc\*(C' will do that.
The matter is complicated further by the fact that some servers allow non-ASCII characters in channel names. \s-1IRC\s0 modules generally don't explicitly encode or decode any \s-1IRC\s0 traffic, but they do have to concatenate parts of a message (e.g. a channel name and a message) before sending it over the wire. So when you do something like \*(C`privmsg($channel, '\*(ae\*(d-i')\*(C', where $channel is the unmodified channel name (a byte string) you got from an earlier \s-1IRC\s0 message, the channel name will get double-encoded when concatenated with your message (a non-ASCII text string) if the channel name contains non-ASCII bytes.
To prevent this, you can't simply decode the channel name and then use it. '#\*(ae\*(d-i' in \s-1CP1252\s0 is not the same channel as '#\*(ae\*(d-i' in \s-1UTF-8\s0, since they are encoded as different sequences of bytes, and the \s-1IRC\s0 server only cares about the byte representation. Therefore, when using a channel name you got from the server (e.g. when replying to message), you should use the original byte string (before it has been decoded with \*(C`decode_irc\*(C'), and encode any other parameters (with \*(C`encode_utf8\*(C') so that your message will be concatenated correctly. At some point, you'll probably want to print the channel name, write it to a log file or use it in a filename, so you'll eventually have to decode it, at which point the \s-1UTF-8\s0 \*(C`#\*(ae\*(d-i\*(C' and \s-1CP1252\s0 \*(C`#\*(ae\*(d-i\*(C' will have to be considered equivalent.
use Encode qw(encode_utf8 encode);
sub message_handler { # these three are all byte strings my ($nick, $channel, $message) = @_;
# bad: if $channel has any non-ASCII bytes, they will get double-encoded privmsg($channel, '\*(ae\*(d-i');
# bad: if $message has any non-ASCII bytes, they will get double-encoded privmsg('#\*(ae\*(d-i', $message);
# good: both are byte strings already, so they will concatenate correctly privmsg($channel, $message);
# good: both are text strings (Latin1 as per Perl's default), so # they'll be concatenated correctly privmsg('#\*(ae\*(d-i', '\*(ae\*(d-i');
# good: similar to the last one, except now they're using UTF-8, which # means that the channel is actually not the same as above use utf8; privmsg('#\*(ae\*(d-i', '\*(ae\*(d-i');
# good: $channel and $msg_bytes are both byte strings my $msg_bytes = encode_utf8('\*(ae\*(d-i'); privmsg($channel, $msg_bytes);
# good: $chan_bytes and $message are both byte strings # here we're sending a message to the utf8-encoded #\*(ae\*(d-i my $utf8_bytes = encode_utf8('#\*(ae\*(d-i'); privmsg($utf8_bytes, $message);
# good: $chan_bytes and $message are both byte strings # here we're sending a message to the cp1252-encoded #\*(ae\*(d-i my $cp1252_bytes = encode('cp1252', '#\*(ae\*(d-i'); privmsg($cp1252_bytes, $message);
# bad: $channel is in an undetermined encoding log_message("Got message from $channel");
# good: using the decoded version of $channel log_message("Got message from ".decode_irc($channel)); }
See also Encode, perluniintro, perlunitut, perlunicode, and perlunifaq.
Hinrik O\*:rn Sigur\*(d-sson <[email protected]> (\*(C`Hinrik\*(C' irc.perl.org, or \*(C`literal\*(C' @ FreeNode).
Chris \*(C`BinGOs\*(C' Williams <[email protected]>
POE::Component::IRC
POE::Component::Server::IRC