This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AISFPlugin.m
140 lines (112 loc) · 4.88 KB
/
AISFPlugin.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
/*
* Adium SpamFilter Plugin - Blacklist annoying messages in Adium
* Copyright (C) 2010 Thijs Alkemade
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* 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 GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "AISFPlugin.h"
#import <Adium/AIContentObject.h>
#import <Adium/AIContentMessage.h>
#import <Adium/AIPreferenceControllerProtocol.h>
#import </usr/include/objc/objc-class.h>
@class AIMedia;
#import <AdiumLibpurple/CBPurpleAccount.h>
@implementation AISFPlugin
- (void)installPlugin
{
//preferences = [[AISFPreferences preferencePaneForPlugin:self] retain];
preferences = [[AISFPreferences sharedInstance] retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willReceiveContent:)
name:Content_WillReceiveContent
object:nil];
AILogWithSignature(@"Doing risky stuff: swizzling authorizationRequestWithDict:");
Method orig_method = nil, alt_method = nil;
SEL orig = @selector(authorizationRequestWithDict:);
SEL new = @selector(authorizationRequestWithDict:);
orig_method = class_getInstanceMethod([CBPurpleAccount class], @selector(authorizationRequestWithDict:));
alt_method = class_getInstanceMethod([CBPurpleAccount class], @selector(_authorizationRequestWithDict:));
if(class_addMethod([CBPurpleAccount class], orig, method_getImplementation(alt_method), method_getTypeEncoding(alt_method)))
class_replaceMethod([CBPurpleAccount class], new, method_getImplementation(orig_method), method_getTypeEncoding(orig_method));
else
method_exchangeImplementations(orig_method, alt_method);
AILogWithSignature(@"Adium spamfilter plugin loaded: %@", [preferences view]);
}
- (void)uninstallPlugin
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)willReceiveContent:(NSNotification *)notification
{
AIContentObject *contentObject = [[notification userInfo] objectForKey:@"Object"];
if (![contentObject isKindOfClass:[AIContentMessage class]] ||
!contentObject.source) {
return;
}
BOOL hidden = NO;
NSArray *blacklist = [adium.preferenceController preferenceForKey:KEY_SF_FILTERS
group:PREF_GROUP_SPAMFILTER];
for (NSDictionary *message in blacklist) {
if ([[message valueForKey:KEY_SF_REGEX] boolValue]) {
NSPredicate *regex;
if ([[message valueForKey:KEY_SF_CASE_SENSITIVE] boolValue]) {
regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", [message valueForKey:KEY_SF_PHRASE]];
} else {
regex = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", [message valueForKey:KEY_SF_PHRASE]];
}
@try {
if ([regex evaluateWithObject:contentObject.message.string]) {
hidden = YES;
AILogWithSignature(@"Hiding %@ as it matches regex %@", contentObject, message);
break;
}
}
@catch (NSException *e) {
AILog(@"Regex %@ seems to have failed: %@", message, e);
// show the error after a delay, so the incoming message doesn't have to wait
[self performSelector:@selector(error:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:message, @"Message", e, @"Exception", nil] afterDelay:0.1];
}
} else if ([contentObject.message.string rangeOfString:[message valueForKey:KEY_SF_PHRASE]
options:([[message valueForKey:KEY_SF_CASE_SENSITIVE] boolValue] ? 0 : NSCaseInsensitiveSearch)].location != NSNotFound) {
hidden = YES;
AILogWithSignature(@"Hiding %@ as it matches %@", contentObject, message);
break;
}
}
if (hidden) {
contentObject.displayContent = NO;
}
}
- (void)error:(NSDictionary *)context
{
NSInteger result = NSRunAlertPanel([NSString stringWithFormat:@"Evaluation of regular expression \"%@\" failed.", [[context valueForKey:@"Message"] valueForKey:KEY_SF_PHRASE]],
[NSString stringWithFormat:@"Adium SpamFilter plugin encountered an error when evaluating this regular expression:\n\n%@", [context valueForKey:@"Exception"]],
@"OK",
@"Edit expression",
nil);
if (result == NSAlertAlternateReturn) {
[preferences editObject:[context valueForKey:@"Message"]];
}
}
- (NSString *)pluginAuthor
{
return @"Thijs Alkemade <thijsalkemade@gmail.com>";
}
- (NSString *)pluginVersion
{
return @"0.1.1";
}
- (NSString *)pluginDescription
{
return @"Allows you to specify filters on incoming messages.";
}
@end