2021-06-24 05:03:30 +02:00
|
|
|
# File: RemindMe.pm
|
|
|
|
#
|
|
|
|
# Purpose: Users can use `remindme` to set up reminders. Reminders are
|
2021-07-07 04:42:16 +02:00
|
|
|
# sent to the user (or to a channel, if option -c is used).
|
2021-06-24 05:03:30 +02:00
|
|
|
|
2017-08-12 10:24:26 +02:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2019-09-01 20:01:18 +02:00
|
|
|
package Plugins::RemindMe;
|
2020-02-09 04:48:05 +01:00
|
|
|
use parent 'Plugins::Plugin';
|
2017-08-12 10:24:26 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2017-08-12 10:24:26 +02:00
|
|
|
|
|
|
|
use DBI;
|
2021-07-07 04:42:16 +02:00
|
|
|
use Time::Duration qw/ago concise duration/;
|
2021-06-24 05:21:08 +02:00
|
|
|
use Time::HiRes qw/time/;
|
|
|
|
use Getopt::Long qw/GetOptionsFromArray/;
|
2017-08-12 10:24:26 +02:00
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# register `remindme` bot command
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_remindme(@_) }, 'remindme', 0);
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# set location of sqlite database
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{filename} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/reminders.sqlite3';
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# open the database
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->dbi_begin;
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# create tables, etc
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->create_database;
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# add saved reminders to event queue
|
2020-03-07 02:09:29 +01:00
|
|
|
$self->enqueue_reminders;
|
2017-08-12 10:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unload {
|
2021-06-24 05:03:30 +02:00
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
# close database
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->dbi_end;
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# unregister `remindme` command
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{commands}->unregister('remindme');
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# remove all reminder events from event queue
|
2021-06-22 02:26:24 +02:00
|
|
|
$self->{pbot}->{event_queue}->dequeue_event('reminder .*');
|
2020-03-07 02:09:29 +01:00
|
|
|
}
|
|
|
|
|
2021-06-24 05:03:30 +02:00
|
|
|
# `remindme` bot command
|
|
|
|
sub cmd_remindme {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
|
|
|
|
if (not $self->{dbh}) {
|
|
|
|
return "Internal error.";
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
my $usage = "Usage: remindme -t <time> <message> [-r <repeat count>] [-c <channel>] | remindme -l [nick] | remindme -d <id>";
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
return $usage if not length $context->{arguments};
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
my ($channel, $repeats, $text, $time, $list_reminders, $delete_id);
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
my $getopt_error;
|
|
|
|
local $SIG{__WARN__} = sub {
|
|
|
|
$getopt_error = shift;
|
|
|
|
chomp $getopt_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
my @opt_args = $self->{pbot}->{interpreter}->split_line($context->{arguments}, strip_quotes => 1);
|
|
|
|
|
|
|
|
Getopt::Long::Configure("bundling");
|
|
|
|
GetOptionsFromArray(
|
|
|
|
\@opt_args,
|
2021-07-09 05:07:00 +02:00
|
|
|
'r=i' => \$repeats,
|
|
|
|
't=s' => \$time,
|
|
|
|
'c=s' => \$channel,
|
|
|
|
'm=s' => \$text,
|
2021-06-24 05:03:30 +02:00
|
|
|
'l:s' => \$list_reminders,
|
2021-07-09 05:07:00 +02:00
|
|
|
'd=i' => \$delete_id
|
2021-06-24 05:03:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return "$getopt_error -- $usage" if defined $getopt_error;
|
|
|
|
|
|
|
|
# option -l was provided; list reminders
|
|
|
|
if (defined $list_reminders) {
|
2021-07-08 21:59:43 +02:00
|
|
|
# unique internal account id for a hostmask
|
2021-06-24 05:03:30 +02:00
|
|
|
my $account;
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# if arg was provided to -l, list reminders belonging to args
|
|
|
|
my $nick_override = $list_reminders if length $list_reminders;
|
|
|
|
|
2021-06-24 05:03:30 +02:00
|
|
|
if ($nick_override) {
|
2021-07-08 21:59:43 +02:00
|
|
|
# look up account id and hostmask for -l argument
|
2021-06-24 05:03:30 +02:00
|
|
|
my $hostmask;
|
|
|
|
|
|
|
|
($account, $hostmask) = $self->{pbot}->{messagehistory}->{database}->find_message_account_by_nick($nick_override);
|
|
|
|
|
|
|
|
if (not $account) {
|
|
|
|
return "I don't know anybody named $nick_override.";
|
|
|
|
}
|
|
|
|
|
|
|
|
# capture nick portion of hostmask
|
|
|
|
($nick_override) = $hostmask =~ m/^([^!]+)!/;
|
|
|
|
} else {
|
2021-07-08 21:59:43 +02:00
|
|
|
# look up caller's account id
|
2021-06-24 05:03:30 +02:00
|
|
|
$account = $self->{pbot}->{messagehistory}->{database}->get_message_account($context->{nick}, $context->{user}, $context->{host});
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# get the root parent account id (consolidates nick-changes, etc)
|
2021-06-24 05:03:30 +02:00
|
|
|
$account = $self->{pbot}->{messagehistory}->{database}->get_ancestor_id($account);
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# get the reminders
|
2021-06-24 05:03:30 +02:00
|
|
|
my $reminders = $self->get_reminders($account);
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# list the reminders
|
2021-06-24 05:03:30 +02:00
|
|
|
my $count = 0;
|
|
|
|
my $text = '';
|
|
|
|
my $now = time;
|
|
|
|
|
|
|
|
foreach my $reminder (@$reminders) {
|
2021-07-07 04:42:16 +02:00
|
|
|
my $interval = $reminder->{alarm} - $now;
|
|
|
|
|
|
|
|
if ($interval < 0) {
|
|
|
|
$interval = 'missed ' . concise ago -$interval;
|
|
|
|
} else {
|
|
|
|
$interval = 'in ' . concise duration $interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
$text .= "$reminder->{id}) $interval";
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
if ($reminder->{repeats}) {
|
2021-07-08 21:59:43 +02:00
|
|
|
$text .= " (repeats every $reminder->{interval}, $reminder->{repeats} more time" . ($reminder->{repeats} == 1 ? '' : 's') . ')';
|
2021-06-24 05:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$text .= ": $reminder->{text}\n";
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not $count) {
|
|
|
|
if ($nick_override) {
|
|
|
|
return "$nick_override has no reminders.";
|
|
|
|
} else {
|
|
|
|
return "You have no reminders.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# reuse $reminders variable to store this text
|
|
|
|
$reminders = $count == 1 ? 'reminder' : 'reminders';
|
|
|
|
|
|
|
|
return "$count $reminders: $text";
|
|
|
|
}
|
|
|
|
|
|
|
|
# option -d was provided; delete a reminder
|
|
|
|
if ($delete_id) {
|
|
|
|
my $reminder = $self->get_reminder($delete_id);
|
|
|
|
|
|
|
|
if (not $reminder) {
|
|
|
|
return "Reminder $delete_id does not exist.";
|
|
|
|
}
|
|
|
|
|
2021-07-07 04:42:16 +02:00
|
|
|
# admins can delete any reminder
|
|
|
|
my $admin = $self->{pbot}->{users}->loggedin_admin($channel ? $channel : $context->{from}, $context->{hostmask});
|
|
|
|
|
|
|
|
if (not $admin) {
|
|
|
|
# not an admin, check if they own this reminder
|
|
|
|
my $account = $self->{pbot}->{messagehistory}->{database}->get_message_account($context->{nick}, $context->{user}, $context->{host});
|
|
|
|
$account = $self->{pbot}->{messagehistory}->{database}->get_ancestor_id($account);
|
|
|
|
|
|
|
|
if ($reminder->{account} != $account) {
|
|
|
|
return "Reminder $delete_id does not belong to you.";
|
|
|
|
}
|
2021-06-24 05:03:30 +02:00
|
|
|
}
|
|
|
|
|
2021-07-07 04:42:16 +02:00
|
|
|
# delete reminder
|
2021-06-24 05:03:30 +02:00
|
|
|
if ($self->delete_reminder($delete_id)) {
|
|
|
|
return "Reminder $delete_id ($reminder->{text}) deleted.";
|
|
|
|
} else {
|
|
|
|
return "Could not delete reminder $delete_id.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# otherwise we're adding a reminder
|
|
|
|
|
2021-07-07 04:42:16 +02:00
|
|
|
# if -m wasn't provided set text to ''
|
2021-06-24 05:03:30 +02:00
|
|
|
$text //= '';
|
|
|
|
|
|
|
|
# add to the reminder text anything left in the arguments
|
|
|
|
if (@opt_args) {
|
2021-06-25 04:17:16 +02:00
|
|
|
$text .= ' ' if length $text;
|
|
|
|
$text .= join ' ', @opt_args;
|
2021-06-24 05:03:30 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
return "Please use -t to specify a time for this reminder." if not $time;
|
2021-06-24 05:03:30 +02:00
|
|
|
return "Please specify a reminder message." if not $text;
|
|
|
|
|
2021-07-09 22:52:08 +02:00
|
|
|
# ensure option -c is a channel
|
|
|
|
if (defined $channel and $channel !~ /^#/) {
|
|
|
|
return "Option -c must be a channel.";
|
|
|
|
}
|
|
|
|
|
2021-06-30 08:39:11 +02:00
|
|
|
# option -c was provided; ensure bot is in channel
|
|
|
|
if ($channel and not $self->{pbot}->{channels}->is_active($channel)) {
|
|
|
|
return "I'm not active in channel $channel.";
|
2021-06-24 05:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# parse "5 minutes", "next week", "3pm", etc into seconds
|
2021-07-08 21:59:43 +02:00
|
|
|
my ($seconds, $error) = $self->{pbot}->{parsedate}->parsedate($time);
|
2021-06-24 05:03:30 +02:00
|
|
|
return $error if $error;
|
|
|
|
|
|
|
|
if ($seconds > 31536000 * 10) {
|
|
|
|
return "Come on now, I'll be dead by then.";
|
|
|
|
}
|
|
|
|
|
|
|
|
# set repeats to 0 if option -r was not provided
|
|
|
|
$repeats //= 0;
|
|
|
|
|
2021-07-07 04:42:16 +02:00
|
|
|
# get user account if user is an admin, undef otherwise
|
|
|
|
my $admin = $self->{pbot}->{users}->loggedin_admin($channel ? $channel : $context->{from}, $context->{hostmask});
|
|
|
|
|
2021-06-24 05:03:30 +02:00
|
|
|
# prevent non-admins from abusing repeat
|
2021-07-07 04:42:16 +02:00
|
|
|
if (not defined $admin and $repeats > 20) {
|
2021-06-24 05:03:30 +02:00
|
|
|
return "You may only set up to 20 repeats.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($repeats < 0) {
|
2021-07-07 04:42:16 +02:00
|
|
|
return "Repeats cannot be negative.";
|
2021-06-24 05:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# set timestamp for alarm
|
2021-07-08 21:59:43 +02:00
|
|
|
my $alarm = time + $seconds;
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
my $account = $self->{pbot}->{messagehistory}->{database}->get_message_account($context->{nick}, $context->{user}, $context->{host});
|
|
|
|
$account = $self->{pbot}->{messagehistory}->{database}->get_ancestor_id($account);
|
|
|
|
|
|
|
|
# limit maximum reminders for non-admin users
|
2021-07-07 04:42:16 +02:00
|
|
|
if (not defined $admin) {
|
2021-06-24 05:03:30 +02:00
|
|
|
my $reminders = $self->get_reminders($account);
|
|
|
|
if (@$reminders >= 50) {
|
|
|
|
return "You may only set 50 reminders at a time. Use `remindme -d id` to remove a reminder.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $id = $self->add_reminder(
|
|
|
|
account => $account,
|
|
|
|
channel => $channel,
|
|
|
|
text => $text,
|
|
|
|
alarm => $alarm,
|
2021-07-08 21:59:43 +02:00
|
|
|
interval => $time,
|
|
|
|
seconds => $seconds,
|
2021-06-24 05:03:30 +02:00
|
|
|
repeats => $repeats,
|
2021-07-07 04:42:16 +02:00
|
|
|
owner => $context->{hostmask},
|
2021-06-24 05:03:30 +02:00
|
|
|
);
|
|
|
|
|
2021-07-07 04:42:16 +02:00
|
|
|
my $duration = concise duration $seconds;
|
|
|
|
|
|
|
|
if ($repeats) {
|
|
|
|
$duration .= " repeating $repeats time" . ($repeats == 1 ? '' : 's');
|
|
|
|
}
|
|
|
|
|
2021-06-24 05:03:30 +02:00
|
|
|
if ($id) {
|
2021-07-07 04:42:16 +02:00
|
|
|
return "Reminder $id added (in $duration).";
|
2021-06-24 05:03:30 +02:00
|
|
|
} else {
|
|
|
|
return "Failed to add reminder.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# invoked whenever a reminder event is ready
|
|
|
|
sub do_reminder {
|
|
|
|
my ($self, $id, $event) = @_;
|
|
|
|
|
|
|
|
my $reminder = $self->get_reminder($id);
|
|
|
|
|
|
|
|
if (not defined $reminder) {
|
|
|
|
$self->{pbot}->{logger}->log("Queued reminder $id no longer exists.\n");
|
|
|
|
|
|
|
|
# unset `repeating` flag for event item in PBot event queue
|
|
|
|
$event->{repeating} = 0;
|
|
|
|
|
|
|
|
# nothing to do
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-24 05:21:08 +02:00
|
|
|
# ensures we get the current nick of the person being reminded
|
|
|
|
my $hostmask = $self->{pbot}->{messagehistory}->{database}->find_most_recent_hostmask($reminder->{account});
|
|
|
|
my ($nick) = $hostmask =~ /^([^!]+)!/;
|
2021-06-24 05:03:30 +02:00
|
|
|
|
2021-06-24 05:21:08 +02:00
|
|
|
# try again in 30 seconds if the person isn't around yet
|
|
|
|
if (not $self->{pbot}->{nicklist}->is_present_any_channel($nick)) {
|
|
|
|
$event->{interval} = 30;
|
|
|
|
$event->{repeating} = 1;
|
|
|
|
return;
|
2021-06-24 05:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# send reminder text to person
|
2021-07-09 22:52:08 +02:00
|
|
|
my $target = $reminder->{channel} || $nick;
|
2021-06-24 05:03:30 +02:00
|
|
|
my $text = $reminder->{text};
|
|
|
|
|
|
|
|
# if sending reminder to channel, highlight person being reminded
|
|
|
|
if ($target =~ /^#/) {
|
|
|
|
$text = "$nick: $text";
|
|
|
|
}
|
|
|
|
|
2021-06-24 05:21:08 +02:00
|
|
|
$self->{pbot}->{conn}->privmsg($target, $text);
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# log event
|
2021-07-08 21:59:43 +02:00
|
|
|
$self->{pbot}->{logger}->log("Reminded $target about \"$text\"; interval: $reminder->{interval}\n");
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# update repeats or delete reminder
|
|
|
|
if ($reminder->{repeats} > 0) {
|
|
|
|
# update reminder
|
|
|
|
$reminder->{repeats}--;
|
2021-07-08 21:59:43 +02:00
|
|
|
|
|
|
|
# parse interval again to get correct offset in seconds
|
|
|
|
# e.g., if it's 12 pm and they set a repeating reminder for 3 pm then
|
|
|
|
# the interval would be 3h. we don't want the reminder to repeat every
|
|
|
|
# 3h but instead every day at 3 pm. so when this reminder fires at 3 pm,
|
|
|
|
# we reparse the interval "3 pm" again to get 24h, instead of storing 3h.
|
|
|
|
my ($seconds) = $self->{pbot}->{parsedate}->parsedate($reminder->{interval});
|
|
|
|
|
|
|
|
# if timeout is 0 or less, prepend "next" and try again.
|
|
|
|
# e.g., if interval is "10 pm" then at 10:00:00 pm the interval will
|
|
|
|
# parse to 0 seconds, i.e. right now, until it is 10:00:01 pm. we really
|
|
|
|
# want the next 10 pm, 24 hours from right now.
|
|
|
|
if ($seconds <= 0) {
|
|
|
|
my $override;
|
|
|
|
if ($reminder->{interval} =~ m/^\d/) {
|
|
|
|
$override = "tomorrow ";
|
|
|
|
} else {
|
|
|
|
$override = "next ";
|
|
|
|
}
|
|
|
|
|
|
|
|
($seconds) = $self->{pbot}->{parsedate}->parsedate("$override $reminder->{interval}");
|
|
|
|
}
|
|
|
|
|
|
|
|
# update alarm timestamp
|
|
|
|
$reminder->{alarm} = time + $seconds;
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
# update reminder in SQLite database
|
|
|
|
my $data = { repeats => $reminder->{repeats}, alarm => $reminder->{alarm} };
|
|
|
|
$self->update_reminder($reminder->{id}, $data);
|
|
|
|
|
|
|
|
# update reminder event in PBot event queue
|
2021-07-08 21:59:43 +02:00
|
|
|
$event->{interval} = $seconds;
|
2021-06-24 05:03:30 +02:00
|
|
|
$event->{repeating} = 1;
|
|
|
|
} else {
|
2021-07-08 21:59:43 +02:00
|
|
|
# delete reminder from SQLite database
|
2021-06-24 05:03:30 +02:00
|
|
|
$self->delete_reminder($reminder->{id});
|
2021-07-08 21:59:43 +02:00
|
|
|
|
|
|
|
# tell PBot event queue not to reschedule this reminder
|
2021-06-24 05:03:30 +02:00
|
|
|
$event->{repeating} = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# add a single reminder to the PBot event queue
|
|
|
|
sub enqueue_reminder {
|
|
|
|
my ($self, $reminder, $timeout) = @_;
|
|
|
|
|
|
|
|
$self->{pbot}->{event_queue}->enqueue_event(
|
|
|
|
sub {
|
|
|
|
my ($event) = @_;
|
|
|
|
$self->do_reminder($reminder->{id}, $event);
|
|
|
|
},
|
|
|
|
$timeout, "reminder $reminder->{id}", $reminder->{repeats}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-24 05:03:30 +02:00
|
|
|
# load all reminders from SQLite database and add them
|
|
|
|
# to PBot's event queue. typically used once at PBot start-up.
|
2020-03-07 02:09:29 +01:00
|
|
|
sub enqueue_reminders {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
return if not $self->{dbh};
|
|
|
|
|
|
|
|
my $reminders = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('SELECT * FROM Reminders');
|
|
|
|
$sth->execute;
|
|
|
|
return $sth->fetchall_arrayref({});
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
$self->{pbot}->{logger}->log("Enqueue reminders failed: $@");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $reminder (@$reminders) {
|
2021-06-24 05:03:30 +02:00
|
|
|
my $timeout = $reminder->{alarm} - time;
|
|
|
|
|
2021-07-07 04:42:16 +02:00
|
|
|
# delete this reminder if it's expired by approximately 1 year
|
|
|
|
if ($timeout <= -(86400 * 31 * 12)) {
|
2021-07-08 21:59:43 +02:00
|
|
|
$self->{pbot}->{logger}->log("Deleting expired reminder: $reminder->{id}) $reminder->{text} set by $reminder->{created_by}\n");
|
2020-03-07 02:09:29 +01:00
|
|
|
$self->delete_reminder($reminder->{id});
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
$self->enqueue_reminder($reminder, $timeout);
|
2020-03-07 02:09:29 +01:00
|
|
|
}
|
2017-08-12 10:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub create_database {
|
2021-06-24 05:03:30 +02:00
|
|
|
my ($self) = @_;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return if not $self->{dbh};
|
2017-08-12 10:24:26 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
eval {
|
|
|
|
$self->{dbh}->do(<<SQL);
|
2017-08-12 10:24:26 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS Reminders (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
account TEXT,
|
2021-06-24 05:21:08 +02:00
|
|
|
channel TEXT,
|
2017-08-12 10:24:26 +02:00
|
|
|
text TEXT,
|
|
|
|
alarm NUMERIC,
|
2021-06-24 05:03:30 +02:00
|
|
|
repeats INTEGER,
|
2021-07-08 21:59:43 +02:00
|
|
|
interval TEXT,
|
2017-08-12 10:24:26 +02:00
|
|
|
created_on NUMERIC,
|
|
|
|
created_by TEXT
|
|
|
|
)
|
|
|
|
SQL
|
2020-02-15 23:38:32 +01:00
|
|
|
};
|
2017-08-12 10:24:26 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("RemindMe: create database failed: $@") if $@;
|
2017-08-12 10:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub dbi_begin {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
eval {
|
|
|
|
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", {RaiseError => 1, PrintError => 0, AutoInactiveDestroy => 1, sqlite_unicode => 1})
|
|
|
|
or die $DBI::errstr;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
$self->{pbot}->{logger}->log("Error opening RemindMe database: $@");
|
|
|
|
delete $self->{dbh};
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-12 10:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub dbi_end {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
return if not $self->{dbh};
|
|
|
|
$self->{dbh}->disconnect;
|
|
|
|
delete $self->{dbh};
|
2017-08-12 10:24:26 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# add a reminder, to SQLite and the PBot event queue
|
2017-08-12 10:24:26 +02:00
|
|
|
sub add_reminder {
|
2021-06-24 05:03:30 +02:00
|
|
|
my ($self, %args) = @_;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# add reminder to SQLite database
|
2020-03-07 02:09:29 +01:00
|
|
|
my $id = eval {
|
2021-06-24 05:21:08 +02:00
|
|
|
my $sth = $self->{dbh}->prepare('INSERT INTO Reminders (account, channel, text, alarm, interval, repeats, created_on, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
|
2021-06-24 05:03:30 +02:00
|
|
|
|
|
|
|
$sth->execute(
|
|
|
|
$args{account},
|
2021-06-24 05:21:08 +02:00
|
|
|
$args{channel},
|
2021-06-24 05:03:30 +02:00
|
|
|
$args{text},
|
|
|
|
$args{alarm},
|
|
|
|
$args{interval},
|
|
|
|
$args{repeats},
|
|
|
|
scalar time,
|
|
|
|
$args{owner},
|
|
|
|
);
|
|
|
|
|
2020-03-07 02:09:29 +01:00
|
|
|
return $self->{dbh}->sqlite_last_insert_rowid;
|
2020-02-15 23:38:32 +01:00
|
|
|
};
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# check for exception
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($@) {
|
|
|
|
$self->{pbot}->{logger}->log("Add reminder failed: $@");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-07 02:09:29 +01:00
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
my $reminder = {
|
|
|
|
id => $id,
|
|
|
|
repeats => $args{repeats},
|
|
|
|
};
|
|
|
|
|
|
|
|
# add reminder to event queue.
|
|
|
|
$self->enqueue_reminder($reminder, $args{seconds});
|
2020-03-07 02:09:29 +01:00
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# return reminder id
|
2020-03-07 02:09:29 +01:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# delete a reminder by its id, from SQLite and the PBot event queue
|
2020-03-07 02:09:29 +01:00
|
|
|
sub delete_reminder {
|
|
|
|
my ($self, $id) = @_;
|
2021-06-24 05:03:30 +02:00
|
|
|
|
2020-03-07 02:09:29 +01:00
|
|
|
return if not $self->{dbh};
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# remove from SQLite database
|
2020-03-07 02:09:29 +01:00
|
|
|
eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('DELETE FROM Reminders WHERE id = ?');
|
|
|
|
$sth->execute($id);
|
|
|
|
};
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# check for exeption
|
2020-03-07 02:09:29 +01:00
|
|
|
if ($@) {
|
|
|
|
$self->{pbot}->{logger}->log("Delete reminder $id failed: $@");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# remove from event queue
|
|
|
|
my $removed = $self->{pbot}->{event_queue}->dequeue_event("reminder $id");
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("RemindMe: dequeued events: $removed\n");
|
2020-02-15 23:38:32 +01:00
|
|
|
return 1;
|
2017-08-12 10:24:26 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# update a reminder's data, in SQLite
|
2017-08-13 01:18:25 +02:00
|
|
|
sub update_reminder {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $id, $data) = @_;
|
2017-08-13 01:18:25 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
eval {
|
|
|
|
my $sql = 'UPDATE Reminders SET ';
|
2017-08-13 01:18:25 +02:00
|
|
|
|
2021-06-24 05:03:30 +02:00
|
|
|
my @fields;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $key (keys %$data) {
|
2021-06-24 05:03:30 +02:00
|
|
|
push @fields, "$key = ?";
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2017-08-13 01:18:25 +02:00
|
|
|
|
2021-06-24 05:03:30 +02:00
|
|
|
$sql .= join ', ', @fields;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$sql .= ' WHERE id = ?';
|
2017-08-13 01:18:25 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $sth = $self->{dbh}->prepare($sql);
|
2017-08-13 01:18:25 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $param = 1;
|
2021-06-24 05:03:30 +02:00
|
|
|
foreach my $key (keys %$data) {
|
|
|
|
$sth->bind_param($param++, $data->{$key});
|
|
|
|
}
|
2017-08-13 01:18:25 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$sth->bind_param($param++, $id);
|
2021-06-24 05:03:30 +02:00
|
|
|
$sth->execute;
|
2020-02-15 23:38:32 +01:00
|
|
|
};
|
2021-06-24 05:03:30 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log($@) if $@;
|
2017-08-13 01:18:25 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# get a single reminder by its id, from SQLite
|
2017-08-12 13:03:37 +02:00
|
|
|
sub get_reminder {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $id) = @_;
|
|
|
|
|
|
|
|
my $reminder = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('SELECT * FROM Reminders WHERE id = ?');
|
|
|
|
$sth->execute($id);
|
|
|
|
return $sth->fetchrow_hashref();
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
2020-03-07 02:09:29 +01:00
|
|
|
$self->{pbot}->{logger}->log("Get reminder failed: $@");
|
2020-02-15 23:38:32 +01:00
|
|
|
return undef;
|
|
|
|
}
|
2021-06-24 05:03:30 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $reminder;
|
2017-08-12 13:03:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:59:43 +02:00
|
|
|
# get all reminders belonging to an account id, from SQLite
|
2017-08-12 13:03:37 +02:00
|
|
|
sub get_reminders {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $account) = @_;
|
2017-08-12 13:03:37 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $reminders = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('SELECT * FROM Reminders WHERE account = ? ORDER BY id');
|
|
|
|
$sth->execute($account);
|
|
|
|
return $sth->fetchall_arrayref({});
|
|
|
|
};
|
2017-08-12 13:03:37 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($@) {
|
2020-03-07 02:09:29 +01:00
|
|
|
$self->{pbot}->{logger}->log("Get reminders failed: $@");
|
2020-02-15 23:38:32 +01:00
|
|
|
return [];
|
|
|
|
}
|
2017-08-12 13:03:37 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $reminders;
|
2017-08-12 13:03:37 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 10:24:26 +02:00
|
|
|
1;
|