| 1 |
/* |
| 2 |
** CWSendmail.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** |
| 6 |
** Author: Ludovic Marcotte <[email protected]> |
| 7 |
** |
| 8 |
** This library is free software; you can redistribute it and/or |
| 9 |
** modify it under the terms of the GNU Lesser General Public |
| 10 |
** License as published by the Free Software Foundation; either |
| 11 |
** version 2.1 of the License, or (at your option) any later version. |
| 12 |
** |
| 13 |
** This library is distributed in the hope that it will be useful, |
| 14 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
** Lesser General Public License for more details. |
| 17 |
** |
| 18 |
** You should have received a copy of the GNU General Public License |
| 19 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import <Pantomime/CWSendmail.h> |
| 23 |
|
| 24 |
#import <Pantomime/CWConstants.h> |
| 25 |
#import <Pantomime/CWMessage.h> |
| 26 |
#import <Pantomime/NSFileManager+Extensions.h> |
| 27 |
#import <Pantomime/NSString+Extensions.h> |
| 28 |
|
| 29 |
#import <Foundation/NSFileHandle.h> |
| 30 |
#import <Foundation/NSFileManager.h> |
| 31 |
#import <Foundation/NSNotification.h> |
| 32 |
#import <Foundation/NSPathUtilities.h> |
| 33 |
#import <Foundation/NSProcessInfo.h> |
| 34 |
#import <Foundation/NSTask.h> |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
|
| 38 |
|
| 39 |
// |
| 40 |
// Sendmail's private interface |
| 41 |
// |
| 42 |
@interface CWSendmail (Private) |
| 43 |
|
| 44 |
- (void) _fail; |
| 45 |
- (void) _taskDidTerminate: (NSNotification *) theNotification; |
| 46 |
|
| 47 |
@end |
| 48 |
|
| 49 |
|
| 50 |
// |
| 51 |
// |
| 52 |
// |
| 53 |
@implementation CWSendmail |
| 54 |
|
| 55 |
- (id) initWithPath: (NSString *) thePath; |
| 56 |
{ |
| 57 |
self = [super init]; |
| 58 |
if (self) |
| 59 |
{ |
| 60 |
[self setPath: thePath]; |
| 61 |
|
| 62 |
_delegate = nil; |
| 63 |
} |
| 64 |
|
| 65 |
return self; |
| 66 |
} |
| 67 |
|
| 68 |
- (void) dealloc |
| 69 |
{ |
| 70 |
RELEASE(_message); |
| 71 |
RELEASE(_data); |
| 72 |
RELEASE(_recipients); |
| 73 |
RELEASE(_path); |
| 74 |
|
| 75 |
[super dealloc]; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
// |
| 81 |
// access / mutation methods |
| 82 |
// |
| 83 |
- (void) setDelegate: (id) theDelegate |
| 84 |
{ |
| 85 |
_delegate = theDelegate; |
| 86 |
} |
| 87 |
|
| 88 |
- (id) delegate |
| 89 |
{ |
| 90 |
return _delegate; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
// |
| 95 |
// |
| 96 |
// |
| 97 |
- (void) setPath: (NSString *) thePath |
| 98 |
{ |
| 99 |
ASSIGN(_path, [thePath stringByTrimmingWhiteSpaces]); |
| 100 |
} |
| 101 |
|
| 102 |
- (NSString *) path |
| 103 |
{ |
| 104 |
return _path; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
// |
| 110 |
// |
| 111 |
// |
| 112 |
- (void) setMessage: (CWMessage *) theMessage |
| 113 |
{ |
| 114 |
ASSIGN(_message, theMessage); |
| 115 |
} |
| 116 |
|
| 117 |
- (CWMessage *) message |
| 118 |
{ |
| 119 |
return _message; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
// |
| 124 |
// |
| 125 |
// |
| 126 |
- (void) setMessageData: (NSData *) theData |
| 127 |
{ |
| 128 |
ASSIGN(_data, theData); |
| 129 |
} |
| 130 |
|
| 131 |
- (NSData *) messageData |
| 132 |
{ |
| 133 |
return _data; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
// |
| 138 |
// That does nothing but we keep it if a developer wanna |
| 139 |
// use that ivar. |
| 140 |
// |
| 141 |
- (void) setRecipients: (NSArray *) theRecipients |
| 142 |
{ |
| 143 |
ASSIGN(_recipients, [NSMutableArray arrayWithArray: theRecipients]); |
| 144 |
} |
| 145 |
|
| 146 |
- (NSArray *) recipients |
| 147 |
{ |
| 148 |
return _recipients; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
// |
| 153 |
// |
| 154 |
// |
| 155 |
- (void) sendMessage |
| 156 |
{ |
| 157 |
NSString *aString, *aFilename; |
| 158 |
NSFileHandle *aFileHandle; |
| 159 |
NSRange aRange; |
| 160 |
NSTask *aTask; |
| 161 |
|
| 162 |
if ((!_message && !_data) || !_path) |
| 163 |
{ |
| 164 |
[self _fail]; |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
if (!_data && _message) |
| 169 |
{ |
| 170 |
[self setMessageData: [_message dataValue]]; |
| 171 |
} |
| 172 |
|
| 173 |
// We verify if _pathToSendmail is a valid one (ie., readable and executable) |
| 174 |
aRange = [_path rangeOfString: @" "]; |
| 175 |
aString = _path; |
| 176 |
|
| 177 |
if (aRange.location != NSNotFound) |
| 178 |
{ |
| 179 |
aString = [_path substringToIndex: aRange.location]; |
| 180 |
} |
| 181 |
|
| 182 |
if (![[NSFileManager defaultManager] isExecutableFileAtPath: aString]) |
| 183 |
{ |
| 184 |
[self _fail]; |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
// We now create our task and send the message |
| 189 |
aFilename = [NSString stringWithFormat: @"%@/%d_%@", NSTemporaryDirectory(), |
| 190 |
[[NSProcessInfo processInfo] processIdentifier], |
| 191 |
NSUserName()]; |
| 192 |
|
| 193 |
if (![_data writeToFile: aFilename atomically: YES]) |
| 194 |
{ |
| 195 |
[self _fail]; |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
[[NSFileManager defaultManager] enforceMode: 0600 atPath: aFilename]; |
| 200 |
aFileHandle = [NSFileHandle fileHandleForReadingAtPath: aFilename]; |
| 201 |
aTask = [[NSTask alloc] init]; |
| 202 |
|
| 203 |
// We register for our notification |
| 204 |
[[NSNotificationCenter defaultCenter] |
| 205 |
addObserver: self |
| 206 |
selector: @selector(_taskDidTerminate:) |
| 207 |
name: NSTaskDidTerminateNotification |
| 208 |
object: aTask]; |
| 209 |
|
| 210 |
// We build our right string |
| 211 |
aString = [_path stringByTrimmingWhiteSpaces]; |
| 212 |
|
| 213 |
// We verify if our program to launch has any arguments |
| 214 |
aRange = [aString rangeOfString: @" "]; |
| 215 |
|
| 216 |
if (aRange.length) |
| 217 |
{ |
| 218 |
[aTask setLaunchPath: [aString substringToIndex: aRange.location]]; |
| 219 |
[aTask setArguments: [[aString substringFromIndex: (aRange.location + 1)] |
| 220 |
componentsSeparatedByString: @" "]]; |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
[aTask setLaunchPath: aString]; |
| 225 |
} |
| 226 |
|
| 227 |
[aTask setStandardInput: aFileHandle]; |
| 228 |
|
| 229 |
// We launch our task |
| 230 |
[aTask launch]; |
| 231 |
|
| 232 |
[aFileHandle closeFile]; |
| 233 |
|
| 234 |
[[NSFileManager defaultManager] removeFileAtPath: aFilename |
| 235 |
handler: nil]; |
| 236 |
} |
| 237 |
|
| 238 |
@end |
| 239 |
|
| 240 |
|
| 241 |
// |
| 242 |
// |
| 243 |
// |
| 244 |
@implementation CWSendmail (Private) |
| 245 |
|
| 246 |
- (void) _fail |
| 247 |
{ |
| 248 |
if (_message) |
| 249 |
POST_NOTIFICATION(PantomimeMessageNotSent, self, |
| 250 |
[NSDictionary dictionaryWithObject: _message forKey: @"Message"]); |
| 251 |
else |
| 252 |
POST_NOTIFICATION(PantomimeMessageNotSent, self, |
| 253 |
[NSDictionary dictionaryWithObject: AUTORELEASE([CWMessage new]) forKey: @"Message"]); |
| 254 |
PERFORM_SELECTOR_1(_delegate, @selector(messageNotSent:), PantomimeMessageNotSent); |
| 255 |
} |
| 256 |
|
| 257 |
- (void) _taskDidTerminate: (NSNotification *) theNotification |
| 258 |
{ |
| 259 |
// We first unregister ourself for the notification |
| 260 |
[[NSNotificationCenter defaultCenter] removeObserver: self]; |
| 261 |
|
| 262 |
if ([[theNotification object] terminationStatus] == 0) |
| 263 |
{ |
| 264 |
POST_NOTIFICATION(PantomimeMessageSent, self, |
| 265 |
[NSDictionary dictionaryWithObject: _message forKey: @"Message"]); |
| 266 |
PERFORM_SELECTOR_2(_delegate, |
| 267 |
@selector(messageSent:), PantomimeMessageSent, _message, @"Message"); |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
[self _fail]; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
@end |