OSDN Git Service

convert to use Any::Config and fix encoding problem
[mubot4fb/mubot4fb.git] / mubot4fb.pl
index 3c6136c..865568c 100755 (executable)
@@ -1,4 +1,8 @@
 #!/usr/bin/perl
+#
+# Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
+# This program is covered by the GNU General Public License 2
+#
 package Mubot4FB;
 
 use strict;
@@ -91,12 +95,12 @@ sub _db_insert {
        my ($me, $db_args) = @_;
 
        my ($scheme, $path) = split(/:\/\//, $db_args->{uri});
-       my $sth = $me->{dbh}->prepare("insert into posts (submitter, fb_post_id, uri, prefix, suffix, scheme, path, post_time) values (?, ?, ?, ?, ?, ?, ?, ?)");
+       my $sth = $me->{dbh}->prepare("insert into posts (submitter, fb_post_id, uri, prefix, comment, scheme, path, post_time) values (?, ?, ?, ?, ?, ?, ?, ?)");
        $sth->bind_param(1, $db_args->{submitter}, SQL_VARCHAR);
        $sth->bind_param(2, $db_args->{fb_post_id}, SQL_BIGINT);
        $sth->bind_param(3, $db_args->{uri}, SQL_VARCHAR);
        $sth->bind_param(4, $db_args->{prefix}, SQL_VARCHAR);
-       $sth->bind_param(5, $db_args->{suffix}, SQL_VARCHAR);
+       $sth->bind_param(5, $db_args->{comment}, SQL_VARCHAR);
        $sth->bind_param(6, $scheme, SQL_VARCHAR);
        $sth->bind_param(7, $path, SQL_VARCHAR);
        $sth->bind_param(8, time, SQL_BIGINT);
@@ -128,7 +132,7 @@ sub _db_search {
 
        my $column = $word =~ /:\/\// ? 'uri' : 'path';
        my $w = '%' . $word . '%';
-       my $sth = $me->{dbh}->prepare('select * from posts where prefix like ? or '.$column.' like ? or suffix like ? order by post_time desc limit 1000');
+       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);
        $sth->bind_param(3, $w, SQL_VARCHAR);
@@ -172,7 +176,7 @@ sub _fb_delete {
 sub _format_submit {
        my ($me, $e) = @_;
 
-       return $e->{submitter}.'が『'.$e->{prefix}.' '.$e->{uri}.' '.$e->{suffix}.'』と'.strftime('%Y-%m-%d %H:%M:%S', localtime($e->{post_time}));
+       return $e->{submitter}.'が『'.$e->{prefix}.' '.$e->{uri}.' '.$e->{comment}.'』と'.strftime('%Y-%m-%d %H:%M:%S', localtime($e->{post_time}));
 }
 
 sub _response {
@@ -190,8 +194,8 @@ sub _add {
        if ($args->{body} =~ /$mu_re/) {
                my $prefix = $1;
                my $uri = $2;
-               my $suffix = $3;
-               my $text = $args->{who} . '曰く、'.$prefix.' '.$suffix;
+               my $comment = $3;
+               my $text = $args->{who} . '曰く、'.$prefix.' '.$comment;
 
                return 0 if ($me->_check_dup($args, $uri));
 
@@ -208,7 +212,7 @@ sub _add {
                                         fb_post_id => $post_id,
                                         uri => $uri,
                                         prefix => $prefix,
-                                        suffix => $suffix});
+                                        comment => $comment});
                        $resp_msg = $args->{who} . ': うい  '.$me->_fb_post_uri($post_id).' で登録';
                } else {
                        $resp_msg = 'can not post to facebook';
@@ -310,43 +314,51 @@ package main;
 use strict;
 use utf8;
 
-use Config::Simple;
+use Config::Any;
+use Hash::Merge::Simple;
+use Data::Recursive::Encode;
+
+use Data::Dumper;
 
 my $config_name = $ARGV[0] || 'not_found';
 
-my %cfg;
-my $config_path = ('/etc/mubot4fb/', $ENV{HOME} . '/.mubot4fb/', $ENV{PWD} . '/mubot4fb_');
-foreach my $c ($config_path) {
-       my $config = $c . $config_name . '.conf';
-       Config::Simple->import_from($config, \%cfg) if (-e $config);
+my $cfg = {};
+my $config_path = ['/etc/mubot4fb/'.$config_name,
+                  $ENV{HOME} . '/.mubot4fb/'. $config_name,
+                  $ENV{PWD} . '/mubot4fb_' . $config_name];
+
+my $c = Config::Any->load_stems({stems => $config_path,  use_ext => 1, flatten_to_hash => 1});
+foreach my $i (keys %$c) {
+       $cfg = Hash::Merge::Simple->merge($cfg, $c->{$i});
 }
-die 'missing config file' unless (keys %cfg);
+die 'missing config file' unless (keys %$cfg);
 
 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)'
-  if (!defined $cfg{'irc_server'}
-      || !defined $cfg{'fb_app_id'}
-      || !defined $cfg{'fb_app_secret'}
-      || !defined $cfg{'fb_access_code'}
-      || !defined $cfg{'fb_page_id'}
-      || !defined $cfg{'fb_postback_url'}
-      || !defined $cfg{'db_user'}
-      || !defined $cfg{'db_pass'}
+  if (!defined $cfg->{'irc_server'}
+      || !defined $cfg->{'fb_app_id'}
+      || !defined $cfg->{'fb_app_secret'}
+      || !defined $cfg->{'fb_access_code'}
+      || !defined $cfg->{'fb_page_id'}
+      || !defined $cfg->{'fb_postback_url'}
+      || !defined $cfg->{'db_user'}
+      || !defined $cfg->{'db_pass'}
     );
-
-$cfg{irc_port} ||= 6667;
-$cfg{irc_channels} ||= ['#mubot4fb'];
-$cfg{irc_nick} ||= 'mubot4fb';
-$cfg{irc_name}||= $cfg{irc_nick};
-$cfg{irc_charset} ||= 'utf8';
-$cfg{database} ||= 'mubot4fb';
-
-my $bot = Mubot4FB->new(server => $cfg{'irc_server'},
-                       port => $cfg{'irc_port'},
-                       channels => $cfg{'irc_channels'},
-                       nick => $cfg{'irc_nick'},
-                       username => $cfg{'irc_name'},
-                       name => $cfg{'irc_name'},
-                       charset => $cfg{'irc_charset'},
-                       cfg => \%cfg)->run();
+$cfg = Data::Recursive::Encode->decode('utf8', $cfg);
+
+$cfg->{irc_port} ||= 6667;
+$cfg->{irc_channels} ||= ['#mubot4fb'];
+$cfg->{irc_nick} ||= 'mubot4fb';
+$cfg->{irc_name}||= $cfg->{irc_nick};
+$cfg->{irc_charset} ||= 'utf8';
+$cfg->{database} ||= 'mubot4fb';
+
+my $bot = Mubot4FB->new(server => $cfg->{'irc_server'},
+                       port => $cfg->{'irc_port'},
+                       channels => $cfg->{'irc_channels'},
+                       nick => $cfg->{'irc_nick'},
+                       username => $cfg->{'irc_name'},
+                       name => $cfg->{'irc_name'},
+                       charset => $cfg->{'irc_charset'},
+                       cfg => $cfg)->run();
 
 1;