| 1 | package Net::Twitter::Cabal; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Class::Accessor::Fast'; | ||
| 7 | __PACKAGE__->mk_accessors( qw/ | ||
| 8 | config | ||
| 9 | / | ||
| 10 | ); | ||
| 11 | |||
| 12 | use Carp; | ||
| 13 | |||
| 14 | use Encode; | ||
| 15 | binmode STDOUT, ":utf8"; | ||
| 16 | |||
| 17 | use Log::Log4perl ':easy'; | ||
| 18 | Log::Log4perl->easy_init( $DEBUG ); | ||
| 19 | |||
| 20 | use AnyEvent::XMPP::Client; | ||
| 21 | use AnyEvent::XMPP::Util qw / split_jid /; | ||
| 22 | use AnyEvent::XMPP::Ext::Disco; | ||
| 23 | use AnyEvent::XMPP::Ext::Version; | ||
| 24 | use AnyEvent::XMPP::Ext::VCard; | ||
| 25 | use AnyEvent::XMPP::Namespaces 'xmpp_ns'; | ||
| 26 | use AnyEvent; | ||
| 27 | |||
| 28 | use Net::Twitter; | ||
| 29 | use Net::Twitter::Cabal::Config; | ||
| 30 | use Net::Twitter::Cabal::Tweet; | ||
| 31 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | Net::Twitter::Cabal - Manage posters to a Twitter stream | ||
| 35 | |||
| 36 | You have a Twitter stream than can be updated by several people. Dealing with | ||
| 37 | authorisation is painful, and there's no way to identify a poster. | ||
| 38 | |||
| 39 | Net::Twitter::Cabal let's you manage this stream from a central point, where | ||
| 40 | you have full control of who can post and without the need to distribute the | ||
| 41 | account's credentials. | ||
| 42 | |||
| 43 | =head1 VERSION | ||
| 44 | |||
| 45 | Version 0.03 | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | our $VERSION = '0.03'; | ||
| 50 | |||
| 51 | |||
| 52 | =head1 SYNOPSIS | ||
| 53 | |||
| 54 | Using Net::Twitter::Cabal you can create centralised endpoints for a group | ||
| 55 | of people to post to a single Twitter stream: | ||
| 56 | |||
| 57 | use Net::Twitter::Cabal; | ||
| 58 | |||
| 59 | my $conspiracy = Net::Twitter::Cabal->new( { | ||
| 60 | 'config' => 'config.yml', | ||
| 61 | } ); | ||
| 62 | |||
| 63 | $conspiracy->run; | ||
| 64 | |||
| 65 | =head1 FUNCTIONS | ||
| 66 | |||
| 67 | =head2 new | ||
| 68 | |||
| 69 | Create a new cabal and initialise the configuration. | ||
| 70 | |||
| 71 | my $conspiracy = Net::Twitter::Cabal->new( { | ||
| 72 | 'config' => 'config.yml'; | ||
| 73 | } ); | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub new { | ||
| 78 | my $proto = shift; | ||
| 79 | my $class = ref $proto || $proto; | ||
| 80 | |||
| 81 | my $self = $class->SUPER::new; | ||
| 82 | |||
| 83 | my $args = shift; | ||
| 84 | |||
| 85 | my $cfgfile = defined $args->{'config'}? $args->{'config'} : undef; | ||
| 86 | my $config = Net::Twitter::Cabal::Config->new( { file => $cfgfile } ); | ||
| 87 | |||
| 88 | $self->config( $config ); | ||
| 89 | |||
| 90 | return $self; | ||
| 91 | } | ||
| 92 | |||
| 93 | =head2 run | ||
| 94 | |||
| 95 | Start the Cabal: | ||
| 96 | |||
| 97 | $conspiracy->run; | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub run { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | my $j = AnyEvent->condvar; | ||
| 105 | my $c = AnyEvent::XMPP::Client->new( debug => 1 ); | ||
| 106 | |||
| 107 | my $c_args = { | ||
| 108 | initial_presence => undef, | ||
| 109 | dont_retrieve_roster => 1, | ||
| 110 | }; | ||
| 111 | |||
| 112 | my $disco = AnyEvent::XMPP::Ext::Disco->new; | ||
| 113 | my $version = AnyEvent::XMPP::Ext::Version->new; | ||
| 114 | $c->add_extension( $disco ); | ||
| 115 | $c->add_extension( $version ); | ||
| 116 | |||
| 117 | my $jid = $self->config->jid; | ||
| 118 | my $pwd = $self->config->password; | ||
| 119 | |||
| 120 | my $twitter = Net::Twitter->new( | ||
| 121 | source => 'cabal', | ||
| 122 | username => $self->config->twitter, | ||
| 123 | password => $self->config->twitterpw, | ||
| 124 | ); | ||
| 125 | |||
| 126 | $c->add_account( $jid, $pwd, undef, undef, $c_args ); | ||
| 127 | my $vcard = AnyEvent::XMPP::Ext::VCard->new; | ||
| 128 | |||
| 129 | $c->reg_cb( | ||
| 130 | stream_ready => sub { | ||
| 131 | my ( $cl, $acc ) = @_; | ||
| 132 | $vcard->hook_on( $acc->connection, 1 ); | ||
| 133 | }, | ||
| 134 | |||
| 135 | session_ready => sub { | ||
| 136 | my ( $cl, $acc ) = @_; | ||
| 137 | INFO $acc->jid . " connected."; | ||
| 138 | |||
| 139 | $vcard->store( $acc->connection, $self->_vcard, sub { | ||
| 140 | if ( $_[0] ) { | ||
| 141 | WARN "Couldn't store vcard: " . $_[0]->string; | ||
| 142 | $cl->finish; | ||
| 143 | } | ||
| 144 | } ); | ||
| 145 | |||
| 146 | $cl->set_presence( | ||
| 147 | undef, | ||
| 148 | "Accepting tweets for " . $self->config->name, | ||
| 149 | 5 | ||
| 150 | ); | ||
| 151 | |||
| 152 | $self->_update_roster( $acc ); | ||
| 153 | }, | ||
| 154 | |||
| 155 | error => sub { | ||
| 156 | my ( $cl, $acc, $err ) = @_; | ||
| 157 | ERROR $err->string; | ||
| 158 | $j->broadcast; | ||
| 159 | }, | ||
| 160 | |||
| 161 | disconnect => sub { | ||
| 162 | WARN "Disconnected: [@_]"; | ||
| 163 | $j->broadcast; | ||
| 164 | }, | ||
| 165 | |||
| 166 | message => sub { $self->_got_message( @_, $twitter->clone ); }, | ||
| 167 | |||
| 168 | contact_request_subscribe => sub { $self->_got_subs_req( @_ ); }, | ||
| 169 | |||
| 170 | contact_subscribed => sub { | ||
| 171 | my ( $cl, $acc, $roster, $contact ) = @_; | ||
| 172 | |||
| 173 | $cl->send_message( AnyEvent::XMPP::IM::Message->new( | ||
| 174 | to => $contact->jid, | ||
| 175 | body => "Hi There! Ready to post your tweets to " . | ||
| 176 | $self->config->name . "." | ||
| 177 | ) ); | ||
| 178 | }, | ||
| 179 | |||
| 180 | contact_did_unsubscribe => sub { | ||
| 181 | my ( $cl, $acc, $roster, $contact ) = @_; | ||
| 182 | my $jid = $contact->jid; | ||
| 183 | INFO "$jid unsubscribed from us"; | ||
| 184 | }, | ||
| 185 | |||
| 186 | contact_unsubscribed => sub { | ||
| 187 | my ( $cl, $acc, $roster, $contact ) = @_; | ||
| 188 | my $jid = $contact->jid; | ||
| 189 | WARN "$jid unsubscribed us"; | ||
| 190 | }, | ||
| 191 | |||
| 192 | roster_update => sub { $j->broadcast; }, | ||
| 193 | ); | ||
| 194 | |||
| 195 | $c->start; | ||
| 196 | $j->wait; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub _got_message { | ||
| 200 | my ( $self, $cl, $acc, $msg, $twitter ) = @_; | ||
| 201 | |||
| 202 | my ( $user, $domain, $resource ) = split_jid( $msg->from ); | ||
| 203 | my $jid = $user . '@' . $domain; | ||
| 204 | my $nick = $self->config->accept->{$jid}; | ||
| 205 | if ( ! defined $nick ) { | ||
| 206 | $cl->send_message( AnyEvent::XMPP::IM::Message->new( | ||
| 207 | to => $msg->from, | ||
| 208 | body => "Sorry, you're not on the list.", | ||
| 209 | ) ); | ||
| 210 | $self->_unsubscribe_from( $acc, $jid ); | ||
| 211 | return; | ||
| 212 | } | ||
| 213 | |||
| 214 | my $text = $msg->any_body; | ||
| 215 | INFO "[$nick]: " . $text; | ||
| 216 | |||
| 217 | my $tweet = Net::Twitter::Cabal::Tweet->new( { | ||
| 218 | poster => $nick, | ||
| 219 | content => "[$nick]: $text", | ||
| 220 | } ); | ||
| 221 | my $res = $twitter->update( $tweet->content ); | ||
| 222 | |||
| 223 | if ( ! defined $res ) { | ||
| 224 | $cl->send_message( AnyEvent::XMPP::IM::Message->new( | ||
| 225 | to => $msg->from, | ||
| 226 | body => "Urgh. Your message '$text' wasn't posted." | ||
| 227 | ) ); | ||
| 228 | } else { | ||
| 229 | $cl->send_message( AnyEvent::XMPP::IM::Message->new( | ||
| 230 | to => $msg->from, | ||
| 231 | body => "Message sent." | ||
| 232 | ) ); | ||
| 233 | } | ||
| 234 | 0; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub _got_subs_req { | ||
| 238 | my ( $self, $cl, $acc, $roster, $contact ) = @_; | ||
| 239 | my $jid = $contact->jid; | ||
| 240 | INFO "Subscription request from $jid"; | ||
| 241 | |||
| 242 | if ( exists $self->config->accept->{$jid} ) { | ||
| 243 | $contact->send_subscribed; | ||
| 244 | $contact->send_subscribe; | ||
| 245 | INFO "Subscribed to $jid"; | ||
| 246 | } else { | ||
| 247 | $contact->send_unsubscribed; | ||
| 248 | INFO "Refused subscription from $jid"; | ||
| 249 | } | ||
| 250 | |||
| 251 | 0; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _unsubscribe_from { | ||
| 255 | my ( $self, $acc, $from ) = @_; | ||
| 256 | |||
| 257 | my $roster = $acc->connection->roster; | ||
| 258 | my $contact = $roster->get_contact( $from ); | ||
| 259 | |||
| 260 | if ( $contact ) { | ||
| 261 | $contact->send_unsubscribed; | ||
| 262 | $contact->send_unsubscribe; | ||
| 263 | } | ||
| 264 | |||
| 265 | INFO "Unsubscribed '$from', which isn't in my accept list."; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _update_roster { | ||
| 269 | my ( $self, $acc ) = @_; | ||
| 270 | |||
| 271 | my $roster = $acc->connection->roster; | ||
| 272 | for my $contact ( $roster->get_contacts ) { | ||
| 273 | my ( $user, $domain, $resource ) = split_jid( $contact->jid ); | ||
| 274 | my $jid = $user . '@' . $domain; | ||
| 275 | if ( ! exists $self->config->accept->{$jid} ) { | ||
| 276 | $self->_unsubscribe_from( $acc, $jid ); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | sub _vcard { | ||
| 282 | my $self = shift; | ||
| 283 | my %vcard; | ||
| 284 | |||
| 285 | $vcard{'URL'} = [ $self->config->url ] | ||
| 286 | if $self->config->url; | ||
| 287 | $vcard{'FN'} = [ $self->config->name ]; | ||
| 288 | |||
| 289 | $vcard{'_avatar_type'} = $self->config->avatar->{'type'}; | ||
| 290 | $vcard{'_avatar'} = $self->config->avatar->{'image'}; | ||
| 291 | |||
| 292 | return \%vcard; | ||
| 293 | } | ||
| 294 | |||
| 295 | =head1 AUTHOR | ||
| 296 | |||
| 297 | Pedro Figueiredo, C<< <me at pedrofigueiredo.org> >> | ||
| 298 | |||
| 299 | =head1 BUGS | ||
| 300 | |||
| 301 | Please report any bugs or feature requests to C<bug-net-twitter-cabal at rt.cpan.org>, or through | ||
| 302 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Twitter-Cabal>. I will be notified, and then you'll | ||
| 303 | automatically be notified of progress on your bug as I make changes. | ||
| 304 | |||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | =head1 SUPPORT | ||
| 309 | |||
| 310 | You can find documentation for this module with the perldoc command. | ||
| 311 | |||
| 312 | perldoc Net::Twitter::Cabal | ||
| 313 | |||
| 314 | |||
| 315 | You can also look for information at: | ||
| 316 | |||
| 317 | =over 4 | ||
| 318 | |||
| 319 | =item * RT: CPAN's request tracker | ||
| 320 | |||
| 321 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Twitter-Cabal> | ||
| 322 | |||
| 323 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 324 | |||
| 325 | L<http://annocpan.org/dist/Net-Twitter-Cabal> | ||
| 326 | |||
| 327 | =item * CPAN Ratings | ||
| 328 | |||
| 329 | L<http://cpanratings.perl.org/d/Net-Twitter-Cabal> | ||
| 330 | |||
| 331 | =item * Search CPAN | ||
| 332 | |||
| 333 | L<http://search.cpan.org/dist/Net-Twitter-Cabal/> | ||
| 334 | |||
| 335 | =back | ||
| 336 | |||
| 337 | |||
| 338 | =head1 ACKNOWLEDGEMENTS | ||
| 339 | |||
| 340 | =over 4 | ||
| 341 | |||
| 342 | =item * Pedro Melo, for suggestions, testing, and holding my hand wrt XMPP | ||
| 343 | |||
| 344 | =item * Robin Redeker, author of AnyEvent::XMPP, for listening to my whining | ||
| 345 | and clearing my doubts | ||
| 346 | |||
| 347 | =item * Nuno Nunes, for testing | ||
| 348 | |||
| 349 | =back | ||
| 350 | |||
| 351 | =head1 COPYRIGHT & LICENSE | ||
| 352 | |||
| 353 | Copyright 2009 Pedro Figueiredo, all rights reserved. | ||
| 354 | |||
| 355 | This program is free software; you can redistribute it and/or modify it | ||
| 356 | under the same terms as Perl itself. | ||
| 357 | |||
| 358 | |||
| 359 | =cut | ||
| 360 | |||
| 361 | 45; # End of Net::Twitter::Cabal |