2014-05-06 07:15:27 +02:00
|
|
|
# File: Quotegrabs_Hashtable.pm
|
|
|
|
#
|
|
|
|
# Purpose: Hashtable backend for storing and retreiving quotegrabs
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
package PBot::Plugin::Quotegrabs::Quotegrabs_Hashtable;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2014-05-06 07:15:27 +02:00
|
|
|
use HTML::Entities;
|
|
|
|
use Time::Duration;
|
|
|
|
use Time::HiRes qw(gettimeofday);
|
|
|
|
use Getopt::Long qw(GetOptionsFromString);
|
|
|
|
|
|
|
|
use POSIX qw(strftime);
|
|
|
|
|
|
|
|
sub new {
|
2020-02-15 23:38:32 +01:00
|
|
|
if (ref($_[1]) eq 'HASH') { Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference"); }
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($class, %conf) = @_;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference in " . __FILE__);
|
|
|
|
$self->{filename} = delete $conf{filename};
|
|
|
|
$self->{quotegrabs} = [];
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub begin {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
$self->load_quotegrabs;
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
sub end { }
|
2014-05-06 07:15:27 +02:00
|
|
|
|
|
|
|
sub load_quotegrabs {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
|
|
|
if (@_) { $filename = shift; }
|
|
|
|
else { $filename = $self->{filename}; }
|
|
|
|
return if not defined $filename;
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("Loading quotegrabs from $filename ...\n");
|
|
|
|
|
|
|
|
open(FILE, "< $filename") or die "Couldn't open $filename: $!\n";
|
|
|
|
my @contents = <FILE>;
|
|
|
|
close(FILE);
|
|
|
|
|
|
|
|
my $i = 0;
|
|
|
|
foreach my $line (@contents) {
|
|
|
|
chomp $line;
|
|
|
|
$i++;
|
|
|
|
my ($nick, $channel, $timestamp, $grabbed_by, $text) = split(/\s+/, $line, 5);
|
|
|
|
if (not defined $nick || not defined $channel || not defined $timestamp || not defined $grabbed_by || not defined $text) {
|
|
|
|
die "Syntax error around line $i of $filename\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $quotegrab = {};
|
|
|
|
$quotegrab->{nick} = $nick;
|
|
|
|
$quotegrab->{channel} = $channel;
|
|
|
|
$quotegrab->{timestamp} = $timestamp;
|
|
|
|
$quotegrab->{grabbed_by} = $grabbed_by;
|
|
|
|
$quotegrab->{text} = $text;
|
|
|
|
$quotegrab->{id} = $i + 1;
|
|
|
|
push @{$self->{quotegrabs}}, $quotegrab;
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log(" $i quotegrabs loaded.\n");
|
|
|
|
$self->{pbot}->{logger}->log("Done.\n");
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub save_quotegrabs {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (@_) { $filename = shift; }
|
|
|
|
else { $filename = $self->{filename}; }
|
|
|
|
return if not defined $filename;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
for (my $i = 0; $i <= $#{$self->{quotegrabs}}; $i++) {
|
|
|
|
my $quotegrab = $self->{quotegrabs}[$i];
|
|
|
|
next if $quotegrab->{timestamp} == 0;
|
|
|
|
print FILE "$quotegrab->{nick} $quotegrab->{channel} $quotegrab->{timestamp} $quotegrab->{grabbed_by} $quotegrab->{text}\n";
|
|
|
|
}
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
close(FILE);
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub add_quotegrab {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $quotegrab) = @_;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
push @{$self->{quotegrabs}}, $quotegrab;
|
|
|
|
$self->save_quotegrabs();
|
|
|
|
return $#{$self->{quotegrabs}} + 1;
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub delete_quotegrab {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $id) = @_;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($id < 1 || $id > $#{$self->{quotegrabs}} + 1) { return undef; }
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
splice @{$self->{quotegrabs}}, $id - 1, 1;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
for (my $i = $id - 1; $i <= $#{$self->{quotegrabs}}; $i++) { $self->{quotegrabs}[$i]->{id}--; }
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->save_quotegrabs();
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub get_quotegrab {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $id) = @_;
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($id < 1 || $id > $#{$self->{quotegrabs}} + 1) { return undef; }
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $self->{quotegrabs}[$id - 1];
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub get_random_quotegrab {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $nick, $channel, $text) = @_;
|
|
|
|
|
|
|
|
$nick = '.*' if not defined $nick;
|
|
|
|
$channel = '.*' if not defined $channel;
|
|
|
|
$text = '.*' if not defined $text;
|
|
|
|
|
|
|
|
my @quotes;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
for (my $i = 0; $i <= $#{$self->{quotegrabs}}; $i++) {
|
|
|
|
my $hash = $self->{quotegrabs}[$i];
|
|
|
|
if ($hash->{channel} =~ /$channel/i && $hash->{nick} =~ /$nick/i && $hash->{text} =~ /$text/i) {
|
|
|
|
$hash->{id} = $i + 1;
|
|
|
|
push @quotes, $hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
$self->{pbot}->{logger}->log("Error in show_random_quotegrab parameters: $@\n");
|
|
|
|
return undef;
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($#quotes < 0) { return undef; }
|
2014-05-06 07:15:27 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $quotes[int rand($#quotes + 1)];
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub get_all_quotegrabs {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = shift;
|
|
|
|
return $self->{quotegrabs};
|
2014-05-06 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|