blib/lib/Bot/BasicBot/Pluggable/Module/TwitterFriendsStatus.pm

filter: Statementstmt 39.4% 28 / 71 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 61.5% 8 / 13 Podpod 100% 4 / 4 totaltotal 33.0% 40 / 121 CC 17 SCAR 31.0
CC
17
Cov
28%
_twitter_friends_status
42.0
said
32.8
tick
30.0
CRAP 22.2
↑ Summary
1 #==============================================================================
2 #
3 # Bot::BasicBot::Pluggable::Module::TwitterFriendsStatus
4 #
5 # DESCRIPTION
6 #
7 # Adds ability for Bot::BasicBot::Pluggable IRC bots to check for comments
8 # posted to Twitter for a list of users and echo them into a channel.
9 #
10 # AUTHORS
11 # Gryphon Shafer <gryphon@cpan.org>
12 # David Precious <davidp@preshweb.co.uk>
13 #
14 # COPYRIGHT
15 # Copyright (C) 2008 by Gryphon Shafer
16 #
17 # This library is free software; you can redistribute it and/or modify it
18 # under the same terms as Perl itself.
19 #
20 #==============================================================================
21  
22 package Bot::BasicBot::Pluggable::Module::TwitterFriendsStatus;
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use XML::Simple 'XMLin';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Bot::BasicBot::Pluggable::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use base 'Bot::BasicBot::Pluggable::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 our $VERSION = 0.2;
31  
32 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 CREDENTIALS => {
34 'netloc' => 'twitter.com:80',
35 'realm' => 'Twitter API',
36 },
37 URL => {
38 'verify_credentials' => 'http://twitter.com/account/verify_credentials.xml',
39 'friends_status' => 'http://twitter.com/statuses/friends_timeline.xml',
40 'friend_add' => [ 'http://twitter.com/friendships/create/', '.xml' ],
41 'friend_remove' => [ 'http://twitter.com/friendships/destroy/', '.xml' ],
42 },
43 TICK_INTERVAL => 60,
44 1 };
45  
46 1 my $tick_counter = TICK_INTERVAL;
47  
48 1 my $ua = LWP::UserAgent->new(
49 'agent' => 'Bot::BasicBot::Pluggable::Module::TwitterFriendsStatus/0.1 ',
50 'max_redirect' => 7,
51 'parse_head' => 1,
52 );
53  
54 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
55 0 return join( ', ',
56 'twitter add <twittername>',
57 'twitter remove <twittername>',
58 '!twitterauth <username> <password>',
59 );
60 }
61  
62 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
63 0 my ($self) = @_;
64 0 my ( $username, $password ) = ( $self->get('username'), $self->get('password') );
65  
66 0 ▶ if ( $username and $password ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($username and $password) 0 0 0
Condition0%0/3
$username and $password
!ll&&!rl&&r
000
MC/DC0%0/2
$username and $password
$username $password
Truth table0%0/3
A and B
A= $usernameB= $password
ABresult
0X0
100
111
67 0 print "Twitter authentication previously successful. Using auth for $username\n";
68  
69 $ua->credentials(
70 CREDENTIALS->{'netloc'},
71 0 CREDENTIALS->{'realm'},
72 $username,
73 $password,
74 );
75 }
76  
77 0 _twitter_friends_status( $self, 0 );
78 }
79  
80 0 ▶sub said {
Subroutine said: not called CC=6 SCAR=34.9
Pod documented
81 0 my ( $self, $message, $priority ) = @_;
82 0 ▶ return unless ( $priority == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless $priority == 2 0 0 0
83  
84 0 ▶ if (my($user,$pass) = $message->{'body'} =~ /^\s*!\s*twitter\s*auth\s*(\S+)\s+(\S+)/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if (() = $message->{'body'} =~ /^\s*!\s*twitter\s*auth\s*(\S+)\s+(\S+)/) { } 0 0 0
elsif ($message->{'body'} =~ /^\s*twitter\s*add\s*(\S+)/) { } 0 0 0
elsif ($message->{'body'} =~ /^\s*twitter\s*remove\s*(\S+)/) { } 0 0 0
85 0 $ua->credentials( CREDENTIALS->{'netloc'}, CREDENTIALS->{'realm'}, $user, $pass );
86 0 my $response = $ua->get( URL->{'verify_credentials'} );
87 0 my $credentials = XMLin($response->content());
88 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 0 warn Dumper($credentials);
90 0 ▶ if ( $credentials->{name} eq $user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($credentials->{'name'} eq $user) { } 0 0 0
91 0 $self->reply(
92 $message,
93 "You have properly authenticated to Twitter as $user\n" .
94 "This authentication will be saved in the bot memory storage.\n" .
95 "$user has $credentials->{followers_count} followers and" .
96 "follows $credentials->{friends_count} users.\n"
97 );
98 0 $self->set( 'username' => $1 );
99 0 $self->set( 'password' => $2 );
100 }
101 else {
102 0 $self->reply(
103 $message,
104 "You have failed to properly authenticate with Twitter as $user",
105 );
106 0 $self->set( 'username' => '' );
107 0 $self->set( 'password' => '' );
108 }
109 }
110  
111 elsif ( $message->{'body'} =~ /^\s*twitter\s*add\s*(\S+)/ ) {
112 0 my $response = $ua->post( URL->{'friend_add'}[0] . $1 . URL->{'friend_add'}[1] );
113 0 $self->reply(
114 $message,
115 "Added $1 to my Twitter following list.",
116 );
117 }
118  
119 elsif ( $message->{'body'} =~ /^\s*twitter\s*remove\s*(\S+)/ ) {
120 0 my $response = $ua->post( URL->{'friend_remove'}[0] . $1 . URL->{'friend_remove'}[1] );
121 0 $self->reply(
122 $message,
123 "Removed $1 from my Twitter following list.",
124 );
125 }
126  
127 0 return;
128 }
129  
130 0 ▶sub tick {
Subroutine tick: not called CC=5 SCAR=34.0
Pod documented
131 0 my ($self) = @_;
132  
133 0 $tick_counter++;
134 0 ▶ return if ( $tick_counter < TICK_INTERVAL );
Branch: 1 branch
BranchTrueFalseTotal
if $tick_counter < 60 0 0 0
135 0 $tick_counter = 0;
136  
137 0 _twitter_friends_status( $self, 1 );
138  
139 0 foreach ( $self->store_keys() ) {
140 0 ▶ $self->unset($_) if ( $_ =~ /^\d+$/ and $self->get($_) + 60 * 60 * 50 < time );
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^\d+$/ and $self->get($_) + 180000 < time 0 0 0
Condition0%0/3
$_ =~ /^\d+$/ and $self->get($_) + 180000 < time
!ll&&!rl&&r
000
MC/DC0%0/2
$_ =~ /^\d+$/ and $self->get($_) + 180000 < time
$_ =~ /^\d+$/ $self->get($_) + 180000 < time
Truth table0%0/3
A and B
A= $_ =~ /^\d+$/B= $self->get($_) + 180000 < time
ABresult
0X0
100
111
141 }
142 }
143  
144 0 ▶sub _twitter_friends_status {
Subroutine _twitter_friends_status: not called CC=6 SCAR=37.4
145 0 my ( $self, $do_tell ) = @_;
146  
147 0 my $response = $ua->get( URL->{'friends_status'} );
148 0 my $friends_status = XMLin(
149 $response->content(),
150 'ForceArray' => 1,
151 'KeyAttr' => [],
152 );
153  
154 0 foreach my $status (
155 0 grep { $_->{'user'}[0]{'name'}[0] ne 'woprircbot' }
156 0 @{ $friends_status->{'status'} }
157 ) {
158 0 ▶ unless ( $self->get( $status->{'id'}[0] ) || 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get($status->{"id"}[0]) or 0) 0 0 0
Condition0%0/2
$self->get($status->{"id"}[0]) or 0
l!l
00
MC/DC0%0/1
$self->get($status->{"id"}[0]) or 0
$self->get($status->{"id"}[0])
Truth table0%0/2
A
A= $self->get($status->{"id"}[0])
Aresult
11
00
159 0 $self->set( $status->{'id'}[0], time );
160 0 ▶ if ($do_tell) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_tell) 0 0 0
161 $self->tell(
162 $_,
163 sprintf( "[Twitter %s] %s", $status->{'user'}[0]{'name'}[0], $status->{'text'}[0] ),
164 0 ) foreach ( $self->bot()->channels() );
165 }
166 }
167 }
168 }
169  
170 1 1;
171 __END__
↑ Summary