-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.cpp | 24 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.h | 1 |
2 files changed, 24 insertions, 1 deletions
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp index 085d5e4..7671133 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp | |||
@@ -1,104 +1,105 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <sys/stat.h> | 2 | #include <sys/stat.h> |
3 | #include <sys/types.h> | 3 | #include <sys/types.h> |
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | #include <fcntl.h> | 5 | #include <fcntl.h> |
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <qdir.h> | 7 | #include <qdir.h> |
8 | #include <qt.h> | 8 | #include <qt.h> |
9 | #include <qmessagebox.h> | ||
9 | 10 | ||
10 | #include <qpe/config.h> | 11 | #include <qpe/config.h> |
11 | #include <qpe/qcopenvelope_qws.h> | 12 | #include <qpe/qcopenvelope_qws.h> |
12 | 13 | ||
13 | #include <libetpan/libetpan.h> | 14 | #include <libetpan/libetpan.h> |
14 | 15 | ||
15 | #include "smtpwrapper.h" | 16 | #include "smtpwrapper.h" |
16 | #include "mailwrapper.h" | 17 | #include "mailwrapper.h" |
17 | #include "abstractmail.h" | 18 | #include "abstractmail.h" |
18 | #include "logindialog.h" | 19 | #include "logindialog.h" |
19 | #include "mailtypes.h" | 20 | #include "mailtypes.h" |
20 | //#include "defines.h" | 21 | //#include "defines.h" |
21 | #include "sendmailprogress.h" | 22 | #include "sendmailprogress.h" |
22 | 23 | ||
23 | const char* SMTPwrapper::USER_AGENT="OpieMail v0.3"; | 24 | const char* SMTPwrapper::USER_AGENT="OpieMail v0.3"; |
24 | 25 | ||
25 | progressMailSend*SMTPwrapper::sendProgress = 0; | 26 | progressMailSend*SMTPwrapper::sendProgress = 0; |
26 | 27 | ||
27 | SMTPwrapper::SMTPwrapper( Settings *s ) | 28 | SMTPwrapper::SMTPwrapper( Settings *s ) |
28 | : QObject() { | 29 | : QObject() { |
29 | settings = s; | 30 | settings = s; |
30 | Config cfg( "mail" ); | 31 | Config cfg( "mail" ); |
31 | cfg.setGroup( "Status" ); | 32 | cfg.setGroup( "Status" ); |
32 | m_queuedMail = cfg.readNumEntry( "outgoing", 0 ); | 33 | m_queuedMail = cfg.readNumEntry( "outgoing", 0 ); |
33 | emit queuedMails( m_queuedMail ); | 34 | emit queuedMails( m_queuedMail ); |
34 | connect( this, SIGNAL( queuedMails( int ) ), this, SLOT( emitQCop( int ) ) ); | 35 | connect( this, SIGNAL( queuedMails( int ) ), this, SLOT( emitQCop( int ) ) ); |
35 | } | 36 | } |
36 | 37 | ||
37 | void SMTPwrapper::emitQCop( int queued ) { | 38 | void SMTPwrapper::emitQCop( int queued ) { |
38 | QCopEnvelope env( "QPE/Pim", "outgoingMails(int)" ); | 39 | QCopEnvelope env( "QPE/Pim", "outgoingMails(int)" ); |
39 | env << queued; | 40 | env << queued; |
40 | } | 41 | } |
41 | 42 | ||
42 | QString SMTPwrapper::mailsmtpError( int errnum ) { | 43 | QString SMTPwrapper::mailsmtpError( int errnum ) { |
43 | switch ( errnum ) { | 44 | switch ( errnum ) { |
44 | case MAILSMTP_NO_ERROR: | 45 | case MAILSMTP_NO_ERROR: |
45 | return tr( "No error" ); | 46 | return tr( "No error" ); |
46 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | 47 | case MAILSMTP_ERROR_UNEXPECTED_CODE: |
47 | return tr( "Unexpected error code" ); | 48 | return tr( "Unexpected error code" ); |
48 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | 49 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: |
49 | return tr( "Service not available" ); | 50 | return tr( "Service not available" ); |
50 | case MAILSMTP_ERROR_STREAM: | 51 | case MAILSMTP_ERROR_STREAM: |
51 | return tr( "Stream error" ); | 52 | return tr( "Stream error" ); |
52 | case MAILSMTP_ERROR_HOSTNAME: | 53 | case MAILSMTP_ERROR_HOSTNAME: |
53 | return tr( "gethostname() failed" ); | 54 | return tr( "gethostname() failed" ); |
54 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | 55 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: |
55 | return tr( "Not implemented" ); | 56 | return tr( "Not implemented" ); |
56 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | 57 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: |
57 | return tr( "Error, action not taken" ); | 58 | return tr( "Error, action not taken" ); |
58 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | 59 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: |
59 | return tr( "Data exceeds storage allocation" ); | 60 | return tr( "Data exceeds storage allocation" ); |
60 | case MAILSMTP_ERROR_IN_PROCESSING: | 61 | case MAILSMTP_ERROR_IN_PROCESSING: |
61 | return tr( "Error in processing" ); | 62 | return tr( "Error in processing" ); |
62 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | 63 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: |
63 | // return tr( "Insufficient system storage" ); | 64 | // return tr( "Insufficient system storage" ); |
64 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | 65 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: |
65 | return tr( "Mailbox unavailable" ); | 66 | return tr( "Mailbox unavailable" ); |
66 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | 67 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: |
67 | return tr( "Mailbox name not allowed" ); | 68 | return tr( "Mailbox name not allowed" ); |
68 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | 69 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: |
69 | return tr( "Bad command sequence" ); | 70 | return tr( "Bad command sequence" ); |
70 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | 71 | case MAILSMTP_ERROR_USER_NOT_LOCAL: |
71 | return tr( "User not local" ); | 72 | return tr( "User not local" ); |
72 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | 73 | case MAILSMTP_ERROR_TRANSACTION_FAILED: |
73 | return tr( "Transaction failed" ); | 74 | return tr( "Transaction failed" ); |
74 | case MAILSMTP_ERROR_MEMORY: | 75 | case MAILSMTP_ERROR_MEMORY: |
75 | return tr( "Memory error" ); | 76 | return tr( "Memory error" ); |
76 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | 77 | case MAILSMTP_ERROR_CONNECTION_REFUSED: |
77 | return tr( "Connection refused" ); | 78 | return tr( "Connection refused" ); |
78 | default: | 79 | default: |
79 | return tr( "Unknown error code" ); | 80 | return tr( "Unknown error code" ); |
80 | } | 81 | } |
81 | } | 82 | } |
82 | 83 | ||
83 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) { | 84 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) { |
84 | return mailimf_mailbox_new( strdup( name.latin1() ), | 85 | return mailimf_mailbox_new( strdup( name.latin1() ), |
85 | strdup( mail.latin1() ) ); | 86 | strdup( mail.latin1() ) ); |
86 | } | 87 | } |
87 | 88 | ||
88 | mailimf_address_list *SMTPwrapper::parseAddresses(const QString&addr ) { | 89 | mailimf_address_list *SMTPwrapper::parseAddresses(const QString&addr ) { |
89 | mailimf_address_list *addresses; | 90 | mailimf_address_list *addresses; |
90 | 91 | ||
91 | if ( addr.isEmpty() ) | 92 | if ( addr.isEmpty() ) |
92 | return NULL; | 93 | return NULL; |
93 | 94 | ||
94 | addresses = mailimf_address_list_new_empty(); | 95 | addresses = mailimf_address_list_new_empty(); |
95 | 96 | ||
96 | bool literal_open = false; | 97 | bool literal_open = false; |
97 | unsigned int startpos = 0; | 98 | unsigned int startpos = 0; |
98 | QStringList list; | 99 | QStringList list; |
99 | QString s; | 100 | QString s; |
100 | unsigned int i = 0; | 101 | unsigned int i = 0; |
101 | for (; i < addr.length();++i) { | 102 | for (; i < addr.length();++i) { |
102 | switch (addr[i]) { | 103 | switch (addr[i]) { |
103 | case '\"': | 104 | case '\"': |
104 | literal_open = !literal_open; | 105 | literal_open = !literal_open; |
@@ -461,268 +462,289 @@ clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) { | |||
461 | } | 462 | } |
462 | 463 | ||
463 | field = getField( fields, MAILIMF_FIELD_BCC ); | 464 | field = getField( fields, MAILIMF_FIELD_BCC ); |
464 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) | 465 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) |
465 | && field->fld_data.fld_bcc->bcc_addr_list ) { | 466 | && field->fld_data.fld_bcc->bcc_addr_list ) { |
466 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); | 467 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); |
467 | } | 468 | } |
468 | 469 | ||
469 | return rcptList; | 470 | return rcptList; |
470 | } | 471 | } |
471 | 472 | ||
472 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) { | 473 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) { |
473 | char *from = NULL; | 474 | char *from = NULL; |
474 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) | 475 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) |
475 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { | 476 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { |
476 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; | 477 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; |
477 | clistiter *it; | 478 | clistiter *it; |
478 | for ( it = clist_begin( cl ); it; it = it->next ) { | 479 | for ( it = clist_begin( cl ); it; it = it->next ) { |
479 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | 480 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; |
480 | from = strdup( mb->mb_addr_spec ); | 481 | from = strdup( mb->mb_addr_spec ); |
481 | } | 482 | } |
482 | } | 483 | } |
483 | 484 | ||
484 | return from; | 485 | return from; |
485 | } | 486 | } |
486 | 487 | ||
487 | char *SMTPwrapper::getFrom( mailmime *mail ) { | 488 | char *SMTPwrapper::getFrom( mailmime *mail ) { |
488 | /* no need to delete - its just a pointer to structure content */ | 489 | /* no need to delete - its just a pointer to structure content */ |
489 | mailimf_field *ffrom = 0; | 490 | mailimf_field *ffrom = 0; |
490 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); | 491 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); |
491 | return getFrom(ffrom); | 492 | return getFrom(ffrom); |
492 | } | 493 | } |
493 | 494 | ||
494 | void SMTPwrapper::progress( size_t current, size_t maximum ) { | 495 | void SMTPwrapper::progress( size_t current, size_t maximum ) { |
495 | if (SMTPwrapper::sendProgress) { | 496 | if (SMTPwrapper::sendProgress) { |
496 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); | 497 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); |
497 | qApp->processEvents(); | 498 | qApp->processEvents(); |
498 | } | 499 | } |
499 | } | 500 | } |
500 | 501 | ||
501 | void SMTPwrapper::storeMail(const char*mail, size_t length, const QString&box) { | 502 | void SMTPwrapper::storeMail(const char*mail, size_t length, const QString&box) { |
502 | if (!mail) | 503 | if (!mail) |
503 | return; | 504 | return; |
504 | QString localfolders = AbstractMail::defaultLocalfolder(); | 505 | QString localfolders = AbstractMail::defaultLocalfolder(); |
505 | AbstractMail*wrap = AbstractMail::getWrapper(localfolders); | 506 | AbstractMail*wrap = AbstractMail::getWrapper(localfolders); |
506 | wrap->createMbox(box); | 507 | wrap->createMbox(box); |
507 | wrap->storeMessage(mail,length,box); | 508 | wrap->storeMessage(mail,length,box); |
508 | delete wrap; | 509 | delete wrap; |
509 | } | 510 | } |
510 | 511 | ||
511 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) { | 512 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) { |
512 | clist *rcpts = 0; | 513 | clist *rcpts = 0; |
513 | char *from, *data; | 514 | char *from, *data; |
514 | size_t size; | 515 | size_t size; |
515 | 516 | ||
516 | if ( smtp == NULL ) { | 517 | if ( smtp == NULL ) { |
517 | return; | 518 | return; |
518 | } | 519 | } |
519 | from = data = 0; | 520 | from = data = 0; |
520 | 521 | ||
521 | mailmessage * msg = 0; | 522 | mailmessage * msg = 0; |
522 | msg = mime_message_init(mail); | 523 | msg = mime_message_init(mail); |
523 | mime_message_set_tmpdir(msg,getenv( "HOME" )); | 524 | mime_message_set_tmpdir(msg,getenv( "HOME" )); |
524 | int r = mailmessage_fetch(msg,&data,&size); | 525 | int r = mailmessage_fetch(msg,&data,&size); |
525 | mime_message_detach_mime(msg); | 526 | mime_message_detach_mime(msg); |
526 | mailmessage_free(msg); | 527 | mailmessage_free(msg); |
527 | if (r != MAIL_NO_ERROR || !data) { | 528 | if (r != MAIL_NO_ERROR || !data) { |
528 | if (data) | 529 | if (data) |
529 | free(data); | 530 | free(data); |
530 | qDebug("Error fetching mime..."); | 531 | qDebug("Error fetching mime..."); |
531 | return; | 532 | return; |
532 | } | 533 | } |
533 | msg = 0; | 534 | msg = 0; |
534 | if (later) { | 535 | if (later) { |
535 | storeMail(data,size,"Outgoing"); | 536 | storeMail(data,size,"Outgoing"); |
536 | if (data) | 537 | if (data) |
537 | free( data ); | 538 | free( data ); |
538 | Config cfg( "mail" ); | 539 | Config cfg( "mail" ); |
539 | cfg.setGroup( "Status" ); | 540 | cfg.setGroup( "Status" ); |
540 | cfg.writeEntry( "outgoing", ++m_queuedMail ); | 541 | cfg.writeEntry( "outgoing", ++m_queuedMail ); |
541 | emit queuedMails( m_queuedMail ); | 542 | emit queuedMails( m_queuedMail ); |
542 | return; | 543 | return; |
543 | } | 544 | } |
544 | from = getFrom( mail ); | 545 | from = getFrom( mail ); |
545 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | 546 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); |
546 | smtpSend(from,rcpts,data,size,smtp); | 547 | smtpSend(from,rcpts,data,size,smtp); |
547 | if (data) { | 548 | if (data) { |
548 | free(data); | 549 | free(data); |
549 | } | 550 | } |
550 | if (from) { | 551 | if (from) { |
551 | free(from); | 552 | free(from); |
552 | } | 553 | } |
553 | if (rcpts) | 554 | if (rcpts) |
554 | smtp_address_list_free( rcpts ); | 555 | smtp_address_list_free( rcpts ); |
555 | } | 556 | } |
556 | 557 | ||
558 | void SMTPwrapper::storeFailedMail(const char*data,unsigned int size, const char*failuremessage) | ||
559 | { | ||
560 | if (data) { | ||
561 | storeMail(data,size,"Sendfailed"); | ||
562 | } | ||
563 | if (failuremessage) { | ||
564 | QMessageBox::critical(0,tr("Error sending mail"), | ||
565 | tr("<center>%1</center>").arg(failuremessage)); | ||
566 | } | ||
567 | } | ||
568 | |||
557 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ) { | 569 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ) { |
558 | const char *server, *user, *pass; | 570 | const char *server, *user, *pass; |
559 | bool ssl; | 571 | bool ssl; |
560 | uint16_t port; | 572 | uint16_t port; |
561 | mailsmtp *session; | 573 | mailsmtp *session; |
562 | int err,result; | 574 | int err,result; |
563 | 575 | ||
564 | result = 1; | 576 | result = 1; |
565 | server = user = pass = 0; | 577 | server = user = pass = 0; |
566 | server = smtp->getServer().latin1(); | 578 | server = smtp->getServer().latin1(); |
567 | 579 | ||
568 | // FIXME: currently only TLS and Plain work. | 580 | // FIXME: currently only TLS and Plain work. |
569 | 581 | ||
570 | ssl = false; | 582 | ssl = false; |
571 | 583 | ||
572 | if ( smtp->ConnectionType() == 2 ) { | 584 | if ( smtp->ConnectionType() == 2 ) { |
573 | ssl = true; | 585 | ssl = true; |
574 | } | 586 | } |
575 | 587 | ||
576 | port = smtp->getPort().toUInt(); | 588 | port = smtp->getPort().toUInt(); |
577 | 589 | ||
578 | session = mailsmtp_new( 20, &progress ); | 590 | session = mailsmtp_new( 20, &progress ); |
579 | if ( session == NULL ) | 591 | if ( session == NULL ) |
580 | goto free_mem; | 592 | goto free_mem; |
581 | 593 | ||
582 | qDebug( "Servername %s at port %i", server, port ); | 594 | qDebug( "Servername %s at port %i", server, port ); |
583 | if ( ssl ) { | 595 | if ( ssl ) { |
584 | qDebug( "SSL session" ); | 596 | qDebug( "SSL session" ); |
585 | err = mailsmtp_ssl_connect( session, server, port ); | 597 | err = mailsmtp_ssl_connect( session, server, port ); |
586 | } else { | 598 | } else { |
587 | qDebug( "No SSL session" ); | 599 | qDebug( "No SSL session" ); |
588 | err = mailsmtp_socket_connect( session, server, port ); | 600 | err = mailsmtp_socket_connect( session, server, port ); |
589 | } | 601 | } |
590 | if ( err != MAILSMTP_NO_ERROR ) { | 602 | if ( err != MAILSMTP_NO_ERROR ) { |
591 | qDebug("Error init connection"); | 603 | qDebug("Error init connection"); |
604 | storeFailedMail(data,size,mailsmtpError(err)); | ||
592 | result = 0; | 605 | result = 0; |
593 | goto free_mem_session; | 606 | goto free_mem_session; |
594 | } | 607 | } |
595 | 608 | ||
596 | err = mailsmtp_init( session ); | 609 | err = mailsmtp_init( session ); |
597 | if ( err != MAILSMTP_NO_ERROR ) { | 610 | if ( err != MAILSMTP_NO_ERROR ) { |
611 | storeFailedMail(data,size,mailsmtpError(err)); | ||
598 | result = 0; | 612 | result = 0; |
599 | goto free_con_session; | 613 | goto free_con_session; |
600 | } | 614 | } |
601 | 615 | ||
602 | qDebug( "INIT OK" ); | 616 | qDebug( "INIT OK" ); |
603 | 617 | ||
604 | if ( smtp->getLogin() ) { | 618 | if ( smtp->getLogin() ) { |
605 | qDebug("smtp with auth"); | 619 | qDebug("smtp with auth"); |
606 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | 620 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { |
607 | // get'em | 621 | // get'em |
608 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | 622 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); |
609 | login.show(); | 623 | login.show(); |
610 | if ( QDialog::Accepted == login.exec() ) { | 624 | if ( QDialog::Accepted == login.exec() ) { |
611 | // ok | 625 | // ok |
612 | user = login.getUser().latin1(); | 626 | user = login.getUser().latin1(); |
613 | pass = login.getPassword().latin1(); | 627 | pass = login.getPassword().latin1(); |
614 | } else { | 628 | } else { |
615 | result = 0; | 629 | result = 0; |
616 | goto free_con_session; | 630 | goto free_con_session; |
617 | } | 631 | } |
618 | } else { | 632 | } else { |
619 | user = smtp->getUser().latin1(); | 633 | user = smtp->getUser().latin1(); |
620 | pass = smtp->getPassword().latin1(); | 634 | pass = smtp->getPassword().latin1(); |
621 | } | 635 | } |
622 | qDebug( "session->auth: %i", session->auth); | 636 | qDebug( "session->auth: %i", session->auth); |
623 | err = mailsmtp_auth( session, (char*)user, (char*)pass ); | 637 | err = mailsmtp_auth( session, (char*)user, (char*)pass ); |
624 | if ( err == MAILSMTP_NO_ERROR ) | 638 | if ( err == MAILSMTP_NO_ERROR ) { |
625 | qDebug("auth ok"); | 639 | qDebug("auth ok"); |
640 | } else { | ||
641 | storeFailedMail(data,size,tr("Authentification failed")); | ||
642 | result = 0; | ||
643 | goto free_con_session; | ||
644 | } | ||
626 | qDebug( "Done auth!" ); | 645 | qDebug( "Done auth!" ); |
627 | } else { | 646 | } else { |
628 | qDebug("SMTP without auth"); | 647 | qDebug("SMTP without auth"); |
648 | result = 0; | ||
649 | goto free_con_session; | ||
629 | } | 650 | } |
630 | 651 | ||
631 | err = mailsmtp_send( session, from, rcpts, data, size ); | 652 | err = mailsmtp_send( session, from, rcpts, data, size ); |
632 | if ( err != MAILSMTP_NO_ERROR ) { | 653 | if ( err != MAILSMTP_NO_ERROR ) { |
654 | storeFailedMail(data,size,mailsmtpError(err)); | ||
633 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); | 655 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); |
634 | result = 0; | 656 | result = 0; |
635 | goto free_con_session; | 657 | goto free_con_session; |
636 | } | 658 | } |
637 | 659 | ||
638 | qDebug( "Mail sent." ); | 660 | qDebug( "Mail sent." ); |
639 | storeMail(data,size,"Sent"); | 661 | storeMail(data,size,"Sent"); |
640 | 662 | ||
641 | free_con_session: | 663 | free_con_session: |
642 | mailsmtp_quit( session ); | 664 | mailsmtp_quit( session ); |
643 | free_mem_session: | 665 | free_mem_session: |
644 | mailsmtp_free( session ); | 666 | mailsmtp_free( session ); |
645 | free_mem: | 667 | free_mem: |
646 | return result; | 668 | return result; |
647 | } | 669 | } |
648 | 670 | ||
649 | void SMTPwrapper::sendMail(const Mail&mail,SMTPaccount*aSmtp,bool later ) { | 671 | void SMTPwrapper::sendMail(const Mail&mail,SMTPaccount*aSmtp,bool later ) { |
650 | mailmime * mimeMail; | 672 | mailmime * mimeMail; |
651 | 673 | ||
652 | SMTPaccount *smtp = aSmtp; | 674 | SMTPaccount *smtp = aSmtp; |
653 | 675 | ||
654 | if (!later && !smtp) { | 676 | if (!later && !smtp) { |
655 | qDebug("Didn't get any send method - giving up"); | 677 | qDebug("Didn't get any send method - giving up"); |
656 | return; | 678 | return; |
657 | } | 679 | } |
658 | mimeMail = createMimeMail(mail ); | 680 | mimeMail = createMimeMail(mail ); |
659 | if ( mimeMail == NULL ) { | 681 | if ( mimeMail == NULL ) { |
660 | qDebug( "sendMail: error creating mime mail" ); | 682 | qDebug( "sendMail: error creating mime mail" ); |
661 | } else { | 683 | } else { |
662 | sendProgress = new progressMailSend(); | 684 | sendProgress = new progressMailSend(); |
663 | sendProgress->show(); | 685 | sendProgress->show(); |
664 | sendProgress->setMaxMails(1); | 686 | sendProgress->setMaxMails(1); |
665 | smtpSend( mimeMail,later,smtp); | 687 | smtpSend( mimeMail,later,smtp); |
666 | qDebug("Clean up done"); | 688 | qDebug("Clean up done"); |
667 | sendProgress->hide(); | 689 | sendProgress->hide(); |
668 | delete sendProgress; | 690 | delete sendProgress; |
669 | sendProgress = 0; | 691 | sendProgress = 0; |
670 | mailmime_free( mimeMail ); | 692 | mailmime_free( mimeMail ); |
671 | } | 693 | } |
672 | } | 694 | } |
673 | 695 | ||
674 | int SMTPwrapper::sendQueuedMail(AbstractMail*wrap,SMTPaccount*smtp,RecMail*which) { | 696 | int SMTPwrapper::sendQueuedMail(AbstractMail*wrap,SMTPaccount*smtp,RecMail*which) { |
675 | size_t curTok = 0; | 697 | size_t curTok = 0; |
676 | mailimf_fields *fields = 0; | 698 | mailimf_fields *fields = 0; |
677 | mailimf_field*ffrom = 0; | 699 | mailimf_field*ffrom = 0; |
678 | clist *rcpts = 0; | 700 | clist *rcpts = 0; |
679 | char*from = 0; | 701 | char*from = 0; |
680 | int res = 0; | 702 | int res = 0; |
681 | 703 | ||
682 | encodedString * data = wrap->fetchRawBody(*which); | 704 | encodedString * data = wrap->fetchRawBody(*which); |
683 | if (!data) | 705 | if (!data) |
684 | return 0; | 706 | return 0; |
685 | int err = mailimf_fields_parse( data->Content(), data->Length(), &curTok, &fields ); | 707 | int err = mailimf_fields_parse( data->Content(), data->Length(), &curTok, &fields ); |
686 | if (err != MAILIMF_NO_ERROR) { | 708 | if (err != MAILIMF_NO_ERROR) { |
687 | delete data; | 709 | delete data; |
688 | delete wrap; | 710 | delete wrap; |
689 | return 0; | 711 | return 0; |
690 | } | 712 | } |
691 | 713 | ||
692 | rcpts = createRcptList( fields ); | 714 | rcpts = createRcptList( fields ); |
693 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); | 715 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); |
694 | from = getFrom(ffrom); | 716 | from = getFrom(ffrom); |
695 | 717 | ||
696 | if (rcpts && from) { | 718 | if (rcpts && from) { |
697 | res = smtpSend(from,rcpts,data->Content(),data->Length(),smtp ); | 719 | res = smtpSend(from,rcpts,data->Content(),data->Length(),smtp ); |
698 | } | 720 | } |
699 | if (fields) { | 721 | if (fields) { |
700 | mailimf_fields_free(fields); | 722 | mailimf_fields_free(fields); |
701 | fields = 0; | 723 | fields = 0; |
702 | } | 724 | } |
703 | if (data) { | 725 | if (data) { |
704 | delete data; | 726 | delete data; |
705 | } | 727 | } |
706 | if (from) { | 728 | if (from) { |
707 | free(from); | 729 | free(from); |
708 | } | 730 | } |
709 | if (rcpts) { | 731 | if (rcpts) { |
710 | smtp_address_list_free( rcpts ); | 732 | smtp_address_list_free( rcpts ); |
711 | } | 733 | } |
712 | return res; | 734 | return res; |
713 | } | 735 | } |
714 | 736 | ||
715 | /* this is a special fun */ | 737 | /* this is a special fun */ |
716 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) { | 738 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) { |
717 | bool returnValue = true; | 739 | bool returnValue = true; |
718 | 740 | ||
719 | if (!smtp) | 741 | if (!smtp) |
720 | return false; | 742 | return false; |
721 | 743 | ||
722 | QString localfolders = AbstractMail::defaultLocalfolder(); | 744 | QString localfolders = AbstractMail::defaultLocalfolder(); |
723 | AbstractMail*wrap = AbstractMail::getWrapper(localfolders); | 745 | AbstractMail*wrap = AbstractMail::getWrapper(localfolders); |
724 | if (!wrap) { | 746 | if (!wrap) { |
725 | qDebug("memory error"); | 747 | qDebug("memory error"); |
726 | return false; | 748 | return false; |
727 | } | 749 | } |
728 | QList<RecMail> mailsToSend; | 750 | QList<RecMail> mailsToSend; |
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.h b/noncore/net/mail/libmailwrapper/smtpwrapper.h index 7dcdbfd..89826d9 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.h +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.h | |||
@@ -1,72 +1,73 @@ | |||
1 | #ifndef SMTPwrapper_H | 1 | #ifndef SMTPwrapper_H |
2 | #define SMTPwrapper_H | 2 | #define SMTPwrapper_H |
3 | 3 | ||
4 | #include <qpe/applnk.h> | 4 | #include <qpe/applnk.h> |
5 | 5 | ||
6 | #include <qbitarray.h> | 6 | #include <qbitarray.h> |
7 | #include <qdatetime.h> | 7 | #include <qdatetime.h> |
8 | #include <libetpan/clist.h> | 8 | #include <libetpan/clist.h> |
9 | 9 | ||
10 | #include "settings.h" | 10 | #include "settings.h" |
11 | 11 | ||
12 | class Mail; | 12 | class Mail; |
13 | class AbstractMail; | 13 | class AbstractMail; |
14 | class RecMail; | 14 | class RecMail; |
15 | class Attachment; | 15 | class Attachment; |
16 | struct mailimf_fields; | 16 | struct mailimf_fields; |
17 | struct mailimf_field; | 17 | struct mailimf_field; |
18 | struct mailimf_mailbox; | 18 | struct mailimf_mailbox; |
19 | struct mailmime; | 19 | struct mailmime; |
20 | struct mailimf_address_list; | 20 | struct mailimf_address_list; |
21 | class progressMailSend; | 21 | class progressMailSend; |
22 | 22 | ||
23 | class SMTPwrapper : public QObject | 23 | class SMTPwrapper : public QObject |
24 | { | 24 | { |
25 | Q_OBJECT | 25 | Q_OBJECT |
26 | 26 | ||
27 | public: | 27 | public: |
28 | SMTPwrapper( Settings *s ); | 28 | SMTPwrapper( Settings *s ); |
29 | virtual ~SMTPwrapper(){} | 29 | virtual ~SMTPwrapper(){} |
30 | void sendMail(const Mail& mail,SMTPaccount*smtp,bool later=false ); | 30 | void sendMail(const Mail& mail,SMTPaccount*smtp,bool later=false ); |
31 | bool flushOutbox(SMTPaccount*smtp); | 31 | bool flushOutbox(SMTPaccount*smtp); |
32 | 32 | ||
33 | static progressMailSend*sendProgress; | 33 | static progressMailSend*sendProgress; |
34 | 34 | ||
35 | signals: | 35 | signals: |
36 | void queuedMails( int ); | 36 | void queuedMails( int ); |
37 | 37 | ||
38 | protected: | 38 | protected: |
39 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); | 39 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); |
40 | mailimf_fields *createImfFields(const Mail &mail ); | 40 | mailimf_fields *createImfFields(const Mail &mail ); |
41 | mailmime *createMimeMail(const Mail&mail ); | 41 | mailmime *createMimeMail(const Mail&mail ); |
42 | 42 | ||
43 | mailimf_address_list *parseAddresses(const QString&addr ); | 43 | mailimf_address_list *parseAddresses(const QString&addr ); |
44 | void addFileParts( mailmime *message,const QList<Attachment>&files ); | 44 | void addFileParts( mailmime *message,const QList<Attachment>&files ); |
45 | mailmime *buildTxtPart(const QString&str ); | 45 | mailmime *buildTxtPart(const QString&str ); |
46 | mailmime *buildFilePart(const QString&filename,const QString&mimetype,const QString&content); | 46 | mailmime *buildFilePart(const QString&filename,const QString&mimetype,const QString&content); |
47 | void smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ); | 47 | void smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ); |
48 | clist *createRcptList( mailimf_fields *fields ); | 48 | clist *createRcptList( mailimf_fields *fields ); |
49 | 49 | ||
50 | static void storeMail(const char*mail, size_t length, const QString&box); | 50 | static void storeMail(const char*mail, size_t length, const QString&box); |
51 | static QString mailsmtpError( int err ); | 51 | static QString mailsmtpError( int err ); |
52 | static void progress( size_t current, size_t maximum ); | 52 | static void progress( size_t current, size_t maximum ); |
53 | static void addRcpts( clist *list, mailimf_address_list *addr_list ); | 53 | static void addRcpts( clist *list, mailimf_address_list *addr_list ); |
54 | static char *getFrom( mailmime *mail ); | 54 | static char *getFrom( mailmime *mail ); |
55 | static char *getFrom( mailimf_field *ffrom); | 55 | static char *getFrom( mailimf_field *ffrom); |
56 | static mailimf_field *getField( mailimf_fields *fields, int type ); | 56 | static mailimf_field *getField( mailimf_fields *fields, int type ); |
57 | int smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ); | 57 | int smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ); |
58 | 58 | ||
59 | void storeMail(mailmime*mail, const QString&box); | 59 | void storeMail(mailmime*mail, const QString&box); |
60 | Settings *settings; | 60 | Settings *settings; |
61 | 61 | ||
62 | int sendQueuedMail(AbstractMail*wrap,SMTPaccount*smtp,RecMail*which); | 62 | int sendQueuedMail(AbstractMail*wrap,SMTPaccount*smtp,RecMail*which); |
63 | void storeFailedMail(const char*data,unsigned int size, const char*failuremessage); | ||
63 | 64 | ||
64 | int m_queuedMail; | 65 | int m_queuedMail; |
65 | static const char* USER_AGENT; | 66 | static const char* USER_AGENT; |
66 | 67 | ||
67 | protected slots: | 68 | protected slots: |
68 | void emitQCop( int queued ); | 69 | void emitQCop( int queued ); |
69 | 70 | ||
70 | }; | 71 | }; |
71 | 72 | ||
72 | #endif | 73 | #endif |