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