Common utilities that help with irc protocol handling
use AnyEvent::IRC::Util qw/parse_irc_msg mk_msg/; my $msgdata = mk_msg (undef, PRIVMSG => "mcmanus", "my hands glow!");
These are some utility functions that might come in handy when handling the \s-1IRC\s0 protocol.
You can export these with eg.:
use AnyEvent::IRC::Util qw/parse_irc_msg/;
This method parses the $ircline, which is one line of the \s-1IRC\s0 protocol without the trailing \*(L"\015\012\*(R". It returns a hash which has the following entrys:
The message prefix.
The \s-1IRC\s0 command.
The parameters to the \s-1IRC\s0 command in a array reference, this includes the trailing parameter (the one after the ':' or the 14th parameter).
This function assembles a \s-1IRC\s0 message. The generated message will look like (pseudo code!) :<prefix> <command> <params> :<trail> Please refer to \s-1RFC\s0 1459 how \s-1IRC\s0 messages normally look like. The prefix will be omitted if they are \*(C`undef\*(C'. Please note that only the last parameter may contain spaces, and if it contains spaces it will be quoted as the trailing part of the \s-1IRC\s0 message. \s-1NOTE:\s0 The trailing \*(L"\015\012\*(R" is \s-1NOT\s0 added by this function! \s-1EXAMPLES:\s0 mk_msg (undef, "PRIVMSG", "magnus", "you suck!"); # will return: "PRIVMSG magnus :you suck!"
mk_msg (undef, "PRIVMSG", "magnus", "Hi!"); # will return: "PRIVMSG magnus :Hi!"
mk_msg (undef, "JOIN", "#test"); # will return: "JOIN #test"
This function decodes \s-1CTCP\s0 messages contained in an \s-1IRC\s0 message. $data should be the last parameter of a \s-1IRC\s0 \s-1PRIVMSG\s0 or \s-1NOTICE\s0. It will first unescape the lower layer, extract \s-1CTCP\s0 messages and then return a list with two elements: the line without the \s-1CTCP\s0 messages and an array reference which contains array references of \s-1CTCP\s0 messages. Those \s-1CTCP\s0 message array references will have the \s-1CTCP\s0 message tag as first element (eg. \*(L"\s-1VERSION\s0\*(R") and the rest of the \s-1CTCP\s0 message as the second element.
This function encodes a \s-1CTCP\s0 message for the transmission via the \s-1NOTICE\s0 or \s-1PRIVMSG\s0 command. @msg is an array of strings or array references. If an array reference occurs in the @msg array it's first element will be interpreted as \s-1CTCP\s0 \s-1TAG\s0 (eg. one of \s-1PING\s0, \s-1VERSION\s0, .. whatever) the rest of the array ref will be appended to the tag and separated by spaces. All parts of the message will be concatenated and lowlevel quoted. That means you can embed _any_ character from 0 to 255 in this message (thats what the lowlevel quoting allows).
This function will filter out any mIRC colors and (most) ansi escape sequences. Unfortunately the mIRC color coding will destroy improper colored numbers. So this function may destroy the message in some occasions a bit.
This function splits an \s-1IRC\s0 user prefix as described by \s-1RFC\s0 2817 into the three parts: nickname, user and host. Which will be returned as a list with that order. $prefix can also be a hash like it is returned by \*(C`parse_irc_msg\*(C'.
Returns true if the prefix is a nick prefix, containing user and host. Joins $nick, $user and $host together to form a prefix.
A shortcut to extract the nickname from the $prefix. $prefix can also be a hash like it is returned by \*(C`parse_irc_msg\*(C'.
A shortcut to extract the username from the $prefix. $prefix can also be a hash like it is returned by \*(C`parse_irc_msg\*(C'.
A shortcut to extract the hostname from the $prefix. $prefix can also be a hash like it is returned by \*(C`parse_irc_msg\*(C'.
This function is a interface to the internal mapping or numeric replies to the reply name in \s-1RFC\s0 2812 (which you may also consult). $code is returned if no name for $code exists (as some server may extended the protocol). This function splits up $string into multiple @lines which are not longer than $maxlinebytes bytes. Encoding can be given in $encoding. (eg. 'utf-8'). But the output will not be encoded. This function takes care that your characters are not garbled.
Robin Redeker, \*(C`<[email protected]>\*(C'
Internet Relay Chat Client To Client Protocol from February 2, 1997 http://www.invlogic.com/irc/ctcp.html
\s-1RFC\s0 1459 - Internet Relay Chat: Client Protocol
Copyright 2006-2009 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.