ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-IRC3/lib/Net/IRC3/Util.pm
(Generate patch)

Comparing Net-IRC3/lib/Net/IRC3/Util.pm (file contents):
Revision 1.11 by elmex, Tue Mar 6 21:44:53 2007 UTC vs.
Revision 1.12 by elmex, Wed Apr 18 21:10:21 2007 UTC

3no warnings; 3no warnings;
4use Exporter; 4use Exporter;
5our @ISA = qw/Exporter/; 5our @ISA = qw/Exporter/;
6our @EXPORT_OK = 6our @EXPORT_OK =
7 qw(mk_msg parse_irc_msg split_prefix prefix_nick 7 qw(mk_msg parse_irc_msg split_prefix prefix_nick
8 decode_ctcp filter_ctcp_text_attr prefix_user prefix_host 8 decode_ctcp encode_ctcp filter_ctcp_text_attr prefix_user prefix_host
9 rfc_code_to_name); 9 rfc_code_to_name);
10 10
11=head1 NAME 11=head1 NAME
12 12
13Net::IRC3::Util - Common utilities that help with IRC protocol handling 13Net::IRC3::Util - Common utilities that help with IRC protocol handling
143 143
144TODO 144TODO
145 145
146=cut 146=cut
147 147
148my @_ctcp_lowlevel_escape = ("\000", "0", "\012", "n", "\015", "r", "\020", "\020");
149
150sub unescape_lowlevel {
151 my ($data) = @_;
152 my %map = reverse @_ctcp_lowlevel_escape;
153 $data =~ s/\020(.)/defined $map{$1} ? $map{$1} : $1/ge;
154 $data
155}
156
157sub escape_lowlevel {
158 my ($data) = @_;
159 my %map = @_ctcp_lowlevel_escape;
160 $data =~ s/([\000\012\015\020])/"\020$map{$1}"/ge;
161 $data
162}
163
164sub unescape_ctcp {
165 my ($data) = @_;
166 $data =~ s/\\(.)/$1 eq 'a' ? "\001" : ($1 eq "\\" ? "\\" : $1)/eg;
167 $data
168}
169
170sub escape_ctcp {
171 my ($data) = @_;
172 $data =~ s/([\\\001])/$1 eq "\001" ? "\\a" : "\\\\"/eg;
173 $data
174}
175
176=item B<decode_ctcp ($trailing)>
177
178This function decodes the C<$trailing> part of an IRC message.
179It will first unescape the lower layer, extract CTCP messages
180and then return a list with two elements: the line without the ctcp messages
181and an array reference which contains array references of CTCP messages.
182Those CTCP message array references will have the CTCP message tag as
183first element (eg. "VERSION") and the rest of the CTCP message as the second
184element.
185
186=cut
187
148sub decode_ctcp { 188sub decode_ctcp {
149 my ($line) = @_; 189 my ($line) = @_;
150 190
191 $line = unescape_lowlevel ($line);
192 my @ctcp;
151 while ($line =~ /\G\001([^\001]*)\001/g) { 193 while ($line =~ /\G\001([^\001]*)\001/g) {
152 my $req = $1; 194 my $msg = unescape_ctcp ($1);
195 my ($tag, $data) = split / /, $msg, 2;
196 push @ctcp, [$tag, $data];
153 } 197 }
154 198
155 $line =~ s/\001[^\001]*\001//g; 199 $line =~ s/\001[^\001]*\001//g;
156 200
157 return $line; 201 return ($line, \@ctcp)
202}
203
204=item B<encode_ctcp (@msg)>
205
206This function encodes a ctcp message for the trailing part of a NOTICE
207or PRIVMSG. C<@msg> is an array of strings or array references.
208If an array reference occurs in the C<@msg> array it's first
209element will be interpreted as CTCP TAG (eg. one of PING, VERSION, .. whatever)
210the rest of the array ref will be appended to the tag and seperated by
211spaces.
212
213All parts of the message will be contatenated and lowlevel quoted.
214That means you can embed _any_ character from 0 to 255 in this message (thats
215what the lowlevel quoting allows).
216
217=cut
218
219sub encode_ctcp {
220 my (@args) = @_;
221 escape_lowlevel (
222 join "", map {
223 ref $_
224 ? "\001" . escape_ctcp (join " ", @$_) . "\001"
225 : $_
226 } @args
227 )
158} 228}
159 229
160=item B<filter_ctcp_text_attr ($line, $cb)> 230=item B<filter_ctcp_text_attr ($line, $cb)>
161 231
162TODO 232TODO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines