| 1 | package Mojo::Webqq::Plugin::KnowledgeBase; | ||
| 2 | our $PRIORITY = 3; | ||
| 3 | use List::Util qw(first); | ||
| 4 | sub retrieve_db { | ||
| 5 | my ($client,$db,$file) = @_; | ||
| 6 | my $new_db = {}; | ||
| 7 | my $fd; | ||
| 8 | if(! open $fd,"<",$file){ | ||
| 9 | $client->warn("无法加载知识库数据文件 $file : $!"); | ||
| 10 | return; | ||
| 11 | } | ||
| 12 | while(<$fd>){ | ||
| 13 | s/\r?\n$//; | ||
| 14 | my($space,$key,$content) = split /\s*(?<!\\)#\s*/,$_,3; | ||
| 15 | next if not $space && $key && $content; | ||
| 16 | $content =~ s/(\\r)?\\n/\n/g; | ||
| 17 | $content =~ s/\\t/\t/g; | ||
| 18 | push @{ $new_db->{$space}{$key} }, $content; | ||
| 19 | } | ||
| 20 | close $fd; | ||
| 21 | %$db = %$new_db; | ||
| 22 | } | ||
| 23 | sub store_db { | ||
| 24 | my($client,$db,$file) = @_; | ||
| 25 | my $fd; | ||
| 26 | if(!open $fd,">",$file){ | ||
| 27 | $client->warn("无法加载知识库数据文件 $file : $!"); | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | for my $space (keys %$db){ | ||
| 31 | for my $key (keys %{$db->{$space}}){ | ||
| 32 | #print $key,$space,join("|",@{$hash->{$space}{$key}}); | ||
| 33 | for $answer (@{$db->{$space}{$key}}){ | ||
| 34 | my $key_n= $key; | ||
| 35 | my $answer_n = $answer; | ||
| 36 | $answer_n =~ s/\r?\n/\\n/g; | ||
| 37 | $answer_n =~ s/\t/\\t/g; | ||
| 38 | $answer_n =~ s/\|/\\|/g; | ||
| 39 | print $fd $space," # ",$key," # ",$answer_n,"\n"; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | close $fd; | ||
| 44 | } | ||
| 45 | sub call{ | ||
| 46 | my $client = shift; | ||
| 47 | my $data = shift; | ||
| 48 | my ($file_size, $file_mtime); | ||
| 49 | $data->{mode} = 'fuzzy' if not defined $data->{mode}; | ||
| 50 | my $file = $data->{file} || './KnowledgeBase.txt'; | ||
| 51 | my $learn_command = defined $data->{learn_command}?quotemeta($data->{learn_command}):'learn|学习'; | ||
| 52 | my $delete_command = defined $data->{delete_command}?quotemeta($data->{delete_command}):'delete|del|删除'; | ||
| 53 | my $base = {}; | ||
| 54 | if(-e $file){ | ||
| 55 | ($file_size, $file_mtime) = (stat $file)[7, 9]; | ||
| 56 | retrieve_db($client,$base,$file); | ||
| 57 | } | ||
| 58 | $client->interval($data->{check_time} || 10,sub{ | ||
| 59 | return if not -e $file; | ||
| 60 | return if not defined $file_size; | ||
| 61 | return if not defined $file_mtime; | ||
| 62 | my ($size, $mtime) = (stat $file)[7, 9]; | ||
| 63 | if($size != $file_size or $mtime != $file_mtime){ | ||
| 64 | $file_size = $size; | ||
| 65 | $file_mtime = $mtime; | ||
| 66 | retrieve_db($client,$base,$file); | ||
| 67 | } | ||
| 68 | }); | ||
| 69 | my $callback = sub{ | ||
| 70 | my($client,$msg) = @_; | ||
| 71 | return if not $msg->allow_plugin; | ||
| 72 | return if $msg->class eq "send" and $msg->from ne "api" and $msg->from ne "irc"; | ||
| 73 | return if $msg->type !~ /^friend_message|group_message|dicsuss_message|sess_message$/; | ||
| 74 | if($msg->type eq 'group_message'){ | ||
| 75 | return if $data->{is_need_at} and $msg->type eq "group_message" and !$msg->is_at; | ||
| 76 | return if ref $data->{ban_group} eq "ARRAY" and first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->name eq $_} @{$data->{ban_group}}; | ||
| 77 | return if ref $data->{allow_group} eq "ARRAY" and !first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->name eq $_} @{$data->{allow_group}} | ||
| 78 | } | ||
| 79 | if($msg->content =~ /^(?:$learn_command)(\*?) | ||
| 80 | \s+ | ||
| 81 | (?|"([^"]+)"|'([^']+)'|([^\s"']+)) | ||
| 82 | \s+ | ||
| 83 | (?|"([^"]+)"|'([^']+)'|([^\s"']+)) | ||
| 84 | /xs){ | ||
| 85 | $msg->allow_plugin(0); | ||
| 86 | return if ref $data->{learn_operator} eq "ARRAY" and ! first {$_ eq $msg->sender->uid} @{$data->{learn_operator}}; | ||
| 87 | my($c,$q,$a) = ($1,$2,$3); | ||
| 88 | return unless defined $q; | ||
| 89 | return unless defined $a; | ||
| 90 | my $space = ''; | ||
| 91 | if(defined $c and $c eq "*"){ | ||
| 92 | $space = '__全局__'; | ||
| 93 | } | ||
| 94 | else{ | ||
| 95 | $space = $msg->type eq "friend_message"?"__我的好友__":$msg->group->displayname; | ||
| 96 | } | ||
| 97 | $q=~s/^\s+|\s+$//g; | ||
| 98 | $a=~s/^\s+|\s+$//g; | ||
| 99 | $a=~s/\\n/\n/g; | ||
| 100 | push @{ $base->{$space}{$q} }, $a; | ||
| 101 | store_db($client,$base,$file); | ||
| 102 | ($file_size, $file_mtime)= (stat $file)[7, 9]; | ||
| 103 | $client->reply_message($msg,"知识库[ $q → $a ]" . ($space eq '__全局__'?"*":"") . "添加成功",sub{$_[1]->from("bot")}); | ||
| 104 | |||
| 105 | } | ||
| 106 | elsif($msg->content =~ /^(?:$delete_command)(\*?) | ||
| 107 | \s+ | ||
| 108 | (?|"([^"]+)"|'([^']+)'|([^\s"']+)) | ||
| 109 | /xs){ | ||
| 110 | $msg->allow_plugin(0); | ||
| 111 | return if ref $data->{delete_operator} eq "ARRAY" and ! first {$_ eq $msg->sender->uid} @{$data->{delete_operator}}; | ||
| 112 | #return if $msg->sender->id ne $client->user->id; | ||
| 113 | my($c,$q) = ($1,$2); | ||
| 114 | $q=~s/^\s+|\s+$//g; | ||
| 115 | return unless defined $q; | ||
| 116 | my $space = ''; | ||
| 117 | if(defined $c and $c eq "*"){ | ||
| 118 | $space = '__全局__'; | ||
| 119 | } | ||
| 120 | else{ | ||
| 121 | $space = $msg->type eq "friend_message"?"__我的好友__":$msg->group->displayname; | ||
| 122 | } | ||
| 123 | delete $base->{$space}{$q}; | ||
| 124 | store_db($client,$base,$file); | ||
| 125 | ($file_size, $file_mtime)= (stat $file)[7, 9]; | ||
| 126 | $client->reply_message($msg,"知识库[ $q ]". ($space eq '__全局__'?"*":"") . "删除成功"),sub{$_[1]->from("bot")}; | ||
| 127 | } | ||
| 128 | else{ | ||
| 129 | #return if $msg->msg_class eq "send" and $msg->msg_from ne "api" and $msg->msg_from ne "irc"; | ||
| 130 | my $content = $msg->content; | ||
| 131 | $content =~s/^[a-zA-Z0-9_]+: ?// if $msg->msg_from eq "irc"; | ||
| 132 | my $space = $msg->type eq "friend_message"?"__我的好友__":$msg->group->displayname; | ||
| 133 | #return unless exists $base->{$space}{$content}; | ||
| 134 | if($data->{mode} eq 'regex'){ | ||
| 135 | my @match_keyword; | ||
| 136 | for my $keyword (keys %{$base->{$space}}){ | ||
| 137 | next if not $content=~/$keyword/; | ||
| 138 | push @match_keyword,$keyword; | ||
| 139 | } | ||
| 140 | if(@match_keyword == 0){ | ||
| 141 | $space = '__全局__'; | ||
| 142 | for my $keyword (keys %{$base->{$space}}){ | ||
| 143 | next if not $content=~/$keyword/; | ||
| 144 | push @match_keyword,$keyword; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | return if @match_keyword == 0; | ||
| 148 | $msg->allow_plugin(0); | ||
| 149 | my $keyword = $match_keyword[int rand @match_keyword]; | ||
| 150 | my $len = @{$base->{$space}{$keyword}}; | ||
| 151 | my $reply = $base->{$space}{$keyword}->[int rand $len]; | ||
| 152 | $reply .= "\n--匹配模式『$keyword』" . ($space eq '__全局__'?"*":"") if $data->{show_keyword}; | ||
| 153 | $client->reply_message($msg,$reply,sub{$_[1]->from("bot")}); | ||
| 154 | } | ||
| 155 | elsif($data->{mode} eq 'fuzzy'){ | ||
| 156 | my @match_keyword; | ||
| 157 | for my $keyword (keys %{$base->{$space}}){ | ||
| 158 | next if not $content=~/\Q$keyword\E/; | ||
| 159 | push @match_keyword,$keyword; | ||
| 160 | } | ||
| 161 | if(@match_keyword == 0){ | ||
| 162 | $space = '__全局__'; | ||
| 163 | for my $keyword (keys %{$base->{$space}}){ | ||
| 164 | next if not $content=~/$keyword/; | ||
| 165 | push @match_keyword,$keyword; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | return if @match_keyword == 0; | ||
| 169 | $msg->allow_plugin(0); | ||
| 170 | my $keyword = $match_keyword[int rand @match_keyword]; | ||
| 171 | my $len = @{$base->{$space}{$keyword}}; | ||
| 172 | my $reply = $base->{$space}{$keyword}->[int rand $len]; | ||
| 173 | $reply .= "\n--匹配关键字『$keyword』" . ($space eq '__全局__'?"*":"") if $data->{show_keyword}; | ||
| 174 | $client->reply_message($msg,$reply,sub{$_[1]->from("bot")}); | ||
| 175 | } | ||
| 176 | else{ | ||
| 177 | $space = '__全局__' if not exists $base->{$space}{$content}; | ||
| 178 | return if not exists $base->{$space}{$content}; | ||
| 179 | $msg->allow_plugin(0); | ||
| 180 | my $len = @{$base->{$space}{$content}}; | ||
| 181 | return if $len ==0; | ||
| 182 | $client->reply_message($msg,$base->{$space}{$content}->[int rand $len] . ($space eq '__全局__'?"*":""),sub{$_[1]->from("bot")}); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | }; | ||
| 186 | $client->on(receive_message=>$callback); | ||
| 187 | $client->on(send_message=>$callback); | ||
| 188 | } | ||
| 189 | 1; |