2020-01-25 14:13:57 -08:00
|
|
|
# File: Date.pm
|
|
|
|
#
|
|
|
|
# Purpose: Adds command to display time and date for timezones.
|
|
|
|
|
2023-02-20 21:31:52 -08:00
|
|
|
# SPDX-FileCopyrightText: 2020-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-10 15:00:22 -07:00
|
|
|
# SPDX-License-Identifier: MIT
|
2020-01-25 14:13:57 -08:00
|
|
|
|
2021-07-13 19:45:56 -07:00
|
|
|
package PBot::Plugin::Date;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
2020-01-25 14:13:57 -08:00
|
|
|
|
2021-06-18 21:23:34 -07:00
|
|
|
use PBot::Imports;
|
2020-01-25 14:13:57 -08:00
|
|
|
|
2023-04-13 17:01:23 -07:00
|
|
|
sub initialize($self, %conf) {
|
2021-06-23 16:42:15 -07:00
|
|
|
# add default registry entry for default timezone
|
|
|
|
# this can be overridden via arguments or user metadata
|
2020-02-15 14:38:32 -08:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'date', 'default_timezone', 'UTC');
|
2021-06-23 16:42:15 -07:00
|
|
|
|
|
|
|
# register `date` bot command
|
2021-07-30 19:01:24 -07:00
|
|
|
$self->{pbot}->{commands}->add(
|
|
|
|
name => 'date',
|
|
|
|
help => 'Show date and time',
|
|
|
|
subref => sub { $self->cmd_date(@_) },
|
|
|
|
);
|
2020-01-25 14:13:57 -08:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:01:23 -07:00
|
|
|
sub unload($self) {
|
2021-07-30 19:01:24 -07:00
|
|
|
$self->{pbot}->{commands}->remove('date');
|
2020-01-25 14:13:57 -08:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:01:23 -07:00
|
|
|
sub cmd_date($self, $context) {
|
2021-07-30 15:01:38 -07:00
|
|
|
my $usage = "Usage: date [-u <user account>] [timezone]";
|
2020-02-15 14:38:32 -08:00
|
|
|
|
2021-07-30 15:01:38 -07:00
|
|
|
my %opts;
|
2021-06-23 16:42:15 -07:00
|
|
|
|
2021-07-30 15:01:38 -07:00
|
|
|
my ($opt_args, $opt_error) = $self->{pbot}->{interpreter}->getopt(
|
|
|
|
$context->{arguments},
|
|
|
|
\%opts,
|
|
|
|
['bundling'],
|
|
|
|
'u=s',
|
|
|
|
'h',
|
2020-02-15 14:38:32 -08:00
|
|
|
);
|
|
|
|
|
2021-07-30 15:01:38 -07:00
|
|
|
return $usage if $opts{h};
|
|
|
|
return "/say $opt_error -- $usage" if $opt_error;
|
2021-06-23 16:42:15 -07:00
|
|
|
|
2021-07-30 15:01:38 -07:00
|
|
|
$context->{arguments} = "@$opt_args";
|
2020-02-15 14:38:32 -08:00
|
|
|
|
2021-07-30 15:01:38 -07:00
|
|
|
my $user_override = $opts{u};
|
2020-04-27 17:05:20 -07:00
|
|
|
my $tz_override;
|
|
|
|
|
2021-06-23 16:42:15 -07:00
|
|
|
# check for user timezone metadata
|
2020-04-27 17:05:20 -07:00
|
|
|
if (defined $user_override) {
|
2021-07-09 14:39:35 -07:00
|
|
|
my $userdata = $self->{pbot}->{users}->{storage}->get_data($user_override);
|
2021-06-23 16:42:15 -07: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-27 17:05:20 -07:00
|
|
|
$tz_override = $userdata->{timezone};
|
|
|
|
} else {
|
2020-05-04 13:21:35 -07:00
|
|
|
$tz_override = $self->{pbot}->{users}->get_user_metadata($context->{from}, $context->{hostmask}, 'timezone') // '';
|
2020-04-27 17:05:20 -07:00
|
|
|
}
|
2020-02-15 14:38:32 -08:00
|
|
|
|
2021-06-23 16:42:15 -07:00
|
|
|
# set default timezone
|
2020-02-15 14:38:32 -08:00
|
|
|
my $timezone = $self->{pbot}->{registry}->get_value('date', 'default_timezone') // 'UTC';
|
2021-06-23 16:42:15 -07:00
|
|
|
|
|
|
|
# override timezone with user metadata
|
2020-02-15 14:38:32 -08:00
|
|
|
$timezone = $tz_override if $tz_override;
|
|
|
|
|
2021-06-23 16:42:15 -07: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 14:38:32 -08:00
|
|
|
|
2021-11-19 18:05:50 -08:00
|
|
|
# execute `date_applet`
|
2020-05-01 20:59:51 -07:00
|
|
|
my $newcontext = {
|
2020-05-04 13:21:35 -07:00
|
|
|
from => $context->{from},
|
|
|
|
nick => $context->{nick},
|
|
|
|
user => $context->{user},
|
|
|
|
host => $context->{host},
|
2021-06-23 16:42:15 -07:00
|
|
|
hostmask => $context->{hostmask},
|
2021-11-19 18:05:50 -08:00
|
|
|
command => "date_applet $timezone",
|
2020-05-04 13:21:35 -07:00
|
|
|
root_channel => $context->{from},
|
2021-11-19 18:05:50 -08:00
|
|
|
root_keyword => "date_applet",
|
|
|
|
keyword => "date_applet",
|
2020-05-04 13:21:35 -07:00
|
|
|
arguments => "$timezone"
|
2020-02-15 14:38:32 -08:00
|
|
|
};
|
|
|
|
|
2021-11-19 18:05:50 -08:00
|
|
|
$self->{pbot}->{applets}->execute_applet($newcontext);
|
2020-01-25 14:13:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|