user/dev discussion of public-inbox itself
 help / color / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH] watch: quiet some warnings on spam mailboxes
Date: Sun,  2 Aug 2020 11:22:07 +0000
Message-ID: <20200802112207.3011-1-e@yhbt.net> (raw)

Email::Address::XS and PublicInbox::MsgTime both emit warnings
which are likely to trigger from spam messages.  Since this can
be configured to remove spam, just filter out those warnings to
avoid cluttering up stderr with useless information.
---
 lib/PublicInbox/InboxWritable.pm | 22 +++++++++++++++++++++-
 lib/PublicInbox/WatchMaildir.pm  | 12 +++++-------
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/lib/PublicInbox/InboxWritable.pm b/lib/PublicInbox/InboxWritable.pm
index 7fb5a150..752f1997 100644
--- a/lib/PublicInbox/InboxWritable.pm
+++ b/lib/PublicInbox/InboxWritable.pm
@@ -9,7 +9,7 @@ use parent qw(PublicInbox::Inbox Exporter);
 use PublicInbox::Import;
 use PublicInbox::Filter::Base qw(REJECT);
 use Errno qw(ENOENT);
-our @EXPORT_OK = qw(eml_from_path);
+our @EXPORT_OK = qw(eml_from_path warn_ignore_cb);
 
 use constant {
 	PERM_UMASK => 0,
@@ -278,4 +278,24 @@ sub cleanup ($) {
 	delete @{$_[0]}{qw(over mm git search)};
 }
 
+# warnings to ignore when handling spam mailboxes and maybe other places
+sub warn_ignore {
+	my $s = "@_";
+	# Email::Address::XS warnings
+	$s =~ /^Argument contains empty address at /
+	|| $s =~ /^Element at index [0-9]+ contains /
+	# PublicInbox::MsgTime
+	|| $s =~ /^bogus TZ offset: .+?, ignoring and assuming \+0000/
+	|| $s =~ /^bad Date: .+? in /
+}
+
+# this expects to be RHS in this assignment: "local $SIG{__WARN__} = ..."
+sub warn_ignore_cb {
+	my $cb = $SIG{__WARN__} // sub { print STDERR @_ };
+	sub {
+		return if warn_ignore(@_);
+		$cb->(@_);
+	}
+}
+
 1;
diff --git a/lib/PublicInbox/WatchMaildir.pm b/lib/PublicInbox/WatchMaildir.pm
index 7d4139a5..142118bd 100644
--- a/lib/PublicInbox/WatchMaildir.pm
+++ b/lib/PublicInbox/WatchMaildir.pm
@@ -7,7 +7,7 @@ package PublicInbox::WatchMaildir;
 use strict;
 use warnings;
 use PublicInbox::Eml;
-use PublicInbox::InboxWritable qw(eml_from_path);
+use PublicInbox::InboxWritable qw(eml_from_path warn_ignore_cb);
 use PublicInbox::Filter::Base qw(REJECT);
 use PublicInbox::Spamcheck;
 use PublicInbox::Sigfd;
@@ -154,6 +154,7 @@ sub _remove_spam {
 	# path must be marked as (S)een
 	$path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
 	my $eml = eml_from_path($path) or return;
+	local $SIG{__WARN__} = warn_ignore_cb();
 	$self->{config}->each_inbox(\&remove_eml_i, [ $self, $eml, $path ]);
 }
 
@@ -197,10 +198,7 @@ sub _try_path {
 		return;
 	}
 	my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
-	local $SIG{__WARN__} = sub {
-		$warn_cb->("path: $path\n");
-		$warn_cb->(@_);
-	};
+	local $SIG{__WARN__} = sub { $warn_cb->("path: $path\n", @_) };
 	if (!ref($inboxes) && $inboxes eq 'watchspam') {
 		return _remove_spam($self, $path);
 	}
@@ -393,6 +391,7 @@ sub imap_import_msg ($$$$) {
 			my $x = import_eml($self, $ibx, $eml);
 		}
 	} elsif ($inboxes eq 'watchspam') {
+		local $SIG{__WARN__} = warn_ignore_cb();
 		my $eml = PublicInbox::Eml->new($raw);
 		my $arg = [ $self, $eml, "$url UID:$uid" ];
 		$self->{config}->each_inbox(\&remove_eml_i, $arg);
@@ -445,8 +444,7 @@ sub imap_fetch_all ($$$) {
 	my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
 	local $SIG{__WARN__} = sub {
 		$batch //= '?';
-		$warn_cb->("$url UID:$batch\n");
-		$warn_cb->(@_);
+		$warn_cb->("$url UID:$batch\n", @_);
 	};
 	my $err;
 	do {

                 reply index

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200802112207.3011-1-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror http://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git