| 1 | package AnyEvent::XMPP::Ext::Superfeedr; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use AnyEvent::Superfeedr::Notification; | ||
| 6 | use base qw/AnyEvent::XMPP::Ext::Pubsub/; | ||
| 7 | |||
| 8 | use AnyEvent::XMPP::Util qw/simxml split_uri/; | ||
| 9 | |||
| 10 | use constant NS => 'http://superfeedr.com/xmpp-pubsub-ext'; | ||
| 11 | |||
| 12 | =pod | ||
| 13 | |||
| 14 | Fires up Pubsub event's plus 2 new events: | ||
| 15 | |||
| 16 | =over 4 | ||
| 17 | |||
| 18 | =item superfeedr_status( $status_hash ) | ||
| 19 | |||
| 20 | A hash with the content of status | ||
| 21 | |||
| 22 | =item superfeedr_notification( $notification | ||
| 23 | |||
| 24 | A L<AnyEvent::Superfeedr::Notification> object. | ||
| 25 | |||
| 26 | =back | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | sub handle_incoming_pubsub_event { | ||
| 31 | my ($self, $node) = @_; | ||
| 32 | |||
| 33 | my (@items, $status_node); | ||
| 34 | my ($code, $next_fetch, $title, $feed_uri); | ||
| 35 | |||
| 36 | if ( ($status_node) = $node->find_all([NS, 'status'])) { | ||
| 37 | my ($http_node) = $status_node->find_all([NS, 'http' ]); | ||
| 38 | my ($next_fetch_node) = $status_node->find_all([NS, 'next_fetch' ]); | ||
| 39 | my ($title_node) = $status_node->find_all([NS, 'title' ]); | ||
| 40 | |||
| 41 | $code = $http_node ? $http_node->attr('code') : undef; | ||
| 42 | $next_fetch = $next_fetch_node ? $next_fetch_node->text : undef; | ||
| 43 | $title = $title_node ? $title_node->text : undef; | ||
| 44 | $feed_uri = $status_node->attr('feed'); | ||
| 45 | |||
| 46 | my $status = { | ||
| 47 | http_status => $code, | ||
| 48 | next_fecth => $next_fetch, | ||
| 49 | title => $title, | ||
| 50 | feed_uri => $feed_uri, | ||
| 51 | }; | ||
| 52 | $self->event(superfeedr_status => $status); | ||
| 53 | } | ||
| 54 | if ( my ($q) = $node->find_all([qw/ pubsub_ev items /]) ) { | ||
| 55 | foreach ( $q->find_all([ qw/pubsub item /] )) { | ||
| 56 | push @items, $_; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | my $notification = AnyEvent::Superfeedr::Notification->new( | ||
| 60 | http_status => $code, | ||
| 61 | next_fetch => $next_fetch, | ||
| 62 | title => $title, | ||
| 63 | feed_uri => $feed_uri, | ||
| 64 | items => [ @items ], | ||
| 65 | ); | ||
| 66 | $self->event(pubsub_recv => @items); | ||
| 67 | $self->event(superfeedr_notification => $notification); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub subscribe_nodes { | ||
| 71 | my ($self, $con, $uris, $cb) = @_; | ||
| 72 | return $self->_pubsub_nodes('subscribe', $con, $uris, $cb); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub unsubscribe_nodes { | ||
| 76 | my ($self, $con, $uris, $cb) = @_; | ||
| 77 | return $self->_pubsub_nodes('unsubscribe', $con, $uris, $cb); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _pubsub_nodes { | ||
| 81 | my ($self, $method, $con, $uris, $cb) = @_; | ||
| 82 | |||
| 83 | my $jid = $con->jid; | ||
| 84 | |||
| 85 | my @children; | ||
| 86 | my $service; | ||
| 87 | for (@$uris) { | ||
| 88 | ($service, my $node) = split_uri($_); | ||
| 89 | push @children, { | ||
| 90 | name => $method, attrs => [ | ||
| 91 | node => $node, | ||
| 92 | jid => $jid, | ||
| 93 | ], | ||
| 94 | }; | ||
| 95 | } | ||
| 96 | |||
| 97 | $con->send_iq ( | ||
| 98 | set => sub { | ||
| 99 | my ($w) = @_; | ||
| 100 | simxml ($w, defns => 'pubsub', node => { | ||
| 101 | name => 'pubsub', childs => \@children, | ||
| 102 | }); | ||
| 103 | }, | ||
| 104 | sub { | ||
| 105 | my ($node, $err) = @_; | ||
| 106 | $cb->(defined $err ? $err : ()) if $cb; | ||
| 107 | }, | ||
| 108 | (defined $service ? (to => $service) : ()) | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; |