line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hubot::Adapter::Mypeople; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Hubot::Adapter::Mypeople::VERSION = '0.0.6'; |
4
|
|
|
|
|
|
|
} |
5
|
1
|
|
|
1
|
|
51327
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use namespace::autoclean; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
extends 'Hubot::Adapter'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use AnyEvent::MyPeopleBot::Client; |
11
|
|
|
|
|
|
|
use JSON::XS; |
12
|
|
|
|
|
|
|
use Encode 'decode_utf8'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use Hubot::Message; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has httpd => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
lazy_build => 1, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has client => ( |
22
|
|
|
|
|
|
|
is => 'rw', |
23
|
|
|
|
|
|
|
isa => 'AnyEvent::MyPeopleBot::Client', |
24
|
|
|
|
|
|
|
lazy_build => 1, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has groups => ( |
28
|
|
|
|
|
|
|
traits => ['Array'], |
29
|
|
|
|
|
|
|
is => 'ro', |
30
|
|
|
|
|
|
|
isa => 'ArrayRef[Str]', |
31
|
|
|
|
|
|
|
default => sub { [] }, |
32
|
|
|
|
|
|
|
handles => { |
33
|
|
|
|
|
|
|
all_groups => 'elements', |
34
|
|
|
|
|
|
|
add_group => 'push', |
35
|
|
|
|
|
|
|
find_group => 'first', |
36
|
|
|
|
|
|
|
count_groups => 'count', |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has exit => ( |
41
|
|
|
|
|
|
|
is => 'rw', |
42
|
|
|
|
|
|
|
isa => 'Bool', |
43
|
|
|
|
|
|
|
default => 0, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub send { |
47
|
|
|
|
|
|
|
my ( $self, $user, @strings ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$self->client->send( |
50
|
|
|
|
|
|
|
$user->{room}, |
51
|
|
|
|
|
|
|
join( "\n", @strings ), |
52
|
|
|
|
|
|
|
sub { $self->httpd->stop if $self->exit } |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub reply { |
57
|
|
|
|
|
|
|
my ( $self, $user, @strings ) = @_; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
@strings = map { $user->{name} . ": $_" } @strings; |
60
|
|
|
|
|
|
|
$self->send( $user, @strings ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub run { |
64
|
|
|
|
|
|
|
my $self = shift; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
unless ( $ENV{HUBOT_MYPEOPLE_APIKEY} ) { |
67
|
|
|
|
|
|
|
print STDERR |
68
|
|
|
|
|
|
|
"HUBOT_MYPEOPLE_APIKEY is not defined, try: export HUBOT_MYPEOPLE_APIKEY='yourapikey'"; |
69
|
|
|
|
|
|
|
exit; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
$self->client( |
73
|
|
|
|
|
|
|
AnyEvent::MyPeopleBot::Client->new( |
74
|
|
|
|
|
|
|
apikey => $ENV{HUBOT_MYPEOPLE_APIKEY} |
75
|
|
|
|
|
|
|
) |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $httpd = $self->robot->httpd; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
$httpd->reg_cb( |
81
|
|
|
|
|
|
|
$ENV{HUBOT_MYPEOPLE_CALLBACK_PATH} || '/' => sub { |
82
|
|
|
|
|
|
|
my ( $httpd, $req ) = @_; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $action = $req->parm('action'); |
85
|
|
|
|
|
|
|
my $buddyId = $req->parm('buddyId'); |
86
|
|
|
|
|
|
|
my $groupId = $req->parm('groupId'); |
87
|
|
|
|
|
|
|
my $content = decode_utf8( $req->parm('content') ); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$req->respond( |
90
|
|
|
|
|
|
|
{ content => [ 'text/plain', 'Your request is succeed' ] } ); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
$self->add_group($groupId) |
93
|
|
|
|
|
|
|
if $groupId && !$self->find_group( sub {/^$groupId$/} ); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
if ( $action =~ /^sendFrom/ ) { |
96
|
|
|
|
|
|
|
$self->respond( |
97
|
|
|
|
|
|
|
$buddyId, $groupId, |
98
|
|
|
|
|
|
|
sub { |
99
|
|
|
|
|
|
|
my $user = shift; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
$self->receive( |
102
|
|
|
|
|
|
|
Hubot::TextMessage->new( |
103
|
|
|
|
|
|
|
user => $user, |
104
|
|
|
|
|
|
|
text => $content, |
105
|
|
|
|
|
|
|
) |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
elsif ( $action =~ /^(createGroup|inviteToGroup)$/ ) { |
111
|
|
|
|
|
|
|
$self->respond( |
112
|
|
|
|
|
|
|
$buddyId, $groupId, |
113
|
|
|
|
|
|
|
sub { |
114
|
|
|
|
|
|
|
my $user = shift; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
$self->receive( |
117
|
|
|
|
|
|
|
Hubot::EnterMessage->new( user => $user ) ); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
elsif ( $action eq 'exitFromGroup' ) { |
122
|
|
|
|
|
|
|
$self->respond( |
123
|
|
|
|
|
|
|
$buddyId, $groupId, |
124
|
|
|
|
|
|
|
sub { |
125
|
|
|
|
|
|
|
my $user = shift; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$self->receive( |
128
|
|
|
|
|
|
|
Hubot::LeaveMessage->new( user => $user ) ); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my $port = $ENV{HUBOT_MYPEOPLE_PORT} || 8080; |
136
|
|
|
|
|
|
|
print __PACKAGE__ . " Accepting connection at http://0:$port\n"; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
$self->emit('connected'); |
139
|
|
|
|
|
|
|
$httpd->run; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub respond { |
143
|
|
|
|
|
|
|
my ( $self, $buddyId, $groupId, $cb ) = @_; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $user = $self->userForId( $buddyId, { room => $groupId || $buddyId } ); |
146
|
|
|
|
|
|
|
return $cb->($user) if $user->{id} ne $user->{name}; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
$self->client->profile( |
149
|
|
|
|
|
|
|
$buddyId, |
150
|
|
|
|
|
|
|
sub { |
151
|
|
|
|
|
|
|
my $data = decode_json(shift); |
152
|
|
|
|
|
|
|
$user->{name} = $data->{buddys}[0]{name}; |
153
|
|
|
|
|
|
|
$cb->($user); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub close { |
159
|
|
|
|
|
|
|
my $self = shift; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
return $self->exit(1) unless $self->count_groups; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my $exit = 0; |
164
|
|
|
|
|
|
|
for my $groupId ( $self->all_groups ) { |
165
|
|
|
|
|
|
|
$self->client->exit( |
166
|
|
|
|
|
|
|
$groupId, |
167
|
|
|
|
|
|
|
sub { |
168
|
|
|
|
|
|
|
my $json = shift; |
169
|
|
|
|
|
|
|
return if $self->count_groups != ++$exit; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
$self->httpd->stop; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
); |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
__END__ |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=pod |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=encoding utf-8 |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 NAME |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Hubot::Adapter::Mypeople |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 VERSION |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
version 0.0.6 |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 SYNOPSIS |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
# you might be never use this module directly |
198
|
|
|
|
|
|
|
$ hubot -a mypeople |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 DESCRIPTION |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
you should register your own bot via L<http://dna.daum.net/myapi/authapi/mypeople/new>. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 CONFIGURATION |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=over |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item * HUBOT_MYPEOPLE_APIKEY |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item * HUBOT_MYPEOPLE_CALLBACK_PATH |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
'/' is default to use. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=back |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 SEE ALSO |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
http://dna.daum.net/myapi/authapi/mypeople/new |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 AUTHOR |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Hyungsuk Hong <hshong@perl.kr> |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
This software is copyright (c) 2013 by Hyungsuk Hong. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
229
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |