forked from saptarshiguha/ReplyWithHeaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailQuotedOriginal.m
434 lines (363 loc) · 17.3 KB
/
MailQuotedOriginal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// replyWitHeaders MailBundle - compose reply with message headers as in forwards
// Copyright (c) 2013 Saptarshi Guha and Jason Schroth
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// This program 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
// MIT License for more details.
//
// You should have received a copy of the MIT License along with this
// program. If not, see <http://opensource.org/licenses/MIT>.
// MailQuotedOriginal.m
// ReplyWithHeader
//
// Created by Jason Schroth on 8/16/12.
//
//
#import "MailQuotedOriginal.h"
@implementation MailQuotedOriginal
-(id)init
{
if (self = [super init]) {
//good stuff...
}
else {
RWH_LOG(@"MailQuotedOriginal: Init failed");
}
return self;
}
-(id)initWithBackEnd:(id)backend
{
//initialze the value with a mutable copy of the attributed string
if( self = [super init] )
{
//set the class document variable
document = [backend document];
RWH_LOG(@"Document=%@",document);
//now initialize the other vars
[self initVars];
//if there is not a child in the original email, it must be plain text
if([origemail firstChild]==NULL)
{
//prep the plain text
[self prepQuotedPlainText];
}
//now that the email is set... set the child nodes
dhc = [origemail childNodes];
//now get the quoted content and remove the first part (where it says "On ... X wrote"
if( dhc.length > 1)
{
if( isPlainText )
{
[self removeOriginalPlainTextHeader];
}
else
{
[self removeOriginalHeader];
}
}
}
else {
RWH_LOG(@"MailQuotedOriginal: Init Backend failed");
}
return self;
}
-(void)initVars
{
NSUserDefaults *prefs = [[NSUserDefaults standardUserDefaults] retain];
NSString *headLine = [prefs objectForKey:@"headerText"];
NSString *fwdLine = [prefs objectForKey:@"forwardHeader"];
RWH_LOG(@"MailQuotedOriginal: initvar Header text: %@",headLine);
//now set the border variable
border = [[document htmlDocument] createDocumentFragmentWithMarkupString: headLine];
// @"<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:0 0 0 0;margin:10px 0 5px 0;'></div>"
fwdborder = [[document htmlDocument] createDocumentFragmentWithMarkupString: fwdLine];
boldhead=YES;
// DOMNode *voo = [document htmlDocument];
// DOMNodeList *vl = [[[[[voo childNodes] item:0] childNodes] item:0] childNodes];
origemail=[[[document htmlDocument]
descendantsWithClassName:@"AppleOriginalContents"] objectAtIndex:0];
//howdeep = 0; //AppleOriginalContents=0 ApplePlainTextBody=1
isPlainText = NO; //AppleOriginalContents: (isPlainText=NO) | ApplePlainTextBody: (isPlainText=YES)
textNodeLocation = 0;
}
-(void)prepQuotedPlainText
{
origemail=[[[document htmlDocument] descendantsWithClassName:@"ApplePlainTextBody"] objectAtIndex:0];
RWH_LOG(@"Orig is now %@", origemail);
isPlainText = YES;
//DOMNodeList *vl = [origemail childNodes];
//for(int i=0;i< vl.length;i++)
//{
// id ii=[ vl item:i];
// RWH_LOG(@"%d(%d,%@,%@,%@,%@,%@)=",i,[ii nodeType],[ii nodeName],[ii attributes],[ii prefix],[ii namespaceURI],[ii localName]);
// if([ii nodeType] != 3) RWH_LOG(@"Origemail child (%@) = %@",[vl item:i],[[vl item:i] outerHTML]);
// else RWH_LOG(@"ND(%d)=%@",i,[ [vl item:i] data]);
//}
if( [[origemail idName] isEqualToString:@"AppleMailSignature"] )
{
int itemnum = 2;
//check that the second child isn't a break element and if so, go to the child 3
if( [[[[origemail children] item:itemnum] outerHTML] isEqualToString:@"<br>"] )
{
itemnum = 3;
}
origemail = [[origemail children] item:itemnum];
}
//this is plain text so do not bold the header...
boldhead=NO;
}
-(void)removeOriginalHeader
{
//NSUserDefaults *nsd= [NSUserDefaults standardUserDefaults];
//BOOL signatureattop = [nsd boolForKey:@"SignaturePlacedAboveQuotedText"];
// RWH_LOG(@"isPlainText = %d", isPlainText);
// for(int i=0; i< dhc.length;i++){
// RWH_LOG(@"%d=(Type %d) %@\n%@\n",i, [[dhc item:i] nodeType], [dhc item:i], [[dhc item:i] nodeName]);
// }
//Mountain Lion created the issue on new messages and "wrote" appears in a new div when replying
// on those messages that arrive after mail.app is opened - so we'll just keep removing items
// from the beginnning until we find the element that has the "wrote:" text in it.
//unfortunately, there is no containsString routine so we have to do it by using a range.
// this method is documented at http://mobiledevelopertips.com/cocoa/nsrange-and-nsstring-objects.html
//setup a regular expression to find a colon followed by some space and a new line -
// the first one should be the original line...
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@":\\s*(\\n|\\r)" options: NSRegularExpressionCaseInsensitive error:&error];
NSRange textRange = [regex rangeOfFirstMatchInString:[[origemail firstChild] stringValue] options:0 range:NSMakeRange(0, [[[origemail firstChild] stringValue] length])];
//keep removing items until we find the "wrote:" text...
while( textRange.location == NSNotFound )
{
RWH_LOG(@"Range is: %@", NSStringFromRange(textRange));
RWH_LOG(@"Length=%ld Text=%@",[[[origemail firstChild] stringValue] length],[[origemail firstChild] stringValue]);
[origemail removeChild:[dhc item:0]];
textRange = [regex rangeOfFirstMatchInString:[[origemail firstChild] stringValue] options:0 range:NSMakeRange(0, [[[origemail firstChild] stringValue] length])];
}
//remove the line with the "wrote:" text
[origemail removeChild:[dhc item:0]];
//remove the first new line element to shorten the distance between the new email and quoted text
// this is required in order to get the header inside the quoted text line
if( [[[origemail firstChild] nodeName] isEqualToString:@"BR"] )
{
[origemail removeChild:[origemail firstChild]];
RWH_LOG(@"Removed BR element, only %d children left",[origemail childElementCount]);
}
}
-(void)removeOriginalPlainTextHeader
{
// is this signature?
// RWH_LOG(@"Sig=%@ %d<%@>",[dhc item:0],[[dhc item:0] nodeType],[[dhc item:0] stringValue]);
// RWH_LOG(@"Sig=%@ %d<%@>",[dhc item:1],[[dhc item:1] nodeType],[[dhc item:1] stringValue]);
// RWH_LOG(@"Sig=%@ %d<%@>",[dhc item:2],[[dhc item:2] nodeType],[[dhc item:2] stringValue]);
// RWH_LOG(@"Sig=%@ %d<%@>",[dhc item:3],[[dhc item:3] nodeType],[[dhc item:3] stringValue]);
// RWH_LOG(@"Sig=%@ %d<%@>",[dhc item:4],[[dhc item:4] nodeType],[[dhc item:4] stringValue]);
//
// RWH_LOG(@"===END===");
for(int i =0;i < dhc.length;i++) {
if ([[dhc item:i] nodeType]==3){
// Text node, On ..., Wrote is text
textNodeLocation=i; break;
}}
RWH_LOG(@"Removing plain text header at %d",textNodeLocation);
// if signature at top, item==3 else item==1
[origemail removeChild:[dhc item:textNodeLocation]];
// RWH_LOG(@"removed item %d",textNodeLocation);
//find the quoted text - if plain text (blockquote does not exist), -which- will point to br element
for(int i =0;i < [origemail childElementCount];i++)
{
if( [[[[origemail childNodes] item:i] nodeName] isEqualToString:@"BLOCKQUOTE"] )
{
//this is the quoted text
textNodeLocation=i;
// RWH_LOG(@"textNodeLocation item is now %d",textNodeLocation);
break;
}
}
RWH_LOG(@"New header location is %d",textNodeLocation);
}
-(void)removeOriginalForwardHeader
{
//remove the elements with text followed by a colon
//setup a regular expression to find a colon followed by some space and a new line -
// the first one should be the original line...
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[^:]+:" options: NSRegularExpressionCaseInsensitive error:&error];
NSRange textRange = [regex rangeOfFirstMatchInString:[[origemail firstChild] stringValue] options:0 range:NSMakeRange(0, [[[origemail firstChild] stringValue] length])];
//keep removing items until we find the "wrote:" text...
while( textRange.location == NSNotFound )
{
RWH_LOG(@"Range is: %@", NSStringFromRange(textRange));
RWH_LOG(@"Length=%ld Text=%@",[[[origemail firstChild] stringValue] length],[[origemail firstChild] stringValue]);
[origemail removeChild:[dhc item:0]];
textRange = [regex rangeOfFirstMatchInString:[[origemail firstChild] stringValue] options:0 range:NSMakeRange(0, [[[origemail firstChild] stringValue] length])];
}
//remove the line with the "wrote:" text
[origemail removeChild:[dhc item:0]];
//remove the first new line element to shorten the distance between the new email and quoted text
// this is required in order to get the header inside the quoted text line
if( [[[origemail firstChild] nodeName] isEqualToString:@"BR"] )
{
[origemail removeChild:[origemail firstChild]];
RWH_LOG(@"Removed BR element, only %d children left",[origemail childElementCount]);
}
}
-(void)insertMailHeader:(MailHeaderString *)headStr
{
//this routine will also add the border
DOMDocumentFragment *headerfragment=[[document htmlDocument] createFragmentForWebArchive:[headStr getWebArch]];
//check if we need to do Entourage 2004 text size transformations...
NSUserDefaults *prefs = [[NSUserDefaults standardUserDefaults] retain];
BOOL supportEntourage = [prefs boolForKey:@"entourage2004Support"];
if( supportEntourage )
{
[self supportEntourage2004:headerfragment];
}
if( isPlainText )
{
if( textNodeLocation>0 )
{
//check if this is plain text by seeing if textNodeLocation points to a br element...
// if not, include in blockquote
if( [[[[origemail childNodes] item:textNodeLocation] nodeName] isEqualToString:@"BR"] )
{
[origemail insertBefore:headerfragment refChild:[dhc item:textNodeLocation] ];
[origemail insertBefore:border refChild:[dhc item:textNodeLocation] ];
}
else
{
[[[origemail childNodes] item:textNodeLocation] insertBefore:headerfragment refChild:[[[origemail childNodes] item:textNodeLocation] firstChild] ];
[[[origemail childNodes] item:textNodeLocation] insertBefore:border refChild:[[[origemail childNodes] item:textNodeLocation] firstChild] ];
}
}
}
else
{
//depending on the options selected to increase quote level or whatever, a reply might not have a grandchild from the first child
//so we need to account for that... man this gets complicated... so if it is a textnode, there are no children... :(
//so account for that too
int numgrandchild = 0;
if( ![ [[origemail firstChild] nodeName] isEqualToString:@"#text"] )
{
numgrandchild = [[origemail firstChild] childElementCount];
}
RWH_LOG(@"numgrandchildren %d=(Type %d) %@\n%@\n",numgrandchild, [[origemail firstChild] nodeType], [origemail firstChild], [[origemail firstChild] nodeName]);
if( numgrandchild == 0 )
{
[origemail insertBefore:headerfragment refChild: [origemail firstChild] ];
[origemail insertBefore:border refChild: [origemail firstChild] ];
}
else
{
[[origemail firstChild] insertBefore:headerfragment refChild: [[origemail firstChild] firstChild] ];
[[origemail firstChild] insertBefore:border refChild: [[origemail firstChild] firstChild]];
}
}
}
-(void)insertFwdHeader
{
if( isPlainText )
{
if( textNodeLocation>0 )
{
//check if this is plain text by seeing if textNodeLocation points to a br element...
// if not, include in blockquote
if( [[[[origemail childNodes] item:textNodeLocation] nodeName] isEqualToString:@"BR"] )
{
[origemail insertBefore:fwdborder refChild:[dhc item:textNodeLocation] ];
}
else
{
[[[origemail childNodes] item:textNodeLocation] insertBefore:fwdborder refChild:[[[origemail childNodes] item:textNodeLocation] firstChild] ];
}
}
}
else
{
//depending on the options selected to increase quote level or whatever, a reply might not have a grandchild from the first child
//so we need to account for that... man this gets complicated... so if it is a textnode, there are no children... :(
//so account for that too
int numgrandchild = 0;
if( ![ [[origemail firstChild] nodeName] isEqualToString:@"#text"] )
{
numgrandchild = [[origemail firstChild] childElementCount];
}
RWH_LOG(@"numgrandchildren %d=(Type %d) %@\n%@\n",numgrandchild, [[origemail firstChild] nodeType], [origemail firstChild], [[origemail firstChild] nodeName]);
if( numgrandchild == 0 )
{
[origemail insertBefore:fwdborder refChild: [origemail firstChild] ];
}
else
{
[[origemail firstChild] insertBefore:fwdborder refChild: [[origemail firstChild] firstChild]];
}
}
}
-(void)supportEntourage2004:(DOMDocumentFragment *) headFrag
{
//kind of silly, but this code is required so that the adulation appears correctly in Entourage 2004
//2004 would interpret the paragraph tag and ignore the specified style information creating large spaces
//between line items
DOMNodeList *fragnodes = [[headFrag firstChild] childNodes];
for(int i=0; i< fragnodes.length;i++)
{
RWH_LOG(@"%d=(Type %d) %@ %@ %@",i, [[fragnodes item:i] nodeType], [fragnodes item:i], [[fragnodes item:i] nodeName],[[fragnodes item:i] nodeValue]);
if( [[fragnodes item:i] nodeType] == 1 )
{
RWH_LOG(@" HTML = %@",[[fragnodes item:i] outerHTML]);
if( [[[fragnodes item:i] nodeName] isEqualToString:@"FONT"] )
{
NSString *fontTag = [[fragnodes item:i] outerHTML];
NSArray *tagComponents = [fontTag componentsSeparatedByString:@" "];
NSString *oldSize;
for( int j=0; j < tagComponents.count; j++) {
NSString *testString = [[tagComponents objectAtIndex:j] commonPrefixWithString:@"size" options:NSCaseInsensitiveSearch];
if( [testString isEqualToString:@"size"] ) {
oldSize = [tagComponents objectAtIndex:j];
RWH_LOG(@" sizeString = %@",oldSize);
}
}
oldSize = [@" " stringByAppendingString:oldSize];
RWH_LOG(@" newsizetext = %@",fontTag);
NSString *newTag = [fontTag stringByReplacingOccurrencesOfString:oldSize withString:@""];
RWH_LOG(@" newString = %@",newTag);
[[fragnodes item:i] setOuterHTML:newTag];
}
}
if( [[[fragnodes item:i] nodeName] isEqualToString:@"P"] )
{
//we have a paragraph element, so now replace it with a break element
DOMDocumentFragment *brelem=[ [document htmlDocument]
createDocumentFragmentWithMarkupString:
@"<br />"
];
if( i == 0)
{
//because the paragraphs are the containers so you get two initially...
brelem = [ [document htmlDocument]
createDocumentFragmentWithMarkupString:
@"<span />"
];
}
DOMNodeList *pnodes = [[fragnodes item:i] childNodes];
for(int j=0; j< pnodes.length;j++)
{
//copy all child nodes to the new node...
[brelem appendChild:[pnodes item:j]];
}
//now replace the paragraph node...
[[headFrag firstChild] replaceChild:brelem oldChild:[fragnodes item:i] ];
}
}
}
@end