user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] lei: flesh out `forwarded' kw support for Maildir and IMAP
@ 2021-04-21 23:50 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2021-04-21 23:50 UTC (permalink / raw)
  To: meta

Maildir and IMAP can both handle `forwarded'.  Ensure we don't
lose `forwarded' when reading from stores which do not support
it, but ensure we can set it when reading from IMAP and Maildir
stores.
---
 lib/PublicInbox/LeiSearch.pm | 12 ++++++++++--
 lib/PublicInbox/LeiToMail.pm |  2 +-
 lib/PublicInbox/NetReader.pm |  1 +
 t/lei-q-kw.t                 | 31 +++++++++++++++++++++++++++++++
 xt/net_writer-imap.t         | 14 +++++++++++---
 5 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/lib/PublicInbox/LeiSearch.pm b/lib/PublicInbox/LeiSearch.pm
index 082176e7..ff615d89 100644
--- a/lib/PublicInbox/LeiSearch.pm
+++ b/lib/PublicInbox/LeiSearch.pm
@@ -103,8 +103,16 @@ sub kw_changed {
 	my $xoids = xoids_for($self, $eml) // return;
 	$docids //= [];
 	@$docids = sort { $a <=> $b } values %$xoids;
-	my @cur_kw = msg_keywords($self, $docids->[0]);
-	join("\0", @$new_kw_sorted) eq join("\0", @cur_kw) ? 0 : 1;
+	my $cur_kw = msg_keywords($self, $docids->[0]);
+
+	# RFC 5550 sec 5.9 on the $Forwarded keyword states:
+	# "Once set, the flag SHOULD NOT be cleared"
+	if (exists($cur_kw->{forwarded}) &&
+			!grep(/\Aforwarded\z/, @$new_kw_sorted)) {
+		delete $cur_kw->{forwarded};
+	}
+	$cur_kw = join("\0", sort keys %$cur_kw);
+	join("\0", @$new_kw_sorted) eq $cur_kw ? 0 : 1;
 }
 
 sub all_terms {
diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index 46a82a4b..0fa0bd9a 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -38,7 +38,7 @@ sub _mbox_hdr_buf ($$$) {
 		if (my $ent = $kw2status{$k}) {
 			push @{$hdr{$ent->[0]}}, $ent->[1];
 		} else { # X-Label?
-			warn "TODO: keyword `$k' not supported for mbox\n";
+			warn "# keyword `$k' not supported for mbox\n";
 		}
 	}
 	# Messages are always 'O' (non-\Recent in IMAP), it saves
diff --git a/lib/PublicInbox/NetReader.pm b/lib/PublicInbox/NetReader.pm
index 821e5d7f..0ef66fd8 100644
--- a/lib/PublicInbox/NetReader.pm
+++ b/lib/PublicInbox/NetReader.pm
@@ -8,6 +8,7 @@ use v5.10.1;
 use parent qw(Exporter PublicInbox::IPC);
 use PublicInbox::Eml;
 our %IMAPflags2kw = map {; "\\\u$_" => $_ } qw(seen answered flagged draft);
+$IMAPflags2kw{'$Forwarded'} = 'forwarded';  # RFC 5550
 
 our @EXPORT = qw(uri_section imap_uri nntp_uri);
 
diff --git a/t/lei-q-kw.t b/t/lei-q-kw.t
index c17411fb..c00a0a43 100644
--- a/t/lei-q-kw.t
+++ b/t/lei-q-kw.t
@@ -205,5 +205,36 @@ open $fh, '<', \$lei_out or BAIL_OUT $!;
 PublicInbox::MboxReader->mboxrd($fh, sub { push @another, shift });
 is($another[0]->header('Status'), 'RO', 'seen kw set');
 
+# forwarded
+{
+	local $ENV{DBG} = 1;
+	$o = "$ENV{HOME}/forwarded";
+	lei_ok(qw(q -o), $o, "m:$m");
+	my @p = glob("$o/cur/*");
+	scalar(@p) == 1 or xbail('multiple when 1 expected', \@p);
+	my $passed = $p[0];
+	$passed =~ s/,S\z/,PS/ or xbail "failed to replace $passed";
+	rename($p[0], $passed) or xbail "rename $!";
+	lei_ok(qw(q -o), $o, 'm:bogus', \'clobber maildir');
+	is_deeply([glob("$o/cur/*")], [], 'old results clobbered');
+	lei_ok(qw(q -o), $o, "m:$m");
+	@p = glob("$o/cur/*");
+	scalar(@p) == 1 or xbail('multiple when 1 expected', \@p);
+	like($p[0], qr/,PS/, 'passed (Forwarded) flag kept');
+	lei_ok(qw(q -o), "mboxrd:$o.mboxrd", "m:$m");
+	open $fh, '<', "$o.mboxrd" or xbail $!;
+	my @res;
+	PublicInbox::MboxReader->mboxrd($fh, sub { push @res, shift });
+	scalar(@res) == 1 or xbail('multiple when 1 expected', \@res);
+	is($res[0]->header('Status'), 'RO', 'seen kw set');
+	is($res[0]->header('X-Status'), undef, 'no X-Status');
+
+	lei_ok(qw(q -o), "mboxrd:$o.mboxrd", 'bogus-for-import-before');
+	lei_ok(qw(q -o), $o, "m:$m");
+	@p = glob("$o/cur/*");
+	scalar(@p) == 1 or xbail('multiple when 1 expected', \@p);
+	like($p[0], qr/,PS/, 'passed (Forwarded) flag still kept');
+}
+
 }); # test_lei
 done_testing;
diff --git a/xt/net_writer-imap.t b/xt/net_writer-imap.t
index 11a10e74..007de35e 100644
--- a/xt/net_writer-imap.t
+++ b/xt/net_writer-imap.t
@@ -173,17 +173,18 @@ test_lei(sub {
 	is_deeply([@$res{qw(m kw)}], ['testmessage@example.com', ['seen']],
 		'kw set');
 
+	# prepare messages for watch
 	$mic = $nwr->mic_for_folder($folder_uri);
-	for my $kw (qw(Deleted Seen Answered Draft)) {
+	for my $kw (qw(Deleted Seen Answered Draft forwarded)) {
 		my $buf = <<EOM;
 From: x\@example.com
 Message-ID: <$kw\@test.example.com>
 
 EOM
-		$mic->append_string($folder_uri->mailbox, $buf, "\\$kw")
+		my $f = $kw eq 'forwarded' ? '$Forwarded' : "\\$kw";
+		$mic->append_string($folder_uri->mailbox, $buf, $f)
 			or BAIL_OUT "append $kw $@";
 	}
-	# $mic->expunge or BAIL_OUT "expunge: $@";
 	$mic->disconnect;
 
 	my $inboxdir = "$ENV{HOME}/wtest";
@@ -214,6 +215,13 @@ EOM
 		'-watch ignored \\Deleted');
 	ok(!defined($mm->num_for('Draft@test.example.com')),
 		'-watch ignored \\Draft');
+	ok(defined($mm->num_for('forwarded@test.example.com')),
+		'-watch takes forwarded message');
+	undef $w; # done with watch
+	lei_ok qw(import), $$folder_uri;
+	lei_ok qw(q m:forwarded@test.example.com);
+	is_deeply(json_utf8->decode($lei_out)->[0]->{kw}, ['forwarded'],
+		'forwarded kw imported from IMAP');
 });
 
 undef $cleanup; # remove temporary folder

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-21 23:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-21 23:50 [PATCH] lei: flesh out `forwarded' kw support for Maildir and IMAP Eric Wong

user/dev discussion of public-inbox itself

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://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

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 meta meta/ https://public-inbox.org/meta \
		meta@public-inbox.org
	public-inbox-index meta

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.mail.public-inbox.meta
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.mail.public-inbox.meta
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.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/

code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

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