back to a stable point. login: successful. XPath query is failing, but we no longer...
[deliciousMeal.git] / MPAppController.m
blobdf9e9f713da3a613d5efaf43f7c0b1a2fab764b6
1 //
2 //  MPAppController.m
3 //  DeliciousMeal
4 //
5 //  Created by Christopher Bowns on 12/23/07.
6 //  Copyright 2007-2008 Mechanical Pants Software. All rights reserved.
7 //
9 #import "MPAppController.h"
11 // comment out nslog_debug definition to turn off logging
12 #ifndef NSLOG_DEBUG
13 #define NSLOG_DEBUG
14 #endif
16 @implementation MPAppController
19 TODO anything on awakeFromNib?
22 - (id) init
24         if(self = [super init])
25         {
26                 
27         }
28         return self;
31 - (IBAction)startDownload:(id)sender
33         #ifdef NSLOG_DEBUG
34         NSLog(@"%s", _cmd);
35         #endif
36         // [self probePort:55000];
37         [self getRootDeliciousLink];
38         [progressSpinner startAnimation:self];
39         /*
40                 TODO Here's the plan:
41                 1. retrieve the root level bookmark
42                 
43                 2. get the hash value
44                 3. bookmark the hash value
45                 4. get the number of bookmarked users
46                 
47                 5. add a new DeliciousPage object to the array with those values
48                 lather rinse repeat ad infinitum, stop when users = 1 (== me?)
49         */
52 - (void) probePort: (int) portNumber
54         #ifdef NSLOG_DEBUG
55         NSLog(@"%s", _cmd);
56         #endif
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
64                                                                              delegate: self];
66         if (portProbeConnection)
67                 deliciousData = [[NSMutableData data] retain];
68         else
69         {
70                 NSLog(@"Unable to get port status: failed to initiate connection");
71                 // [self callBackWithStatus: PORT_STATUS_ERROR];
72         }
75 - (void)getRootDeliciousLink
77         #ifdef NSLOG_DEBUG
78         NSLog(@"%s", _cmd);
79         #endif
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
87                                                                 delegate: self];
89         if (connection)
90                 deliciousData = [[NSMutableData data] retain];
91         else
92         {
93                 NSLog(@"Unable to connect!");
94                 // [self callBackWithStatus: PORT_STATUS_ERROR];
95         }
99 #pragma mark NSURLConnection delegate methods
101 - (void)connection:(NSURLConnection*)connection
102 didReceiveResponse:(NSURLResponse*)response
104         #ifdef NSLOG_DEBUG
105         NSLog(@"%s", _cmd);
106         #endif
107         [deliciousData setLength: 0];
110 - (void)connection:(NSURLConnection*)connection
111     didReceiveData:(NSData*)data
113         #ifdef NSLOG_DEBUG
114         NSLog(@"%s", _cmd);
115         #endif
116         [deliciousData appendData: data];
119 - (void)connection:(NSURLConnection*)connection
120 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challenge
122         /*
123                 TODO at this point, log the entire response. what the hell are they asking for? 401 auth? 403 try again?
124         */
125         #ifdef NSLOG_DEBUG
126         NSLog(@"%s", _cmd);
127         #endif
128         if ( [challenge previousFailureCount] > 1) // || [challenge proposedCredential] != nil )
129         {
130                 #ifdef NSLOG_DEBUG
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");
134                 #endif
135                 [[challenge sender] cancelAuthenticationChallenge:challenge];
136                 // [progressSpinner stopAnimation:self];
137                 // [deliciousData release];
138                 return;
139         }
140         
141         
142         if ( [challenge proposedCredential] != nil )
143         {
144                 #ifdef NSLOG_DEBUG
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]);
147                 #endif
148         }
149         
150         /*
151                 TODO didReceiveAuthenticationChallenge: remove hardcoded password
152         */
153         NSURLCredential *loginCredential = [NSURLCredential credentialWithUser:@"cipherswarm"
154                                                                       password:@"e2ca7b52"
155                                                                    persistence:NSURLCredentialPersistenceForSession];
156         
157         [[challenge sender] useCredential:loginCredential
158                forAuthenticationChallenge:challenge];
159         
163 - (void)connection:(NSURLConnection*)connection
164   didFailWithError:(NSError*)error
166         #ifdef NSLOG_DEBUG
167         NSLog(@"%s", _cmd);
168         NSLog(@"Unable to retrieve tags: connection failed (%@)", [error localizedDescription]);
169         #endif
170         // [self callBackWithStatus: PORT_STATUS_ERROR];
171         [progressSpinner stopAnimation:self];
172         [deliciousData release];
176 - (void)connectionDidFinishLoading: (NSURLConnection *) connection
178         #ifdef NSLOG_DEBUG
179         NSLog(@"%s", _cmd);
180         #endif
181         [progressSpinner stopAnimation:self];
182         /*
183                 TODO where else should we send a stop? on error condition, right?
184         */
185         NSXMLDocument *deliciousResult;
186         deliciousResult = [[NSXMLDocument alloc] initWithData: deliciousData options: NSXMLDocumentTidyHTML error: nil];
188         if (deliciousResult == nil)
189         {
190                 NSLog(@"Unable to open page: failed to create xml document");
191                 // [self callBackWithStatus: PORT_STATUS_ERROR];
192         }
193         NSArray *nodes = [deliciousResult nodesForXPath: @"/posts/post/tag/" error: nil];
194         if ([nodes count] != 1)
195         {
196                 NSLog(@"Unable to get tags: invalid (outdated) XPath expression");
197                         // [self callBackWithStatus: PORT_STATUS_ERROR];
198         }
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"])
205         {
206                 // [self callBackWithStatus: PORT_STATUS_OPEN];
207                 NSLog(@"%s, Port is open.", _cmd);
208         }
209         else if ([portStatus isEqualToString: @"stealth"])
210         {
211                 // [self callBackWithStatus: PORT_STATUS_STEALTH];
212                 NSLog(@"%s, Port is stealthed", _cmd);
213         }       
214         else if ([portStatus isEqualToString: @"closed"])
215         {
216                 // [self callBackWithStatus: PORT_STATUS_CLOSED];
217                 NSLog(@"%s, Port is closed.", _cmd);
218         }
219         else {
220                 // [self callBackWithStatus: PORT_STATUS_ERROR];
221                 NSLog(@"Unable to get port status: unknown port state");
222         }*/
223         
224         [deliciousData release];
228 @end