Complete.Org: Mailing Lists: Archives: offlineimap: May 2006:
[PATCH] improve savemessage_searchforheader
Home

[PATCH] improve savemessage_searchforheader

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: offlineimap mailing list <offlineimap@xxxxxxxxxxxx>
Subject: [PATCH] improve savemessage_searchforheader
From: Adam Spiers <offlineimap@xxxxxxxxxxxxxx>
Date: Mon, 1 May 2006 13:28:12 +0100
Reply-to: Adam Spiers <offlineimap@xxxxxxxxxxxxxx>

Hi all,

I found some issues with savemessage_searchforheader() in
offlineimap/folder/IMAP.py.

Firstly, Groupwise's IMAP server does not support parentheses in
SEARCH commands.  Offlineimap sends them like this:

  CNBD29 UID SEARCH (HEADER 
X-OfflineIMAP-1019557698-4e6f76656c6c2d66756c6c-436162696e65742f417263686976652f323030362f3031
 "1146222195-0387820881614-v4.0.11")

This causes a BAD response from the Groupwise IMAP server:

  CNBD29 BAD UID SEARCH Invalid parameter: HEADER 
X-OfflineIMAP-1019557698-4e6f76656c6c2d66756c6c-436162696e65742f417263686976652f323030362f3031
 "1146222195-0387820881614-v4.0.11"

The below patch removes the parentheses (and also passes the arguments
separately, since the routines called by uid() also perform quoting).

However, offlineimap still does not behave at all robustly in the face
of a BAD response to a UID SEARCH - savemessage_searchforheader()
returns 0, which results in an erroneous UID FETCH 0.  The server
replies with a BAD response as a result of the invalid UID, and
offlineimap then crashes :-(

Furthermore, offlineimap does not behave robustly in the face of the
UID SEARCH response which does not find any messages, e.g.

  A004 UID SEARCH 2334
  * SEARCH
  A004 OK UID SEARCH completed

In this case, matchinguids ends up being [''] since the code expects
a list of space-delimited numbers which it can split(' '), but 
''.split(' ') returns [''].  Then savemessage_searchforheader() tries
to return long(matchinguids[0]), but of course python doesn't like it
when you ask for long('').

So even with the below patch, there are two remaining issues in
savemessage_searchforheader() which IMHO require more defensive
programming.  It would be great if someone with more knowledge of
offlineimap (and python!) addressed these, or at least confirmed that
my analysis is correct.

Cheers,
Adam

diff -u -r offlineimap/offlineimap/folder/IMAP.py 
offlineimap-adam/offlineimap/folder/IMAP.py
--- offlineimap/offlineimap/folder/IMAP.py      2005-08-24 14:13:27.000000000 
+0100
+++ offlineimap-adam/offlineimap/folder/IMAP.py 2006-04-29 00:08:07.000000000 
+0100
@@ -158,8 +158,7 @@
         # Now find the UID it got.
         headervalue = imapobj._quote(headervalue)
         try:
-            matchinguids = imapobj.uid('search', None,
-                                       '(HEADER %s %s)' % (headername, 
headervalue))[1][0]
+            matchinguids = imapobj.uid('search', 'HEADER', headername, 
headervalue)[1][0]
         except imapobj.error:
             # IMAP server doesn't implement search or had a problem.
             return 0



[Prev in Thread] Current Thread [Next in Thread]