File: SOGoCredentialsFile.m

package info (click to toggle)
sogo 5.12.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,828 kB
  • sloc: objc: 122,550; javascript: 75,288; sh: 2,352; ansic: 2,055; perl: 861; python: 777; sql: 307; makefile: 187; php: 43; xml: 27
file content (112 lines) | stat: -rw-r--r-- 2,911 bytes parent folder | download | duplicates (10)
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
/* SOGoCredentialsFile.m - this file is part of SOGo
 *
 * Copyright (C) 2013 Inverse inc.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSData.h>

#import "SOGoCredentialsFile.h"

@implementation SOGoCredentialsFile

+ (id) credentialsFromFile: (NSString *) file
{
  SOGoCredentialsFile *newCreds;
  newCreds = [[self  alloc] initFromFile: file
                            withEncoding: NSUTF8StringEncoding];
  [newCreds autorelease];
  return newCreds;
}


- (id) init
{
  if ((self = [super init]))
    {
      _username = nil;
      _password = nil;
      _credentialsFile = nil;
    }
  return self;
}

- (void) dealloc
{
  [_username release];
  [_password release];
  [_credentialsFile release];
  [super dealloc];
}

- (id) initFromFile: (NSString *) file
       withEncoding: (NSStringEncoding) enc
{
  id ret;
  NSData *credentialsData;
  NSRange r;
  NSString *creds;

  ret = nil;
  if (file)
    {
      if ((self = [self init]))
        {
          credentialsData = [NSData dataWithContentsOfFile: file];
          if (credentialsData == nil)
            NSLog(@"Failed to load credentials file: %@", file);
          else
            {
              creds = [[NSString alloc] initWithData: credentialsData
                                            encoding: enc];
              [creds autorelease];
              creds = [creds stringByTrimmingCharactersInSet: 
                [NSCharacterSet characterSetWithCharactersInString: @"\r\n"]];
              r = [creds rangeOfString: @":"];
              if (r.location == NSNotFound)
                NSLog(@"Invalid credentials file content, missing ':' separator (%@)", file);
              else
                {
                  _username = [[creds substringToIndex: r.location] retain];
                  _password = [[creds substringFromIndex: r.location+1] retain];
                  _credentialsFile = [file retain];
                  ret = self;
                }
            }
        }
    }
  return ret;
}


- (NSString *) username
{
  return self->_username;
}

- (NSString *) password
{
  return self->_password;
}

- (NSString *) credentialsFile
{
  return self->_credentialsFile;
}

@end