From 7c467a78fafb259e23044747d861e2fa16dd3940 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 29 Jan 2020 12:40:45 -0800 Subject: [PATCH] Plugins/Date: now accepts -u to override user account for timezone setting --- Plugins/Date.pm | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Plugins/Date.pm b/Plugins/Date.pm index ccadb15a..cc958a60 100644 --- a/Plugins/Date.pm +++ b/Plugins/Date.pm @@ -14,6 +14,7 @@ use strict; use feature 'unicode_strings'; +use Getopt::Long qw(GetOptionsFromString); use Carp (); sub new { @@ -39,11 +40,36 @@ sub unload { sub datecmd { my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_; + my $usage = "date [-u ] [timezone]"; + Getopt::Long::Configure("bundling"); + + my $getopt_error; + local $SIG{__WARN__} = sub { + $getopt_error = shift; + chomp $getopt_error; + }; + + my ($user_override, $show_usage); + my ($ret, $args) = GetOptionsFromString($arguments, + 'u=s' => \$user_override, + 'h' => \$show_usage + ); + + return $usage if $show_usage; + return "/say $getopt_error -- $usage" if defined $getopt_error; + $arguments = "@$args"; + + my $hostmask = defined $user_override ? $user_override : "$nick!$user\@$host"; + my $tz_override = $self->{pbot}->{users}->get_loggedin_user_metadata($from, $hostmask, 'timezone') // ''; + my $timezone = $self->{pbot}->{registry}->get_value('date', 'default_timezone') // 'UTC'; - my $tz_override = $self->{pbot}->{users}->get_loggedin_user_metadata($from, "$nick!$user\@$host", 'timezone'); $timezone = $tz_override if $tz_override; $timezone = $arguments if length $arguments; + if (defined $user_override and not length $tz_override) { + return "No timezone set or user account does not exist."; + } + my $newstuff = { from => $from, nick => $nick, user => $user, host => $host, command => "date_module $timezone", root_channel => $from, root_keyword => "date_module",