Plagger/Plugin/Notify/Campfire.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 106 CC 9 SCAR 29.9
CC
9
Cov
0%
login
56.0
initialize
6.0
speak
6.0
CRAP 19.9
1 package Plagger::Plugin::Notify::Campfire;
2  
3 use strict;
4 use base qw( Plagger::Plugin );
5 use Time::HiRes;
6  
7 our $VERSION = 0.01;
8  
9 sub plugin_id {
10 my $self = shift;
11 $self->class_id . '-' . $self->conf->{email};
12 }
13  
14 sub register {
15 my ( $self, $context ) = @_;
16 $context->register_hook(
17 $self,
18 'publish.init' => \&initialize,
19 'publish.entry' => \&publish_entry,
20 );
21 }
22  
23 sub initialize {
24 my ( $self, $context ) = @_;
25 $self->{campfire} =
26 Plagger::Plugin::Notify::Campfire::Mechanize->new($self);
27 unless ( $self->{campfire}->login ) {
28 $context->log( error => "Login to Campfire failed." );
29 return;
30 }
31 $context->log( info => 'Login to Campfire succeeded.' );
32 }
33  
34 sub publish_entry {
35 my ( $self, $context, $args ) = @_;
36 $self->{campfire}->speak( $args->{entry}->title );
37 $self->{campfire}->speak( $args->{entry}->link );
38 $context->log( info => 'Speak: ' . $args->{entry}->title );
39 Time::HiRes::sleep( $self->conf->{speak_interval} || 2 );
40 }
41  
42 package Plagger::Plugin::Notify::Campfire::Mechanize;
43  
44 use strict;
45 use Plagger::Mechanize;
46 use HTTP::Request::Common;
47 use Encode;
48  
49 sub new {
50 my $class = shift;
51 my $plugin = shift;
52  
53 my $mech = Plagger::Mechanize->new(cookie_jar => $plugin->cookie_jar);
54 $mech->agent_alias("Windows IE 6");
55  
56 bless {
57 mecha => $mech,
58 nickname => $plugin->conf->{nickname},
59 email => $plugin->conf->{email},
60 password => $plugin->conf->{password},
61 room_url => $plugin->conf->{room_url},
62 guest_url => $plugin->conf->{guest_url},
63 }, $class;
64 }
65  
66 sub login {
67 my $self = shift;
68  
69 my $start_url = $self->{guest_url} || $self->{room_url};
70 my $res = $self->{mecha}->get($start_url);
71 return 0 unless $self->{mecha}->success;
72  
73 # still login
74 return 1 if ( $self->{mecha}->content =~ /chat-wrapper/);
75  
76 if ( $self->{guest_url} ) {
77 $self->{mecha}->submit_form(
78 fields => {
79 name => $self->{nickname},
80 remember => 1,
81 },
82 );
83 }
84 else {
85 $self->{mecha}->submit_form(
86 fields => {
87 email_address => $self->{email},
88 password => $self->{password},
89 remember => 1,
90 },
91 );
92 }
93 $self->{mecha}->submit;
94 return 0 unless $self->{mecha}->success;
95 return 0 if $self->{mecha}->content =~ /Oops/;
96  
97 unless ( $self->{room_url} ) {
98 $self->{room_url} = $self->{guest_url};
99 my ( $room_no, ) = $self->{mecha}->content =~ /participant_list-(\d+)/;
100 $self->{room_url} =~ s!/\w+$!/room/$room_no!;
101 }
102  
103 }
104  
105 sub speak {
106 my ( $self, $message ) = @_;
107 $self->{mecha}->request(
108 POST $self->{room_url} . "/speak",
109 [ message => encode( 'utf-8', $message ) ]
110 );
111 return 0 unless $self->{mecha}->success;
112 }
113  
114 1;
115  
116 __END__