5 // Created by Christopher Bowns on 12/23/07.
6 // Copyright 2007-2008 Mechanical Pants Software. All rights reserved.
9 #import "MPAppController.h"
11 // comment out nslog_debug definition to turn off logging
16 @implementation MPAppController
19 TODO anything on awakeFromNib?
24 if(self = [super init])
31 - (IBAction)startDownload:(id)sender
36 // [self probePort:55000];
37 [self getRootDeliciousLink];
38 [progressSpinner startAnimation:self];
41 1. retrieve the root level bookmark
44 3. bookmark the hash value
45 4. get the number of bookmarked users
47 5. add a new DeliciousPage object to the array with those values
48 lather rinse repeat ad infinitum, stop when users = 1 (== me?)
52 - (void) probePort: (int) portNumber
57 NSURLRequest *portProbeRequest;
59 portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: [NSString stringWithFormat: @"https://www.grc.com/x/portprobe=%d", portNumber]]
60 cachePolicy: NSURLRequestReloadIgnoringCacheData
61 timeoutInterval: 15.0];
63 NSURLConnection *portProbeConnection = [NSURLConnection connectionWithRequest: portProbeRequest
66 if (portProbeConnection)
67 deliciousData = [[NSMutableData data] retain];
70 NSLog(@"Unable to get port status: failed to initiate connection");
71 // [self callBackWithStatus: PORT_STATUS_ERROR];
75 - (void)getRootDeliciousLink
80 NSURLRequest *request;
82 request = [NSURLRequest requestWithURL: [NSURL URLWithString: [NSString stringWithFormat: @"https://api.del.icio.us/v1/posts/get?&url=http://del.icio.us/", 80]]
83 cachePolicy: NSURLRequestReloadIgnoringCacheData
84 timeoutInterval: 15.0];
86 NSURLConnection *connection = [NSURLConnection connectionWithRequest: request
90 deliciousData = [[NSMutableData data] retain];
93 NSLog(@"Unable to connect!");
94 // [self callBackWithStatus: PORT_STATUS_ERROR];
99 #pragma mark NSURLConnection delegate methods
101 - (void)connection:(NSURLConnection*)connection
102 didReceiveResponse:(NSURLResponse*)response
107 [deliciousData setLength: 0];
110 - (void)connection:(NSURLConnection*)connection
111 didReceiveData:(NSData*)data
116 [deliciousData appendData: data];
119 - (void)connection:(NSURLConnection*)connection
120 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge
123 TODO at this point, log the entire response. what the hell are they asking for? 401 auth? 403 try again?
128 if ( [challenge previousFailureCount] > 1) // || [challenge proposedCredential] != nil )
131 NSLog(@"%s: cancelling authentication challenge:", _cmd);
132 NSLog(@"%s: [challenge previousFailureCount] > 1 == %@", _cmd, [challenge previousFailureCount] > 1 ? @"YES" : @"NO");
133 // NSLog(@"%s: [challenge proposedCredential] != nil == %@", _cmd, [challenge proposedCredential] != nil ? @"YES" : @"NO");
135 [[challenge sender] cancelAuthenticationChallenge:challenge];
136 // [progressSpinner stopAnimation:self];
137 // [deliciousData release];
142 if ( [challenge proposedCredential] != nil )
145 NSLog(@"%s [challenge proposedCredential] != nil == %@", _cmd, [challenge proposedCredential] != nil ? @"YES" : @"NO");
146 NSLog(@"%s credential: user: %s, pass: %s", _cmd, [[challenge proposedCredential] user], [[challenge proposedCredential] password]);
151 TODO didReceiveAuthenticationChallenge: remove hardcoded password
153 NSURLCredential *loginCredential = [NSURLCredential credentialWithUser:@"cipherswarm"
155 persistence:NSURLCredentialPersistenceForSession];
157 [[challenge sender] useCredential:loginCredential
158 forAuthenticationChallenge:challenge];
163 - (void)connection:(NSURLConnection*)connection
164 didFailWithError:(NSError*)error
168 NSLog(@"Unable to retrieve tags: connection failed (%@)", [error localizedDescription]);
170 // [self callBackWithStatus: PORT_STATUS_ERROR];
171 [progressSpinner stopAnimation:self];
172 [deliciousData release];
176 - (void)connectionDidFinishLoading: (NSURLConnection *) connection
181 [progressSpinner stopAnimation:self];
183 TODO where else should we send a stop? on error condition, right?
185 NSXMLDocument *deliciousResult;
186 deliciousResult = [[NSXMLDocument alloc] initWithData: deliciousData options: NSXMLDocumentTidyHTML error: nil];
188 if (deliciousResult == nil)
190 NSLog(@"Unable to open page: failed to create xml document");
191 // [self callBackWithStatus: PORT_STATUS_ERROR];
193 NSArray *nodes = [deliciousResult nodesForXPath: @"/posts/post/tag/" error: nil];
194 if ([nodes count] != 1)
196 NSLog(@"Unable to get tags: invalid (outdated) XPath expression");
197 // [self callBackWithStatus: PORT_STATUS_ERROR];
199 // NSString *portStatus = [[[[nodes objectAtIndex: 0] stringValue] stringByTrimmingCharactersInSet:
200 // [[NSCharacterSet letterCharacterSet] invertedSet]] lowercaseString];
202 NSLog(@"%s: tags: %s", _cmd, [[nodes objectAtIndex: 0] stringValue]);
204 /* if ([portStatus isEqualToString: @"open"])
206 // [self callBackWithStatus: PORT_STATUS_OPEN];
207 NSLog(@"%s, Port is open.", _cmd);
209 else if ([portStatus isEqualToString: @"stealth"])
211 // [self callBackWithStatus: PORT_STATUS_STEALTH];
212 NSLog(@"%s, Port is stealthed", _cmd);
214 else if ([portStatus isEqualToString: @"closed"])
216 // [self callBackWithStatus: PORT_STATUS_CLOSED];
217 NSLog(@"%s, Port is closed.", _cmd);
220 // [self callBackWithStatus: PORT_STATUS_ERROR];
221 NSLog(@"Unable to get port status: unknown port state");
224 [deliciousData release];