<commit_before># File: Date.pm
# Author: pragma-
#
# Purpose: Adds command to display time and date for timezones.

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

package Plugins::Date;

use warnings;
use strict;

use feature 'unicode_strings';

use Carp ();

sub new {
  Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref $_[1] eq 'HASH';
  my ($class, %conf) = @_;
  my $self = bless {}, $class;
  $self->initialize(%conf);
  return $self;
}

sub initialize {
  my ($self, %conf) = @_;
  $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
  $self->{pbot}->{registry}->add_default('text', 'date', 'default_timezone', 'UTC');
  $self->{pbot}->{commands}->register(sub { $self->datecmd(@_) },  "date", 0);
}

sub unload {
  my $self = shift;
  $self->{pbot}->{commands}->unregister("date");
}

sub datecmd {
  my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;

  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;

  my $newstuff = {
    from => $from, nick => $nick, user => $user, host => $host,
    command => "date_module $timezone", root_channel => $from, root_keyword => "date_module",
    keyword => "date_module", arguments => "$timezone"
  };

  $self->{pbot}->{factoids}->{factoidmodulelauncher}->execute_module($newstuff);
}<commit_msg>Plugins/Date: now accepts -u to override user account for timezone setting<commit_after># File: Date.pm
# Author: pragma-
#
# Purpose: Adds command to display time and date for timezones.

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

package Plugins::Date;

use warnings;
use strict;

use feature 'unicode_strings';

use Getopt::Long qw(GetOptionsFromString);
use Carp ();

sub new {
  Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref $_[1] eq 'HASH';
  my ($class, %conf) = @_;
  my $self = bless {}, $class;
  $self->initialize(%conf);
  return $self;
}

sub initialize {
  my ($self, %conf) = @_;
  $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
  $self->{pbot}->{registry}->add_default('text', 'date', 'default_timezone', 'UTC');
  $self->{pbot}->{commands}->register(sub { $self->datecmd(@_) },  "date", 0);
}

sub unload {
  my $self = shift;
  $self->{pbot}->{commands}->unregister("date");
}

sub datecmd {
  my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;

  my $usage = "date [-u <user account>] [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';
  $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",
    keyword => "date_module", arguments => "$timezone"
  };

  $self->{pbot}->{factoids}->{factoidmodulelauncher}->execute_module($newstuff);
}
{'fraction_non_alphanumeric': 0.18444548872180452, 'fraction_numerical': 0.002819548872180451, 'mean_word_length': 4.2881987577639755, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2705160', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1492, 'n_words': 405}