3 # Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
4 # This program is covered by the GNU General Public License 2
11 use base 'Bot::BasicBot';
14 use HTTP::Request::Common;
15 use DBI qw/:sql_types/;
20 my $mu_re = qr/^([^\s]+)\s+((?:https?|ftps?):\/\/[^\s]+)\s+(.+)$/i;
25 my $fb = Facebook::Graph->new(app_id => $me->{cfg}->{fb_app_id},
26 secret => $me->{cfg}->{fb_app_secret},
27 postback => $me->{cfg}->{fb_postback_url});
29 my $res_token = $fb->request_access_token($me->{cfg}->{fb_access_code});
30 die 'token get error' if (!defined $res_token || !$res_token->response->is_success);
32 my $acts = $fb->fetch('me/accounts');
33 die 'can not get account list' if(!defined $acts || !$acts);
35 my $page_access_token = '';
36 foreach my $d (@{$acts->{data}}) {
37 if ($d->{id} eq $me->{cfg}->{fb_page_id}) {
38 $page_access_token = $d->{'access_token'};
41 die 'can not get access tokenfor page_id=' . $me->{cfg}->{fb_page_id} if ($page_access_token eq '');
43 return $me->{fbo} = Facebook::Graph->new(access_token => $page_access_token);
48 $me->{dbh} = DBI->connect('DBI:mysql:'.$me->{cfg}->{database}, $me->{cfg}->{db_user}, $me->{cfg}->{db_pass},{mysql_enable_utf8 => 1}) || die $DBI::errstr;
54 $me->{last_search} = {};
58 my ($me, $text, $uri) = @_;
60 return $me->{fbo}->add_post($me->{cfg}->{fb_page_id})
74 my ($me, $args, $uri) = @_;
78 my $sth = $me->{dbh}->prepare('select * from posts where uri = ? order by post_time desc limit 1');
79 my $rv = $sth->execute($uri);
80 my $res = $sth->fetchrow_hashref;
82 if ($res->{post_time} < time() - 7 * 24 * 60 * 60) {
83 $me->_response($args, 'だいぶ前 '.$me->_format_submit($res).'にいってたにゃー '.$me->_fb_post_uri($res->{fb_post_id}));
85 $me->_response($args, '既に '.$me->_format_submit($res).'に言ってますよ? '.$me->{cfg}->{fb_page_url}.'posts/'.$res->{fb_post_id});
95 my ($me, $db_args) = @_;
97 my ($scheme, $path) = split(/:\/\//, $db_args->{uri});
98 my $sth = $me->{dbh}->prepare("insert into posts (submitter, fb_post_id, uri, prefix, comment, scheme, path, post_time) values (?, ?, ?, ?, ?, ?, ?, ?)");
99 $sth->bind_param(1, $db_args->{submitter}, SQL_VARCHAR);
100 $sth->bind_param(2, $db_args->{fb_post_id}, SQL_BIGINT);
101 $sth->bind_param(3, $db_args->{uri}, SQL_VARCHAR);
102 $sth->bind_param(4, $db_args->{prefix}, SQL_VARCHAR);
103 $sth->bind_param(5, $db_args->{comment}, SQL_VARCHAR);
104 $sth->bind_param(6, $scheme, SQL_VARCHAR);
105 $sth->bind_param(7, $path, SQL_VARCHAR);
106 $sth->bind_param(8, time, SQL_BIGINT);
107 my $rv = $sth->execute();
114 my ($me, $db_args) = @_;
115 $db_args->{submitter_type} ||= 1;
117 my $sth = $me->{dbh}->prepare("delete from posts where fb_post_id = ? and submitter = ? and submitter_type = ?");
119 $sth->bind_param(1, $db_args->{fb_post_id}, SQL_BIGINT);
120 $sth->bind_param(2, $db_args->{submitter}, SQL_VARCHAR);
121 $sth->bind_param(3, $db_args->{submitter_type}, SQL_INTEGER);
122 my $rv = $sth->execute();
123 my $ret = $rv ? $sth->rows : 0;
131 my ($me, $word) = @_;
133 my $column = $word =~ /:\/\// ? 'uri' : 'path';
134 my $w = '%' . $word . '%';
135 my $sth = $me->{dbh}->prepare('select * from posts where prefix like ? or '.$column.' like ? or comment like ? order by post_time desc limit 1000');
136 $sth->bind_param(1, $w, SQL_VARCHAR);
137 $sth->bind_param(2, $w, SQL_VARCHAR);
138 $sth->bind_param(3, $w, SQL_VARCHAR);
141 my $ret = $sth->fetchall_arrayref({});
147 sub _db_search_lastpost {
150 my $sth = $me->{dbh}->prepare('select * from posts where submitter = ? order by post_time desc limit 1');
151 $sth->bind_param(1, $who, SQL_VARCHAR);
154 my $ret = $sth->fetchrow_hashref();
161 my ($me, $post_id) = @_;
163 return $me->{cfg}->{fb_page_url} . 'posts/' . $post_id;
167 my ($me, $post_id) = @_;
169 my $req = HTTP::Request::Common::DELETE($me->_fb_post_uri($post_id));
170 $req->header('Content-Length', 0);
172 eval{$resp = LWP::UserAgent->new->request($req)};
179 return $e->{submitter}.'が『'.$e->{prefix}.' '.$e->{uri}.' '.$e->{comment}.'』と'.strftime('%Y-%m-%d %H:%M:%S', localtime($e->{post_time}));
183 my ($me, $args, $msg) = @_;
185 $me->say(channel => $args->{channel},
192 my ($resp, $resp_msg);
194 if ($args->{body} =~ /$mu_re/) {
198 my $text = $args->{who} . '曰く、'.$prefix.' '.$comment;
200 return 0 if ($me->_check_dup($args, $uri));
202 eval{$resp = $me->publish($text, $uri)};
205 eval{$resp = $me->publish($text, $uri)};
206 $post_ok = 0 if ($@);
210 my (undef, $post_id) = split(/_/, $resp->{id});
211 $me->_db_insert({submitter => $args->{who},
212 fb_post_id => $post_id,
215 comment => $comment});
216 $resp_msg = $args->{who} . ': うい '.$me->_fb_post_uri($post_id).' で登録';
218 $resp_msg = 'can not post to facebook';
227 my ($me, $args) = @_;
229 my $last_post = $me->_db_search_lastpost($args->{who});
231 if (!defined $last_post) {
232 return $args->{who}.': いまのっていつの? というか ないし';
233 } elsif ($last_post->{post_time} < time() - 3600) {
234 return $args->{who}.': いまのっていつの? 最後のはこれだけど古いんだにゃ ' . $me->_fb_post_uri($last_post->{fb_post_id});
236 return $me->_delete($args, $last_post->{'fb_post_id'});
241 my ($me, $args, $post_id) =@_;
242 my ($resp_msg, $resp);
244 $me->{dbh}->begin_work;
245 if ($resp = $me->_db_delete({fb_post_id => $post_id, submitter => $args->{who}})) {
246 # fb 側のエントリを削除しないといけない
247 if ($me->_fb_delete($post_id)) {
249 $resp_msg = $args->{who} . ': 削除しました ' . $me->_fb_post_uri($post_id);
251 $resp_msg = $args->{who} . ': 削除に失敗しましたよ? ' . $me->_fb_post_uri($post_id);
252 $me->{dbh}->rollback;
255 $resp_msg = $args->{who} . ': そんな投稿ないよ? ' . $me->_fb_post_uri($post_id);
256 $me->{dbh}->rollback;
262 my ($me, $args) = @_;
264 if ($args->{body} =~ /^ふみくん\s+(.+)\?\s*$/) {
265 $me->{last_search}->{$args->{who}} = undef;
266 $me->{last_search}->{$args->{who}} = $me->_db_search($1);
267 return $me->_search_next($args);
272 my ($me, $args) = @_;
274 my $resp_msg = 'ないっす';
275 if (defined $me->{last_search}->{$args->{who}}) {
276 my $ent = pop($me->{last_search}->{$args->{who}});
278 my $count = @{$me->{last_search}->{$args->{who}}};
281 $resp_msg = $args->{who} . ': ' . $me->_format_submit($ent).'に言ってた '.($count ? '[ほか'.$count.'件] ' : '[ほかにはもうないよ] ').$me->_fb_post_uri($ent->{fb_post_id});
292 my ($me, $args) = @_;
295 if ($args->{body} =~ /$mu_re/) {
296 $resp_msg = $me->_add($args) unless ($1 eq 'deb');
297 } elsif ($args->{body} =~ /^ふみくん\s+(.+)\s*$/) {
299 if ($cmd eq 'いまのなし') {
300 $resp_msg = $me->_delete_prev($args);
301 } elsif ($cmd =~ /削除\s+(?:$me->{cfg}->{fb_page_url}posts\/)?([0-9]+)$/) {
302 $resp_msg = $me->_delete($args, $1);
303 } elsif ($cmd =~ /\?$/) {
304 $resp_msg = $me->_search_start($args);
305 } elsif ($cmd =~ /つぎ/) {
306 $resp_msg = $me->_search_next($args);
310 $me->_response($args, $resp_msg) if ($resp_msg);
319 my $config_name = $ARGV[0] || 'not_found';
322 my $config_path = ('/etc/mubot4fb/', $ENV{HOME} . '/.mubot4fb/', $ENV{PWD} . '/mubot4fb_');
323 foreach my $c ($config_path) {
324 my $config = $c . $config_name . '.conf';
325 Config::Simple->import_from($config, \%cfg) if (-e $config);
327 die 'missing config file' unless (keys %cfg);
329 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)'
330 if (!defined $cfg{'irc_server'}
331 || !defined $cfg{'fb_app_id'}
332 || !defined $cfg{'fb_app_secret'}
333 || !defined $cfg{'fb_access_code'}
334 || !defined $cfg{'fb_page_id'}
335 || !defined $cfg{'fb_postback_url'}
336 || !defined $cfg{'db_user'}
337 || !defined $cfg{'db_pass'}
340 $cfg{irc_port} ||= 6667;
341 $cfg{irc_channels} ||= ['#mubot4fb'];
342 $cfg{irc_nick} ||= 'mubot4fb';
343 $cfg{irc_name}||= $cfg{irc_nick};
344 $cfg{irc_charset} ||= 'utf8';
345 $cfg{database} ||= 'mubot4fb';
347 my $bot = Mubot4FB->new(server => $cfg{'irc_server'},
348 port => $cfg{'irc_port'},
349 channels => $cfg{'irc_channels'},
350 nick => $cfg{'irc_nick'},
351 username => $cfg{'irc_name'},
352 name => $cfg{'irc_name'},
353 charset => $cfg{'irc_charset'},
354 cfg => \%cfg)->run();