Diffstat (limited to 'noncore/net/mail/smtpwrapper.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mail/smtpwrapper.cpp | 732 |
1 files changed, 0 insertions, 732 deletions
diff --git a/noncore/net/mail/smtpwrapper.cpp b/noncore/net/mail/smtpwrapper.cpp deleted file mode 100644 index 7e03af9..0000000 --- a/noncore/net/mail/smtpwrapper.cpp +++ b/dev/null | |||
@@ -1,732 +0,0 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <sys/stat.h> | ||
3 | #include <sys/types.h> | ||
4 | #include <unistd.h> | ||
5 | #include <fcntl.h> | ||
6 | #include <string.h> | ||
7 | #include <qdir.h> | ||
8 | #include <qt.h> | ||
9 | |||
10 | #include <qpe/config.h> | ||
11 | #include <qpe/qcopenvelope_qws.h> | ||
12 | |||
13 | #include <libetpan/libetpan.h> | ||
14 | |||
15 | #include "smtpwrapper.h" | ||
16 | #include "mailwrapper.h" | ||
17 | #include "mboxwrapper.h" | ||
18 | #include "logindialog.h" | ||
19 | #include "mailtypes.h" | ||
20 | #include "defines.h" | ||
21 | #include "sendmailprogress.h" | ||
22 | |||
23 | progressMailSend*SMTPwrapper::sendProgress = 0; | ||
24 | |||
25 | SMTPwrapper::SMTPwrapper( Settings *s ) | ||
26 | : QObject() | ||
27 | { | ||
28 | settings = s; | ||
29 | Config cfg( "mail" ); | ||
30 | cfg.setGroup( "Status" ); | ||
31 | m_queuedMail = cfg.readNumEntry( "outgoing", 0 ); | ||
32 | emit queuedMails( m_queuedMail ); | ||
33 | connect( this, SIGNAL( queuedMails( int ) ), this, SLOT( emitQCop( int ) ) ); | ||
34 | } | ||
35 | |||
36 | void SMTPwrapper::emitQCop( int queued ) { | ||
37 | QCopEnvelope env( "QPE/Pim", "outgoingMails(int)" ); | ||
38 | env << queued; | ||
39 | } | ||
40 | |||
41 | QString SMTPwrapper::mailsmtpError( int errnum ) | ||
42 | { | ||
43 | switch ( errnum ) { | ||
44 | case MAILSMTP_NO_ERROR: | ||
45 | return tr( "No error" ); | ||
46 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | ||
47 | return tr( "Unexpected error code" ); | ||
48 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | ||
49 | return tr( "Service not available" ); | ||
50 | case MAILSMTP_ERROR_STREAM: | ||
51 | return tr( "Stream error" ); | ||
52 | case MAILSMTP_ERROR_HOSTNAME: | ||
53 | return tr( "gethostname() failed" ); | ||
54 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | ||
55 | return tr( "Not implemented" ); | ||
56 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | ||
57 | return tr( "Error, action not taken" ); | ||
58 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | ||
59 | return tr( "Data exceeds storage allocation" ); | ||
60 | case MAILSMTP_ERROR_IN_PROCESSING: | ||
61 | return tr( "Error in processing" ); | ||
62 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | ||
63 | // return tr( "Insufficient system storage" ); | ||
64 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | ||
65 | return tr( "Mailbox unavailable" ); | ||
66 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | ||
67 | return tr( "Mailbox name not allowed" ); | ||
68 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | ||
69 | return tr( "Bad command sequence" ); | ||
70 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | ||
71 | return tr( "User not local" ); | ||
72 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | ||
73 | return tr( "Transaction failed" ); | ||
74 | case MAILSMTP_ERROR_MEMORY: | ||
75 | return tr( "Memory error" ); | ||
76 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | ||
77 | return tr( "Connection refused" ); | ||
78 | default: | ||
79 | return tr( "Unknown error code" ); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) | ||
84 | { | ||
85 | return mailimf_mailbox_new( strdup( name.latin1() ), | ||
86 | strdup( mail.latin1() ) ); | ||
87 | } | ||
88 | |||
89 | mailimf_address_list *SMTPwrapper::parseAddresses(const QString&addr ) | ||
90 | { | ||
91 | mailimf_address_list *addresses; | ||
92 | |||
93 | if ( addr.isEmpty() ) return NULL; | ||
94 | |||
95 | addresses = mailimf_address_list_new_empty(); | ||
96 | |||
97 | bool literal_open = false; | ||
98 | unsigned int startpos = 0; | ||
99 | QStringList list; | ||
100 | QString s; | ||
101 | unsigned int i = 0; | ||
102 | for (; i < addr.length();++i) { | ||
103 | switch (addr[i]) { | ||
104 | case '\"': | ||
105 | literal_open = !literal_open; | ||
106 | break; | ||
107 | case ',': | ||
108 | if (!literal_open) { | ||
109 | s = addr.mid(startpos,i-startpos); | ||
110 | if (!s.isEmpty()) { | ||
111 | list.append(s); | ||
112 | qDebug("Appended %s",s.latin1()); | ||
113 | } | ||
114 | // !!!! this is a MUST BE! | ||
115 | startpos = ++i; | ||
116 | } | ||
117 | break; | ||
118 | default: | ||
119 | break; | ||
120 | } | ||
121 | } | ||
122 | s = addr.mid(startpos,i-startpos); | ||
123 | if (!s.isEmpty()) { | ||
124 | list.append(s); | ||
125 | qDebug("Appended %s",s.latin1()); | ||
126 | } | ||
127 | QStringList::Iterator it; | ||
128 | for ( it = list.begin(); it != list.end(); it++ ) { | ||
129 | int err = mailimf_address_list_add_parse( addresses, (char*)(*it).latin1() ); | ||
130 | if ( err != MAILIMF_NO_ERROR ) { | ||
131 | qDebug( "Error parsing" ); | ||
132 | qDebug( *it ); | ||
133 | } else { | ||
134 | qDebug( "Parse success! %s",(*it).latin1()); | ||
135 | } | ||
136 | } | ||
137 | return addresses; | ||
138 | } | ||
139 | |||
140 | mailimf_fields *SMTPwrapper::createImfFields(const Mail&mail ) | ||
141 | { | ||
142 | mailimf_fields *fields; | ||
143 | mailimf_field *xmailer; | ||
144 | mailimf_mailbox *sender=0,*fromBox=0; | ||
145 | mailimf_mailbox_list *from=0; | ||
146 | mailimf_address_list *to=0, *cc=0, *bcc=0, *reply=0; | ||
147 | char *subject = strdup( mail.getSubject().latin1() ); | ||
148 | int err; | ||
149 | |||
150 | sender = newMailbox( mail.getName(), mail.getMail() ); | ||
151 | if ( sender == NULL ) goto err_free; | ||
152 | |||
153 | fromBox = newMailbox( mail.getName(), mail.getMail() ); | ||
154 | if ( fromBox == NULL ) goto err_free_sender; | ||
155 | |||
156 | from = mailimf_mailbox_list_new_empty(); | ||
157 | if ( from == NULL ) goto err_free_fromBox; | ||
158 | |||
159 | err = mailimf_mailbox_list_add( from, fromBox ); | ||
160 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; | ||
161 | |||
162 | to = parseAddresses( mail.getTo() ); | ||
163 | if ( to == NULL ) goto err_free_from; | ||
164 | |||
165 | cc = parseAddresses( mail.getCC() ); | ||
166 | bcc = parseAddresses( mail.getBCC() ); | ||
167 | reply = parseAddresses( mail.getReply() ); | ||
168 | |||
169 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, | ||
170 | NULL, NULL, subject ); | ||
171 | if ( fields == NULL ) goto err_free_reply; | ||
172 | |||
173 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), | ||
174 | strdup( USER_AGENT ) ); | ||
175 | if ( xmailer == NULL ) goto err_free_fields; | ||
176 | |||
177 | err = mailimf_fields_add( fields, xmailer ); | ||
178 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; | ||
179 | |||
180 | return fields; // Success :) | ||
181 | |||
182 | err_free_xmailer: | ||
183 | if (xmailer) mailimf_field_free( xmailer ); | ||
184 | err_free_fields: | ||
185 | if (fields) mailimf_fields_free( fields ); | ||
186 | err_free_reply: | ||
187 | if (reply) mailimf_address_list_free( reply ); | ||
188 | if (bcc) mailimf_address_list_free( bcc ); | ||
189 | if (cc) mailimf_address_list_free( cc ); | ||
190 | if (to) mailimf_address_list_free( to ); | ||
191 | err_free_from: | ||
192 | if (from) mailimf_mailbox_list_free( from ); | ||
193 | err_free_fromBox: | ||
194 | mailimf_mailbox_free( fromBox ); | ||
195 | err_free_sender: | ||
196 | if (sender) mailimf_mailbox_free( sender ); | ||
197 | err_free: | ||
198 | if (subject) free( subject ); | ||
199 | qDebug( "createImfFields - error" ); | ||
200 | |||
201 | return NULL; // Error :( | ||
202 | } | ||
203 | |||
204 | mailmime *SMTPwrapper::buildTxtPart(const QString&str ) | ||
205 | { | ||
206 | mailmime *txtPart; | ||
207 | mailmime_fields *fields; | ||
208 | mailmime_content *content; | ||
209 | mailmime_parameter *param; | ||
210 | int err; | ||
211 | |||
212 | param = mailmime_parameter_new( strdup( "charset" ), | ||
213 | strdup( "iso-8859-1" ) ); | ||
214 | if ( param == NULL ) goto err_free; | ||
215 | |||
216 | content = mailmime_content_new_with_str( "text/plain" ); | ||
217 | if ( content == NULL ) goto err_free_param; | ||
218 | |||
219 | err = clist_append( content->ct_parameters, param ); | ||
220 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; | ||
221 | |||
222 | fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT); | ||
223 | if ( fields == NULL ) goto err_free_content; | ||
224 | |||
225 | txtPart = mailmime_new_empty( content, fields ); | ||
226 | if ( txtPart == NULL ) goto err_free_fields; | ||
227 | |||
228 | err = mailmime_set_body_text( txtPart, (char*)str.data(), str.length() ); | ||
229 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | ||
230 | |||
231 | return txtPart; // Success :) | ||
232 | |||
233 | err_free_txtPart: | ||
234 | mailmime_free( txtPart ); | ||
235 | err_free_fields: | ||
236 | mailmime_fields_free( fields ); | ||
237 | err_free_content: | ||
238 | mailmime_content_free( content ); | ||
239 | err_free_param: | ||
240 | mailmime_parameter_free( param ); | ||
241 | err_free: | ||
242 | qDebug( "buildTxtPart - error" ); | ||
243 | |||
244 | return NULL; // Error :( | ||
245 | } | ||
246 | |||
247 | mailmime *SMTPwrapper::buildFilePart(const QString&filename,const QString&mimetype,const QString&TextContent ) | ||
248 | { | ||
249 | mailmime * filePart = 0; | ||
250 | mailmime_fields * fields = 0; | ||
251 | mailmime_content * content = 0; | ||
252 | mailmime_parameter * param = 0; | ||
253 | char*name = 0; | ||
254 | char*file = 0; | ||
255 | int err; | ||
256 | |||
257 | int pos = filename.findRev( '/' ); | ||
258 | |||
259 | if (filename.length()>0) { | ||
260 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); | ||
261 | name = strdup( tmp.latin1() ); // just filename | ||
262 | file = strdup( filename.latin1() ); // full name with path | ||
263 | } | ||
264 | |||
265 | int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; | ||
266 | int mechanism = MAILMIME_MECHANISM_BASE64; | ||
267 | |||
268 | if ( mimetype.startsWith( "text/" ) ) { | ||
269 | param = mailmime_parameter_new( strdup( "charset" ), | ||
270 | strdup( "iso-8859-1" ) ); | ||
271 | mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; | ||
272 | } | ||
273 | |||
274 | fields = mailmime_fields_new_filename( | ||
275 | disptype, name, | ||
276 | mechanism ); | ||
277 | content = mailmime_content_new_with_str( (char*)mimetype.latin1() ); | ||
278 | if (content!=0 && fields != 0) { | ||
279 | if (param) { | ||
280 | clist_append(content->ct_parameters,param); | ||
281 | param = 0; | ||
282 | } | ||
283 | if (filename.length()>0) { | ||
284 | QFileInfo f(filename); | ||
285 | param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); | ||
286 | clist_append(content->ct_parameters,param); | ||
287 | param = 0; | ||
288 | } | ||
289 | filePart = mailmime_new_empty( content, fields ); | ||
290 | } | ||
291 | if (filePart) { | ||
292 | if (filename.length()>0) { | ||
293 | err = mailmime_set_body_file( filePart, file ); | ||
294 | } else { | ||
295 | err = mailmime_set_body_text(filePart,strdup(TextContent.data()),TextContent.length()); | ||
296 | } | ||
297 | if (err != MAILIMF_NO_ERROR) { | ||
298 | qDebug("Error setting body with file %s",file); | ||
299 | mailmime_free( filePart ); | ||
300 | filePart = 0; | ||
301 | } | ||
302 | } | ||
303 | |||
304 | if (!filePart) { | ||
305 | if ( param != NULL ) { | ||
306 | mailmime_parameter_free( param ); | ||
307 | } | ||
308 | if (content) { | ||
309 | mailmime_content_free( content ); | ||
310 | } | ||
311 | if (fields) { | ||
312 | mailmime_fields_free( fields ); | ||
313 | } else { | ||
314 | if (name) { | ||
315 | free( name ); | ||
316 | } | ||
317 | if (file) { | ||
318 | free( file ); | ||
319 | } | ||
320 | } | ||
321 | } | ||
322 | return filePart; // Success :) | ||
323 | |||
324 | } | ||
325 | |||
326 | void SMTPwrapper::addFileParts( mailmime *message,const QList<Attachment>&files ) | ||
327 | { | ||
328 | const Attachment *it; | ||
329 | unsigned int count = files.count(); | ||
330 | qDebug("List contains %i values",count); | ||
331 | for ( unsigned int i = 0; i < count; ++i ) { | ||
332 | qDebug( "Adding file" ); | ||
333 | mailmime *filePart; | ||
334 | int err; | ||
335 | it = ((QList<Attachment>)files).at(i); | ||
336 | |||
337 | filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); | ||
338 | if ( filePart == NULL ) { | ||
339 | qDebug( "addFileParts: error adding file:" ); | ||
340 | qDebug( it->getFileName() ); | ||
341 | continue; | ||
342 | } | ||
343 | err = mailmime_smart_add_part( message, filePart ); | ||
344 | if ( err != MAILIMF_NO_ERROR ) { | ||
345 | mailmime_free( filePart ); | ||
346 | qDebug("error smart add"); | ||
347 | } | ||
348 | } | ||
349 | } | ||
350 | |||
351 | mailmime *SMTPwrapper::createMimeMail(const Mail &mail ) | ||
352 | { | ||
353 | mailmime *message, *txtPart; | ||
354 | mailimf_fields *fields; | ||
355 | int err; | ||
356 | |||
357 | fields = createImfFields( mail ); | ||
358 | if ( fields == NULL ) goto err_free; | ||
359 | |||
360 | message = mailmime_new_message_data( NULL ); | ||
361 | if ( message == NULL ) goto err_free_fields; | ||
362 | |||
363 | mailmime_set_imf_fields( message, fields ); | ||
364 | |||
365 | txtPart = buildTxtPart( mail.getMessage() ); | ||
366 | |||
367 | if ( txtPart == NULL ) goto err_free_message; | ||
368 | |||
369 | err = mailmime_smart_add_part( message, txtPart ); | ||
370 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | ||
371 | |||
372 | addFileParts( message, mail.getAttachments() ); | ||
373 | |||
374 | return message; // Success :) | ||
375 | |||
376 | err_free_txtPart: | ||
377 | mailmime_free( txtPart ); | ||
378 | err_free_message: | ||
379 | mailmime_free( message ); | ||
380 | err_free_fields: | ||
381 | mailimf_fields_free( fields ); | ||
382 | err_free: | ||
383 | qDebug( "createMimeMail: error" ); | ||
384 | |||
385 | return NULL; // Error :( | ||
386 | } | ||
387 | |||
388 | mailimf_field *SMTPwrapper::getField( mailimf_fields *fields, int type ) | ||
389 | { | ||
390 | mailimf_field *field; | ||
391 | clistiter *it; | ||
392 | |||
393 | it = clist_begin( fields->fld_list ); | ||
394 | while ( it ) { | ||
395 | field = (mailimf_field *) it->data; | ||
396 | if ( field->fld_type == type ) { | ||
397 | return field; | ||
398 | } | ||
399 | it = it->next; | ||
400 | } | ||
401 | |||
402 | return NULL; | ||
403 | } | ||
404 | |||
405 | void SMTPwrapper::addRcpts( clist *list, mailimf_address_list *addr_list ) | ||
406 | { | ||
407 | clistiter *it, *it2; | ||
408 | |||
409 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { | ||
410 | mailimf_address *addr; | ||
411 | addr = (mailimf_address *) it->data; | ||
412 | |||
413 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { | ||
414 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); | ||
415 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { | ||
416 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; | ||
417 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { | ||
418 | mailimf_mailbox *mbox; | ||
419 | mbox = (mailimf_mailbox *) it2->data; | ||
420 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); | ||
421 | } | ||
422 | } | ||
423 | } | ||
424 | } | ||
425 | |||
426 | clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) | ||
427 | { | ||
428 | clist *rcptList; | ||
429 | mailimf_field *field; | ||
430 | |||
431 | rcptList = esmtp_address_list_new(); | ||
432 | |||
433 | field = getField( fields, MAILIMF_FIELD_TO ); | ||
434 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) | ||
435 | && field->fld_data.fld_to->to_addr_list ) { | ||
436 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); | ||
437 | } | ||
438 | |||
439 | field = getField( fields, MAILIMF_FIELD_CC ); | ||
440 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) | ||
441 | && field->fld_data.fld_cc->cc_addr_list ) { | ||
442 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); | ||
443 | } | ||
444 | |||
445 | field = getField( fields, MAILIMF_FIELD_BCC ); | ||
446 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) | ||
447 | && field->fld_data.fld_bcc->bcc_addr_list ) { | ||
448 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); | ||
449 | } | ||
450 | |||
451 | return rcptList; | ||
452 | } | ||
453 | |||
454 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) | ||
455 | { | ||
456 | char *from = NULL; | ||
457 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) | ||
458 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { | ||
459 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; | ||
460 | clistiter *it; | ||
461 | for ( it = clist_begin( cl ); it; it = it->next ) { | ||
462 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | ||
463 | from = strdup( mb->mb_addr_spec ); | ||
464 | } | ||
465 | } | ||
466 | |||
467 | return from; | ||
468 | } | ||
469 | |||
470 | char *SMTPwrapper::getFrom( mailmime *mail ) | ||
471 | { | ||
472 | /* no need to delete - its just a pointer to structure content */ | ||
473 | mailimf_field *ffrom = 0; | ||
474 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); | ||
475 | return getFrom(ffrom); | ||
476 | } | ||
477 | |||
478 | void SMTPwrapper::progress( size_t current, size_t maximum ) | ||
479 | { | ||
480 | if (SMTPwrapper::sendProgress) { | ||
481 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); | ||
482 | qApp->processEvents(); | ||
483 | } | ||
484 | } | ||
485 | |||
486 | void SMTPwrapper::storeMail(char*mail, size_t length, const QString&box) | ||
487 | { | ||
488 | if (!mail) return; | ||
489 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); | ||
490 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | ||
491 | wrap->storeMessage(mail,length,box); | ||
492 | delete wrap; | ||
493 | } | ||
494 | |||
495 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) | ||
496 | { | ||
497 | clist *rcpts = 0; | ||
498 | char *from, *data; | ||
499 | size_t size; | ||
500 | |||
501 | if ( smtp == NULL ) { | ||
502 | return; | ||
503 | } | ||
504 | from = data = 0; | ||
505 | |||
506 | mailmessage * msg = 0; | ||
507 | msg = mime_message_init(mail); | ||
508 | mime_message_set_tmpdir(msg,getenv( "HOME" )); | ||
509 | int r = mailmessage_fetch(msg,&data,&size); | ||
510 | mime_message_detach_mime(msg); | ||
511 | mailmessage_free(msg); | ||
512 | if (r != MAIL_NO_ERROR || !data) { | ||
513 | if (data) free(data); | ||
514 | qDebug("Error fetching mime..."); | ||
515 | return; | ||
516 | } | ||
517 | QString tmp = data; | ||
518 | tmp.replace(QRegExp("\r+",true,false),""); | ||
519 | msg = 0; | ||
520 | if (later) { | ||
521 | storeMail((char*)tmp.data(),tmp.length(),"Outgoing"); | ||
522 | if (data) free( data ); | ||
523 | Config cfg( "mail" ); | ||
524 | cfg.setGroup( "Status" ); | ||
525 | cfg.writeEntry( "outgoing", ++m_queuedMail ); | ||
526 | emit queuedMails( m_queuedMail ); | ||
527 | return; | ||
528 | } | ||
529 | from = getFrom( mail ); | ||
530 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | ||
531 | smtpSend(from,rcpts,data,size,smtp); | ||
532 | if (data) {free(data);} | ||
533 | if (from) {free(from);} | ||
534 | if (rcpts) smtp_address_list_free( rcpts ); | ||
535 | } | ||
536 | |||
537 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ) | ||
538 | { | ||
539 | char *server, *user, *pass; | ||
540 | bool ssl; | ||
541 | uint16_t port; | ||
542 | mailsmtp *session; | ||
543 | int err,result; | ||
544 | |||
545 | result = 1; | ||
546 | server = user = pass = 0; | ||
547 | server = strdup( smtp->getServer().latin1() ); | ||
548 | ssl = smtp->getSSL(); | ||
549 | port = smtp->getPort().toUInt(); | ||
550 | |||
551 | session = mailsmtp_new( 20, &progress ); | ||
552 | if ( session == NULL ) goto free_mem; | ||
553 | |||
554 | qDebug( "Servername %s at port %i", server, port ); | ||
555 | if ( ssl ) { | ||
556 | qDebug( "SSL session" ); | ||
557 | err = mailsmtp_ssl_connect( session, server, port ); | ||
558 | } else { | ||
559 | qDebug( "No SSL session" ); | ||
560 | err = mailsmtp_socket_connect( session, server, port ); | ||
561 | } | ||
562 | if ( err != MAILSMTP_NO_ERROR ) {result = 0;goto free_mem_session;} | ||
563 | |||
564 | err = mailsmtp_init( session ); | ||
565 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} | ||
566 | |||
567 | qDebug( "INIT OK" ); | ||
568 | |||
569 | if ( smtp->getLogin() ) { | ||
570 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | ||
571 | // get'em | ||
572 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | ||
573 | login.show(); | ||
574 | if ( QDialog::Accepted == login.exec() ) { | ||
575 | // ok | ||
576 | user = strdup( login.getUser().latin1() ); | ||
577 | pass = strdup( login.getPassword().latin1() ); | ||
578 | } else { | ||
579 | result = 0; goto free_con_session; | ||
580 | } | ||
581 | } else { | ||
582 | user = strdup( smtp->getUser().latin1() ); | ||
583 | pass = strdup( smtp->getPassword().latin1() ); | ||
584 | } | ||
585 | qDebug( "session->auth: %i", session->auth); | ||
586 | err = mailsmtp_auth( session, user, pass ); | ||
587 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); | ||
588 | qDebug( "Done auth!" ); | ||
589 | } | ||
590 | |||
591 | err = mailsmtp_send( session, from, rcpts, data, size ); | ||
592 | if ( err != MAILSMTP_NO_ERROR ) { | ||
593 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); | ||
594 | result = 0; goto free_con_session; | ||
595 | } | ||
596 | |||
597 | qDebug( "Mail sent." ); | ||
598 | storeMail(data,size,"Sent"); | ||
599 | |||
600 | free_con_session: | ||
601 | mailsmtp_quit( session ); | ||
602 | free_mem_session: | ||
603 | mailsmtp_free( session ); | ||
604 | free_mem: | ||
605 | if (server) free( server ); | ||
606 | if ( smtp->getLogin() ) { | ||
607 | free( user ); | ||
608 | free( pass ); | ||
609 | } | ||
610 | return result; | ||
611 | } | ||
612 | |||
613 | void SMTPwrapper::sendMail(const Mail&mail,SMTPaccount*aSmtp,bool later ) | ||
614 | { | ||
615 | mailmime * mimeMail; | ||
616 | |||
617 | SMTPaccount *smtp = aSmtp; | ||
618 | |||
619 | if (!later && !smtp) { | ||
620 | qDebug("Didn't get any send method - giving up"); | ||
621 | return; | ||
622 | } | ||
623 | mimeMail = createMimeMail(mail ); | ||
624 | if ( mimeMail == NULL ) { | ||
625 | qDebug( "sendMail: error creating mime mail" ); | ||
626 | } else { | ||
627 | sendProgress = new progressMailSend(); | ||
628 | sendProgress->show(); | ||
629 | sendProgress->setMaxMails(1); | ||
630 | smtpSend( mimeMail,later,smtp); | ||
631 | qDebug("Clean up done"); | ||
632 | sendProgress->hide(); | ||
633 | delete sendProgress; | ||
634 | sendProgress = 0; | ||
635 | mailmime_free( mimeMail ); | ||
636 | } | ||
637 | } | ||
638 | |||
639 | int SMTPwrapper::sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which) | ||
640 | { | ||
641 | char*data = 0; | ||
642 | size_t length = 0; | ||
643 | size_t curTok = 0; | ||
644 | mailimf_fields *fields = 0; | ||
645 | mailimf_field*ffrom = 0; | ||
646 | clist *rcpts = 0; | ||
647 | char*from = 0; | ||
648 | int res = 0; | ||
649 | |||
650 | wrap->fetchRawBody(*which,&data,&length); | ||
651 | if (!data) return 0; | ||
652 | int err = mailimf_fields_parse( data, length, &curTok, &fields ); | ||
653 | if (err != MAILIMF_NO_ERROR) { | ||
654 | free(data); | ||
655 | delete wrap; | ||
656 | return 0; | ||
657 | } | ||
658 | |||
659 | rcpts = createRcptList( fields ); | ||
660 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); | ||
661 | from = getFrom(ffrom); | ||
662 | |||
663 | qDebug("Size: %i vs. %i",length,strlen(data)); | ||
664 | if (rcpts && from) { | ||
665 | res = smtpSend(from,rcpts,data,length,smtp ); | ||
666 | } | ||
667 | if (fields) { | ||
668 | mailimf_fields_free(fields); | ||
669 | fields = 0; | ||
670 | } | ||
671 | if (data) { | ||
672 | free(data); | ||
673 | } | ||
674 | if (from) { | ||
675 | free(from); | ||
676 | } | ||
677 | if (rcpts) { | ||
678 | smtp_address_list_free( rcpts ); | ||
679 | } | ||
680 | return res; | ||
681 | } | ||
682 | |||
683 | /* this is a special fun */ | ||
684 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) | ||
685 | { | ||
686 | bool returnValue = true; | ||
687 | |||
688 | if (!smtp) return false; | ||
689 | |||
690 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); | ||
691 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | ||
692 | if (!wrap) { | ||
693 | qDebug("memory error"); | ||
694 | return false; | ||
695 | } | ||
696 | QList<RecMail> mailsToSend; | ||
697 | QList<RecMail> mailsToRemove; | ||
698 | QString mbox("Outgoing"); | ||
699 | wrap->listMessages(mbox,mailsToSend); | ||
700 | if (mailsToSend.count()==0) { | ||
701 | delete wrap; | ||
702 | return false; | ||
703 | } | ||
704 | mailsToSend.setAutoDelete(false); | ||
705 | sendProgress = new progressMailSend(); | ||
706 | sendProgress->show(); | ||
707 | sendProgress->setMaxMails(mailsToSend.count()); | ||
708 | |||
709 | while (mailsToSend.count()>0) { | ||
710 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { | ||
711 | QMessageBox::critical(0,tr("Error sending mail"), | ||
712 | tr("Error sending queued mail - breaking")); | ||
713 | returnValue = false; | ||
714 | break; | ||
715 | } | ||
716 | mailsToRemove.append(mailsToSend.at(0)); | ||
717 | mailsToSend.removeFirst(); | ||
718 | sendProgress->setCurrentMails(mailsToRemove.count()); | ||
719 | } | ||
720 | Config cfg( "mail" ); | ||
721 | cfg.setGroup( "Status" ); | ||
722 | m_queuedMail = 0; | ||
723 | cfg.writeEntry( "outgoing", m_queuedMail ); | ||
724 | emit queuedMails( m_queuedMail ); | ||
725 | sendProgress->hide(); | ||
726 | delete sendProgress; | ||
727 | sendProgress = 0; | ||
728 | wrap->deleteMails(mbox,mailsToRemove); | ||
729 | mailsToSend.setAutoDelete(true); | ||
730 | delete wrap; | ||
731 | return returnValue; | ||
732 | } | ||