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