|
@@ -10,8 +10,32 @@
|
10
|
10
|
|
11
|
11
|
@implementation Reddit
|
12
|
12
|
|
|
13
|
+NSString *version = @"1.0.0";
|
|
14
|
+NSString *author = @"xythobuz";
|
|
15
|
+NSString *appName = @"RedditBar";
|
|
16
|
+
|
13
|
17
|
@synthesize username, modhash, password;
|
14
|
18
|
|
|
19
|
+-(NSString *)urlencode:(NSString *)string {
|
|
20
|
+ NSMutableString *output = [NSMutableString string];
|
|
21
|
+ const unsigned char *source = (const unsigned char *)[string UTF8String];
|
|
22
|
+ long sourceLen = strlen((const char *)source);
|
|
23
|
+ for (int i = 0; i < sourceLen; i++) {
|
|
24
|
+ const unsigned char thisChar = source[i];
|
|
25
|
+ if (thisChar == ' '){
|
|
26
|
+ [output appendString:@"+"];
|
|
27
|
+ } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
|
|
28
|
+ (thisChar >= 'a' && thisChar <= 'z') ||
|
|
29
|
+ (thisChar >= 'A' && thisChar <= 'Z') ||
|
|
30
|
+ (thisChar >= '0' && thisChar <= '9')) {
|
|
31
|
+ [output appendFormat:@"%c", thisChar];
|
|
32
|
+ } else {
|
|
33
|
+ [output appendFormat:@"%%%02X", thisChar];
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+ return output;
|
|
37
|
+}
|
|
38
|
+
|
15
|
39
|
-(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash {
|
16
|
40
|
self = [super init];
|
17
|
41
|
if (self) {
|
|
@@ -33,8 +57,49 @@
|
33
|
57
|
}
|
34
|
58
|
|
35
|
59
|
-(NSString *)queryModhash {
|
36
|
|
- sleep(1);
|
37
|
|
- return nil;
|
|
60
|
+ NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
|
|
61
|
+ [stringData appendFormat:@"&user=%@", [self urlencode: username]];
|
|
62
|
+ [stringData appendFormat:@"&passwd=%@", [self urlencode: password]];
|
|
63
|
+ NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
|
|
64
|
+ NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
|
|
65
|
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://ssl.reddit.com/api/login"]];
|
|
66
|
+ [request setTimeoutInterval:5.0];
|
|
67
|
+ [request setCachePolicy:NSURLCacheStorageNotAllowed];
|
|
68
|
+ [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
|
|
69
|
+ [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
|
|
70
|
+ [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
|
|
71
|
+ [request setHTTPMethod:@"POST"];
|
|
72
|
+ [request setHTTPBody:requestBodyData];
|
|
73
|
+ NSURLResponse *response = nil;
|
|
74
|
+ NSError *error = nil;
|
|
75
|
+ NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
|
|
76
|
+ if (error != nil) {
|
|
77
|
+ return nil;
|
|
78
|
+ } else {
|
|
79
|
+ NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
|
|
80
|
+ long code = [resp statusCode];
|
|
81
|
+ if (code == 200) {
|
|
82
|
+ id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
|
83
|
+
|
|
84
|
+ if (error)
|
|
85
|
+ return nil;
|
|
86
|
+
|
|
87
|
+ if([object isKindOfClass:[NSDictionary class]]) {
|
|
88
|
+ NSDictionary *results = object;
|
|
89
|
+ NSDictionary *json = [results valueForKey:@"json"];
|
|
90
|
+ if (json == nil)
|
|
91
|
+ return nil;
|
|
92
|
+ NSDictionary *data = [json valueForKey:@"data"];
|
|
93
|
+ if (data == nil)
|
|
94
|
+ return nil;
|
|
95
|
+ return [data valueForKey:@"modhash"];
|
|
96
|
+ } else {
|
|
97
|
+ return nil;
|
|
98
|
+ }
|
|
99
|
+ } else {
|
|
100
|
+ return nil;
|
|
101
|
+ }
|
|
102
|
+ }
|
38
|
103
|
}
|
39
|
104
|
|
40
|
105
|
-(BOOL)isAuthenticated {
|