2016-01-28 10:01:33 +03:00
|
|
|
package II::Config;
|
|
|
|
|
|
|
|
use Config::Tiny;
|
2016-01-28 17:14:07 +03:00
|
|
|
use LWP::UserAgent;
|
|
|
|
use HTTP::Request;
|
|
|
|
use Data::Dumper;
|
2016-01-28 10:01:33 +03:00
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
|
|
|
|
my $self = { _file => 'config.ini', };
|
|
|
|
|
|
|
|
bless $self, $class;
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Load configuration
|
|
|
|
sub load {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $file = $self->{_file};
|
|
|
|
|
|
|
|
my $tiny = Config::Tiny->new();
|
|
|
|
$config = $tiny->read($file);
|
2016-01-28 17:14:07 +03:00
|
|
|
|
2016-01-28 10:01:33 +03:00
|
|
|
my $key = $config->{auth}->{key};
|
|
|
|
my $nick = $config->{auth}->{nick};
|
|
|
|
my $host = $config->{node}->{host};
|
|
|
|
my @echoareas = split /,/, $config->{node}->{echoareas};
|
|
|
|
my $name = $config->{node}->{name};
|
|
|
|
|
2016-01-28 17:14:07 +03:00
|
|
|
if ( $config->{node}->{list} eq 'yes' ) {
|
|
|
|
@echoareas = echo_from_list($host);
|
|
|
|
}
|
|
|
|
|
2016-01-28 10:01:33 +03:00
|
|
|
$c = {
|
|
|
|
nick => $nick,
|
|
|
|
key => $key,
|
|
|
|
host => $host,
|
|
|
|
echoareas => [@echoareas],
|
|
|
|
name => $name,
|
|
|
|
elastic_host => $config->{elastic}->{host},
|
|
|
|
elastic_index => $config->{elastic}->{index},
|
|
|
|
};
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
}
|
|
|
|
|
2016-01-28 17:14:07 +03:00
|
|
|
# Make echoareas list from list.txt
|
|
|
|
sub echo_from_list {
|
|
|
|
my ($host) = @_;
|
|
|
|
|
|
|
|
my $list = $host . 'list.txt';
|
|
|
|
my @echoes;
|
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new();
|
|
|
|
$ua->agent("Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0)");
|
|
|
|
|
|
|
|
my $req = HTTP::Request->new( GET => $list );
|
|
|
|
my $res = $ua->request($req);
|
|
|
|
|
|
|
|
if ( $res->is_success ) {
|
|
|
|
my @e = split /\n/, $res->content();
|
2016-01-28 17:37:11 +03:00
|
|
|
foreach my $d (@e) {
|
|
|
|
my @description = split /:/, $d;
|
2016-01-28 17:14:07 +03:00
|
|
|
push @echoes, $description[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return @echoes;
|
|
|
|
}
|
|
|
|
|
2016-01-28 10:01:33 +03:00
|
|
|
1;
|