From b5e4968b35bb423519ffa02bc24919c2331070d2 Mon Sep 17 00:00:00 2001 From: eleksir Date: Sat, 22 Oct 2016 18:32:29 +0300 Subject: [PATCH] optimize fastpath, cleanup, move all possible routines to threads --- img_url_wget.pl | 38 +++++++++++++++++++++----------------- notifications.pl | 27 +++++++++++++++------------ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/img_url_wget.pl b/img_url_wget.pl index 7b012af..26b66df 100644 --- a/img_url_wget.pl +++ b/img_url_wget.pl @@ -14,18 +14,10 @@ sub urlencode($); sub hookfn; sub freehooks; -my $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 $wgetpath; share($wgetpath); 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"); my @hooks; @@ -42,12 +34,15 @@ sub hookfn { my @words = split(/\s+/, $text); 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; my $t = undef; do { - $t = threads->create('dlfunc', $_); + $t = threads->create('dlfunc', $str); sleep 1 unless(defined($t)); } unless (defined($t)); @@ -68,6 +63,15 @@ sub hookfn { sub dlfunc($) { 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); if (defined($extension)) { @@ -80,17 +84,17 @@ sub dlfunc($) { } $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 $rename = 1; my (undef, undef, undef, $format) = $im->Ping($savepath); if (defined($format)) { - $rename = 0 if (($format eq 'JPEG') and ($file =~ /jpe?g$/i)); - $rename = 0 if (($format eq 'GIF') and ($file =~ /gif$/i)); - $rename = 0 if (($format =~ /^PNG/) and ($file =~ /png$/i)); + $rename = 0 if (($format eq 'JPEG') and ($savepath =~ /jpe?g$/i)); + $rename = 0 if (($format eq 'GIF') and ($savepath =~ /gif$/i)); + $rename = 0 if (($format =~ /^PNG/) and ($savepath =~ /png$/i)); rename $savepath, sprintf("%s.%s", $savepath, lc($format)) if ($rename == 1); } diff --git a/notifications.pl b/notifications.pl index 431b9ca..42427db 100644 --- a/notifications.pl +++ b/notifications.pl @@ -40,11 +40,9 @@ share($active); sub hookfn { my ($nick, $text, $modechar) = @{$_[0]}; - my $channel = HexChat::get_info('channel'); - my $network = HexChat::get_info('network'); + my $channel = HexChat::get_info('channel') // ''; + my $network = HexChat::get_info('network') // ''; $nick = '' unless(defined($nick)); - $channel = '' unless(defined($channel)); - $network = '' unless(defined($network)); # load settings, parse whitelists here my $flag = 1; # show notification @@ -96,16 +94,19 @@ sub hookfn { } # 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; my $t = undef; do { - $t = threads->create('notify', $topic, $text); + $t = threads->create('notify', $channel, $nick, $text); sleep 1 unless(defined($t)); } unless (defined($t)); @@ -114,7 +115,7 @@ sub hookfn { if ($active == 1) { hook_timer( 500, \&timeraction); } - + undef $nick; undef $text; undef $modechar; undef $channel; undef $network; undef $flag; @@ -124,10 +125,12 @@ sub hookfn { } 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); + undef $channel, undef $nick, undef $text; undef $topic; $active = 0; - undef $topic; undef $text; } sub timeraction {