From 71031b88a768a04d2c1684f76dc433562b09457c Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 29 Jan 2020 12:02:08 -0800 Subject: [PATCH] Plugins/Weather: `weather` can now accept a -u option to specify user account --- Plugins/Weather.pm | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Plugins/Weather.pm b/Plugins/Weather.pm index 338f5525..2ed53732 100644 --- a/Plugins/Weather.pm +++ b/Plugins/Weather.pm @@ -16,6 +16,7 @@ use feature 'unicode_strings'; use LWP::UserAgent::WithCache; use XML::LibXML; +use Getopt::Long qw(GetOptionsFromString); use Carp (); sub new { @@ -40,12 +41,35 @@ sub unload { sub weathercmd { my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_; - my $location_override = $self->{pbot}->{users}->get_loggedin_user_metadata($from, "$nick!$user\@$host", 'location') // ''; + my $usage = "Usage: weather [-u ] "; + 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 $location_override = $self->{pbot}->{users}->get_loggedin_user_metadata($from, $hostmask, 'location') // ''; $arguments = $location_override if not length $arguments; + if (defined $user_override and not length $location_override) { + return "User account $user_override not found."; + } + if (not length $arguments) { - return "Usage: weather "; + return $usage; } return $self->get_weather($arguments);