2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "UIxMailEditorAction.h"
24 @interface UIxMailReplyAction : UIxMailEditorAction
27 #include <SoObjects/Mailer/SOGoMailObject.h>
28 #include <SoObjects/Mailer/SOGoDraftObject.h>
29 #include <NGImap4/NGImap4EnvelopeAddress.h>
30 #include <NGImap4/NGImap4Envelope.h>
33 @implementation UIxMailReplyAction
35 - (BOOL)hasReplyPrefix:(NSString *)_subject {
36 static NSString *replyPrefixes[] = {
38 @"RE:", // Outlook v11 (English?)
39 @"AW:", // German Outlook v11
40 @"Re[", // numbered Re, eg "Re[2]:"
44 for (i = 0; replyPrefixes[i] != nil; i++) {
45 if ([_subject hasPrefix:replyPrefixes[i]])
51 - (NSString *)replySubject:(NSString *)_subject {
52 if (![_subject isNotNull] || [_subject length] == 0)
55 if ([self hasReplyPrefix:_subject]) {
56 /* do not do: "Re: Re: Re: My Mail" - a single Re is sufficient ;-) */
60 return [@"Re: " stringByAppendingString:_subject];
63 - (void)addEMailsOfAddresses:(NSArray *)_addrs toArray:(NSMutableArray *)_ma {
66 for (i = 0, count = [_addrs count]; i < count; i++)
67 [_ma addObject:[(NGImap4EnvelopeAddress *)[_addrs objectAtIndex:i] email]];
70 - (void)fillInReplyAddresses:(NSMutableDictionary *)_info
71 replyToAll:(BOOL)_replyToAll
72 envelope:(NGImap4Envelope *)_envelope
75 The rules as implemented by Thunderbird:
76 - if there is a 'reply-to' header, only include that (as TO)
77 - if we reply to all, all non-from addresses are added as CC
78 - the from is always the lone TO (except for reply-to)
80 Note: we cannot check reply-to, because Cyrus even sets a reply-to in the
81 envelope if none is contained in the message itself! (bug or
84 TODO: what about sender (RFC 822 3.6.2)
89 to = [NSMutableArray arrayWithCapacity:2];
91 /* first check for "reply-to" */
93 addrs = [_envelope replyTo];
94 if ([addrs count] == 0) {
95 /* no "reply-to", try "from" */
96 addrs = [_envelope from];
98 [self addEMailsOfAddresses:addrs toArray:to];
99 [_info setObject:to forKey:@"to"];
101 /* CC processing if we reply-to-all: add all 'to' and 'cc' */
104 to = [NSMutableArray arrayWithCapacity:8];
106 [self addEMailsOfAddresses:[_envelope to] toArray:to];
107 [self addEMailsOfAddresses:[_envelope cc] toArray:to];
109 [_info setObject:to forKey:@"cc"];
113 - (NSString *)contentForReplyOnParts:(NSDictionary *)_prts keys:(NSArray *)_k {
114 static NSString *textPartSeparator = @"\n---\n";
118 ms = [NSMutableString stringWithCapacity:16000];
120 for (i = 0, count = [_k count]; i < count; i++) {
123 k = [_k objectAtIndex:i];
125 // TODO: this is DUP code to SOGoMailObject
126 if ([k isEqualToString:@"body[text]"])
128 else if ([k hasPrefix:@"body["]) {
129 k = [k substringFromIndex:5];
130 if ([k length] > 0) k = [k substringToIndex:([k length] - 1)];
133 v = [_prts objectForKey:k];
134 if (![v isKindOfClass:[NSString class]]) {
135 [self logWithFormat:@"Note: cannot show part %@", k];
141 if (i != 0) [ms appendString:textPartSeparator];
142 [ms appendString:[v stringByApplyingMailQuoting]];
147 - (NSString *)contentForReply {
151 keys = [[self clientObject] plainTextContentFetchKeys];
152 if ([keys count] == 0)
155 if ([keys count] > 1) {
156 /* filter keys, only include top-level, or if none, the first */
157 NSMutableArray *topLevelKeys = nil;
160 for (i = 0; i < [keys count]; i++) {
163 r = [[keys objectAtIndex:i] rangeOfString:@"."];
167 if (topLevelKeys == nil)
168 topLevelKeys = [NSMutableArray arrayWithCapacity:4];
169 [topLevelKeys addObject:[keys objectAtIndex:i]];
172 if ([topLevelKeys count] > 0) {
173 /* use top-level keys if we have some */
177 /* just take the first part */
178 keys = [NSArray arrayWithObject:[keys objectAtIndex:0]];
182 parts = [[self clientObject] fetchPlainTextStrings:keys];
183 return [self contentForReplyOnParts:parts keys:keys];
186 - (id)replyToAll:(BOOL)_replyToAll {
187 NSMutableDictionary *info;
192 /* ensure mail exists and is filled */
194 // TODO: we could transport the body structure in a hidden field of the mail
195 // viewer to avoid refetching the core-info?
196 tmp = [[self clientObject] fetchCoreInfos];
197 if ([tmp isKindOfClass:[NSException class]])
199 if (![tmp isNotNull])
200 return [self didNotFindMailError];
204 if ((error = [self _setupNewDraft]) != nil)
207 /* fill draft info */
209 info = [NSMutableDictionary dictionaryWithCapacity:16];
211 [info setObject:[self replySubject:[[self clientObject] subject]]
213 [self fillInReplyAddresses:info replyToAll:_replyToAll
214 envelope:[[self clientObject] envelope]];
216 /* fill in text content */
218 if ((tmp = [self contentForReply]) != nil)
219 [info setObject:tmp forKey:@"text"];
221 /* save draft info */
223 if ((error = [self->newDraft storeInfo:info]) != nil)
226 // TODO: we might want to pass the original URL to the editor for a final
227 // redirect back to the message?
228 result = [self redirectToEditNewDraft];
234 return [self replyToAll:NO];
236 - (id)replyallAction {
237 return [self replyToAll:YES];
240 @end /* UIxMailReplyAction */