3 # Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
4 # This program is covered by the GNU General Public License 2
12 use HTTP::Request::Common;
16 my $class = ref $proto || $proto;
17 my $self = {cfg => shift};
26 my $fb = Facebook::Graph->new(app_id => $me->{cfg}->{fb_app_id},
27 secret => $me->{cfg}->{fb_app_secret},
28 postback => $me->{cfg}->{fb_postback_url});
30 my $res_token = $fb->request_access_token($me->{cfg}->{fb_access_code});
31 die 'token get error' if (!defined $res_token || !$res_token->response->is_success);
33 my $acts = $fb->fetch('me/accounts');
34 die 'can not get account list' if(!defined $acts || !$acts);
36 my $page_access_token = '';
37 foreach my $d (@{$acts->{data}}) {
38 if ($d->{id} eq $me->{cfg}->{fb_page_id}) {
39 $page_access_token = $d->{'access_token'};
42 die 'can not get access tokenfor page_id=' . $me->{cfg}->{fb_page_id} if ($page_access_token eq '');
44 return $me->{fbo} = Facebook::Graph->new(access_token => $page_access_token);
48 my ($me, $text, $uri) = @_;
50 return $me->{fbo}->add_post($me->{cfg}->{fb_page_id})
58 my ($me, $post_id) = @_;
60 return $me->{cfg}->{fb_page_url} . 'posts/' . $post_id;
64 my ($me, $post_id) = @_;
66 my $uri = $fb->query->find($post_id)->uri_as_string;
67 my $req = HTTP::Request::Common::DELETE($uri);
68 $req->header('Content-Length', 0);
70 eval{$resp = LWP::UserAgent->new->request($req)};
80 use base 'Bot::BasicBot';
83 use HTTP::Request::Common;
84 use DBI qw/:sql_types/;
89 my $mu_re = qr/^([^\s]+)\s+((?:https?|ftps?):\/\/[^\s]+)\s+(.+)$/i;
94 $me->{dbh} = DBI->connect('DBI:mysql:'.$me->{cfg}->{database}, $me->{cfg}->{db_user}, $me->{cfg}->{db_pass},{mysql_enable_utf8 => 1}) || die $DBI::errstr;
100 $me->{last_search} = {};
105 $me->{fb} = Mubot4FB::FB->new($me->{cfg});
110 my ($me, $args, $uri) = @_;
114 my $sth = $me->{dbh}->prepare('select * from posts where uri = ? order by post_time desc limit 1');
115 my $rv = $sth->execute($uri);
116 my $res = $sth->fetchrow_hashref;
118 if ($res->{post_time} < time() - 7 * 24 * 60 * 60) {
119 $me->_response($args, 'だいぶ前 '.$me->_format_submit($res).'にいってたにゃー '.$me->_fb_post_uri($res->{fb_post_id}));
121 $me->_response($args, '既に '.$me->_format_submit($res).'に言ってますよ? '.$me->{cfg}->{fb_page_url}.'posts/'.$res->{fb_post_id});
131 my ($me, $db_args) = @_;
133 my ($scheme, $path) = split(/:\/\//, $db_args->{uri});
134 my $sth = $me->{dbh}->prepare("insert into posts (submitter, fb_post_id, uri, prefix, comment, scheme, path, post_time) values (?, ?, ?, ?, ?, ?, ?, ?)");
135 $sth->bind_param(1, $db_args->{submitter}, SQL_VARCHAR);
136 $sth->bind_param(2, $db_args->{fb_post_id}, SQL_BIGINT);
137 $sth->bind_param(3, $db_args->{uri}, SQL_VARCHAR);
138 $sth->bind_param(4, $db_args->{prefix}, SQL_VARCHAR);
139 $sth->bind_param(5, $db_args->{comment}, SQL_VARCHAR);
140 $sth->bind_param(6, $scheme, SQL_VARCHAR);
141 $sth->bind_param(7, $path, SQL_VARCHAR);
142 $sth->bind_param(8, time, SQL_BIGINT);
143 my $rv = $sth->execute();
150 my ($me, $db_args) = @_;
151 $db_args->{submitter_type} ||= 1;
153 my $sth = $me->{dbh}->prepare("delete from posts where fb_post_id = ? and submitter = ? and submitter_type = ?");
155 $sth->bind_param(1, $db_args->{fb_post_id}, SQL_BIGINT);
156 $sth->bind_param(2, $db_args->{submitter}, SQL_VARCHAR);
157 $sth->bind_param(3, $db_args->{submitter_type}, SQL_INTEGER);
158 my $rv = $sth->execute();
159 my $ret = $rv ? $sth->rows : 0;
167 my ($me, $word) = @_;
169 my $column = $word =~ /:\/\// ? 'uri' : 'path';
170 my $w = '%' . $word . '%';
171 my $sth = $me->{dbh}->prepare('select * from posts where prefix like ? or '.$column.' like ? or comment like ? order by post_time desc limit 1000');
172 $sth->bind_param(1, $w, SQL_VARCHAR);
173 $sth->bind_param(2, $w, SQL_VARCHAR);
174 $sth->bind_param(3, $w, SQL_VARCHAR);
177 my $ret = $sth->fetchall_arrayref({});
183 sub _db_search_lastpost {
186 my $sth = $me->{dbh}->prepare('select * from posts where submitter = ? order by post_time desc limit 1');
187 $sth->bind_param(1, $who, SQL_VARCHAR);
190 my $ret = $sth->fetchrow_hashref();
199 return $e->{submitter}.'が『'.$e->{prefix}.' '.$e->{uri}.' '.$e->{comment}.'』と'.strftime('%Y-%m-%d %H:%M:%S', localtime($e->{post_time}));
203 my ($me, $args, $msg) = @_;
205 $me->say(channel => $args->{channel},
212 my ($resp, $resp_msg);
214 if ($args->{body} =~ /$mu_re/) {
218 my $text = $args->{who} . '曰く、'.$prefix.' '.$comment;
220 return 0 if ($me->_check_dup($args, $uri));
222 eval{$resp = $me->{fb}->publish($text, $uri)};
225 eval{$resp = $me->{fb}->publish($text, $uri)};
226 $post_ok = 0 if ($@);
230 my (undef, $post_id) = split(/_/, $resp->{id});
231 $me->_db_insert({submitter => $args->{who},
232 fb_post_id => $post_id,
235 comment => $comment});
236 $resp_msg = $args->{who} . ': うい '.$me->{fb}->post_uri($post_id).' で登録';
238 $resp_msg = 'can not post to facebook';
247 my ($me, $args) = @_;
249 my $last_post = $me->_db_search_lastpost($args->{who});
251 if (!defined $last_post) {
252 return $args->{who}.': いまのっていつの? というか ないし';
253 } elsif ($last_post->{post_time} < time() - 3600) {
254 return $args->{who}.': いまのっていつの? 最後のはこれだけど古いんだにゃ ' . $me->{fb}->post_uri($last_post->{fb_post_id});
256 return $me->_delete($args, $last_post->{'fb_post_id'});
261 my ($me, $args, $post_id) =@_;
262 my ($resp_msg, $resp);
264 $me->{dbh}->begin_work;
265 if ($resp = $me->_db_delete({fb_post_id => $post_id, submitter => $args->{who}})) {
266 # fb 側のエントリを削除しないといけない
267 if ($me->{fb}->remove($post_id)) {
269 $resp_msg = $args->{who} . ': 削除しました ' . $me->{fb}->post_uri($post_id);
271 $resp_msg = $args->{who} . ': 削除に失敗しましたよ? ' . $me->{fb}->post_uri($post_id);
272 $me->{dbh}->rollback;
275 $resp_msg = $args->{who} . ': そんな投稿ないよ? ' . $me->{fb}->post_uri($post_id);
276 $me->{dbh}->rollback;
282 my ($me, $args) = @_;
284 if ($args->{body} =~ /^ふみくん\s+(.+)\?\s*$/) {
285 $me->{last_search}->{$args->{who}} = undef;
286 $me->{last_search}->{$args->{who}} = $me->_db_search($1);
287 return $me->_search_next($args);
292 my ($me, $args) = @_;
294 my $resp_msg = 'ないっす';
295 if (defined $me->{last_search}->{$args->{who}}) {
296 my $ent = pop($me->{last_search}->{$args->{who}});
298 my $count = @{$me->{last_search}->{$args->{who}}};
301 $resp_msg = $args->{who} . ': ' . $me->_format_submit($ent).'に言ってた '.($count ? '[ほか'.$count.'件] ' : '[ほかにはもうないよ] ').$me->{fb}->post_uri($ent->{fb_post_id});
312 my ($me, $args) = @_;
315 if ($args->{body} =~ /$mu_re/) {
316 $resp_msg = $me->_add($args) unless ($1 eq 'deb');
317 } elsif ($args->{body} =~ /^ふみくん\s+(.+)\s*$/) {
319 if ($cmd eq 'いまのなし') {
320 $resp_msg = $me->_delete_prev($args);
321 } elsif ($cmd =~ /削除\s+(?:$me->{cfg}->{fb_page_url}posts\/)?([0-9]+)$/) {
322 $resp_msg = $me->_delete($args, $1);
323 } elsif ($cmd =~ /\?$/) {
324 $resp_msg = $me->_search_start($args);
325 } elsif ($cmd =~ /つぎ/) {
326 $resp_msg = $me->_search_next($args);
330 $me->_response($args, $resp_msg) if ($resp_msg);
338 use Hash::Merge::Simple;
339 use Data::Recursive::Encode;
343 my $config_name = $ARGV[0] || 'not_found';
346 my $config_path = ['/etc/mubot4fb/'.$config_name,
347 $ENV{HOME} . '/.mubot4fb/'. $config_name,
348 $ENV{PWD} . '/mubot4fb_' . $config_name];
350 my $c = Config::Any->load_stems({stems => $config_path, use_ext => 1, flatten_to_hash => 1});
351 foreach my $i (keys %$c) {
352 $cfg = Hash::Merge::Simple->merge($cfg, $c->{$i});
354 die 'missing config file' unless (keys %$cfg);
356 die 'missing some config parameters should be defined (irc_server, fb_app_id, fb_app_secret, fb_access_code, fb_page_id fb_postback_url)'
357 if (!defined $cfg->{'irc_server'}
358 || !defined $cfg->{'fb_app_id'}
359 || !defined $cfg->{'fb_app_secret'}
360 || !defined $cfg->{'fb_access_code'}
361 || !defined $cfg->{'fb_page_id'}
362 || !defined $cfg->{'fb_postback_url'}
363 || !defined $cfg->{'db_user'}
364 || !defined $cfg->{'db_pass'}
366 $cfg = Data::Recursive::Encode->decode('utf8', $cfg);
368 $cfg->{irc_port} ||= 6667;
369 $cfg->{irc_channels} ||= ['#mubot4fb'];
370 $cfg->{irc_nick} ||= 'mubot4fb';
371 $cfg->{irc_name}||= $cfg->{irc_nick};
372 $cfg->{irc_charset} ||= 'utf8';
373 $cfg->{database} ||= 'mubot4fb';
375 my $bot = Mubot4FB->new(server => $cfg->{'irc_server'},
376 port => $cfg->{'irc_port'},
377 channels => $cfg->{'irc_channels'},
378 nick => $cfg->{'irc_nick'},
379 username => $cfg->{'irc_name'},
380 name => $cfg->{'irc_name'},
381 charset => $cfg->{'irc_charset'},