3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-16 00:49:35 +01:00
pbot/lib/PBot/Plugin/Date.pm

99 lines
2.9 KiB
Perl
Raw Normal View History

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