-
Notifications
You must be signed in to change notification settings - Fork 3
/
ComposeBackEnd2.m
257 lines (212 loc) · 10.3 KB
/
ComposeBackEnd2.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
// 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.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "ComposeBackEnd2.h"
#import "WebKit/DOMDocumentFragment.h"
#import "WebKit/DOMNodeList.h"
#import "WebKit/DOMHTMLCollection.h"
#import "WebKit/DOMHTMLElement.h"
#import "WebKit/DOMHTMLDocument.h"
#import "WebKit/DOMHTMLDivElement.h"
#import "WebKit/WebResource.h"
#import "WebKit/WebArchive.h"
#import <objc/objc.h>
#import <objc/objc-runtime.h>
#import <objc/objc-class.h>
IMP rph_imp1,rph_sih_imp;
SEL rph_sel1,rph_sih_sel;
Class ComposeBackEndClass ;
char *rph_ms = "_continueToSetupContentsForView:withParsedMessages:";
char *rph_sih = "setOriginalMessageWebArchive:";
@implementation ComposeBackEnd2
+(void)load{
ComposeBackEndClass = NSClassFromString(@"ComposeBackEnd");
if(!ComposeBackEndClass){
NSLog(@"ReplyWithHeader: Could not find ComposeBackEnd, not good");
return;
}
class_setSuperclass([self class], ComposeBackEndClass);
/*Class MessageHeadersClass = NSClassFromString(@"MessageHeaders");
unsigned int outCount;
Method *array = class_copyMethodList(MessageHeadersClass, &outCount);
NSLog(@"Count=%d",outCount);
for( int i = 0; i < outCount; i++ )
{
//char *type = method_copyReturnType(array[i]);
NSLog(@"Meth=%s returns=%s",sel_getName(method_getName(array[i])),method_copyReturnType(array[i]));
}
*/
Method oldm,newm;
oldm =
class_getInstanceMethod(self, sel_registerName(rph_ms)); //Called 1st
newm =
class_getInstanceMethod(self, @selector(rph_continueToSetupContentsForView:withParsedMessages:));
rph_sel1 = sel_registerName(rph_ms);
rph_imp1 = class_getMethodImplementation(ComposeBackEndClass, rph_sel1);
method_exchangeImplementations(oldm, newm);
// oldm =
// class_getInstanceMethod(self, sel_registerName(rph_sih)); //Called 1st
// newm =
// class_getInstanceMethod(self, @selector(rph_setOriginalMessageWebArchive:));
//
// rph_sih_sel = sel_registerName(rph_ms);
// rph_sih_imp = class_getMethodImplementation(ComposeBackEndClass, rph_sel1);
//
// method_exchangeImplementations(oldm, newm);
}
-(void)domystuff{
return;
}
//- (void)rph_setOriginalMessageWebArchive:(id)fp8{
// NSLog(@"setOriginalMessageWebArchive %@",fp8);
//}
- (void)rph_continueToSetupContentsForView:(id)arg1 withParsedMessages:(id)arg2
{
// NSLog(@"Inside my_continueToSetupContentsForView=%@",self);
// NSLog(@"%@",arg1);
// NSLog(@"%@",arg2);
id beforewhat;
(rph_imp1)(self,rph_sel1,arg1,arg2);
int selftype=[self type];
int which =0;
if( selftype==1 || selftype ==2) {
id document = [self document];
// NSLog(@"Document=%@",document);
DOMDocumentFragment *border=[ [document htmlDocument]
createDocumentFragmentWithMarkupString:
@"-----Original Message-----"
// @"<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:0 0 0 0;margin:10px 0 5px 0;'></div>"
];
BOOL boldhead=YES;
// DOMNode *voo = [document htmlDocument];
// DOMNodeList *vl = [[[[[voo childNodes] item:0] childNodes] item:0] childNodes];
DOMHTMLDivElement *origemail=[[[document htmlDocument]
descendantsWithClassName:@"AppleOriginalContents"] objectAtIndex:0];
int howdeep = 0; //AppleOriginalContents=0 ApplePlainTextBody=1
if([origemail firstChild]==NULL){
origemail=[[[document htmlDocument] descendantsWithClassName:@"ApplePlainTextBody"] objectAtIndex:0];
// NSLog(@"Orig is now %@", origemail);
howdeep=1;
// DOMNodeList *vl = [origemail childNodes];
// for(int i=0;i< vl.length;i++){
// id ii=[ vl item:i];
// NSLog(@"%d(%d,%@,%@,%@,%@,%@)=",i,[ii nodeType],[ii nodeName],[ii attributes],[ii prefix],[ii namespaceURI],[ii localName]);
// if([ii nodeType] != 3) NSLog(@"Origemail child (%@) = %@",[vl item:i],[[vl item:i] outerHTML]);
// else NSLog(@"ND(%d)=%@",i,[ [vl item:i] data]);
// }
// children] item:1];
if ([[ origemail idName] isEqualToString:@"AppleMailSignature"]){
origemail=[[[[[document htmlDocument] descendantsWithClassName:@"ApplePlainTextBody"] objectAtIndex:0]
children] item:2];
if ([[origemail outerHTML] isEqualToString:@"<br>"])
origemail=[[[[[document htmlDocument] descendantsWithClassName:@"ApplePlainTextBody"] objectAtIndex:0]
children] item:3];
}
boldhead=NO;
}
NSAttributedString *headerString =[[self originalMessageHeaders]
attributedStringShowingHeaderDetailLevel:1];
// useHeadIndents:NO
// useBold:boldhead
// includeBCC:YES];
DOMNodeList *dhc = [origemail childNodes];
//NSUserDefaults *nsd= [NSUserDefaults standardUserDefaults];
//BOOL signatureattop = [nsd boolForKey:@"SignaturePlacedAboveQuotedText"];
// NSLog(@"howdeep = %d", howdeep);
// for(int i=0; i< dhc.length;i++){
// NSLog(@"%d=(Type %d) %@\n%@\n",i, [[dhc item:i] nodeType], [dhc item:i], [[dhc item:i] nodeName]);
// }
// the first one is "On .... X wrote"
if(dhc.length>1 && howdeep==0) {
[origemail removeChild:[dhc item:0]];
// NSLog(@"Removed Original Text, only %d children left",[origemail childElementCount]);
if( [[[origemail firstChild] nodeName] isEqualToString:@"BR"] ) {
[origemail removeChild:[origemail firstChild]];
// NSLog(@"Removed BR element, only %d children left",[origemail childElementCount]);
}
}
if(dhc.length>1 && howdeep==1) {
// is this signature?
// NSLog(@"Sig=%@ %d<%@>",[dhc item:0],[[dhc item:0] nodeType],[[dhc item:0] stringValue]);
// NSLog(@"Sig=%@ %d<%@>",[dhc item:1],[[dhc item:1] nodeType],[[dhc item:1] stringValue]);
// NSLog(@"Sig=%@ %d<%@>",[dhc item:2],[[dhc item:2] nodeType],[[dhc item:2] stringValue]);
// NSLog(@"Sig=%@ %d<%@>",[dhc item:3],[[dhc item:3] nodeType],[[dhc item:3] stringValue]);
// NSLog(@"Sig=%@ %d<%@>",[dhc item:4],[[dhc item:4] nodeType],[[dhc item:4] stringValue]);
//
// NSLog(@"===END===");
for(int i =0;i < dhc.length;i++) {
if ([[dhc item:i] nodeType]==3){
// Text node, On ..., Wrote is text
which=i; break;
}}
// if signature at top, item==3 else item==1
[origemail removeChild:[dhc item:which]];
// NSLog(@"removed item %d",which);
//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
which=i;
// NSLog(@"which item is now %d",which);
break;
}
}
}
//remove the color attribute so that the text is black instead of gray
//also remove paragraph style included in the header to avoid spacing issues when received by some mail clients
NSMutableAttributedString *newheaderString = [headerString mutableCopy];
[newheaderString removeAttribute:@"NSColor" range:NSMakeRange(0,[newheaderString length])];
[newheaderString removeAttribute:@"NSParagraphStyle" range:NSMakeRange(0,[newheaderString length])];
//NSLog(@"Sig=%@",newheaderString);
WebArchive * headerwebarchive=[newheaderString webArchiveForRange:NSMakeRange(0,[newheaderString length]) fixUpNewlines:YES];
DOMDocumentFragment *headerfragment=[ [document htmlDocument] createFragmentForWebArchive:headerwebarchive];
if(howdeep==0){
//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];
}
// NSLog(@"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]];
}
}else if(howdeep==1){
if(which>0){
//check if this is plain text by seeing if -which- points to a br element... if not, include in blockquote
if( [[[[origemail childNodes] item:which] nodeName] isEqualToString:@"BR"] ) {
[origemail insertBefore:headerfragment refChild:[dhc item:which] ];
[origemail insertBefore:border refChild:[dhc item:which] ];
}
else {
[[[origemail childNodes] item:which] insertBefore:headerfragment refChild:[[[origemail childNodes] item:which] firstChild] ];
[[[origemail childNodes] item:which] insertBefore:border refChild:[[[origemail childNodes] item:which] firstChild] ];
}
}
}
}
}
@end