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