/
RemoteHostsSource.m
343 lines (315 loc) · 13.8 KB
/
RemoteHostsSource.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
//
// RemoteHostsSource.m
// RemoteHosts
//
// Created by Rob McBroom on 12/24/09.
//
#import "RemoteHostsSource.h"
@implementation QSRemoteHostsSource
// if this returns FALSE, the source will be rescanned
// if it returns TRUE, the source is left alone
// unconditional returns will cause it to either be scanned every time, or never
- (BOOL)indexIsValidFromDate:(NSDate *)indexDate forEntry:(NSDictionary *)theEntry
{
// use the plist settings to determine which file to check
NSMutableDictionary *settings = [theEntry objectForKey:kItemSettings];
NSString *sourceFile = [self fullPathForSettings:settings];
// get the last modified date on the source file
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:sourceFile isDirectory:NULL]) return YES;
NSDate *modDate = [[manager attributesOfItemAtPath:sourceFile error:NULL] fileModificationDate];
// compare dates and return whether or not the entry should be rescanned
if ([modDate compare:indexDate] == NSOrderedDescending) return NO;
// if we fall through to this point, don't rescan by default
return YES;
}
// show this on the drop-down for adding custom catalog entries
- (BOOL)isVisibleSource
{
return YES;
}
- (NSView *)settingsView
{
if (![super settingsView]) {
[NSBundle loadNibNamed:NSStringFromClass([self class]) owner:self];
}
return [super settingsView];
}
- (NSImage *) iconForEntry:(NSDictionary *)theEntry
{
return [QSResourceManager imageNamed:@"com.apple.xserve"];
}
// this is set in the Info.plist
// not sure if this method is to conditioanlly override it or what
// - (NSImage *) iconForEntry:(NSDictionary *)dict
// {
// return [QSResourceManager imageNamed:@"com.apple.xserve"];
// }
// Return a unique identifier for an object (if you haven't assigned one before)
//- (NSString *)identifierForObject:(id <QSObject>)object
//{
// return @"QSMachineObject0";
//}
- (NSArray *) objectsForEntry:(NSDictionary *)theEntry
{
// use the plist settings to determine which file to load from
NSDictionary *settings = [theEntry objectForKey:kItemSettings];
NSString *path = [self fullPathForSettings:settings];
//NSLog(@"Loading remote hosts from: %@", path);
// a list of objects that will get returned (and added to the Catalog)
NSMutableArray *objects = [NSMutableArray arrayWithCapacity:1];
// somewhere to dump errors
NSError *e;
// read the entire file in as a string
NSString *hostsSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&e];
// bail out with an error if the file couldn't be opened
if(!hostsSource) {
// there was an error reading the file
NSLog(@"Remote hosts could not be loaded from %@: %@", path, [e localizedFailureReason]);
return nil;
}
// read in hosts, one per line
NSArray *lines = [hostsSource lines];
NSPredicate *regextest = predicateForValidHostname();
// a place to store groups as they're discovered
NSMutableDictionary *groups = [[NSMutableDictionary alloc] init];
[lines enumerateObjectsUsingBlock:^(NSString *line, NSUInteger i, BOOL *stop) {
// skip empty lines
if ([line length] != 0) {
// allow other metadata in the file, separated by whitespace
// hostname or FQDN should be the first thing on the line
NSArray *lineParts = [line componentsSeparatedByString:@" "];
// ~/.ssh/known_hosts could be host or host,ip
// to support that file, split on comma
NSArray *hostParts = [[lineParts objectAtIndex:0] componentsSeparatedByString:@","];
NSString *host = [hostParts objectAtIndex:0];
if ([regextest evaluateWithObject:host])
{
// this looks like a valid hostname
// build a QSObject
QSObject *newObject = hostObjectForSource(host, @"RemoteHostsSource");
[newObject setObject:@"RemoteHostsSource" forMeta:@"source"];
// add some metadata
if([lineParts count] > 1)
{
NSRange allButFirst;
allButFirst.location = 1;
allButFirst.length = [lineParts count] - 1;
for (NSString *metadata in [lineParts subarrayWithRange:allButFirst])
{
// metadata should look like 'key:value'
// if we find something like this, add it
NSArray *dataParts = [metadata componentsSeparatedByString:@":"];
if ([dataParts count] == 2)
{
[newObject setObject:[dataParts objectAtIndex:1] forMeta:[dataParts objectAtIndex:0]];
}
}
// look for groups
NSString *groupList = [newObject objectForMeta:@"groups"];
if (groupList) {
NSArray *groupArray = [groupList componentsSeparatedByString:@","];
// remember which hosts belong to groups
for (NSString *groupName in groupArray) {
if (![groups objectForKey:groupName]) {
[groups setObject:[NSMutableArray arrayWithCapacity:1] forKey:groupName];
}
[[groups objectForKey:groupName] addObject:[newObject identifier]];
}
}
}
// make the description and label more useful if possible
NSString *ostype = [newObject objectForMeta:kQSMetaOSType];
NSString *hostType;
if (ostype)
{
hostType = [ostype capitalizedString];
} else {
hostType = @"Remote";
}
// the "details" string appears in smaller text below the object in the UI
[newObject setDetails:[NSString stringWithFormat:@"%@ (%@ Host)", host, hostType]];
NSString *labelExtra = [newObject objectForMeta:@"label"];
if (labelExtra)
{
[newObject setLabel:[NSString stringWithFormat:@"%@ • %@", [newObject displayName], labelExtra]];
}
// if the object is OK, add it to the list
if (newObject)
[objects addObject:newObject];
}
}
}];
// add groups to the catalog
QSObject *newObject = nil;
for (NSString *groupName in [groups allKeys]) {
newObject = [QSObject makeObjectWithIdentifier:[NSString stringWithFormat:@"remote-host-group-%@", groupName]];
[newObject setName:[NSString stringWithFormat:@"%@ Remote Host Group", groupName]];
[newObject setDetails:[NSString stringWithFormat:@"%@ Remote Host Group", groupName]];
[newObject setLabel:groupName];
[newObject setObject:[groups objectForKey:groupName] forMeta:kQSMetaGroupMembers];
[newObject setObject:groupName forType:QSRemoteHostsGroupType];
[newObject setPrimaryType:QSRemoteHostsGroupType];
[objects addObject:newObject];
}
groups = nil;
return objects;
}
// this method gets the path for a file to scan from an Info.plist
- (NSString *)fullPathForSettings:(NSDictionary *)settings
{
if (![settings objectForKey:kItemPath]) return nil;
NSString *itemPath = [[settings objectForKey:kItemPath] stringByResolvingWildcardsInPath];
if (![itemPath isAbsolutePath]) {
NSString *bundlePath = [[QSReg bundleWithIdentifier:[settings objectForKey:kItemBaseBundle]] bundlePath];
if (!bundlePath) bundlePath = [[NSBundle mainBundle] bundlePath];
itemPath = [bundlePath stringByAppendingPathComponent:itemPath];
}
return itemPath;
}
- (IBAction)selectHostsFile:(NSButton *)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
NSString *oldFile = [[hostsFilePath stringValue] stringByStandardizingPath];
NSString *oldPath = [oldFile stringByDeletingLastPathComponent];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanChooseFiles:YES];
[openPanel setShowsHiddenFiles:YES];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:oldPath isDirectory:YES]];
if (![openPanel runModal]) return;
NSString *newFile = [[openPanel URL] path];
[hostsFilePath setStringValue:[newFile stringByAbbreviatingWithTildeInPath]];
[[self selection] setName:[newFile lastPathComponent]];
// update catalog entry
NSMutableDictionary *settings = [[self currentEntry] objectForKey:kItemSettings];
if (!settings) {
settings = [NSMutableDictionary dictionaryWithCapacity:1];
[[self currentEntry] setObject:settings forKey:kItemSettings];
}
[settings setObject:[hostsFilePath stringValue] forKey:kItemPath];
[settings setObject:[[hostsFilePath stringValue] lastPathComponent] forKey:kItemName];
[currentEntry setObject:[NSNumber numberWithFloat:[NSDate timeIntervalSinceReferenceDate]] forKey:kItemModificationDate];
[self populateFields];
[[NSNotificationCenter defaultCenter] postNotificationName:QSCatalogEntryChangedNotification object:[self currentEntry]];
}
- (void)populateFields
{
NSMutableDictionary *settings = [[self currentEntry] objectForKey:kItemSettings];
NSString *path = [settings objectForKey:kItemPath];
[hostsFilePath setStringValue:(path?path:@"")];
}
// Object Handler Methods
- (void)setQuickIconForObject:(QSObject *)object
{
[object setIcon:[QSResourceManager imageNamed:@"com.apple.xserve"]];
}
- (BOOL)loadIconForObject:(QSObject *)object
{
// check for an icon in metadata
NSImage *icon = [QSResourceManager imageNamed:[object objectForMeta:@"icon"]];
if (!icon)
{
// no icon specified, so pick one based on OS type
NSString *ostype = [object objectForMeta:kQSMetaOSType];
if ([ostype isEqualToString:@"lom"]) {
// Lights-Out Management interface
icon = [QSResourceManager imageNamed:@"ToolbarUtilitiesFolderIcon"];
} else {
// check for OS icons stored in the bundle
if ([ostype isEqualToString:@"unix"] || [ostype isEqualToString:@"linux"]) {
ostype = @"unix-linux";
}
NSString *iconFile = [NSString stringWithFormat:@"%@.png", ostype];
icon = [QSResourceManager imageNamed:iconFile inBundle:[NSBundle bundleForClass:[self class]]];
}
}
if (icon) {
[object setIcon:icon];
}
return YES;
}
- (BOOL)objectHasChildren:(QSObject *)object
{
return YES;
}
- (BOOL)loadChildrenForObject:(QSObject *)object
{
NSMutableArray *children = [NSMutableArray arrayWithCapacity:1];
if ([[object primaryType] isEqualToString:QSRemoteHostsType]) {
NSString *hostName = [object name];
// look up the LOM address for this host and add it as a child
NSString *lom = [object objectForMeta:@"lom"];
if (lom)
{
// NSString *hostNameOnly = [[host componentsSeparatedByString:@"."] objectAtIndex:0];
// NSString *label = [NSString stringWithFormat:@"%@ • LOM", hostName];
NSString *label = @"Lights-Out Management";
// using objectWithString here would cause Quicksilver to treat the address as a URL
// so we create the object with a few explicit details to make it act like text
NSString *ident = [NSString stringWithFormat:@"remote-host-%@", lom];
QSObject *lomObject = [QSObject objectWithName:lom];
[lomObject setIdentifier:ident];
[lomObject setObject:lom forType:QSRemoteHostsType];
// this type allows paste, large type, e-mail, IM, etc
[lomObject setObject:lom forType:QSTextType];
[lomObject setPrimaryType:QSRemoteHostsType];
[lomObject setLabel:label];
[lomObject setObject:@"lom" forMeta:kQSMetaOSType];
[children addObject:lomObject];
}
// if there's a URL for host info, add it as a child
NSString *infoURLtemplate = [[NSUserDefaults standardUserDefaults] objectForKey:kInfoURL];
if (infoURLtemplate) {
// there's a URL defined to provide host info
NSString *nameForURL;
if ([[NSUserDefaults standardUserDefaults] boolForKey:kStripDomain]) {
nameForURL = [[hostName componentsSeparatedByString:@"."] objectAtIndex:0];
} else {
nameForURL = hostName;
}
NSString *infoURL = [infoURLtemplate stringByReplacingOccurrencesOfString:@"***" withString:nameForURL];
QSObject *hostInfo = [QSObject URLObjectWithURL:infoURL title:@"Host Info"];
[hostInfo setIcon:[QSResourceManager imageNamed:@"ToolbarInfo"]];
[children addObject:hostInfo];
}
// look up the IP address for this host and add it as a child
NSHost *host = [NSHost hostWithName:hostName];
// this action doesn't support the comma-trick
// if there is no such host, skip this
if (host) {
for (NSString *ip in [host addresses])
{
// using objectWithString here would cause Quicksilver to treat the IP as a URL
// so we create the object with a few explicit details to make it act like text
QSObject *ipObject = [QSObject objectWithName:ip];
[ipObject setObject:ip forType:QSTextType];
[ipObject setIcon:[QSResourceManager imageNamed:@"GenericNetworkIcon"]];
[children addObject:ipObject];
}
for (NSString *alias in [host names])
{
// using objectWithString here would cause Quicksilver to treat the IP as a URL
// so we create the object with a few explicit details to make it act like text
QSObject *aliasObject = [QSObject objectWithName:alias];
[aliasObject setObject:alias forType:QSTextType];
[aliasObject setIcon:[QSResourceManager imageNamed:@"GenericNetworkIcon"]];
[children addObject:aliasObject];
}
}
[object setChildren:children];
return YES;
} else if ([[object primaryType] isEqualToString:QSRemoteHostsGroupType]) {
// get the individual QSObjects for hosts in this group
QSObject *host;
for (NSString *ident in [object objectForMeta:@"members"]) {
host = [QSLib objectWithIdentifier:ident];
if (host) {
[children addObject:host];
}
}
[object setChildren:children];
return YES;
}
return NO;
}
@end