Package: xchat
Version: 2.8.8-6
Followup-For: Bug #683626

Dear Maintainer,
Attached is an improved version of the above plugin. I've already been
using it for the last week. Added are more facilities to gather stats.
# Copyright (c) 2012 - George Danchev <danc...@spnet.net>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;
use Fcntl;

my $time_start = localtime();
my $self = "int2bdo";
my $vers = "0.2";
my $seve = $self . "/" . $vers;

my $logf = ".xchat2/" . $self . ".log";
my %cmd = (
    STAT => $self . "_stat",
    ZERO => $self . "_zero",
    VERB => $self . "_verb",
    HELP => $self . "_help",
);
my ($opt_STAT, $opt_VERB, $opt_HELP) = (undef, 0, undef);
my $desc = "Suspicious ints 2 bugs.d.o/int (verbosity:$opt_VERB; for help type 
to self: $cmd{'HELP'})";
my $burl = "http://bugs.debian.org";;
my %freq = ( # NONE_0 => 0,
    SURE_1 => 0,
    SURE_2 => 0,
    WILD_3 => 0,
    WILD_4 => 0,
    WILD_5 => 0,
    WILD_6 => 0,
);
sub readlog();

### init
Xchat::register($self, $vers, $desc);
Xchat::hook_print('Channel Message', \&hrefbugs);
Xchat::hook_print('Your Message', \&hrefbugs);
Xchat::hook_print('Your Message', \&dispatch);
Xchat::print("Loaded $seve - $desc");
readlog();

###
sub readlog() {
    if ( sysopen(LOG, $logf, O_RDONLY) ) {
        while (<LOG>) {
            chomp;
            next if ( /^\s*$/ or /^\s*#\s*/ );
            if ( /^\s*(\S+)\s+(\d+)/ ) {
                my ($k, $v) = ($1, $2);
                if (exists $freq{$k}) {
                    $freq{$k} = $v;
                    Xchat::print("$seve: loaded $k $v from $logf");
                }
            }
        }
        close(LOG);
    }
    else {
        Xchat::print("$seve: $logf can not be opened");
    }
    return Xchat::EAT_NONE;
}

###
sub writelog() {
    if ( sysopen(LOG, $logf, O_CREAT | O_WRONLY | O_TRUNC) ) {
        my $time_written = localtime();
        print LOG "# $self $vers\n";
        print LOG "# $time_start - started\n";
        print LOG "# $time_written - last written\n";
        foreach my $key (sort { $freq{$b} <=> $freq{$a} } keys %freq) { 
            print LOG "$key $freq{$key}\n";
        }
        close(LOG);
    }
}

###
sub print_line() {
    Xchat::print("-" x 20);
}

###
sub percfre($) {
    my $fre = shift;
    my $hits = 0;
    map { $hits += $_ } values %freq;
    return $hits ? sprintf("%.1f" , ($fre/$hits)*100.0) : 0;
}

###
sub hrefbugs() {
    my $in = $_[0][1];
    W: while ( $in =~
        /(?:\#([123456789]{1}\d+))          # 1. sure guess
        |(?:bug\s*\#?([123456789]{1}\d+))   # 2. sure guess
        |(?:^([123456789]{1}\d+)\.?$)       # 3. wild guess
        |(?:\s([123456789]{1}\d+)\.?$)      # 4. wild guess
        |(?:([123456789]{1}\d+)^.\d)        # 5. wild guess - skip common 
'version 1.2' pattern
        |(?:\s([123456789]{1}\d+)\s)        # 6. wild guess - too much noise, 
but ...
        /xgi
    ) {
        my ($nam, $fre, $bug) = (undef, undef, undef);
        if    (defined($1)) {
            ($nam, $fre, $bug) = ( 'sure_1', ++$freq{'SURE_1'}, $1);
        }
        elsif (defined($2)) {
            ($nam, $fre, $bug) = ( 'sure_2', ++$freq{'SURE_2'}, $2);
        }
        elsif (defined($3)) {
            ($nam, $fre, $bug) = ( 'WILD_3', ++$freq{'WILD_3'}, $3);
        }
        elsif (defined($4)) {
            ($nam, $fre, $bug) = ( 'WILD_4', ++$freq{'WILD_4'}, $4);
        }
        elsif (defined($5)) {
            ($nam, $fre, $bug) = ( 'WILD_5', ++$freq{'WILD_5'}, $5);
        }
        elsif (defined($6)) {
            ($nam, $fre, $bug) = ( 'WILD_6', ++$freq{'WILD_6'}, $6);
        }
        else { # no-op
            ($nam, $fre, $bug) = (undef, undef, undef);
        }

        if (defined($nam) and defined($fre) and defined($bug) ) {
            my @top = sort { $b <=> $a } values %freq;
            my $rank = 0;
            for ($rank=0; $rank < scalar @top; $rank++) {
                if ( $top[$rank] eq $fre ) {
                    $rank++;
                    last;
                }
            }
            if ($opt_VERB) {
                my $perc = percfre($fre);
                Xchat::print("BUGUESS by $seve: rank:$rank name:$nam freq:$fre 
($perc%) url: $burl/$bug");
            }
            else {
                Xchat::print("BUGUESS by $seve: $burl/$bug");
            }
        }
    }
    writelog();
    return Xchat::EAT_NONE;
}

###
sub showstat() {
    my $time_now = localtime();
    print_line();
    Xchat::print("STAT by $seve: $time_start - $time_now (ranked by 
frequency)");
    print_line();
    Xchat::print("RANK\t NAME     FREQ\n");
    my $rank = 1;
    foreach my $key (sort {$freq{$b} <=> $freq{$a} } keys %freq) {
        my $perc = percfre($freq{$key});
        my $draw = "#" x $freq{$key};
#       Xchat::print("$rank\t $key   $freq{$key} ($perc%)\t $draw");
        Xchat::printf("%d\t %s  %2d %2d%s %s\n", $rank, $key, $freq{$key}, 
$perc, "%", $draw);
        $rank++;
    }
    print_line();
    return Xchat::EAT_XCHAT;
}

###
sub zerostat() {
    map { $_ = 0 } values %freq;
    writelog();
}

###
sub showverb($) {
    my $in = shift;
    if ( ( !defined($in) ) or ( defined($in) and $in > 0 ) ) {
        $opt_VERB = 1; Xchat::print("$seve: setting VERBOSE mode\n");
    }
    else {
        $opt_VERB = 0; Xchat::print("$seve: setting QUIET mode\n");
    }
    return Xchat::EAT_XCHAT;
}

###
sub showhelp() {
    print_line();
    Xchat::print("HELP by $seve (type to self):");
    print_line();
    foreach my $key (sort(keys %cmd)) {
        if ($key eq 'VERB') {
            Xchat::print("$cmd{$key}=[0,1]");
        }
        else {
            Xchat::print("$cmd{$key}");
        }
    }
    print_line();
    return Xchat::EAT_XCHAT;
}

###
sub dispatch() {
    my $in = $_[0][1];
    showstat()   if ( $in =~ /$cmd{'STAT'}/ );
    zerostat()   if ( $in =~ /$cmd{'ZERO'}/ );
    showverb($1) if ( $in =~ /$cmd{'VERB'}=?([+-]*\d)?/ );
    showhelp()   if ( $in =~ /$cmd{'HELP'}/ );
    # ignore anything else
    return Xchat::EAT_NONE;
}

Reply via email to