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

Revision 810 (checked in by miyagawa, 14 years ago)
  • added Plagger::Mechanize for a wrapper class around WWW::Mech (to set User-Agent from global config)
  • Updated plugins to use Plagger::Mechanize where WWW::Mech was used.
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 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__
117
118 =head1 NAME
119
120 Plagger::Plugin::Notify::Campfire - Notification bot for Campfire
121
122 =head1 SYNOPSIS
123
124 To use bot as a guest (recommended),
125
126   - module: Notify::Campfire
127     config:
128       guest_url: http://exmaple.campfirenow.com/room/NNNN
129       nickname: nickname
130       speak_interval: 3
131
132 Or, to use bot using existent login credentials,
133
134   - module: Notify::Campfire
135     config:
136       room_url: http://exmaple.campfirenow.com/NNNN
137       email: example@example.com
138       password: xxxxxx
139       speak_interval: 2
140
141 =head1 DESCRIPTION
142
143 This plugin notifies feed updates to 37 Signals' Campfire
144 L<http://www.campfirenow.com/> chat room.
145
146 Note that you don't have to supply emali and password if you set
147 global cookie_jar in your configuration file and the cookie_jar
148 contains a valid login session there, such as:
149
150   global:
151     user_agent:
152       cookies: /path/to/cookies.txt
153
154 See L<Plagger::Cookies> for details.
155
156 =head1 AUTHOR
157
158 Takeshi Nagayama
159
160 =head1 SEE ALSO
161
162 L<Plagger>, L<http://www.campfirenow.com/>, L<WWW::Mechanize>
163
164 =cut
Note: See TracBrowser for help on using the browser.