]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Sieve/SOGoSieveScriptObject.m
git-svn-id: http://svn.opengroupware.org/SOGo/trunk@900 d1b88da0-ebda-0310-925b-ed51d...
[scalable-opengroupware.org] / SoObjects / Sieve / SOGoSieveScriptObject.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 "SOGoSieveScriptObject.h"
23 #include "SOGoSieveScriptsFolder.h"
24 #include "common.h"
25 #include <NGImap4/NGSieveClient.h>
26
27 @implementation SOGoSieveScriptObject
28
29 /* script */
30
31 - (NSString *)fetchScript {
32   NGSieveClient *client;
33   
34   if ((client = [self sieveClient]) == nil)
35     return nil;
36   
37   return [client getScript:[self nameInContainer]];
38 }
39
40 /* content */
41
42 - (NSString *)contentAsString {
43   return [self fetchScript];
44 }
45 - (NSData *)content {
46   return [[self contentAsString] dataUsingEncoding:NSUTF8StringEncoding];
47 }
48
49 - (NSException *)exceptionForFailedPutResult:(NSDictionary *)_result {
50   NSString *reason;
51   
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.";
57   
58   return [NSException exceptionWithHTTPStatus:500 /* Server Error */
59                       reason:reason];
60 }
61
62 - (NSException *)writeContent:(id)_content {
63   NGSieveClient *client;
64   NSDictionary  *result;
65   
66   if (_content == nil) {
67     return [NSException exceptionWithHTTPStatus:400 /* Bad Request */
68                         reason:@"Missing content to write!"];
69   }
70   
71   if ((client = [self sieveClient]) == nil) {
72     return [NSException exceptionWithHTTPStatus:500 /* Server Error */
73                         reason:@"Failed to create NGSieveClient object"];
74   }
75   
76   result = [client putScript:[self nameInContainer] script:_content];
77   if (![[result valueForKey:@"result"] boolValue])
78     return [self exceptionForFailedPutResult:result];
79   
80   return nil; /* everything is great */
81 }
82
83 /* operations */
84
85 - (NSException *)delete {
86   NSDictionary *res;
87   NSString *r;
88   
89   res = [[self sieveClient] deleteScript:[self nameInContainer]];
90   if ([[res valueForKey:@"result"] boolValue])
91     return nil;
92   
93   // TODO: make it a debug log
94   [self logWithFormat:@"sieve delete failed: %@", res];
95   
96   r = [@"Sieve delete failed: " stringByAppendingString:[res description]];
97   return [NSException exceptionWithHTTPStatus:500 /* Server Error */
98                       reason:r];
99 }
100
101 - (NSException *)activate {
102   return [[self container] activateScript:[self nameInContainer]];
103 }
104
105 /* name lookup */
106
107 - (id)lookupName:(NSString *)_key inContext:(id)_ctx acquire:(BOOL)_flag {
108   id obj;
109   
110   /* first check attributes directly bound to the object */
111   if ((obj = [super lookupName:_key inContext:_ctx acquire:NO]))
112     return obj;
113   
114   /* return 404 to stop acquisition */
115   return [NSException exceptionWithHTTPStatus:404 /* Not Found */];
116 }
117
118 /* operations */
119
120 - (id)PUTAction:(id)_context {
121   NSException *e;
122   NSString *content;
123
124   content = [[(WOContext *)_context request] contentAsString];
125   
126   if ((e = [self writeContent:content]))
127     return e;
128   
129   return self;
130 }
131
132 /* message type */
133
134 - (NSString *)outlookMessageClass {
135   return @"IPM.Filter";
136 }
137
138
139 @end /* SOGoSieveScriptObject */