]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailAccounts.m
added special support for shared mail accounts (with .-.)
[scalable-opengroupware.org] / SOGo / SoObjects / Mailer / SOGoMailAccounts.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 "SOGoMailAccounts.h"
23 #include "common.h"
24 #include <NGObjWeb/SoObject+SoDAV.h>
25 #include <SOGo/WOContext+Agenor.h>
26
27 @implementation SOGoMailAccounts
28
29 static NSString *AgenorShareLoginMarker  = @".-.";
30
31 /* detect webmail being accessed from the outside */
32
33 - (BOOL)isInternetRequest {
34   return [[(WOApplication *)[WOApplication application] context] 
35             isAccessFromIntranet] ? NO : YES;
36 }
37
38 /* listing the available mailboxes */
39
40 - (NSArray *)toManyRelationshipKeys {
41   WOContext *ctx;
42   id        user;
43   id        account;
44   NSArray   *shares;
45   
46   if ((ctx = [[WOApplication application] context]) == nil) {
47     [self logWithFormat:@"ERROR(%s): cannot procede without context!",
48             __PRETTY_FUNCTION__];
49     return nil;
50   }
51   user    = [ctx activeUser];
52   account = [user valueForKey:@"primaryIMAP4AccountString"];
53   if ([account isNotNull]) account = [NSArray arrayWithObject:account];
54   
55   if ([self isInternetRequest]) /* only show primary mailbox in Internet */
56     return account;
57   
58   shares  = [user valueForKey:@"additionalIMAP4AccountStrings"];
59   return ([shares count] == 0)
60     ? account
61     : [account arrayByAddingObjectsFromArray:shares];
62 }
63
64 /* name lookup */
65
66 - (BOOL)isValidMailAccountName:(NSString *)_key {
67   if ([_key length] == 0)
68     return NO;
69   
70   return YES;
71 }
72
73 - (id)mailAccountWithName:(NSString *)_key inContext:(id)_ctx {
74   static Class ctClass = Nil;
75   id ct;
76   
77   if (ctClass == Nil)
78     ctClass = NSClassFromString(@"SOGoMailAccount");
79   if (ctClass == Nil) {
80     [self errorWithFormat:@"missing SOGoMailAccount class!"];
81     return nil;
82   }
83   
84   ct = [[ctClass alloc] initWithName:_key inContainer:self];
85   return [ct autorelease];
86 }
87 - (id)sharedMailAccountWithName:(NSString *)_key inContext:(id)_ctx {
88   static Class ctClass = Nil;
89   id ct;
90   
91   if (ctClass == Nil)
92     ctClass = NSClassFromString(@"SOGoSharedMailAccount");
93   if (ctClass == Nil) {
94     [self errorWithFormat:@"missing SOGoSharedMailAccount class!"];
95     return nil;
96   }
97   
98   ct = [[ctClass alloc] initWithName:_key inContainer:self];
99   return [ct autorelease];
100 }
101
102 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
103   id obj;
104   
105   /* first check attributes directly bound to the application */
106   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
107     return obj;
108   
109   if ([self isValidMailAccountName:_key]) {
110     /* forbid shares for requests coming from the Internet */
111     BOOL isSharedKey;
112     
113     isSharedKey = [_key rangeOfString:AgenorShareLoginMarker].length > 0;
114     
115     if ([self isInternetRequest]) {
116       if (isSharedKey) {
117         return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
118                             reason:
119                               @"Access to shares forbidden from the Internet"];
120       }
121     }
122     
123     return isSharedKey
124       ? [self sharedMailAccountWithName:_key inContext:_ctx]
125       : [self mailAccountWithName:_key inContext:_ctx];
126   }
127
128   /* return 404 to stop acquisition */
129   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
130 }
131
132 /* WebDAV */
133
134 - (BOOL)davIsCollection {
135   return YES;
136 }
137
138 @end /* SOGoMailAccounts */