| Bot-Net documentation | Contained in the Bot-Net distribution. |
Bot::Net::Config - the configuration for your bot net
my $config = Bot::Net->config;
my $bot_config = $config->bot('CopyBot');
my $server_config = $config->server('MasterHost');
head1 DESCRIPTION
This module loads and stores the configuration for your bot net. The bot net configuration is typically stored in etc/config.yml of your bot net application directory. However, you can have multiple configurations, which can be specified to the spawn command using the --config option.
You should not need to call this method directory. Instead:
my $config = Bot::Net->config;
will call the constructor as needed.
The main configuration file for your Bot::Net application is stored in etc/net.yml. This returns a single value from that file if a KEY is specified or returns the entire configuration if no key is given.
Returns the path to the main configuration file for your Bot::Net application, which is stored in etc/net.yml.
Reloads one of the configuration files. You normally won't need to call this method, it will be called automatically.
Saves a configuration file. This can be useful when making changes to the configuration from within a bot that you would liked saved for future use.
Returns the location of the configuration for the server named NAME.
Returns the configuration for the named server.
Returns the location of the bot configuration file for the named bot.
Returns teh configuration for the named bot. This will include the default configuration and bot-specific overrides.
Bot::Net will search for configuration files in the following places (and in the following order). The first file found according to this order will be used.
If the environment variable BOT_NET_CONFIG_PATH is set. It is assumed to be a list of one or more paths (separated by colons) containing the names of the directories to search for configuration files. The directories will be searched in the order given in the variable.
The program will look for the binary file (using FindBin) that was executed (probably bin/botnet) and find the etc directory one level above the directory containing the script. For example, if you're running your bot net from /home/sterling/MyNet/bin/botnet, it would look in /home/sterling/MyNet/etc for your configuration files.
Within the directory found, the files will be named as follows:
The net.yml file must be found in this directory.
Server config files will be found in a subdirectory named server and then subdirectories based upon the name of the server.
For example, a server package named MyNet::Server::Foo::Bar::Master would find it's configuration file under server/Foo/Bar/Master.yml.
Bot configuration files are in the subdirectory named bot and then subdirectories based upon the name of the bot.
For example, a bot package named MyNet::Bot::Foo::Bar::ChanOp would find it's configuration file under bot/Foo/Bar/ChanOp.yml.
Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
This program is free software and may be modified and distributed under the same terms as Perl itself.
| Bot-Net documentation | Contained in the Bot-Net distribution. |
use strict; use warnings; package Bot::Net::Config; use File::Find::Rule; use File::Spec; use FindBin; use Hash::Merge qw/ merge /; use Readonly; use YAML::Syck qw/ LoadFile DumpFile /;
sub new { my $class = shift; bless {}, $class; } sub _search_paths { my @paths = ( [ $FindBin::Bin, '..', 'etc' ] ); if ($ENV{BOT_NET_CONFIG_PATH}) { my @env_paths = split /:/, $ENV{BOT_NET_CONFIG_PATH}; unshift @paths, map { [ $_ ] } @env_paths; } return @paths; } sub _search_for_file { my @file_path = @_; for my $search_path (_search_paths()) { my $file_name = File::Spec->catfile(@$search_path, @file_path); return $file_name if -f $file_name; } return undef; }
sub net { my $self = shift; my $key = shift; $self->load_config('net') unless defined $self->{net}; return defined $key ? $self->{net}{$key} : $self->{net}; }
sub net_file { my $self = shift; return _search_for_file('net.yml'); }
sub load_config { my $self = shift; my $type = shift; my $name = shift; my $filename = $type eq 'net' ? $self->net_file : $type eq 'bot' ? $self->bot_file($name) : $type eq 'server' ? $self->server_file($name) : die "I don't know how to load a $type config file."; my $config = LoadFile($filename); if ($type eq 'net') { $self->{$type} = $config; } else { $self->{$type}{$name} = $config; } }
sub save_config { my $self = shift; my $type = shift; my $name = shift; my $config = $type eq 'net' ? $self->{$type} : $self->{$type}{$name} ; die "No configuration found for $type:$name." unless defined $config; my $filename = $type eq 'net' ? $self->net_file : $type eq 'bot' ? $self->bot_file($name) : $type eq 'server' ? $self->server_file($name) : die "I don't know how to save a $type config file."; DumpFile($filename, $config); }
sub server_file { my $self = shift; my $name = shift; my @path = split /::/, $name; $path[ $#path ] .= '.yml'; return _search_for_file('server', @path); }
sub server { my $self = shift; my $name = shift; $self->load_config( server => $name ) unless defined $self->{server}{$name}; return $self->{server}{$name}; }
sub bot_file { my $self = shift; my $name = shift; my @path = split /::/, $name; $path[ $#path ] .= '.yml'; return _search_for_file('bot', @path); }
sub bot { my $self = shift; my $name = shift; $self->load_config( bot => $name ) unless defined $self->{bot}{$name}; return $self->{bot}{$name}; }
1;