]> err.no Git - scalable-opengroupware.org/blobdiff - SoObjects/Mailer/SOGoDraftObject.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1205 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / Mailer / SOGoDraftObject.m
index 6c49beb97d42bd72b4b67ca7fd384c980b3f4f0b..2cc71010e49324d027058eefafd6d656707c3bd9 100644 (file)
 
 #import <SoObjects/SOGo/NSArray+Utilities.h>
 #import <SoObjects/SOGo/NSCalendarDate+SOGo.h>
+#import <SoObjects/SOGo/NSString+Utilities.h>
 #import <SoObjects/SOGo/SOGoMailer.h>
 #import <SoObjects/SOGo/SOGoUser.h>
+
+#import "NSData+Mail.h"
 #import "SOGoMailAccount.h"
 #import "SOGoMailFolder.h"
 #import "SOGoMailObject.h"
 
 static NSString *contentTypeValue = @"text/plain; charset=utf-8";
 static NSString *headerKeys[] = {@"subject", @"to", @"cc", @"bcc", 
-                                @"from", @"replyTo", nil};
-
-@interface NSString (NGMimeHelpers)
-
-- (NSString *) asQPSubjectString: (NSString *) encoding;
-
-@end
-
-@implementation NSString (NGMimeHelpers)
-
-- (NSString *) asQPSubjectString: (NSString *) encoding;
-{
-  NSString *qpString, *subjectString;
-  NSData *subjectData, *destSubjectData;
-
-  subjectData = [self dataUsingEncoding: NSUTF8StringEncoding];
-  destSubjectData = [subjectData dataByEncodingQuotedPrintable];
-
-  qpString = [[NSString alloc] initWithData: destSubjectData
-                              encoding: NSASCIIStringEncoding];
-  [qpString autorelease];
-  if ([qpString length] > [self length])
-    subjectString = [NSString stringWithFormat: @"=?%@?Q?%@?=",
-                             encoding, qpString];
-  else
-    subjectString = self;
-
-  return subjectString;
-}
-
-@end
+                                @"from", @"replyTo",
+                                nil};
 
 @implementation SOGoDraftObject
 
@@ -128,6 +102,7 @@ static BOOL        showTextAttachmentsInline  = NO;
       text = @"";
       sourceURL = nil;
       sourceFlag = nil;
+      inReplyTo = nil;
     }
 
   return self;
@@ -141,6 +116,7 @@ static BOOL        showTextAttachmentsInline  = NO;
   [path release];
   [sourceURL release];
   [sourceFlag release];
+  [inReplyTo release];
   [super dealloc];
 }
 
@@ -190,7 +166,7 @@ static BOOL        showTextAttachmentsInline  = NO;
   id headerValue;
   unsigned int count;
 
-  for (count = 0; count < 6; count++)
+  for (count = 0; count < 7; count++)
     {
       headerValue = [newHeaders objectForKey: headerKeys[count]];
       if (headerValue)
@@ -216,6 +192,11 @@ static BOOL        showTextAttachmentsInline  = NO;
   return text;
 }
 
+- (void) setInReplyTo: (NSString *) newInReplyTo
+{
+  ASSIGN (inReplyTo, newInReplyTo);
+}
+
 - (void) setSourceURL: (NSString *) newSourceURL
 {
   ASSIGN (sourceURL, newSourceURL);
@@ -237,6 +218,8 @@ static BOOL        showTextAttachmentsInline  = NO;
       [infos setObject: headers forKey: @"headers"];
       if (text)
        [infos setObject: text forKey: @"text"];
+      if (inReplyTo)
+       [infos setObject: inReplyTo forKey: @"inReplyTo"];
       if (IMAP4ID > -1)
        [infos setObject: [NSNumber numberWithInt: IMAP4ID]
               forKey: @"IMAP4ID"];
@@ -291,6 +274,10 @@ static BOOL        showTextAttachmentsInline  = NO;
   value = [infoDict objectForKey: @"sourceFlag"];
   if (value)
     [self setSourceFlag: value];
+
+  value = [infoDict objectForKey: @"inReplyTo"];
+  if (value)
+    [self setInReplyTo: value];
 }
 
 - (NSString *) relativeImap4Name
@@ -426,30 +413,49 @@ static BOOL        showTextAttachmentsInline  = NO;
     }
 }
 
+- (NSArray *) _attachmentBodiesFromPaths: (NSArray *) paths
+                      fromResponseFetch: (NSDictionary *) fetch;
+{
+  NSEnumerator *attachmentKeys;
+  NSMutableArray *bodies;
+  NSString *currentKey;
+  NSDictionary *body;
+
+  bodies = [NSMutableArray array];
+
+  attachmentKeys = [paths objectEnumerator];
+  while ((currentKey = [attachmentKeys nextObject]))
+    {
+      body = [fetch objectForKey: [currentKey lowercaseString]];
+      [bodies addObject: [body objectForKey: @"data"]];
+    }
+
+  return bodies;
+}
+
 - (void) _fetchAttachments: (NSArray *) parts
                   fromMail: (SOGoMailObject *) sourceMail
 {
   unsigned int count, max;
-  NSDictionary *currentPart, *attachment, *body;
-  NSArray *paths, *result;
+  NSArray *paths, *bodies;
+  NSData *body;
+  NSDictionary *currentInfo;
+  NGHashMap *response;
 
   max = [parts count];
   if (max > 0)
     {
       paths = [parts keysWithFormat: @"BODY[%{path}]"];
-      result = [[sourceMail fetchParts: paths] objectForKey: @"fetch"];
+      response = [[sourceMail fetchParts: paths] objectForKey: @"RawResponse"];
+      bodies = [self _attachmentBodiesFromPaths: paths
+                    fromResponseFetch: [response objectForKey: @"fetch"]];
       for (count = 0; count < max; count++)
        {
-         currentPart = [parts objectAtIndex: count];
-         body = [[result objectAtIndex: count] objectForKey: @"body"];
-         attachment = [NSDictionary dictionaryWithObjectsAndKeys:
-                                      [currentPart objectForKey: @"filename"],
-                                    @"filename",
-                                    [currentPart objectForKey: @"mimetype"],
-                                    @"mime-type",
-                                    nil];
-         [self saveAttachment: [body objectForKey: @"data"]
-               withMetadata: attachment];
+         currentInfo = [parts objectAtIndex: count];
+         body = [[bodies objectAtIndex: count]
+                  bodyDataFromEncoding: [currentInfo
+                                          objectForKey: @"encoding"]];
+         [self saveAttachment: body withMetadata: currentInfo];
        }
     }
 }
@@ -498,15 +504,21 @@ static BOOL        showTextAttachmentsInline  = NO;
 - (void) fetchMailForReplying: (SOGoMailObject *) sourceMail
                        toAll: (BOOL) toAll
 {
-  NSString *contentForReply;
+  NSString *contentForReply, *msgID;
   NSMutableDictionary *info;
+  NGImap4Envelope *sourceEnvelope;
 
   [sourceMail fetchCoreInfos];
 
   info = [NSMutableDictionary dictionaryWithCapacity: 16];
   [info setObject: [sourceMail subjectForReply] forKey: @"subject"];
+
+  sourceEnvelope = [sourceMail envelope];
   [self _fillInReplyAddresses: info replyToAll: toAll
-       envelope: [sourceMail envelope]];
+       envelope: sourceEnvelope];
+  msgID = [sourceEnvelope messageID];
+  if ([msgID length] > 0)
+    [self setInReplyTo: msgID];
   contentForReply = [sourceMail contentForReply];
   [self setText: contentForReply];
   [self setHeaders: info];
@@ -538,7 +550,7 @@ static BOOL        showTextAttachmentsInline  = NO;
 //   error = [newDraft saveAttachment:content withName:@"forward.mail"];
       attachment = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [sourceMail filenameForForward], @"filename",
-                                @"message/rfc822", @"mime-type",
+                                @"message/rfc822", @"mimetype",
                                 nil];
       [self saveAttachment: [sourceMail content]
            withMetadata: attachment];
@@ -587,7 +599,7 @@ static BOOL        showTextAttachmentsInline  = NO;
 
 - (BOOL) isValidAttachmentName: (NSString *) _name
 {
-  static NSString *sescape[] = { @"/", @"..", @"~", @"\"", @"'", @" ", nil };
+  static NSString *sescape[] = { @"/", @"..", @"~", @"\"", @"'", nil };
   unsigned i;
   NSRange  r;
 
@@ -620,7 +632,8 @@ static BOOL        showTextAttachmentsInline  = NO;
                    withMetadata: (NSDictionary *) metadata
 {
   NSString *p, *name, *mimeType;
-  
+  NSRange r;
+
   if (![_attach isNotNull]) {
     return [NSException exceptionWithHTTPStatus:400 /* Bad Request */
                        reason: @"Missing attachment content!"];
@@ -630,7 +643,13 @@ static BOOL        showTextAttachmentsInline  = NO;
     return [NSException exceptionWithHTTPStatus:500 /* Server Error */
                        reason: @"Could not create folder for draft!"];
   }
+
   name = [metadata objectForKey: @"filename"];
+  r = [name rangeOfString: @"\\"
+           options: NSBackwardsSearch];
+  if (r.length > 0)
+    name = [name substringFromIndex: r.location + 1];
+
   if (![self isValidAttachmentName: name])
     return [self invalidAttachmentNameError: name];
   
@@ -641,7 +660,7 @@ static BOOL        showTextAttachmentsInline  = NO;
                          reason: @"Could not write attachment to draft!"];
     }
 
-  mimeType = [metadata objectForKey: @"mime-type"];
+  mimeType = [metadata objectForKey: @"mimetype"];
   if ([mimeType length] > 0)
     {
       p = [self pathToAttachmentWithName:
@@ -1029,7 +1048,9 @@ static BOOL        showTextAttachmentsInline  = NO;
     [map setObjects:[map objectsForKey: @"from"] forKey: @"reply-to"];
   
   /* add subject */
-  
+  if (inReplyTo)
+    [map setObject: inReplyTo forKey: @"in-reply-to"];
+
   if ([(s = [headers objectForKey: @"subject"]) length] > 0)
     [map setObject: [s asQPSubjectString: @"utf-8"]
         forKey: @"subject"];