forked from saptarshiguha/ReplyWithHeaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplyWithHeader.m
165 lines (123 loc) · 5.7 KB
/
ReplyWithHeader.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
// 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>.
#import "ReplyWithHeader.h"
@implementation ReplyWithHeader
#pragma mark Class initialization
+ (void)initialize {
RWH_LOG();
//class_setSuperclass - Deprecated, but there does not appear to be a better way for this...
if (self == [ReplyWithHeader class]) {
class_setSuperclass(self, NSClassFromString(@"MVMailBundle"));
}
[super registerBundle];
//add the ReplyWithHeaderMessage methods to the ComposeBackEnd class
[ReplyWithHeaderMessage rwhAddMethodsToClass:NSClassFromString(@"ComposeBackEnd")];
//now switch the _continueToSetupContentsForView method in the ComposeBackEnd implementation so that the
// newly added rph_continueToSetupContentsForView method is called instead...
[NSClassFromString(@"ComposeBackEnd")
rwhSwizzle:@selector(_continueToSetupContentsForView:withParsedMessages:)
meth:@selector(rph_continueToSetupContentsForView:withParsedMessages:)
classMeth:NO // it is an implementation method
];
[ReplyWithHeaderPreferences rwhAddMethodsToClass:NSClassFromString(@"NSPreferences")];
[NSClassFromString(@"NSPreferences")
rwhSwizzle:@selector(sharedPreferences)
meth:@selector(rwhSharedPreferences)
classMeth:YES
];
//enableBundle
//headerText
//entourage2004Support
NSUserDefaults *prefs = [[NSUserDefaults standardUserDefaults] retain];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], @"enableBundle",
@"-----Original Message-----", @"headerText",
[NSNumber numberWithBool:NO], @"entourage2004Support",
[NSNumber numberWithBool:NO], @"replaceForward",
@"-----Forwarded Message-----<br />", @"forwardHeader",
nil];
[prefs registerDefaults:dict];
//The modules have been loaded
NSLog(@"Loaded ReplyWithHeader %@",[[NSBundle bundleForClass:[ReplyWithHeader class]] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]);
NSLog(@"ReplyWithHeaders: Oh its a wonderful life");
}
#pragma mark MVMailBundle class methods
+ (BOOL)hasPreferencesPanel {
RWH_LOG();
return YES;
}
+ (NSString*)preferencesOwnerClassName {
RWH_LOG();
return @"ReplyWithHeaderPreferencesModule";
}
+ (NSString*)preferencesPanelName {
RWH_LOG();
return @"RWH";
}
@end
@implementation NSObject (ReplyWithHeaderObject)
#pragma mark Class methods
+ (void)rwhAddMethodsToClass:(Class)cls
{
RWH_LOG(@"%@", cls);
unsigned int numMeths = 0;
Method* meths = class_copyMethodList(object_getClass([self class]), &numMeths);
Class c = object_getClass(cls);
//add the methods
[self rwhAddMethods:meths numMethods:numMeths toClass:&c origClass:&cls];
//clean up the memory
if (meths != nil)
{ free(meths); }
//keep doing it until they are the same class
while(c != cls)
{
c = cls;
meths = class_copyMethodList([self class], &numMeths);
[self rwhAddMethods:meths numMethods:numMeths toClass:&c origClass:&cls];
}
}
+ (void)rwhAddMethods:(Method *)m numMethods:(unsigned int)cnt toClass:(Class *)c origClass:(Class *) cls
{
unsigned int i = 0;
//add the method from the current class (self) to the class identified
for (i = 0; i < cnt; i++)
{
//add the methods to Class c class
if(!class_addMethod(*c,method_getName(m[i]),method_getImplementation(m[i]),method_getTypeEncoding(m[i]) ))
{ RWH_LOG(@"rwhAddMethods: could not add %s to %@",sel_getName(method_getName(m[i])),*cls); }
else
{ RWH_LOG(@"rwhAddMethods: added %s to %@",sel_getName(method_getName(m[i])),*cls); }
}
}
+ (void)rwhSwizzle:(SEL)origSel meth:(SEL)newSel classMeth:(BOOL)cls
{
//get the original method and the new method... need to test if it is a class or instance method to determine
// the function to call to get the method
Method origMeth = (cls?class_getClassMethod([self class], origSel):class_getInstanceMethod([self class], origSel));
Method newMeth = (cls?class_getClassMethod([self class], newSel):class_getInstanceMethod([self class], newSel));
//log the swizzle for debugging...
RWH_LOG(@"%s (%p), %s (%p), %s",sel_getName(origSel), method_getImplementation(origMeth),
sel_getName(newSel), method_getImplementation(newMeth),(cls ? "YES" : "NO"));
//this is how we swizzle
method_exchangeImplementations(origMeth, newMeth);
}
@end