1

optimize fastpath, cleanup, move all possible routines to threads

This commit is contained in:
eleksir
2016-10-22 18:32:29 +03:00
parent 73f7997dbf
commit b5e4968b35
2 changed files with 36 additions and 29 deletions

View File

@@ -14,18 +14,10 @@ sub urlencode($);
sub hookfn; sub hookfn;
sub freehooks; sub freehooks;
my $wgetpath; my $wgetpath; share($wgetpath);
if (-f "/bin/wget") {
$wgetpath = '/bin/wget';
} elsif (-f "/usr/bin/wget") {
$wgetpath = '/usr/bin/wget';
} elsif (-f "/usr/local/bin/wget") {
$wgetpath = '/usr/local/bin/wget';
}
my $script_name = "Image URL Auto Grabber and Downloader, wget flavour"; my $script_name = "Image URL Auto Grabber and Downloader, wget flavour";
HexChat::register($script_name, '0.8.5', 'Automatically grabs and downloads image URLs via wget', \&freehooks); HexChat::register($script_name, '0.8.6', 'Automatically grabs and downloads image URLs via wget', \&freehooks);
HexChat::print("$script_name loaded\n"); HexChat::print("$script_name loaded\n");
my @hooks; my @hooks;
@@ -42,12 +34,15 @@ sub hookfn {
my @words = split(/\s+/, $text); my @words = split(/\s+/, $text);
foreach (@words) { foreach (@words) {
if ($_ =~ m{https?://([a-zA-Z0-9.-]+\.[a-zA-Z]+)/(?:.*)}) { my $str = $_;
next unless(substr($str, 0, 4) eq 'http');
if ($str =~ m{https?://([a-zA-Z0-9.-]+\.[a-zA-Z]+)/(?:.*)}) {
$active = 1; $active = 1;
my $t = undef; my $t = undef;
do { do {
$t = threads->create('dlfunc', $_); $t = threads->create('dlfunc', $str);
sleep 1 unless(defined($t)); sleep 1 unless(defined($t));
} unless (defined($t)); } unless (defined($t));
@@ -68,6 +63,15 @@ sub hookfn {
sub dlfunc($) { sub dlfunc($) {
my $url = shift; my $url = shift;
if (-f "/bin/wget") {
$wgetpath = '/bin/wget';
} elsif (-f "/usr/bin/wget") {
$wgetpath = '/usr/bin/wget';
} elsif (-f "/usr/local/bin/wget") {
$wgetpath = '/usr/local/bin/wget';
}
my $extension = is_picture($url); my $extension = is_picture($url);
if (defined($extension)) { if (defined($extension)) {
@@ -80,17 +84,17 @@ sub dlfunc($) {
} }
$url = urlencode($url); $url = urlencode($url);
system($wgetpath, '--no-check-certificate', '-q', '-T', '20', '-O', $file, '-o', '/dev/null', $url); system($wgetpath, '--no-check-certificate', '-q', '-T', '20', '-O', $savepath, '-o', '/dev/null', $url);
if ($file =~ /(png|jpe?g|gif)$/i){ if ($savepath =~ /(png|jpe?g|gif)$/i){
my $im = Image::Magick->new(); my $im = Image::Magick->new();
my $rename = 1; my $rename = 1;
my (undef, undef, undef, $format) = $im->Ping($savepath); my (undef, undef, undef, $format) = $im->Ping($savepath);
if (defined($format)) { if (defined($format)) {
$rename = 0 if (($format eq 'JPEG') and ($file =~ /jpe?g$/i)); $rename = 0 if (($format eq 'JPEG') and ($savepath =~ /jpe?g$/i));
$rename = 0 if (($format eq 'GIF') and ($file =~ /gif$/i)); $rename = 0 if (($format eq 'GIF') and ($savepath =~ /gif$/i));
$rename = 0 if (($format =~ /^PNG/) and ($file =~ /png$/i)); $rename = 0 if (($format =~ /^PNG/) and ($savepath =~ /png$/i));
rename $savepath, sprintf("%s.%s", $savepath, lc($format)) if ($rename == 1); rename $savepath, sprintf("%s.%s", $savepath, lc($format)) if ($rename == 1);
} }

View File

@@ -40,11 +40,9 @@ share($active);
sub hookfn { sub hookfn {
my ($nick, $text, $modechar) = @{$_[0]}; my ($nick, $text, $modechar) = @{$_[0]};
my $channel = HexChat::get_info('channel'); my $channel = HexChat::get_info('channel') // '';
my $network = HexChat::get_info('network'); my $network = HexChat::get_info('network') // '';
$nick = '' unless(defined($nick)); $nick = '' unless(defined($nick));
$channel = '' unless(defined($channel));
$network = '' unless(defined($network));
# load settings, parse whitelists here # load settings, parse whitelists here
my $flag = 1; # show notification my $flag = 1; # show notification
@@ -96,16 +94,19 @@ sub hookfn {
} }
# settings are loaded # settings are loaded
return EAT_NONE if ($flag == 0); if ($flag == 0) {
undef $nick; undef $text; undef $modechar;
undef $channel; undef $network;
undef $flag;
undef $nicklist; undef $chanlist; undef $netlist;
return EAT_NONE;
}
HexChat::strip_code($text);
$text =~ s/\"/\\"/g;
my $topic = sprintf("%s at %s says:\n", $nick, $channel);
$active = 1; $active = 1;
my $t = undef; my $t = undef;
do { do {
$t = threads->create('notify', $topic, $text); $t = threads->create('notify', $channel, $nick, $text);
sleep 1 unless(defined($t)); sleep 1 unless(defined($t));
} unless (defined($t)); } unless (defined($t));
@@ -114,7 +115,7 @@ sub hookfn {
if ($active == 1) { if ($active == 1) {
hook_timer( 500, \&timeraction); hook_timer( 500, \&timeraction);
} }
undef $nick; undef $text; undef $modechar; undef $nick; undef $text; undef $modechar;
undef $channel; undef $network; undef $channel; undef $network;
undef $flag; undef $flag;
@@ -124,10 +125,12 @@ sub hookfn {
} }
sub notify(@) { sub notify(@) {
my($topic, $text) = @_; my($channel, $nick, $text) = @_;
HexChat::strip_code($text);
my $topic = sprintf("%s at %s says:\n", $nick, $channel);
system("notify-send", "-u", "normal", "-t", "12000", "-a", "hexchat", $topic, "-i", "hexchat", $text); system("notify-send", "-u", "normal", "-t", "12000", "-a", "hexchat", $topic, "-i", "hexchat", $text);
undef $channel, undef $nick, undef $text; undef $topic;
$active = 0; $active = 0;
undef $topic; undef $text;
} }
sub timeraction { sub timeraction {