]> err.no Git - sope/blobdiff - sope-mime/NGImap4/NGImap4Client.m
fixed to qualifier handling
[sope] / sope-mime / NGImap4 / NGImap4Client.m
index 0511a259c9ce82b1ccba776bf1fad6e5805542af..3995c095605740f2dc9b564e6eaeb9b9d11d60fa 100644 (file)
@@ -1,20 +1,20 @@
 /*
-  Copyright (C) 2000-2004 SKYRIX Software AG
+  Copyright (C) 2000-2005 SKYRIX Software AG
 
-  This file is part of OpenGroupware.org.
+  This file is part of SOPE.
 
-  OGo is free software; you can redistribute it and/or modify it under
+  SOPE is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
 
-  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.
 
   You should have received a copy of the GNU Lesser General Public
-  License along with OGo; see the file COPYING.  If not, write to the
+  License along with SOPE; see the file COPYING.  If not, write to the
   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.
 */
 - (NSString *)imap4SearchString;
 @end
 
+@interface EOSortOrdering(IMAPAdditions)
+- (NSString *)imap4SortString;
+@end
+
+@interface NSArray(IMAPAdditions)
+- (NSString *)imap4SortStringForSortOrderings;
+@end
+
 @interface NGImap4Client(ConnectionRegistration)
 
 - (void)removeFromConnectionRegister;
@@ -98,7 +106,6 @@ static unsigned int MaxImapClients     = 0;
 static int          ProfileImapEnabled = -1;
 static int          LogImapEnabled     = -1;
 static int          PreventExceptions  = -1;
-static NSArray      *AllowedSortKeys   = nil;
 static BOOL         fetchDebug         = NO;
 static BOOL         ImapDebugEnabled   = NO;
 
@@ -129,10 +136,6 @@ static BOOL         ImapDebugEnabled   = NO;
   }
   if (ImapClients == NULL)
     ImapClients = calloc(MaxImapClients + 2, sizeof(id));
-
-  AllowedSortKeys = [[NSArray alloc] initWithObjects:
-                                         @"ARRIVAL", @"CC", @"DATE", @"FROM",
-                                         @"SIZE", @"SUBJECT", @"TO", nil];
 }
 
 /* constructors */
@@ -258,6 +261,10 @@ static BOOL         ImapDebugEnabled   = NO;
   return self->serverGID;
 }
 
+- (NSString *)selectedFolderName {
+  return self->selectedFolder;
+}
+
 /* connection */
 
 - (id)_openSocket {
@@ -608,9 +615,9 @@ static BOOL         ImapDebugEnabled   = NO;
    */
   NSString *s;
   id tmp;
-
-  tmp = self->selectedFolder;
-
+  
+  tmp = self->selectedFolder; // remember ptr to old folder name
+  
   if ([_folder length] == 0)
     return nil;
   if ((_folder = [self _folder2ImapFolder:_folder]) == nil)
@@ -618,7 +625,7 @@ static BOOL         ImapDebugEnabled   = NO;
 
   self->selectedFolder = [_folder copy];
   
-  [tmp release]; tmp = nil;
+  [tmp release]; tmp = nil; // release old folder name
 
   s = [NSString stringWithFormat:@"select \"%@\"", self->selectedFolder];
   return [self->normer normalizeSelectResponse:[self processCommand:s]];
@@ -694,15 +701,20 @@ static BOOL         ImapDebugEnabled   = NO;
 }
 
 - (NSDictionary *)fetchUids:(NSArray *)_uids parts:(NSArray *)_parts {
+  /*
+    eg: 'UID FETCH 1189,1325,1326 ([TODO])'
+  */
   NSAutoreleasePool *pool;
   NSString          *cmd;
   NSDictionary      *result;
+  NSString          *uidsStr, *partsStr;
   id fetchres;
   
   pool = [[NSAutoreleasePool alloc] init];
-  cmd  = [NSString stringWithFormat:@"uid fetch %@ (%@)",
-                  [self _uidsJoinedForFetchCmd:_uids],
-                  [self _partsJoinedForFetchCmd:_parts]];
+  
+  uidsStr  = [self _uidsJoinedForFetchCmd:_uids];
+  partsStr = [self _partsJoinedForFetchCmd:_parts];
+  cmd  = [NSString stringWithFormat:@"uid fetch %@ (%@)", uidsStr, partsStr];
   
   fetchres = [self processCommand:cmd];
   result   = [[self->normer normalizeFetchResponse:fetchres] retain];
@@ -830,6 +842,17 @@ static BOOL         ImapDebugEnabled   = NO;
   
   return [self->normer normalizeResponse:[self processCommand:cmd]];
 }
+- (NSDictionary *)copyUids:(NSArray *)_uids toFolder:(NSString *)_folder {
+  NSString *cmd;
+  
+  if ((_folder = [self _folder2ImapFolder:_folder]) == nil)
+    return nil;
+  
+  cmd = [NSString stringWithFormat:@"uid copy %@ \"%@\"", 
+                 [_uids componentsJoinedByString:@","], _folder];
+  
+  return [self->normer normalizeResponse:[self processCommand:cmd]];
+}
 
 - (NSDictionary *)getQuotaRoot:(NSString *)_folder {
   NSString *cmd;
@@ -868,24 +891,25 @@ static BOOL         ImapDebugEnabled   = NO;
     
     while (cntOld < (len - 1)) {
       if (old[cntOld] == '\n') {
-        new[cntNew++] = '\r';
-        new[cntNew++] = '\n';
+        new[cntNew] = '\r'; cntNew++;
+        new[cntNew] = '\n'; cntNew++;
       }
       else if (old[cntOld] != '\r') {
-        new[cntNew++] = old[cntOld];
+        new[cntNew] = old[cntOld]; cntNew++;
       }
       cntOld++;
     }
     if (old[cntOld] == '\n') {
-      new[cntNew++] = '\r';
-      new[cntNew++] = '\n';
+      new[cntNew] = '\r'; cntNew++;
+      new[cntNew] = '\n'; cntNew++;
     }
     else if (old[cntOld] != '\r') {
-      new[cntNew++] = old[cntOld];
-    } 
-    message = [(NSString *)[NSString alloc] 
-                  initWithCString:new length:cntNew];
-    if (new) free(new); new = NULL;
+      new[cntNew] = old[cntOld]; cntNew++;
+    }
+    
+    // TODO: fix this junk, do not treat the message as a string, its NSData
+    message = [(NSString *)[NSString alloc] initWithCString:new length:cntNew];
+    if (new != NULL) free(new); new = NULL;
   }
   
   icmd = [NSString stringWithFormat:@"append \"%@\" (%@) {%d}",
@@ -895,6 +919,7 @@ static BOOL         ImapDebugEnabled   = NO;
   result = [self processCommand:icmd
                  withTag:YES withNotification:NO];
   
+  // TODO: explain that
   if ([[result objectForKey:@"ContinuationResponse"] boolValue])
     result = [self processCommand:message withTag:NO];
 
@@ -941,7 +966,7 @@ static BOOL         ImapDebugEnabled   = NO;
           qualifier:_qualifier];
     return nil;
   }
-  return result;
+  return [@" " stringByAppendingString:result];
 }
 
 - (NSDictionary *)threadBySubject:(BOOL)_bySubject
@@ -977,62 +1002,11 @@ static BOOL         ImapDebugEnabled   = NO;
 }
 
 - (NSString *)_generateIMAP4SortOrdering:(EOSortOrdering *)_sortOrdering {
-  SEL      sel;
-  NSString *key;
-    
-  key = [_sortOrdering key];
-  if ([key length] == 0)
-    return nil;
-    
-  if (![AllowedSortKeys containsObject:[key uppercaseString]]) {
-    [self logWithFormat:@"ERROR[%s] key %@ is not allowed here!",
-           __PRETTY_FUNCTION__, key];
-    return nil;
-  }
-
-  sel = [_sortOrdering selector];
-  if (sel_eq(sel, EOCompareDescending) ||
-      sel_eq(sel, EOCompareCaseInsensitiveDescending)) {
-    return [@"REVERSE " stringByAppendingString:key];
-  }
-  // TODO: check other selectors whether they make sense instead of silent acc.
-  
-  return key;
+  // TODO: still called by anything?
+  return [_sortOrdering imap4SortString];
 }
-
 - (NSString *)_generateIMAP4SortOrderings:(NSArray *)_sortOrderings {
-  /*
-    turn EOSortOrdering into an IMAP4 value for "SORT()"
-    
-    eg: "DATE REVERSE SUBJECT"
-    
-    It also checks a set of allowed sort-keys (don't know why)
-  */
-  NSMutableString *sortStr;
-  NSEnumerator    *soe;
-  EOSortOrdering  *so;
-  BOOL            isFirst = YES;
-  
-  if ([_sortOrderings count] == 0)
-    return nil;
-  
-  sortStr = [NSMutableString stringWithCapacity:128];
-  soe     = [_sortOrderings objectEnumerator];
-  while ((so = [soe nextObject])) {
-    NSString *s;
-
-    s = [self _generateIMAP4SortOrdering:so];
-    if (s == nil)
-      continue;
-    
-    if (isFirst)
-      isFirst = NO;
-    else
-      [sortStr appendString:@" "];
-    
-    [sortStr appendString:s];
-  }
-  return isFirst ? nil : sortStr;
+  return [_sortOrderings imap4SortStringForSortOrderings];
 }
 
 - (NSDictionary *)primarySort:(NSString *)_sort
@@ -1317,13 +1291,14 @@ static BOOL         ImapDebugEnabled   = NO;
   [self sendCommand:_command withTag:YES logText:_command];
 }
 
-static inline NSArray *_flags2ImapFlags(NGImap4Client *self, NSArray *_flags) {
+- (NSArray *)_flags2ImapFlags:(NSArray *)_flags {
+  /* adds backslashes in front of the flags */
   NSEnumerator *enumerator;
   NSArray      *result;
   id           obj;
   id           *objs;
   unsigned     cnt;
-
+  
   objs = calloc([_flags count] + 2, sizeof(id));
   cnt  = 0;
   enumerator = [_flags objectEnumerator];
@@ -1332,9 +1307,12 @@ static inline NSArray *_flags2ImapFlags(NGImap4Client *self, NSArray *_flags) {
     cnt++;
   }
   result = [NSArray arrayWithObjects:objs count:cnt];
-  if (objs) free(objs);
+  if (objs != NULL) free(objs);
   return result;
 }
+static inline NSArray *_flags2ImapFlags(NGImap4Client *self, NSArray *_flags) {
+  return [self _flags2ImapFlags:_flags];
+}
 
 - (NSString *)_folder2ImapFolder:(NSString *)_folder {
   NSArray *array;