From 883a62920d5783b935a4732fd350b5191db1fec0 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 22 Jun 2021 13:37:01 -0700 Subject: [PATCH] Progress on refactoring and polishing everything --- PBot/StdinReader.pm | 8 ++++---- PBot/Utils/Indefinite.pm | 8 +++++--- PBot/Utils/LWPUserAgentCached.pm | 13 ++++++++++--- PBot/Utils/ParseDate.pm | 12 ++++++------ PBot/Utils/SafeFilename.pm | 14 +++++++++++--- PBot/Utils/ValidateString.pm | 18 ++++++++++++------ 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/PBot/StdinReader.pm b/PBot/StdinReader.pm index 20c2eb2f..349af76f 100644 --- a/PBot/StdinReader.pm +++ b/PBot/StdinReader.pm @@ -45,15 +45,15 @@ sub initialize { sub stdin_reader { my ($self, $input) = @_; + # make sure we're in the foreground first + $self->{foreground} = (tcgetpgrp($self->{tty_fd}) == getpgrp()) ? 1 : 0; + return if not $self->{foreground}; + # decode STDIN input from utf8 $input = decode('UTF-8', $input); chomp $input; - # make sure we're in the foreground first - $self->{foreground} = (tcgetpgrp($self->{tty_fd}) == getpgrp()) ? 1 : 0; - return if not $self->{foreground}; - $self->{pbot}->{logger}->log("---------------------------------------------\n"); $self->{pbot}->{logger}->log("Got STDIN: $input\n"); diff --git a/PBot/Utils/Indefinite.pm b/PBot/Utils/Indefinite.pm index ea87f0da..84252e12 100644 --- a/PBot/Utils/Indefinite.pm +++ b/PBot/Utils/Indefinite.pm @@ -1,8 +1,10 @@ +# File: Indefinite.pm +# +# Purpose: Implements a/an inflexion for nouns. + package PBot::Utils::Indefinite; -use 5.010; use warnings; - -use feature 'unicode_strings'; +use PBot::Imports; require Exporter; our @ISA = qw/Exporter/; diff --git a/PBot/Utils/LWPUserAgentCached.pm b/PBot/Utils/LWPUserAgentCached.pm index 599925b6..a8b456e5 100644 --- a/PBot/Utils/LWPUserAgentCached.pm +++ b/PBot/Utils/LWPUserAgentCached.pm @@ -1,10 +1,17 @@ -package PBot::Utils::LWPUserAgentCached; -use strict; - +# File: LWPUserAgentCached.pm +# # Purpose: variant of LWP::UserAgent::WithCache. Instead of depending on # the 'expires' or 'Last-Modified' attributes, we always cache for the # specified duration. +# 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 PBot::Utils::LWPUserAgentCached; + +use PBot::Imports; + use base qw/LWP::UserAgent/; use Cache::FileCache; use File::HomeDir; diff --git a/PBot/Utils/ParseDate.pm b/PBot/Utils/ParseDate.pm index c490c55f..51bc7a01 100644 --- a/PBot/Utils/ParseDate.pm +++ b/PBot/Utils/ParseDate.pm @@ -1,16 +1,16 @@ -#!/usr/bin/env perl +# File: ParseDate.pm +# +# Purpose: Intelligently parses strings like "3pm", "5 minutes", "next week", +# etc, into seconds. # 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/. -use warnings; -use strict; - -use feature 'unicode_strings'; - package PBot::Utils::ParseDate; +use PBot::Imports; + use DateTime; use DateTime::Format::Flexible; use DateTime::Format::Duration; diff --git a/PBot/Utils/SafeFilename.pm b/PBot/Utils/SafeFilename.pm index 7d3f7d5e..238497e1 100644 --- a/PBot/Utils/SafeFilename.pm +++ b/PBot/Utils/SafeFilename.pm @@ -1,14 +1,22 @@ +# File: SafeFilename.pm +# +# Purpose: for strings containing filenames, translates potentially unsafe +# characters into safe expansions; e.g. "foo/bar" becomes "foo&fslash;bar". + +# 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 PBot::Utils::SafeFilename; -use 5.010; use warnings; -use feature 'unicode_strings'; +use PBot::Imports; require Exporter; our @ISA = qw/Exporter/; our @EXPORT = qw/safe_filename/; sub safe_filename { - my $name = shift; + my ($name) = @_; my $safe = ''; while ($name =~ m/(.)/gms) { diff --git a/PBot/Utils/ValidateString.pm b/PBot/Utils/ValidateString.pm index c28229be..e18586aa 100644 --- a/PBot/Utils/ValidateString.pm +++ b/PBot/Utils/ValidateString.pm @@ -1,12 +1,18 @@ +# File: ValidateString.pm +# +# Purpose: ensures that a given string conforms to PBot's limitations +# for internal strings. This means ensuring the string is not too long, +# does not have undesired characters, etc. + +# 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 PBot::Utils::ValidateString; -use warnings; -use strict; +use PBot::Imports; -use feature 'unicode_strings'; -use utf8; - -# export validate_string() subroutine +# export validate_string subroutine require Exporter; our @ISA = qw/Exporter/; our @EXPORT = qw/validate_string/;