1

logic fix, again; grand code cleanup

This commit is contained in:
eleksir
2016-10-01 20:03:11 +03:00
parent 23d2a751fa
commit 2ffea839c6

View File

@@ -1,12 +1,15 @@
# TODO: huge cleanup and code refactor required! # TODO: code cleanup
use strict; use strict;
use warnings "all"; use warnings "all";
use HexChat qw(:all); use HexChat qw(:all);
use threads; use threads;
use threads::shared; use threads::shared;
sub hookfn; sub loadliststatus($);
sub loadlist($);
sub savesetting(@);
sub notify(@); sub notify(@);
sub hookfn;
sub timeraction; sub timeraction;
sub notify_cmd; sub notify_cmd;
@@ -20,7 +23,7 @@ my $help = 'Usage:
/notify show - shows whitelists and their statuses /notify show - shows whitelists and their statuses
'; ';
register($script_name, '0.5', 'Sends *nix desktop notifications'); register($script_name, '0.7beta', 'Sends *nix desktop notifications');
HexChat::print("$script_name loaded\n"); HexChat::print("$script_name loaded\n");
hook_print('Channel Message', \&hookfn); hook_print('Channel Message', \&hookfn);
@@ -42,94 +45,44 @@ sub hookfn {
# load settings, parse whitelists here # load settings, parse whitelists here
my $flag = 1; # show notification my $flag = 1; # show notification
my $nicklist = HexChat::plugin_pref_get('nicklist'); my $nicklist = loadliststatus('nicklist');
unless (defined($nicklist)) { my $chanlist = loadliststatus('chanlist');
if (HexChat::plugin_pref_set('nicklist', '0') == 0) { my $netlist = loadliststatus('netlist');
HexChat::print("Unable to save settings for $script_name\n");
}
$nicklist = 0;
}
my $chanlist = HexChat::plugin_pref_get('chanlist');
unless (defined($chanlist)) {
if (HexChat::plugin_pref_set('chanlist', '0') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$chanlist = 0;
}
my $netlist = HexChat::plugin_pref_get('netlist');
unless (defined($netlist)) {
if (HexChat::plugin_pref_set('netlist', '0') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$netlist = 0;
}
$flag = 0 if (($nicklist != 0) or ($chanlist != 0) or ($netlist != 0)); $flag = 0 if (($nicklist != 0) or ($chanlist != 0) or ($netlist != 0));
if ($nicklist != 0) { if ($nicklist != 0) {
my $nicks = HexChat::plugin_pref_get('nicks'); my $nicks = loadlist('nicks');
unless (defined($nicks)) {
if (HexChat::plugin_pref_set('nicks', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nicks = '';
}
my @nicklist = split(/ /, $nicks); my @nicklist = split(/ /, $nicks);
foreach (@nicklist) { foreach my $n (@nicklist) {
next unless(defined($_)); next unless(defined($n));
next unless($_ eq ''); next if($n eq '');
$flag = 1 if ($_ eq $nick); $flag = 1 if ($n eq $nick);
} }
} }
if (($chanlist != 0) and ($flag == 0)) { if (($chanlist != 0) and ($flag == 0)) {
my $chans = HexChat::plugin_pref_get('chans'); my $chans = loadlist('chans');
unless (defined($chans)) {
if (HexChat::plugin_pref_set('chans', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$chans = '';
}
my @chanlist = split(/ /, $chans); my @chanlist = split(/ /, $chans);
foreach (@chanlist) { foreach (@chanlist) {
next unless(defined($_)); next unless(defined($_));
next unless($_ eq ''); next if($_ eq '');
$flag = 1 if ($_ eq $channel); $flag = 1 if ($_ eq $channel);
} }
} }
if (($netlist != 0) and ($flag == 0)) { if (($netlist != 0) and ($flag == 0)) {
my $nets = HexChat::plugin_pref_get('nets'); my $nets = loadlist('nets');
unless (defined($nets)) {
if (HexChat::plugin_pref_set('nets', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nets = '';
}
my @netlist = split(/ /, $nets); my @netlist = split(/ /, $nets);
foreach (@netlist) { foreach (@netlist) {
next unless(defined($_)); next unless(defined($_));
next unless($_ eq ''); next if($_ eq '');
$flag = 1 if ($_ eq $network); $flag = 1 if ($_ eq $network);
} }
} }
# settings are loaded # settings are loaded
return EAT_NONE if ($flag == 0); return EAT_NONE if ($flag == 0);
@@ -178,32 +131,9 @@ sub notify_cmd {
} }
if ($cmd eq 'status') { if ($cmd eq 'status') {
my $nicklist = HexChat::plugin_pref_get('nicklist'); my $nicklist = loadliststatus('nicklist');
unless (defined($nicklist)) { my $chanlist = loadliststatus('chanlist');
if (HexChat::plugin_pref_set('nicklist', '0') == 0) { my $netlist = loadliststatus('netlist');
HexChat::print("Unable to save settings for $script_name\n");
}
$nicklist = 0;
}
my $chanlist = HexChat::plugin_pref_get('chanlist');
unless (defined($chanlist)) {
if (HexChat::plugin_pref_set('chanlist', '0') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$chanlist = 0;
}
my $netlist = HexChat::plugin_pref_get('netlist');
unless (defined($netlist)) {
if (HexChat::plugin_pref_set('netlist', '0') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$netlist = 0;
}
if (($nicklist == 0) and ($chanlist == 0) and ($netlist == 0)) { if (($nicklist == 0) and ($chanlist == 0) and ($netlist == 0)) {
HexChat::print("All whitelists are disbled, will not apply any filters, and be show all notifications.\n"); HexChat::print("All whitelists are disbled, will not apply any filters, and be show all notifications.\n");
@@ -270,82 +200,34 @@ sub notify_cmd {
} elsif ($cmd eq 'show') { } elsif ($cmd eq 'show') {
my $str = ''; my $str = '';
my $nicklist = HexChat::plugin_pref_get('nicklist'); my $nicklist = loadliststatus('nicklist');
unless (defined($nicklist)) {
if (HexChat::plugin_pref_set('nicklist', '0') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nicklist = 0;
}
if ($nicklist == 0) { if ($nicklist == 0) {
$str .= "Nicks whitelist: disabled\n"; $str .= "Nicks whitelist: disabled\n";
} else { } else {
$str .= "Nicks whitelist: enabled\n"; $str .= "Nicks whitelist: enabled\n";
} }
my $nicks = HexChat::plugin_pref_get('nicks'); my $nicks = loadlist('nicks');
unless (defined($nicks)) {
if (HexChat::plugin_pref_set('nicks', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nicks = '';
}
$str .= "Whitelisted nicks = $nicks\n"; $str .= "Whitelisted nicks = $nicks\n";
my $chanlist = HexChat::plugin_pref_get('chanlist'); my $chanlist = loadliststatus('chanlist');
unless (defined($chanlist)) {
if (HexChat::plugin_pref_set('chanlist', '0') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$chanlist = 0;
}
if ($chanlist == 0) { if ($chanlist == 0) {
$str .= "Channel whitelist: disabled\n"; $str .= "Channel whitelist: disabled\n";
} else { } else {
$str .= "Channel whitelist: enabled\n"; $str .= "Channel whitelist: enabled\n";
} }
my $chans = HexChat::plugin_pref_get('chans'); my $chans = loadlist('chans');
unless (defined($chans)) {
if (HexChat::plugin_pref_set('chans', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$chans = '';
}
$str .= "Whitelisted channels = $chans\n"; $str .= "Whitelisted channels = $chans\n";
my $netlist = HexChat::plugin_pref_get('netlist'); my $netlist = loadliststatus('netlist');
unless (defined($netlist)) {
if (HexChat::plugin_pref_set('netlist', '0') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$netlist = 0;
}
if ($netlist == 0) { if ($netlist == 0) {
$str .= "Networks whitelist: disabled\n"; $str .= "Networks whitelist: disabled\n";
} else { } else {
$str .= "Networks whitelist: enabled\n"; $str .= "Networks whitelist: enabled\n";
} }
my $nets = HexChat::plugin_pref_get('nets'); my $nets = loadlist('nets');
unless (defined($nets)) {
if (HexChat::plugin_pref_set('nets', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nets = '';
}
$str .= "Whitelisted networks = $nets\n"; $str .= "Whitelisted networks = $nets\n";
HexChat::print($str); HexChat::print($str);
@@ -354,60 +236,30 @@ sub notify_cmd {
my $str = ''; my $str = '';
if ((defined($entity)) and (defined($value))) { if ((defined($entity)) and (defined($value))) {
if ($entity eq 'nick') { if ($entity eq 'nick') {
my $nicks = HexChat::plugin_pref_get('nicks'); my $nicks = loadlist('nicks');
unless (defined($nicks)) {
if (HexChat::plugin_pref_set('nicks', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nicks = '';
}
$nicks .= " $value"; $nicks .= " $value";
if (HexChat::plugin_pref_set('nicks', $nicks) == 0) { unless (defined(savesetting('nicks', $nicks))) {
HexChat::print("Unable to save settings for $script_name\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} }
HexChat::print("Nicks whitelist now: $nicks\n"); HexChat::print("Nicks whitelist now: $nicks\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} elsif ($entity eq 'chan') { } elsif ($entity eq 'chan') {
my $chans = HexChat::plugin_pref_get('chans'); my $chans = loadlist('chans');
unless (defined($chans)) {
if (HexChat::plugin_pref_set('chans', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$chans = '';
}
$chans .= " $value"; $chans .= " $value";
if (HexChat::plugin_pref_set('chans', $chans) == 0) { unless (defined(savesetting('chans', $chans))) {
HexChat::print("Unable to save settings for $script_name\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} }
HexChat::print("Channels whitelist now: $chans\n"); HexChat::print("Channels whitelist now: $chans\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} elsif ($entity eq 'net') { } elsif ($entity eq 'net') {
my $nets = HexChat::plugin_pref_get('nets'); my $nets = loadlist('nets');
unless (defined($nets)) {
if (HexChat::plugin_pref_set('nets', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nets = '';
}
$nets .= " $value"; $nets .= " $value";
if (HexChat::plugin_pref_set('nets', $nets) == 0) { unless (defined(savesetting('nets', $nets))) {
HexChat::print("Unable to save settings for $script_name\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} }
@@ -420,16 +272,7 @@ sub notify_cmd {
} elsif ($cmd eq 'del') { } elsif ($cmd eq 'del') {
if ((defined($entity)) and (defined($value))) { if ((defined($entity)) and (defined($value))) {
if ($entity eq 'nick') { if ($entity eq 'nick') {
my $nicks = HexChat::plugin_pref_get('nicks'); my $nicks = loadlist('nicks');
unless (defined($nicks)) {
if (HexChat::plugin_pref_set('nicks', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nicks = '';
}
my @nicklist = split(/ /, $nicks); my @nicklist = split(/ /, $nicks);
for (my $i = 0; $i < @nicklist; $i++) { for (my $i = 0; $i < @nicklist; $i++) {
@@ -439,8 +282,7 @@ sub notify_cmd {
$nicks = join(' ', @nicklist); $nicks = join(' ', @nicklist);
$nicks =~ s/ +/ /g; $nicks =~ s/ +/ /g;
if (HexChat::plugin_pref_set('nicks', $nicks) == 0) { unless (defined(savesetting('nicks', $nicks))) {
HexChat::print("Unable to save settings for $script_name\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} }
@@ -449,16 +291,7 @@ sub notify_cmd {
} }
if ($entity eq 'chan') { if ($entity eq 'chan') {
my $chans = HexChat::plugin_pref_get('chans'); my $chans = loadlist('chans');
unless (defined($chans)) {
if (HexChat::plugin_pref_set('chans', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$chans = '';
}
my @chanlist = split(/ /, $chans); my @chanlist = split(/ /, $chans);
for (my $i = 0; $i < @chanlist; $i++) { for (my $i = 0; $i < @chanlist; $i++) {
@@ -468,8 +301,7 @@ sub notify_cmd {
$chans = join(' ', @chanlist); $chans = join(' ', @chanlist);
$chans =~ s/ +/ /g; $chans =~ s/ +/ /g;
if (HexChat::plugin_pref_set('chans', $chans) == 0) { unless (defined(savesetting('chans', $chans))) {
HexChat::print("Unable to save settings for $script_name\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} }
@@ -478,16 +310,7 @@ sub notify_cmd {
} }
if ($entity eq 'net') { if ($entity eq 'net') {
my $nets = HexChat::plugin_pref_get('nets'); my $nets = loadlist('nets');
unless (defined($nets)) {
if (HexChat::plugin_pref_set('nets', '') == 0) {
HexChat::print("Unable to save settings for $script_name\n");
}
$nets = '';
}
my @netlist = split(/ /, $nets); my @netlist = split(/ /, $nets);
for (my $i = 0; $i < @netlist; $i++) { for (my $i = 0; $i < @netlist; $i++) {
@@ -497,12 +320,11 @@ sub notify_cmd {
$nets = join(' ', @netlist); $nets = join(' ', @netlist);
$nets =~ s/ +/ /g; $nets =~ s/ +/ /g;
if (HexChat::plugin_pref_set('nets', $nets) == 0) { unless (defined(savesetting('nets', $nets))) {
HexChat::print("Unable to save settings for $script_name\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} }
HexChat::print("Networkss whitelist now: $nets\n"); HexChat::print("Networks whitelist now: $nets\n");
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} }
@@ -514,3 +336,38 @@ sub notify_cmd {
HexChat::print($help); HexChat::print($help);
return HexChat::EAT_ALL; return HexChat::EAT_ALL;
} }
sub loadliststatus($){
my $listtype = shift;
my $list = HexChat::plugin_pref_get($listtype);
unless (defined($list)) {
savesetting('list', '0');
$list = 0;
}
return $list;
}
sub loadlist($){
my $setting = shift;
my $val = HexChat::plugin_pref_get($setting);
unless (defined($setting)) {
savesetting($setting, '');
$val = '';
}
return $val;
}
sub savesetting(@) {
my $setting = shift;
my $value = shift;
if (HexChat::plugin_pref_set($setting, $value) == 0) {
HexChat::print("Unable to save settings for $script_name\n");
return undef;
}
return 1;
}