From f03a970c35524c6dbd8f1d154b83a25efbf7121b Mon Sep 17 00:00:00 2001
From: Jean Raby <jraby@inverse.ca>
Date: Tue, 5 Feb 2013 18:02:00 -0500
Subject: [PATCH] Add quoted string support in sieve authenticate

By setting 'SieveQuotedAuth' to TRUE, NGSieveClient will send
the auth string on a single line, in a 'quoted string' instead
of the string literal like it does by default.

This should be used as a workaround for broken manage sieve servers.
Fixes #2230
---
 sope-mime/NGImap4/NGSieveClient.m |   28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/sope-mime/NGImap4/NGSieveClient.m b/sope-mime/NGImap4/NGSieveClient.m
index 22c705c..e579ad9 100644
--- a/sope-mime/NGImap4/NGSieveClient.m
+++ b/sope-mime/NGImap4/NGSieveClient.m
@@ -82,6 +82,7 @@ static NSNumber *NoNumber          = nil;
 static BOOL     ProfileImapEnabled = NO;
 static BOOL     LOG_PASSWORD       = NO;
 static BOOL     debugImap4         = NO;
+static BOOL     quotedAuth         = NO;
 
 + (void)initialize {
   static BOOL didInit = NO;
@@ -93,6 +94,7 @@ static BOOL     debugImap4         = NO;
   LOG_PASSWORD       = [ud boolForKey:@"SieveLogPassword"];
   ProfileImapEnabled = [ud boolForKey:@"ProfileImapEnabled"];
   debugImap4         = [ud boolForKey:@"ImapDebugEnabled"];
+  quotedAuth         = [ud boolForKey:@"SieveQuotedAuth"];
   
   YesNumber = [[NSNumber numberWithBool:YES] retain];
   NoNumber  = [[NSNumber numberWithBool:NO] retain];
@@ -391,6 +393,7 @@ static BOOL     debugImap4         = NO;
 - (NSDictionary *)login {
   NGHashMap *map  = nil;
   NSData    *auth;
+  NSString  *s, *logText;
   char      *buf;
   int       bufLen, logLen, authLen;
   
@@ -426,20 +429,27 @@ static BOOL     debugImap4         = NO;
   auth = [NSData dataWithBytesNoCopy:buf length:bufLen];
   auth = [auth dataByEncodingBase64WithLineLength:4096 /* 'unlimited' */];
   
+  if (quotedAuth)
+    {
+      s = [NSString stringWithFormat:@"AUTHENTICATE \"PLAIN\" \"%s\"",
+                                      [auth bytes]];
+      logText = [NSString stringWithString:
+                        @"AUTHENTICATE \"PLAIN\" LOGIN:PASSWORD\r\n"];
+    }
+  else
+    {
+      s = [NSString stringWithFormat:@"AUTHENTICATE \"PLAIN\" {%d+}\r\n%s",
+                                     [auth length], [auth bytes]];
+      logText = [NSString stringWithString:
+                        @"AUTHENTICATE \"PLAIN\" {%d+}\r\nLOGIN:PASSWORD\r\n"];
+    }
+
   if (LOG_PASSWORD) {
-    NSString *s;
-    
-    s = [NSString stringWithFormat:@"AUTHENTICATE \"PLAIN\" {%d+}\r\n%s",
-                  [auth length], [auth bytes]];
     map = [self processCommand:s];
   }
   else {
-    NSString *s;
-    
-    s = [NSString stringWithFormat:@"AUTHENTICATE \"PLAIN\" {%d+}\r\n%s",
-                  [auth length], [auth bytes]];
     map = [self processCommand:s
-                logText:@"AUTHENTICATE \"PLAIN\" {%d+}\r\nLOGIN:PASSWORD\r\n"];
+                logText:logText];
   }
 
   if (map == nil) {
-- 
1.7.9.5

