]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailBaseObject.m
consolidated imap client objects to NGImap4
[scalable-opengroupware.org] / SOGo / 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 #if 0
32 static BOOL debugOn = YES;
33 #endif
34
35 - (id)initWithImap4URL:(NSURL *)_url inContainer:(id)_container {
36   NSString *n;
37   
38   n = [[_url path] lastPathComponent];
39   if ((self = [self initWithName:n inContainer:_container])) {
40     self->imap4URL = [_url retain];
41   }
42   return self;
43 }
44
45 - (void)dealloc {
46   [self->imap4URL release];
47   [super dealloc];
48 }
49
50 /* hierarchy */
51
52 - (SOGoMailAccount *)mailAccountFolder {
53   if (![[self container] respondsToSelector:_cmd]) {
54     [self warnWithFormat:@"weird container of mailfolder: %@",
55             [self container]];
56     return nil;
57   }
58   
59   return [[self container] mailAccountFolder];
60 }
61
62 /* IMAP4 */
63
64 - (NGImap4ConnectionManager *)mailManager {
65   return [NGImap4ConnectionManager defaultConnectionManager];
66 }
67
68 - (NSString *)relativeImap4Name {
69   [self warnWithFormat:@"subclass should override %@", 
70           NSStringFromSelector(_cmd)];
71   return nil;
72 }
73 - (NSURL *)baseImap4URL {
74   if (![[self container] respondsToSelector:@selector(imap4URL)]) {
75     [self warnWithFormat:@"container does not implement -imap4URL!"];
76     return nil;
77   }
78   
79   return [[self container] imap4URL];
80 }
81 - (NSURL *)imap4URL {
82   NSString *sn;
83   NSURL    *base;
84   
85   if (self->imap4URL != nil) 
86     return self->imap4URL;
87   
88   if ((sn = [self relativeImap4Name]) == nil)
89     return nil;
90   
91   if (![[self container] respondsToSelector:_cmd]) {
92     [self warnWithFormat:@"container does not implement -imap4URL!"];
93     return nil;
94   }
95   
96   if ((base = [self baseImap4URL]) == nil)
97     return nil;
98   
99   sn = [[base path] stringByAppendingPathComponent:sn];
100   self->imap4URL = [[NSURL alloc] initWithString:sn relativeToURL:base];
101   return self->imap4URL;
102 }
103
104 - (NSString *)imap4Login {
105   if (![[self container] respondsToSelector:_cmd])
106     return nil;
107   
108   return [[self container] imap4Login];
109 }
110 - (NSString *)imap4Password {
111   /*
112     Extract password from basic authentication.
113     
114     TODO: we might want to
115     a) move the primary code to SOGoMailAccount
116     b) cache the password
117   */
118   WORequest *rq;
119   NSString  *auth;
120   NSArray   *creds;
121   
122   rq = [[(WOApplication *)[WOApplication application] context] request];
123   if ((auth = [rq headerForKey:@"authorization"]) == nil) {
124     /* no basic auth */
125     return nil;
126   }
127   
128   creds = [SoHTTPAuthenticator parseCredentials:auth];
129   if ([creds count] < 2)
130     /* somehow invalid */
131     return nil;
132   
133   return [creds objectAtIndex:1]; /* the password */
134 }
135
136 - (NGImap4Client *)imap4ClientForURL:(NSURL *)_url password:(NSString *)_pwd {
137   return [[self mailManager] imap4ClientForURL:_url password:_pwd];
138 }
139
140 - (NGImap4Client *)imap4Client {
141   return [self imap4ClientForURL:[self imap4URL]
142                password:[self imap4Password]];
143 }
144
145 - (void)flushMailCaches {
146   [[self mailManager] flushCachesForURL:[self imap4URL]];
147 }
148
149 /* IMAP4 names */
150
151 - (BOOL)isBodyPartKey:(NSString *)_key inContext:(id)_ctx {
152   /*
153     Every key starting with a digit is consider an IMAP4 mime part key, used in
154     SOGoMailObject and SOGoMailBodyPart.
155   */
156   if ([_key length] == 0)
157     return NO;
158   
159   if (isdigit([_key characterAtIndex:0]))
160     return YES;
161   
162   return NO;
163 }
164
165 /* debugging */
166
167 - (NSString *)loggingPrefix {
168   /* improve perf ... */
169   return [NSString stringWithFormat:@"<0x%08X[%@]:%@>",
170                      self, NSStringFromClass([self class]),
171                      [self nameInContainer]];
172 }
173 @end /* SOGoMailBaseObject */