]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Mailer/SOGoMailAccounts.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1071 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / 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 "SOGoUser+Mail.h"
24 #include "common.h"
25 #include <NGObjWeb/SoObject+SoDAV.h>
26 #include <SOGo/WOContext+Agenor.h>
27
28 @implementation SOGoMailAccounts
29
30 static NSString *AgenorShareLoginMarker  = @".-.";
31
32 /* detect webmail being accessed from the outside */
33
34 - (BOOL)isInternetRequest {
35   return ([context isAccessFromIntranet] ? NO : YES);
36 }
37
38 /* listing the available mailboxes */
39
40 - (BOOL) isInHomeFolderBranchOfLoggedInAccount: (NSString *) userLogin
41 {
42   return [[[self container] nameInContainer] isEqualToString: userLogin];
43 }
44
45 - (NSArray *)toManyRelationshipKeys {
46   SOGoUser *user;
47   id        account;
48   NSArray   *shares;
49   NSString *userLogin;
50   
51   /*
52     Note: this is not strictly correct. The accounts being retrieved should be
53           the accounts based on the container object of this folder. Given
54           sufficient rights (eg delegation rights!), this would allow you to
55           browse the hierarchies of other users.
56           
57           But then, the home-folder would need to know about mail
58           functionality which isn't perfect either.
59           => TODO
60   */
61   user = [context activeUser];
62   userLogin = [user login];
63   
64   /* for now: return nothing if the home-folder does not belong to the login */
65   if (![self isInHomeFolderBranchOfLoggedInAccount: userLogin]) {
66     [self warnWithFormat:@"User %@ tried to access mail hierarchy of %@",
67           [user login], [[self container] nameInContainer]];
68     return nil;
69   }
70   
71   account = [user primaryIMAP4AccountString];
72   if ([account isNotNull]) account = [NSArray arrayWithObject:account];
73   
74   if ([self isInternetRequest]) /* only show primary mailbox in Internet */
75     return account;
76   
77   shares  = [user valueForKey:@"additionalIMAP4AccountStrings"];
78   return ([shares count] == 0)
79     ? account
80     : [account arrayByAddingObjectsFromArray:shares];
81 }
82
83 - (NSArray *) fetchIdentitiesWithOnlyEmitterAccess: (BOOL) _flag
84 {
85   NSString *accountString;
86
87   accountString = [[context activeUser] primaryIMAP4AccountString];
88
89   return [NSArray arrayWithObject: accountString];
90 }
91
92 - (NSArray *)fetchAllIdentities {
93   return [self fetchIdentitiesWithOnlyEmitterAccess:NO];
94 }
95
96 - (NSArray *)fetchIdentitiesWithEmitterPermissions {
97   return [self fetchIdentitiesWithOnlyEmitterAccess:YES];
98 }
99
100 /* name lookup */
101
102 - (BOOL)isValidMailAccountName:(NSString *)_key {
103   if ([_key length] == 0)
104     return NO;
105   
106   return YES;
107 }
108
109 - (id)mailAccountWithName:(NSString *)_key inContext:(id)_ctx {
110   static Class ctClass = Nil;
111   id ct;
112   
113   if (ctClass == Nil)
114     ctClass = NSClassFromString(@"SOGoMailAccount");
115   if (ctClass == Nil) {
116     [self errorWithFormat:@"missing SOGoMailAccount class!"];
117     return nil;
118   }
119   
120   ct = [[ctClass alloc] initWithName:_key inContainer:self];
121   return [ct autorelease];
122 }
123
124 - (id)sharedMailAccountWithName:(NSString *)_key inContext:(id)_ctx {
125   static Class ctClass = Nil;
126   id ct;
127   
128   if (ctClass == Nil)
129     ctClass = NSClassFromString(@"SOGoSharedMailAccount");
130   if (ctClass == Nil) {
131     [self errorWithFormat:@"missing SOGoSharedMailAccount class!"];
132     return nil;
133   }
134   
135   ct = [[ctClass alloc] initWithName:_key inContainer:self];
136   return [ct autorelease];
137 }
138
139 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag
140 {
141   id obj;
142   NSString *userLogin;
143
144   userLogin = [[context activeUser] login];
145   
146   /* first check attributes directly bound to the application */
147   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
148     return obj;
149   
150   if (![self isInHomeFolderBranchOfLoggedInAccount: userLogin]) {
151     [self warnWithFormat:@"User %@ tried to access mail hierarchy of %@",
152           userLogin, [[self container] nameInContainer]];
153     
154     return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
155                         reason:@"Tried to access the mail of another user"];
156   }
157   
158   if ([self isValidMailAccountName:_key]) {
159     /* forbid shares for requests coming from the Internet */
160     BOOL isSharedKey;
161     
162     isSharedKey = [_key rangeOfString:AgenorShareLoginMarker].length > 0;
163     
164     if ([self isInternetRequest]) {
165       if (isSharedKey) {
166         return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
167                             reason:
168                               @"Access to shares forbidden from the Internet"];
169       }
170     }
171     
172     return isSharedKey
173       ? [self sharedMailAccountWithName:_key inContext:_ctx]
174       : [self mailAccountWithName:_key inContext:_ctx];
175   }
176
177   /* return 404 to stop acquisition */
178   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
179 }
180
181 /* WebDAV */
182
183 - (BOOL) davIsCollection
184 {
185   return YES;
186 }
187
188 /* acls */
189
190 - (NSArray *) aclsForUser: (NSString *) uid
191 {
192   return nil;
193 }
194
195
196 @end /* SOGoMailAccounts */