Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Mail rule action "Move to specified folder" not working
### Description

If mail rule action is "Move to specified folder", the mail.log contains:

```
[2023-06-06 23:50:00,229] [DEBUG] [paperless_mail] Processing mail account T-Online (Thorsten)
[2023-06-06 23:50:00,342] [DEBUG] [paperless_mail] GMAIL Label Support: False
[2023-06-06 23:50:00,343] [DEBUG] [paperless_mail] AUTH=PLAIN Support: False
[2023-06-06 23:50:00,389] [DEBUG] [paperless_mail] Account T-Online (Thorsten): Processing 1 rule(s)
[2023-06-06 23:50:00,395] [DEBUG] [paperless_mail] Rule T-Online (Thorsten).Paperless Consume Ordner: Selecting folder INBOX.Paperless
[2023-06-06 23:50:00,410] [DEBUG] [paperless_mail] Rule T-Online (Thorsten).Paperless Consume Ordner: Searching folder with criteria None
[2023-06-06 23:50:00,422] [ERROR] [paperless_mail] Rule T-Online (Thorsten).Paperless Consume Ordner: Error while processing rule: SEARCH command error: BAD [b'Error in IMAP command SEARCH: Unknown argument NONE (0.001 + 0.000 secs).']
Traceback (most recent call last):
  File "/usr/src/paperless/src/paperless_mail/mail.py", line 495, in handle_mail_account
    total_processed_files += self._handle_mail_rule(
  File "/usr/src/paperless/src/paperless_mail/mail.py", line 562, in _handle_mail_rule
    for message in messages:
  File "/usr/local/lib/python3.9/site-packages/imap_tools/mailbox.py", line 170, in fetch
    nums = tuple((reversed if reverse else iter)(self.numbers(criteria, charset)))[limit_range]
  File "/usr/local/lib/python3.9/site-packages/imap_tools/mailbox.py", line 108, in numbers
    search_result = self.client.search(charset, encoded_criteria)
  File "/usr/local/lib/python3.9/imaplib.py", line 732, in search
    typ, dat = self._simple_command(name, 'CHARSET', charset, *criteria)
  File "/usr/local/lib/python3.9/imaplib.py", line 1230, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/local/lib/python3.9/imaplib.py", line 1055, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: SEARCH command error: BAD [b'Error in IMAP command SEARCH: Unknown argument NONE (0.001 + 0.000 secs).']
```

It seems that this action is the only one not working, since it's the only one with  `Searching folder with criteria None` , and this seems to be the root cause for the error.

If mail rule action is set to e.g "Flag the mail, don't process flaged mails", then the mail.log looks fine:
```
[2023-06-06 23:40:00,273] [DEBUG] [paperless_mail] Processing mail account T-Online (Thorsten)
[2023-06-06 23:40:00,425] [DEBUG] [paperless_mail] GMAIL Label Support: False
[2023-06-06 23:40:00,426] [DEBUG] [paperless_mail] AUTH=PLAIN Support: False
[2023-06-06 23:40:00,468] [DEBUG] [paperless_mail] Account T-Online (Thorsten): Processing 1 rule(s)
[2023-06-06 23:40:00,474] [DEBUG] [paperless_mail] Rule T-Online (Thorsten).Paperless Consume Ordner: Selecting folder INBOX.Paperless
[2023-06-06 23:40:00,489] [DEBUG] [paperless_mail] Rule T-Online (Thorsten).Paperless Consume Ordner: Searching folder with criteria (UNKEYWORD PaperlessConsumed)
[2023-06-06 23:40:00,502] [DEBUG] [paperless_mail] Rule T-Online (Thorsten).Paperless Consume Ordner: Processed 0 matching mail(s)
```

### Steps to reproduce

1. Create mail rule, choose action  "Move to specified folder"
2. Wait ten minutes
3. Check mail.log

### Webserver logs

```bash
See error description
```


### Browser logs

_No response_

### Paperless-ngx version

1.15.0

### Host OS

Synology

### Installation method

Docker - official image

### Browser

_No response_

### Configuration changes

_No response_

### Other

_No response_


Discussion:
I can't test it at the moment, but likely it's from this line: https://github.com/paperless-ngx/paperless-ngx/blob/36b4fff5c75f4f4effdf6fd560f4cb8c858a518d/src/paperless_mail/mail.py#L385

That condition not being satisfied would result in an implicit `None` return, which `imap_tools` then sends off to the server.  Some might handle it, but at least appears not to.

I'm guessing you've also set no maximum age?  ie a maximum age of 0?

Resolving patch:
diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py
--- a/src/paperless_mail/mail.py
+++ b/src/paperless_mail/mail.py
@@ -384,6 +384,8 @@ def make_criterias(rule: MailRule, supports_gmail_labels: bool):
     if isinstance(rule_query, dict):
         if len(rule_query) or len(criterias):
             return AND(**rule_query, **criterias)
+        else:
+            return "ALL"
     else:
         return AND(rule_query, **criterias)
 


Test patch:
diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.py
--- a/src/paperless_mail/tests/test_mail.py
+++ b/src/paperless_mail/tests/test_mail.py
@@ -721,6 +721,31 @@ def test_handle_mail_account_move(self):
         self.assertEqual(len(self.bogus_mailbox.messages), 2)
         self.assertEqual(len(self.bogus_mailbox.messages_spam), 1)
 
+    def test_handle_mail_account_move_no_filters(self):
+        account = MailAccount.objects.create(
+            name="test",
+            imap_server="",
+            username="admin",
+            password="secret",
+        )
+
+        _ = MailRule.objects.create(
+            name="testrule",
+            account=account,
+            action=MailRule.MailAction.MOVE,
+            action_parameter="spam",
+            maximum_age=0,
+        )
+
+        self.assertEqual(len(self.bogus_mailbox.messages), 3)
+        self.assertEqual(len(self.bogus_mailbox.messages_spam), 0)
+
+        self.mail_account_handler.handle_mail_account(account)
+        self.apply_mail_actions()
+
+        self.assertEqual(len(self.bogus_mailbox.messages), 0)
+        self.assertEqual(len(self.bogus_mailbox.messages_spam), 3)
+
     def test_handle_mail_account_tag(self):
         account = MailAccount.objects.create(
             name="test",