1 /* SOGoMailObject+Draft.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
25 #import <NGObjWeb/WOApplication.h>
26 #import <NGObjWeb/WOResponse.h>
27 #import <NGObjWeb/WOContext+SoObjects.h>
28 #import <NGExtensions/NSString+misc.h>
29 #import <NGExtensions/NSObject+Logs.h>
31 #import <SoObjects/SOGo/NSArray+Utilities.h>
32 #import <SoObjects/SOGo/SOGoUser.h>
34 #import "NSString+Mail.h"
35 #import "SOGoMailForward.h"
36 #import "SOGoMailObject+Draft.h"
37 #import "SOGoMailReply.h"
39 #define maxFilenameLength 64
41 @implementation SOGoMailObject (SOGoDraftObjectExtensions)
43 - (NSString *) subjectForReply
45 static NSString *replyPrefixes[] = {
47 @"RE:", // Outlook v11 (English?)
48 @"AW:", // German Outlook v11
49 @"Re[", // numbered Re, eg "Re[2]:"
54 NSString *subject, *newSubject;
58 subject = [self decodedSubject];
60 while (!hasPrefix && replyPrefixes[i])
61 if ([subject hasPrefix: replyPrefixes[i]])
69 newSubject = [NSString stringWithFormat: @"Re: %@", subject];
75 - (NSString *) _contentForEditingFromKeys: (NSArray *) keys
79 NSString *rawPart, *content, *contentKey;
85 types = [keys objectsForKey: @"mimeType"];
86 index = [types indexOfObject: @"text/plain"];
87 if (index == NSNotFound)
89 index = [types indexOfObject: @"text/html"];
94 if (index == NSNotFound)
98 contentKey = [keys objectAtIndex: index];
99 parts = [self fetchPlainTextStrings:
100 [NSArray arrayWithObject: contentKey]];
101 rawPart = [[parts allValues] objectAtIndex: 0];
103 content = [rawPart htmlToText];
114 - (NSString *) contentForEditing
116 NSMutableArray *keys;
117 NSArray *acceptedTypes;
120 = [NSArray arrayWithObjects: @"text/plain", @"text/html", nil];
121 keys = [NSMutableArray new];
122 [self addRequiredKeysOfStructure: [self bodyStructure]
123 path: @"" toArray: keys acceptedTypes: acceptedTypes];
125 return [self _contentForEditingFromKeys: keys];
128 - (NSString *) contentForReply
130 SOGoUser *currentUser;
134 currentUser = [context activeUser];
135 pageName = [NSString stringWithFormat: @"SOGoMail%@Reply",
136 [currentUser language]];
137 page = [[WOApplication application] pageWithName: pageName
139 [page setRepliedMail: self];
141 return [[page generateResponse] contentAsString];
144 - (NSString *) filenameForForward
147 NSMutableString *newSubject;
148 static NSString *sescape[] = {
149 @"/", @"..", @"~", @"\"", @"'", @" ", @".", nil
151 unsigned int count, length;
153 subject = [self decodedSubject];
154 length = [subject length];
157 subject = @"forward";
158 length = [subject length];
161 if (length > maxFilenameLength)
162 length = maxFilenameLength;
163 newSubject = [NSMutableString
164 stringWithString: [subject substringToIndex: length]];
166 while (sescape[count])
168 [newSubject replaceString: sescape[count]
172 [newSubject appendString: @".eml"];
177 - (NSString *) subjectForForward
179 NSString *subject, *newSubject;
181 subject = [self decodedSubject];
182 if ([subject length] > 0)
183 newSubject = [NSString stringWithFormat: @"[Fwd: %@]", subject];
185 newSubject = subject;
190 - (NSString *) contentForInlineForward
192 SOGoUser *currentUser;
194 SOGoMailForward *page;
196 currentUser = [context activeUser];
197 pageName = [NSString stringWithFormat: @"SOGoMail%@Forward",
198 [currentUser language]];
199 page = [[WOApplication application] pageWithName: pageName
201 [page setForwardedMail: self];
203 return [[page generateResponse] contentAsString];
206 - (void) _fetchFileAttachmentKey: (NSDictionary *) part
207 intoArray: (NSMutableArray *) keys
208 withPath: (NSString *) path
210 NSDictionary *disposition, *currentFile;
211 NSString *filename, *mimeType;
213 disposition = [part objectForKey: @"disposition"];
214 filename = [[disposition objectForKey: @"parameterList"]
215 objectForKey: @"filename"];
218 mimeType = [NSString stringWithFormat: @"%@/%@",
219 [part objectForKey: @"type"],
220 [part objectForKey: @"subtype"]];
221 currentFile = [NSDictionary dictionaryWithObjectsAndKeys:
222 filename, @"filename",
223 [mimeType lowercaseString], @"mimetype",
225 objectForKey: @"encoding"], @"encoding",
227 [keys addObject: currentFile];
231 - (void) _fetchFileAttachmentKeysInPart: (NSDictionary *) part
232 intoArray: (NSMutableArray *) keys
233 withPath: (NSString *) path
235 NSEnumerator *subparts;
238 NSDictionary *currentPart;
241 type = [[part objectForKey: @"type"] lowercaseString];
242 if ([type isEqualToString: @"multipart"])
244 subparts = [[part objectForKey: @"parts"] objectEnumerator];
245 currentPart = [subparts nextObject];
250 newPath = [NSString stringWithFormat: @"%@.%d", path, count];
252 newPath = [NSString stringWithFormat: @"%d", count];
253 [self _fetchFileAttachmentKeysInPart: currentPart
256 currentPart = [subparts nextObject];
261 [self _fetchFileAttachmentKey: part intoArray: keys withPath: path];
264 #warning we might need to handle parts with a "name" attribute
265 - (NSArray *) fetchFileAttachmentKeys
267 NSMutableArray *keys;
269 keys = [NSMutableArray array];
270 [self _fetchFileAttachmentKeysInPart: [self bodyStructure]
271 intoArray: keys withPath: nil];