[go: up one dir, main page]

File: SOGoMailer.m

package info (click to toggle)
sogo 1.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 21,620 kB
  • sloc: objc: 99,218; python: 4,840; sh: 1,217; perl: 861; makefile: 132; sql: 53; php: 43; ansic: 4
file content (254 lines) | stat: -rw-r--r-- 6,727 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* SOGoMailer.m - this file is part of SOGo
 *
 * Copyright (C) 2007-2010 Inverse inc.
 *
 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <Foundation/NSArray.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>

#import <NGObjWeb/NSException+HTTP.h>
#import <NGExtensions/NSObject+Logs.h>
#import <NGMail/NGSendMail.h>
#import <NGMail/NGSmtpClient.h>
#import <NGMime/NGMimePartGenerator.h>
#import <NGStreams/NGInternetSocketAddress.h>

#import "NSString+Utilities.h"
#import "SOGoDomainDefaults.h"
#import "SOGoSystemDefaults.h"

#import "SOGoMailer.h"

@implementation SOGoMailer

+ (SOGoMailer *) mailerWithDomainDefaults: (SOGoDomainDefaults *) dd
{
  return [[self alloc] initWithDomainDefaults: dd];
}

- (id) initWithDomainDefaults: (SOGoDomainDefaults *) dd
{
  if ((self = [self init]))
    {
      ASSIGN (mailingMechanism, [dd mailingMechanism]);
      ASSIGN (smtpServer, [dd smtpServer]);
    }

  return self;
}

- (id) init
{
  if ((self = [super init]))
    {
      mailingMechanism = nil;
      smtpServer = nil;
    }

  return self;
}

- (void) dealloc
{
  [mailingMechanism release];
  [smtpServer release];
  [super dealloc];
}

- (NSException *) _sendmailSendData: (NSData *) mailData
		       toRecipients: (NSArray *) recipients
			     sender: (NSString *) sender
{
  NSException *result;
  NGSendMail *mailer;

  mailer = [NGSendMail sharedSendMail];
  if ([mailer isSendMailAvailable])
    result = [mailer sendMailData: mailData
		     toRecipients: recipients
		     sender: sender];
  else
    result = [NSException exceptionWithHTTPStatus: 500
			  reason: @"cannot send message:"
			  @" no sendmail binary!"];

  return result;
}

- (NSException *) _sendMailData: (NSData *) mailData
		     withClient: (NGSmtpClient *) client
{
  NSException *result;

  if ([client sendData: mailData])
    result = nil;
  else
    result = [NSException exceptionWithHTTPStatus: 500
			  reason: @"cannot send message:"
			  @" (smtp) failure when sending data"];

  return result;
}

- (NSException *) _smtpSendData: (NSData *) mailData
		   toRecipients: (NSArray *) recipients
			 sender: (NSString *) sender
{
  NGInternetSocketAddress *addr;
  NSString *currentTo, *host;
  NSMutableArray *toErrors;
  NSEnumerator *addresses; 
  NGSmtpClient *client;
  NSException *result;
  NSRange r;

  unsigned int port;

  client = [NGSmtpClient smtpClient];
  host = smtpServer;
  port = 25;

  // We check if there is a port specified in the smtpServer ivar value
  r = [smtpServer rangeOfString: @":"];
  
  if (r.length)
    {
      port = [[smtpServer substringFromIndex: r.location+1] intValue];
      host = [smtpServer substringToIndex: r.location];
    }

  addr = [NGInternetSocketAddress addressWithPort: port
				  onHost: host];

  NS_DURING
    {
      [client connectToAddress: addr];
      if ([client mailFrom: sender])
	{
	  toErrors = [NSMutableArray array];
	  addresses = [recipients objectEnumerator];
	  currentTo = [addresses nextObject];
	  while (currentTo)
	    {
	      if (![client recipientTo: [currentTo pureEMailAddress]])
		{
		  [self logWithFormat: @"error with recipient '%@'", currentTo];
		  [toErrors addObject: [currentTo pureEMailAddress]];
		}
	      currentTo = [addresses nextObject];
	    }
	  if ([toErrors count] == [recipients count])
	    result = [NSException exceptionWithHTTPStatus: 500
				  reason: @"cannot send message:"
				  @" (smtp) all recipients discarded"];
	  else if ([toErrors count] > 0)
	    result = [NSException exceptionWithHTTPStatus: 500
				  reason: [NSString stringWithFormat: 
						      @"cannot send message (smtp) - recipients discarded:\n%@",
						    [toErrors componentsJoinedByString: @", "]]];
	  else
	    result = [self _sendMailData: mailData withClient: client];
	}
      else
        result = [NSException
                   exceptionWithHTTPStatus: 500
                                    reason: @"cannot send message: (smtp) originator not accepted"];
      [client quit];
      [client disconnect];
    }
  NS_HANDLER
    {
      NSLog(@"Could not connect to the SMTP server %@ on port %d", host, port);
      result = [NSException exceptionWithHTTPStatus: 500
					     reason: @"cannot send message:"
			    @" (smtp) error when connecting"];
    }
  NS_ENDHANDLER;

  return result;
}

- (NSException *) sendMailData: (NSData *) data
		  toRecipients: (NSArray *) recipients
			sender: (NSString *) sender
{
  NSException *result;

  if (![recipients count])
    result = [NSException exceptionWithHTTPStatus: 500
			  reason: @"cannot send message: no recipients set"];
  else
    {
      if (![sender length])
	result = [NSException exceptionWithHTTPStatus: 500
			      reason: @"cannot send message: no sender set"];
      else
	{
	  if ([mailingMechanism isEqualToString: @"sendmail"])
	    result = [self _sendmailSendData: data
			   toRecipients: recipients
			   sender: [sender pureEMailAddress]];
	  else
	    result = [self _smtpSendData: data
			   toRecipients: recipients
			   sender: [sender pureEMailAddress]];
	}
    }

  return result;
}

- (NSException *) sendMimePart: (id <NGMimePart>) part
		  toRecipients: (NSArray *) recipients
			sender: (NSString *) sender
{
  NSData *mailData;

  mailData = [[NGMimePartGenerator mimePartGenerator]
	       generateMimeFromPart: part];

  return [self sendMailData: mailData
	       toRecipients: recipients
	       sender: sender];
}

- (NSException *) sendMailAtPath: (NSString *) filename
		    toRecipients: (NSArray *) recipients
			  sender: (NSString *) sender
{
  NSException *result;
  NSData *mailData;

  mailData = [NSData dataWithContentsOfFile: filename];
  if ([mailData length] > 0)
    result = [self sendMailData: mailData
		   toRecipients: recipients
		   sender: sender];
  else
    result = [NSException exceptionWithHTTPStatus: 500
			  reason: @"cannot send message: no data"
			  @" (missing or empty file?)"];

  return nil;
}

@end