| 1 |
NAME |
| 2 |
IRC::Utils - Common utilities for IRC-related tasks |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
|
| 8 |
use IRC::Utils ':ALL'; |
| 9 |
|
| 10 |
my $nickname = '^Lame|BOT[moo]'; |
| 11 |
my $uppercase_nick = uc_irc($nickname); |
| 12 |
my $lowercase_nick = lc_irc($nickname); |
| 13 |
|
| 14 |
print "They're equivalent\n" if eq_irc($uppercase_nick, $lowercase_nick); |
| 15 |
|
| 16 |
my $mode_line = 'ov+b-i Bob sue stalin*!*@*'; |
| 17 |
my $hashref = parse_mode_line($mode_line); |
| 18 |
|
| 19 |
my $banmask = 'stalin*'; |
| 20 |
my $full_banmask = normalize_mask($banmask); |
| 21 |
|
| 22 |
if (matches_mask($full_banmask, '[email protected]')) { |
| 23 |
print "EEK!"; |
| 24 |
} |
| 25 |
|
| 26 |
my $decoded = irc_decode($raw_irc_message); |
| 27 |
print $decoded, "\n"; |
| 28 |
|
| 29 |
if (has_color($message)) { |
| 30 |
print 'COLOR CODE ALERT!\n"; |
| 31 |
} |
| 32 |
|
| 33 |
my $results_hashref = matches_mask_array(\@masks, \@items_to_match_against); |
| 34 |
|
| 35 |
my $nick = parse_user('[email protected]'); |
| 36 |
my ($nick, $user, $host) = parse_user('[email protected]'); |
| 37 |
|
| 38 |
DESCRIPTION |
| 39 |
The functions in this module take care of many of the tasks you are |
| 40 |
faced with when working with IRC. Mode lines, ban masks, message |
| 41 |
encoding and formatting, etc. |
| 42 |
|
| 43 |
FUNCTIONS |
| 44 |
"uc_irc" |
| 45 |
Takes one mandatory parameter, a string to convert to IRC uppercase, and |
| 46 |
one optional parameter, the casemapping of the ircd (which can be |
| 47 |
'rfc1459', 'strict-rfc1459' or 'ascii'. Default is 'rfc1459'). Returns |
| 48 |
the IRC uppercase equivalent of the passed string. |
| 49 |
|
| 50 |
"lc_irc" |
| 51 |
Takes one mandatory parameter, a string to convert to IRC lowercase, and |
| 52 |
one optional parameter, the casemapping of the ircd (which can be |
| 53 |
'rfc1459', 'strict-rfc1459' or 'ascii'. Default is 'rfc1459'). Returns |
| 54 |
the IRC lowercase equivalent of the passed string. |
| 55 |
|
| 56 |
"eq_irc" |
| 57 |
Takes two mandatory parameters, IRC strings (channels or nicknames) to |
| 58 |
compare. A third, optional parameter specifies the casemapping. Returns |
| 59 |
true if the two strings are equivalent, false otherwise |
| 60 |
|
| 61 |
# long version |
| 62 |
lc_irc($one, $map) eq lc_irc($two, $map) |
| 63 |
|
| 64 |
# short version |
| 65 |
eq_irc($one, $two, $map) |
| 66 |
|
| 67 |
"parse_mode_line" |
| 68 |
Takes a list representing an IRC mode line. Returns a hashref. |
| 69 |
Optionally you can also supply an arrayref and a hashref to specify |
| 70 |
valid channel modes (default: "[qw(beI k l imnpstaqr)]") and status |
| 71 |
modes (default: "{o => '@', h => '%', v => '+'}"), respectively. |
| 72 |
|
| 73 |
If the modeline couldn't be parsed the hashref will be empty. On success |
| 74 |
the following keys will be available in the hashref: |
| 75 |
|
| 76 |
'modes', an arrayref of normalised modes; |
| 77 |
|
| 78 |
'args', an arrayref of applicable arguments to the modes; |
| 79 |
|
| 80 |
Example: |
| 81 |
|
| 82 |
my $hashref = parse_mode_line( 'ov+b-i', 'Bob', 'sue', 'stalin*!*@*' ); |
| 83 |
|
| 84 |
# $hashref will be: |
| 85 |
{ |
| 86 |
modes => [ '+o', '+v', '+b', '-i' ], |
| 87 |
args => [ 'Bob', 'sue', 'stalin*!*@*' ], |
| 88 |
} |
| 89 |
|
| 90 |
"normalize_mask" |
| 91 |
Takes one parameter, a string representing an IRC mask. Returns a |
| 92 |
normalised full mask. |
| 93 |
|
| 94 |
Example: |
| 95 |
|
| 96 |
$fullbanmask = normalize_mask( 'stalin*' ); |
| 97 |
|
| 98 |
# $fullbanmask will be: 'stalin*!*@*'; |
| 99 |
|
| 100 |
"matches_mask" |
| 101 |
Takes two parameters, a string representing an IRC mask and something to |
| 102 |
match against the IRC mask, such as a nick!user@hostname string. Returns |
| 103 |
a true value if they match, a false value otherwise. Optionally, one may |
| 104 |
pass the casemapping (see "uc_irc"), as this function uses "uc_irc" |
| 105 |
internally. |
| 106 |
|
| 107 |
"matches_mask_array" |
| 108 |
Takes two array references, the first being a list of strings |
| 109 |
representing IRC masks, the second a list of somethings to test against |
| 110 |
the masks. Returns an empty hashref if there are no matches. Otherwise, |
| 111 |
the keys will be the masks matched, each value being an arrayref of the |
| 112 |
strings that matched it. Optionally, one may pass the casemapping (see |
| 113 |
"uc_irc"), as this function uses "uc_irc" internally. |
| 114 |
|
| 115 |
"unparse_mode_line" |
| 116 |
Takes one argument, a string representing a number of mode changes. |
| 117 |
Returns a condensed version of the changes. |
| 118 |
|
| 119 |
my $mode_line = unparse_mode_line('+o+o+o-v+v'); |
| 120 |
$mode_line is now '+ooo-v+v' |
| 121 |
|
| 122 |
"gen_mode_change" |
| 123 |
Takes two arguments, strings representing a set of IRC user modes before |
| 124 |
and after a change. Returns a string representing what changed. |
| 125 |
|
| 126 |
my $mode_change = gen_mode_change('abcde', 'befmZ'); |
| 127 |
$mode_change is now '-acd+fmZ' |
| 128 |
|
| 129 |
"parse_user" |
| 130 |
Takes one parameter, a string representing a user in the form |
| 131 |
nick!user@hostname. In a scalar context it returns just the nickname. In |
| 132 |
a list context it returns a list consisting of the nick, user and |
| 133 |
hostname, respectively. |
| 134 |
|
| 135 |
"is_valid_chan_name" |
| 136 |
Takes one argument, a channel name to validate. Returns true or false if |
| 137 |
the channel name is valid or not. You can supply a second argument, an |
| 138 |
array of characters of allowed channel prefixes. Defaults to "['#', |
| 139 |
'&']". |
| 140 |
|
| 141 |
"is_valid_nick_name" |
| 142 |
Takes one argument, a nickname to validate. Returns true or false if the |
| 143 |
nickname is valid or not. |
| 144 |
|
| 145 |
"numeric_to_name" |
| 146 |
Takes an IRC server numerical reply code (e.g. '001') as an argument, |
| 147 |
and returns the corresponding name (e.g. 'RPL_WELCOME'). |
| 148 |
|
| 149 |
"name_to_numeric" |
| 150 |
Takes an IRC server reply name (e.g. 'RPL_WELCOME') as an argument, and |
| 151 |
returns the corresponding numerical code (e.g. '001'). |
| 152 |
|
| 153 |
"has_color" |
| 154 |
Takes one parameter, a string of IRC text. Returns true if it contains |
| 155 |
any IRC color codes, false otherwise. Useful if you want your bot to |
| 156 |
kick users for (ab)using colors. :) |
| 157 |
|
| 158 |
"has_formatting" |
| 159 |
Takes one parameter, a string of IRC text. Returns true if it contains |
| 160 |
any IRC formatting codes, false otherwise. |
| 161 |
|
| 162 |
"strip_color" |
| 163 |
Takes one parameter, a string of IRC text. Returns the string stripped |
| 164 |
of all IRC color codes. |
| 165 |
|
| 166 |
"strip_formatting" |
| 167 |
Takes one parameter, a string of IRC text. Returns the string stripped |
| 168 |
of all IRC formatting codes. |
| 169 |
|
| 170 |
"decode_irc" |
| 171 |
This function takes a byte string (i.e. an unmodified IRC message) and |
| 172 |
returns a text string. Since the source encoding might have been UTF-8, |
| 173 |
you should store it with UTF-8 or some other Unicode encoding in your |
| 174 |
file/database/whatever to be safe. For a more detailed discussion, see |
| 175 |
"ENCODING". |
| 176 |
|
| 177 |
use IRC::Utils qw(decode_irc); |
| 178 |
|
| 179 |
sub message_handler { |
| 180 |
my ($nick, $channel, $message) = @_; |
| 181 |
|
| 182 |
# not wise, $message is a byte string of unkown encoding |
| 183 |
print $message, "\n"; |
| 184 |
|
| 185 |
$message = decode_irc($what); |
| 186 |
|
| 187 |
# good, $message is a text string |
| 188 |
print $message, "\n"; |
| 189 |
} |
| 190 |
|
| 191 |
CONSTANTS |
| 192 |
Use the following constants to add formatting and mIRC color codes to |
| 193 |
IRC messages. |
| 194 |
|
| 195 |
Normal text: |
| 196 |
|
| 197 |
NORMAL |
| 198 |
|
| 199 |
Formatting: |
| 200 |
|
| 201 |
BOLD |
| 202 |
UNDERLINE |
| 203 |
REVERSE |
| 204 |
ITALIC |
| 205 |
FIXED |
| 206 |
|
| 207 |
Colors: |
| 208 |
|
| 209 |
WHITE |
| 210 |
BLACK |
| 211 |
BLUE |
| 212 |
GREEN |
| 213 |
RED |
| 214 |
BROWN |
| 215 |
PURPLE |
| 216 |
ORANGE |
| 217 |
YELLOW |
| 218 |
LIGHT_GREEN |
| 219 |
TEAL |
| 220 |
LIGHT_CYAN |
| 221 |
LIGHT_BLUE |
| 222 |
PINK |
| 223 |
GREY |
| 224 |
LIGHT_GREY |
| 225 |
|
| 226 |
Individual non-color formatting codes can be cancelled with their |
| 227 |
corresponding constant, but you can also cancel all of them at once with |
| 228 |
"NORMAL". To cancel the effect of color codes, you must use "NORMAL". |
| 229 |
which of course has the side effect of cancelling all other formatting |
| 230 |
codes as well. |
| 231 |
|
| 232 |
$msg = 'This word is '.YELLOW.'yellow'.NORMAL.' while this word is'.BOLD.'bold'.BOLD; |
| 233 |
$msg = UNDERLINE.BOLD.'This sentence is both underlined and bold.'.NORMAL; |
| 234 |
|
| 235 |
ENCODING |
| 236 |
Messages |
| 237 |
The only encoding requirement the IRC protocol places on its messages is |
| 238 |
that they be 8-bits and ASCII-compatible. This has resulted in most of |
| 239 |
the Western world settling on ASCII-compatible Latin-1 (usually |
| 240 |
Microsoft's CP1252, a Latin-1 variant) as a convention. Recently, |
| 241 |
popular IRC clients (mIRC, xchat, certain irssi configurations) have |
| 242 |
begun sending a mixture of CP1252 and UTF-8 over the wire to allow more |
| 243 |
characters without breaking backward compatibility (too much). They send |
| 244 |
CP1252 encoded messages if the characters fit within that encoding, |
| 245 |
otherwise falling back to UTF-8, and likewise autodetecting the encoding |
| 246 |
(UTF-8 or CP1252) of incoming messages. Since writing text with mixed |
| 247 |
encoding to a file, terminal, or database is not a good idea, you need a |
| 248 |
way to decode messages from IRC. "decode_irc" will do that. |
| 249 |
|
| 250 |
Channel names |
| 251 |
The matter is complicated further by the fact that some servers allow |
| 252 |
non-ASCII characters in channel names. IRC modules generally don't |
| 253 |
explicitly encode or decode any IRC traffic, but they do have to |
| 254 |
concatenate parts of a message (e.g. a channel name and a message) |
| 255 |
before sending it over the wire. So when you do something like |
| 256 |
"privmsg($channel, 'æði')", where $channel is the unmodified channel |
| 257 |
name (a byte string) you got from an earlier IRC message, the channel |
| 258 |
name will get double-encoded when concatenated with your message (a |
| 259 |
non-ASCII text string) if the channel name contains non-ASCII bytes. |
| 260 |
|
| 261 |
To prevent this, you can't simply decode the channel name and then use |
| 262 |
it. '#æði' in CP1252 is not the same channel as '#æði' in UTF-8, since |
| 263 |
they are encoded as different sequences of bytes, and the IRC server |
| 264 |
only cares about the byte representation. Therefore, when using a |
| 265 |
channel name you got from the server (e.g. when replying to message), |
| 266 |
you should use the original byte string (before it has been decoded with |
| 267 |
"decode_irc"), and encode any other parameters (with "encode_utf8") so |
| 268 |
that your message will be concatenated correctly. At some point, you'll |
| 269 |
probably want to print the channel name, write it to a log file or use |
| 270 |
it in a filename, so you'll eventually have to decode it, at which point |
| 271 |
the UTF-8 "#æði" and CP1252 "#æði" will have to be considered |
| 272 |
equivalent. |
| 273 |
|
| 274 |
use Encode qw(encode_utf8 encode); |
| 275 |
|
| 276 |
sub message_handler { |
| 277 |
# these three are all byte strings |
| 278 |
my ($nick, $channel, $message) = @_; |
| 279 |
|
| 280 |
# bad: if $channel has any non-ASCII bytes, they will get double-encoded |
| 281 |
privmsg($channel, 'æði'); |
| 282 |
|
| 283 |
# bad: if $message has any non-ASCII bytes, they will get double-encoded |
| 284 |
privmsg('#æði', $message); |
| 285 |
|
| 286 |
# good: both are byte strings already, so they will concatenate correctly |
| 287 |
privmsg($channel, $message); |
| 288 |
|
| 289 |
# good: both are text strings (Latin1 as per Perl's default), so |
| 290 |
# they'll be concatenated correctly |
| 291 |
privmsg('#æði', 'æði'); |
| 292 |
|
| 293 |
# good: similar to the last one, except now they're using UTF-8, which |
| 294 |
# means that the channel is actually not the same as above |
| 295 |
use utf8; |
| 296 |
privmsg('#æði', 'æði'); |
| 297 |
|
| 298 |
# good: $channel and $msg_bytes are both byte strings |
| 299 |
my $msg_bytes = encode_utf8('æði'); |
| 300 |
privmsg($channel, $msg_bytes); |
| 301 |
|
| 302 |
# good: $chan_bytes and $message are both byte strings |
| 303 |
# here we're sending a message to the utf8-encoded #æði |
| 304 |
my $utf8_bytes = encode_utf8('#æði'); |
| 305 |
privmsg($utf8_bytes, $message); |
| 306 |
|
| 307 |
# good: $chan_bytes and $message are both byte strings |
| 308 |
# here we're sending a message to the cp1252-encoded #æði |
| 309 |
my $cp1252_bytes = encode('cp1252', '#æði'); |
| 310 |
privmsg($cp1252_bytes, $message); |
| 311 |
|
| 312 |
# bad: $channel is in an undetermined encoding |
| 313 |
log_message("Got message from $channel"); |
| 314 |
|
| 315 |
# good: using the decoded version of $channel |
| 316 |
log_message("Got message from ".decode_irc($channel)); |
| 317 |
} |
| 318 |
|
| 319 |
See also Encode, perluniintro, perlunitut, perlunicode, and perlunifaq. |
| 320 |
|
| 321 |
AUTHOR |
| 322 |
Hinrik Örn Sigurðsson <[email protected]> ("Hinrik" irc.perl.org, or |
| 323 |
"literal" @ FreeNode). |
| 324 |
|
| 325 |
Chris "BinGOs" Williams <[email protected]> |
| 326 |
|
| 327 |
SEE ALSO |
| 328 |
POE::Component::IRC |
| 329 |
|
| 330 |
POE::Component::Server::IRC |
| 331 |
|