Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error parsing labels #545

Closed
HeinrichHartmann opened this issue May 3, 2018 · 16 comments
Closed

Error parsing labels #545

HeinrichHartmann opened this issue May 3, 2018 · 16 comments

Comments

@HeinrichHartmann
Copy link

General informations

  • system/distribution (with version): 7.2.0 (git master)
  • offlineimap version (offlineimap -V): offlineimap v7.2.0, imaplib2 v2.57 (bundled), Python v2.7.10, OpenSSL 0.9.8zh 14 Jan 2016
  • Python version: Python 2.7.10
  • server name or domain:
  • CLI options: -a Gmail-Private

Configuration file offlineimaprc

...

Logs, error

 ERROR: ERROR in syncfolder for Gmail-Private folder [Gmail]/All Mail: Traceback (most recent call last):
  File "/Users/hartmann/git/offlineimap/offlineimap/accounts.py", line 633, in syncfolder
    remotefolder.cachemessagelist()
  File "/Users/hartmann/git/offlineimap/offlineimap/folder/Gmail.py", line 168, in cachemessagelist
    labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])
  File "/Users/hartmann/git/offlineimap/offlineimap/imaputil.py", line 162, in imapsplit
    (quoted, rest) = __split_quoted(workstr)
  File "/Users/hartmann/git/offlineimap/offlineimap/imaputil.py", line 276, in __split_quoted
    raise ValueError("can't find ending quote '%s' in '%s'"% (q, s))
ValueError: can't find ending quote '"' in '"Webserver (RW.net'

  can't find ending quote '"' in '"Webserver (RW.net'
Account sync Gmail-Private:
 *** Finished account 'Gmail-Private' in 0:50

Steps to reproduce the error

Hit the above error message when syncing my Gmail account.

  1. Get a Gmail Account
  2. Make a label "Webserver (RW.net)"
  3. Run offlineimap
@HeinrichHartmann
Copy link
Author

__split_qouted is called with this string "Webserver (RW.net so the problem seems to be further up.

@HeinrichHartmann
Copy link
Author

Inserted a few more prints. Here is how imapsplit is called:

imapsplit('X-GM-LABELS ("Webserver (RW.net)" "\\Important") UID 82031 FLAGS ()')
imapsplit('"Webserver (RW.net')

Looks like the parens are not matched correctly.

@nicolas33
Copy link
Member

Could you show the string messagestr in folder/Gmail.py in the for loop (line 150), please?

@HeinrichHartmann
Copy link
Author

This is the value of the offending message str:
121251 (X-GM-LABELS ("Webserver (RW.net)" "\\Inbox" GInbox) UID 275440 FLAGS (\Seen))

For reference, this is hot the full print log looks like:

messagestr:'121251 (X-GM-LABELS ("Webserver (RW.net)" "\\Inbox" GInbox) UID 275440 FLAGS (\Seen))'
imapsplit('X-GM-LABELS ("Webserver (RW.net)" "\\Inbox" GInbox) UID 275440 FLAGS (\Seen)')
imapsplit('"Webserver (RW.net')
__split_qouted('"Webserver (RW.net')

@nicolas33
Copy link
Member

Thank you.

The issue stands in folder/Gmail.py:

165                 m = re.search('\(([^\)]*)\)', options['X-GM-LABELS'])
167                     labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])

At line 165, options is {'FLAGS': '(\\Seen)', 'X-GM-LABELS': '("Webserver (RW.net)" "\\Inbox" GInbox)', 'UID': '275440'}

In the console, I get this output:

>>> labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "offlineimap/imaputil.py", line 162, in imapsplit
    (quoted, rest) = __split_quoted(workstr)
  File "offlineimap/imaputil.py", line 276, in __split_quoted
    raise ValueError("can't find ending quote '%s' in '%s'"% (q, s))
ValueError: can't find ending quote '"' in '"Webserver (RW.net'
>>> m.group(1)
'"Webserver (RW.net'

So, our regex fails at matching the first label inside the double quotes. The parenthesis are not offending.
Now, it's time to find the fix.

nicolas33 added a commit to nicolas33/offlineimap that referenced this issue May 4, 2018
Github-ref: OfflineIMAP#545
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
@nicolas33
Copy link
Member

@HeinrichHartmann Could you please try the above version with a fix?

@HeinrichHartmann
Copy link
Author

@nicolas33 Sure!
Unfortunately it does not seem to help:

hartmann@HHMB:/Users/hartmann/git/offlineimap git:(4313355)
$ python offlineimap.py -a Gmail-Private                                                                                                                                                                      2018-05-07 11:22:27 [6.338s]
OfflineIMAP 7.2.0
  Licensed under the GNU GPL v2 or any later version (with an OpenSSL exception)
imaplib2 v2.57 (bundled), Python v2.7.15, OpenSSL 1.0.2o  27 Mar 2018
Account sync Gmail-Private:
 *** Processing account Gmail-Private
 Establishing connection to imap.gmail.com:993 (Gmail-Private-remote)
Folder [Gmail]/All Mail [acc: Gmail-Private]:
 Syncing [Gmail]/All Mail: Gmail -> GmailMaildir
 Collecting data from messages on [Gmail]/All Mail
 ERROR: ERROR in syncfolder for Gmail-Private folder [Gmail]/All Mail: Traceback (most recent call last):
  File "/Users/hartmann/git/offlineimap/offlineimap/accounts.py", line 633, in syncfolder
    remotefolder.cachemessagelist()
  File "/Users/hartmann/git/offlineimap/offlineimap/folder/Gmail.py", line 169, in cachemessagelist
    labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])
  File "/Users/hartmann/git/offlineimap/offlineimap/imaputil.py", line 162, in imapsplit
    (quoted, rest) = __split_quoted(workstr)
  File "/Users/hartmann/git/offlineimap/offlineimap/imaputil.py", line 276, in __split_quoted
    raise ValueError("can't find ending quote '%s' in '%s'"% (q, s))
ValueError: can't find ending quote '"' in '"Webserver (RW.net'

  can't find ending quote '"' in '"Webserver (RW.net'
Account sync Gmail-Private:
 *** Finished account 'Gmail-Private' in 0:42
ERROR: Exceptions occurred during the run!
ERROR: ERROR in syncfolder for Gmail-Private folder [Gmail]/All Mail: Traceback (most recent call last):
  File "/Users/hartmann/git/offlineimap/offlineimap/accounts.py", line 633, in syncfolder
    remotefolder.cachemessagelist()
  File "/Users/hartmann/git/offlineimap/offlineimap/folder/Gmail.py", line 169, in cachemessagelist
    labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])
  File "/Users/hartmann/git/offlineimap/offlineimap/imaputil.py", line 162, in imapsplit
    (quoted, rest) = __split_quoted(workstr)
  File "/Users/hartmann/git/offlineimap/offlineimap/imaputil.py", line 276, in __split_quoted
    raise ValueError("can't find ending quote '%s' in '%s'"% (q, s))
ValueError: can't find ending quote '"' in '"Webserver (RW.net'

  can't find ending quote '"' in '"Webserver (RW.net'
hartmann@HHMB:/Users/hartmann/git/offlineimap git:(4313355)
$ git status                                                                                                                                                                                                 2018-05-07 11:23:16 [42.420s]
HEAD detached at N33/b545-gmail-error-parsing-label
nothing to commit, working tree clean

@HeinrichHartmann
Copy link
Author

Also I don't see how your fix would help with that string:

  • String: ("Webserver (RW.net)" "\\Inbox" GInbox) UID 275440 FLAGS (\Seen)
  • Regexp: '^[(]([^\)]*)[)]'

Matches "Webserver (RW.net ...

Looking for balanced parens would also not fix the issue, since the label can contain arbitrary bracket expressions.
A proper solution would probably need to match the quoted string properly, allowing escaped quotations marks in the middle. Not sure how to do this best.

@HeinrichHartmann
Copy link
Author

Note that reproducing this is very easy: Just add a label XXX-) to a new email and run offlineimap.

nicolas33 added a commit to nicolas33/offlineimap that referenced this issue May 7, 2018
Github-ref: OfflineIMAP#545
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
@nicolas33
Copy link
Member

Sorry, for the noise. I've forgot to change the middle of the regexp. Here is the fix that I intend.

@HeinrichHartmann
Copy link
Author

Looks good. I am still waiting for my full mailbox to be synced up (200K+ Mails), before I can be 100% sure that it worked.

@nicolas33
Copy link
Member

Ok, let us know.

@HeinrichHartmann
Copy link
Author

New error:

Thread 'Copy message from Gmail-Private-remote:[Gmail]/All Mail' terminated with exception:
Traceback (most recent call last):
  File "/Users/hartmann/git/offlineimap/offlineimap/threadutil.py", line 160, in run
    Thread.run(self)
  File "/usr/local/Cellar/python@2/2.7.15/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/Users/hartmann/git/offlineimap/offlineimap/folder/Gmail.py", line 290, in copymessageto
    super(GmailFolder, self).copymessageto(uid, dstfolder, statusfolder, register)
  File "/Users/hartmann/git/offlineimap/offlineimap/folder/Base.py", line 839, in copymessageto
    message = self.getmessage(uid)
  File "/Users/hartmann/git/offlineimap/offlineimap/folder/Gmail.py", line 84, in getmessage
    labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])
  File "/Users/hartmann/git/offlineimap/offlineimap/imaputil.py", line 162, in imapsplit
    (quoted, rest) = __split_quoted(workstr)
  File "/Users/hartmann/git/offlineimap/offlineimap/imaputil.py", line 276, in __split_quoted
    raise ValueError("can't find ending quote '%s' in '%s'"% (q, s))
ValueError: can't find ending quote '"' in '"Webserver (RW.net'

Looks like this regexp is also problematic:

https://github.com/OfflineIMAP/offlineimap/blob/master/offlineimap/folder/Gmail.py?utf8=%E2%9C%93#L82

Fix ?!

diff --git a/offlineimap/folder/Gmail.py b/offlineimap/folder/Gmail.py
index 51cd635..af0d0b2 100644
--- a/offlineimap/folder/Gmail.py
+++ b/offlineimap/folder/Gmail.py
@@ -79,7 +79,7 @@ class GmailFolder(IMAPFolder):

         # Embed the labels into the message headers
         if self.synclabels:
-            m = re.search('X-GM-LABELS\s*\(([^\)]*)\)', data[0][0])
+            m = re.search('X-GM-LABELS\s*\((.*)\)', data[0][0])
             if m:
                 labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])
             else:

nicolas33 added a commit to nicolas33/offlineimap that referenced this issue May 9, 2018
Originally-written-by: Heinrich Hartmann <Heinrich@HeinrichHartmann.com>
Github-ref: OfflineIMAP#545
@nicolas33
Copy link
Member

Your fix looks good. I've changed the backslashes to the brackets that I find more readable and for consistency.

@HeinrichHartmann
Copy link
Author

Cool. This solves the issue for me. Thanks for fixing it so quickly!

@nicolas33
Copy link
Member

Thanks for the feedback.

nicolas33 added a commit that referenced this issue May 18, 2018
Reported-by Heinrich Hartmann <Heinrich@HeinrichHartmann.com>
Tested-by Heinrich Hartmann <Heinrich@HeinrichHartmann.com>
Github-ref: #545
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants