-rw-r--r-- | kmicromail/libmailwrapper/smtpwrapper.cpp | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/kmicromail/libmailwrapper/smtpwrapper.cpp b/kmicromail/libmailwrapper/smtpwrapper.cpp index 448a2e9..a6c12e4 100644 --- a/kmicromail/libmailwrapper/smtpwrapper.cpp +++ b/kmicromail/libmailwrapper/smtpwrapper.cpp | |||
@@ -1,490 +1,500 @@ | |||
1 | #include "smtpwrapper.h" | 1 | #include "smtpwrapper.h" |
2 | #include "mailwrapper.h" | 2 | #include "mailwrapper.h" |
3 | #include "abstractmail.h" | 3 | #include "abstractmail.h" |
4 | #include "logindialog.h" | 4 | #include "logindialog.h" |
5 | #include "mailtypes.h" | 5 | #include "mailtypes.h" |
6 | #include "sendmailprogress.h" | 6 | #include "sendmailprogress.h" |
7 | 7 | ||
8 | //#include <opie2/odebug.h> | 8 | //#include <opie2/odebug.h> |
9 | //#include <qt.h> | 9 | //#include <qt.h> |
10 | #include <qapplication.h> | 10 | #include <qapplication.h> |
11 | #include <qmessagebox.h> | 11 | #include <qmessagebox.h> |
12 | #include <stdlib.h> | 12 | #include <stdlib.h> |
13 | #ifndef DESKTOP_VERSION | 13 | #ifndef DESKTOP_VERSION |
14 | //#include <qpe/config.h> | 14 | //#include <qpe/config.h> |
15 | #include <qpe/qcopenvelope_qws.h> | 15 | #include <qpe/qcopenvelope_qws.h> |
16 | #endif | 16 | #endif |
17 | #include <libetpan/libetpan.h> | 17 | #include <libetpan/libetpan.h> |
18 | #include <klocale.h> | 18 | #include <klocale.h> |
19 | #include <kglobal.h> | 19 | #include <kglobal.h> |
20 | #include <kconfig.h> | 20 | #include <kconfig.h> |
21 | 21 | ||
22 | 22 | ||
23 | using namespace Opie::Core; | 23 | using namespace Opie::Core; |
24 | progressMailSend*SMTPwrapper::sendProgress = 0; | 24 | progressMailSend*SMTPwrapper::sendProgress = 0; |
25 | 25 | ||
26 | SMTPwrapper::SMTPwrapper(SMTPaccount * aSmtp ) | 26 | SMTPwrapper::SMTPwrapper(SMTPaccount * aSmtp ) |
27 | : Generatemail() | 27 | : Generatemail() |
28 | { | 28 | { |
29 | m_SmtpAccount = aSmtp; | 29 | m_SmtpAccount = aSmtp; |
30 | KConfig cfg( locateLocal("config", "kopiemailrc" ) ); | 30 | KConfig cfg( locateLocal("config", "kopiemailrc" ) ); |
31 | cfg.setGroup( "Status" ); | 31 | cfg.setGroup( "Status" ); |
32 | m_queuedMail = cfg.readNumEntry( "outgoing", 0 ); | 32 | m_queuedMail = cfg.readNumEntry( "outgoing", 0 ); |
33 | emit queuedMails( m_queuedMail ); | 33 | emit queuedMails( m_queuedMail ); |
34 | connect( this, SIGNAL( queuedMails(int) ), this, SLOT( emitQCop(int) ) ); | 34 | connect( this, SIGNAL( queuedMails(int) ), this, SLOT( emitQCop(int) ) ); |
35 | m_smtp = 0; | 35 | m_smtp = 0; |
36 | } | 36 | } |
37 | 37 | ||
38 | SMTPwrapper::~SMTPwrapper() | 38 | SMTPwrapper::~SMTPwrapper() |
39 | { | 39 | { |
40 | disc_server(); | 40 | disc_server(); |
41 | } | 41 | } |
42 | 42 | ||
43 | void SMTPwrapper::emitQCop( int queued ) { | 43 | void SMTPwrapper::emitQCop( int queued ) { |
44 | #ifndef DESKTOP_VERSION | 44 | #ifndef DESKTOP_VERSION |
45 | // LR : not used in kde-pim | 45 | // LR : not used in kde-pim |
46 | //QCopEnvelope env( "QPE/Pim", "outgoingMails(int)" ); | 46 | //QCopEnvelope env( "QPE/Pim", "outgoingMails(int)" ); |
47 | //env << queued; | 47 | //env << queued; |
48 | #endif | 48 | #endif |
49 | } | 49 | } |
50 | 50 | ||
51 | QString SMTPwrapper::mailsmtpError( int errnum ) { | 51 | QString SMTPwrapper::mailsmtpError( int errnum ) { |
52 | switch ( errnum ) { | 52 | switch ( errnum ) { |
53 | case MAILSMTP_NO_ERROR: | 53 | case MAILSMTP_NO_ERROR: |
54 | return i18n( "No error" ); | 54 | return i18n( "No error" ); |
55 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | 55 | case MAILSMTP_ERROR_UNEXPECTED_CODE: |
56 | return i18n( "Unexpected error code" ); | 56 | return i18n( "Unexpected error code" ); |
57 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | 57 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: |
58 | return i18n( "Service not available" ); | 58 | return i18n( "Service not available" ); |
59 | case MAILSMTP_ERROR_STREAM: | 59 | case MAILSMTP_ERROR_STREAM: |
60 | return i18n( "Stream error" ); | 60 | return i18n( "Stream error" ); |
61 | case MAILSMTP_ERROR_HOSTNAME: | 61 | case MAILSMTP_ERROR_HOSTNAME: |
62 | return i18n( "gethostname() failed" ); | 62 | return i18n( "gethostname() failed" ); |
63 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | 63 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: |
64 | return i18n( "Not implemented" ); | 64 | return i18n( "Not implemented" ); |
65 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | 65 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: |
66 | return i18n( "Error, action not taken" ); | 66 | return i18n( "Error, action not taken" ); |
67 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | 67 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: |
68 | return i18n( "Data exceeds storage allocation" ); | 68 | return i18n( "Data exceeds storage allocation" ); |
69 | case MAILSMTP_ERROR_IN_PROCESSING: | 69 | case MAILSMTP_ERROR_IN_PROCESSING: |
70 | return i18n( "Error in processing" ); | 70 | return i18n( "Error in processing" ); |
71 | case MAILSMTP_ERROR_STARTTLS_NOT_SUPPORTED: | 71 | case MAILSMTP_ERROR_STARTTLS_NOT_SUPPORTED: |
72 | return i18n( "Starttls not supported" ); | 72 | return i18n( "Starttls not supported" ); |
73 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | 73 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: |
74 | // return i18n( "Insufficient system storage" ); | 74 | // return i18n( "Insufficient system storage" ); |
75 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | 75 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: |
76 | return i18n( "Mailbox unavailable" ); | 76 | return i18n( "Mailbox unavailable" ); |
77 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | 77 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: |
78 | return i18n( "Mailbox name not allowed" ); | 78 | return i18n( "Mailbox name not allowed" ); |
79 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | 79 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: |
80 | return i18n( "Bad command sequence" ); | 80 | return i18n( "Bad command sequence" ); |
81 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | 81 | case MAILSMTP_ERROR_USER_NOT_LOCAL: |
82 | return i18n( "User not local" ); | 82 | return i18n( "User not local" ); |
83 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | 83 | case MAILSMTP_ERROR_TRANSACTION_FAILED: |
84 | return i18n( "Transaction failed" ); | 84 | return i18n( "Transaction failed" ); |
85 | case MAILSMTP_ERROR_MEMORY: | 85 | case MAILSMTP_ERROR_MEMORY: |
86 | return i18n( "Memory error" ); | 86 | return i18n( "Memory error" ); |
87 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | 87 | case MAILSMTP_ERROR_CONNECTION_REFUSED: |
88 | return i18n( "Connection refused" ); | 88 | return i18n( "Connection refused" ); |
89 | default: | 89 | default: |
90 | return i18n( "Unknown error code" ); | 90 | return i18n( "Unknown error code" ); |
91 | } | 91 | } |
92 | } | 92 | } |
93 | 93 | ||
94 | 94 | ||
95 | void SMTPwrapper::progress( size_t current, size_t maximum ) { | 95 | void SMTPwrapper::progress( size_t current, size_t maximum ) { |
96 | if (SMTPwrapper::sendProgress) { | 96 | if (SMTPwrapper::sendProgress) { |
97 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); | 97 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); |
98 | qApp->processEvents(); | 98 | qApp->processEvents(); |
99 | } | 99 | } |
100 | } | 100 | } |
101 | 101 | ||
102 | void SMTPwrapper::storeMail(const char*mail, size_t length, const QString&box) { | 102 | void SMTPwrapper::storeMail(const char*mail, size_t length, const QString&box) { |
103 | if (!mail) | 103 | if (!mail) |
104 | return; | 104 | return; |
105 | QString localfolders = AbstractMail::defaultLocalfolder(); | 105 | QString localfolders = AbstractMail::defaultLocalfolder(); |
106 | AbstractMail*wrap = AbstractMail::getWrapper(localfolders); | 106 | AbstractMail*wrap = AbstractMail::getWrapper(localfolders); |
107 | wrap->createMbox(box); | 107 | wrap->createMbox(box); |
108 | wrap->storeMessage(mail,length,box); | 108 | wrap->storeMessage(mail,length,box); |
109 | delete wrap; | 109 | delete wrap; |
110 | } | 110 | } |
111 | 111 | ||
112 | bool SMTPwrapper::smtpSend( mailmime *mail,bool later) { | 112 | bool SMTPwrapper::smtpSend( mailmime *mail,bool later) { |
113 | clist *rcpts = 0; | 113 | clist *rcpts = 0; |
114 | char *from, *data; | 114 | char *from, *data; |
115 | size_t size; | 115 | size_t size; |
116 | 116 | ||
117 | from = data = 0; | 117 | from = data = 0; |
118 | 118 | ||
119 | mailmessage * msg = 0; | 119 | mailmessage * msg = 0; |
120 | msg = mime_message_init(mail); | 120 | msg = mime_message_init(mail); |
121 | mime_message_set_tmpdir(msg,getenv( "HOME" )); | 121 | mime_message_set_tmpdir(msg,getenv( "HOME" )); |
122 | int r = mailmessage_fetch(msg,&data,&size); | 122 | int r = mailmessage_fetch(msg,&data,&size); |
123 | mime_message_detach_mime(msg); | 123 | mime_message_detach_mime(msg); |
124 | mailmessage_free(msg); | 124 | mailmessage_free(msg); |
125 | if (r != MAIL_NO_ERROR || !data) { | 125 | if (r != MAIL_NO_ERROR || !data) { |
126 | if (data) | 126 | if (data) |
127 | free(data); | 127 | free(data); |
128 | qDebug("Error fetching mime... "); | 128 | qDebug("Error fetching mime... "); |
129 | return false; | 129 | return false; |
130 | } | 130 | } |
131 | msg = 0; | 131 | msg = 0; |
132 | if (later) { | 132 | if (later) { |
133 | storeMail(data,size,"Outgoing"); | 133 | storeMail(data,size,"Outgoing"); |
134 | if (data) | 134 | if (data) |
135 | free( data ); | 135 | free( data ); |
136 | KConfig cfg( locateLocal("config", "kopiemailrc" ) ); | 136 | KConfig cfg( locateLocal("config", "kopiemailrc" ) ); |
137 | cfg.setGroup( "Status" ); | 137 | cfg.setGroup( "Status" ); |
138 | cfg.writeEntry( "outgoing", ++m_queuedMail ); | 138 | cfg.writeEntry( "outgoing", ++m_queuedMail ); |
139 | emit queuedMails( m_queuedMail ); | 139 | emit queuedMails( m_queuedMail ); |
140 | return true; | 140 | return true; |
141 | } | 141 | } |
142 | from = getFrom( mail ); | 142 | from = getFrom( mail ); |
143 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | 143 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); |
144 | bool result = smtpSend(from,rcpts,data,size); | 144 | bool result = smtpSend(from,rcpts,data,size); |
145 | if (data) { | 145 | if (data) { |
146 | free(data); | 146 | free(data); |
147 | } | 147 | } |
148 | if (from) { | 148 | if (from) { |
149 | free(from); | 149 | free(from); |
150 | } | 150 | } |
151 | if (rcpts) | 151 | if (rcpts) |
152 | smtp_address_list_free( rcpts ); | 152 | smtp_address_list_free( rcpts ); |
153 | return result; | 153 | return result; |
154 | } | 154 | } |
155 | 155 | ||
156 | void SMTPwrapper::storeFailedMail(const char*data,unsigned int size, const char*failuremessage) | 156 | void SMTPwrapper::storeFailedMail(const char*data,unsigned int size, const char*failuremessage) |
157 | { | 157 | { |
158 | if (data) { | 158 | if (data) { |
159 | storeMail(data,size,"Sendfailed"); | 159 | storeMail(data,size,"Sendfailed"); |
160 | } | 160 | } |
161 | if (failuremessage) { | 161 | if (failuremessage) { |
162 | QMessageBox::critical(0,i18n("Error sending mail"), | 162 | QMessageBox::critical(0,i18n("Error sending mail"), |
163 | failuremessage); | 163 | failuremessage); |
164 | } | 164 | } |
165 | } | 165 | } |
166 | 166 | ||
167 | int SMTPwrapper::start_smtp_tls() | 167 | int SMTPwrapper::start_smtp_tls() |
168 | { | 168 | { |
169 | if (!m_smtp) { | 169 | if (!m_smtp) { |
170 | return MAILSMTP_ERROR_IN_PROCESSING; | 170 | return MAILSMTP_ERROR_IN_PROCESSING; |
171 | } | 171 | } |
172 | int err = mailesmtp_starttls(m_smtp); | 172 | int err = mailesmtp_starttls(m_smtp); |
173 | if (err != MAILSMTP_NO_ERROR) return err; | 173 | if (err != MAILSMTP_NO_ERROR) return err; |
174 | mailstream_low * low; | 174 | mailstream_low * low; |
175 | mailstream_low * new_low; | 175 | mailstream_low * new_low; |
176 | low = mailstream_get_low(m_smtp->stream); | 176 | low = mailstream_get_low(m_smtp->stream); |
177 | if (!low) { | 177 | if (!low) { |
178 | return MAILSMTP_ERROR_IN_PROCESSING; | 178 | return MAILSMTP_ERROR_IN_PROCESSING; |
179 | } | 179 | } |
180 | int fd = mailstream_low_get_fd(low); | 180 | int fd = mailstream_low_get_fd(low); |
181 | if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) { | 181 | if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) { |
182 | mailstream_low_free(low); | 182 | mailstream_low_free(low); |
183 | mailstream_set_low(m_smtp->stream, new_low); | 183 | mailstream_set_low(m_smtp->stream, new_low); |
184 | } else { | 184 | } else { |
185 | return MAILSMTP_ERROR_IN_PROCESSING; | 185 | return MAILSMTP_ERROR_IN_PROCESSING; |
186 | } | 186 | } |
187 | return err; | 187 | return err; |
188 | } | 188 | } |
189 | 189 | ||
190 | void SMTPwrapper::connect_server() | 190 | void SMTPwrapper::connect_server() |
191 | { | 191 | { |
192 | QString server, user, pass; | 192 | QString server, user, pass; |
193 | bool ssl; | 193 | bool ssl; |
194 | uint16_t port; | 194 | uint16_t port; |
195 | ssl = false; | 195 | ssl = false; |
196 | bool try_tls = false; | 196 | bool try_tls = false; |
197 | bool force_tls=false; | 197 | bool force_tls=false; |
198 | QString failuretext = ""; | 198 | QString failuretext = ""; |
199 | 199 | ||
200 | if (m_smtp || !m_SmtpAccount) { | 200 | if (m_smtp || !m_SmtpAccount) { |
201 | return; | 201 | return; |
202 | } | 202 | } |
203 | server = m_SmtpAccount->getServer(); | 203 | server = m_SmtpAccount->getServer(); |
204 | if ( m_SmtpAccount->ConnectionType() == 3 ) { | 204 | if ( m_SmtpAccount->ConnectionType() == 3 ) { |
205 | ssl = true; | 205 | ssl = true; |
206 | try_tls = false; | 206 | try_tls = false; |
207 | } else if (m_SmtpAccount->ConnectionType() == 2) { | 207 | } else if (m_SmtpAccount->ConnectionType() == 2) { |
208 | force_tls = true; | 208 | force_tls = true; |
209 | try_tls = true; | 209 | try_tls = true; |
210 | } else if (m_SmtpAccount->ConnectionType() == 1) { | 210 | } else if (m_SmtpAccount->ConnectionType() == 1) { |
211 | try_tls = true; | 211 | try_tls = true; |
212 | } | 212 | } |
213 | int result = 1; | 213 | int result = 1; |
214 | port = m_SmtpAccount->getPort().toUInt(); | 214 | port = m_SmtpAccount->getPort().toUInt(); |
215 | 215 | ||
216 | m_smtp = mailsmtp_new( 20, &progress ); | 216 | m_smtp = mailsmtp_new( 20, &progress ); |
217 | if ( m_smtp == NULL ) { | 217 | if ( m_smtp == NULL ) { |
218 | /* no failure message cause this happens when problems with memory - than we | 218 | /* no failure message cause this happens when problems with memory - than we |
219 | we can not display any messagebox */ | 219 | we can not display any messagebox */ |
220 | return; | 220 | return; |
221 | } | 221 | } |
222 | //m_smtp->auth = MAILSMTP_AUTH_LOGIN; | ||
222 | 223 | ||
223 | int err = MAILSMTP_NO_ERROR; | 224 | int err = MAILSMTP_NO_ERROR; |
224 | ; // odebug << "Servername " << server << " at port " << port << "" << oendl; | 225 | ; // odebug << "Servername " << server << " at port " << port << "" << oendl; |
225 | if ( ssl ) { | 226 | if ( ssl ) { |
226 | qDebug("smtp: ssl_connect "); | 227 | qDebug("smtp: ssl_connect "); |
227 | err = mailsmtp_ssl_connect( m_smtp, server.latin1(), port ); | 228 | err = mailsmtp_ssl_connect( m_smtp, server.latin1(), port ); |
228 | } else { | 229 | } else { |
229 | ; // odebug << "No SSL session" << oendl; | 230 | ; // odebug << "No SSL session" << oendl; |
230 | err = mailsmtp_socket_connect( m_smtp, server.latin1(), port ); | 231 | err = mailsmtp_socket_connect( m_smtp, server.latin1(), port ); |
231 | } | 232 | } |
232 | if ( err != MAILSMTP_NO_ERROR ) { | 233 | if ( err != MAILSMTP_NO_ERROR ) { |
233 | qDebug("Error init SMTP connection" ); | 234 | qDebug("Error init SMTP connection" ); |
234 | failuretext = i18n("Error init SMTP connection:\n%1").arg(mailsmtpError(err)); | 235 | failuretext = i18n("Error init SMTP connection:\n%1").arg(mailsmtpError(err)); |
235 | result = 0; | 236 | result = 0; |
236 | } | 237 | } |
237 | 238 | ||
238 | qDebug("SMTP connection inited "); | 239 | qDebug("SMTP connection inited "); |
239 | /* switch to tls after init 'cause there it will send the ehlo */ | 240 | /* switch to tls after init 'cause there it will send the ehlo */ |
240 | if (result) { | 241 | if (result) { |
241 | err = mailsmtp_init( m_smtp ); | 242 | err = mailsmtp_init( m_smtp ); |
242 | if (err != MAILSMTP_NO_ERROR) { | 243 | if (err != MAILSMTP_NO_ERROR) { |
243 | result = 0; | 244 | result = 0; |
244 | qDebug("Error init SMTP connection "); | 245 | qDebug("Error init SMTP connection "); |
245 | failuretext = i18n("Error init SMTP connection:\n%1").arg(mailsmtpError(err)); | 246 | failuretext = i18n("Error init SMTP connection:\n%1").arg(mailsmtpError(err)); |
246 | } | 247 | } |
247 | } | 248 | } |
248 | if (result && try_tls) { | 249 | if (result && try_tls) { |
249 | qDebug("Smpt: Try TLS... "); | 250 | qDebug("Smpt: Try TLS... "); |
250 | err = start_smtp_tls(); | 251 | err = start_smtp_tls(); |
251 | if (err != MAILSMTP_NO_ERROR) { | 252 | if (err != MAILSMTP_NO_ERROR) { |
252 | try_tls = false; | 253 | try_tls = false; |
253 | qDebug("Smpt: No TLS possible "); | 254 | qDebug("Smpt: No TLS possible "); |
254 | } else { | 255 | } else { |
255 | err = mailesmtp_ehlo(m_smtp); | 256 | qDebug("Smpt: Using TLS "); |
256 | if ( err != MAILSMTP_NO_ERROR ) | ||
257 | result = 0; | ||
258 | else | ||
259 | qDebug("Smpt: Using TLS "); | ||
260 | } | 257 | } |
261 | } | 258 | } |
259 | |||
262 | //qDebug("mailesmtp_ehlo %d ",err ); | 260 | //qDebug("mailesmtp_ehlo %d ",err ); |
263 | if (!try_tls && force_tls) { | 261 | if (!try_tls && force_tls) { |
264 | result = 0; | 262 | result = 0; |
265 | failuretext = i18n("Error init SMTP tls:%1").arg(mailsmtpError(err)); | 263 | failuretext = i18n("Error init SMTP tls:%1").arg(mailsmtpError(err)); |
264 | } | ||
265 | if ( mailesmtp_ehlo(m_smtp) != MAILSMTP_NO_ERROR ) { | ||
266 | qDebug("Smpt: ehlo failed "); | ||
267 | result = 0; | ||
266 | } | 268 | } |
267 | 269 | else { | |
270 | //qDebug("Smpt: auth is %d -- %d %d",m_smtp->auth, MAILSMTP_AUTH_LOGIN, MAILSMTP_AUTH_PLAIN); | ||
271 | if ( m_smtp->auth & MAILSMTP_AUTH_LOGIN && m_smtp->auth & MAILSMTP_AUTH_PLAIN ) { | ||
272 | qDebug("Smpt: Using MAILSMTP_AUTH_LOGIN "); | ||
273 | m_smtp->auth -= MAILSMTP_AUTH_PLAIN; | ||
274 | //qDebug("Smpt: auth is %d -- %d %d",m_smtp->auth, MAILSMTP_AUTH_LOGIN, MAILSMTP_AUTH_PLAIN); | ||
275 | } | ||
276 | } | ||
277 | |||
268 | if (result==1 && m_SmtpAccount->getLogin() ) { | 278 | if (result==1 && m_SmtpAccount->getLogin() ) { |
269 | ; // odebug << "smtp with auth" << oendl; | 279 | ; // odebug << "smtp with auth" << oendl; |
270 | if ( m_SmtpAccount->getUser().isEmpty() || m_SmtpAccount->getPassword().isEmpty() ) { | 280 | if ( m_SmtpAccount->getUser().isEmpty() || m_SmtpAccount->getPassword().isEmpty() ) { |
271 | // get'em | 281 | // get'em |
272 | LoginDialog login( m_SmtpAccount->getUser(), | 282 | LoginDialog login( m_SmtpAccount->getUser(), |
273 | m_SmtpAccount->getPassword(), NULL, 0, true ); | 283 | m_SmtpAccount->getPassword(), NULL, 0, true ); |
274 | login.show(); | 284 | login.show(); |
275 | if ( QDialog::Accepted == login.exec() ) { | 285 | if ( QDialog::Accepted == login.exec() ) { |
276 | // ok | 286 | // ok |
277 | user = login.getUser(); | 287 | user = login.getUser(); |
278 | pass = login.getPassword(); | 288 | pass = login.getPassword(); |
279 | } else { | 289 | } else { |
280 | result = 0; | 290 | result = 0; |
281 | failuretext=i18n("Login aborted - \nstoring mail to localfolder"); | 291 | failuretext=i18n("Login aborted - \nstoring mail to localfolder"); |
282 | } | 292 | } |
283 | } else { | 293 | } else { |
284 | user = m_SmtpAccount->getUser(); | 294 | user = m_SmtpAccount->getUser(); |
285 | pass = m_SmtpAccount->getPassword(); | 295 | pass = m_SmtpAccount->getPassword(); |
286 | } | 296 | } |
287 | ; // odebug << "session->auth: " << m_smtp->auth << "" << oendl; | 297 | ; // odebug << "session->auth: " << m_smtp->auth << "" << oendl; |
288 | if (result) { | 298 | if (result) { |
289 | err = mailsmtp_auth( m_smtp, (char*)user.latin1(), (char*)pass.latin1() ); | 299 | err = mailsmtp_auth( m_smtp, (char*)user.latin1(), (char*)pass.latin1() ); |
290 | if ( err == MAILSMTP_NO_ERROR ) { | 300 | if ( err == MAILSMTP_NO_ERROR ) { |
291 | qDebug("Smtp authentification ok "); | 301 | qDebug("Smtp authentification ok "); |
292 | } else { | 302 | } else { |
293 | failuretext = i18n("Authentification failed"); | 303 | failuretext = i18n("Authentification failed"); |
294 | result = 0; | 304 | result = 0; |
295 | } | 305 | } |
296 | } | 306 | } |
297 | } | 307 | } |
298 | if ( result == 0 ) { | 308 | if ( result == 0 ) { |
299 | mailsmtp_free(m_smtp); | 309 | mailsmtp_free(m_smtp); |
300 | m_smtp = 0; | 310 | m_smtp = 0; |
301 | } | 311 | } |
302 | } | 312 | } |
303 | 313 | ||
304 | void SMTPwrapper::disc_server() | 314 | void SMTPwrapper::disc_server() |
305 | { | 315 | { |
306 | if (m_smtp) { | 316 | if (m_smtp) { |
307 | mailsmtp_quit( m_smtp ); | 317 | mailsmtp_quit( m_smtp ); |
308 | mailsmtp_free( m_smtp ); | 318 | mailsmtp_free( m_smtp ); |
309 | m_smtp = 0; | 319 | m_smtp = 0; |
310 | } | 320 | } |
311 | } | 321 | } |
312 | 322 | ||
313 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size ) | 323 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size ) |
314 | { | 324 | { |
315 | int err,result; | 325 | int err,result; |
316 | QString failuretext = ""; | 326 | QString failuretext = ""; |
317 | 327 | ||
318 | connect_server(); | 328 | connect_server(); |
319 | 329 | ||
320 | result = 1; | 330 | result = 1; |
321 | if (m_smtp) { | 331 | if (m_smtp) { |
322 | err = mailsmtp_send( m_smtp, from, rcpts, data, size ); | 332 | err = mailsmtp_send( m_smtp, from, rcpts, data, size ); |
323 | if ( err != MAILSMTP_NO_ERROR ) { | 333 | if ( err != MAILSMTP_NO_ERROR ) { |
324 | qDebug("Error sending mail"); | 334 | qDebug("Error sending mail"); |
325 | failuretext=i18n("Error sending mail:\n%1").arg(mailsmtpError(err)); | 335 | failuretext=i18n("Error sending mail:\n%1").arg(mailsmtpError(err)); |
326 | result = 0; | 336 | result = 0; |
327 | } | 337 | } |
328 | } else { | 338 | } else { |
329 | result = 0; | 339 | result = 0; |
330 | } | 340 | } |
331 | 341 | ||
332 | if (!result) { | 342 | if (!result) { |
333 | storeFailedMail(data,size,failuretext); | 343 | storeFailedMail(data,size,failuretext); |
334 | } else { | 344 | } else { |
335 | ; // odebug << "Mail sent." << oendl; | 345 | ; // odebug << "Mail sent." << oendl; |
336 | storeMail(data,size,"Sent"); | 346 | storeMail(data,size,"Sent"); |
337 | } | 347 | } |
338 | return result; | 348 | return result; |
339 | } | 349 | } |
340 | 350 | ||
341 | bool SMTPwrapper::sendMail(const Opie::Core::OSmartPointer<Mail>&mail,bool later ) | 351 | bool SMTPwrapper::sendMail(const Opie::Core::OSmartPointer<Mail>&mail,bool later ) |
342 | { | 352 | { |
343 | mailmime * mimeMail; | 353 | mailmime * mimeMail; |
344 | bool result = true; | 354 | bool result = true; |
345 | mimeMail = createMimeMail(mail ); | 355 | mimeMail = createMimeMail(mail ); |
346 | if ( mimeMail == 0 ) { | 356 | if ( mimeMail == 0 ) { |
347 | qDebug("SMTP wrapper:Error creating mail! "); | 357 | qDebug("SMTP wrapper:Error creating mail! "); |
348 | return false; | 358 | return false; |
349 | } else { | 359 | } else { |
350 | sendProgress = new progressMailSend(); | 360 | sendProgress = new progressMailSend(); |
351 | sendProgress->show(); | 361 | sendProgress->show(); |
352 | sendProgress->setMaxMails(1); | 362 | sendProgress->setMaxMails(1); |
353 | result = smtpSend( mimeMail,later); | 363 | result = smtpSend( mimeMail,later); |
354 | ; // odebug << "Clean up done" << oendl; | 364 | ; // odebug << "Clean up done" << oendl; |
355 | sendProgress->hide(); | 365 | sendProgress->hide(); |
356 | delete sendProgress; | 366 | delete sendProgress; |
357 | sendProgress = 0; | 367 | sendProgress = 0; |
358 | mailmime_free( mimeMail ); | 368 | mailmime_free( mimeMail ); |
359 | if ( m_smtp ) { | 369 | if ( m_smtp ) { |
360 | mailsmtp_free(m_smtp); | 370 | mailsmtp_free(m_smtp); |
361 | m_smtp = 0; | 371 | m_smtp = 0; |
362 | } | 372 | } |
363 | } | 373 | } |
364 | return result; | 374 | return result; |
365 | } | 375 | } |
366 | 376 | ||
367 | int SMTPwrapper::sendQueuedMail(AbstractMail*wrap,const RecMailP&which) { | 377 | int SMTPwrapper::sendQueuedMail(AbstractMail*wrap,const RecMailP&which) { |
368 | size_t curTok = 0; | 378 | size_t curTok = 0; |
369 | mailimf_fields *fields = 0; | 379 | mailimf_fields *fields = 0; |
370 | mailimf_field*ffrom = 0; | 380 | mailimf_field*ffrom = 0; |
371 | clist *rcpts = 0; | 381 | clist *rcpts = 0; |
372 | char*from = 0; | 382 | char*from = 0; |
373 | int res = 0; | 383 | int res = 0; |
374 | 384 | ||
375 | encodedString * data = wrap->fetchRawBody(which); | 385 | encodedString * data = wrap->fetchRawBody(which); |
376 | if (!data) | 386 | if (!data) |
377 | return 0; | 387 | return 0; |
378 | int err = mailimf_fields_parse( data->Content(), data->Length(), &curTok, &fields ); | 388 | int err = mailimf_fields_parse( data->Content(), data->Length(), &curTok, &fields ); |
379 | if (err != MAILIMF_NO_ERROR) { | 389 | if (err != MAILIMF_NO_ERROR) { |
380 | delete data; | 390 | delete data; |
381 | delete wrap; | 391 | delete wrap; |
382 | return 0; | 392 | return 0; |
383 | } | 393 | } |
384 | 394 | ||
385 | rcpts = createRcptList( fields ); | 395 | rcpts = createRcptList( fields ); |
386 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); | 396 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); |
387 | from = getFrom(ffrom); | 397 | from = getFrom(ffrom); |
388 | 398 | ||
389 | if (rcpts && from) { | 399 | if (rcpts && from) { |
390 | res = smtpSend(from,rcpts,data->Content(),data->Length()); | 400 | res = smtpSend(from,rcpts,data->Content(),data->Length()); |
391 | } | 401 | } |
392 | if (fields) { | 402 | if (fields) { |
393 | mailimf_fields_free(fields); | 403 | mailimf_fields_free(fields); |
394 | fields = 0; | 404 | fields = 0; |
395 | } | 405 | } |
396 | if (data) { | 406 | if (data) { |
397 | delete data; | 407 | delete data; |
398 | } | 408 | } |
399 | if (from) { | 409 | if (from) { |
400 | free(from); | 410 | free(from); |
401 | } | 411 | } |
402 | if (rcpts) { | 412 | if (rcpts) { |
403 | smtp_address_list_free( rcpts ); | 413 | smtp_address_list_free( rcpts ); |
404 | } | 414 | } |
405 | return res; | 415 | return res; |
406 | } | 416 | } |
407 | 417 | ||
408 | /* this is a special fun */ | 418 | /* this is a special fun */ |
409 | bool SMTPwrapper::flushOutbox() { | 419 | bool SMTPwrapper::flushOutbox() { |
410 | bool returnValue = true; | 420 | bool returnValue = true; |
411 | 421 | ||
412 | ; // odebug << "Sending the queue" << oendl; | 422 | ; // odebug << "Sending the queue" << oendl; |
413 | if (!m_SmtpAccount) { | 423 | if (!m_SmtpAccount) { |
414 | ; // odebug << "No smtp account given" << oendl; | 424 | ; // odebug << "No smtp account given" << oendl; |
415 | return false; | 425 | return false; |
416 | } | 426 | } |
417 | 427 | ||
418 | bool reset_user_value = false; | 428 | bool reset_user_value = false; |
419 | QString localfolders = AbstractMail::defaultLocalfolder(); | 429 | QString localfolders = AbstractMail::defaultLocalfolder(); |
420 | AbstractMail*wrap = AbstractMail::getWrapper(localfolders); | 430 | AbstractMail*wrap = AbstractMail::getWrapper(localfolders); |
421 | if (!wrap) { | 431 | if (!wrap) { |
422 | ; // odebug << "memory error" << oendl; | 432 | ; // odebug << "memory error" << oendl; |
423 | return false; | 433 | return false; |
424 | } | 434 | } |
425 | QString oldPw, oldUser; | 435 | QString oldPw, oldUser; |
426 | QValueList<RecMailP> mailsToSend; | 436 | QValueList<RecMailP> mailsToSend; |
427 | QValueList<RecMailP> mailsToRemove; | 437 | QValueList<RecMailP> mailsToRemove; |
428 | QString mbox("Outgoing"); | 438 | QString mbox("Outgoing"); |
429 | wrap->listMessages(mbox,mailsToSend); | 439 | wrap->listMessages(mbox,mailsToSend); |
430 | if (mailsToSend.count()==0) { | 440 | if (mailsToSend.count()==0) { |
431 | delete wrap; | 441 | delete wrap; |
432 | ; // odebug << "No mails to send" << oendl; | 442 | ; // odebug << "No mails to send" << oendl; |
433 | return false; | 443 | return false; |
434 | } | 444 | } |
435 | 445 | ||
436 | oldPw = m_SmtpAccount->getPassword(); | 446 | oldPw = m_SmtpAccount->getPassword(); |
437 | oldUser = m_SmtpAccount->getUser(); | 447 | oldUser = m_SmtpAccount->getUser(); |
438 | if (m_SmtpAccount->getLogin() && (m_SmtpAccount->getUser().isEmpty() || m_SmtpAccount->getPassword().isEmpty()) ) { | 448 | if (m_SmtpAccount->getLogin() && (m_SmtpAccount->getUser().isEmpty() || m_SmtpAccount->getPassword().isEmpty()) ) { |
439 | // get'em | 449 | // get'em |
440 | QString user,pass; | 450 | QString user,pass; |
441 | LoginDialog login( m_SmtpAccount->getUser(), m_SmtpAccount->getPassword(), NULL, 0, true ); | 451 | LoginDialog login( m_SmtpAccount->getUser(), m_SmtpAccount->getPassword(), NULL, 0, true ); |
442 | login.show(); | 452 | login.show(); |
443 | if ( QDialog::Accepted == login.exec() ) { | 453 | if ( QDialog::Accepted == login.exec() ) { |
444 | // ok | 454 | // ok |
445 | user = login.getUser().latin1(); | 455 | user = login.getUser().latin1(); |
446 | pass = login.getPassword().latin1(); | 456 | pass = login.getPassword().latin1(); |
447 | reset_user_value = true; | 457 | reset_user_value = true; |
448 | m_SmtpAccount->setUser(user); | 458 | m_SmtpAccount->setUser(user); |
449 | m_SmtpAccount->setPassword(pass); | 459 | m_SmtpAccount->setPassword(pass); |
450 | } else { | 460 | } else { |
451 | return true; | 461 | return true; |
452 | } | 462 | } |
453 | } | 463 | } |
454 | 464 | ||
455 | 465 | ||
456 | sendProgress = new progressMailSend(); | 466 | sendProgress = new progressMailSend(); |
457 | sendProgress->show(); | 467 | sendProgress->show(); |
458 | sendProgress->setMaxMails(mailsToSend.count()); | 468 | sendProgress->setMaxMails(mailsToSend.count()); |
459 | 469 | ||
460 | while (returnValue && mailsToSend.count()>0) { | 470 | while (returnValue && mailsToSend.count()>0) { |
461 | if (sendQueuedMail(wrap, (*mailsToSend.begin()))==0) { | 471 | if (sendQueuedMail(wrap, (*mailsToSend.begin()))==0) { |
462 | QMessageBox::critical(0,i18n("Error sending mail"), | 472 | QMessageBox::critical(0,i18n("Error sending mail"), |
463 | i18n("Error sending queued mail.\nBreaking.")); | 473 | i18n("Error sending queued mail.\nBreaking.")); |
464 | returnValue = false; | 474 | returnValue = false; |
465 | } | 475 | } |
466 | mailsToRemove.append((*mailsToSend.begin())); | 476 | mailsToRemove.append((*mailsToSend.begin())); |
467 | mailsToSend.remove(mailsToSend.begin()); | 477 | mailsToSend.remove(mailsToSend.begin()); |
468 | sendProgress->setCurrentMails(mailsToRemove.count()); | 478 | sendProgress->setCurrentMails(mailsToRemove.count()); |
469 | } | 479 | } |
470 | if (reset_user_value) { | 480 | if (reset_user_value) { |
471 | m_SmtpAccount->setUser(oldUser); | 481 | m_SmtpAccount->setUser(oldUser); |
472 | m_SmtpAccount->setPassword(oldPw); | 482 | m_SmtpAccount->setPassword(oldPw); |
473 | } | 483 | } |
474 | 484 | ||
475 | KConfig cfg( locateLocal("config", "kopiemailrc" ) ); | 485 | KConfig cfg( locateLocal("config", "kopiemailrc" ) ); |
476 | cfg.setGroup( "Status" ); | 486 | cfg.setGroup( "Status" ); |
477 | m_queuedMail = mailsToSend.count(); | 487 | m_queuedMail = mailsToSend.count(); |
478 | cfg.writeEntry( "outgoing", m_queuedMail ); | 488 | cfg.writeEntry( "outgoing", m_queuedMail ); |
479 | emit queuedMails( m_queuedMail ); | 489 | emit queuedMails( m_queuedMail ); |
480 | sendProgress->hide(); | 490 | sendProgress->hide(); |
481 | delete sendProgress; | 491 | delete sendProgress; |
482 | sendProgress = 0; | 492 | sendProgress = 0; |
483 | wrap->deleteMails(mbox,mailsToRemove); | 493 | wrap->deleteMails(mbox,mailsToRemove); |
484 | delete wrap; | 494 | delete wrap; |
485 | if ( m_smtp ) { | 495 | if ( m_smtp ) { |
486 | mailsmtp_free(m_smtp); | 496 | mailsmtp_free(m_smtp); |
487 | m_smtp = 0; | 497 | m_smtp = 0; |
488 | } | 498 | } |
489 | return returnValue; | 499 | return returnValue; |
490 | } | 500 | } |