2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #include "SOGoSieveScriptObject.h"
23 #include "SOGoSieveScriptsFolder.h"
25 #include <NGImap4/NGSieveClient.h>
27 @implementation SOGoSieveScriptObject
31 - (NSString *)fetchScript {
32 NGSieveClient *client;
34 if ((client = [self sieveClient]) == nil)
37 return [client getScript:[self nameInContainer]];
42 - (NSString *)contentAsString {
43 return [self fetchScript];
46 return [[self contentAsString] dataUsingEncoding:NSUTF8StringEncoding];
49 - (NSException *)exceptionForFailedPutResult:(NSDictionary *)_result {
52 /* Note: valueForKey:@"reason" does not work?! */
53 reason = [(NSDictionary *)[_result valueForKey:@"RawResponse"]
54 objectForKey:@"reason"];
55 if (![reason isNotNull])
56 reason = @"Failed to upload Sieve script.";
58 return [NSException exceptionWithHTTPStatus:500 /* Server Error */
62 - (NSException *)writeContent:(id)_content {
63 NGSieveClient *client;
66 if (_content == nil) {
67 return [NSException exceptionWithHTTPStatus:400 /* Bad Request */
68 reason:@"Missing content to write!"];
71 if ((client = [self sieveClient]) == nil) {
72 return [NSException exceptionWithHTTPStatus:500 /* Server Error */
73 reason:@"Failed to create NGSieveClient object"];
76 result = [client putScript:[self nameInContainer] script:_content];
77 if (![[result valueForKey:@"result"] boolValue])
78 return [self exceptionForFailedPutResult:result];
80 return nil; /* everything is great */
85 - (NSException *)delete {
89 res = [[self sieveClient] deleteScript:[self nameInContainer]];
90 if ([[res valueForKey:@"result"] boolValue])
93 // TODO: make it a debug log
94 [self logWithFormat:@"sieve delete failed: %@", res];
96 r = [@"Sieve delete failed: " stringByAppendingString:[res description]];
97 return [NSException exceptionWithHTTPStatus:500 /* Server Error */
101 - (NSException *)activate {
102 return [[self container] activateScript:[self nameInContainer]];
107 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
110 /* first check attributes directly bound to the object */
111 if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
114 /* return 404 to stop acquisition */
115 return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
120 - (id)PUTAction:(id)_context {
124 content = [[(WOContext *)_context request] contentAsString];
126 if ((e = [self writeContent:content]))
134 - (NSString *)outlookMessageClass {
135 return @"IPM.Filter";
139 @end /* SOGoSieveScriptObject */