| 1 |
elmex |
1.1 |
package Net::XMPP2::Util; |
| 2 |
|
|
use warnings; |
| 3 |
|
|
use strict; |
| 4 |
|
|
use Encode; |
| 5 |
|
|
use Net::LibIDN qw/idn_prep_name idn_prep_resource idn_prep_node/; |
| 6 |
|
|
|
| 7 |
|
|
=head1 NAME |
| 8 |
|
|
|
| 9 |
|
|
Net::XMPP2::Util - Utility functions for Net::XMPP2 |
| 10 |
|
|
|
| 11 |
|
|
=head1 SYNOPSIS |
| 12 |
|
|
|
| 13 |
|
|
use Net::XMPP2::Util; |
| 14 |
|
|
... |
| 15 |
|
|
|
| 16 |
|
|
=head1 FUNCTIONS |
| 17 |
|
|
|
| 18 |
|
|
=head2 resourceprep ($string) |
| 19 |
|
|
|
| 20 |
|
|
This function applies the stringprep profile for resources to C<$string> |
| 21 |
|
|
and returns the result. |
| 22 |
|
|
|
| 23 |
|
|
=cut |
| 24 |
|
|
|
| 25 |
|
|
sub resourceprep { |
| 26 |
|
|
my ($str) = @_; |
| 27 |
|
|
decode_utf8 (idn_prep_resource (encode_utf8 ($str), 'UTF-8')) |
| 28 |
|
|
} |
| 29 |
|
|
|
| 30 |
|
|
=head2 nodeprep ($string) |
| 31 |
|
|
|
| 32 |
|
|
This function applies the stringprep profile for nodes to C<$string> |
| 33 |
|
|
and returns the result. |
| 34 |
|
|
|
| 35 |
|
|
=cut |
| 36 |
|
|
|
| 37 |
|
|
sub nodeprep { |
| 38 |
|
|
my ($str) = @_; |
| 39 |
|
|
decode_utf8 (idn_prep_node (encode_utf8 ($str), 'UTF-8')) |
| 40 |
|
|
} |
| 41 |
|
|
|
| 42 |
|
|
=head2 join_jid ($node, $domain, $resource) |
| 43 |
|
|
|
| 44 |
|
|
This function joins the parts C<$node>, C<$domain> and C<$resource> |
| 45 |
|
|
to a full jid and applies stringprep profiles. If the profiles couldn't |
| 46 |
|
|
be applied undef will be returned. |
| 47 |
|
|
|
| 48 |
|
|
=cut |
| 49 |
|
|
|
| 50 |
|
|
sub join_jid { |
| 51 |
|
|
my ($node, $domain, $resource) = @_; |
| 52 |
|
|
my $jid = ""; |
| 53 |
|
|
|
| 54 |
|
|
if (defined $node) { |
| 55 |
|
|
$node = nodeprep ($node); |
| 56 |
|
|
return undef unless defined $node; |
| 57 |
|
|
$jid .= "$node\@"; |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
$domain = $domain; # TODO: apply IDNA! |
| 61 |
|
|
$jid .= $domain; |
| 62 |
|
|
|
| 63 |
|
|
if (defined $resource) { |
| 64 |
|
|
$resource = resourceprep ($resource); |
| 65 |
|
|
return undef unless defined $resource; |
| 66 |
|
|
$jid .= "/$resource"; |
| 67 |
|
|
} |
| 68 |
|
|
|
| 69 |
|
|
$jid |
| 70 |
|
|
} |
| 71 |
|
|
|
| 72 |
|
|
=head2 stringprep_jid ($jid) |
| 73 |
|
|
|
| 74 |
|
|
This applies stringprep to all parts of the jid according to the RFC 3920. |
| 75 |
|
|
Use this if you want to compare two jids like this: |
| 76 |
|
|
|
| 77 |
|
|
stringprep_jid ($jid_a) eq stringprep_jid ($jid_b) |
| 78 |
|
|
|
| 79 |
|
|
This function returns undef if the C<$jid> couldn't successfully be parsed |
| 80 |
|
|
and the preparations done. |
| 81 |
|
|
|
| 82 |
|
|
=cut |
| 83 |
|
|
|
| 84 |
|
|
sub stringprep_jid { |
| 85 |
|
|
my ($jid) = @_; |
| 86 |
|
|
if ($jid =~ /^([^@]*)@?([^\/]+)\/(.*)$/) { |
| 87 |
|
|
return join_jid ($1, $2, $3); |
| 88 |
|
|
} else { |
| 89 |
|
|
return undef; |
| 90 |
|
|
} |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
=head1 AUTHOR |
| 94 |
|
|
|
| 95 |
|
|
Robin Redeker, C<< <elmex at ta-sa.org> >> |
| 96 |
|
|
|
| 97 |
|
|
=head1 BUGS |
| 98 |
|
|
|
| 99 |
|
|
Please report any bugs or feature requests to |
| 100 |
|
|
C<bug-net-xmpp2 at rt.cpan.org>, or through the web interface at |
| 101 |
|
|
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-XMPP2>. |
| 102 |
|
|
I will be notified, and then you'll automatically be notified of progress on |
| 103 |
|
|
your bug as I make changes. |
| 104 |
|
|
|
| 105 |
|
|
=head1 SUPPORT |
| 106 |
|
|
|
| 107 |
|
|
You can find documentation for this module with the perldoc command. |
| 108 |
|
|
|
| 109 |
|
|
perldoc Net::XMPP2 |
| 110 |
|
|
|
| 111 |
|
|
You can also look for information at: |
| 112 |
|
|
|
| 113 |
|
|
=over 4 |
| 114 |
|
|
|
| 115 |
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
| 116 |
|
|
|
| 117 |
|
|
L<http://annocpan.org/dist/Net-XMPP2> |
| 118 |
|
|
|
| 119 |
|
|
=item * CPAN Ratings |
| 120 |
|
|
|
| 121 |
|
|
L<http://cpanratings.perl.org/d/Net-XMPP2> |
| 122 |
|
|
|
| 123 |
|
|
=item * RT: CPAN's request tracker |
| 124 |
|
|
|
| 125 |
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-XMPP2> |
| 126 |
|
|
|
| 127 |
|
|
=item * Search CPAN |
| 128 |
|
|
|
| 129 |
|
|
L<http://search.cpan.org/dist/Net-XMPP2> |
| 130 |
|
|
|
| 131 |
|
|
=back |
| 132 |
|
|
|
| 133 |
|
|
=head1 ACKNOWLEDGEMENTS |
| 134 |
|
|
|
| 135 |
|
|
=head1 COPYRIGHT & LICENSE |
| 136 |
|
|
|
| 137 |
|
|
Copyright 2007 Robin Redeker, all rights reserved. |
| 138 |
|
|
|
| 139 |
|
|
This program is free software; you can redistribute it and/or modify it |
| 140 |
|
|
under the same terms as Perl itself. |
| 141 |
|
|
|
| 142 |
|
|
=cut |
| 143 |
|
|
|
| 144 |
|
|
1; # End of Net::XMPP2 |