This repository was archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBGLastFmHandshakeResponse.m
More file actions
123 lines (103 loc) · 4.66 KB
/
BGLastFmHandshakeResponse.m
File metadata and controls
123 lines (103 loc) · 4.66 KB
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
//
// BGLastFmHandshakeResponse.m
// LastFmProtocolTester
//
// Created by Ben Gummer on 8/07/07.
// Copyright 2007 Ben Gummer. All rights reserved.
//
#import "BGLastFmHandshakeResponse.h"
#import "BGLastFmDefines.h"
#define Use_Timeout_Url NO
@implementation BGLastFmHandshakeResponse
- (id) init {
self = [super init];
if (self != nil) {
responseComponents = [NSMutableDictionary new];
[responseComponents setObject:[NSNumber numberWithBool:NO] forKey:Handshake_DidFail];
}
return self;
}
-(id)initWithHandshakeResponseString:(NSString *)aString {
self = [super init];
if (self != nil) {
responseLines = [[aString componentsSeparatedByString:@"\n"] retain];
NSString *statusLine = [responseLines objectAtIndex:0];
responseType = 0;
if ([statusLine rangeOfString:@"OK"].length > 0) {
responseType = 1;
} else if ([statusLine rangeOfString:@"BANNED"].length > 0) {
responseType = 2;
} else if ([statusLine rangeOfString:@"BADAUTH"].length > 0) {
responseType = 3;
} else if ([statusLine rangeOfString:@"BADTIME"].length > 0) {
responseType = 4;
} else if ([statusLine rangeOfString:@"FAILED"].length > 0) {
responseType = 5;
}
responseComponents = [[self parseResponse] retain];
}
return self;
}
- (void) dealloc {
[responseLines release];
[responseComponents release];
[super dealloc];
}
-(NSDictionary *)parseResponse {
NSMutableDictionary *responseDetailsDictionary = [[NSMutableDictionary new] autorelease];
if (responseType==1) {
[responseDetailsDictionary setObject:@"OK" forKey:Handshake_ResponseTypeString];
[responseDetailsDictionary setObject:[responseLines objectAtIndex:1] forKey:Handshake_SessionKey];
[responseDetailsDictionary setObject:[NSURL URLWithString:[responseLines objectAtIndex:2]] forKey:Handshake_NowPlayingURL];
// [responseDetailsDictionary setObject:[NSURL URLWithString:@"http://scrobblepod.sourceforge.net/timeout.php?time=25"] forKey:Handshake_PostURL];
[responseDetailsDictionary setObject:[NSURL URLWithString:[responseLines objectAtIndex:3]] forKey:Handshake_PostURL];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:NO] forKey:Handshake_DidFail];
} else if (responseType==2) {
[responseDetailsDictionary setObject:@"BANNED" forKey:Handshake_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:YES] forKey:Handshake_DidFail];
[responseDetailsDictionary setObject:@"Client is banned from accessing audioscrobbler webservice" forKey:Handshake_FailureReason];
} else if (responseType==3) {
[responseDetailsDictionary setObject:@"BADAUTH" forKey:Handshake_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:YES] forKey:Handshake_DidFail];
[responseDetailsDictionary setObject:@"Authentication details provided were incorrect" forKey:Handshake_FailureReason];
} else if (responseType==4) {
[responseDetailsDictionary setObject:@"BADTIME" forKey:Handshake_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:YES] forKey:Handshake_DidFail];
[responseDetailsDictionary setObject:@"The timestamp provided was not close enough to the current time" forKey:Handshake_FailureReason];
} else if (responseType==5) {
[responseDetailsDictionary setObject:@"FAILED" forKey:Handshake_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:YES] forKey:Handshake_DidFail];
[responseDetailsDictionary setObject:[[responseLines objectAtIndex:0] substringFromIndex:7] forKey:Handshake_FailureReason];
} else if (responseType==0) {
[responseDetailsDictionary setObject:@"UNKNOWNRESPONSE" forKey:Handshake_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:YES] forKey:Handshake_DidFail];
[responseDetailsDictionary setObject:@"Handshake response could not be parsed" forKey:Handshake_FailureReason];
}
return responseDetailsDictionary;
}
-(NSString *)sessionKey {
return [responseComponents objectForKey:Handshake_SessionKey];
}
-(NSString *)failureReason {
return [responseComponents objectForKey:Handshake_FailureReason];
}
-(BOOL)didFail {
return [[responseComponents objectForKey:Handshake_DidFail] boolValue];
}
-(void)setDidFail:(BOOL)aBool {
[responseComponents setObject:[NSNumber numberWithBool:YES] forKey:Handshake_DidFail];
}
-(int)responseType {
return responseType;
}
-(NSURL *)nowPlayingURL {
return [responseComponents objectForKey:Handshake_NowPlayingURL];
}
-(NSURL *)postURL {
if (Use_Timeout_Url) return [NSURL URLWithString:@"http://scrobblepod.sourceforge.net/timeout.php?time=25"];
return [responseComponents objectForKey:Handshake_PostURL];
}
-(NSString *)description {
return [NSString stringWithFormat:@"%@",responseComponents];
}
@end