OSDN Git Service

tiny cleanup
[mubot4fb/mubot4fb.git] / mubot4fb.pl
index 5b5a024..a94cdce 100755 (executable)
@@ -11,6 +11,8 @@ use Facebook::Graph;
 use LWP::UserAgent;
 use HTTP::Request::Common;
 
+use Data::Dumper;
+
 sub new {
        my $proto = shift;
        my $class = ref $proto || $proto;
@@ -63,11 +65,18 @@ sub post_uri {
 sub remove {
        my ($me, $post_id) = @_;
 
-       my $req = HTTP::Request::Common::DELETE($me->post_uri($post_id));
-       $req->header('Content-Length', 0);
+       my $uri = $me->{fbo}->query->find($post_id)->uri_as_string;
+       my $req = HTTP::Request::Common::DELETE($uri);
+       warn Dumper($req) if ($me->{cfg}->{debug});
        my $resp;
-       eval{$resp = LWP::UserAgent->new->request($req)};
-       return !$@;
+       $resp = LWP::UserAgent->new->request($req);
+       warn Dumper($resp) if ($me->{cfg}->{debug});
+       if ($resp->is_success && $resp->code == 200 && $resp->content eq 'true') {
+               return 1;
+       } else {
+               warn 'DELETE ERROR: http code: ' . $resp->code() . ' , http content: ' . $resp->content;
+               return 0;
+       }
 }
 
 1;
@@ -77,9 +86,6 @@ use strict;
 use utf8;
 
 use base 'Bot::BasicBot';
-use Facebook::Graph;
-use LWP::UserAgent;
-use HTTP::Request::Common;
 use DBI qw/:sql_types/;
 use POSIX 'strftime';
 
@@ -105,26 +111,15 @@ sub init {
        $me->db_init();
 }
 
-sub _check_dup {
-       my ($me, $args, $uri) = @_;
-
-       my $found = 0;
-
+sub _db_check_dup {
+       my ($me, $db_args) = @_;
        my $sth = $me->{dbh}->prepare('select * from posts where uri = ? order by post_time desc limit 1');
-       my $rv = $sth->execute($uri);
-       my $res = $sth->fetchrow_hashref;
-       if ($res) {
-               if ($res->{post_time} < time() - 7 * 24 * 60 * 60) {
-                       $me->_response($args, 'だいぶ前 '.$me->_format_submit($res).'にいってたにゃー '.$me->_fb_post_uri($res->{fb_post_id}));
-               } else {
-                       $me->_response($args, '既に '.$me->_format_submit($res).'に言ってますよ? '.$me->{cfg}->{fb_page_url}.'posts/'.$res->{fb_post_id});
-                       $found = 1;
-               }
-       }
-       $sth->finish;
+       my $rv = $sth->execute($db_args->{uri});
+       my $ret = $sth->fetchrow_hashref;
 
-       return $found;
-};
+       $sth->finish;
+       return $ret;
+}
 
 sub _db_insert {
        my ($me, $db_args) = @_;
@@ -163,10 +158,10 @@ sub _db_delete {
 }
 
 sub _db_search {
-       my ($me, $word) = @_;
+       my ($me, $db_args) = @_;
 
-       my $column = $word =~ /:\/\// ? 'uri' : 'path';
-       my $w = '%' . $word . '%';
+       my $column = $db_args->{word} =~ /:\/\// ? 'uri' : 'path';
+       my $w = '%' . $db_args->{word} . '%';
        my $sth = $me->{dbh}->prepare('select * from posts where prefix like ? or '.$column.' like ? or comment like ? order by post_time desc limit 1000');
        $sth->bind_param(1, $w, SQL_VARCHAR);
        $sth->bind_param(2, $w, SQL_VARCHAR);
@@ -180,10 +175,10 @@ sub _db_search {
 }
 
 sub _db_search_lastpost {
-       my ($me, $who) = @_;
+       my ($me, $db_args) = @_;
 
        my $sth = $me->{dbh}->prepare('select * from posts where submitter = ? order by post_time desc limit 1');
-       $sth->bind_param(1, $who, SQL_VARCHAR);
+       $sth->bind_param(1, $db_args->{who}, SQL_VARCHAR);
        $sth->execute();
 
        my $ret = $sth->fetchrow_hashref();
@@ -207,7 +202,6 @@ sub _response {
 
 sub _add {
        my ($me, $args)  =@_;
-       my $post_ok = 1;
        my ($resp, $resp_msg);
 
        if ($args->{body} =~ /$mu_re/) {
@@ -216,27 +210,33 @@ sub _add {
                my $comment = $3;
                my $text = $args->{who} . '曰く、'.$prefix.' '.$comment;
 
-               return 0 if ($me->_check_dup($args, $uri));
-
-               eval{$resp = $me->{fb}->publish($text, $uri)};
-               if ($@) {
-                       $me->fb_init();
+               if (my $res = $me->_db_check_dup({uri =>$uri})) {
+                       if ($res->{post_time} < time() - 7 * 24 * 60 * 60) {
+                               $resp_msg = 'だいぶ前 '.$me->_format_submit($res).'にいってたにゃー '.$me->{fb}->post_uri($res->{fb_post_id});
+                       } else {
+                               $resp_msg = '既に '.$me->_format_submit($res).'に言ってますよ? '.$me->{fb}->post_uri($res->{fb_post_id});
+                       }
+               } else {
+                       my $post_ok = 1;
                        eval{$resp = $me->{fb}->publish($text, $uri)};
-                       $post_ok = 0 if ($@);
-               }
+                       if ($@) {
+                               $me->fb_init();
+                               eval{$resp = $me->{fb}->publish($text, $uri)};
+                               $post_ok = 0 if ($@);
+                       }
 
-               if ($post_ok) {
-                       my (undef, $post_id) = split(/_/, $resp->{id});
-                       $me->_db_insert({submitter => $args->{who},
-                                        fb_post_id => $post_id,
-                                        uri => $uri,
-                                        prefix => $prefix,
-                                        comment => $comment});
-                       $resp_msg = $args->{who} . ': うい  '.$me->{fb}->post_uri($post_id).' で登録';
-               } else {
-                       $resp_msg = 'can not post to facebook';
+                       if ($post_ok) {
+                               my (undef, $post_id) = split(/_/, $resp->{id});
+                               $me->_db_insert({submitter => $args->{who},
+                                                fb_post_id => $post_id,
+                                                uri => $uri,
+                                                prefix => $prefix,
+                                                comment => $comment});
+                               $resp_msg = $args->{who} . ': うい  '.$me->{fb}->post_uri($post_id).' で登録';
+                       } else {
+                               $resp_msg = 'can not post to facebook';
+                       }
                }
-
                return $resp_msg;
        }
        return 0;
@@ -245,7 +245,7 @@ sub _add {
 sub _delete_prev {
        my ($me, $args) = @_;
 
-       my $last_post = $me->_db_search_lastpost($args->{who});
+       my $last_post = $me->_db_search_lastpost({who => $args->{who}});
 
        if (!defined $last_post) {
                return $args->{who}.': いまのっていつの? というか ないし';
@@ -282,7 +282,7 @@ sub _search_start {
 
        if ($args->{body} =~ /^ふみくん\s+(.+)\?\s*$/) {
                $me->{last_search}->{$args->{who}} = undef;
-               $me->{last_search}->{$args->{who}} = $me->_db_search($1);
+               $me->{last_search}->{$args->{who}} = $me->_db_search({word => $1});
                return $me->_search_next($args);
        }
 }
@@ -370,6 +370,7 @@ $cfg->{irc_nick} ||= 'mubot4fb';
 $cfg->{irc_name}||= $cfg->{irc_nick};
 $cfg->{irc_charset} ||= 'utf8';
 $cfg->{database} ||= 'mubot4fb';
+$cfg->{debug} ||= 0;
 
 my $bot = Mubot4FB->new(server => $cfg->{'irc_server'},
                        port => $cfg->{'irc_port'},