]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOMailDelivery.m
renamed packages as discussed in the developer list
[sope] / sope-appserver / NGObjWeb / WOMailDelivery.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 // $Id$
22
23 #include <NGObjWeb/WOMailDelivery.h>
24 #include <NGObjWeb/WOComponent.h>
25 #include <NGObjWeb/WOResponse.h>
26 #include "common.h"
27
28 @implementation WOMailDelivery
29
30 + (int)version {
31   return 2;
32 }
33
34 WOMailDelivery *sharedInstance = nil;
35
36 + (id)sharedInstance {
37   if (sharedInstance == nil)
38     sharedInstance = [[WOMailDelivery alloc] init];
39   return sharedInstance;
40 }
41
42 // composing mails
43
44 - (id)composeEmailFrom:(NSString *)_senderAddress
45   to:(NSArray *)_receiverAddresses
46   cc:(NSArray *)_ccAddresses
47   subject:(NSString *)_subject
48   plainText:(NSString *)_text
49   send:(BOOL)_sendFlag
50 {
51   NSMutableDictionary *email = [NSMutableDictionary dictionaryWithCapacity:16];
52   NSData *content;
53
54   if (_senderAddress == nil)          return nil;
55   if ([_receiverAddresses count] < 1) return nil;
56   
57   if (_subject == nil)     _subject = @"";
58   if (_text == nil)        _text    = @"";
59   if (_ccAddresses == nil) _ccAddresses = [NSArray array];
60   
61   [email setObject:_subject           forKey:@"subject"];
62   [email setObject:_receiverAddresses forKey:@"to"];
63   [email setObject:_ccAddresses       forKey:@"cc"];
64   [email setObject:_senderAddress     forKey:@"from"];
65   [email setObject:@"text/plain; charset=us-ascii" forKey:@"content-type"];
66
67   content = [NSData dataWithBytes:[_text cString] length:[_text cStringLength]];
68   [email setObject:content forKey:@"body"];
69   [email setObject:[NSNumber numberWithInt:[content length]]
70          forKey:@"content-length"];
71
72   if (_sendFlag) {
73     if (![self sendEmail:email])
74       return nil;
75   }
76   return email;
77 }
78
79 - (id)composeEmailFrom:(NSString *)_senderAddress
80   to:(NSArray *)_receiverAddresses
81   cc:(NSArray *)_ccAddresses
82   subject:(NSString *)_subject
83   component:(WOComponent *)_component
84   send:(BOOL)_sendFlag
85 {
86   NSMutableDictionary *email = [NSMutableDictionary dictionaryWithCapacity:16];
87
88   if (_senderAddress == nil)          return nil;
89   if ([_receiverAddresses count] < 1) return nil;
90   if (_subject     == nil) _subject = @"";
91   if (_ccAddresses == nil) _ccAddresses = [NSArray array];
92
93   [email setObject:_subject           forKey:@"subject"];
94   [email setObject:_receiverAddresses forKey:@"to"];
95   [email setObject:_ccAddresses       forKey:@"cc"];
96   [email setObject:_senderAddress     forKey:@"from"];
97
98   /* gen response */
99   {
100     WOResponse *response;
101     NSString   *contentType;
102
103     response = [_component generateResponse];
104     if ([response status] != 200)
105       // could not generate response
106       return nil;
107
108     contentType = [response headerForKey:@"content-type"];
109     if (contentType == nil) contentType = @"text/html";
110     
111     [email setObject:contentType forKey:@"content-type"];
112     [email setObject:[response content] forKey:@"body"];
113     [email setObject:[NSNumber numberWithInt:[[response content] length]]
114            forKey:@"content-length"];
115   }
116
117   if (_sendFlag) {
118     if (![self sendEmail:email])
119       return nil;
120   }
121   return email;
122 }
123
124 // sending mails
125
126 - (BOOL)sendEmail:(id)_email {
127   NSMutableString *sendmail = [NSMutableString stringWithCapacity:256];
128   NSArray *to, *cc;
129   FILE *toMail;
130
131   to = [_email objectForKey:@"to"];
132   cc = [_email objectForKey:@"cc"];
133
134   [sendmail appendString:[[NSUserDefaults standardUserDefaults]
135                                           stringForKey:@"WOSendMail"]];
136   [sendmail appendString:@" "];
137   [sendmail appendString:[to componentsJoinedByString:@" "]];
138   [sendmail appendString:@" "];
139   [sendmail appendString:[cc componentsJoinedByString:@" "]];
140
141   if ((toMail = popen([sendmail cString], "w"))) {
142     NSEnumerator *e = nil;
143     id entry;
144     NSString *tmp;
145     
146     if ((tmp = [[_email objectForKey:@"from"] stringValue])) {
147       if (fprintf(toMail, "Reply-To: %s\n", [tmp cString]) < 0)
148         goto failed;
149       if (fprintf(toMail, "From: %s\n", [tmp cString]) < 0)
150         goto failed;
151     }
152     
153     e = [to objectEnumerator];
154     while ((entry = [e nextObject])) {
155       if (fprintf(toMail, "To:%s\n", [[entry stringValue] cString]) < 0)
156         goto failed;
157     }
158
159     e = [cc objectEnumerator];
160     while ((entry = [e nextObject])) {
161       if (fprintf(toMail, "Cc:%s\n", [[entry stringValue] cString]) < 0)
162         goto failed;
163     }
164     
165     if ((tmp = [[_email objectForKey:@"subject"] stringValue])) {
166       if (fprintf(toMail, "Subject:%s\n", [tmp cString]) < 0)
167         goto failed;
168     }
169
170     if ((tmp = [[_email objectForKey:@"content-type"] stringValue])) {
171       if (fprintf(toMail, "Content-type:%s\n", [tmp cString]) < 0)
172         goto failed;
173     }
174     if ((tmp = [[_email objectForKey:@"content-length"] stringValue])) {
175       if (fprintf(toMail, "Content-length:%s\n", [tmp cString]) < 0)
176         goto failed;
177     }
178     
179     // end header
180     if (fprintf(toMail, "\n") < 0)
181       goto failed;
182
183     // write body
184     {
185       NSData *body = [_email objectForKey:@"body"];
186       if (fwrite([body bytes], [body length], 1, toMail) < 0)
187         goto failed;
188     }
189     fprintf(toMail, "\n");
190     pclose(toMail);
191
192     return YES;
193
194   failed:
195     pclose(toMail);
196     return NO;
197   }
198   return NO;
199 }
200
201 @end /* WOMailDelivery */