-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTweak.xm
343 lines (266 loc) · 11.5 KB
/
Tweak.xm
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
#import "notiblockpref/NBPNotificationFilter.h"
#import <Cephei/HBPreferences.h>
@interface BBSound
@end
@interface BBBulletinRequest
@property (nonatomic,copy) NSString * title;
@property (nonatomic,copy) NSString * subtitle;
@property (nonatomic,copy) NSString * message;
@property (nonatomic,copy) NSString * bulletinID;
@property (nonatomic,copy) NSString * sectionID;
@property (nonatomic,retain) BBSound * sound;
@property (assign,nonatomic) BOOL turnsOnDisplay;
@end
@interface BBBulletin : BBBulletinRequest
@end
@interface BBServer
-(void)publishBulletin:(id)arg1 destinations:(unsigned long long)arg2;
-(void)_clearBulletinIDs:(id)arg1 forSectionID:(id)arg2 shouldSync:(BOOL)arg3;
@end
@interface NCNotificationViewController: UIViewController
@end
@interface NCNotificationShortLookViewController: NCNotificationViewController
-(id)_initWithNotificationRequest:(id)arg1 revealingAdditionalContentOnPresentation:(BOOL)arg2 ;
@end
@interface NCNotificationRequest
@property (nonatomic,readonly) BBBulletin * bulletin;
@end
@interface NotiBlockChecker : NSObject
+ (int)blockTypeForBulletin:(BBBulletin *)bulletin;
+ (BOOL)areWeCurrentlyInSchedule:(NSDate *)startTime arg2:(NSDate *)endTime arg3:(NSArray *)weekdays;
+ (BOOL)doesMessageMatchFilterType:(BOOL)titleMatches arg2:(BOOL)subtitleMatches arg3:(BOOL)messageMatches arg4:(int)filterType;
@end
NSMutableDictionary *filters;
@implementation NotiBlockChecker
/**
returns whether a message should be filtered based on some boolean logic of filters
**/
+ (BOOL)doesMessageMatchFilterType:(BOOL)titleMatches arg2:(BOOL)subtitleMatches arg3:(BOOL)messageMatches arg4:(int)filterType {
NSLog(@"NOTIBLOCK - checking matched: title: %@, subtitle: %@, message: %@, filterType: %d",(titleMatches ? @"true" : @"false"), (subtitleMatches ? @"true" : @"false"), (messageMatches ? @"true" : @"false"), filterType);
if (filterType == 0) {
return titleMatches || subtitleMatches || messageMatches;
} else if (filterType == 1) {
return titleMatches;
} else if (filterType == 2) {
return subtitleMatches;
} else if (filterType == 3) {
return messageMatches;
}
return NO;
}
/**
returns whether we are currently inbetween the start time and and time and on a weekday set to true in an array of bools
**/
+(BOOL)areWeCurrentlyInSchedule:(NSDate *)startTime arg2:(NSDate *)endTime arg3:(NSArray *)weekdays {
NSLog(@"NOTIBLOCK - checking schedule");
NSDate *curTime = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *startComponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:startTime];
NSCalendar *newStartCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *newStartTime = [newStartCalendar dateBySettingHour:[startComponents hour] minute:[startComponents minute] second:0 ofDate:curTime options:0];
NSDateComponents *endComponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:endTime];
NSCalendar *newEndCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *newEndTime = [newEndCalendar dateBySettingHour:[endComponents hour] minute:[endComponents minute] second:0 ofDate:curTime options:0];
NSInteger curWeekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday
fromDate:curTime] - 1;
//day of week is not selected
if (![((NSNumber *)weekdays[curWeekday]) boolValue]) {
NSLog(@"NOTIBLOCK - day is not active. skipping filter");
return false;
}
if ([[newStartTime earlierDate:newEndTime] isEqualToDate:newEndTime]) { //backwards (start time before end time)
NSLog(@"NOTIBLOCK - backwards schedule mode");
return [[newStartTime earlierDate:curTime] isEqualToDate:newStartTime] || [[newEndTime earlierDate:curTime] isEqualToDate:curTime];
} else { //regular, check for in between
NSLog(@"NOTIBLOCK - regular schedule mode");
NSLog(@"NOTIBLOCK - curTime: %f", [curTime timeIntervalSince1970]);
NSLog(@"NOTIBLOCK - newStartTime: %f", [newStartTime timeIntervalSince1970]);
NSLog(@"NOTIBLOCK - newEndTime: %f", [newEndTime timeIntervalSince1970]);
NSLog(@"NOTIBLOCK - is after start - %@",([[newStartTime earlierDate:curTime] isEqualToDate:newStartTime] ? @"true" : @"false"));
NSLog(@"NOTIBLOCK - is before end - %@",([[newEndTime earlierDate:curTime] isEqualToDate:curTime] ? @"true" : @"false"));
return [[newEndTime earlierDate:curTime] isEqualToDate:curTime] && [[newStartTime earlierDate:curTime] isEqualToDate:newStartTime];
}
}
/**
0 - do not block
1 - soft block, show in lock screen
2 - hard block, clear from notifications
**/
+ (int)blockTypeForBulletin:(BBBulletin *)bulletin {
NSString *title = [bulletin.title lowercaseString];
NSString *subtitle = [bulletin.subtitle lowercaseString];
NSString *message = [bulletin.message lowercaseString];
//NSString *bulletinID = bulletin.bulletinID;
NSString *sectionId = bulletin.sectionID;
// NSLog(@"NOTIBLOCK - Entered publish bulletin for %@ with ID: %@ ", sectionId, bulletinID);
//NSLog(@"NOTIBLOCK - BulletinID:%@ Title: %@ Subtitle: %@ Message: %@", bulletinID, title, subtitle, message );
BOOL filtered = NO;
if (filters == nil) {
NSLog(@"NOTIBLOCK - No filters. returning");
return 0;
}
NSLog(@"NOTIBLOCK - loading all filters: %lu", (unsigned long)[filters count]);
NSMutableArray *allFilters = [filters objectForKey:@""];
NSMutableArray *appFilters = [filters objectForKey:sectionId];
if (allFilters == nil) {
allFilters = [[NSMutableArray alloc] init];
}
if (appFilters != nil) {
allFilters = [[allFilters arrayByAddingObjectsFromArray:appFilters] mutableCopy];
}
NSLog(@"NOTIBLOCK - loading relevant filters for --%@--: %lu",sectionId, (unsigned long)[allFilters count]);
if (title == nil) {
title = @"";
}
if (subtitle == nil) {
subtitle = @"";
}
if (message == nil) {
message = @"";
}
BOOL showInNotificationCenter = false;
for (NotificationFilter *filter in allFilters) {
//check for schedule and skip if not inside
if (filter.onSchedule && ![self areWeCurrentlyInSchedule:filter.startTime arg2:filter.endTime arg3:filter.weekDays]) {
//not inside schedule, skip
NSLog(@"NOTIBLOCK - schedule was on, but we determined it is not currently active. skipping filter");
continue;
}
NSString *filterText = [filter.filterText lowercaseString];
BOOL titleMatches = false;
BOOL subtitleMatches = false;
BOOL messageMatches = false;
//do filtering
if (filter.blockType == 0) { //starts with
NSLog(@"NOTIBLOCK - checking if string starts with text");
titleMatches = [title hasPrefix:filterText];
subtitleMatches = [subtitle hasPrefix:filterText];
messageMatches = [message hasPrefix:filterText];
} else if (filter.blockType == 1) { //ends with
NSLog(@"NOTIBLOCK - checking if string end with text");
titleMatches = [title hasSuffix:filterText];
subtitleMatches = [subtitle hasSuffix:filterText];
messageMatches = [message hasSuffix:filterText];
} else if (filter.blockType == 2) { //contains
NSLog(@"NOTIBLOCK - checking if string contains text");
titleMatches = [title rangeOfString:filterText].location != NSNotFound;
subtitleMatches = [subtitle rangeOfString:filterText].location != NSNotFound;
messageMatches = [message rangeOfString:filterText].location != NSNotFound;
} else if (filter.blockType == 3) { //exact text
NSLog(@"NOTIBLOCK - checking if string matches text");
titleMatches = [title isEqualToString:filterText];
subtitleMatches = [subtitle isEqualToString:filterText];
messageMatches = [message isEqualToString:filterText];
} else if (filter.blockType == 4) { //regex
NSLog(@"NOTIBLOCK - checking if string matches regex");
NSPredicate *notifTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", filterText];
titleMatches = ![title isEqualToString:@""] && [notifTest evaluateWithObject: title];
subtitleMatches = ![subtitle isEqualToString:@""] && [notifTest evaluateWithObject: subtitle];
messageMatches = ![message isEqualToString:@""] && [notifTest evaluateWithObject: message];
} else if (filter.blockType == 5) { //always
NSLog(@"NOTIBLOCK - app should always be filtered. filtering turned on");
filtered = YES;
}
if ([self doesMessageMatchFilterType:titleMatches arg2:subtitleMatches arg3:messageMatches arg4:filter.filterType]) {
NSLog(@"NOTIBLOCK - filtering was matched");
filtered = YES;
}
if (filter.whitelistMode) {
NSLog(@"NOTIBLOCK - whitelist Mode on");
filtered = !filtered;
}
if (filter.showInNotificationCenter) {
showInNotificationCenter = YES;
}
}
if (filtered) {
if (showInNotificationCenter) {
return 1;
} else {
return 2;
}
} else {
return 0;
}
}
@end
%hook SpringBoard
/**
Loads filters into memory from disk when springboard is loaded
**/
-(void)applicationDidFinishLaunching:(id)application {
%orig;
NSLog(@"NOTIBLOCK - springboard launch");
//do in background thread?
HBPreferences *defaults = [[HBPreferences alloc] initWithIdentifier:@"com.shemeshapps.notiblock"];
NSData *data = [defaults objectForKey:@"filter_array"];
HBPreferences *prefDefaults = [[HBPreferences alloc] initWithIdentifier:@"com.shemeshapps.notiblockpref"];
bool enabled = [([prefDefaults objectForKey:@"enabled"] ?: @(YES)) boolValue];
//for (NSString *key in [[prefDefaults dictionaryRepresentation] allKeys]) {
// NSLog(@"NOTIBLOCK - pref keys: %@", key);
//}
if(!enabled) {
NSLog(@"NOTIBLOCK - tweak disabled. not loading filters");
return;
}
if (data != nil) {
filters = [[NSMutableDictionary alloc] init];
NSArray *dictFilterarray = (NSArray *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
for (NSDictionary * dict in dictFilterarray) {
NotificationFilter *filter = [[NotificationFilter alloc] initWithDictionary:dict];
NSString *dictKey = @"";
if (filter.appToBlock != nil) {
dictKey = filter.appToBlock.appIdentifier;
}
if (![filters objectForKey:dictKey]) {
[filters setObject:[[NSMutableArray alloc] init] forKey:dictKey];
}
NSMutableArray *appIdfilters = [filters objectForKey:dictKey];
[appIdfilters addObject:filter];
NSLog(@"NOTIBLOCK - adding filter to dict for appkey: --%@--", dictKey);
}
} else {
NSLog(@"NOTIBLOCK - springboard data load was nil");
}
}
%end
/**
Code to hide the banner dropdown view
**/
%hook NCNotificationShortLookViewController
-(id)_initWithNotificationRequest:(id)arg1 revealingAdditionalContentOnPresentation:(BOOL)arg2 {
BBBulletin *bulletin = ((NCNotificationRequest *)arg1).bulletin;
NCNotificationShortLookViewController *temp = %orig;
if ([NotiBlockChecker blockTypeForBulletin: bulletin] != 0) {
self.view.hidden = YES;
[self.view setUserInteractionEnabled:NO];
}
return temp;
}
- (void)viewWillAppear:(BOOL)animated {
%orig;
if(self.view.hidden) {
NSLog(@"NOTIBLOCK - NCNotificationShortLookViewController will appear - dismissing banner");
[self dismissViewControllerAnimated:NO completion:nil];
}
}
%end
%hook BBServer
/**
Check for filters and block notifications if needed
**/
-(void)publishBulletin:(id)arg1 destinations:(unsigned long long)arg2 {
BBBulletin *bulletin = ((BBBulletin *)arg1);
int blockType = [NotiBlockChecker blockTypeForBulletin: bulletin];
if (blockType == 0) {
%orig;
return;
}
bulletin.sound = nil;
bulletin.turnsOnDisplay = NO;
%orig(bulletin, arg2);
if (blockType == 2) {
[self _clearBulletinIDs:@[bulletin.bulletinID] forSectionID:bulletin.sectionID shouldSync:YES];
}
}
%end