File Coverage

blib/lib/App/Maisha/Plugin/Identica.pm
Criterion Covered Total %
statement 35 135 25.9
branch 4 32 12.5
condition 0 11 0.0
subroutine 9 23 39.1
pod 15 15 100.0
total 63 216 29.1


line stmt bran cond sub pod time code
1             package App::Maisha::Plugin::Identica;
2              
3 7     7   144485 use strict;
  7         16  
  7         172  
4 7     7   30 use warnings;
  7         14  
  7         297  
5              
6             our $VERSION = '0.22';
7              
8             #----------------------------------------------------------------------------
9             # Library Modules
10              
11 7     7   37 use base qw(App::Maisha::Plugin::Base);
  7         53  
  7         1120  
12 7     7   37 use base qw(Class::Accessor::Fast);
  7         32  
  7         814  
13 7     7   3240 use File::Path;
  7         12  
  7         458  
14 7     7   1975 use Net::Twitter;
  7         4843193  
  7         228  
15 7     7   47 use Storable;
  7         14  
  7         7436  
16              
17             #----------------------------------------------------------------------------
18             # Accessors
19              
20             __PACKAGE__->mk_accessors($_) for qw(api users);
21              
22             #----------------------------------------------------------------------------
23             # Public API
24              
25             #http://identi.ca/settings/oauthapps/show/185
26              
27             sub new {
28 1     1 1 22 my $class = shift;
29 1         5 my $self = {
30             consumer_key => 'fb8163dcdb32e6c8e60734e961187553',
31             consumer_secret => '8251970fb08ea39a90f6b494e3a2aabc',
32             };
33              
34 1         2 bless $self, $class;
35 1         3 return $self;
36             }
37              
38             sub login {
39 1     1 1 3 my ($self,$config) = @_;
40 1         2 my $api;
41              
42 1 50       8 unless($config->{username}) { warn "No username supplied\n"; return }
  0         0  
  0         0  
43              
44 1         4 eval {
45             $api = Net::Twitter->new(
46             traits => [qw/API::Search API::REST OAuth/],
47             consumer_key => $self->{consumer_key},
48             consumer_secret => $self->{consumer_secret},
49 1         13 identica => 1,
50             ssl => 1
51             );
52             };
53              
54 1 50       1826 unless($api) {
55 0         0 warn "Unable to establish connection to Identica API\n";
56 0         0 return 0;
57             }
58              
59             # for testing purposes we don't want to login
60 1 50       4 if(!$config->{test}) {
61 0         0 eval {
62 0         0 my $datafile = $config->{home} . '/.maisha/identica.dat';
63 0   0     0 my $access_tokens = eval { retrieve($datafile) } || {};
64              
65 0 0 0     0 if ( $access_tokens && $access_tokens->{$config->{username}}) {
66 0         0 $api->access_token($access_tokens->{$config->{username}}->[0]);
67 0         0 $api->access_token_secret($access_tokens->{$config->{username}}->[1]);
68             } else {
69 0         0 my $auth_url = $api->get_authorization_url;
70 0         0 print " Authorize this application at: $auth_url\nThen, enter the PIN# provided to continue: ";
71              
72 0         0 my $pin = <STDIN>; # wait for input
73 0         0 chomp $pin;
74              
75 0 0       0 unless($pin) {
76 0         0 warn "No PIN provided, Maisha will not be able to access Identica account until authorized to do so\n";
77 0         0 return 0;
78             }
79              
80             # request_access_token stores the tokens in $nt AND returns them
81 0         0 my $access_tokens = {};
82 0         0 my @access_tokens;
83 0         0 eval { @access_tokens = $api->request_access_token(verifier => $pin) };
  0         0  
84 0 0       0 unless(@access_tokens) {
85 0         0 warn "Invalid PIN provided, Maisha will not be able to access your Identica account until authorized to do so\n";
86 0         0 return 0;
87             }
88 0         0 $access_tokens->{$config->{username}} = \@access_tokens;
89              
90 0         0 mkpath( $config->{home} . '/.maisha' );
91              
92             # save the access tokens
93 0         0 store $access_tokens, $datafile;
94 0         0 chmod 0640, $datafile; # make sure it has reasonable permissions
95             }
96             };
97              
98 0 0       0 if($@) {
99 0         0 warn "Unable to login to Identica\n";
100 0         0 return 0;
101             }
102             }
103              
104 1         18 $self->api($api);
105              
106 1 50       10 if(!$config->{test}) {
107 0         0 print "...building user cache for Identica\n";
108 0         0 $self->_build_users();
109             }
110              
111 1         3 return 1;
112             }
113              
114             sub _build_users {
115 0     0     my $self = shift;
116 0           my %users;
117              
118 0           eval {
119 0           my $f = $self->api->friends();
120 0 0 0       if($f && @$f) { for(@$f) { next unless($_); $users{$_->{screen_name}} = 1 } }
  0 0          
  0            
  0            
121 0           $f = $self->api->followers();
122 0 0 0       if($f && @$f) { for(@$f) { next unless($_); $users{$_->{screen_name}} = 1 } }
  0 0          
  0            
  0            
123             };
124              
125 0           $self->users(\%users);
126             }
127              
128             sub api_reauthorize {
129 0     0 1   my $self = shift;
130 0           my $config = $self->config;
131 0           my $api = $self->api;
132              
133             # for testing purposes we don't want to login
134 0 0         if(!$config->{test}) {
135 0           eval {
136 0           my $datafile = $config->{home} . '/.maisha/identica.dat';
137              
138 0           my $auth_url = $api->get_authorization_url;
139 0           print "Please re-authorize this application at: $auth_url\nThen, enter the PIN# provided to continue: ";
140              
141 0           my $pin = <STDIN>; # wait for input
142 0           chomp $pin;
143              
144 0 0         unless($pin) {
145 0           warn "No PIN provided, Maisha will not be able to access direct messages until reauthorization is completed\n";
146 0           return 0;
147             }
148              
149             # request_access_token stores the tokens in $nt AND returns them
150 0           my $access_tokens = {};
151 0           my @access_tokens;
152 0           eval { @access_tokens = $api->request_access_token(verifier => $pin) };
  0            
153 0 0         unless(@access_tokens) {
154 0           warn "Invalid PIN provided, Maisha will not be able to access direct messages until reauthorization is completed\n";
155 0           return 0;
156             }
157 0           $access_tokens->{$config->{username}} = \@access_tokens;
158              
159 0           unlink $datafile;
160 0           mkpath( $config->{home} . '/.maisha' );
161              
162             # save the access tokens
163 0           store $access_tokens, $datafile;
164 0           chmod 0640, $datafile; # make sure it has reasonable permissions
165             };
166              
167 0 0         if($@) {
168 0           warn "Unable to login to Identica\n";
169 0           return 0;
170             }
171             }
172              
173 0           return 1;
174             }
175              
176             sub api_user {
177 0     0 1   my $self = shift;
178 0           $self->api->show_user(@_);
179             }
180              
181             sub api_user_timeline {
182 0     0 1   my $self = shift;
183 0           $self->api->user_timeline(@_);
184             }
185              
186             sub api_friends {
187 0     0 1   my $self = shift;
188 0           $self->api->following(@_);
189             }
190              
191             sub api_friends_timeline {
192 0     0 1   my $self = shift;
193 0           $self->api->following_timeline(@_);
194             }
195              
196             sub api_public_timeline {
197 0     0 1   my $self = shift;
198 0           $self->api->public_timeline(@_);
199             }
200              
201             sub api_followers {
202 0     0 1   my $self = shift;
203 0           $self->api->followers(@_);
204             }
205              
206             sub api_update {
207 0     0 1   my $self = shift;
208 0           $self->api->update(@_);
209             }
210              
211             sub api_replies {
212 0     0 1   my $self = shift;
213 0           $self->api->replies(@_);
214             }
215              
216             sub api_send_message {
217 0     0 1   my $self = shift;
218 0           $self->api->new_direct_message(@_);
219             }
220              
221             sub api_direct_messages_to {
222 0     0 1   my $self = shift;
223 0           $self->api->direct_messages(@_);
224             }
225              
226             sub api_direct_messages_from {
227 0     0 1   my $self = shift;
228 0           $self->api->sent_direct_messages(@_);
229             }
230              
231             sub api_search {
232 0     0 1   my $self = shift;
233 0           $self->api->search(@_);
234             }
235              
236             1;
237              
238             __END__
239              
240             =head1 NAME
241              
242             App::Maisha::Plugin::Identica - Maisha interface to Identi.ca
243              
244             =head1 SYNOPSIS
245              
246             maisha
247             maisha> use Identica
248             use ok
249              
250             =head1 DESCRIPTION
251              
252             App::Maisha::Plugin::Identica is the gateway for Maisha to access the Identi.ca
253             API.
254              
255             =head1 METHODS
256              
257             =head2 Constructor
258              
259             =over 4
260              
261             =item * new
262              
263             =back
264              
265             =head2 Process Methods
266              
267             =over 4
268              
269             =item * login
270              
271             Login to the service.
272              
273             =back
274              
275             =head2 API Methods
276              
277             The API methods are used to interface to with the Identica API.
278              
279             =over 4
280              
281             =item * api_reauthorize
282              
283             =item * api_user
284              
285             =item * api_user_timeline
286              
287             =item * api_friends
288              
289             =item * api_friends_timeline
290              
291             =item * api_public_timeline
292              
293             =item * api_followers
294              
295             =item * api_update
296              
297             =item * api_replies
298              
299             =item * api_send_message
300              
301             =item * api_direct_messages_to
302              
303             =item * api_direct_messages_from
304              
305             =item * api_search
306              
307             =back
308              
309             =head1 AUTHENTICATION
310              
311             As Twitter has now disabled Basic Authentication to access their API, to be
312             consistent Maisha now requires that access to both Twitter and Identica uses
313             OAuth.
314              
315             With this new method of authentication, the application will provide a URL,
316             which the user needs to cut-n-paste into a browser to logging in to the
317             service, using your regular username/password, then 'Allow' Maisha to access
318             your account. This will then allow Maisha to post to your account. You will
319             then be given a PIN, which should then be entered at the prompt on the Maisha
320             command line.
321              
322             Once you have completed authentication, the application will then store your
323             access tokens permanently under your profile on your computer. Then when you
324             next use the application it will retrieve these access tokens automatically and
325             you will no longer need to register the application.
326              
327             For further information please see the Identica FAQ -
328             L<http://status.net/wiki/TwitterCompatibleAPI>
329              
330             =head1 SEE ALSO
331              
332             For further information regarding the commands and configuration, please see
333             the 'maisha' script included with this distribution.
334              
335             L<App::Maisha>
336              
337             L<Net::Identica>
338              
339             =head1 WEBSITES
340              
341             =over 4
342              
343             =item * Main Site: L<http://maisha.grango.org>
344              
345             =item * Git Repo: L<http://github.com/barbie/maisha/tree/master>
346              
347             =item * RT Queue: L<RT: http://rt.cpan.org/Public/Dist/Display.html?Name=App-Maisha>
348              
349             =back
350              
351             =head1 AUTHOR
352              
353             Barbie, <barbie@cpan.org>
354             for Miss Barbell Productions <http://www.missbarbell.co.uk>.
355              
356             =head1 COPYRIGHT AND LICENSE
357              
358             Copyright (C) 2009-2019 by Barbie
359              
360             This distribution is free software; you can redistribute it and/or
361             modify it under the Artistic License v2.
362              
363             =cut