From 8a9300f8b5d212e0f877c11f25ad4aa0cd882dba Mon Sep 17 00:00:00 2001
From: Matt Taggart <taggart@riseup.net>
Date: Mon, 23 Sep 2019 19:09:08 -0700
Subject: [PATCH] countbouncecodes, for a given domain give counts of the
 bounce codes

---
 tools/countbouncecodes | 54 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100755 tools/countbouncecodes

diff --git a/tools/countbouncecodes b/tools/countbouncecodes
new file mode 100755
index 0000000..cdaf581
--- /dev/null
+++ b/tools/countbouncecodes
@@ -0,0 +1,54 @@
+#!/usr/bin/perl -w
+# countbouncecodes - for a given domain, give a list of bounce codes by count
+# NOTE: this script requires the current user has read access to the db
+# Matt Taggart <taggart@riseup.net> 2019-09
+
+my $listsql=q(mysql -N --batch --database=sympa --execute "select user_subscriber, bounce_subscriber from subscriber_table WHERE bounce_subscriber <> 'NULL'");
+
+if ( ! $ARGV[0] ) {
+  print "usage: countbouncecodes <domain>\n";
+  exit 1;
+}
+my $target = $ARGV[0];
+
+open(LISTDUMP, "$listsql|") or die "cannot get dump of address+bounce pairs from database\n";
+
+while (<LISTDUMP>) {
+  chomp;
+  # match sane address and list
+  m/^<?(.*\@(.*?))>?\s*\t(.*) (.*) (.*) (.*)$/;
+  my $address=$1;
+  my $domain=$2;
+  my $firstbounce=$3;
+  my $lastbounce=$4;
+  my $bouncescore=$5;
+  my $bouncecode=$6;
+
+  #print "$address $domain $firstbounce $lastbounce $bouncescore $bouncecode\n";
+
+  if ( $domain eq $target ) {
+    # hash, key is code, value is count
+    $codes{$bouncecode}++;
+  }
+}
+
+format Foo =
+@>>>>   @<<<<<<<<<<<
+$count, $code
+.
+
+$~= 'Foo';
+
+$count='count';
+$code='code';
+
+print "Bounce codes for $target\n";
+write;
+print "-------------\n";
+
+# sort by count
+foreach $code ( sort { $codes{$b} <=> $codes{$a} } keys %codes) {
+  $count=$codes{$code};
+  write;
+  #if ( $count == $limit ) {last;}
+}
-- 
GitLab