forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomainListWindowController.m
executable file
·347 lines (284 loc) · 11.2 KB
/
DomainListWindowController.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
//
// DomainListWindowController.m
// SelfControl
//
// Created by Charlie Stigler on 2/7/09.
// Copyright 2009 Eyebeam.
// This file is part of SelfControl.
//
// SelfControl 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 3 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, see <http://www.gnu.org/licenses/>.
#import "DomainListWindowController.h"
@implementation DomainListWindowController
- (DomainListWindowController*)init {
if(self = [super initWithWindowNibName:@"DomainList"]) {
defaults_ = [NSUserDefaults standardUserDefaults];
NSArray* curArray = [defaults_ arrayForKey: @"HostBlacklist"];
if(curArray == nil)
domainList_ = [NSMutableArray arrayWithCapacity: 10];
else
domainList_ = [curArray mutableCopy];
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
}
return self;
}
- (void)showWindow:(id)sender {
[[self window] makeKeyAndOrderFront: self];
if ([domainList_ count] == 0) {
[self addDomain: self];
}
}
- (IBAction)addDomain:(id)sender
{
[domainList_ addObject:@""];
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[domainListTableView_ reloadData];
NSIndexSet* rowIndex = [NSIndexSet indexSetWithIndex: [domainList_ count] - 1];
[domainListTableView_ selectRowIndexes: rowIndex
byExtendingSelection: NO];
[domainListTableView_ editColumn: 0 row:([domainList_ count] - 1)
withEvent:nil
select:YES];
}
- (IBAction)removeDomain:(id)sender
{
NSIndexSet* selected = [domainListTableView_ selectedRowIndexes];
[domainListTableView_ abortEditing];
// This isn't the most efficient way to do this, but the code is much cleaner
// than other methods and the domain blacklist will probably never be large
// enough for it to be an issue.
NSUInteger index = [selected firstIndex];
int shift = 0;
while (index != NSNotFound) {
if ((index - shift) >= [domainList_ count])
break;
[domainList_ removeObjectAtIndex: index - shift];
shift++;
index = [selected indexGreaterThanIndex: index];
}
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[domainListTableView_ reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: self];
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView {
return [domainList_ count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {
if (rowIndex < 0 || rowIndex + 1 > [domainList_ count]) return nil;
return domainList_[rowIndex];
}
- (void)controlTextDidEndEditing:(NSNotification *)note {
NSInteger editedRow = [domainListTableView_ editedRow];
NSString* editedString = [[[[note userInfo] objectForKey: @"NSFieldEditor"] textStorage] string];
editedString = [editedString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (![editedString length]) {
NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex: editedRow];
[domainListTableView_ beginUpdates];
[domainListTableView_ removeRowsAtIndexes: indexSet withAnimation: NSTableViewAnimationSlideUp];
[domainList_ removeObjectAtIndex: editedRow];
[domainListTableView_ reloadData];
[domainListTableView_ endUpdates];
return;
}
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(NSString*)newString
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex {
if (rowIndex < 0 || rowIndex + 1 > [domainList_ count]) {
return;
}
// All of this is just code to standardize and clean up the input value.
// This'll remove whitespace and lowercase the string.
NSString* str = [[newString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString];
if([str rangeOfCharacterFromSet: [NSCharacterSet newlineCharacterSet]].location != NSNotFound) {
// only hits LF linebreaks, but componentsSeparatedByCharacterSet won't work on 10.4
NSArray* listComponents = [str componentsSeparatedByString: @"\n"];
for(int i = 0; i < [listComponents count]; i++) {
if(i == 0) {
[self tableView: aTableView setObjectValue: listComponents[i] forTableColumn: aTableColumn row: rowIndex];
}
else {
[domainList_ addObject:@""];
[self tableView: aTableView setObjectValue: listComponents[i] forTableColumn: aTableColumn row: [domainList_ count] - 1];
}
}
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[domainListTableView_ reloadData];
return;
}
// Remove "http://" if a user tried to put that in
NSArray* splitString = [str componentsSeparatedByString: @"http://"];
for(int i = 0; i < [splitString count]; i++) {
if(![splitString[i] isEqual: @""]) {
str = splitString[i];
break;
}
}
// Remove "https://" if a user tried to put that in
splitString = [str componentsSeparatedByString: @"https://"];
for(int i = 0; i < [splitString count]; i++) {
if(![splitString[i] isEqual: @""]) {
str = splitString[i];
break;
}
}
// Remove URL login names/passwords (username:password@host) if a user tried to put that in
splitString = [str componentsSeparatedByString: @"@"];
str = [splitString lastObject];
// Delete anything after a "/" in case a user tried to copy-paste a web address.
// str = [[str componentsSeparatedByString: @"/"] objectAtIndex: 0];
int maskLength = -1;
int portNum = -1;
splitString = [str componentsSeparatedByString: @"/"];
str = splitString[0];
NSString* stringToSearchForPort = str;
if([splitString count] >= 2) {
maskLength = [splitString[1] intValue];
// If the int value is 0, we couldn't find a valid integer representation
// in the split off string
if(maskLength == 0)
maskLength = -1;
stringToSearchForPort = splitString[1];
}
splitString = [stringToSearchForPort componentsSeparatedByString: @":"];
if(stringToSearchForPort == str) {
str = splitString[0];
}
if([splitString count] >= 2) {
portNum = [splitString[1] intValue];
// If the int value is 0, we couldn't find a valid integer representation
// in the split off string
if(portNum == 0)
portNum = -1;
}
if ([str length] || portNum >= 0){
NSString* maskString;
NSString* portString;
if(maskLength == -1)
maskString = @"";
else
maskString = [NSString stringWithFormat: @"/%d", maskLength];
if(portNum == -1)
portString = @"";
else
portString = [NSString stringWithFormat: @":%d", portNum];
str = [NSString stringWithFormat: @"%@%@%@", str, maskString, portString];
domainList_[rowIndex] = str;
}
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[aTableView reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: self];
}
- (void)tableView:(NSTableView *)tableView
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn
row:(int)row {
// this method is really inefficient. rewrite/optimize later.
[defaults_ synchronize];
// Initialize the cell's text color to black
[cell setTextColor: [NSColor blackColor]];
NSString* str = [[cell title] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([str isEqual: @""]) return;
if([defaults_ boolForKey: @"HighlightInvalidHosts"]) {
// Validate the value as either an IP or a hostname. In case of failure,
// we'll make its text color red.
int maskLength = -1;
int portNum = -1;
NSArray* splitString = [str componentsSeparatedByString: @"/"];
str = [splitString[0] lowercaseString];
NSString* stringToSearchForPort = str;
if([splitString count] >= 2) {
maskLength = [splitString[1] intValue];
// If the int value is 0, we couldn't find a valid integer representation
// in the split off string
if(maskLength == 0)
maskLength = -1;
stringToSearchForPort = splitString[1];
}
splitString = [stringToSearchForPort componentsSeparatedByString: @":"];
if(stringToSearchForPort == str) {
str = splitString[0];
}
if([splitString count] >= 2) {
portNum = [splitString[1] intValue];
// If the int value is 0, we couldn't find a valid integer representation
// in the split off string
if(portNum == 0)
portNum = -1;
}
BOOL isIP;
NSString* ipValidationRegex = @"^([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
NSPredicate *ipRegexTester = [NSPredicate
predicateWithFormat:@"SELF MATCHES %@",
ipValidationRegex];
isIP = [ipRegexTester evaluateWithObject: str];
if(!isIP) {
NSString* hostnameValidationRegex = @"^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$";
NSPredicate *hostnameRegexTester = [NSPredicate
predicateWithFormat:@"SELF MATCHES %@",
hostnameValidationRegex
];
if(![hostnameRegexTester evaluateWithObject: str] && ![str isEqualToString: @"*"] && ![str isEqualToString: @""]) {
[cell setTextColor: [NSColor redColor]];
return;
}
}
// We shouldn't have a mask length if it's not an IP, fail
if(!isIP && maskLength != -1) {
[cell setTextColor: [NSColor redColor]];
return;
}
if(([str isEqualToString: @"*"] || [str isEqualToString: @""]) && portNum == -1) {
[cell setTextColor: [NSColor redColor]];
return;
}
[cell setTextColor: [NSColor blackColor]];
}
}
- (void)addHostArray:(NSArray*)arr {
for(int i = 0; i < [arr count]; i++) {
// Check for dupes
if(![domainList_ containsObject: arr[i]])
[domainList_ addObject: arr[i]];
}
[defaults_ setObject: domainList_ forKey: @"HostBlacklist"];
[domainListTableView_ reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName: @"SCConfigurationChangedNotification"
object: self];
}
- (IBAction)importCommonDistractingWebsites:(id)sender {
[self addHostArray: [HostImporter commonDistractingWebsites]];
}
- (IBAction)importNewsAndPublications:(id)sender {
[self addHostArray: [HostImporter newsAndPublications]];
}
- (IBAction)importIncomingMailServersFromThunderbird:(id)sender {
[self addHostArray: [HostImporter incomingMailHostnamesFromThunderbird]];
}
- (IBAction)importOutgoingMailServersFromThunderbird:(id)sender {
[self addHostArray: [HostImporter outgoingMailHostnamesFromThunderbird]];
}
- (IBAction)importIncomingMailServersFromMail:(id)sender {
[self addHostArray: [HostImporter incomingMailHostnamesFromMail]];
}
- (IBAction)importOutgoingMailServersFromMail:(id)sender {
[self addHostArray: [HostImporter outgoingMailHostnamesFromMail]];
}
- (IBAction)importIncomingMailServersFromMailMate:(id)sender {
[self addHostArray: [HostImporter incomingMailHostnamesFromMailMate]];
}
- (IBAction)importOutgoingMailServersFromMailMate:(id)sender {
[self addHostArray: [HostImporter outgoingMailHostnamesFromMailMate]];
}
@end