| 1 |
// |
| 2 |
// POPRequest.m |
| 3 |
// |
| 4 |
// Created by Hori,Masaki on Tue Nov 26 2002. |
| 5 |
// |
| 6 |
|
| 7 |
#import "POPRequest.h" |
| 8 |
|
| 9 |
enum { |
| 10 |
statusPOPConnect, |
| 11 |
statusPOPAuthentication, |
| 12 |
statusPOPReadUID, |
| 13 |
sratusPOPReadHeaders, |
| 14 |
statusPOPReadMail, |
| 15 |
statsuPOPDeleteMail, |
| 16 |
statusPreTerminate, |
| 17 |
statusPOPQuit, |
| 18 |
|
| 19 |
statusPOPTerminate, |
| 20 |
}; |
| 21 |
|
| 22 |
@interface POPRequest (Private) |
| 23 |
-(void)connect; |
| 24 |
-(void)authentication; |
| 25 |
-(void)readUIDAndMakeArrayOfMustFetchHeader; |
| 26 |
-(void)readHeadersAndMakeArrayOfMustFetchMail; |
| 27 |
-(void)readMailsAndMakeArrayOfDeleteMail; |
| 28 |
-(void)deleteMails; |
| 29 |
-(void)quitOfPOPAccess; |
| 30 |
@end |
| 31 |
|
| 32 |
@implementation POPRequest |
| 33 |
|
| 34 |
-(void)dealloc |
| 35 |
{ |
| 36 |
[account release]; |
| 37 |
[popEngine release]; |
| 38 |
[workingArray release]; |
| 39 |
|
| 40 |
[super dealloc]; |
| 41 |
} |
| 42 |
|
| 43 |
-(void)nextPhase |
| 44 |
{ |
| 45 |
NSLock* lock = [[NSLock alloc] init]; |
| 46 |
|
| 47 |
[lock lock]; |
| 48 |
{ |
| 49 |
if( status < statusPOPTerminate ) { |
| 50 |
status++; |
| 51 |
} |
| 52 |
} |
| 53 |
[lock unlock]; |
| 54 |
[lock release]; |
| 55 |
} |
| 56 |
|
| 57 |
-(void)setPOPEngineClass:(Class)aClass |
| 58 |
{ |
| 59 |
NSLog(@"PrototypePOPThreadServer setEngineClass:"); |
| 60 |
popEngineClass = aClass; |
| 61 |
} |
| 62 |
|
| 63 |
-(void)setAccountInfo:(id <AccountInfo>)aAccount |
| 64 |
{ |
| 65 |
NSLog(@"PrototypePOPThreadServer setAccount:"); |
| 66 |
if(account) { |
| 67 |
[account release]; |
| 68 |
account = nil; |
| 69 |
} |
| 70 |
account = [aAccount retain]; |
| 71 |
} |
| 72 |
|
| 73 |
-(void)setDelegate:(id)delegate |
| 74 |
{ |
| 75 |
_delegate = delegate; |
| 76 |
} |
| 77 |
|
| 78 |
-(oneway void)operate |
| 79 |
{ |
| 80 |
[account setStatus:accountInfoConnecting]; |
| 81 |
|
| 82 |
while(YES) { |
| 83 |
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
| 84 |
|
| 85 |
switch(status) { |
| 86 |
case statusPOPConnect: |
| 87 |
[self connect]; |
| 88 |
break; |
| 89 |
case statusPOPAuthentication: |
| 90 |
[self authentication]; |
| 91 |
break; |
| 92 |
case statusPOPReadUID: |
| 93 |
[self readUIDAndMakeArrayOfMustFetchHeader]; |
| 94 |
break; |
| 95 |
case sratusPOPReadHeaders: |
| 96 |
[self readHeadersAndMakeArrayOfMustFetchMail]; |
| 97 |
break; |
| 98 |
case statusPOPReadMail: |
| 99 |
[self readMailsAndMakeArrayOfDeleteMail]; |
| 100 |
break; |
| 101 |
case statsuPOPDeleteMail: |
| 102 |
[self deleteMails]; |
| 103 |
break; |
| 104 |
case statusPreTerminate: |
| 105 |
[self nextPhase]; |
| 106 |
break; |
| 107 |
case statusPOPQuit: |
| 108 |
[self quitOfPOPAccess]; |
| 109 |
break; |
| 110 |
case statusPOPTerminate: |
| 111 |
// |
| 112 |
[account setStatus:accountInfoNone]; |
| 113 |
[pool release]; |
| 114 |
return; |
| 115 |
default: |
| 116 |
// |
| 117 |
[account setStatus:accountInfoNone]; |
| 118 |
[pool release]; |
| 119 |
return; |
| 120 |
} |
| 121 |
[pool release]; |
| 122 |
} |
| 123 |
[account setStatus:accountInfoNone]; |
| 124 |
} |
| 125 |
|
| 126 |
-(oneway void)terminate |
| 127 |
{ |
| 128 |
NSLock* lock = [[NSLock alloc] init]; |
| 129 |
|
| 130 |
[lock lock]; |
| 131 |
{ |
| 132 |
status = statusPreTerminate; |
| 133 |
NSLog(@"Request Catch Terminate!"); |
| 134 |
} |
| 135 |
[lock unlock]; |
| 136 |
[lock release]; |
| 137 |
} |
| 138 |
|
| 139 |
@end |
| 140 |
|
| 141 |
|
| 142 |
@implementation POPRequest (Private) |
| 143 |
|
| 144 |
-(void)connect |
| 145 |
{ |
| 146 |
NSLog(@"PrototypePOPThreadServer connect"); |
| 147 |
|
| 148 |
if( ![popEngineClass conformsToProtocol:@protocol(POPEngine)] ) { |
| 149 |
[self terminate]; |
| 150 |
NSLog(@"POPEngine does not conform Protocol POPEngine"); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
if( popEngine && ![popEngine isMemberOfClass:popEngineClass] ) { |
| 155 |
[popEngine release]; |
| 156 |
popEngine = nil; |
| 157 |
} |
| 158 |
|
| 159 |
if( !popEngine ) { |
| 160 |
popEngine = [[popEngineClass allocWithZone:[self zone]] init]; |
| 161 |
} |
| 162 |
|
| 163 |
if( !popEngine ) { |
| 164 |
[self terminate]; |
| 165 |
NSLog(@"Can not allocate POPEngine"); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
[popEngine setHostName:[account host]]; |
| 170 |
|
| 171 |
// Notification |
| 172 |
/***************/ |
| 173 |
/***************/ |
| 174 |
if( ![popEngine connect] ) { |
| 175 |
// Delegation |
| 176 |
if([_delegate isRetryConnect] ) { |
| 177 |
return; |
| 178 |
} else { |
| 179 |
NSLog(@"Missing Host Name Of POP server"); |
| 180 |
[self terminate]; |
| 181 |
return; |
| 182 |
} |
| 183 |
} else { |
| 184 |
[self nextPhase]; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
-(void)authentication |
| 189 |
{ |
| 190 |
NSLog(@"PrototypePOPThreadServer authentication"); |
| 191 |
|
| 192 |
[popEngine setAccount:[account account]]; |
| 193 |
[popEngine setPassword:[account password]]; |
| 194 |
|
| 195 |
// Notification |
| 196 |
/***************/ |
| 197 |
/***************/ |
| 198 |
if( ![popEngine authentication] ) { |
| 199 |
// Delegation |
| 200 |
if( [_delegate isRetryAuthentication] ) { |
| 201 |
if( ![popEngine isConnected] ) { |
| 202 |
[popEngine disconnect]; |
| 203 |
status = statusPOPConnect; |
| 204 |
} |
| 205 |
return; |
| 206 |
} else { |
| 207 |
NSLog(@"Missing Account Info of POP server"); |
| 208 |
[self terminate]; |
| 209 |
return; |
| 210 |
} |
| 211 |
} else { |
| 212 |
[self nextPhase]; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
-(void)readUIDAndMakeArrayOfMustFetchHeader |
| 217 |
{ |
| 218 |
NSArray* messageID; |
| 219 |
|
| 220 |
NSLog(@"PrototypePOPThreadServer readUIDAndMakeArrayOfMustFetchHeader"); |
| 221 |
|
| 222 |
messageID = [popEngine messageIDs]; |
| 223 |
if( messageID ) { |
| 224 |
workingArray = [[_delegate makeMustFetchHeader:messageID] retain]; |
| 225 |
} |
| 226 |
|
| 227 |
[self nextPhase]; |
| 228 |
} |
| 229 |
|
| 230 |
-(void)readHeadersAndMakeArrayOfMustFetchMail |
| 231 |
{ |
| 232 |
NSMutableArray* fetchMails = [[NSMutableArray allocWithZone:[self zone]] init]; |
| 233 |
NSEnumerator* fetchHeaderNumberEnum; |
| 234 |
NSNumber* fetchHeadersNumber; |
| 235 |
|
| 236 |
NSLog(@"PrototypePOPThreadServer readHeadersAndMakeArrayOfMustFetchMail"); |
| 237 |
|
| 238 |
fetchHeaderNumberEnum = [workingArray objectEnumerator]; |
| 239 |
while( status != statusPreTerminate && (fetchHeadersNumber = [fetchHeaderNumberEnum nextObject]) ) { |
| 240 |
NSData* header = [popEngine headerAtPosition:[fetchHeadersNumber unsignedIntValue]]; |
| 241 |
|
| 242 |
if( [_delegate isFetchMailHasHeader:header] ) { |
| 243 |
[fetchMails addObject:fetchHeadersNumber]; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
[workingArray release]; |
| 248 |
workingArray = fetchMails; |
| 249 |
|
| 250 |
[self nextPhase]; |
| 251 |
} |
| 252 |
|
| 253 |
-(void)readMailsAndMakeArrayOfDeleteMail |
| 254 |
{ |
| 255 |
NSMutableArray* deleteMails = [[NSMutableArray allocWithZone:[self zone]] init]; |
| 256 |
NSEnumerator* fetchMailsNumberEnum = [workingArray objectEnumerator]; |
| 257 |
NSNumber* fetchMailsNumber; |
| 258 |
|
| 259 |
NSLog(@"PrototypePOPThreadServer readMailsAndMakeArrayOfDeleteMail"); |
| 260 |
|
| 261 |
while ( status != statusPreTerminate && (fetchMailsNumber = [fetchMailsNumberEnum nextObject]) ) { |
| 262 |
// Notification |
| 263 |
/***************/ |
| 264 |
/***************/ |
| 265 |
NSData* mail = [popEngine mailAtPosition:[fetchMailsNumber unsignedIntValue]]; |
| 266 |
|
| 267 |
if( [_delegate addMailAndIsDelete:mail] ) { |
| 268 |
[deleteMails addObject:fetchMailsNumber]; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
[workingArray release]; |
| 273 |
workingArray = deleteMails; |
| 274 |
|
| 275 |
[self nextPhase]; |
| 276 |
} |
| 277 |
|
| 278 |
-(void)deleteMails |
| 279 |
{ |
| 280 |
NSEnumerator* deleteMailsEnum = [workingArray objectEnumerator]; |
| 281 |
NSNumber* deleteMailNumber; |
| 282 |
|
| 283 |
NSLog(@"PrototypePOPThreadServer deleteMails"); |
| 284 |
|
| 285 |
while( status != statusPreTerminate && (deleteMailNumber = [deleteMailsEnum nextObject]) ) { |
| 286 |
// Notification |
| 287 |
/***************/ |
| 288 |
/***************/ |
| 289 |
[popEngine deleteMailAtPosition:[deleteMailNumber unsignedIntValue]]; |
| 290 |
} |
| 291 |
|
| 292 |
[workingArray release]; |
| 293 |
workingArray = nil; |
| 294 |
|
| 295 |
[self nextPhase]; |
| 296 |
} |
| 297 |
|
| 298 |
-(void)quitOfPOPAccess |
| 299 |
{ |
| 300 |
NSLog(@"PrototypePOPThreadServer quitOfPOPAccess"); |
| 301 |
[popEngine disconnect]; |
| 302 |
|
| 303 |
[self nextPhase]; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
@end |
| 308 |
|