forked from coolvision/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HostImporter.m
executable file
·169 lines (139 loc) · 6.35 KB
/
HostImporter.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
//
// HostImporter.m
// SelfControl
//
// Created by Charlie Stigler on 2/16/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 "HostImporter.h"
@implementation HostImporter
+ (NSArray*)incomingMailHostnamesFromMail {
NSMutableArray* hostnames = [NSMutableArray arrayWithCapacity: 10];
NSString* sandboxedPreferences = [@"~/Library/Containers/com.apple.mail/Data/Library/Preferences/com.apple.mail" stringByExpandingTildeInPath];
NSDictionary* defaults = [[NSUserDefaults standardUserDefaults] persistentDomainForName: sandboxedPreferences];
NSArray* incomingAccounts = defaults[@"MailAccounts"];
for(int i = 0; i < [incomingAccounts count]; i++) {
NSMutableString* hostname = [incomingAccounts[i][@"Hostname"] mutableCopy];
// The LocalAccountName account has no hostname, so trying to add it would cause an error
if(hostname != nil) {
// If it has a defined port number, add it to the host to block for added
// block specificity, so only incoming mail is blocked.
if([incomingAccounts[i][@"PortNumber"] length]) {
[hostname appendString: @":"];
[hostname appendString: incomingAccounts[i][@"PortNumber"]];
// If it doesn't have a defined port number, we'll go through and choose
// the default port for the type of account it is.
} else if([incomingAccounts[i][@"AccountType"] isEqual: @"POPAccount"]) {
if(incomingAccounts[i][@"SSLEnabled"]) {
[hostname appendString: @":995"];
} else {
[hostname appendString: @":110"];
}
} else if([incomingAccounts[i][@"AccountType"] isEqual: @"IMAPAccount"]) {
if(incomingAccounts[i][@"SSLEnabled"]) {
[hostname appendString: @":993"];
} else {
[hostname appendString: @":143"];
}
}
[hostnames addObject: hostname];
}
}
return hostnames;
}
+ (NSArray*)outgoingMailHostnamesFromMail {
NSMutableArray* hostnames = [NSMutableArray arrayWithCapacity: 10];
NSString* sandboxedPreferences = [@"~/Library/Containers/com.apple.mail/Data/Library/Preferences/com.apple.mail" stringByExpandingTildeInPath];
NSDictionary* defaults = [[NSUserDefaults standardUserDefaults] persistentDomainForName: sandboxedPreferences];
NSArray* outgoingAccounts = defaults[@"DeliveryAccounts"];
for(int i = 0; i < [outgoingAccounts count]; i++) {
NSMutableString* hostname = [outgoingAccounts[i][@"Hostname"] mutableCopy];
if(hostname != nil) {
if([outgoingAccounts[i][@"PortNumber"] length]) {
[hostname appendString: @":"];
[hostname appendString: outgoingAccounts[i][@"PortNumber"]];
} else if (outgoingAccounts[i][@"SSLEnabled"]) {
// If it doesn't have a defined port number, we'll block all the default outoging ports
[hostnames addObject: [hostname stringByAppendingString: @":465"]];
[hostname appendString: @":587"];
} else {
[hostname appendString: @":25"];
}
[hostnames addObject: hostname];
}
}
return hostnames;
}
+ (NSArray*)incomingMailHostnamesFromMailMate {
NSMutableArray* hostnames = [NSMutableArray arrayWithCapacity: 10];
NSString* sourcesPath = [@"~/Library/Application Support/MailMate/Sources.plist" stringByExpandingTildeInPath];
NSData* plistData = [NSData dataWithContentsOfFile: sourcesPath];
if (!plistData) return hostnames;
NSDictionary* prefs = [NSPropertyListSerialization propertyListWithData: plistData options: NSPropertyListImmutable format: nil error: nil];
if (!prefs) return hostnames;
NSArray* accounts = prefs[@"sources"];
for(int i = 0; i < [accounts count]; i++) {
NSURL* serverURL = [NSURL URLWithString: accounts[i][@"serverURL"]];
if (!serverURL) continue;
NSMutableString* hostname = [serverURL.host mutableCopy];
if (![hostname length]) continue;
// If it has a defined port number, add it to the host to block for added
// block specificity, so only incoming mail is blocked.
if([accounts[i][@"port"] length]) {
[hostname appendString: @":"];
[hostname appendString: accounts[i][@"port"]];
} else if([serverURL.scheme isEqualToString: @"imap"]) {
[hostname appendString: @":993"];
[hostnames addObject: [hostname stringByAppendingString: @":143"]];
} else if([serverURL.scheme isEqualToString: @"pop"]) {
[hostname appendString: @":995"];
[hostnames addObject: [hostname stringByAppendingString: @":110"]];
}
[hostnames addObject: hostname];
}
return hostnames;
}
+ (NSArray*)outgoingMailHostnamesFromMailMate {
NSMutableArray* hostnames = [NSMutableArray arrayWithCapacity: 10];
NSString* submissionPath = [@"~/Library/Application Support/MailMate/Submission.plist" stringByExpandingTildeInPath];
NSData* plistData = [NSData dataWithContentsOfFile: submissionPath];
if (!plistData) return hostnames;
NSDictionary* prefs = [NSPropertyListSerialization propertyListWithData: plistData options: NSPropertyListImmutable format: nil error: nil];
if (!prefs) return hostnames;
NSArray* smtpServers = prefs[@"smtpServers"];
for(int i = 0; i < [smtpServers count]; i++) {
NSURL* serverURL = [NSURL URLWithString: smtpServers[i][@"serverURL"]];
if (!serverURL) continue;
NSMutableString* hostname = [serverURL.host mutableCopy];
if (![hostname length]) continue;
// If it has a defined port number, add it to the host to block for added
// block specificity, so only incoming mail is blocked.
if([smtpServers[i][@"port"] length]) {
[hostname appendString: @":"];
[hostname appendString: smtpServers[i][@"port"]];
} else {
[hostname appendString: @":25"];
[hostnames addObject: [hostname stringByAppendingString: @":587"]];
}
[hostnames addObject: hostname];
}
return hostnames;
}
+ (NSArray*)incomingMailHostnamesFromThunderbird {
return [ThunderbirdPreferenceParser incomingHostnames];
}
+ (NSArray*)outgoingMailHostnamesFromThunderbird {
return [ThunderbirdPreferenceParser outgoingHostnames];
}
@end