summaryrefslogtreecommitdiffabout
path: root/kmicromail/libmailwrapper
authorzautrix <zautrix>2005-02-28 14:46:07 (UTC)
committer zautrix <zautrix>2005-02-28 14:46:07 (UTC)
commitb1f912cbb6a9daf050e94d337de0e0e73417284a (patch) (unidiff)
tree09220df0ef11ccc32c4eafcc2a63b64056068036 /kmicromail/libmailwrapper
parentff810f8f74f6928e664bf52f8e8d128edb8ac5ad (diff)
downloadkdepimpi-b1f912cbb6a9daf050e94d337de0e0e73417284a.zip
kdepimpi-b1f912cbb6a9daf050e94d337de0e0e73417284a.tar.gz
kdepimpi-b1f912cbb6a9daf050e94d337de0e0e73417284a.tar.bz2
ompi refresh outgoing fix
Diffstat (limited to 'kmicromail/libmailwrapper') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libmailwrapper/smtpwrapper.cpp26
1 files changed, 15 insertions, 11 deletions
diff --git a/kmicromail/libmailwrapper/smtpwrapper.cpp b/kmicromail/libmailwrapper/smtpwrapper.cpp
index f54fe2b..24f4786 100644
--- a/kmicromail/libmailwrapper/smtpwrapper.cpp
+++ b/kmicromail/libmailwrapper/smtpwrapper.cpp
@@ -98,276 +98,280 @@ void SMTPwrapper::progress( size_t current, size_t maximum ) {
98 } 98 }
99} 99}
100 100
101void SMTPwrapper::storeMail(const char*mail, size_t length, const QString&box) { 101void SMTPwrapper::storeMail(const char*mail, size_t length, const QString&box) {
102 if (!mail) 102 if (!mail)
103 return; 103 return;
104 QString localfolders = AbstractMail::defaultLocalfolder(); 104 QString localfolders = AbstractMail::defaultLocalfolder();
105 AbstractMail*wrap = AbstractMail::getWrapper(localfolders); 105 AbstractMail*wrap = AbstractMail::getWrapper(localfolders);
106 wrap->createMbox(box); 106 wrap->createMbox(box);
107 wrap->storeMessage(mail,length,box); 107 wrap->storeMessage(mail,length,box);
108 delete wrap; 108 delete wrap;
109} 109}
110 110
111bool SMTPwrapper::smtpSend( mailmime *mail,bool later) { 111bool SMTPwrapper::smtpSend( mailmime *mail,bool later) {
112 clist *rcpts = 0; 112 clist *rcpts = 0;
113 char *from, *data; 113 char *from, *data;
114 size_t size; 114 size_t size;
115 115
116 from = data = 0; 116 from = data = 0;
117 117
118 mailmessage * msg = 0; 118 mailmessage * msg = 0;
119 msg = mime_message_init(mail); 119 msg = mime_message_init(mail);
120 mime_message_set_tmpdir(msg,getenv( "HOME" )); 120 mime_message_set_tmpdir(msg,getenv( "HOME" ));
121 int r = mailmessage_fetch(msg,&data,&size); 121 int r = mailmessage_fetch(msg,&data,&size);
122 mime_message_detach_mime(msg); 122 mime_message_detach_mime(msg);
123 mailmessage_free(msg); 123 mailmessage_free(msg);
124 if (r != MAIL_NO_ERROR || !data) { 124 if (r != MAIL_NO_ERROR || !data) {
125 if (data) 125 if (data)
126 free(data); 126 free(data);
127 qDebug("Error fetching mime... "); 127 qDebug("Error fetching mime... ");
128 return false; 128 return false;
129 } 129 }
130 msg = 0; 130 msg = 0;
131 if (later) { 131 if (later) {
132 storeMail(data,size,"Outgoing"); 132 storeMail(data,size,"Outgoing");
133 if (data) 133 if (data)
134 free( data ); 134 free( data );
135 KConfig cfg( locateLocal("config", "kopiemailrc" ) ); 135 KConfig cfg( locateLocal("config", "kopiemailrc" ) );
136 cfg.setGroup( "Status" ); 136 cfg.setGroup( "Status" );
137 cfg.writeEntry( "outgoing", ++m_queuedMail ); 137 cfg.writeEntry( "outgoing", ++m_queuedMail );
138 emit queuedMails( m_queuedMail ); 138 emit queuedMails( m_queuedMail );
139 return true; 139 return true;
140 } 140 }
141 from = getFrom( mail ); 141 from = getFrom( mail );
142 rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); 142 rcpts = createRcptList( mail->mm_data.mm_message.mm_fields );
143 bool result = smtpSend(from,rcpts,data,size); 143 bool result = smtpSend(from,rcpts,data,size);
144 if (data) { 144 if (data) {
145 free(data); 145 free(data);
146 } 146 }
147 if (from) { 147 if (from) {
148 free(from); 148 free(from);
149 } 149 }
150 if (rcpts) 150 if (rcpts)
151 smtp_address_list_free( rcpts ); 151 smtp_address_list_free( rcpts );
152 return result; 152 return result;
153} 153}
154 154
155void SMTPwrapper::storeFailedMail(const char*data,unsigned int size, const char*failuremessage) 155void SMTPwrapper::storeFailedMail(const char*data,unsigned int size, const char*failuremessage)
156{ 156{
157 if (data) { 157 if (data) {
158 storeMail(data,size,"Sendfailed"); 158 storeMail(data,size,"Sendfailed");
159 } 159 }
160 if (failuremessage) { 160 if (failuremessage) {
161 QMessageBox::critical(0,i18n("Error sending mail"), 161 QMessageBox::critical(0,i18n("Error sending mail"),
162 i18n("<center>%1</center>").arg(failuremessage)); 162 failuremessage);
163 } 163 }
164} 164}
165 165
166int SMTPwrapper::start_smtp_tls() 166int SMTPwrapper::start_smtp_tls()
167{ 167{
168 if (!m_smtp) { 168 if (!m_smtp) {
169 return MAILSMTP_ERROR_IN_PROCESSING; 169 return MAILSMTP_ERROR_IN_PROCESSING;
170 } 170 }
171 int err = mailesmtp_starttls(m_smtp); 171 int err = mailesmtp_starttls(m_smtp);
172 if (err != MAILSMTP_NO_ERROR) return err; 172 if (err != MAILSMTP_NO_ERROR) return err;
173 mailstream_low * low; 173 mailstream_low * low;
174 mailstream_low * new_low; 174 mailstream_low * new_low;
175 low = mailstream_get_low(m_smtp->stream); 175 low = mailstream_get_low(m_smtp->stream);
176 if (!low) { 176 if (!low) {
177 return MAILSMTP_ERROR_IN_PROCESSING; 177 return MAILSMTP_ERROR_IN_PROCESSING;
178 } 178 }
179 int fd = mailstream_low_get_fd(low); 179 int fd = mailstream_low_get_fd(low);
180 if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) { 180 if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) {
181 mailstream_low_free(low); 181 mailstream_low_free(low);
182 mailstream_set_low(m_smtp->stream, new_low); 182 mailstream_set_low(m_smtp->stream, new_low);
183 } else { 183 } else {
184 return MAILSMTP_ERROR_IN_PROCESSING; 184 return MAILSMTP_ERROR_IN_PROCESSING;
185 } 185 }
186 return err; 186 return err;
187} 187}
188 188
189void SMTPwrapper::connect_server() 189void SMTPwrapper::connect_server()
190{ 190{
191 QString server, user, pass; 191 QString server, user, pass;
192 bool ssl; 192 bool ssl;
193 uint16_t port; 193 uint16_t port;
194 ssl = false; 194 ssl = false;
195 bool try_tls = true; 195 bool try_tls = true;
196 bool force_tls=false; 196 bool force_tls=false;
197 QString failuretext = ""; 197 QString failuretext = "";
198 198
199 if (m_smtp || !m_SmtpAccount) { 199 if (m_smtp || !m_SmtpAccount) {
200 return; 200 return;
201 } 201 }
202 server = m_SmtpAccount->getServer(); 202 server = m_SmtpAccount->getServer();
203 if ( m_SmtpAccount->ConnectionType() == 2 ) { 203 if ( m_SmtpAccount->ConnectionType() == 2 ) {
204 ssl = true; 204 ssl = true;
205 try_tls = false; 205 try_tls = false;
206 } else if (m_SmtpAccount->ConnectionType() == 1) { 206 } else if (m_SmtpAccount->ConnectionType() == 1) {
207 force_tls = true; 207 force_tls = true;
208 } 208 }
209 int result = 1; 209 int result = 1;
210 port = m_SmtpAccount->getPort().toUInt(); 210 port = m_SmtpAccount->getPort().toUInt();
211 211
212 m_smtp = mailsmtp_new( 20, &progress ); 212 m_smtp = mailsmtp_new( 20, &progress );
213 if ( m_smtp == NULL ) { 213 if ( m_smtp == NULL ) {
214 /* no failure message cause this happens when problems with memory - than we 214 /* no failure message cause this happens when problems with memory - than we
215 we can not display any messagebox */ 215 we can not display any messagebox */
216 return; 216 return;
217 } 217 }
218 218
219 int err = MAILSMTP_NO_ERROR; 219 int err = MAILSMTP_NO_ERROR;
220 ; // odebug << "Servername " << server << " at port " << port << "" << oendl; 220 ; // odebug << "Servername " << server << " at port " << port << "" << oendl;
221 if ( ssl ) { 221 if ( ssl ) {
222 ; // odebug << "SSL session" << oendl; 222 qDebug("smtp: ssl_connect ");
223 err = mailsmtp_ssl_connect( m_smtp, server.latin1(), port ); 223 err = mailsmtp_ssl_connect( m_smtp, server.latin1(), port );
224 } else { 224 } else {
225 ; // odebug << "No SSL session" << oendl; 225 ; // odebug << "No SSL session" << oendl;
226 err = mailsmtp_socket_connect( m_smtp, server.latin1(), port ); 226 err = mailsmtp_socket_connect( m_smtp, server.latin1(), port );
227 } 227 }
228 if ( err != MAILSMTP_NO_ERROR ) { 228 if ( err != MAILSMTP_NO_ERROR ) {
229 ; // odebug << "Error init connection" << oendl; 229 qDebug("Error init SMTP connection" );
230 failuretext = i18n("Error init SMTP connection: %1").arg(mailsmtpError(err)); 230 failuretext = i18n("Error init SMTP connection:\n%1").arg(mailsmtpError(err));
231 result = 0; 231 result = 0;
232 } 232 }
233 233
234 qDebug("SMTP connection inited ");
234 /* switch to tls after init 'cause there it will send the ehlo */ 235 /* switch to tls after init 'cause there it will send the ehlo */
235 if (result) { 236 if (result) {
236 err = mailsmtp_init( m_smtp ); 237 err = mailsmtp_init( m_smtp );
237 if (err != MAILSMTP_NO_ERROR) { 238 if (err != MAILSMTP_NO_ERROR) {
238 result = 0; 239 result = 0;
239 failuretext = i18n("Error init SMTP connection: %1").arg(mailsmtpError(err)); 240 qDebug("Error init SMTP connection ");
241 failuretext = i18n("Error init SMTP connection:\n%1").arg(mailsmtpError(err));
240 } 242 }
241 } 243 }
242
243 if (try_tls) { 244 if (try_tls) {
245 qDebug("Smpt: Try tls ");
244 err = start_smtp_tls(); 246 err = start_smtp_tls();
245 if (err != MAILSMTP_NO_ERROR) { 247 if (err != MAILSMTP_NO_ERROR) {
246 try_tls = false; 248 try_tls = false;
249 qDebug("no tls ");
247 } else { 250 } else {
248 err = mailesmtp_ehlo(m_smtp); 251 err = mailesmtp_ehlo(m_smtp);
249 } 252 }
250 } 253 }
251 254 //qDebug("mailesmtp_ehlo %d ",err );
252 if (!try_tls && force_tls) { 255 if (!try_tls && force_tls) {
253 result = 0; 256 result = 0;
254 failuretext = i18n("Error init SMTP tls: %1").arg(mailsmtpError(err)); 257 failuretext = i18n("Error init SMTP tls:%1").arg(mailsmtpError(err));
255 } 258 }
256 259
257 if (result==1 && m_SmtpAccount->getLogin() ) { 260 if (result==1 && m_SmtpAccount->getLogin() ) {
258 ; // odebug << "smtp with auth" << oendl; 261 ; // odebug << "smtp with auth" << oendl;
259 if ( m_SmtpAccount->getUser().isEmpty() || m_SmtpAccount->getPassword().isEmpty() ) { 262 if ( m_SmtpAccount->getUser().isEmpty() || m_SmtpAccount->getPassword().isEmpty() ) {
260 // get'em 263 // get'em
261 LoginDialog login( m_SmtpAccount->getUser(), 264 LoginDialog login( m_SmtpAccount->getUser(),
262 m_SmtpAccount->getPassword(), NULL, 0, true ); 265 m_SmtpAccount->getPassword(), NULL, 0, true );
263 login.show(); 266 login.show();
264 if ( QDialog::Accepted == login.exec() ) { 267 if ( QDialog::Accepted == login.exec() ) {
265 // ok 268 // ok
266 user = login.getUser(); 269 user = login.getUser();
267 pass = login.getPassword(); 270 pass = login.getPassword();
268 } else { 271 } else {
269 result = 0; 272 result = 0;
270 failuretext=i18n("Login aborted - storing mail to localfolder"); 273 failuretext=i18n("Login aborted - \nstoring mail to localfolder");
271 } 274 }
272 } else { 275 } else {
273 user = m_SmtpAccount->getUser(); 276 user = m_SmtpAccount->getUser();
274 pass = m_SmtpAccount->getPassword(); 277 pass = m_SmtpAccount->getPassword();
275 } 278 }
276 ; // odebug << "session->auth: " << m_smtp->auth << "" << oendl; 279 ; // odebug << "session->auth: " << m_smtp->auth << "" << oendl;
277 if (result) { 280 if (result) {
278 err = mailsmtp_auth( m_smtp, (char*)user.latin1(), (char*)pass.latin1() ); 281 err = mailsmtp_auth( m_smtp, (char*)user.latin1(), (char*)pass.latin1() );
279 if ( err == MAILSMTP_NO_ERROR ) { 282 if ( err == MAILSMTP_NO_ERROR ) {
280 ; // odebug << "auth ok" << oendl; 283 qDebug("Smtp authentification ok ");
281 } else { 284 } else {
282 failuretext = i18n("Authentification failed"); 285 failuretext = i18n("Authentification failed");
283 result = 0; 286 result = 0;
284 } 287 }
285 } 288 }
286 } 289 }
287} 290}
288 291
289void SMTPwrapper::disc_server() 292void SMTPwrapper::disc_server()
290{ 293{
291 if (m_smtp) { 294 if (m_smtp) {
292 mailsmtp_quit( m_smtp ); 295 mailsmtp_quit( m_smtp );
293 mailsmtp_free( m_smtp ); 296 mailsmtp_free( m_smtp );
294 m_smtp = 0; 297 m_smtp = 0;
295 } 298 }
296} 299}
297 300
298int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size ) 301int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size )
299{ 302{
300 int err,result; 303 int err,result;
301 QString failuretext = ""; 304 QString failuretext = "";
302 305
303 connect_server(); 306 connect_server();
304 307
305 result = 1; 308 result = 1;
306 if (m_smtp) { 309 if (m_smtp) {
307 err = mailsmtp_send( m_smtp, from, rcpts, data, size ); 310 err = mailsmtp_send( m_smtp, from, rcpts, data, size );
308 if ( err != MAILSMTP_NO_ERROR ) { 311 if ( err != MAILSMTP_NO_ERROR ) {
309 failuretext=i18n("Error sending mail: %1").arg(mailsmtpError(err)); 312 qDebug("Error sending mail");
313 failuretext=i18n("Error sending mail:\n%1").arg(mailsmtpError(err));
310 result = 0; 314 result = 0;
311 } 315 }
312 } else { 316 } else {
313 result = 0; 317 result = 0;
314 } 318 }
315 319
316 if (!result) { 320 if (!result) {
317 storeFailedMail(data,size,failuretext); 321 storeFailedMail(data,size,failuretext);
318 } else { 322 } else {
319 ; // odebug << "Mail sent." << oendl; 323 ; // odebug << "Mail sent." << oendl;
320 storeMail(data,size,"Sent"); 324 storeMail(data,size,"Sent");
321 } 325 }
322 return result; 326 return result;
323} 327}
324 328
325bool SMTPwrapper::sendMail(const Opie::Core::OSmartPointer<Mail>&mail,bool later ) 329bool SMTPwrapper::sendMail(const Opie::Core::OSmartPointer<Mail>&mail,bool later )
326{ 330{
327 mailmime * mimeMail; 331 mailmime * mimeMail;
328 bool result = true; 332 bool result = true;
329 mimeMail = createMimeMail(mail ); 333 mimeMail = createMimeMail(mail );
330 if ( mimeMail == 0 ) { 334 if ( mimeMail == 0 ) {
331 qDebug("SMTP wrapper:Error creating mail! "); 335 qDebug("SMTP wrapper:Error creating mail! ");
332 return false; 336 return false;
333 } else { 337 } else {
334 sendProgress = new progressMailSend(); 338 sendProgress = new progressMailSend();
335 sendProgress->show(); 339 sendProgress->show();
336 sendProgress->setMaxMails(1); 340 sendProgress->setMaxMails(1);
337 result = smtpSend( mimeMail,later); 341 result = smtpSend( mimeMail,later);
338 ; // odebug << "Clean up done" << oendl; 342 ; // odebug << "Clean up done" << oendl;
339 sendProgress->hide(); 343 sendProgress->hide();
340 delete sendProgress; 344 delete sendProgress;
341 sendProgress = 0; 345 sendProgress = 0;
342 mailmime_free( mimeMail ); 346 mailmime_free( mimeMail );
343 } 347 }
344 return result; 348 return result;
345} 349}
346 350
347int SMTPwrapper::sendQueuedMail(AbstractMail*wrap,const RecMailP&which) { 351int SMTPwrapper::sendQueuedMail(AbstractMail*wrap,const RecMailP&which) {
348 size_t curTok = 0; 352 size_t curTok = 0;
349 mailimf_fields *fields = 0; 353 mailimf_fields *fields = 0;
350 mailimf_field*ffrom = 0; 354 mailimf_field*ffrom = 0;
351 clist *rcpts = 0; 355 clist *rcpts = 0;
352 char*from = 0; 356 char*from = 0;
353 int res = 0; 357 int res = 0;
354 358
355 encodedString * data = wrap->fetchRawBody(which); 359 encodedString * data = wrap->fetchRawBody(which);
356 if (!data) 360 if (!data)
357 return 0; 361 return 0;
358 int err = mailimf_fields_parse( data->Content(), data->Length(), &curTok, &fields ); 362 int err = mailimf_fields_parse( data->Content(), data->Length(), &curTok, &fields );
359 if (err != MAILIMF_NO_ERROR) { 363 if (err != MAILIMF_NO_ERROR) {
360 delete data; 364 delete data;
361 delete wrap; 365 delete wrap;
362 return 0; 366 return 0;
363 } 367 }
364 368
365 rcpts = createRcptList( fields ); 369 rcpts = createRcptList( fields );
366 ffrom = getField(fields, MAILIMF_FIELD_FROM ); 370 ffrom = getField(fields, MAILIMF_FIELD_FROM );
367 from = getFrom(ffrom); 371 from = getFrom(ffrom);
368 372
369 if (rcpts && from) { 373 if (rcpts && from) {
370 res = smtpSend(from,rcpts,data->Content(),data->Length()); 374 res = smtpSend(from,rcpts,data->Content(),data->Length());
371 } 375 }
372 if (fields) { 376 if (fields) {
373 mailimf_fields_free(fields); 377 mailimf_fields_free(fields);