3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 19:22:40 +01:00

Progress on refactoring and polishing everything

This commit is contained in:
Pragmatic Software 2021-06-23 16:42:15 -07:00
parent 008bba9ed4
commit fec192ca69
3 changed files with 35 additions and 9 deletions

View File

@ -126,6 +126,7 @@ sub on_disconnect {
my ($self, $event_type, $event) = @_; my ($self, $event_type, $event) = @_;
$self->{pbot}->{logger}->log("Disconnected...\n"); $self->{pbot}->{logger}->log("Disconnected...\n");
$self->{pbot}->{connected} = 0; $self->{pbot}->{connected} = 0;
$self->{pbot}->connect;
return 0; return 0;
} }

View File

@ -287,7 +287,7 @@ sub connect {
} }
sub register_signal_handlers { sub register_signal_handlers {
my $self = shift; my ($self) = @_;
$SIG{INT} = sub { $SIG{INT} = sub {
my $msg = "SIGINT received, exiting immediately.\n"; my $msg = "SIGINT received, exiting immediately.\n";
@ -303,7 +303,7 @@ sub register_signal_handlers {
# called when PBot terminates # called when PBot terminates
sub atexit { sub atexit {
my $self = shift; my ($self) = @_;
$self->{atexit}->execute_all; $self->{atexit}->execute_all;
if (exists $self->{logger}) { if (exists $self->{logger}) {
$self->{logger}->log("Good-bye.\n"); $self->{logger}->log("Good-bye.\n");
@ -345,7 +345,7 @@ sub do_one_loop {
# main entry point # main entry point
sub start { sub start {
my $self = shift; my ($self) = @_;
$self->connect; $self->connect;

View File

@ -15,7 +15,12 @@ use Getopt::Long qw(GetOptionsFromArray);
sub initialize { sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
# add default registry entry for default timezone
# this can be overridden via arguments or user metadata
$self->{pbot}->{registry}->add_default('text', 'date', 'default_timezone', 'UTC'); $self->{pbot}->{registry}->add_default('text', 'date', 'default_timezone', 'UTC');
# register `date` bot command
$self->{pbot}->{commands}->register(sub { $self->cmd_date(@_) }, "date", 0); $self->{pbot}->{commands}->register(sub { $self->cmd_date(@_) }, "date", 0);
} }
@ -26,17 +31,19 @@ sub unload {
sub cmd_date { sub cmd_date {
my ($self, $context) = @_; my ($self, $context) = @_;
my $usage = "date [-u <user account>] [timezone]"; my $usage = "date [-u <user account>] [timezone]";
my $getopt_error; my $getopt_error;
local $SIG{__WARN__} = sub { local $SIG{__WARN__} = sub {
$getopt_error = shift; $getopt_error = shift;
chomp $getopt_error; chomp $getopt_error;
}; };
Getopt::Long::Configure("bundling");
my ($user_override, $show_usage); my ($user_override, $show_usage);
my @opt_args = $self->{pbot}->{interpreter}->split_line($context->{arguments}, strip_quotes => 1); my @opt_args = $self->{pbot}->{interpreter}->split_line($context->{arguments}, strip_quotes => 1);
Getopt::Long::Configure("bundling");
GetOptionsFromArray( GetOptionsFromArray(
\@opt_args, \@opt_args,
'u=s' => \$user_override, 'u=s' => \$user_override,
@ -45,30 +52,48 @@ sub cmd_date {
return $usage if $show_usage; return $usage if $show_usage;
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
$context->{arguments} = "@opt_args"; $context->{arguments} = "@opt_args";
my $tz_override; my $tz_override;
# check for user timezone metadata
if (defined $user_override) { if (defined $user_override) {
my $userdata = $self->{pbot}->{users}->{users}->get_data($user_override); my $userdata = $self->{pbot}->{users}->{users}->get_data($user_override);
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 defined $userdata;
return "User account does not have `timezone` set. They may use the `my` command to set their `timezone` user metadata." if not exists $userdata->{timezone}; 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}; $tz_override = $userdata->{timezone};
} else { } else {
$tz_override = $self->{pbot}->{users}->get_user_metadata($context->{from}, $context->{hostmask}, 'timezone') // ''; $tz_override = $self->{pbot}->{users}->get_user_metadata($context->{from}, $context->{hostmask}, 'timezone') // '';
} }
# set default timezone
my $timezone = $self->{pbot}->{registry}->get_value('date', 'default_timezone') // 'UTC'; my $timezone = $self->{pbot}->{registry}->get_value('date', 'default_timezone') // 'UTC';
# override timezone with user metadata
$timezone = $tz_override if $tz_override; $timezone = $tz_override if $tz_override;
$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."; } # 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.";
}
# execute `date_module`
my $newcontext = { my $newcontext = {
from => $context->{from}, from => $context->{from},
nick => $context->{nick}, nick => $context->{nick},
user => $context->{user}, user => $context->{user},
host => $context->{host}, host => $context->{host},
hostmask => $context->{hostmask},
command => "date_module $timezone", command => "date_module $timezone",
root_channel => $context->{from}, root_channel => $context->{from},
root_keyword => "date_module", root_keyword => "date_module",