| 1 | package AnyEvent::XMPP::Ext::Pubsub; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use AnyEvent::XMPP::Util qw/simxml split_uri/; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use AnyEvent::XMPP::Namespaces qw/xmpp_ns/; | ||
| 5 | use AnyEvent::XMPP::Ext; | ||
| 6 | |||
| 7 | our @ISA = qw/AnyEvent::XMPP::Ext/; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | AnyEvent::XMPP::Ext::Pubsub - Implements XEP-0060: Publish-Subscribe | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | my $con = AnyEvent::XMPP::Connection->new (...); | ||
| 16 | $con->add_extension (my $ps = AnyEvent::XMPP::Ext::Pubsub->new); | ||
| 17 | ... | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This module implements all tasks of handling the publish subscribe | ||
| 22 | mechanism. (partially implemented) | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub handle_incoming_pubsub_event { | ||
| 27 | my ($self, $node) = @_; | ||
| 28 | |||
| 29 | my (@items); | ||
| 30 | if(my ($q) = $node->find_all ([qw/pubsub_ev items/])) { | ||
| 31 | foreach($q->find_all ([qw/pubsub_ev item/])) { | ||
| 32 | push @items, $_; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | $self->event(pubsub_recv => @items); | ||
| 36 | } | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | =over 4 | ||
| 41 | |||
| 42 | =item B<new> | ||
| 43 | |||
| 44 | This is the constructor for a pubsub object. | ||
| 45 | It takes no further arguments. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $this = shift; | ||
| 51 | my $class = ref($this) || $this; | ||
| 52 | my $self = bless { @_ }, $class; | ||
| 53 | $self->init; | ||
| 54 | $self | ||
| 55 | } | ||
| 56 | |||
| 57 | sub init { | ||
| 58 | my ($self) = @_; | ||
| 59 | |||
| 60 | $self->reg_cb ( | ||
| 61 | ext_before_message_xml => sub { | ||
| 62 | my ($self, $con, $node) = @_; | ||
| 63 | |||
| 64 | my $handled = 0; | ||
| 65 | for ($node->find_all ([qw/pubsub_ev event/])) { | ||
| 66 | $self->stop_event; | ||
| 67 | $self->handle_incoming_pubsub_event($_); | ||
| 68 | } | ||
| 69 | |||
| 70 | $handled | ||
| 71 | } | ||
| 72 | ); | ||
| 73 | } | ||
| 74 | |||
| 75 | =item B<delete_node($con, $uri, $cb)> | ||
| 76 | C<$con> is the connection already established, | ||
| 77 | C<$uri> is the name of the node to be created | ||
| 78 | C<$cb> is the callback | ||
| 79 | |||
| 80 | Try to remove a node. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub delete_node { | ||
| 85 | my ($self, $con, $uri, $cb) = @_; | ||
| 86 | |||
| 87 | my ($service, $node) = split_uri ($uri); | ||
| 88 | |||
| 89 | $con->send_iq ( | ||
| 90 | set => sub { | ||
| 91 | my ($w) = @_; | ||
| 92 | simxml ($w, defns => 'pubsub_own', node => { | ||
| 93 | name => 'pubsub', childs => [ | ||
| 94 | { name => 'delete', attrs => [ node => $node ] }, | ||
| 95 | ] | ||
| 96 | }); | ||
| 97 | }, | ||
| 98 | sub { | ||
| 99 | my ($node, $err) = @_; | ||
| 100 | $cb->(defined $err ? $err : ()) if $cb; | ||
| 101 | }, | ||
| 102 | (defined $service ? (to => $service) : ()) | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | |||
| 106 | =item B<create_node ($con, $uri, $cb)> | ||
| 107 | C<$con> is the connection already established, | ||
| 108 | C<$uri> is the name of the node to be created | ||
| 109 | C<$cb> is the callback | ||
| 110 | |||
| 111 | Try to create a node. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub create_node { | ||
| 116 | my ($self, $con, $uri, $cb) = @_; | ||
| 117 | |||
| 118 | my ($service, $node) = split_uri ($uri); | ||
| 119 | |||
| 120 | $con->send_iq ( | ||
| 121 | set => sub { | ||
| 122 | my ($w) = @_; | ||
| 123 | simxml ($w, defns => 'pubsub', node => { | ||
| 124 | name => 'pubsub', childs => [ | ||
| 125 | { name => 'create', attrs => [ node => $node ] }, | ||
| 126 | { name => 'configure' } | ||
| 127 | ] | ||
| 128 | }); | ||
| 129 | }, | ||
| 130 | sub { | ||
| 131 | my ($node, $err) = @_; | ||
| 132 | $cb->(defined $err ? $err : ()) if $cb; | ||
| 133 | }, | ||
| 134 | (defined $service ? (to => $service) : ()) | ||
| 135 | ); | ||
| 136 | } | ||
| 137 | |||
| 138 | =item B<subscribe_node($con, $uri, $cb)> | ||
| 139 | C<$con> is the connection already established, | ||
| 140 | C<$uri> is the name of the node to be created | ||
| 141 | C<$cb> is the callback | ||
| 142 | |||
| 143 | Try to retrieve items. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub subscribe_node { | ||
| 148 | my ($self, $con, $uri, $cb) = @_; | ||
| 149 | my $jid = $con->jid; | ||
| 150 | |||
| 151 | my ($service, $node) = split_uri ($uri); | ||
| 152 | |||
| 153 | $con->send_iq ( | ||
| 154 | set => sub { | ||
| 155 | my ($w) = @_; | ||
| 156 | simxml ($w, defns => 'pubsub', node => { | ||
| 157 | name => 'pubsub', childs => [ | ||
| 158 | { name => 'subscribe', attrs => [ | ||
| 159 | node => $node, | ||
| 160 | jid => $jid ] | ||
| 161 | } | ||
| 162 | ] | ||
| 163 | }); | ||
| 164 | }, | ||
| 165 | sub { | ||
| 166 | my ($node, $err) = @_; | ||
| 167 | $cb->(defined $err ? $err : ()) if $cb; | ||
| 168 | }, | ||
| 169 | (defined $service ? (to => $service) : ()) | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | =item B<unsubscribe_node>($con, $uri, $bc)> | ||
| 174 | C<$con> is the connection already established, | ||
| 175 | C<$uri> is the name of the node to be created | ||
| 176 | C<$cb> is the callback | ||
| 177 | |||
| 178 | Try to unsubscribe from a node. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub unsubscribe_node { | ||
| 183 | my ($self, $con, $uri, $cb) = @_; | ||
| 184 | my $jid = $con->jid; | ||
| 185 | |||
| 186 | my ($service, $node) = split_uri ($uri); | ||
| 187 | |||
| 188 | $con->send_iq ( | ||
| 189 | set => sub { | ||
| 190 | my ($w) = @_; | ||
| 191 | simxml ($w, defns => 'pubsub', node => { | ||
| 192 | name => 'pubsub', childs => [ | ||
| 193 | { name => 'unsubscribe', attrs => [ | ||
| 194 | node => $node, | ||
| 195 | jid => $jid ] | ||
| 196 | } | ||
| 197 | ] | ||
| 198 | }); | ||
| 199 | }, | ||
| 200 | sub { | ||
| 201 | my ($node, $err) = @_; | ||
| 202 | $cb->(defined $err ? $err : ()) if $cb; | ||
| 203 | }, | ||
| 204 | (defined $service ? (to => $service) : ()) | ||
| 205 | ); | ||
| 206 | } | ||
| 207 | |||
| 208 | =item B<publish_item($con, $uri, $create_cb, $cb)> | ||
| 209 | C<$con> is the connection already established, | ||
| 210 | C<$uri> is the name of the node to be created | ||
| 211 | C<$create_cb> is the callback | ||
| 212 | C<$cb> is the callback | ||
| 213 | |||
| 214 | Try to publish an item. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub publish_item { | ||
| 219 | my ($self, $con, $uri, $create_cb, $cb) = @_; | ||
| 220 | |||
| 221 | my ($service, $node) = split_uri ($uri); | ||
| 222 | |||
| 223 | $con->send_iq ( | ||
| 224 | set => sub { | ||
| 225 | my ($w) = @_; | ||
| 226 | simxml ($w, defns => 'pubsub', node => { | ||
| 227 | name => 'pubsub', childs => [ | ||
| 228 | { name => 'publish', attrs => [ node => $node ], childs => [ | ||
| 229 | { name => 'item', childs => [ $create_cb ] } | ||
| 230 | ] | ||
| 231 | }, | ||
| 232 | ] | ||
| 233 | }); | ||
| 234 | }, | ||
| 235 | sub { | ||
| 236 | my ($node, $err) = @_; | ||
| 237 | warn "OK $create_cb / $cb\n"; | ||
| 238 | $cb->(defined $err ? $err : ()) if $cb; | ||
| 239 | }, | ||
| 240 | (defined $service ? (to => $service) : ()) | ||
| 241 | ); | ||
| 242 | } | ||
| 243 | |||
| 244 | =item B<retrive_items($con, $uri, $cb)> | ||
| 245 | C<$con> is the connection already established, | ||
| 246 | C<$uri> is the name of the node to be created | ||
| 247 | C<$cb> is the callback | ||
| 248 | |||
| 249 | Try to retrieve items. | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub retrieve_items { | ||
| 254 | my ($self, $con, $uri, $cb) = @_; | ||
| 255 | |||
| 256 | my($service, $node) = split_uri ($uri); | ||
| 257 | |||
| 258 | $con->send_iq ( | ||
| 259 | get => sub { | ||
| 260 | my ($w) = @_; | ||
| 261 | simxml ($w, defns => 'pubsub', node => { | ||
| 262 | name => 'pubsub', childs => [ | ||
| 263 | { name => 'items', attrs => [ node => $node ] } | ||
| 264 | ] | ||
| 265 | }); | ||
| 266 | }, | ||
| 267 | sub { | ||
| 268 | my ($node, $err) = @_; | ||
| 269 | $cb->(defined $err ? $err : ()) if $cb; | ||
| 270 | }, | ||
| 271 | (defined $service ? (to => $service) : ()) | ||
| 272 | ); | ||
| 273 | } | ||
| 274 | |||
| 275 | =item B<retrive_item($con, $uri, $id, $cb)> | ||
| 276 | C<$con> is the connection already established, | ||
| 277 | C<$uri> is the name of the node to be created | ||
| 278 | C<$id> is the id of the entry to be retrieved | ||
| 279 | C<$cb> is the cb | ||
| 280 | |||
| 281 | Try to retrieve item. | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub retrieve_item { | ||
| 286 | my ($self, $con, $uri, $id, $cb) = @_; | ||
| 287 | |||
| 288 | my($service, $node) = split_uri ($uri); | ||
| 289 | |||
| 290 | $con->send_iq ( | ||
| 291 | get => sub { | ||
| 292 | my ($w) = @_; | ||
| 293 | simxml( $w, defns => 'pubsub', node => { | ||
| 294 | name => 'pubsub', childs => [ | ||
| 295 | { name => 'items', attrs => [ node => $node ], | ||
| 296 | childs => [ | ||
| 297 | { name => 'item', attrs => [ id => $id ] }] | ||
| 298 | } | ||
| 299 | ] | ||
| 300 | }); | ||
| 301 | }, | ||
| 302 | sub { | ||
| 303 | my ($node, $err) = @_; | ||
| 304 | $cb->(defined $err ? $err : ()) if $cb; | ||
| 305 | }, | ||
| 306 | (defined $service ? (to => $service) : ()) | ||
| 307 | ); | ||
| 308 | } | ||
| 309 | |||
| 310 | =back | ||
| 311 | |||
| 312 | =head1 AUTHOR | ||
| 313 | |||
| 314 | Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >> | ||
| 315 | |||
| 316 | =head1 CONTRIBUTORS | ||
| 317 | |||
| 318 | Chris Miceli - additional work on the pubsub extension | ||
| 319 | |||
| 320 | =head1 COPYRIGHT & LICENSE | ||
| 321 | |||
| 322 | Copyright 2007, 2008 Robin Redeker, all rights reserved. | ||
| 323 | |||
| 324 | This program is free software; you can redistribute it and/or modify it | ||
| 325 | under the same terms as Perl itself. | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | 1; # End of AnyEvent::XMPP::Ext::Pubsub | ||
| 330 | |||