ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-IRC3/lib/Net/IRC3/Util.pm
Revision: 1.8
Committed: Fri Feb 23 20:14:58 2007 UTC (17 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.7: +11 -3 lines
Log Message:
added some tests and fixed quite some bugs and added missing
documentation.

File Contents

# Content
1 package Net::IRC3::Util;
2 use strict;
3 use Exporter;
4 our @ISA = qw/Exporter/;
5 our @EXPORT_OK =
6 qw(mk_msg parse_irc_msg split_prefix prefix_nick
7 decode_ctcp filter_ctcp_text_attr prefix_user prefix_host
8 rfc_code_to_name);
9
10 =head1 NAME
11
12 Net::IRC3::Util - Common utilities that help with IRC protocol handling
13
14 =head1 SYNOPSIS
15
16 use Net::IRC3 qw/parse_irc_msg mk_msg/;
17
18 my $msgdata = mk_msg (undef, PRIVMSG => "my hands glow!", "mcmanus");
19
20 =head1 FUNCTIONS
21
22 These are some utility functions that might come in handy when
23 handling the IRC protocol.
24
25 You can export these with eg.:
26
27 use Net::IRC3 qw/parse_irc_msg/;
28
29 =over 4
30
31 =item B<parse_irc_msg ($ircline)>
32
33 This method parses the C<$ircline>, which is one line of the IRC protocol
34 without the trailing "\015\012".
35
36 It returns a hash which has the following entrys:
37
38 =over 4
39
40 =item prefix
41
42 The message prefix.
43
44 =item command
45
46 The IRC command.
47
48 =item params
49
50 The parameters to the IRC command in a array reference,
51 this includes the trailing parameter (the one after the ':' or
52 the 14th parameter).
53
54 =item trailing
55
56 This is set if there was a trailing parameter (the one after the ':' or
57 the 14th parameter).
58
59 =back
60
61 =cut
62
63 sub parse_irc_msg {
64 my ($msg) = @_;
65
66 my $cmd;
67 my $pref;
68 my $t;
69 my @a;
70
71 my $p = $msg =~ s/^(:([^ ]+)[ ])?([A-Za-z]+|\d{3})//;
72 $pref = $2;
73 $cmd = $3;
74
75 my $i = 0;
76
77 while ($msg =~ s/^[ ]([^ :\015\012\0][^ \015\012\0]*)//) {
78
79 push @a, $1 if defined $1;
80 if (++$i > 13) { last; }
81 }
82
83 if ($i == 14) {
84
85 if ($msg =~ s/^[ ]:?([^\015\012\0]*)//) {
86 $t = $1 if $1 ne "";
87 }
88
89 } else {
90
91 if ($msg =~ s/^[ ]:([^\015\012\0]*)//) {
92 $t = $1 if $1 ne "";
93 }
94 }
95
96 push @a, $t if defined $t;
97
98 my $m = { prefix => $pref, command => $cmd, params => \@a, trailing => $t };
99 return $p ? $m : undef;
100 }
101
102 =item B<mk_msg ($prefix, $command, $trailing, @params)>
103
104 This function assembles a IRC message. The generated
105 message will look like (pseudo code!)
106
107 :<prefix> <command> <params> :<trail>
108
109 Please refer to RFC 2812 how IRC messages normally look like.
110
111 The prefix and the trailing string will be omitted if they are C<undef>.
112
113 EXAMPLES:
114
115 mk_msg (undef, "PRIVMSG", "you suck!", "magnus");
116 # will return: "PRIVMSG magnus :you suck!\015\012"
117
118 mk_msg (undef, "JOIN", undef, "#test");
119 # will return: "JOIN #test\015\012"
120
121 =cut
122
123 sub mk_msg {
124 my ($prefix, $command, $trail, @params) = @_;
125 my $msg = "";
126
127 $msg .= defined $prefix ? ":$prefix " : "";
128 $msg .= "$command";
129
130 # FIXME: params must be counted, and if > 13 they have to be
131 # concationated with $trail
132 map { $msg .= " $_" } @params;
133
134 $msg .= defined $trail ? " :$trail" : "";
135 $msg .= "\015\012";
136
137 return $msg;
138 }
139
140
141 =item B<decode_ctcp ($line)>
142
143 TODO
144
145 =cut
146
147 sub decode_ctcp {
148 my ($line) = @_;
149
150 while ($line =~ /\G\001([^\001]*)\001/g) {
151 my $req = $1;
152 }
153
154 $line =~ s/\001[^\001]*\001//g;
155
156 return $line;
157 }
158
159 =item B<filter_ctcp_text_attr ($line, $cb)>
160
161 TODO
162
163 =cut
164 # implemented after the below CTCP spec, but
165 # doesnt seem to be used by anyone... so it's untested.
166 sub filter_ctcp_text_attr {
167 my ($line, $cb) = @_;
168 $cb ||= sub { '' };
169 $line =~ s/\006([BVUSI])/{warn "FIL\n"; my $c = $cb->($1); defined $c ? $c : "\006$1"}/ieg;
170 $line =~ s/\006CA((?:I[0-9A-F]|#[0-9A-F]{3}){2})/{my $c = $cb->($1); defined $c ? $c : "\006CA$1"}/ieg;
171 $line =~ s/\006C([FB])(I[0-9A-F]|#[0-9A-F]{3})/{my $c = $cb->($1, $2); defined $c ? $c : "\006C$1$2"}/ieg;
172 $line =~ s/\006CX([AFB])/{my $c = $cb->($1); defined $c ? $c : "\006CX$1"}/ieg;
173 return $line;
174 }
175
176 =item B<split_prefix ($prefix)>
177
178 This function splits an IRC user prefix as described by RFC 2817
179 into the three parts: nickname, user and host. Which will be
180 returned as a list with that order.
181
182 C<$prefix> can also be a hash like it is returned by C<parse_irc_msg>.
183
184 =cut
185
186 sub split_prefix {
187 my ($prfx) = @_;
188
189 if (ref ($prfx) eq 'HASH') {
190 $prfx = $prfx->{prefix};
191 }
192
193 $prfx =~ m/^\s*([^!]*)!([^@]*)@(.*?)\s*$/;
194 return ($1, $2, $3);
195 }
196
197 =item B<prefix_nick ($prefix)>
198
199 A shortcut to extract the nickname from the C<$prefix>.
200
201 C<$prefix> can also be a hash like it is returned by C<parse_irc_msg>.
202
203 =cut
204
205 sub prefix_nick {
206 my ($prfx) = @_;
207 return (split_prefix ($prfx))[0];
208 }
209
210 =item B<prefix_user ($prefix)>
211
212 A shortcut to extract the username from the C<$prefix>.
213
214 C<$prefix> can also be a hash like it is returned by C<parse_irc_msg>.
215
216 =cut
217
218 sub prefix_user {
219 my ($prfx) = @_;
220 return (split_prefix ($prfx))[1];
221 }
222
223 =item B<prefix_host ($prefix)>
224
225 A shortcut to extract the hostname from the C<$prefix>.
226
227 C<$prefix> can also be a hash like it is returned by C<parse_irc_msg>.
228
229 =cut
230
231 sub prefix_host {
232 my ($prfx) = @_;
233 return (split_prefix ($prfx))[2];
234 }
235
236
237 =item B<rfc_code_to_name ($code)>
238
239 This function is a interface to the internal mapping or numeric
240 replies to the reply name in RFC 2812 (which you may also consult).
241
242 C<$code> is returned if no name for C<$code> exists
243 (as some server may extended the protocol).
244
245 =back
246
247 =cut
248
249 our %RFC_NUMCODE_MAP = (
250 '001' => 'RPL_WELCOME',
251 '002' => 'RPL_YOURHOST',
252 '003' => 'RPL_CREATED',
253 '004' => 'RPL_MYINFO',
254 '005' => 'RPL_BOUNCE',
255 '200' => 'RPL_TRACELINK',
256 '201' => 'RPL_TRACECONNECTING',
257 '202' => 'RPL_TRACEHANDSHAKE',
258 '203' => 'RPL_TRACEUNKNOWN',
259 '204' => 'RPL_TRACEOPERATOR',
260 '205' => 'RPL_TRACEUSER',
261 '206' => 'RPL_TRACESERVER',
262 '207' => 'RPL_TRACESERVICE',
263 '208' => 'RPL_TRACENEWTYPE',
264 '209' => 'RPL_TRACECLASS',
265 '210' => 'RPL_TRACERECONNECT',
266 '211' => 'RPL_STATSLINKINFO',
267 '212' => 'RPL_STATSCOMMANDS',
268 '219' => 'RPL_ENDOFSTATS',
269 '221' => 'RPL_UMODEIS',
270 '233' => 'RPL_SERVICE',
271 '234' => 'RPL_SERVLIST',
272 '235' => 'RPL_SERVLISTEND',
273 '242' => 'RPL_STATSUPTIME',
274 '243' => 'RPL_STATSOLINE',
275 '250' => 'RPL_STATSDLINE',
276 '251' => 'RPL_LUSERCLIENT',
277 '252' => 'RPL_LUSEROP',
278 '253' => 'RPL_LUSERUNKNOWN',
279 '254' => 'RPL_LUSERCHANNELS',
280 '255' => 'RPL_LUSERME',
281 '256' => 'RPL_ADMINME',
282 '257' => 'RPL_ADMINLOC1',
283 '258' => 'RPL_ADMINLOC2',
284 '259' => 'RPL_ADMINEMAIL',
285 '261' => 'RPL_TRACELOG',
286 '262' => 'RPL_TRACEEND',
287 '263' => 'RPL_TRYAGAIN',
288 '301' => 'RPL_AWAY',
289 '302' => 'RPL_USERHOST',
290 '303' => 'RPL_ISON',
291 '305' => 'RPL_UNAWAY',
292 '306' => 'RPL_NOWAWAY',
293 '311' => 'RPL_WHOISUSER',
294 '312' => 'RPL_WHOISSERVER',
295 '313' => 'RPL_WHOISOPERATOR',
296 '314' => 'RPL_WHOWASUSER',
297 '315' => 'RPL_ENDOFWHO',
298 '317' => 'RPL_WHOISIDLE',
299 '318' => 'RPL_ENDOFWHOIS',
300 '319' => 'RPL_WHOISCHANNELS',
301 '321' => 'RPL_LISTSTART',
302 '322' => 'RPL_LIST',
303 '323' => 'RPL_LISTEND',
304 '324' => 'RPL_CHANNELMODEIS',
305 '325' => 'RPL_UNIQOPIS',
306 '331' => 'RPL_NOTOPIC',
307 '332' => 'RPL_TOPIC',
308 '341' => 'RPL_INVITING',
309 '342' => 'RPL_SUMMONING',
310 '346' => 'RPL_INVITELIST',
311 '347' => 'RPL_ENDOFINVITELIST',
312 '348' => 'RPL_EXCEPTLIST',
313 '349' => 'RPL_ENDOFEXCEPTLIST',
314 '351' => 'RPL_VERSION',
315 '352' => 'RPL_WHOREPLY',
316 '353' => 'RPL_NAMREPLY',
317 '364' => 'RPL_LINKS',
318 '365' => 'RPL_ENDOFLINKS',
319 '366' => 'RPL_ENDOFNAMES',
320 '367' => 'RPL_BANLIST',
321 '368' => 'RPL_ENDOFBANLIST',
322 '369' => 'RPL_ENDOFWHOWAS',
323 '371' => 'RPL_INFO',
324 '372' => 'RPL_MOTD',
325 '374' => 'RPL_ENDOFINFO',
326 '375' => 'RPL_MOTDSTART',
327 '376' => 'RPL_ENDOFMOTD',
328 '381' => 'RPL_YOUREOPER',
329 '382' => 'RPL_REHASHING',
330 '383' => 'RPL_YOURESERVICE',
331 '384' => 'RPL_MYPORTIS',
332 '391' => 'RPL_TIME',
333 '392' => 'RPL_USERSSTART',
334 '393' => 'RPL_USERS',
335 '394' => 'RPL_ENDOFUSERS',
336 '395' => 'RPL_NOUSERS',
337 '401' => 'ERR_NOSUCHNICK',
338 '402' => 'ERR_NOSUCHSERVER',
339 '403' => 'ERR_NOSUCHCHANNEL',
340 '404' => 'ERR_CANNOTSENDTOCHAN',
341 '405' => 'ERR_TOOMANYCHANNELS',
342 '406' => 'ERR_WASNOSUCHNICK',
343 '407' => 'ERR_TOOMANYTARGETS',
344 '408' => 'ERR_NOSUCHSERVICE',
345 '409' => 'ERR_NOORIGIN',
346 '411' => 'ERR_NORECIPIENT',
347 '412' => 'ERR_NOTEXTTOSEND',
348 '413' => 'ERR_NOTOPLEVEL',
349 '414' => 'ERR_WILDTOPLEVEL',
350 '415' => 'ERR_BADMASK',
351 '421' => 'ERR_UNKNOWNCOMMAND',
352 '422' => 'ERR_NOMOTD',
353 '423' => 'ERR_NOADMININFO',
354 '424' => 'ERR_FILEERROR',
355 '431' => 'ERR_NONICKNAMEGIVEN',
356 '432' => 'ERR_ERRONEUSNICKNAME',
357 '433' => 'ERR_NICKNAMEINUSE',
358 '436' => 'ERR_NICKCOLLISION',
359 '437' => 'ERR_UNAVAILRESOURCE',
360 '441' => 'ERR_USERNOTINCHANNEL',
361 '442' => 'ERR_NOTONCHANNEL',
362 '443' => 'ERR_USERONCHANNEL',
363 '444' => 'ERR_NOLOGIN',
364 '445' => 'ERR_SUMMONDISABLED',
365 '446' => 'ERR_USERSDISABLED',
366 '451' => 'ERR_NOTREGISTERED',
367 '461' => 'ERR_NEEDMOREPARAMS',
368 '462' => 'ERR_ALREADYREGISTRED',
369 '463' => 'ERR_NOPERMFORHOST',
370 '464' => 'ERR_PASSWDMISMATCH',
371 '465' => 'ERR_YOUREBANNEDCREEP',
372 '466' => 'ERR_YOUWILLBEBANNED',
373 '467' => 'ERR_KEYSET',
374 '471' => 'ERR_CHANNELISFULL',
375 '472' => 'ERR_UNKNOWNMODE',
376 '473' => 'ERR_INVITEONLYCHAN',
377 '474' => 'ERR_BANNEDFROMCHAN',
378 '475' => 'ERR_BADCHANNELKEY',
379 '476' => 'ERR_BADCHANMASK',
380 '477' => 'ERR_NOCHANMODES',
381 '478' => 'ERR_BANLISTFULL',
382 '481' => 'ERR_NOPRIVILEGES',
383 '482' => 'ERR_CHANOPRIVSNEEDED',
384 '483' => 'ERR_CANTKILLSERVER',
385 '484' => 'ERR_RESTRICTED',
386 '485' => 'ERR_UNIQOPPRIVSNEEDED',
387 '491' => 'ERR_NOOPERHOST',
388 '492' => 'ERR_NOSERVICEHOST',
389 '501' => 'ERR_UMODEUNKNOWNFLAG',
390 '502' => 'ERR_USERSDONTMATCH',
391 );
392
393 sub rfc_code_to_name {
394 my ($code) = @_;
395 return $RFC_NUMCODE_MAP{$code} || $code;
396 }
397
398 =head1 AUTHOR
399
400 Robin Redeker, C<< <elmex@ta-sa.org> >>
401
402 =head1 SEE ALSO
403
404 Internet Relay Chat Client To Client Protocol from February 2, 1997
405 http://www.invlogic.com/irc/ctcp.html
406
407 RFC 2812 - Internet Relay Chat: Client Protocol
408
409 =head1 COPYRIGHT & LICENSE
410
411 Copyright 2006 Robin Redeker, all rights reserved.
412
413 This program is free software; you can redistribute it and/or modify it
414 under the same terms as Perl itself.
415
416 =cut
417
418 1;