Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created January 5, 2016 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/662d9e8c923d97746b4e to your computer and use it in GitHub Desktop.
Save zoffixznet/662d9e8c923d97746b4e to your computer and use it in GitHub Desktop.
use IRC::Client::Plugin;
use DBIish;
use DBDish::SQLite::Connection;
unit class IRC::Client::Plugin::Factoid:ver<1.001001> is IRC::Client::Plugin;
has Regex $.trigger;
has Bool $.say-not-found = True;
has Str $.db-filename = 'factoids.db';
has DBDish::SQLite::Connection $!dbh;
method irc-start-up ($) {
my $need-deploy = not $!db-filename.IO.e;
$!dbh = DBIish.connect: "SQLite", :database($!db-filename), :RaiseError;
return unless $need-deploy;
$!dbh.do: q:to/END-SQL/;
CREATE TABLE factoids (
id INTEGER PRIMARY KEY,
fact TEXT,
def TEXT
);
END-SQL
}
method irc-to-me ($irc, $e, %res) {
return IRC_NOT_HANDLED
if $!trigger and %res<what>.subst-mutate: $!trigger, '';
%res<what> = do given %res<what> {
when /^ 'purge' \s+ 'factoid' \s+ $<fact>=(.+) \s*/ {
self!purge-fact: ~$<fact>;
}
when /^ 'delete' \s+ 'factoid' \s+ $<fact>=(.+) \s*/ {
self!delete-fact: ~$<fact>;
}
when /$<fact>=(.+) \s+ ':is:' \s+ $<def>=(.+)/ {
self!add-fact: ~$<fact>, ~$<def>;
}
default { self!find-fact: $_, :1limit; }
}
$irc.respond: |%res;
}
method !add-fact (Str $fact, Str $def) {
$!dbh.do: 'INSERT INTO factoids (fact, def) VALUES (?,?)', $<fact>, $<def>;
return "Added $fact as $def";
}
method !delete-fact (Str $fact) {
return "Didn't find $fact in the database"
unless self!find-facts: $fact, :1limit;
self!add-fact: $fact, '';
return "Marked factoid `$fact` as deleted";
}
method !find-facts (Str $fact, Int :$limit) {
my $sth;
my $sql = 'SELECT id FROM factoids WHERE fact = ? ';
if $limit {
$sth = $!dbh.prepare: $sql ~ 'LIMIT ?';
$sth.execute: $fact, $limit;
}
else {
$sth = $!dbh.prepare: $sql;
$sth.execute: $fact;
}
return $sth.fetchall-array;
}
method !purge-fact (Str $fact) {
my @facts = self!find-facts: ~$<fact>
or return "Did not find $<fact> in the database";
$!dbh.do: "DELETE FROM factoids WHERE id IN({ join ',', '?' xx @facts })";
return "Purged factoid `$fact` and its {@facts.elems} edits";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment