user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 3/2] imap: *SEARCH: fix CHARSET handling
  @ 2020-06-16  7:04  7% ` Eric Wong
  0 siblings, 0 replies; 1+ results
From: Eric Wong @ 2020-06-16  7:04 UTC (permalink / raw)
  To: meta

We no longer pass an arrayref to search_common() or
parse_query(), so handle the CHARSET directive in
the Parse::RecDescent-generated parser directly.
---
 lib/PublicInbox/IMAP.pm         | 23 +++++++++--------------
 lib/PublicInbox/IMAPsearchqp.pm |  8 ++++++--
 t/imap_searchqp.t               | 12 ++++++++++++
 3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/lib/PublicInbox/IMAP.pm b/lib/PublicInbox/IMAP.pm
index dd983dfd282..64b57a3ef69 100644
--- a/lib/PublicInbox/IMAP.pm
+++ b/lib/PublicInbox/IMAP.pm
@@ -1154,13 +1154,8 @@ sub xap_append ($$$$) {
 }
 
 sub parse_query ($$) {
-	my ($self, $rest) = @_;
-	if (uc($rest->[0]) eq 'CHARSET') {
-		shift @$rest;
-		defined(my $c = shift @$rest) or return 'BAD missing charset';
-		$c =~ /\A(?:UTF-8|US-ASCII)\z/ or return 'NO [BADCHARSET]';
-	}
-	my $q = PublicInbox::IMAPsearchqp::parse($self, join(' ', @$rest));
+	my ($self, $query) = @_;
+	my $q = PublicInbox::IMAPsearchqp::parse($self, $query);
 	if (ref($q)) {
 		my $max = $self->{ibx}->over->max;
 		my $beg = 1;
@@ -1202,9 +1197,9 @@ sub search_xap_range { # long_response
 }
 
 sub search_common {
-	my ($self, $tag, $rest, $want_msn) = @_;
+	my ($self, $tag, $query, $want_msn) = @_;
 	my $ibx = $self->{ibx} or return "$tag BAD No mailbox selected\r\n";
-	my $q = parse_query($self, $rest);
+	my $q = parse_query($self, $query);
 	return "$tag $q\r\n" if !ref($q);
 	my ($sql, $range_info) = delete @$q{qw(sql range_info)};
 	if (!scalar(keys %$q)) { # overview.sqlite3
@@ -1222,14 +1217,14 @@ sub search_common {
 	}
 }
 
-sub cmd_uid_search ($$$;) {
-	my ($self, $tag) = splice(@_, 0, 2);
-	search_common($self, $tag, \@_);
+sub cmd_uid_search ($$$) {
+	my ($self, $tag, $query) = @_;
+	search_common($self, $tag, $query);
 }
 
 sub cmd_search ($$$;) {
-	my ($self, $tag) = splice(@_, 0, 2);
-	search_common($self, $tag, \@_, 1);
+	my ($self, $tag, $query) = @_;
+	search_common($self, $tag, $query, 1);
 }
 
 sub args_ok ($$) { # duplicated from PublicInbox::NNTP
diff --git a/lib/PublicInbox/IMAPsearchqp.pm b/lib/PublicInbox/IMAPsearchqp.pm
index fba3baccf50..c9b442cb4fa 100644
--- a/lib/PublicInbox/IMAPsearchqp.pm
+++ b/lib/PublicInbox/IMAPsearchqp.pm
@@ -165,7 +165,7 @@ sub msn_set {
 my $prd = Parse::RecDescent->new(<<'EOG');
 <nocheck>
 { my $q = $PublicInbox::IMAPsearchqp::q; }
-search_key : search_key1(s) { $return = $q }
+search_key : CHARSET(?) search_key1(s) { $return = $q }
 search_key1 : "ALL" | "RECENT" | "UNSEEN" | "NEW"
 	| OR_search_keys
 	| NOT_search_key
@@ -188,6 +188,10 @@ search_key1 : "ALL" | "RECENT" | "UNSEEN" | "NEW"
 	| sub_query
 	| <error>
 
+charset : /\S+/
+CHARSET : 'CHARSET' charset
+{ $item{charset} =~ /\A(?:UTF-8|US-ASCII)\z/ ? 1 : die('NO [BADCHARSET]'); }
+
 SENTSINCE_date : 'SENTSINCE' date { $q->SENTSINCE(\%item) }
 SENTON_date : 'SENTON' date { $q->SENTON(\%item) }
 SENTBEFORE_date : 'SENTBEFORE' date { $q->SENTBEFORE(\%item) }
@@ -253,7 +257,7 @@ sub parse {
 	%$q = (sql => \$sql, imap => $imap); # imap = PublicInbox::IMAP obj
 	# $::RD_TRACE = 1;
 	my $res = eval { $prd->search_key(uc($query)) };
-	return $@ if $@ && $@ =~ /\ABAD /;
+	return $@ if $@ && $@ =~ /\A(?:BAD|NO) /;
 	return 'BAD unexpected result' if !$res || $res != $q;
 	if (exists $q->{sql}) {
 		delete $q->{xap};
diff --git a/t/imap_searchqp.t b/t/imap_searchqp.t
index 3e4dde6ffae..d73600b35d2 100644
--- a/t/imap_searchqp.t
+++ b/t/imap_searchqp.t
@@ -21,6 +21,18 @@ is($q->{xap}, '(t:"brian" OR (f:"ryan" OR (t:"joe" OR c:"scott")))',
 $q = $parse->(qq{HEADER CC b SENTSINCE 2-Oct-1993});
 is($q->{xap}, 'c:"b" d:19931002..', 'compound query');
 
+$q = $parse->(qq{CHARSET UTF-8 From b});
+is($q->{xap}, 'f:"b"', 'charset handled');
+$q = $parse->(qq{CHARSET WTF-8 From b});
+like($q, qr/\ANO \[/, 'bad charset rejected');
+{
+	# TODO: squelch errors by default? clients could flood logs
+	open my $fh, '>:scalar', \(my $buf) or die;
+	local *STDERR = $fh;
+	$q = $parse->(qq{CHARSET});
+}
+like($q, qr/\ABAD /, 'bad charset rejected');
+
 $q = $parse->(qq{HEADER CC B (SENTBEFORE 2-Oct-1993)});
 is($q->{xap}, 'c:"b" d:..19931002', 'compound query w/ parens');
 

^ permalink raw reply related	[relevance 7%]

Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2020-06-16  5:05     [PATCH 0/2] imap: search improvements Eric Wong
2020-06-16  7:04  7% ` [PATCH 3/2] imap: *SEARCH: fix CHARSET handling Eric Wong

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).