root/trunk/plagger/lib/Plagger/Plugin/Notify/Campfire.pm

Revision 242 (checked in by miyagawa, 14 years ago)

Campfire: remember me!

Line 
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 register {
10     my ( $self, $context ) = @_;
11     $context->register_hook(
12         $self,
13         'publish.init'  => \&initialize,
14         'publish.entry' => \&publish_entry,
15     );
16 }
17
18 sub initialize {
19     my ( $self, $context ) = @_;
20     $self->{campfire} =
21       Plagger::Plugin::Notify::Campfire::Mechanize->new($self);
22     unless ( $self->{campfire}->login ) {
23         $context->log( error => "Login to Campfire failed." );
24         return;
25     }
26     $context->log( info => 'Login to Campfire succeeded.' );
27 }
28
29 sub publish_entry {
30     my ( $self, $context, $args ) = @_;
31     $self->{campfire}->speak( $args->{entry}->title );
32     $self->{campfire}->speak( $args->{entry}->link );
33     $context->log( info => 'Speak: ' . $args->{entry}->title );
34     Time::HiRes::sleep( $self->conf->{speak_interval} || 2 );
35 }
36
37 package Plagger::Plugin::Notify::Campfire::Mechanize;
38
39 use strict;
40 use WWW::Mechanize;
41 use HTTP::Request::Common;
42 use Encode;
43
44 sub new {
45     my $class  = shift;
46     my $plugin = shift;
47
48     my $mech = WWW::Mechanize->new(cookie_jar => $plugin->cache->cookie_jar);
49     $mech->agent_alias("Windows IE 6");
50
51     bless {
52         mecha      => $mech,
53         nickname   => $plugin->conf->{nickname},
54         email      => $plugin->conf->{email},
55         password   => $plugin->conf->{password},
56         room_url   => $plugin->conf->{room_url},
57         guest_url  => $plugin->conf->{guest_url},
58     }, $class;
59 }
60
61 sub login {
62     my $self = shift;
63
64     my $start_url = $self->{guest_url} || $self->{room_url};
65     my $res = $self->{mecha}->get($start_url);
66     return 0 unless $self->{mecha}->success;
67
68     # still login
69     return 1 if ( $self->{mecha}->content =~ /chat-wrapper/);
70
71     if ( $self->{guest_url} ) {
72         $self->{mecha}->submit_form(
73             fields => {
74                 name => $self->{nickname},
75                 remember => 1,
76             },
77         );
78     }
79     else {
80         $self->{mecha}->submit_form(
81             fields => {
82                 email_address => $self->{email},
83                 password      => $self->{password},
84                 remember      => 1,
85             },
86         );
87     }
88     $self->{mecha}->submit;
89     return 0 unless $self->{mecha}->success;
90     return 0 if $self->{mecha}->content =~ /Oops/;
91
92     unless ( $self->{room_url} ) {
93         $self->{room_url} = $self->{guest_url};
94         my ( $room_no, ) = $self->{mecha}->content =~ /participant_list-(\d+)/;
95         $self->{room_url} =~ s!/\w+$!/room/$room_no!;
96     }
97
98 }
99
100 sub speak {
101     my ( $self, $message ) = @_;
102     $self->{mecha}->request(
103         POST $self->{room_url} . "/speak",
104         [ message => encode( 'utf-8', $message ) ]
105     );
106     return 0 unless $self->{mecha}->success;
107 }
108
109 1;
110
111 __END__
112
113 =head1 NAME
114
115 Plagger::Plugin::Notify::Campfire - Notification bot for Campfire
116
117 =head1 SYNOPSIS
118
119 To use bot as a guest (recommended),
120
121   - module: Notify::Campfire
122     config:
123       guest_url: http://exmaple.campfirenow.com/room/NNNN
124       nickname: nickname
125       speak_interval: 3
126
127 Or, to use bot using existent login credentials,
128
129   - module: Notify::Campfire
130     config:
131       room_url: http://exmaple.campfirenow.com/NNNN
132       email: example@example.com
133       password: xxxxxx
134       speak_interval: 2
135
136 =head1 DESCRIPTION
137
138 This plugin notifies feed updates to 37 Signals' Campfire
139 L<http://www.campfirenow.com/> chat room.
140
141 =head1 AUTHOR
142
143 Takeshi Nagayama
144
145 =head1 SEE ALSO
146
147 L<Plagger>, L<http://www.campfirenow.com/>, L<WWW::Mechanize>
148
149 =cut
Note: See TracBrowser for help on using the browser.