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 Storable;
use vars qw($VERSION);
$VERSION = $PBot::PBot::VERSION;
@ -50,7 +52,7 @@ sub initialize {
$self->{FLOOD_CHAT} = 0;
$self->{FLOOD_JOIN} = 1;
$self->{message_history} = {};
$self->load_message_history;
my $filename = delete $conf{filename} // $self->{pbot}->{data_dir} . '/ban_whitelist';
$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->whitelist(@_) }, "whitelist", 10);
$pbot->commands->register(sub { return $self->save_message_history_cmd(@_) }, "save_message_history", 60);
}
sub ban_whitelisted {
@ -687,4 +690,25 @@ sub on_whoisaccount {
$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;

View File

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

View File

@ -69,7 +69,7 @@ sub initialize {
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->{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->{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_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} // '!';

View File

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

141
pbot.pl
View File

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