]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Mailer/SOGoMailBaseObject.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1071 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / Mailer / SOGoMailBaseObject.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "SOGoMailBaseObject.h"
23 #include "SOGoMailManager.h"
24 #include "common.h"
25 #include <NGObjWeb/SoObject+SoDAV.h>
26 #include <NGObjWeb/SoHTTPAuthenticator.h>
27 #include <NGExtensions/NSURL+misc.h>
28
29 @implementation SOGoMailBaseObject
30
31 + (int)version {
32   return [super version] + 1 /* v1 */;
33 }
34 + (void)initialize {
35   NSAssert2([super version] == 0,
36             @"invalid superclass (%@) version %i !",
37             NSStringFromClass([self superclass]), [super version]);
38 }
39
40 #if 0
41 static BOOL debugOn = YES;
42 #endif
43
44 - (id)initWithImap4URL:(NSURL *)_url inContainer:(id)_container {
45   NSString *n;
46   
47   n = [[_url path] lastPathComponent];
48   if ((self = [self initWithName:n inContainer:_container])) {
49     self->imap4URL = [_url retain];
50   }
51   return self;
52 }
53
54 - (void)dealloc {
55   [self->imap4URL release];
56   [super dealloc];
57 }
58
59 /* hierarchy */
60
61 - (SOGoMailAccount *)mailAccountFolder {
62   if (![[self container] respondsToSelector:_cmd]) {
63     [self warnWithFormat:@"weird container of mailfolder: %@",
64             [self container]];
65     return nil;
66   }
67   
68   return [[self container] mailAccountFolder];
69 }
70
71 - (SOGoMailAccounts *)mailAccountsFolder {
72   id o;
73   
74   for (o = [self container]; [o isNotNull]; o = [o container]) {
75     if ([o isKindOfClass:NSClassFromString(@"SOGoMailAccounts")])
76       return o;;
77   }
78   return nil;
79 }
80
81 /* IMAP4 */
82
83 - (NGImap4ConnectionManager *)mailManager {
84   return [NGImap4ConnectionManager defaultConnectionManager];
85 }
86 - (NGImap4Connection *)imap4Connection {
87   if (self->imap4 == nil) {
88     self->imap4 = [[[self mailManager] connectionForURL:[self imap4URL] 
89                                        password:[self imap4Password]] retain];
90     if (self->imap4 == nil) {
91       [self errorWithFormat:@"Could not connect IMAP4."];
92       self->imap4 = [[NSNull null] retain];
93     }
94   }
95   return [self->imap4 isNotNull] ? self->imap4 : nil;
96 }
97
98 - (NSString *)relativeImap4Name {
99   [self warnWithFormat:@"subclass should override %@", 
100           NSStringFromSelector(_cmd)];
101   return nil;
102 }
103 - (NSURL *)baseImap4URL {
104   if (![[self container] respondsToSelector:@selector(imap4URL)]) {
105     [self warnWithFormat:@"container does not implement -imap4URL!"];
106     return nil;
107   }
108   
109   return [[self container] imap4URL];
110 }
111 - (NSURL *)imap4URL {
112   NSString *sn;
113   NSURL    *base;
114   
115   if (self->imap4URL != nil) 
116     return self->imap4URL;
117   
118   if ((sn = [self relativeImap4Name]) == nil)
119     return nil;
120   
121   if (![[self container] respondsToSelector:_cmd]) {
122     [self warnWithFormat:@"container does not implement -imap4URL!"];
123     return nil;
124   }
125   
126   if ((base = [self baseImap4URL]) == nil)
127     return nil;
128   
129   sn = [[base path] stringByAppendingPathComponent:sn];
130   self->imap4URL = [[NSURL alloc] initWithString:sn relativeToURL:base];
131   return self->imap4URL;
132 }
133
134 - (NSString *)imap4Login {
135   if (![[self container] respondsToSelector:_cmd])
136     return nil;
137   
138   return [[self container] imap4Login];
139 }
140 - (NSString *)imap4Password {
141   /*
142     Extract password from basic authentication.
143     
144     TODO: we might want to
145     a) move the primary code to SOGoMailAccount
146     b) cache the password
147   */
148   WORequest *rq;
149   NSString  *auth;
150   NSArray   *creds;
151   
152   rq = [context request];
153   if ((auth = [rq headerForKey:@"authorization"]) == nil) {
154     /* no basic auth */
155     return nil;
156   }
157   
158   creds = [SoHTTPAuthenticator parseCredentials:auth];
159   if ([creds count] < 2)
160     /* somehow invalid */
161     return nil;
162   
163   return [creds objectAtIndex:1]; /* the password */
164 }
165
166 - (void)flushMailCaches {
167   [[self mailManager] flushCachesForURL:[self imap4URL]];
168 }
169
170 /* IMAP4 names */
171
172 - (BOOL)isBodyPartKey:(NSString *)_key inContext:(id)_ctx {
173   /*
174     Every key starting with a digit is consider an IMAP4 mime part key, used in
175     SOGoMailObject and SOGoMailBodyPart.
176   */
177   if ([_key length] == 0)
178     return NO;
179   
180   if (isdigit([_key characterAtIndex:0]))
181     return YES;
182   
183   return NO;
184 }
185
186 - (NSArray *) aclsForUser: (NSString *) uid
187 {
188   return nil;
189 }
190
191 @end /* SOGoMailBaseObject */