{"id":4621,"date":"2014-06-06T08:58:42","date_gmt":"2014-06-06T06:58:42","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=4621"},"modified":"2014-06-09T22:17:46","modified_gmt":"2014-06-09T20:17:46","slug":"imap-search-requires-parentheses","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/imap-search-requires-parentheses","title":{"rendered":"IMAP search requires parentheses"},"content":{"rendered":"<p>IMAP protocol uses Polish notation (also known as prefix notation) for client queries. In general it means, that operator (in case of IMAP search it&#8217;s always a logical operator) is placed left of its operands.<\/p>\n<h2>Polish notation<\/h2>\n<p>Expression that would be written in conventional infix notation as:<br \/>\n<code>(5 \u2212 6) * 7<\/code><br \/>\ncan be written in prefix\/Polish notation as:<br \/>\n<code>* (\u2212 5 6) 7<\/code><br \/>\nAs all mathematical operands are binary (or have defined number of operands) we don&#8217;t need parentheses to correctly evaluate this expression:<br \/>\n<code>* \u2212 5 6 7<\/code><\/p>\n<p><strong>Polish notation doesn&#8217;t require parentheses<\/strong>.<\/p>\n<h2>IMAP search needs parentheses<\/h2>\n<p>Although IMAP search syntax uses Polish notation<strong> it actually requires parentheses to be evaluated unambiguously<\/strong>.<\/p>\n<p>This is <strong>because there is no explicitly defined AND operator<\/strong>, that takes exactly 2 operands.<\/p>\n<p><a href=\"\/mail\/rfc\/3501\">RFC 3501<\/a>:<\/p>\n<blockquote><p>When multiple keys are specified, the result is the intersection (AND function) of all the messages that match those keys.<\/p><\/blockquote>\n<p>So although it is true that Polish and reverse Polish notations don&#8217;t require parentheses, it is not true that IMAP search doesn&#8217;t require them.<\/p>\n<h2>&#8216;OR\/AND&#8217; example<\/h2>\n<h3>Expression 1a:<\/h3>\n<p>Regular notation:<br \/>\n<code>(SUBJECT subject OR BODY body) AND FROM from<\/code><\/p>\n<p>Polish notation:<br \/>\n<code>AND OR SUBJECT subject BODY body FROM from<\/code><\/p>\n<p>IMAP&#8217;s Polish notation (note there is no AND operator):<br \/>\n<code><span style=\"color: #ff0000;\">OR SUBJECT subject BODY body FROM from<\/span><\/code><\/p>\n<h3>Expression 1b:<\/h3>\n<p>Regular notation:<br \/>\n<code>SUBJECT subject OR (BODY body AND FROM from)<\/code><\/p>\n<p>Polish notation:<br \/>\n<code>OR SUBJECT subject AND BODY body FROM from<\/code><\/p>\n<p>IMAP&#8217;s Polish notation (note there is no AND operator):<br \/>\n<code><span style=\"color: #ff0000;\">OR SUBJECT subject BODY body FROM from<\/span><\/code><\/p>\n<h3>1a != 1b<\/h3>\n<p>Expression 1a is not equal to Expression 1b:<br \/>\n<code>((a or b) and c) != (a or (b and c))<\/code><\/p>\n<p>For:<br \/>\n<code>a = true<br \/>\nb = true<br \/>\nc = false<\/code><\/p>\n<p>We have:<br \/>\n<code>((true or true) and false) != (true or (true and false))<br \/>\nfalse != true<\/code><\/p>\n<h3>Parentheses are needed<\/h3>\n<p>But when there is no AND operator, IMAP&#8217;s Polish notations look exactly the same, thus we need parentheses!<\/p>\n<p><strong>Expression 1a (IMAP&#8217;s Polish notation with parentheses):<\/strong><br \/>\n<code><span style=\"color: #22AA22;\">(OR SUBJECT subject BODY body) FROM from<\/span><\/code><\/p>\n<p><strong>Expression 1b (IMAP&#8217;s Polish notation with parentheses):<\/strong><br \/>\n<code><span style=\"color: #22AA22;\">OR SUBJECT subject (BODY body FROM from)<\/span><\/code><\/p>\n<p><code>OR SUBJECT subject BODY body FROM from<\/code><br \/>\nis treated like<br \/>\n<code>(OR SUBJECT subject BODY body) FROM from<\/code><\/p>\n<h2>&#8216;NOT&#8217; example<\/h2>\n<h3>Expression 2a:<\/h3>\n<p>Regular notation:<br \/>\n<code>NOT (SUBJECT subject AND BODY body)<\/code><\/p>\n<p>Polish notation:<br \/>\n<code>NOT AND SUBJECT subject BODY body<\/code><\/p>\n<p>IMAP&#8217;s Polish notation (note there is no AND operator):<br \/>\n<code><span style=\"color: #ff0000;\">NOT SUBJECT subject BODY body<\/span><\/code><\/p>\n<h3>Expression 2b:<\/h3>\n<p>Regular notation:<br \/>\n<code>(NOT SUBJECT subject) AND BODY body<\/code><\/p>\n<p>Polish notation:<br \/>\n<code>AND NOT SUBJECT subject BODY body<\/code><\/p>\n<p>IMAP&#8217;s Polish notation (note there is no AND operator):<br \/>\n<code><span style=\"color: #ff0000;\">NOT SUBJECT subject BODY body<\/span><\/code><\/p>\n<h3>2a != 2b<\/h3>\n<p>Expression 2a is obviously not equal to Expression 2b. <\/p>\n<h3>Parentheses are needed<\/h3>\n<p>Again IMAP&#8217;s Polish notations look the same. We need parentheses!<\/p>\n<p><strong>Expression 2a (IMAP&#8217;s Polish notation with parentheses):<\/strong><br \/>\n<code><span style=\"color: #22AA22;\">NOT (SUBJECT subject BODY body)<\/span><\/code><\/p>\n<p><strong>Expression 2b (IMAP&#8217;s Polish notation with parentheses):<\/strong><br \/>\n<code><span style=\"color: #22AA22;\">(NOT SUBJECT subject) BODY body<\/span><\/code><\/p>\n<p><code>NOT SUBJECT subject BODY body<\/code><br \/>\nis treated like<br \/>\n<code>(NOT SUBJECT subject) BODY body<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IMAP protocol uses Polish notation (also known as prefix notation) for client queries. In general it means, that operator (in case of IMAP search it&#8217;s always a logical operator) is placed left of its operands. Polish notation Expression that would be written in conventional infix notation as: (5 \u2212 6) * 7 can be written [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[28,77],"class_list":["post-4621","post","type-post","status-publish","format-standard","hentry","category-mail-dll","category-programming","tag-imap","tag-imap-component"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4621"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=4621"}],"version-history":[{"count":33,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4621\/revisions"}],"predecessor-version":[{"id":4659,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4621\/revisions\/4659"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=4621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=4621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=4621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}