00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "LoginManager.h"
00010
00011
00012 @implementation LoginManager
00013
00014 bool serverReplyReceived = NO;
00015
00016 - (id) init {
00017 self = [super init];
00018 if (self != nil) {
00019 name = nil;
00020 pass = nil;
00021 state = LOGIN_GETNAME;
00022
00023
00024 [notificationCenter addObserver:self selector:@selector(serverReportsInvalidServer:)
00025 name:@"SP_LOGIN_INVALID_SERVER" object:nil];
00026 [notificationCenter addObserver:self selector:@selector(serverReportsLoginAccepted:)
00027 name:@"SP_LOGIN_ACCEPTED" object:nil];
00028 [notificationCenter addObserver:self selector:@selector(serverReportsLoginDenied:)
00029 name:@"SP_LOGIN_NOT_ACCEPTED" object:nil];
00030 }
00031 return self;
00032 }
00033
00034 -(int) state {
00035 return state;
00036 }
00037
00038
00039 -(void) reset {
00040 state = LOGIN_GETNAME;
00041 NSLog(@"LoginManager.reset state %d", state);
00042 }
00043
00044
00045 - (void) serverReportsInvalidServer: (id) me {
00046 [notificationCenter postNotificationName:@"LM_LOGIN_INVALID_SERVER" object:self userInfo:nil];
00047 state = LOGIN_GETNAME;
00048 }
00049
00050 - (void) serverReportsLoginAccepted: (id) me {
00051
00052 switch(state) {
00053 case LOGIN_GETNAME:
00054 state = LOGIN_GETPASS;
00055 if ([[name uppercaseString] isEqualToString:@"GUEST"]) {
00056 [self setPassword:nil];
00057 } else {
00058 [notificationCenter postNotificationName:@"LM_LOGIN_GET_PASSWORD" object:self userInfo:nil];
00059 }
00060 break;
00061 case LOGIN_GETPASS:
00062 case LOGIN_MAKEPASS1:
00063 case LOGIN_MAKEPASS2:
00064 case LOGIN_GUEST_LOGIN:
00065 case LOGIN_AUTOLOGIN_PASS:
00066 state = LOGIN_COMPLETE;
00067 [notificationCenter postNotificationName:@"LM_LOGIN_COMPLETE" object:self userInfo:nil];
00068 break;
00069 default:
00070 NSLog(@"LoginManager.serverReportsLoginAccepted state %d", state);
00071 break;
00072 }
00073 }
00074
00075 - (void) serverReportsLoginDenied: (id) me {
00076
00077 switch(state) {
00078 case LOGIN_GETNAME:
00079 state = LOGIN_MAKEPASS1;
00080 [notificationCenter postNotificationName:@"LM_LOGIN_MAKE_PASSWORD" object:self userInfo:nil];
00081 break;
00082 case LOGIN_AUTOLOGIN_NAME:
00083 state = LOGIN_GETNAME;
00084 break;
00085 case LOGIN_GETPASS:
00086 case LOGIN_MAKEPASS2:
00087 case LOGIN_GUEST_LOGIN:
00088 case LOGIN_AUTOLOGIN_PASS:
00089 state = LOGIN_BADPASS;
00090 [notificationCenter postNotificationName:@"LM_LOGIN_BAD_PASSWORD" object:self userInfo:nil];
00091 break;
00092 default:
00093 NSLog(@"LoginManager.serverReportsLoginDenied state %d", state);
00094 break;
00095 }
00096
00097 }
00098
00099
00100 - (void) setName:(NSString *)loginName {
00101
00102 if (state != LOGIN_GETNAME) {
00103 NSLog(@"LoginManager.setName strange ? did not expect state %d", state);
00104 state = LOGIN_GETNAME;
00105 }
00106
00107
00108 [name release];
00109 name = loginName;
00110 [name retain];
00111
00112
00113 bool checkName = YES;
00114 if ([name isEqualToString:@"Guest"] || [name isEqualToString:@"guest"]) {
00115 checkName = NO;
00116 }
00117
00118 [notificationCenter postNotificationName:@"COMM_SEND_LOGIN_REQ"
00119 object:nil
00120 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
00121 name, @"name",
00122 @"", @"pass",
00123 NSUserName(), @"login",
00124 [NSNumber numberWithInt:(checkName ? 1 : 0)], @"query",
00125 nil]];
00126 }
00127
00128 - (void) setPassword:(NSString *)loginPassword {
00129
00130
00131 [pass release];
00132 pass = loginPassword;
00133 [pass retain];
00134
00135
00136 [notificationCenter postNotificationName:@"COMM_SEND_LOGIN_REQ"
00137 object:nil
00138 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
00139 name, @"name",
00140 pass, @"pass",
00141 NSUserName(), @"login",
00142 (char) 0, @"query",
00143 nil]];
00144 }
00145
00146
00147 @end