| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Hubot::Adapter::Campfire; |
|
2
|
|
|
|
|
|
|
$Hubot::Adapter::Campfire::VERSION = '0.2.7'; |
|
3
|
1
|
|
|
1
|
|
1650
|
use Moose; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use namespace::autoclean; |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
extends 'Hubot::Adapter'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use AnyEvent; |
|
9
|
|
|
|
|
|
|
use AnyEvent::HTTP; |
|
10
|
|
|
|
|
|
|
use AnyEvent::Campfire::Client; |
|
11
|
|
|
|
|
|
|
use JSON::XS; |
|
12
|
|
|
|
|
|
|
use Encode 'encode_utf8'; |
|
13
|
|
|
|
|
|
|
use HTTP::Request; |
|
14
|
|
|
|
|
|
|
use LWP::UserAgent; |
|
15
|
|
|
|
|
|
|
use Try::Tiny; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use Hubot::Message; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has 'robot' => ( is => 'ro', isa => 'Hubot::Robot', ); |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has 'cv' => ( is => 'ro', lazy_build => 1, ); |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has 'bot' => ( is => 'rw', ); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'httpClient' => ( is => 'ro', default => sub { LWP::UserAgent->new }, ); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _build_cv { AnyEvent->condvar } |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub send { |
|
30
|
|
|
|
|
|
|
my ( $self, $user, @strings ) = @_; |
|
31
|
|
|
|
|
|
|
$self->bot->speak( $user->{room}, encode_utf8($_) ) for @strings; |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub reply { |
|
35
|
|
|
|
|
|
|
my ( $self, $user, @strings ) = @_; |
|
36
|
|
|
|
|
|
|
@strings = map { $user->{name} . ": $_" } @strings; |
|
37
|
|
|
|
|
|
|
$self->send( $user, @strings ); |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub run { |
|
41
|
|
|
|
|
|
|
my $self = shift; |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my %options = ( |
|
44
|
|
|
|
|
|
|
token => $ENV{HUBOT_CAMPFIRE_TOKEN}, |
|
45
|
|
|
|
|
|
|
rooms => $ENV{HUBOT_CAMPFIRE_ROOMS}, |
|
46
|
|
|
|
|
|
|
account => $ENV{HUBOT_CAMPFIRE_ACCOUNT}, |
|
47
|
|
|
|
|
|
|
); |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$self->cv->begin; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $bot = AnyEvent::Campfire::Client->new(%options); |
|
52
|
|
|
|
|
|
|
$bot->on( |
|
53
|
|
|
|
|
|
|
'join', |
|
54
|
|
|
|
|
|
|
sub { |
|
55
|
|
|
|
|
|
|
my ( $e, $data ) = @_; |
|
56
|
|
|
|
|
|
|
$self->receive( new Hubot::EnterMessage ); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
); |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$bot->on( |
|
61
|
|
|
|
|
|
|
'message', |
|
62
|
|
|
|
|
|
|
sub { |
|
63
|
|
|
|
|
|
|
my ( $e, $data ) = @_; |
|
64
|
|
|
|
|
|
|
my $user = $self->userForId( $data->{user_id}, |
|
65
|
|
|
|
|
|
|
{ room => $data->{room_id}, } ); |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
if ( $user->{name} eq $user->{id} ) { |
|
68
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
|
69
|
|
|
|
|
|
|
GET => sprintf( |
|
70
|
|
|
|
|
|
|
"https://%s.campfirenow.com/users/%s", |
|
71
|
|
|
|
|
|
|
$options{account}, $user->{id} |
|
72
|
|
|
|
|
|
|
), |
|
73
|
|
|
|
|
|
|
); |
|
74
|
|
|
|
|
|
|
$req->header( 'Accept', 'application/json' ); |
|
75
|
|
|
|
|
|
|
$req->header( 'Authorization', $bot->authorization ); |
|
76
|
|
|
|
|
|
|
my $res = $self->httpClient->request($req); # non-async |
|
77
|
|
|
|
|
|
|
return unless $res->is_success; |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $userData; |
|
80
|
|
|
|
|
|
|
try { |
|
81
|
|
|
|
|
|
|
$userData = decode_json( $res->content ); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
catch { |
|
84
|
|
|
|
|
|
|
$bot->emit( 'error', $_ ); |
|
85
|
|
|
|
|
|
|
}; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
$user->{name} = $userData->{user}{name}; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
## TODO: support EnterMessage, LeaveMessage |
|
91
|
|
|
|
|
|
|
$self->receive( |
|
92
|
|
|
|
|
|
|
new Hubot::TextMessage( |
|
93
|
|
|
|
|
|
|
user => $user, |
|
94
|
|
|
|
|
|
|
text => $data->{body}, |
|
95
|
|
|
|
|
|
|
) |
|
96
|
|
|
|
|
|
|
); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
$bot->on( |
|
101
|
|
|
|
|
|
|
'error', |
|
102
|
|
|
|
|
|
|
sub { |
|
103
|
|
|
|
|
|
|
my ( $e, $error ) = @_; |
|
104
|
|
|
|
|
|
|
print STDERR "$error\n"; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
); |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$bot->on( |
|
109
|
|
|
|
|
|
|
'leave', |
|
110
|
|
|
|
|
|
|
sub { |
|
111
|
|
|
|
|
|
|
$self->cv->send; # TODO: support multiple rooms `leave` |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
); |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
$self->bot($bot); |
|
116
|
|
|
|
|
|
|
$self->emit('connected'); |
|
117
|
|
|
|
|
|
|
$self->cv->recv; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub close { |
|
121
|
|
|
|
|
|
|
my $self = shift; |
|
122
|
|
|
|
|
|
|
$self->bot->exit; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=pod |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=encoding utf-8 |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 NAME |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Hubot::Adapter::Campfire - Campfire adapter for L<Hubot> |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 VERSION |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
version 0.2.7 |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$ export HUBOT_CAMPFIRE_TOKEN='xxxx' |
|
144
|
|
|
|
|
|
|
$ export HUBOT_CAMPFIRE_ROOMS='1234' |
|
145
|
|
|
|
|
|
|
$ export HUBOT_CAMPFIRE_ACCOUNT='aanoaa' |
|
146
|
|
|
|
|
|
|
$ hubot -a campfire |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Campfire is the web based chat application built by L<37 Signals|http://37signals.com/>. |
|
151
|
|
|
|
|
|
|
The Campfire adapter is one of the original adapters in Hubot. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 CONFIGURATION |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=over |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item HUBOT_CAMPFIRE_TOKEN |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This can be found by logging in with your hubot's account click the My Info link and make a note of the API token. |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=item HUBOT_CAMPFIRE_ROOMS |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
If you join the rooms you want your hubot to join will see notice a numerical ID for the room in the URL. Make a note of each ID for the rooms you want your hubot to join. |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item HUBOT_CAMPFIRE_ACCOUNT |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This is simply the first part of the domain you visit for your Campfire account. For example if your Campfire was at C<hubot.campfirenow.com> your subdomain is hubot. Make a note of the subdomain. |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=back |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=over |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item L<https://github.com/37signals/campfire-api#authentication> |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item L<https://github.com/github/hubot/wiki/Adapter:-Campfire> |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=back |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 AUTHOR |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Hyungsuk Hong <hshong@perl.kr> |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Hyungsuk Hong. |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
190
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=cut |