Deletions are complicated by POP’s message-numbering scheme. We learned about the potential for synchronization errors between the server’s inbox and the fetched email list in Chapter 14, when studying the mailtools package PyMailGUI uses (near Example 14-21). In brief, POP assigns each message a relative sequential number, starting from one, and these numbers are passed to the server to fetch and delete messages. The server’s inbox is normally locked while a connection is held so that a series of deletions can be run as an atomic operation; no other inbox changes occur until the connection is closed.
However, message number changes also have some implications for the GUI itself. It’s all right if new mail arrives while we’re displaying the result of a prior download—the new mail is assigned higher numbers, beyond what is displayed on the client. But if we delete a message in the middle of a mailbox after the index has been loaded from the mail server, the numbers of all messages after the one deleted change (they are decremented by one). As a result, some message numbers might no longer be valid if deletions are made while viewing previously loaded email.
To work around this, PyMailGUI adjusts all the displayed numbers after a Delete by simply removing the entries for deleted mails from its index list and mail cache. However, this adjustment is not enough to keep the GUI in sync with the server’s inbox if the inbox is modified at a time other than after the end, by deletions in another email client (even in another PyMailGUI session) or by deletions performed by the mail server itself (e.g., messages determined to be undeliverable and automatically removed from the inbox).
To handle these cases, PyMailGUI uses the safe deletion and synchronization tests inmailtools. That module uses mail header matching to detect mail list and server inbox synchronization errors. For instance, if another email client has deleted a message prior to the one to be deleted by PyMailGUI,mailtoolscatches the problem and cancels the deletion, and an error popup like the one in Figure 15-34 is displayed.
Figure 15-34. Safe deletion test detection of inbox difference
Similarly, index loads and message fetches run a synchronization test inmailtools, as well. Figure 15-35 captures the error generated if a message has been deleted in another client since we last loaded the server index window.
Figure 15-35. Synchronization error on after delete in another client
In both error cases, the inbox is automatically reloaded by PyMailGUI immediately after the error popup is dismissed. This scheme ensures that PyMailGUI won’t delete or display the wrong message, in the rare case that the server’s inbox is changed without its knowledge. Seemailtoolsin Chapter 14 for more on synchronization tests.