/usr/share/perl5/AnyEvent/XMPP/Util.pm is in libanyevent-xmpp-perl 0.55-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 | package AnyEvent::XMPP::Util;
use strict;
no warnings;
use Encode;
use Net::LibIDN qw/idn_prep_name idn_prep_resource idn_prep_node/;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns_maybe/;
use Time::Local;
require Exporter;
our @EXPORT_OK = qw/resourceprep nodeprep prep_join_jid join_jid
split_jid split_uri stringprep_jid prep_bare_jid bare_jid
is_bare_jid simxml dump_twig_xml install_default_debug_dump
cmp_jid cmp_bare_jid
node_jid domain_jid res_jid
prep_node_jid prep_domain_jid prep_res_jid
from_xmpp_datetime to_xmpp_datetime to_xmpp_time
xmpp_datetime_as_timestamp
filter_xml_chars filter_xml_attr_hash_chars
/;
our @ISA = qw/Exporter/;
=head1 NAME
AnyEvent::XMPP::Util - Utility functions for AnyEvent::XMPP
=head1 SYNOPSIS
use AnyEvent::XMPP::Util qw/split_jid/;
...
=head1 FUNCTIONS
These functions can be exported if you want:
=over 4
=item B<resourceprep ($string)>
This function applies the stringprep profile for resources to C<$string>
and returns the result.
=cut
sub resourceprep {
my ($str) = @_;
decode_utf8 (idn_prep_resource (encode_utf8 ($str), 'UTF-8'))
}
=item B<nodeprep ($string)>
This function applies the stringprep profile for nodes to C<$string>
and returns the result.
=cut
sub nodeprep {
my ($str) = @_;
decode_utf8 (idn_prep_node (encode_utf8 ($str), 'UTF-8'))
}
=item B<prep_join_jid ($node, $domain, $resource)>
This function joins the parts C<$node>, C<$domain> and C<$resource>
to a full jid and applies stringprep profiles. If the profiles couldn't
be applied undef will be returned.
=cut
sub prep_join_jid {
my ($node, $domain, $resource) = @_;
my $jid = "";
if ($node ne '') {
$node = nodeprep ($node);
return undef unless defined $node;
$jid .= "$node\@";
}
$domain = $domain; # TODO: apply IDNA!
$jid .= $domain;
if ($resource ne '') {
$resource = resourceprep ($resource);
return undef unless defined $resource;
$jid .= "/$resource";
}
$jid
}
=item B<join_jid ($user, $domain, $resource)>
This is a plain concatenation of C<$user>, C<$domain> and C<$resource>
without stringprep.
See also L<prep_join_jid>
=cut
sub join_jid {
my ($node, $domain, $resource) = @_;
my $jid = "";
$jid .= "$node\@" if $node ne '';
$jid .= $domain;
$jid .= "/$resource" if $resource ne '';
$jid
}
=item B<split_uri ($uri)>
This function splits up the C<$uri> into service and node
part and will return them as list.
my ($service, $node) = split_uri ($uri);
=cut
sub split_uri {
my ($uri) = @_;
if ($uri =~ /^xmpp:(\S+)\?\w+;node=(\S+)$/) {
return ($1, $2);
} else {
return (undef, $uri);
}
}
=item B<split_jid ($jid)>
This function splits up the C<$jid> into user/node, domain and resource
part and will return them as list.
my ($user, $host, $res) = split_jid ($jid);
=cut
sub split_jid {
my ($jid) = @_;
if ($jid =~ /^(?:([^@]*)@)?([^\/]+)(?:\/(.*))?$/) {
return ($1 eq '' ? undef : $1, $2, $3 eq '' ? undef : $3);
} else {
return (undef, undef, undef);
}
}
=item B<node_jid ($jid)>
See C<prep_res_jid> below.
=item B<domain_jid ($jid)>
See C<prep_res_jid> below.
=item B<res_jid ($jid)>
See C<prep_res_jid> below.
=item B<prep_node_jid ($jid)>
See C<prep_res_jid> below.
=item B<prep_domain_jid ($jid)>
See C<prep_res_jid> below.
=item B<prep_res_jid ($jid)>
These functions return the corresponding parts of a JID.
The C<prep_> prefixed JIDs return the stringprep'ed versions.
=cut
sub node_jid { (split_jid ($_[0]))[0] }
sub domain_jid { (split_jid ($_[0]))[1] }
sub res_jid { (split_jid ($_[0]))[2] }
sub prep_node_jid { nodeprep (node_jid ($_[0])) }
sub prep_domain_jid { (domain_jid ($_[0])) }
sub prep_res_jid { resourceprep (res_jid ($_[0])) }
=item B<stringprep_jid ($jid)>
This applies stringprep to all parts of the jid according to the RFC 3920.
Use this if you want to compare two jids like this:
stringprep_jid ($jid_a) eq stringprep_jid ($jid_b)
This function returns undef if the C<$jid> couldn't successfully be parsed
and the preparations done.
=cut
sub stringprep_jid {
my ($jid) = @_;
my ($user, $host, $res) = split_jid ($jid);
return undef unless defined ($user) || defined ($host) || defined ($res);
return prep_join_jid ($user, $host, $res);
}
=item B<cmp_jid ($jid1, $jid2)>
This function compares two jids C<$jid1> and C<$jid2>
whether they are equal.
=cut
sub cmp_jid {
my ($jid1, $jid2) = @_;
stringprep_jid ($jid1) eq stringprep_jid ($jid2)
}
=item B<cmp_bare_jid ($jid1, $jid2)>
This function compares two jids C<$jid1> and C<$jid2> whether their
bare part is equal.
=cut
sub cmp_bare_jid {
my ($jid1, $jid2) = @_;
cmp_jid (bare_jid ($jid1), bare_jid ($jid2))
}
=item B<prep_bare_jid ($jid)>
This function makes the jid C<$jid> a bare jid, meaning:
it will strip off the resource part. With stringprep.
=cut
sub prep_bare_jid {
my ($jid) = @_;
my ($user, $host, $res) = split_jid ($jid);
prep_join_jid ($user, $host)
}
=item B<bare_jid ($jid)>
This function makes the jid C<$jid> a bare jid, meaning:
it will strip off the resource part. But without stringprep.
=cut
sub bare_jid {
my ($jid) = @_;
my ($user, $host, $res) = split_jid ($jid);
join_jid ($user, $host)
}
=item B<is_bare_jid ($jid)>
This method returns a boolean which indicates whether C<$jid> is a
bare JID.
=cut
sub is_bare_jid {
my ($jid) = @_;
my ($user, $host, $res) = split_jid ($jid);
not defined $res
}
=item B<filter_xml_chars ($string)>
This function removes all characters from C<$string> which
are not allowed in XML and returns the new string.
=cut
sub filter_xml_chars($) {
my ($string) = @_;
$string =~ s/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFFFF}]+//g;
$string
}
=item B<filter_xml_attr_hash_chars ($hashref)>
This runs all values of the C<$hashref> through C<filter_xml_chars> (see above)
and changes them in-place!
=cut
sub filter_xml_attr_hash_chars {
my ($hash) = @_;
$hash->{$_} = filter_xml_chars $hash->{$_} for keys %$hash
}
=item B<simxml ($w, %xmlstruct)>
This function takes a L<XML::Writer> as first argument (C<$w>) and the
rest key value pairs:
simxml ($w,
defns => '<xmlnamespace>',
node => <node>,
prefixes => { prefix => namespace, ... },
);
Where node is:
<node> := {
ns => '<xmlnamespace>',
name => 'tagname',
attrs => [ 'name', 'value', 'name2', 'value2', ... ],
childs => [ <node>, ... ]
}
| {
dns => '<xmlnamespace>', # this will set that namespace to
# the default namespace before using it.
name => 'tagname',
attrs => [ 'name', 'value', 'name2', 'value2', ... ],
childs => [ <node>, ... ]
}
| sub { my ($w) = @_; ... } # with $w being a XML::Writer object
| "textnode"
Please note: C<childs> stands for C<child sequence> :-)
Also note that if you omit the C<ns> key for nodes there is a fall back
to the namespace of the parent element or the last default namespace.
This makes it easier to write things like this:
{
defns => 'muc_owner',
node => { name => 'query' }
}
(Without having to include C<ns> in the node.)
Please note that all attribute values and character data will be filtered
by C<filter_xml_chars>.
This is a bigger example:
...
$msg->append_creation( sub {
my($w) = @_;
simxml($w,
defns => 'muc_user', # sets the default namepsace for all following elements
node => {
name => 'x', # element 'x' in namespace 'muc_user'
childs => [
{
'name' => 'invite', # element 'invite' in namespace 'muc_user'
'attrs' => [ 'to', $to_jid ], # to="$to_jid" attribute for 'invite'
'childs' => [
{ # the <reason>$reason</reason> element in the invite element
'name' => 'reason',
childs => [ $reason ]
}
],
}
]
}
);
});
=cut
sub simxml {
my ($w, %desc) = @_;
if (my $n = $desc{defns}) {
$w->addPrefix (xmpp_ns_maybe ($n), '');
}
unless (exists $desc{fb_ns}) {
$desc{fb_ns} = $desc{defns};
}
if (my $p = $desc{prefixes}) {
for (keys %{$p || {}}) {
$w->addPrefix (xmpp_ns_maybe ($_), $p->{$_});
}
}
my $node = $desc{node};
if (not defined $node) {
return;
} elsif (ref ($node) eq 'CODE') {
$node->($w);
} elsif (ref ($node)) {
my $ns = $node->{dns} ? $node->{dns} : $node->{ns};
$ns = $ns ? $ns : $desc{fb_ns};
$ns = xmpp_ns_maybe ($ns);
my $tag = $ns ? [$ns, $node->{name}] : $node->{name};
my %attrs = @{$node->{attrs} || []};
filter_xml_attr_hash_chars \%attrs;
if (@{$node->{childs} || []}) {
$w->startTag ($tag, %attrs);
my (@args);
if ($node->{defns}) { @args = (defns => $node->{defns}) }
for (@{$node->{childs}}) {
if (ref ($_) eq 'HASH' && $_->{dns}) {
push @args, (defns => $_->{dns})
}
if (ref ($_) eq 'HASH' && $_->{ns}) {
push @args, (fb_ns => $_->{ns})
} else {
push @args, (fb_ns => $desc{fb_ns})
}
simxml ($w, node => $_, @args)
}
$w->endTag;
} else {
$w->emptyTag ($tag, %attrs);
}
} else {
$w->characters (filter_xml_chars $node);
}
}
=item B<to_xmpp_time ($sec, $min, $hour, $tz, $secfrac)>
This function transforms a time to the XMPP date time format.
The meanings and value ranges of C<$sec>, ..., C<$hour> are explained
in the perldoc of Perl's builtin C<localtime>.
C<$tz> has to be either C<"UTC"> or of the form C<[+-]hh:mm>, it can be undefined
and wont occur in the time string then.
C<$secfrac> are optional and can be the fractions of the second.
See also XEP-0082.
=cut
sub to_xmpp_time {
my ($sec, $min, $hour, $tz, $secfrac) = @_;
my $frac = sprintf "%.3f", $secfrac;
substr $frac, 0, 1, '';
sprintf "%02d:%02d:%02d%s%s",
$hour, $min, $sec,
(defined $secfrac ? $frac : ""),
(defined $tz ? $tz : "")
}
=item B<to_xmpp_datetime ($sec,$min,$hour,$mday,$mon,$year,$tz, $secfrac)>
This function transforms a time to the XMPP date time format.
The meanings of C<$sec>, ..., C<$year> are explained in the perldoc
of Perl's C<localtime> builtin and have the same value ranges.
C<$tz> has to be either C<"Z"> (for UTC) or of the form C<[+-]hh:mm> (offset
from UTC), if it is undefined "Z" will be used.
C<$secfrac> are optional and can be the fractions of the second.
See also XEP-0082.
=cut
sub to_xmpp_datetime {
my ($sec, $min, $hour, $mday, $mon, $year, $tz, $secfrac) = @_;
my $time = to_xmpp_time ($sec, $min, $hour, (defined $tz ? $tz : 'Z'), $secfrac);
sprintf "%04d-%02d-%02dT%s", $year + 1900, $mon + 1, $mday, $time;
}
=item B<from_xmpp_datetime ($string)>
This function transforms the C<$string> which is either a time or datetime in XMPP
format. If the string was not in the right format an empty list is returned.
Otherwise this is returned:
my ($sec, $min, $hour, $mday, $mon, $year, $tz, $secfrac)
= from_xmpp_datetime ($string);
For the value ranges and semantics of C<$sec>, ..., C<$srcfrac> please look at the
documentation for C<to_xmpp_datetime>.
C<$tz> and C<$secfrac> might be undefined.
If C<$tz> is undefined the timezone is to be assumed to be UTC.
If C<$string> contained just a time C<$mday>, C<$mon> and C<$year> will be undefined.
See also XEP-0082.
=cut
sub from_xmpp_datetime {
my ($string) = @_;
if ($string !~
/^(?:(\d{4})-?(\d{2})-?(\d{2})T)?(\d{2}):(\d{2}):(\d{2})(\.\d{3})?(Z|[+-]\d{2}:\d{2})?/)
{
return ()
}
($6, $5, $4,
($3 ne '' ? $3 : undef),
($2 ne '' ? $2 - 1 : undef),
($1 ne '' ? $1 - 1900 : undef),
($8 ne '' ? $8 : undef),
($7 ne '' ? $7 : undef))
}
=item B<xmpp_datetime_as_timestamp ($string)>
This function takes the same arguments as C<from_xmpp_datetime>, but returns a
unix timestamp, like C<time ()> would.
This function requires the L<POSIX> module.
=cut
sub xmpp_datetime_as_timestamp {
my ($string) = @_;
my ($s, $m, $h, $md, $mon, $year, $tz) = from_xmpp_datetime ($string);
return 0 unless defined $h;
my $ts = timegm ($s, $m, $h, $md, $mon, $year);
if ($tz =~ /^([+-])(\d{2}):(\d{2})$/) {
$ts += ($1 eq '-' ? -1 : 1) * ($2 * 3600 + $3 * 60)
}
$ts
}
sub dump_twig_xml {
my $data = shift;
require XML::Twig;
my $t = XML::Twig->new;
if ($t->safe_parse ("<deb>$data</deb>")) {
$t->set_pretty_print ('indented');
return ($t->sprint . "\n");
} else {
return "$data\n";
}
}
sub install_default_debug_dump {
my ($con) = @_;
$con->reg_cb (
debug_recv => sub {
my ($con, $data) = @_;
printf "recv>> %s:%d\n%s", $con->{host}, $con->{port}, dump_twig_xml ($data)
},
debug_send => sub {
my ($con, $data) = @_;
printf "send<< %s:%d\n%s", $con->{host}, $con->{port}, dump_twig_xml ($data)
},
)
}
=back
=head1 AUTHOR
Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
=head1 COPYRIGHT & LICENSE
Copyright 2007, 2008 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.
=cut
1; # End of AnyEvent::XMPP
|