3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-02 00:49:26 +01:00

Use Storable to save/load message history hash

Reuse config_dir/data_dir variable in pbot.pl
This commit is contained in:
Pragmatic Software 2014-02-05 01:10:56 +00:00
parent 0f2616a552
commit 5a61f57f3c
5 changed files with 105 additions and 76 deletions

View File

@ -15,6 +15,8 @@ use strict;
use feature 'switch'; use feature 'switch';
use Storable;
use vars qw($VERSION); use vars qw($VERSION);
$VERSION = $PBot::PBot::VERSION; $VERSION = $PBot::PBot::VERSION;
@ -50,7 +52,7 @@ sub initialize {
$self->{FLOOD_CHAT} = 0; $self->{FLOOD_CHAT} = 0;
$self->{FLOOD_JOIN} = 1; $self->{FLOOD_JOIN} = 1;
$self->{message_history} = {}; $self->load_message_history;
my $filename = delete $conf{filename} // $self->{pbot}->{data_dir} . '/ban_whitelist'; my $filename = delete $conf{filename} // $self->{pbot}->{data_dir} . '/ban_whitelist';
$self->{ban_whitelist} = PBot::DualIndexHashObject->new(name => 'BanWhitelist', filename => $filename); $self->{ban_whitelist} = PBot::DualIndexHashObject->new(name => 'BanWhitelist', filename => $filename);
@ -60,6 +62,7 @@ sub initialize {
$pbot->commands->register(sub { return $self->unbanme(@_) }, "unbanme", 0); $pbot->commands->register(sub { return $self->unbanme(@_) }, "unbanme", 0);
$pbot->commands->register(sub { return $self->whitelist(@_) }, "whitelist", 10); $pbot->commands->register(sub { return $self->whitelist(@_) }, "whitelist", 10);
$pbot->commands->register(sub { return $self->save_message_history_cmd(@_) }, "save_message_history", 60);
} }
sub ban_whitelisted { sub ban_whitelisted {
@ -687,4 +690,25 @@ sub on_whoisaccount {
$self->check_nickserv_accounts($nick, $account); $self->check_nickserv_accounts($nick, $account);
} }
sub save_message_history {
my $self = shift;
store($self->{message_history}, $self->{pbot}->{message_history_file});
}
sub load_message_history {
my $self = shift;
if(-e $self->{pbot}->{message_history_file}) {
$self->{message_history} = retrieve($self->{pbot}->{message_history_file});
} else {
$self->{message_history} = {};
}
}
sub save_message_history_cmd {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
$self->save_message_history;
return "Message history saved.";
}
1; 1;

View File

@ -157,8 +157,9 @@ sub ack_die {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
$self->{pbot}->logger->log("$nick!$user\@$host made me exit.\n"); $self->{pbot}->logger->log("$nick!$user\@$host made me exit.\n");
$self->{pbot}->factoids->save_factoids(); $self->{pbot}->factoids->save_factoids;
$self->{pbot}->ignorelist->save_ignores(); $self->{pbot}->ignorelist->save_ignores;
$self->{pbot}->antiflood->save_message_history;
$self->{pbot}->conn->privmsg($from, "Good-bye.") if defined $from; $self->{pbot}->conn->privmsg($from, "Good-bye.") if defined $from;
$self->{pbot}->conn->quit("Departure requested."); $self->{pbot}->conn->quit("Departure requested.");
exit 0; exit 0;

View File

@ -69,7 +69,7 @@ sub initialize {
my $log_file = delete $conf{log_file}; my $log_file = delete $conf{log_file};
$self->{conf_dir} = delete $conf{conf_dir} // "$ENV{HOME}/pbot/config"; $self->{config_dir} = delete $conf{config_dir} // "$ENV{HOME}/pbot/config";
$self->{data_dir} = delete $conf{data_dir} // "$ENV{HOME}/pbot/data"; $self->{data_dir} = delete $conf{data_dir} // "$ENV{HOME}/pbot/data";
$self->{module_dir} = delete $conf{module_dir} // "$ENV{HOME}/pbot/modules"; $self->{module_dir} = delete $conf{module_dir} // "$ENV{HOME}/pbot/modules";
@ -83,9 +83,10 @@ sub initialize {
$self->{ircname} = delete $conf{ircname} // "http://code.google.com/p/pbot2-pl/"; $self->{ircname} = delete $conf{ircname} // "http://code.google.com/p/pbot2-pl/";
$self->{identify_password} = delete $conf{identify_password} // ""; $self->{identify_password} = delete $conf{identify_password} // "";
$self->{max_msg_len} = delete $conf{max_msg_len} // 430; $self->{max_msg_len} = delete $conf{max_msg_len} // 425;
$self->{MAX_FLOOD_MESSAGES} = delete $conf{MAX_FLOOD_MESSAGES} // 4; $self->{MAX_FLOOD_MESSAGES} = delete $conf{MAX_FLOOD_MESSAGES} // 4;
$self->{MAX_NICK_MESSAGES} = delete $conf{MAX_NICK_MESSAGES} // 32; $self->{MAX_NICK_MESSAGES} = delete $conf{MAX_NICK_MESSAGES} // 32;
$self->{message_history_file} = delete $conf{message_history_file} // "$ENV{HOME}/pbot/data/message_history";
$self->{trigger} = delete $conf{trigger} // '!'; $self->{trigger} = delete $conf{trigger} // '!';

View File

@ -13,8 +13,8 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 477, BUILD_REVISION => 478,
BUILD_DATE => "2013-12-23", BUILD_DATE => "2014-02-04",
}; };
1; 1;

135
pbot.pl
View File

@ -18,96 +18,99 @@ use PBot::PBot;
my $bothome = "$ENV{HOME}/pbot"; my $bothome = "$ENV{HOME}/pbot";
my %config = ( my %config = (
# ----------------------------------------------------- # -----------------------------------------------------
# Be sure to set your IRC information to a registered NickServ account # Be sure to set your IRC information to a registered NickServ account
# if you want channel auto-join to work. # if you want channel auto-join to work.
# ----------------------------------------------------- # -----------------------------------------------------
# IRC server address to connect to # IRC server address to connect to
ircserver => 'irc.freenode.net', ircserver => 'irc.freenode.net',
# IRC port # IRC port
port => '6667', port => '6667',
# Use SSL? 0 = disabled, 1 = enabled # Use SSL? 0 = disabled, 1 = enabled
# Note that you may need to use a specific port for SSL; e.g., freenode uses 6697 or 7000 for SSL # Note that you may need to use a specific port for SSL; e.g., freenode uses 6697 or 7000 for SSL
# Uncomment SSL_ca_path or SSL_ca_file below to enable SSL verification (will still work without # Uncomment SSL_ca_path or SSL_ca_file below to enable SSL verification (will still work without
# verification, but will be susceptible to man-in-the-middle attacks) # verification, but will be susceptible to man-in-the-middle attacks)
SSL => 0, SSL => 0,
# SSL CA certificates path; e.g., linux: /etc/ssl/certs # SSL CA certificates path; e.g., linux: /etc/ssl/certs
# SSL_ca_path => '/etc/ssl/certs', # SSL_ca_path => '/etc/ssl/certs',
# SSL CA file, if SSL_ca_path will not do; e.g., OpenBSD: /etc/ssl/cert.pem # SSL CA file, if SSL_ca_path will not do; e.g., OpenBSD: /etc/ssl/cert.pem
# SSL_ca_file => '/etc/ssl/cert.pem', # SSL_ca_file => '/etc/ssl/cert.pem',
# IRC nick (what people see when you talk in channels) # IRC nick (what people see when you talk in channels)
# (must be a nick registered with a NickServ account for channel auto-join to work) # (must be a nick registered with a NickServ account for channel auto-join to work)
botnick => 'pbot3', botnick => 'pbot3',
# IRC username (what appears in front of your hostname in /whois) # IRC username (what appears in front of your hostname in /whois)
username => 'pbot3', username => 'pbot3',
# IRC realname (extra /whois information) # IRC realname (extra /whois information)
ircname => 'http://www.iso-9899.info/wiki/Candide', ircname => 'http://www.iso-9899.info/wiki/Candide',
# Password to send to NickServ for identification # Password to send to NickServ for identification
# (channels will not be auto-joined until identified) # (channels will not be auto-joined until identified)
identify_password => '*', identify_password => '*',
# The bot is triggered by using its name, or the following trigger SINGLE character # The bot is triggered by using its name, or the following trigger SINGLE character
trigger => '.', trigger => '.',
# ----------------------------------------------------- # -----------------------------------------------------
# The bot can export the latest factoids and quotegrabs to an HTML # The bot can export the latest factoids and quotegrabs to an HTML
# document. If you run a webserver or something similiar, you may # document. If you run a webserver or something similiar, you may
# wish to set the following items ending with 'path' to point to # wish to set the following items ending with 'path' to point to
# a suitable location for the webserver, and to set the items # a suitable location for the webserver, and to set the items
# ending with 'site' to the public-facing URL where the files # ending with 'site' to the public-facing URL where the files
# may be viewed in a browser. # may be viewed in a browser.
# ----------------------------------------------------- # -----------------------------------------------------
export_factoids_path => "$bothome/factoids.html", export_factoids_path => "$bothome/factoids.html",
export_factoids_site => 'http://blackshell.com/~msmud/candide/factoids.html', export_factoids_site => 'http://blackshell.com/~msmud/candide/factoids.html',
export_quotegrabs_path => "$bothome/quotegrabs.html", export_quotegrabs_path => "$bothome/quotegrabs.html",
export_quotegrabs_site => 'http://blackshell.com/~msmud/candide/quotegrabs.html', export_quotegrabs_site => 'http://blackshell.com/~msmud/candide/quotegrabs.html',
# ----------------------------------------------------- # -----------------------------------------------------
# You shouldn't need to change anything below this line. # You shouldn't need to change anything below this line.
# ----------------------------------------------------- # -----------------------------------------------------
# Maximum messages to remember per nick/hostmask # Maximum messages to remember per nick/hostmask
MAX_NICK_MESSAGES => 256, MAX_NICK_MESSAGES => 256,
# Path to data directory # Path to data directory
data_dir => "$bothome/data", data_dir => "$bothome/data",
# Path to config directory # Path to config directory
conf_dir => "$bothome/config", config_dir => "$bothome/config",
# Path to directory containing external script-like modules # Path to directory containing external script-like modules
module_dir => "$bothome/modules", module_dir => "$bothome/modules",
# Location of file where bot log information will be output (in addition to stdout) # Location of file where bot log information will be output (in addition to stdout)
# (if you use pbot.sh and you change log_file, be sure to also change the log path in pbot.sh) # (if you use pbot.sh and you change log_file, be sure to also change the log path in pbot.sh)
log_file => "$bothome/log/log", log_file => "$bothome/log/log",
);
# Location of file containing bot admin information # Location of file containing bot admin information
admins_file => "$bothome/config/admins", $config{admins_file} = "$config{config_dir}/admins";
# Location of file containing channel information # Location of file containing channel information
channels_file => "$bothome/config/channels", $config{channels_file} = "$config{config_dir}/channels";
# Location of file containing ignorelist entries # Location of file containing ignorelist entries
ignorelist_file => "$bothome/config/ignorelist", $config{ignorelist_file} = "$config{config_dir}/ignorelist";
# Location of file containing factoids and modules # Location of file containing factoids and modules
factoids_file => "$bothome/data/factoids", $config{factoids_file} = "$config{data_dir}/factoids";
# Location of file containing channel user quotes # Location of file containing channel user quotes
quotegrabs_file => "$bothome/data/quotegrabs", $config{quotegrabs_file} = "$config{data_dir}/quotegrabs";
);
# Location of file containing message history
$config{message_history_file} = "$config{data_dir}/message_history";
# Create and initialize bot object # Create and initialize bot object
my $pbot = PBot::PBot->new(%config); my $pbot = PBot::PBot->new(%config);