#!/usr/bin/perl
#
# Summarize unwanted groups from the current news logs.
#
# Written by Russ Allbery <eagle@eyrie.org>
# Copyright 2020 Russ Allbery <eagle@eyrie.org>
# Copyright 1998, 2007
#     The Board of Trustees of the Leland Stanford Junior University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

use 5.028;
use autodie;
use strict;
use warnings;

# Say with error checking.  autodie unfortunately can't help us with this
# because say cannot be prototyped and hence can't be overridden.
#
# @args - Arguments to say
#
# Throws: Text exception on output failure
sub say_checked {
    my (@args) = @_;
    say @args or croak('say failed');
    return;
}

# Given a reference to a hash containing numeric counts, return the keys in
# reverse sorted order.
#
# $hash_ref - Reference to a hash
#
# Returns: Keys as a list in reverse sorted order.
sub keys_by_reverse_count {
    my ($hash_ref) = @_;
    my @keys = keys($hash_ref->%*);
    return reverse(sort { $hash_ref->{$a} <=> $hash_ref->{$b} } @keys);
}

# Clean up the name of this program for error messages.
$0 =~ s{ .*/ }{}xms;

# Pull in the name of the site we're scanning for.
my $site = shift or die "Usage: $0 <sitename>\n";

# Open the log file and scan through it, looking for lines matching the site
# and the pattern of an unwanted newsgroup.  Accumulate rejected article
# counts into a hash.
my (%reasons, %unwanted, %hierarchies);
while (defined(my $line = <>)) {
    my ($mode, $from, undef, $reason) = (split(m{ }xms, $line, 7))[3 .. 6];
    next unless $mode eq q{-};
    next unless $from eq $site;
    if ($reason =~ m{ Unwanted [ ] newsgroup [ ] ["] (\S+) ["] }xms) {
        my $group = $1;
        $unwanted{$group}++;
        my ($hierarchy) = (split(m{ [.] }xms, $group, 2));
        $hierarchies{$hierarchy}++;
    } else {
        chomp($reason);
        $reasons{$reason}++;
    }
}

# Print out all of the unwanted groups from most traffic to least.
say_checked('Unwanted groups:');
for my $group (keys_by_reverse_count(\%unwanted)) {
    say_checked(sprintf('%6d %s', $unwanted{$group}, $group));
}

# Now summarize the unwanted hierarchies.
say_checked("\nUnwanted groups by hierarchy:");
for my $hierarchy (keys_by_reverse_count(\%hierarchies)) {
    say_checked(sprintf('%6d %s', $hierarchies{$hierarchy}, $hierarchy));
}

# Finally, print out the other reasons for rejection.
say_checked("\nOther rejections:");
for my $reason (keys_by_reverse_count(\%reasons)) {
    say_checked(sprintf('%6d %s', $reasons{$reason}, $reason));
}

__END__

##############################################################################
# Documentation
##############################################################################

=for stopwords
Allbery

=head1 NAME

inn-unwanted - Summarize unwanted groups from the current news logs

=head1 SYNOPSIS

B<inn-unwanted> I<site> I<log-path>

=head1 DESCRIPTION

Troll through the news log and spit out the reasons why we've refused articles
from a particular site.  This can be run at any time and can break down
reasons better than the nightly report.  We use this to tell other sites how
to fine-tune their feeds to us.

B<inn-unwanted> takes a site name and then some number of files on the command
line, or just a site name to read from standard input.  Normally you'll want
to point it at F</var/log/news/news>, which contains the news log for the
current day.

=head1 AUTHOR

Russ Allbery <eagle@eyrie.org>

=cut
