From fec192ca692772cb05a93f8ac6bada2279d0692f Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 23 Jun 2021 16:42:15 -0700 Subject: [PATCH] Progress on refactoring and polishing everything --- PBot/IRCHandlers.pm | 1 + PBot/PBot.pm | 6 +++--- Plugins/Date.pm | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/PBot/IRCHandlers.pm b/PBot/IRCHandlers.pm index eeae8fad..23e6c686 100644 --- a/PBot/IRCHandlers.pm +++ b/PBot/IRCHandlers.pm @@ -126,6 +126,7 @@ sub on_disconnect { my ($self, $event_type, $event) = @_; $self->{pbot}->{logger}->log("Disconnected...\n"); $self->{pbot}->{connected} = 0; + $self->{pbot}->connect; return 0; } diff --git a/PBot/PBot.pm b/PBot/PBot.pm index 857603ba..57bd967f 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -287,7 +287,7 @@ sub connect { } sub register_signal_handlers { - my $self = shift; + my ($self) = @_; $SIG{INT} = sub { my $msg = "SIGINT received, exiting immediately.\n"; @@ -303,7 +303,7 @@ sub register_signal_handlers { # called when PBot terminates sub atexit { - my $self = shift; + my ($self) = @_; $self->{atexit}->execute_all; if (exists $self->{logger}) { $self->{logger}->log("Good-bye.\n"); @@ -345,7 +345,7 @@ sub do_one_loop { # main entry point sub start { - my $self = shift; + my ($self) = @_; $self->connect; diff --git a/Plugins/Date.pm b/Plugins/Date.pm index 210216f9..b8541a2c 100644 --- a/Plugins/Date.pm +++ b/Plugins/Date.pm @@ -15,7 +15,12 @@ use Getopt::Long qw(GetOptionsFromArray); sub initialize { 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'); + + # register `date` bot command $self->{pbot}->{commands}->register(sub { $self->cmd_date(@_) }, "date", 0); } @@ -26,17 +31,19 @@ sub unload { sub cmd_date { my ($self, $context) = @_; + my $usage = "date [-u ] [timezone]"; + my $getopt_error; local $SIG{__WARN__} = sub { $getopt_error = shift; chomp $getopt_error; }; - Getopt::Long::Configure("bundling"); - my ($user_override, $show_usage); my @opt_args = $self->{pbot}->{interpreter}->split_line($context->{arguments}, strip_quotes => 1); + + Getopt::Long::Configure("bundling"); GetOptionsFromArray( \@opt_args, 'u=s' => \$user_override, @@ -45,30 +52,48 @@ sub cmd_date { return $usage if $show_usage; return "/say $getopt_error -- $usage" if defined $getopt_error; + $context->{arguments} = "@opt_args"; my $tz_override; + # check for user timezone metadata if (defined $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}; } else { $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'; + + # override timezone with user metadata $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 = { from => $context->{from}, nick => $context->{nick}, user => $context->{user}, host => $context->{host}, + hostmask => $context->{hostmask}, command => "date_module $timezone", root_channel => $context->{from}, root_keyword => "date_module",