]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailBaseObject.m
e264229124675adbe0333f8dcced33dbabf124ab
[scalable-opengroupware.org] / SOGo / SoObjects / Mailer / SOGoMailBaseObject.m
1 /*
2   Copyright (C) 2004 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 // $Id$
22
23 #include "SOGoMailBaseObject.h"
24 #include "SOGoMailManager.h"
25 #include "common.h"
26 #include <NGObjWeb/SoObject+SoDAV.h>
27 #include <NGObjWeb/SoHTTPAuthenticator.h>
28 #include <NGExtensions/NSURL+misc.h>
29
30 @implementation SOGoMailBaseObject
31
32 #if 0
33 static BOOL debugOn = YES;
34 #endif
35
36 - (id)initWithImap4URL:(NSURL *)_url inContainer:(id)_container {
37   NSString *n;
38   
39   n = [[_url path] lastPathComponent];
40   if ((self = [self initWithName:n inContainer:_container])) {
41     self->imap4URL = [_url retain];
42   }
43   return self;
44 }
45
46 - (void)dealloc {
47   [self->imap4URL release];
48   [super dealloc];
49 }
50
51 /* hierarchy */
52
53 - (SOGoMailAccount *)mailAccountFolder {
54   if (![[self container] respondsToSelector:_cmd]) {
55     [self warnWithFormat:@"weird container of mailfolder: %@",
56             [self container]];
57     return nil;
58   }
59   
60   return [[self container] mailAccountFolder];
61 }
62
63 /* IMAP4 */
64
65 - (SOGoMailManager *)mailManager {
66   return [SOGoMailManager defaultMailManager];
67 }
68
69 - (NSString *)relativeImap4Name {
70   [self warnWithFormat:@"subclass should override %@", 
71           NSStringFromSelector(_cmd)];
72   return nil;
73 }
74 - (NSURL *)baseImap4URL {
75   if (![[self container] respondsToSelector:@selector(imap4URL)]) {
76     [self warnWithFormat:@"container does not implement -imap4URL!"];
77     return nil;
78   }
79   
80   return [[self container] imap4URL];
81 }
82 - (NSURL *)imap4URL {
83   NSString *sn;
84   NSURL    *base;
85   
86   if (self->imap4URL != nil) 
87     return self->imap4URL;
88   
89   if ((sn = [self relativeImap4Name]) == nil)
90     return nil;
91   
92   if (![[self container] respondsToSelector:_cmd]) {
93     [self warnWithFormat:@"container does not implement -imap4URL!"];
94     return nil;
95   }
96   
97   if ((base = [self baseImap4URL]) == nil)
98     return nil;
99   
100   sn = [[base path] stringByAppendingPathComponent:sn];
101   self->imap4URL = [[NSURL alloc] initWithString:sn relativeToURL:base];
102   return self->imap4URL;
103 }
104
105 - (NSString *)imap4FolderName {
106   return [[self mailManager] imap4FolderNameForURL:[self imap4URL]];
107 }
108
109 - (NSString *)imap4Password {
110   /*
111     Extract password from basic authentication.
112     
113     TODO: we might want to
114     a) move the primary code to SOGoMailAccount
115     b) cache the password
116   */
117   WORequest *rq;
118   NSString  *auth;
119   NSArray   *creds;
120   
121   rq = [[(WOApplication *)[WOApplication application] context] request];
122   if ((auth = [rq headerForKey:@"authorization"]) == nil) {
123     /* no basic auth */
124     return nil;
125   }
126   
127   creds = [SoHTTPAuthenticator parseCredentials:auth];
128   if ([creds count] < 2)
129     /* somehow invalid */
130     return nil;
131   
132   return [creds objectAtIndex:1]; /* the password */
133 }
134
135 - (NGImap4Client *)imap4ClientForURL:(NSURL *)_url password:(NSString *)_pwd {
136   return [[self mailManager] imap4ClientForURL:_url password:_pwd];
137 }
138
139 - (NGImap4Client *)imap4Client {
140   return [self imap4ClientForURL:[self imap4URL]
141                password:[self imap4Password]];
142 }
143
144 - (void)flushMailCaches {
145   [[self mailManager] flushCachesForURL:[self imap4URL]];
146 }
147
148 /* debugging */
149
150 - (NSString *)loggingPrefix {
151   /* improve perf ... */
152   return [NSString stringWithFormat:@"<0x%08X[%@]:%@>",
153                      self, NSStringFromClass([self class]),
154                      [self nameInContainer]];
155 }
156 @end /* SOGoMailBaseObject */