Patch to conditionally set IMAP \Seen flag

Robert Tsai <[email protected]>
Newsgroups gmane.mail.getmail.user
Message-ID <CAKvGS0Hzu1RTPtncyCgOGH1tzbbwnu_4BdVoSE5ZNkjkr-nmrA@mail.gmail.com>
Hi--

Version 4.52.0 introduced an "EXPERIMENTAL" feature to set the \Seen flag
when deleting from an IMAP mailbox, documented in the CHANGELOG as
accommodating MSExchange

I've attached a patch (against getmail-5.8) to conditionally set this IMAP
flag based on an option, rather than hard-coding in that flag, with its
default value set to True, to preserve the behavior introduced in 4.52.0.

The always-set-\Seen behavior is OK if getmail is infrequently run as a
"Gmail backup application"; the "primary" client (e.g., web app or phone)
will likely have read the mail already anyway.

However, this doesn't work very well if getmail runs before the message is
seen by the primary client, such as if getmail is run very frequently as an
incremental backup application (e.g., every 5 minutes). In this case:

   - Setting \Seen is likely to "incorrectly" mark a message as read before
   the "primary" client has a chance to see it.
   - *Not* setting "\Seen" has the effect of marking the message as
   "unread" (in Gmail, anyway), which is the "more correct" user-visible
   "no-op" if getmail runs before the "primary" client. (If getmail runs
   *after* the "primary" client, then the message will mysteriously appear to
   transition from "read" to "unread").

Ideally, where would be a way to simply preserve the Seen/NotSeen state of
the message on the server, while setting the \Deleted flag, but I don't
know if that is possible. So I simply made \Seen behavior in this codepath
a user-configurable option.

I noticed this change in behavior when upgrading my system from Ubuntu
16.04 (getmail-4.48) to Ubuntu 18.04 (getmail-5.5).

Please consider accepting this patch for inclusion into upstream.

Thanks,
--Rob

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
set_seen_on_delete.patch.txt (text/plain, 3.2 KB)
diff -ru getmail-5.8/docs/configuration.html getmail-5.8.1/docs/configuration.html
--- getmail-5.8/docs/configuration.html	2018-11-09 07:23:19.000000000 -0800
+++ getmail-5.8.1/docs/configuration.html	2019-01-07 22:53:36.663422785 -0800
@@ -674,6 +674,12 @@
         <a href="http://www.bytereef.org/howto/oauth2/getmail.html">additional
         information about using it here</a>.
     </li>
+    <li>
+        set_seen_on_delete
+        (<a href="#parameter-boolean">boolean</a>)
+        &mdash; if set, sets the &bsol;Seen flag on deleted messages. This is
+	on by default, but can be disabled.
+    </li>
 </ul>
 
 <h4 id="retriever-ssl-client">SSL Client Parameters</h4>
diff -ru getmail-5.8/docs/configuration.txt getmail-5.8.1/docs/configuration.txt
--- getmail-5.8/docs/configuration.txt	2018-11-09 07:23:20.000000000 -0800
+++ getmail-5.8.1/docs/configuration.txt	2019-01-07 22:53:36.647423332 -0800
@@ -431,6 +431,8 @@
        access and refresh tokens to, and the file requires manual initial
        setup. This functionality was contributed by Stefan Krah, who has
        additional information about using it here.
+     * set_seen_on_delete (boolean) - if set, sets the \Seen flag on
+       deleted messages. This is on by default, but can be disabled.
 
 SSL Client Parameters
 
diff -ru getmail-5.8/getmailcore/_retrieverbases.py getmail-5.8.1/getmailcore/_retrieverbases.py
--- getmail-5.8/getmailcore/_retrieverbases.py	2018-11-09 07:23:16.000000000 -0800
+++ getmail-5.8.1/getmailcore/_retrieverbases.py	2019-01-07 23:22:21.584395922 -0800
@@ -1530,9 +1530,13 @@
                 response = self._parse_imapuidcmdresponse(
                     'COPY', uid, self.conf['move_on_delete']
                 )
+            flags = ['\Deleted']
+            if self.conf['set_seen_on_delete']:
+                flags.append('\Seen')
+            flagstr = '(' + ' '.join(flags) + ')'
             self.log.debug('deleting message "%s"' % uid + os.linesep)
             response = self._parse_imapuidcmdresponse(
-                'STORE', uid, 'FLAGS', '(\Deleted \Seen)'
+                'STORE', uid, 'FLAGS', flagstr
             )
         except imaplib.IMAP4.error, o:
             raise getmailOperationError('IMAP error (%s)' % o)
diff -ru getmail-5.8/getmailcore/retrievers.py getmail-5.8.1/getmailcore/retrievers.py
--- getmail-5.8/getmailcore/retrievers.py	2018-04-02 14:47:38.000000000 -0700
+++ getmail-5.8.1/getmailcore/retrievers.py	2019-01-07 23:01:02.428180964 -0800
@@ -389,6 +389,7 @@
         ConfBool(name='use_cram_md5', required=False, default=False),
         ConfBool(name='use_kerberos', required=False, default=False),
         ConfBool(name='use_xoauth2', required=False, default=False),
+        ConfBool(name='set_seen_on_delete', required=False, default=True),
     )
     received_from = None
     received_with = 'IMAP4'
@@ -440,6 +441,7 @@
         ConfBool(name='use_kerberos', required=False, default=False),
         ConfBool(name='use_xoauth2', required=False, default=False),
         ConfString(name='ssl_cert_hostname', required=False, default=None),
+        ConfBool(name='set_seen_on_delete', required=False, default=True),
     )
     received_from = None
     received_with = 'IMAP4-SSL'
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.