2020-01-25 23:13:57 +01:00
|
|
|
# File: Date.pm
|
|
|
|
#
|
|
|
|
# Purpose: Adds command to display time and date for timezones.
|
|
|
|
|
|
|
|
# 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/.
|
|
|
|
|
|
|
|
package Plugins::Date;
|
2020-02-09 04:48:05 +01:00
|
|
|
use parent 'Plugins::Plugin';
|
2020-01-25 23:13:57 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2020-01-25 23:13:57 +01:00
|
|
|
|
2020-02-16 23:13:41 +01:00
|
|
|
use Getopt::Long qw(GetOptionsFromArray);
|
2020-01-25 23:13:57 +01:00
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-06-24 01:42:15 +02:00
|
|
|
|
|
|
|
# add default registry entry for default timezone
|
|
|
|
# this can be overridden via arguments or user metadata
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'date', 'default_timezone', 'UTC');
|
2021-06-24 01:42:15 +02:00
|
|
|
|
|
|
|
# register `date` bot command
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_date(@_) }, "date", 0);
|
2020-01-25 23:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unload {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
$self->{pbot}->{commands}->unregister("date");
|
2020-01-25 23:13:57 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_date {
|
|
|
|
my ($self, $context) = @_;
|
2021-06-24 01:42:15 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $usage = "date [-u <user account>] [timezone]";
|
2021-06-24 01:42:15 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $getopt_error;
|
|
|
|
local $SIG{__WARN__} = sub {
|
|
|
|
$getopt_error = shift;
|
|
|
|
chomp $getopt_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
my ($user_override, $show_usage);
|
2020-05-04 22:21:35 +02:00
|
|
|
my @opt_args = $self->{pbot}->{interpreter}->split_line($context->{arguments}, strip_quotes => 1);
|
2021-06-24 01:42:15 +02:00
|
|
|
|
|
|
|
Getopt::Long::Configure("bundling");
|
2020-02-16 23:13:41 +01:00
|
|
|
GetOptionsFromArray(
|
|
|
|
\@opt_args,
|
2020-02-15 23:38:32 +01:00
|
|
|
'u=s' => \$user_override,
|
|
|
|
'h' => \$show_usage
|
|
|
|
);
|
|
|
|
|
|
|
|
return $usage if $show_usage;
|
|
|
|
return "/say $getopt_error -- $usage" if defined $getopt_error;
|
2021-06-24 01:42:15 +02:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
$context->{arguments} = "@opt_args";
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2020-04-28 02:05:20 +02:00
|
|
|
my $tz_override;
|
|
|
|
|
2021-06-24 01:42:15 +02:00
|
|
|
# check for user timezone metadata
|
2020-04-28 02:05:20 +02:00
|
|
|
if (defined $user_override) {
|
2021-07-09 23:39:35 +02:00
|
|
|
my $userdata = $self->{pbot}->{users}->{storage}->get_data($user_override);
|
2021-06-24 01:42:15 +02:00
|
|
|
|
|
|
|
if (not defined $userdata) {
|
|
|
|
return "No such user account $user_override. They may use the `my` command to create a user account and set their `timezone` user metadata."
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not exists $userdata->{timezone}) {
|
|
|
|
return "User account does not have `timezone` set. They may use the `my` command to set their `timezone` user metadata."
|
|
|
|
}
|
|
|
|
|
2020-04-28 02:05:20 +02:00
|
|
|
$tz_override = $userdata->{timezone};
|
|
|
|
} else {
|
2020-05-04 22:21:35 +02:00
|
|
|
$tz_override = $self->{pbot}->{users}->get_user_metadata($context->{from}, $context->{hostmask}, 'timezone') // '';
|
2020-04-28 02:05:20 +02:00
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-06-24 01:42:15 +02:00
|
|
|
# set default timezone
|
2020-02-15 23:38:32 +01:00
|
|
|
my $timezone = $self->{pbot}->{registry}->get_value('date', 'default_timezone') // 'UTC';
|
2021-06-24 01:42:15 +02:00
|
|
|
|
|
|
|
# override timezone with user metadata
|
2020-02-15 23:38:32 +01:00
|
|
|
$timezone = $tz_override if $tz_override;
|
|
|
|
|
2021-06-24 01:42:15 +02:00
|
|
|
# override timezone with bot command arguments
|
|
|
|
$timezone = $context->{arguments} if length $context->{arguments};
|
|
|
|
|
|
|
|
if (defined $user_override and not length $tz_override) {
|
|
|
|
return "No timezone set or user account does not exist.";
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-06-24 01:42:15 +02:00
|
|
|
# execute `date_module`
|
2020-05-02 05:59:51 +02:00
|
|
|
my $newcontext = {
|
2020-05-04 22:21:35 +02:00
|
|
|
from => $context->{from},
|
|
|
|
nick => $context->{nick},
|
|
|
|
user => $context->{user},
|
|
|
|
host => $context->{host},
|
2021-06-24 01:42:15 +02:00
|
|
|
hostmask => $context->{hostmask},
|
2020-05-04 22:21:35 +02:00
|
|
|
command => "date_module $timezone",
|
|
|
|
root_channel => $context->{from},
|
|
|
|
root_keyword => "date_module",
|
|
|
|
keyword => "date_module",
|
|
|
|
arguments => "$timezone"
|
2020-02-15 23:38:32 +01:00
|
|
|
};
|
|
|
|
|
2020-05-02 05:59:51 +02:00
|
|
|
$self->{pbot}->{modules}->execute_module($newcontext);
|
2020-01-25 23:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|