| 1 | package Net::Social::Service::Jabber; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use base qw(Net::Social::Service); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Jabber::NodeFactory; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Jabber::Connection; | ||
| 7 | use Net::Social qw(:all); | ||
| 8 | use Data::Dumper; | ||
| 9 | use vars qw($VERSION); | ||
| 10 | |||
| 11 | $VERSION = "0.1"; | ||
| 12 | |||
| 13 | |||
| 14 | my $resource = __PACKAGE__."-".$VERSION; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Net::Social::Service::Jabber - a Jabber plugin for Net::Social | ||
| 19 | |||
| 20 | =head1 PARAMS | ||
| 21 | |||
| 22 | For reading C<Net::Social::Service::Jabber> needs | ||
| 23 | |||
| 24 | =over 4 | ||
| 25 | |||
| 26 | =item username | ||
| 27 | |||
| 28 | Your Jabber username in the form <id>@<server> | ||
| 29 | |||
| 30 | =item | ||
| 31 | |||
| 32 | Your Jabber password. | ||
| 33 | |||
| 34 | =back | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub params {( | ||
| 39 | read => { | ||
| 40 | "username" => { required => 1, | ||
| 41 | description => "Your Jabber UserName (in the form <name>@<host>", | ||
| 42 | }, | ||
| 43 | "password" => { required => 1, | ||
| 44 | description => "Your Jabber password", | ||
| 45 | sensitive => 1, | ||
| 46 | }, | ||
| 47 | }, | ||
| 48 | )} | ||
| 49 | |||
| 50 | =head1 METHODS | ||
| 51 | |||
| 52 | =head2 friends | ||
| 53 | |||
| 54 | Returns your friends. It defines the keys C<id>, C<username>, C<name> and C<type>. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub friends { | ||
| 59 | my $self = shift; | ||
| 60 | return () unless $self->{_logged_in}; | ||
| 61 | my $user = $self->{_details}->{username}; | ||
| 62 | my $pass = $self->{_details}->{password}; | ||
| 63 | my $port = $self->{_details}->{port} || 5222; | ||
| 64 | my ($name, $server) = split '@', $user; | ||
| 65 | |||
| 66 | # Do we want to create a per object jabber connection or per call? | ||
| 67 | my $nf = Jabber::NodeFactory->new; | ||
| 68 | my $c = Jabber::Connection->new(server => $server.':'.$port, log => 0); | ||
| 69 | my $running = 1; | ||
| 70 | my @users; | ||
| 71 | |||
| 72 | $c->connect || die $c->lastError; | ||
| 73 | $c->register_handler('iq', sub { on_iq_message(\@users, \$running, @_) } ); | ||
| 74 | $c->auth($name, $pass, $resource); | ||
| 75 | |||
| 76 | my $iq = $nf->newNode('iq'); | ||
| 77 | $iq->attr('type', Jabber::NS::IQ_GET()); | ||
| 78 | $iq->attr('id', 'roster_get_id'); | ||
| 79 | $iq->insertTag('query', Jabber::NS::NS_ROSTER()); | ||
| 80 | |||
| 81 | |||
| 82 | $c->send($iq); | ||
| 83 | |||
| 84 | my $t0 = time; | ||
| 85 | while ($running) { | ||
| 86 | $c->process(1); | ||
| 87 | last if time-$t0 > 10; | ||
| 88 | } | ||
| 89 | $c->disconnect; | ||
| 90 | return @users; | ||
| 91 | } | ||
| 92 | my %type_convert = ( | ||
| 93 | to => FRIENDED, | ||
| 94 | from => FRIENDED_BY, | ||
| 95 | both => MUTUAL, | ||
| 96 | none => NONE, | ||
| 97 | ); | ||
| 98 | |||
| 99 | sub on_iq_message { | ||
| 100 | my $users = shift; | ||
| 101 | my $running = shift; | ||
| 102 | my $node = shift; | ||
| 103 | return unless $node->attr('id') eq 'roster_get_id'; | ||
| 104 | my $query = $node->getTag('query'); | ||
| 105 | foreach my $item ($query->getChildren) { | ||
| 106 | my %user = ( | ||
| 107 | id => $item->attr('jid'), | ||
| 108 | username => $item->attr('name'), | ||
| 109 | name => $item->attr('nickname') || $item->attr('name'), | ||
| 110 | type => $type_convert{$item->attr('subscription')}, | ||
| 111 | ); | ||
| 112 | push @$users, \%user; | ||
| 113 | } | ||
| 114 | $$running = 0; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Not quite ready yet | ||
| 118 | #sub add_friend { | ||
| 119 | # my $self = shift; | ||
| 120 | # my $friend = shift; | ||
| 121 | # my $nf = $self->{_node_factory}; | ||
| 122 | # my $c = $self->{_connection}; | ||
| 123 | # | ||
| 124 | # my $iq = $nf->newNode('iq'); | ||
| 125 | # $iq->attr('type', Jabber::NS::IQ_SET()); | ||
| 126 | # $iq->attr('id', 'roster_set_id'); | ||
| 127 | # | ||
| 128 | # my $item = $nf->newNode('item'); | ||
| 129 | # $item->attr('jid', $friend); | ||
| 130 | # $item->attr('name', $name) if defined $name; | ||
| 131 | # $item->insertTag('group')->data('friends'); | ||
| 132 | # | ||
| 133 | # $iq->insertTag('query', Jabber::NS::NS_ROSTER())->rawdata($item->toStr); | ||
| 134 | # | ||
| 135 | # die $iq->toStr; | ||
| 136 | # $c->send($iq); | ||
| 137 | #} | ||
| 138 | # sub DESTROY { $_->{connection}->disconnect } | ||
| 139 | |||
| 140 | =head1 AUTHOR | ||
| 141 | |||
| 142 | Simon Wistow <simon@thegestalt.org> | ||
| 143 | |||
| 144 | =head1 COPYRIGHT | ||
| 145 | |||
| 146 | Copyright, 2007 - Simon Wistow | ||
| 147 | |||
| 148 | Distributed under the same terms as Perl itself | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | |||
| 153 | 1; | ||