]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailAccounts.m
use common way to detect internet access
[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/AgenorUserManager.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 [[(WOApplication *)[WOApplication application] context] 
36             isAccessFromIntranet] ? NO : YES;
37 }
38
39 /* listing the available mailboxes */
40
41 - (NSArray *)toManyRelationshipKeys {
42   static AgenorUserManager *um = nil;
43   NSString *uid;
44   id       account;
45   NSArray  *shares;
46   
47   if (um == nil)
48     um = [[AgenorUserManager sharedUserManager] retain];
49   
50   uid     = [[self container] davDisplayName]; /* the uid part of the URL */
51   account = [um getIMAPAccountStringForUID:uid];
52   if (account != nil) account = [NSArray arrayWithObject:account];
53   
54   if ([self isInternetRequest]) /* only show primary mailbox in Internet */
55     return account;
56   
57   shares  = [um getSharedMailboxAccountStringsForUID:uid];
58   return ([shares count] == 0)
59     ? account
60     : [account arrayByAddingObjectsFromArray:shares];
61 }
62
63 /* name lookup */
64
65 - (BOOL)isValidMailAccountName:(NSString *)_key {
66   if ([_key length] == 0)
67     return NO;
68   
69   return YES;
70 }
71
72 - (id)mailAccountWithName:(NSString *)_key inContext:(id)_ctx {
73   static Class ctClass = Nil;
74   id ct;
75   
76   if (ctClass == Nil)
77     ctClass = NSClassFromString(@"SOGoMailAccount");
78   if (ctClass == Nil) {
79     [self errorWithFormat:@"missing SOGoMailAccount class!"];
80     return nil;
81   }
82   
83   ct = [[ctClass alloc] initWithName:_key inContainer:self];
84   return [ct autorelease];
85 }
86
87 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
88   id obj;
89   
90   /* first check attributes directly bound to the application */
91   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
92     return obj;
93   
94   if ([self isValidMailAccountName:_key]) {
95     /* forbid shares for requests coming from the Internet */
96     if ([self isInternetRequest]) {
97       if ([_key rangeOfString:AgenorShareLoginMarker].length > 0) {
98         return [NSException exceptionWithHTTPStatus:403 /* Forbidden */
99                             reason:
100                               @"Access to shares forbidden from the Internet"];
101       }
102     }
103     
104     return [self mailAccountWithName:_key inContext:_ctx];
105   }
106
107   /* return 404 to stop acquisition */
108   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
109 }
110
111 /* WebDAV */
112
113 - (BOOL)davIsCollection {
114   return YES;
115 }
116
117 @end /* SOGoMailAccounts */