1/* SOGoCredentialsFile.m - this file is part of SOGo
2 *
3 * Copyright (C) 2013 Inverse inc.
4 *
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This file is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; see the file COPYING.  If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21#import <Foundation/NSCharacterSet.h>
22#import <Foundation/NSData.h>
23#import <Foundation/NSString.h>
24
25#import "SOGoCredentialsFile.h"
26
27@implementation SOGoCredentialsFile
28
29+ (id) credentialsFromFile: (NSString *) file
30{
31  SOGoCredentialsFile *newCreds;
32  newCreds = [[self  alloc] initFromFile: file
33                            withEncoding: NSUTF8StringEncoding];
34  [newCreds autorelease];
35  return newCreds;
36}
37
38
39- (id) init
40{
41  if ((self = [super init]))
42    {
43      _username = nil;
44      _password = nil;
45      _credentialsFile = nil;
46    }
47  return self;
48}
49
50- (void) dealloc
51{
52  [_username release];
53  [_password release];
54  [_credentialsFile release];
55  [super dealloc];
56}
57
58- (id) initFromFile: (NSString *) file
59       withEncoding: (NSStringEncoding) enc
60{
61  id ret;
62  NSData *credentialsData;
63  NSRange r;
64  NSString *creds;
65
66  ret = nil;
67  if (file)
68    {
69      if ((self = [self init]))
70        {
71          credentialsData = [NSData dataWithContentsOfFile: file];
72          if (credentialsData == nil)
73            NSLog(@"Failed to load credentials file: %@", file);
74          else
75            {
76              creds = [[NSString alloc] initWithData: credentialsData
77                                            encoding: enc];
78              [creds autorelease];
79              creds = [creds stringByTrimmingCharactersInSet:
80                [NSCharacterSet characterSetWithCharactersInString: @"\r\n"]];
81              r = [creds rangeOfString: @":"];
82              if (r.location == NSNotFound)
83                NSLog(@"Invalid credentials file content, missing ':' separator (%@)", file);
84              else
85                {
86                  _username = [[creds substringToIndex: r.location] retain];
87                  _password = [[creds substringFromIndex: r.location+1] retain];
88                  _credentialsFile = [file retain];
89                  ret = self;
90                }
91            }
92        }
93    }
94  return ret;
95}
96
97
98- (NSString *) username
99{
100  return self->_username;
101}
102
103- (NSString *) password
104{
105  return self->_password;
106}
107
108- (NSString *) credentialsFile
109{
110  return self->_credentialsFile;
111}
112
113@end
114