]> err.no Git - scalable-opengroupware.org/blob - UI/SOGoUI/SOGoACLAdvisory.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1137 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / SOGoUI / SOGoACLAdvisory.m
1 /* SOGoACLAdvisory.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <NGObjWeb/WOResponse.h>
24 #import <NGExtensions/NGHashMap.h>
25 #import <NGMail/NGMimeMessage.h>
26 #import <NGMail/NGSendMail.h>
27 #import <NGMime/NGMimeBodyPart.h>
28 #import <NGMime/NGMimeMultipartBody.h>
29
30 #import <SoObjects/SOGo/SOGoUser.h>
31 #import <SoObjects/SOGo/SOGoObject.h>
32 #import <SoObjects/SOGo/LDAPUserManager.h>
33 #import <SoObjects/SOGo/NSCalendarDate+SOGo.h>
34
35 #import "SOGoACLAdvisory.h"
36
37 @implementation SOGoACLAdvisory
38
39 - (id) init
40 {
41   if ((self = [super init]))
42     {
43       aclObject = nil;
44       recipientUID = nil;
45
46       isSubject = NO;
47       isBody = NO;
48     }
49
50   return self;
51 }
52
53 - (void) dealloc
54 {
55   [recipientUID release];
56   [aclObject release];
57   [super dealloc];
58 }
59
60 - (void) setACLObject: (SOGoObject *) newACLObject
61 {
62   ASSIGN (aclObject, newACLObject);
63 }
64
65 - (void) setRecipientUID: (NSString *) newRecipientUID
66 {
67   ASSIGN (recipientUID, newRecipientUID);
68 }
69
70 - (BOOL) isSubject
71 {
72   return isSubject;
73 }
74
75 - (BOOL) isBody
76 {
77   return isBody;
78 }
79
80 - (NSString *) currentUserName
81 {
82   return [[context activeUser] cn];
83 }
84
85 - (NSString *) httpAdvisoryURL
86 {
87   NSMutableString *url;
88
89 #warning the url returned by SOGoMail may be empty, we need to handle that
90   url
91     = [NSMutableString stringWithString:
92                          [aclObject httpURLForAdvisoryToUser: recipientUID]];
93   if (![url hasSuffix: @"/"])
94     [url appendString: @"/"];
95
96   return url;
97 }
98
99 - (NSString *) resourceName
100 {
101   return [aclObject nameInContainer];
102 }
103
104 - (NSString *) subject
105 {
106   NSString *subject;
107
108   isSubject = YES;
109   subject = [[self generateResponse] contentAsString];
110   isSubject = NO;
111
112   return [subject stringByTrimmingSpaces];
113 }
114
115 - (NSString *) body
116 {
117   NSString *body;
118
119   isBody = YES;
120   body = [[self generateResponse] contentAsString];
121   isBody = NO;
122
123   return [body stringByTrimmingSpaces];
124 }
125
126 - (NSString *) aclMethod
127 {
128   [self subclassResponsibility: _cmd];
129
130   return nil;
131 }
132
133 - (NGMimeBodyPart *) _textPart
134 {
135   NGMutableHashMap *headerMap;
136   NGMimeBodyPart *part;
137   NSData *body;
138
139   headerMap = [NGMutableHashMap hashMapWithCapacity: 1];
140   [headerMap setObject: @"text/plain; charset=utf-8" forKey: @"content-type"];
141
142   part = [NGMimeBodyPart bodyPartWithHeader: headerMap];
143   body = [[self body] dataUsingEncoding: NSUTF8StringEncoding];
144   [part setBody: [self body]];
145
146   return part;
147 }
148
149 - (NGMimeBodyPart *) _sogoNotificationPart
150 {
151   NGMutableHashMap *headerMap;
152   NGMimeBodyPart *part;
153   NSData *body;
154
155   /* calendar part */
156   headerMap = [NGMutableHashMap hashMapWithCapacity: 1];
157   [headerMap setObject: [NSString stringWithFormat:
158                                     @"%@; method=%@; type=%@; charset=%@",
159                                   @"application/x-sogo-notification",
160                                   [self aclMethod], [aclObject folderType],
161                                   @"utf-8"]
162              forKey: @"content-type"];
163
164   part = [NGMimeBodyPart bodyPartWithHeader: headerMap];
165   body = [[aclObject resourceURLForAdvisoryToUser: recipientUID]
166            dataUsingEncoding: NSUTF8StringEncoding];
167   [part setBody: body];
168
169   return part;
170 }
171
172 - (void) send
173 {
174   NSString *recipient, *date;
175   NGMutableHashMap *headerMap;
176   NGMimeMessage *message;
177   NGMimeMultipartBody *body;
178   SOGoUser *activeUser;
179
180   activeUser = [context activeUser];
181   recipient = [[LDAPUserManager sharedUserManager] getFullEmailForUID: recipientUID];
182
183   headerMap = [NGMutableHashMap hashMapWithCapacity: 5];
184   [headerMap setObject: @"multipart/alternative" forKey: @"content-type"];
185   [headerMap setObject: [activeUser fullEmail] forKey: @"From"];
186   [headerMap setObject: recipient forKey: @"To"];
187   date = [[NSCalendarDate date] rfc822DateString];
188   [headerMap setObject: date forKey: @"Date"];
189   [headerMap setObject: [self subject] forKey: @"Subject"];
190   message = [NGMimeMessage messageWithHeader: headerMap];
191
192   body = [[NGMimeMultipartBody alloc] initWithPart: message];
193   [body addBodyPart: [self _textPart]];
194   [body addBodyPart: [self _sogoNotificationPart]];
195   [message setBody: body];
196   [body release];
197
198   [[NGSendMail sharedSendMail] sendMimePart: message
199                                toRecipients: [NSArray arrayWithObject: recipient]
200                                sender: [activeUser primaryEmail]];
201 }
202
203 @end