root/WiredServer/trunk/WCAccounts.m

Revision 4496, 10.8 kB (checked in by morris, 2 years ago)

Fix 64-bit warnings

  • Property svn:keywords set to author date id revision
Line 
1 /* $Id$ */
2
3 /*
4  *  Copyright (c) 2003-2006 Axel Andersson
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "WCAccounts.h"
30
31 @implementation WCAccount
32
33 - (id)initWithType:(WCAccountType)type {
34         self = [super init];
35        
36         _fields = [[NSMutableArray alloc] init];
37         _type = type;
38        
39         return self;
40 }
41
42
43
44 - (id)initWithRecord:(NSString *)record type:(WCAccountType)type {
45         self = [self initWithType:type];
46        
47         [_fields setArray:[record componentsSeparatedByString:@":"]];
48        
49         if([self type] == WCAccountGroup) {
50                 [_fields insertObject:@"" atIndex:1];
51                 [_fields insertObject:@"" atIndex:1];
52         }
53        
54         return self;
55 }
56
57
58
59 - (id)initWithName:(NSString *)name type:(WCAccountType)type {
60         return [self initWithRecord:[NSSWF:@"%@:::1:0:1:0:1:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0", name]
61                                                    type:type];
62 }
63
64
65
66 - (void)dealloc {
67         [_fields release];
68
69         [super dealloc];
70 }
71
72
73
74 #pragma mark -
75
76 - (NSString *)description {
77         NSMutableArray  *fields;
78        
79         fields = [[_fields mutableCopy] autorelease];
80        
81         if([self type] == WCAccountGroup) {
82                 [fields removeObjectAtIndex:1];
83                 [fields removeObjectAtIndex:1];
84         }
85
86         return [fields componentsJoinedByString:@":"];
87 }
88
89
90
91 #pragma mark -
92
93 - (void)setType:(WCAccountType)type {
94         _type = type;
95 }
96
97
98
99 - (WCAccountType)type {
100         return _type;
101 }
102
103
104
105 - (void)setName:(NSString *)name {
106         [_fields replaceObjectAtIndex:0 withObject:name];
107 }
108
109
110
111 - (NSString *)name {
112         return [_fields stringAtIndex:0];
113 }
114
115
116
117 - (void)setPassword:(NSString *)password {
118         if([self type] == WCAccountUser) {
119                 if([password length] > 0 && ![password isEqualToString:[self password]])
120                         password = [password SHA1];
121                
122                 [_fields replaceObjectAtIndex:1 withObject:password];
123         }
124 }
125
126
127
128 - (NSString *)password {
129         if([self type] == WCAccountGroup)
130                 return @"";
131        
132         return [_fields stringAtIndex:1];
133 }
134
135
136
137 - (void)setGroup:(NSString *)name {
138         if([self type] == WCAccountUser)
139                 [_fields replaceObjectAtIndex:2 withObject:name];
140 }
141
142
143
144 - (NSString *)group {
145         if([self type] == WCAccountGroup)
146                 return @"";
147
148         return [_fields stringAtIndex:2];
149 }
150
151
152
153 - (void)setGetUserInfo:(BOOL)value {
154         [_fields replaceObjectAtIndex:3 withObject:value ? @"1" : @"0"];
155 }
156
157
158
159 - (BOOL)getUserInfo {
160         return [[_fields stringAtIndex:3] isEqualToString:@"1"];
161 }
162
163
164
165 - (void)setBroadcast:(BOOL)value {
166         [_fields replaceObjectAtIndex:4 withObject:value ? @"1" : @"0"];
167 }
168
169
170
171 - (BOOL)broadcast {
172         return [[_fields stringAtIndex:4] isEqualToString:@"1"];
173 }
174
175
176
177 - (void)setPostNews:(BOOL)value {
178         [_fields replaceObjectAtIndex:5 withObject:value ? @"1" : @"0"];
179 }
180
181
182
183 - (BOOL)postNews {
184         return [[_fields stringAtIndex:5] isEqualToString:@"1"];
185 }
186
187
188
189 - (void)setClearNews:(BOOL)value {
190         [_fields replaceObjectAtIndex:6 withObject:value ? @"1" : @"0"];
191 }
192
193
194
195 - (BOOL)clearNews {
196         return [[_fields stringAtIndex:6] isEqualToString:@"1"];
197 }
198
199
200
201 - (void)setDownload:(BOOL)value {
202         [_fields replaceObjectAtIndex:7 withObject:value ? @"1" : @"0"];
203 }
204
205
206
207 - (BOOL)download {
208         return [[_fields stringAtIndex:7] isEqualToString:@"1"];
209 }
210
211
212
213 - (void)setUpload:(BOOL)value {
214         [_fields replaceObjectAtIndex:8 withObject:value ? @"1" : @"0"];
215 }
216
217
218
219 - (BOOL)upload {
220         return [[_fields stringAtIndex:8] isEqualToString:@"1"];
221 }
222
223
224
225 - (void)setUploadAnywhere:(BOOL)value {
226         [_fields replaceObjectAtIndex:9 withObject:value ? @"1" : @"0"];
227 }
228
229
230
231 - (BOOL)uploadAnywhere {
232         return [[_fields stringAtIndex:9] isEqualToString:@"1"];
233 }
234
235
236
237 - (void)setCreateFolders:(BOOL)value {
238         [_fields replaceObjectAtIndex:10 withObject:value ? @"1" : @"0"];
239 }
240
241
242
243 - (BOOL)createFolders {
244         return [[_fields stringAtIndex:10] isEqualToString:@"1"];
245 }
246
247
248
249 - (void)setMoveFiles:(BOOL)value {
250         [_fields replaceObjectAtIndex:11 withObject:value ? @"1" : @"0"];
251 }
252
253
254
255 - (BOOL)moveFiles {
256         return [[_fields stringAtIndex:11] isEqualToString:@"1"];
257 }
258
259
260
261 - (void)setDeleteFiles:(BOOL)value {
262         [_fields replaceObjectAtIndex:12 withObject:value ? @"1" : @"0"];
263 }
264
265
266
267 - (BOOL)deleteFiles {
268         return [[_fields stringAtIndex:12] isEqualToString:@"1"];
269 }
270
271
272
273 - (void)setViewDropBoxes:(BOOL)value {
274         [_fields replaceObjectAtIndex:13 withObject:value ? @"1" : @"0"];
275 }
276
277
278
279 - (BOOL)viewDropBoxes {
280         return [[_fields stringAtIndex:13] isEqualToString:@"1"];
281 }
282
283
284
285 - (void)setCreateAccounts:(BOOL)value {
286         [_fields replaceObjectAtIndex:14 withObject:value ? @"1" : @"0"];
287 }
288
289
290
291 - (BOOL)createAccounts {
292         return [[_fields stringAtIndex:14] isEqualToString:@"1"];
293 }
294
295
296
297 - (void)setEditAccounts:(BOOL)value {
298         [_fields replaceObjectAtIndex:15 withObject:value ? @"1" : @"0"];
299 }
300
301
302
303 - (BOOL)editAccounts {
304         return [[_fields stringAtIndex:15] isEqualToString:@"1"];
305 }
306
307
308
309 - (void)setDeleteAccounts:(BOOL)value {
310         [_fields replaceObjectAtIndex:16 withObject:value ? @"1" : @"0"];
311 }
312
313
314
315 - (BOOL)deleteAccounts {
316         return [[_fields stringAtIndex:16] isEqualToString:@"1"];
317 }
318
319
320
321 - (void)setElevatePrivileges:(BOOL)value {
322         [_fields replaceObjectAtIndex:17 withObject:value ? @"1" : @"0"];
323 }
324
325
326
327 - (BOOL)elevatePrivileges {
328         return [[_fields stringAtIndex:17] isEqualToString:@"1"];
329 }
330
331
332
333 - (void)setKickUsers:(BOOL)value {
334         [_fields replaceObjectAtIndex:18 withObject:value ? @"1" : @"0"];
335 }
336
337
338
339 - (BOOL)kickUsers {
340         return [[_fields stringAtIndex:18] isEqualToString:@"1"];
341 }
342
343
344
345 - (void)setBanUsers:(BOOL)value {
346         [_fields replaceObjectAtIndex:19 withObject:value ? @"1" : @"0"];
347 }
348
349
350
351 - (BOOL)banUsers {
352         return [[_fields stringAtIndex:19] isEqualToString:@"1"];
353 }
354
355
356
357 - (void)setCannotBeKicked:(BOOL)value {
358         [_fields replaceObjectAtIndex:20 withObject:value ? @"1" : @"0"];
359 }
360
361
362
363 - (BOOL)cannotBeKicked {
364         return [[_fields stringAtIndex:20] isEqualToString:@"1"];
365 }
366
367
368
369 - (void)setDownloadSpeed:(int)value {
370         [_fields replaceObjectAtIndex:21 withObject:[NSSWF:@"%d", value]];
371 }
372
373
374
375 - (int)downloadSpeed {
376         return [[_fields stringAtIndex:21] intValue];
377 }
378
379
380
381 - (void)setUploadSpeed:(int)value {
382         [_fields replaceObjectAtIndex:22 withObject:[NSSWF:@"%d", value]];
383 }
384
385
386
387 - (int)uploadSpeed {
388         return [[_fields stringAtIndex:22] intValue];
389 }
390
391
392
393 - (void)setDownloads:(int)value {
394         [_fields replaceObjectAtIndex:23 withObject:[NSSWF:@"%d", value]];
395 }
396
397
398
399 - (int)downloads {
400         return [[_fields stringAtIndex:23] intValue];
401 }
402
403
404
405 - (void)setUploads:(int)value {
406         [_fields replaceObjectAtIndex:24 withObject:[NSSWF:@"%d", value]];
407 }
408
409
410
411 - (int)uploads {
412         return [[_fields stringAtIndex:24] intValue];
413 }
414
415
416
417 - (void)setSetTopic:(BOOL)value {
418         [_fields replaceObjectAtIndex:25 withObject:value ? @"1" : @"0"];
419 }
420
421
422
423 - (BOOL)setTopic {
424         return [[_fields stringAtIndex:25] isEqualToString:@"1"];
425 }
426
427
428
429 #pragma mark -
430
431 - (NSComparisonResult)compareName:(WCAccount *)other {
432         return [[self name] compare:[other name] options:NSCaseInsensitiveSearch | NSNumericSearch];
433 }
434
435 @end
436
437
438
439 @implementation WCAccounts
440
441 - (id)init {
442         self = [super init];
443        
444         _accounts = [[NSMutableArray alloc] init];
445         _lock = [[NSRecursiveLock alloc] init];
446        
447         _dateFormatter = [[WIDateFormatter alloc] init];
448         [_dateFormatter setTimeStyle:NSDateFormatterShortStyle];
449         [_dateFormatter setDateStyle:NSDateFormatterShortStyle];
450        
451         return self;
452 }
453
454
455
456 - (id)initWithString:(NSString *)string type:(WCAccountType)type {
457         NSEnumerator    *enumerator;
458         NSString                *line;
459         WCAccount               *account;
460        
461         self = [self init];
462        
463         enumerator = [[string componentsSeparatedByString:@"\n"] objectEnumerator];
464        
465         while((line = [enumerator nextObject])) {
466                 if([line hasPrefix:@"#"] || [line length] == 0)
467                         continue;
468                
469                 account = [[WCAccount alloc] initWithRecord:line type:type];
470                
471                 if(account) {
472                         [_accounts addObject:account];
473                         [account release];
474                 }
475         }
476        
477         return self;
478 }
479
480
481
482 - (id)initWithData:(NSData *)data type:(WCAccountType)type {
483         return [self initWithString:[NSString stringWithData:data encoding:NSUTF8StringEncoding] type:type];
484 }
485
486
487
488 - (id)initWithContentsOfFile:(NSString *)file type:(WCAccountType)type {
489         return [self initWithData:[NSData dataWithContentsOfFile:file] type:type];
490 }
491
492
493
494 - (id)initWithContentsOfURL:(NSURL *)url type:(WCAccountType)type {
495         return [self initWithData:[NSData dataWithContentsOfURL:url] type:type];
496 }
497
498
499
500 - (void)dealloc {
501         [_accounts release];
502         [_lock release];
503         [_dateFormatter release];
504        
505         [super dealloc];
506 }
507
508
509
510 #pragma mark -
511
512 - (void)addAccount:(WCAccount *)account {
513         [_lock lock];
514         [_accounts addObject:account];
515         [_lock unlock];
516 }
517
518
519
520 - (void)deleteAccount:(WCAccount *)account {
521         [_lock lock];
522         [_accounts removeObject:account];
523         [_lock unlock];
524 }
525
526
527
528 #pragma mark -
529
530 - (NSUInteger)count {
531         return [_accounts count];
532 }
533
534
535
536 - (NSArray *)accounts {
537         return _accounts;
538 }
539
540
541
542 - (WCAccount *)accountWithName:(NSString *)name {
543         NSEnumerator    *enumerator;
544         WCAccount               *account, *result = NULL;
545        
546         [_lock lock];
547        
548         enumerator = [[self accounts] objectEnumerator];
549        
550         while((account = [enumerator nextObject])) {
551                 if([[account name] isEqualToString:name]) {
552                         result = account;
553                        
554                         break;
555                 }
556         }
557        
558         [_lock unlock];
559        
560         return result;
561 }
562
563
564
565 #pragma mark -
566
567 - (BOOL)writeToFile:(NSString *)file {
568         NSEnumerator            *enumerator;
569         NSMutableString         *string;
570         WCAccount                       *account;
571        
572         if(![[NSFileManager defaultManager] fileExistsAtPath:file]) {
573                 [[NSFileManager defaultManager] createFileAtPath:file
574                                                                                                 contents:NULL
575                                                                                           attributes:NULL];
576         }
577        
578         string = [NSMutableString string];
579         [string appendFormat:WCLS(@"# This file was generated by %@ at %@", @"File comment (application, date)"),
580                 [[self bundle] objectForInfoDictionaryKey:@"CFBundleExecutable"],
581                 [_dateFormatter stringFromDate:[NSDate date]]];
582         [string appendString:@"\n"];
583        
584         enumerator = [[_accounts sortedArrayUsingSelector:@selector(compareName:)] objectEnumerator];
585        
586         while((account = [enumerator nextObject]))
587                 [string appendFormat:@"%@\n", [account description]];
588        
589         return [[string dataUsingEncoding:NSUTF8StringEncoding] writeToFile:file atomically:YES];
590 }
591
592
593
594 - (BOOL)writeToURL:(NSURL *)url {
595         if(![url isFileURL]) {
596                 NSLog(@"*** [%@ writeToURL:]: remote URLs not supported", self);
597                
598                 return NO;
599         }
600        
601         return [self writeToFile:[url path]];
602 }
603
604 @end
Note: See TracBrowser for help on using the browser.