| 1 | package Morris::Plugin::QOTD; | ||
| 2 | use Moose; | ||
| 3 | use Text::MeCab; | ||
| 4 | use namespace::clean -except => qw(meta); | ||
| 5 | |||
| 6 | extends 'Morris::Plugin'; | ||
| 7 | with 'Morris::Plugin::WithDB'; | ||
| 8 | |||
| 9 | has command => ( | ||
| 10 | is => 'rw', | ||
| 11 | isa => 'RegexpRef', | ||
| 12 | default => sub { qr/dan/ }, | ||
| 13 | coerce => 1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | my @facemarks = ( | ||
| 17 | '( ´・ω・`)', | ||
| 18 | '(,,゚Д゚)', | ||
| 19 | '(´∀`*)', | ||
| 20 | '(・∀・)', | ||
| 21 | '(゚Д゚ )', | ||
| 22 | '\(^o^)/', | ||
| 23 | '(#゚Д゚)', | ||
| 24 | '( ´_ゝ`)∂', | ||
| 25 | 'ヽ(`Д´)ノ', | ||
| 26 | ); | ||
| 27 | |||
| 28 | after register => sub { | ||
| 29 | my ($self, $conn) = @_; | ||
| 30 | $conn->register_hook( 'chat.privmsg', sub { $self->handle_message(@_) } ); | ||
| 31 | }; | ||
| 32 | |||
| 33 | after setup_dbh => sub { | ||
| 34 | my ($self, $dbh) = @_; | ||
| 35 | $dbh->exec(<<EOSQL, \&Morris::_noop_cb); | ||
| 36 | CREATE TABLE IF NOT EXISTS qotd ( | ||
| 37 | id integer auto_increment primary key, | ||
| 38 | channel text not null, | ||
| 39 | quote text not null, | ||
| 40 | ends_with_connector BOOLEAN NOT NULL DEFAULT 0, | ||
| 41 | created_on integer not null, | ||
| 42 | UNIQUE (channel, quote) | ||
| 43 | ); | ||
| 44 | EOSQL | ||
| 45 | $dbh->commit; | ||
| 46 | }; | ||
| 47 | |||
| 48 | sub handle_message { | ||
| 49 | my ($self, $msg) = @_; | ||
| 50 | |||
| 51 | my $command = $self->command; | ||
| 52 | my $message = $msg->message; | ||
| 53 | my $channel = $msg->channel; | ||
| 54 | my $quote; | ||
| 55 | |||
| 56 | if ( $message =~ m{^ | ||
| 57 | \s* | ||
| 58 | ! | ||
| 59 | ( | ||
| 60 | (?:$command)+ | ||
| 61 | ) | ||
| 62 | (?: | ||
| 63 | :? | ||
| 64 | \s+ | ||
| 65 | (?: | ||
| 66 | (?: | ||
| 67 | (forget\s+)? | ||
| 68 | (.*) | ||
| 69 | ) | ||
| 70 | | | ||
| 71 | (?: | ||
| 72 | talk | ||
| 73 | ) | ||
| 74 | ) | ||
| 75 | )? | ||
| 76 | $}x | ||
| 77 | ) { | ||
| 78 | # how many? | ||
| 79 | my $reply; | ||
| 80 | if ($2) { | ||
| 81 | $quote = $3; | ||
| 82 | $self->forget_quote( $msg, $3 ); | ||
| 83 | } elsif ($3) { | ||
| 84 | $self->insert_quote( $msg, $3 ); | ||
| 85 | } else { | ||
| 86 | my $x = $1; | ||
| 87 | my $count = scalar(my @a = ($x =~ /\G((?:$command))/g)); | ||
| 88 | |||
| 89 | $self->select_quote( $msg, $count ); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | sub ends_with_connector { | ||
| 95 | my ($self, $quote) = @_; | ||
| 96 | my $mecab = Text::MeCab->new(); | ||
| 97 | |||
| 98 | my $ret = 0; | ||
| 99 | my $prev; | ||
| 100 | for( my $node = $mecab->parse($quote); $node; $node = $node->next ) { | ||
| 101 | my $type = (split /,/, $node->feature)[0]; | ||
| 102 | if ($type =~ /[EB]OS/ && ($prev eq '接続詞' || $prev eq '助詞')) { | ||
| 103 | $ret = 1; | ||
| 104 | } | ||
| 105 | $prev = $type; | ||
| 106 | } | ||
| 107 | return $ret; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub forget_quote { | ||
| 111 | my ($self, $msg, $quote) = @_; | ||
| 112 | my $dbh = $self->get_dbh(); | ||
| 113 | $dbh->exec( | ||
| 114 | "DELETE FROM qotd WHERE channel = ? AND quote = ?", | ||
| 115 | $msg->channel, | ||
| 116 | $quote, | ||
| 117 | sub { | ||
| 118 | my $rv = $_[2]; | ||
| 119 | my $reply = ($rv > 0) ? | ||
| 120 | "「$quote」を忘れた! ($rv)" : | ||
| 121 | "「$quote」なんて無かった!" | ||
| 122 | ; | ||
| 123 | $self->connection->irc_notice({ | ||
| 124 | channel => $msg->channel, | ||
| 125 | message => $reply | ||
| 126 | }); | ||
| 127 | } | ||
| 128 | ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub insert_quote { | ||
| 132 | my ($self, $msg, $quote) = @_; | ||
| 133 | my $ends_with_connector = $self->ends_with_connector($quote); | ||
| 134 | my $dbh = $self->get_dbh(); | ||
| 135 | $dbh->exec( | ||
| 136 | "INSERT INTO qotd (channel, quote, ends_with_connector, created_on) VALUES (?, ?, ?, ?)", | ||
| 137 | $msg->channel, | ||
| 138 | $quote, | ||
| 139 | $ends_with_connector, | ||
| 140 | time(), | ||
| 141 | sub { | ||
| 142 | $dbh->exec( | ||
| 143 | "SELECT count(*) FROM qotd WHERE channel = ?", | ||
| 144 | $msg->channel, | ||
| 145 | sub { | ||
| 146 | my $rows = $_[1]; | ||
| 147 | $self->connection->irc_notice({ | ||
| 148 | channel => $msg->channel, | ||
| 149 | message => "$quote (登録数:$rows->[0]->[0])", | ||
| 150 | }); | ||
| 151 | } | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | ); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub select_quote { | ||
| 158 | my ($self, $msg, $count) = @_; | ||
| 159 | |||
| 160 | my $dbh = $self->get_dbh(); | ||
| 161 | |||
| 162 | # Do we have any items without a connector? | ||
| 163 | $dbh->exec( | ||
| 164 | "SELECT quote FROM qotd WHERE channel = ? AND ends_with_connector = 0 ORDER BY random() LIMIT 1", | ||
| 165 | $msg->channel, | ||
| 166 | sub { | ||
| 167 | my ($dbh, $rows, $rv) = @_; | ||
| 168 | if (scalar(@$rows) <= 0) { | ||
| 169 | # agh, nothing. bail out bail out | ||
| 170 | $self->connection->irc_notice({ | ||
| 171 | channel => $msg->channel, | ||
| 172 | message => $facemarks[ rand @facemarks ] . " < nothing to see here!" | ||
| 173 | }) | ||
| 174 | } else { | ||
| 175 | # proceed | ||
| 176 | $self->select_quote_more( $msg, $dbh, $rows->[0]->[0], $count ); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | ); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub select_quote_more { | ||
| 183 | my ($self, $msg, $dbh, $last, $count) = @_; | ||
| 184 | |||
| 185 | if ($count == 1) { | ||
| 186 | $self->connection->irc_notice({ | ||
| 187 | channel => $msg->channel, | ||
| 188 | message => $facemarks[ rand @facemarks ] . " < $last" | ||
| 189 | }); | ||
| 190 | } else { | ||
| 191 | $dbh->exec( | ||
| 192 | "SELECT quote FROM qotd WHERE channel = ? ORDER BY random() LIMIT " . ($count - 1), | ||
| 193 | $msg->channel, | ||
| 194 | sub { | ||
| 195 | my ($dbh, $rows, $rv) = @_; | ||
| 196 | |||
| 197 | return unless $rv; | ||
| 198 | |||
| 199 | $self->connection->irc_notice({ | ||
| 200 | channel => $msg->channel, | ||
| 201 | message => $facemarks[ rand @facemarks ] . " < " . join(' ', (map { $_->[0] } @$rows), $last) | ||
| 202 | }); | ||
| 203 | } | ||
| 204 | ); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | __PACKAGE__->meta->make_immutable; | ||
| 209 | |||
| 210 | |||
| 211 | 1; |