]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Mailer/SOGoMailBaseObject.m
4ba1c62cadffbdce6c5ea50358831da8c23ec985
[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 #import <NGObjWeb/SoObject+SoDAV.h>
23 #import <NGObjWeb/SoHTTPAuthenticator.h>
24 #import <NGExtensions/NSNull+misc.h>
25 #import <NGExtensions/NSObject+Logs.h>
26 #import <NGExtensions/NSString+misc.h>
27 #import <NGExtensions/NSURL+misc.h>
28
29 #import <SoObjects/SOGo/SOGoAuthenticator.h>
30
31 #import "SOGoMailManager.h"
32
33 #import "SOGoMailBaseObject.h"
34
35 @implementation SOGoMailBaseObject
36
37 #if 0
38 static BOOL debugOn = YES;
39 #endif
40
41 - (id) initWithImap4URL: (NSURL *) _url
42             inContainer: (id) _container
43 {
44   NSString *n;
45   
46   n = [[_url path] lastPathComponent];
47   if ((self = [self initWithName: n inContainer:_container]))
48     {
49       imap4URL = [_url retain];
50     }
51
52   return self;
53 }
54
55 - (void) dealloc
56 {
57   [imap4URL release];
58   [super dealloc];
59 }
60
61 /* hierarchy */
62
63 - (SOGoMailAccount *) mailAccountFolder
64 {
65   SOGoMailAccount *folder;
66
67   if ([container respondsToSelector:_cmd])
68     folder = [container mailAccountFolder];
69   else
70     {
71       [self warnWithFormat: @"weird container of mailfolder: %@",
72             container];
73       folder = nil;
74     }
75   
76   return folder;
77 }
78
79 - (SOGoMailAccounts *) mailAccountsFolder
80 {
81   id o;
82   SOGoMailAccounts *folder;
83   Class folderClass;
84
85   folder = nil;
86
87   folderClass = NSClassFromString (@"SOGoMailAccounts");
88   o = container;
89   while (!folder && [o isNotNull])
90     if ([o isKindOfClass: folderClass])
91       folder = o;
92     else
93       o = [o container];
94
95   return o;
96 }
97
98 /* IMAP4 */
99
100 - (NGImap4ConnectionManager *) mailManager
101 {
102   return [NGImap4ConnectionManager defaultConnectionManager];
103 }
104
105 - (NGImap4Connection *) imap4Connection
106 {
107   if (!imap4)
108     {
109       imap4 = [[self mailManager] connectionForURL: [self imap4URL] 
110                                   password: [self imap4Password]];
111       if (imap4)
112         [imap4 retain];
113       else
114         [self errorWithFormat:@"Could not connect IMAP4."];
115     }
116
117   return imap4;
118 }
119
120 - (NSString *) relativeImap4Name
121 {
122   [self subclassResponsibility: _cmd];
123
124   return nil;
125 }
126
127 - (NSURL *) baseImap4URL
128 {
129   NSURL *url;
130
131   if ([container respondsToSelector:@selector(imap4URL)])
132     url = [container imap4URL];
133   else
134     {
135       [self warnWithFormat:@"container does not implement -imap4URL!"];
136       url = nil;
137     }
138   
139   return url;
140 }
141
142 - (NSMutableString *) imap4URLString
143 {
144   NSMutableString *urlString;
145
146   urlString = [container imap4URLString];
147   [urlString appendFormat: @"%@/", [nameInContainer stringByEscapingURL]];
148
149   return urlString;
150 }
151
152 - (NSURL *) imap4URL
153 {
154   /* this could probably be handled better from NSURL but it's buggy in
155      GNUstep */
156   if (!imap4URL)
157     {
158       imap4URL = [[NSURL alloc] initWithString: [self imap4URLString]];
159     }
160
161   return imap4URL;
162 }
163
164 - (NSString *) imap4Login
165 {
166   if (![container respondsToSelector:_cmd])
167     return nil;
168   
169   return [container imap4Login];
170 }
171
172 - (NSString *) imap4Password
173 {
174   /*
175     Extract password from basic authentication.
176     
177     TODO: we might want to
178     a) move the primary code to SOGoMailAccount
179     b) cache the password
180   */
181
182   return [[self authenticatorInContext: context] passwordInContext: context];
183 }
184
185 - (void)flushMailCaches {
186   [[self mailManager] flushCachesForURL:[self imap4URL]];
187 }
188
189 /* IMAP4 names */
190
191 - (BOOL)isBodyPartKey:(NSString *)_key inContext:(id)_ctx {
192   /*
193     Every key starting with a digit is consider an IMAP4 mime part key, used in
194     SOGoMailObject and SOGoMailBodyPart.
195   */
196   if ([_key length] == 0)
197     return NO;
198   
199   if (isdigit([_key characterAtIndex:0]))
200     return YES;
201   
202   return NO;
203 }
204
205 - (NSArray *) aclsForUser: (NSString *) uid
206 {
207   return nil;
208 }
209
210 @end /* SOGoMailBaseObject */