-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.cpp | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp index 00181ff..4764b77 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp | |||
@@ -157,575 +157,573 @@ mailimf_fields *SMTPwrapper::createImfFields(const Mail&mail ) | |||
157 | 157 | ||
158 | from = mailimf_mailbox_list_new_empty(); | 158 | from = mailimf_mailbox_list_new_empty(); |
159 | if ( from == NULL ) goto err_free_fromBox; | 159 | if ( from == NULL ) goto err_free_fromBox; |
160 | 160 | ||
161 | err = mailimf_mailbox_list_add( from, fromBox ); | 161 | err = mailimf_mailbox_list_add( from, fromBox ); |
162 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; | 162 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; |
163 | 163 | ||
164 | to = parseAddresses( mail.getTo() ); | 164 | to = parseAddresses( mail.getTo() ); |
165 | if ( to == NULL ) goto err_free_from; | 165 | if ( to == NULL ) goto err_free_from; |
166 | 166 | ||
167 | cc = parseAddresses( mail.getCC() ); | 167 | cc = parseAddresses( mail.getCC() ); |
168 | bcc = parseAddresses( mail.getBCC() ); | 168 | bcc = parseAddresses( mail.getBCC() ); |
169 | reply = parseAddresses( mail.getReply() ); | 169 | reply = parseAddresses( mail.getReply() ); |
170 | 170 | ||
171 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, | 171 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, |
172 | NULL, NULL, subject ); | 172 | NULL, NULL, subject ); |
173 | if ( fields == NULL ) goto err_free_reply; | 173 | if ( fields == NULL ) goto err_free_reply; |
174 | 174 | ||
175 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), | 175 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), |
176 | strdup( USER_AGENT ) ); | 176 | strdup( USER_AGENT ) ); |
177 | if ( xmailer == NULL ) goto err_free_fields; | 177 | if ( xmailer == NULL ) goto err_free_fields; |
178 | 178 | ||
179 | err = mailimf_fields_add( fields, xmailer ); | 179 | err = mailimf_fields_add( fields, xmailer ); |
180 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; | 180 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; |
181 | 181 | ||
182 | return fields; // Success :) | 182 | return fields; // Success :) |
183 | 183 | ||
184 | err_free_xmailer: | 184 | err_free_xmailer: |
185 | if (xmailer) mailimf_field_free( xmailer ); | 185 | if (xmailer) mailimf_field_free( xmailer ); |
186 | err_free_fields: | 186 | err_free_fields: |
187 | if (fields) mailimf_fields_free( fields ); | 187 | if (fields) mailimf_fields_free( fields ); |
188 | err_free_reply: | 188 | err_free_reply: |
189 | if (reply) mailimf_address_list_free( reply ); | 189 | if (reply) mailimf_address_list_free( reply ); |
190 | if (bcc) mailimf_address_list_free( bcc ); | 190 | if (bcc) mailimf_address_list_free( bcc ); |
191 | if (cc) mailimf_address_list_free( cc ); | 191 | if (cc) mailimf_address_list_free( cc ); |
192 | if (to) mailimf_address_list_free( to ); | 192 | if (to) mailimf_address_list_free( to ); |
193 | err_free_from: | 193 | err_free_from: |
194 | if (from) mailimf_mailbox_list_free( from ); | 194 | if (from) mailimf_mailbox_list_free( from ); |
195 | err_free_fromBox: | 195 | err_free_fromBox: |
196 | mailimf_mailbox_free( fromBox ); | 196 | mailimf_mailbox_free( fromBox ); |
197 | err_free_sender: | 197 | err_free_sender: |
198 | if (sender) mailimf_mailbox_free( sender ); | 198 | if (sender) mailimf_mailbox_free( sender ); |
199 | err_free: | 199 | err_free: |
200 | if (subject) free( subject ); | 200 | if (subject) free( subject ); |
201 | qDebug( "createImfFields - error" ); | 201 | qDebug( "createImfFields - error" ); |
202 | 202 | ||
203 | return NULL; // Error :( | 203 | return NULL; // Error :( |
204 | } | 204 | } |
205 | 205 | ||
206 | mailmime *SMTPwrapper::buildTxtPart(const QString&str ) | 206 | mailmime *SMTPwrapper::buildTxtPart(const QString&str ) |
207 | { | 207 | { |
208 | mailmime *txtPart; | 208 | mailmime *txtPart; |
209 | mailmime_fields *fields; | 209 | mailmime_fields *fields; |
210 | mailmime_content *content; | 210 | mailmime_content *content; |
211 | mailmime_parameter *param; | 211 | mailmime_parameter *param; |
212 | int err; | 212 | int err; |
213 | 213 | ||
214 | param = mailmime_parameter_new( strdup( "charset" ), | 214 | param = mailmime_parameter_new( strdup( "charset" ), |
215 | strdup( "iso-8859-1" ) ); | 215 | strdup( "iso-8859-1" ) ); |
216 | if ( param == NULL ) goto err_free; | 216 | if ( param == NULL ) goto err_free; |
217 | 217 | ||
218 | content = mailmime_content_new_with_str( "text/plain" ); | 218 | content = mailmime_content_new_with_str( "text/plain" ); |
219 | if ( content == NULL ) goto err_free_param; | 219 | if ( content == NULL ) goto err_free_param; |
220 | 220 | ||
221 | err = clist_append( content->ct_parameters, param ); | 221 | err = clist_append( content->ct_parameters, param ); |
222 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; | 222 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; |
223 | 223 | ||
224 | fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT); | 224 | fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT); |
225 | if ( fields == NULL ) goto err_free_content; | 225 | if ( fields == NULL ) goto err_free_content; |
226 | 226 | ||
227 | txtPart = mailmime_new_empty( content, fields ); | 227 | txtPart = mailmime_new_empty( content, fields ); |
228 | if ( txtPart == NULL ) goto err_free_fields; | 228 | if ( txtPart == NULL ) goto err_free_fields; |
229 | 229 | ||
230 | err = mailmime_set_body_text( txtPart, (char*)str.data(), str.length() ); | 230 | err = mailmime_set_body_text( txtPart, (char*)str.data(), str.length() ); |
231 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 231 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
232 | 232 | ||
233 | return txtPart; // Success :) | 233 | return txtPart; // Success :) |
234 | 234 | ||
235 | err_free_txtPart: | 235 | err_free_txtPart: |
236 | mailmime_free( txtPart ); | 236 | mailmime_free( txtPart ); |
237 | err_free_fields: | 237 | err_free_fields: |
238 | mailmime_fields_free( fields ); | 238 | mailmime_fields_free( fields ); |
239 | err_free_content: | 239 | err_free_content: |
240 | mailmime_content_free( content ); | 240 | mailmime_content_free( content ); |
241 | err_free_param: | 241 | err_free_param: |
242 | mailmime_parameter_free( param ); | 242 | mailmime_parameter_free( param ); |
243 | err_free: | 243 | err_free: |
244 | qDebug( "buildTxtPart - error" ); | 244 | qDebug( "buildTxtPart - error" ); |
245 | 245 | ||
246 | return NULL; // Error :( | 246 | return NULL; // Error :( |
247 | } | 247 | } |
248 | 248 | ||
249 | mailmime *SMTPwrapper::buildFilePart(const QString&filename,const QString&mimetype,const QString&TextContent ) | 249 | mailmime *SMTPwrapper::buildFilePart(const QString&filename,const QString&mimetype,const QString&TextContent ) |
250 | { | 250 | { |
251 | mailmime * filePart = 0; | 251 | mailmime * filePart = 0; |
252 | mailmime_fields * fields = 0; | 252 | mailmime_fields * fields = 0; |
253 | mailmime_content * content = 0; | 253 | mailmime_content * content = 0; |
254 | mailmime_parameter * param = 0; | 254 | mailmime_parameter * param = 0; |
255 | char*name = 0; | 255 | char*name = 0; |
256 | char*file = 0; | 256 | char*file = 0; |
257 | int err; | 257 | int err; |
258 | 258 | ||
259 | int pos = filename.findRev( '/' ); | 259 | int pos = filename.findRev( '/' ); |
260 | 260 | ||
261 | if (filename.length()>0) { | 261 | if (filename.length()>0) { |
262 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); | 262 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); |
263 | name = strdup( tmp.latin1() ); // just filename | 263 | name = strdup( tmp.latin1() ); // just filename |
264 | file = strdup( filename.latin1() ); // full name with path | 264 | file = strdup( filename.latin1() ); // full name with path |
265 | } | 265 | } |
266 | 266 | ||
267 | int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; | 267 | int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; |
268 | int mechanism = MAILMIME_MECHANISM_BASE64; | 268 | int mechanism = MAILMIME_MECHANISM_BASE64; |
269 | 269 | ||
270 | if ( mimetype.startsWith( "text/" ) ) { | 270 | if ( mimetype.startsWith( "text/" ) ) { |
271 | param = mailmime_parameter_new( strdup( "charset" ), | 271 | param = mailmime_parameter_new( strdup( "charset" ), |
272 | strdup( "iso-8859-1" ) ); | 272 | strdup( "iso-8859-1" ) ); |
273 | mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; | 273 | mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; |
274 | } | 274 | } |
275 | 275 | ||
276 | fields = mailmime_fields_new_filename( | 276 | fields = mailmime_fields_new_filename( |
277 | disptype, name, | 277 | disptype, name, |
278 | mechanism ); | 278 | mechanism ); |
279 | content = mailmime_content_new_with_str( (char*)mimetype.latin1() ); | 279 | content = mailmime_content_new_with_str( (char*)mimetype.latin1() ); |
280 | if (content!=0 && fields != 0) { | 280 | if (content!=0 && fields != 0) { |
281 | if (param) { | 281 | if (param) { |
282 | clist_append(content->ct_parameters,param); | 282 | clist_append(content->ct_parameters,param); |
283 | param = 0; | 283 | param = 0; |
284 | } | 284 | } |
285 | if (filename.length()>0) { | 285 | if (filename.length()>0) { |
286 | QFileInfo f(filename); | 286 | QFileInfo f(filename); |
287 | param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); | 287 | param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); |
288 | clist_append(content->ct_parameters,param); | 288 | clist_append(content->ct_parameters,param); |
289 | param = 0; | 289 | param = 0; |
290 | } | 290 | } |
291 | filePart = mailmime_new_empty( content, fields ); | 291 | filePart = mailmime_new_empty( content, fields ); |
292 | } | 292 | } |
293 | if (filePart) { | 293 | if (filePart) { |
294 | if (filename.length()>0) { | 294 | if (filename.length()>0) { |
295 | err = mailmime_set_body_file( filePart, file ); | 295 | err = mailmime_set_body_file( filePart, file ); |
296 | } else { | 296 | } else { |
297 | err = mailmime_set_body_text(filePart,strdup(TextContent.data()),TextContent.length()); | 297 | err = mailmime_set_body_text(filePart,strdup(TextContent.data()),TextContent.length()); |
298 | } | 298 | } |
299 | if (err != MAILIMF_NO_ERROR) { | 299 | if (err != MAILIMF_NO_ERROR) { |
300 | qDebug("Error setting body with file %s",file); | 300 | qDebug("Error setting body with file %s",file); |
301 | mailmime_free( filePart ); | 301 | mailmime_free( filePart ); |
302 | filePart = 0; | 302 | filePart = 0; |
303 | } | 303 | } |
304 | } | 304 | } |
305 | 305 | ||
306 | if (!filePart) { | 306 | if (!filePart) { |
307 | if ( param != NULL ) { | 307 | if ( param != NULL ) { |
308 | mailmime_parameter_free( param ); | 308 | mailmime_parameter_free( param ); |
309 | } | 309 | } |
310 | if (content) { | 310 | if (content) { |
311 | mailmime_content_free( content ); | 311 | mailmime_content_free( content ); |
312 | } | 312 | } |
313 | if (fields) { | 313 | if (fields) { |
314 | mailmime_fields_free( fields ); | 314 | mailmime_fields_free( fields ); |
315 | } else { | 315 | } else { |
316 | if (name) { | 316 | if (name) { |
317 | free( name ); | 317 | free( name ); |
318 | } | 318 | } |
319 | if (file) { | 319 | if (file) { |
320 | free( file ); | 320 | free( file ); |
321 | } | 321 | } |
322 | } | 322 | } |
323 | } | 323 | } |
324 | return filePart; // Success :) | 324 | return filePart; // Success :) |
325 | 325 | ||
326 | } | 326 | } |
327 | 327 | ||
328 | void SMTPwrapper::addFileParts( mailmime *message,const QList<Attachment>&files ) | 328 | void SMTPwrapper::addFileParts( mailmime *message,const QList<Attachment>&files ) |
329 | { | 329 | { |
330 | const Attachment *it; | 330 | const Attachment *it; |
331 | unsigned int count = files.count(); | 331 | unsigned int count = files.count(); |
332 | qDebug("List contains %i values",count); | 332 | qDebug("List contains %i values",count); |
333 | for ( unsigned int i = 0; i < count; ++i ) { | 333 | for ( unsigned int i = 0; i < count; ++i ) { |
334 | qDebug( "Adding file" ); | 334 | qDebug( "Adding file" ); |
335 | mailmime *filePart; | 335 | mailmime *filePart; |
336 | int err; | 336 | int err; |
337 | it = ((QList<Attachment>)files).at(i); | 337 | it = ((QList<Attachment>)files).at(i); |
338 | 338 | ||
339 | filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); | 339 | filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); |
340 | if ( filePart == NULL ) { | 340 | if ( filePart == NULL ) { |
341 | qDebug( "addFileParts: error adding file:" ); | 341 | qDebug( "addFileParts: error adding file:" ); |
342 | qDebug( it->getFileName() ); | 342 | qDebug( it->getFileName() ); |
343 | continue; | 343 | continue; |
344 | } | 344 | } |
345 | err = mailmime_smart_add_part( message, filePart ); | 345 | err = mailmime_smart_add_part( message, filePart ); |
346 | if ( err != MAILIMF_NO_ERROR ) { | 346 | if ( err != MAILIMF_NO_ERROR ) { |
347 | mailmime_free( filePart ); | 347 | mailmime_free( filePart ); |
348 | qDebug("error smart add"); | 348 | qDebug("error smart add"); |
349 | } | 349 | } |
350 | } | 350 | } |
351 | } | 351 | } |
352 | 352 | ||
353 | mailmime *SMTPwrapper::createMimeMail(const Mail &mail ) | 353 | mailmime *SMTPwrapper::createMimeMail(const Mail &mail ) |
354 | { | 354 | { |
355 | mailmime *message, *txtPart; | 355 | mailmime *message, *txtPart; |
356 | mailimf_fields *fields; | 356 | mailimf_fields *fields; |
357 | int err; | 357 | int err; |
358 | 358 | ||
359 | fields = createImfFields( mail ); | 359 | fields = createImfFields( mail ); |
360 | if ( fields == NULL ) goto err_free; | 360 | if ( fields == NULL ) goto err_free; |
361 | 361 | ||
362 | message = mailmime_new_message_data( NULL ); | 362 | message = mailmime_new_message_data( NULL ); |
363 | if ( message == NULL ) goto err_free_fields; | 363 | if ( message == NULL ) goto err_free_fields; |
364 | 364 | ||
365 | mailmime_set_imf_fields( message, fields ); | 365 | mailmime_set_imf_fields( message, fields ); |
366 | 366 | ||
367 | txtPart = buildTxtPart( mail.getMessage() ); | 367 | txtPart = buildTxtPart( mail.getMessage() ); |
368 | 368 | ||
369 | if ( txtPart == NULL ) goto err_free_message; | 369 | if ( txtPart == NULL ) goto err_free_message; |
370 | 370 | ||
371 | err = mailmime_smart_add_part( message, txtPart ); | 371 | err = mailmime_smart_add_part( message, txtPart ); |
372 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 372 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
373 | 373 | ||
374 | addFileParts( message, mail.getAttachments() ); | 374 | addFileParts( message, mail.getAttachments() ); |
375 | 375 | ||
376 | return message; // Success :) | 376 | return message; // Success :) |
377 | 377 | ||
378 | err_free_txtPart: | 378 | err_free_txtPart: |
379 | mailmime_free( txtPart ); | 379 | mailmime_free( txtPart ); |
380 | err_free_message: | 380 | err_free_message: |
381 | mailmime_free( message ); | 381 | mailmime_free( message ); |
382 | err_free_fields: | 382 | err_free_fields: |
383 | mailimf_fields_free( fields ); | 383 | mailimf_fields_free( fields ); |
384 | err_free: | 384 | err_free: |
385 | qDebug( "createMimeMail: error" ); | 385 | qDebug( "createMimeMail: error" ); |
386 | 386 | ||
387 | return NULL; // Error :( | 387 | return NULL; // Error :( |
388 | } | 388 | } |
389 | 389 | ||
390 | mailimf_field *SMTPwrapper::getField( mailimf_fields *fields, int type ) | 390 | mailimf_field *SMTPwrapper::getField( mailimf_fields *fields, int type ) |
391 | { | 391 | { |
392 | mailimf_field *field; | 392 | mailimf_field *field; |
393 | clistiter *it; | 393 | clistiter *it; |
394 | 394 | ||
395 | it = clist_begin( fields->fld_list ); | 395 | it = clist_begin( fields->fld_list ); |
396 | while ( it ) { | 396 | while ( it ) { |
397 | field = (mailimf_field *) it->data; | 397 | field = (mailimf_field *) it->data; |
398 | if ( field->fld_type == type ) { | 398 | if ( field->fld_type == type ) { |
399 | return field; | 399 | return field; |
400 | } | 400 | } |
401 | it = it->next; | 401 | it = it->next; |
402 | } | 402 | } |
403 | 403 | ||
404 | return NULL; | 404 | return NULL; |
405 | } | 405 | } |
406 | 406 | ||
407 | void SMTPwrapper::addRcpts( clist *list, mailimf_address_list *addr_list ) | 407 | void SMTPwrapper::addRcpts( clist *list, mailimf_address_list *addr_list ) |
408 | { | 408 | { |
409 | clistiter *it, *it2; | 409 | clistiter *it, *it2; |
410 | 410 | ||
411 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { | 411 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { |
412 | mailimf_address *addr; | 412 | mailimf_address *addr; |
413 | addr = (mailimf_address *) it->data; | 413 | addr = (mailimf_address *) it->data; |
414 | 414 | ||
415 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { | 415 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { |
416 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); | 416 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); |
417 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { | 417 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { |
418 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; | 418 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; |
419 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { | 419 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { |
420 | mailimf_mailbox *mbox; | 420 | mailimf_mailbox *mbox; |
421 | mbox = (mailimf_mailbox *) it2->data; | 421 | mbox = (mailimf_mailbox *) it2->data; |
422 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); | 422 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); |
423 | } | 423 | } |
424 | } | 424 | } |
425 | } | 425 | } |
426 | } | 426 | } |
427 | 427 | ||
428 | clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) | 428 | clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) |
429 | { | 429 | { |
430 | clist *rcptList; | 430 | clist *rcptList; |
431 | mailimf_field *field; | 431 | mailimf_field *field; |
432 | 432 | ||
433 | rcptList = esmtp_address_list_new(); | 433 | rcptList = esmtp_address_list_new(); |
434 | 434 | ||
435 | field = getField( fields, MAILIMF_FIELD_TO ); | 435 | field = getField( fields, MAILIMF_FIELD_TO ); |
436 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) | 436 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) |
437 | && field->fld_data.fld_to->to_addr_list ) { | 437 | && field->fld_data.fld_to->to_addr_list ) { |
438 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); | 438 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); |
439 | } | 439 | } |
440 | 440 | ||
441 | field = getField( fields, MAILIMF_FIELD_CC ); | 441 | field = getField( fields, MAILIMF_FIELD_CC ); |
442 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) | 442 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) |
443 | && field->fld_data.fld_cc->cc_addr_list ) { | 443 | && field->fld_data.fld_cc->cc_addr_list ) { |
444 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); | 444 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); |
445 | } | 445 | } |
446 | 446 | ||
447 | field = getField( fields, MAILIMF_FIELD_BCC ); | 447 | field = getField( fields, MAILIMF_FIELD_BCC ); |
448 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) | 448 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) |
449 | && field->fld_data.fld_bcc->bcc_addr_list ) { | 449 | && field->fld_data.fld_bcc->bcc_addr_list ) { |
450 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); | 450 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); |
451 | } | 451 | } |
452 | 452 | ||
453 | return rcptList; | 453 | return rcptList; |
454 | } | 454 | } |
455 | 455 | ||
456 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) | 456 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) |
457 | { | 457 | { |
458 | char *from = NULL; | 458 | char *from = NULL; |
459 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) | 459 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) |
460 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { | 460 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { |
461 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; | 461 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; |
462 | clistiter *it; | 462 | clistiter *it; |
463 | for ( it = clist_begin( cl ); it; it = it->next ) { | 463 | for ( it = clist_begin( cl ); it; it = it->next ) { |
464 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | 464 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; |
465 | from = strdup( mb->mb_addr_spec ); | 465 | from = strdup( mb->mb_addr_spec ); |
466 | } | 466 | } |
467 | } | 467 | } |
468 | 468 | ||
469 | return from; | 469 | return from; |
470 | } | 470 | } |
471 | 471 | ||
472 | char *SMTPwrapper::getFrom( mailmime *mail ) | 472 | char *SMTPwrapper::getFrom( mailmime *mail ) |
473 | { | 473 | { |
474 | /* no need to delete - its just a pointer to structure content */ | 474 | /* no need to delete - its just a pointer to structure content */ |
475 | mailimf_field *ffrom = 0; | 475 | mailimf_field *ffrom = 0; |
476 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); | 476 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); |
477 | return getFrom(ffrom); | 477 | return getFrom(ffrom); |
478 | } | 478 | } |
479 | 479 | ||
480 | void SMTPwrapper::progress( size_t current, size_t maximum ) | 480 | void SMTPwrapper::progress( size_t current, size_t maximum ) |
481 | { | 481 | { |
482 | if (SMTPwrapper::sendProgress) { | 482 | if (SMTPwrapper::sendProgress) { |
483 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); | 483 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); |
484 | qApp->processEvents(); | 484 | qApp->processEvents(); |
485 | } | 485 | } |
486 | } | 486 | } |
487 | 487 | ||
488 | void SMTPwrapper::storeMail(const char*mail, size_t length, const QString&box) | 488 | void SMTPwrapper::storeMail(const char*mail, size_t length, const QString&box) |
489 | { | 489 | { |
490 | if (!mail) return; | 490 | if (!mail) return; |
491 | QString localfolders = AbstractMail::defaultLocalfolder(); | 491 | QString localfolders = AbstractMail::defaultLocalfolder(); |
492 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | 492 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); |
493 | wrap->storeMessage(mail,length,box); | 493 | wrap->storeMessage(mail,length,box); |
494 | delete wrap; | 494 | delete wrap; |
495 | } | 495 | } |
496 | 496 | ||
497 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) | 497 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) |
498 | { | 498 | { |
499 | clist *rcpts = 0; | 499 | clist *rcpts = 0; |
500 | char *from, *data; | 500 | char *from, *data; |
501 | size_t size; | 501 | size_t size; |
502 | 502 | ||
503 | if ( smtp == NULL ) { | 503 | if ( smtp == NULL ) { |
504 | return; | 504 | return; |
505 | } | 505 | } |
506 | from = data = 0; | 506 | from = data = 0; |
507 | 507 | ||
508 | mailmessage * msg = 0; | 508 | mailmessage * msg = 0; |
509 | msg = mime_message_init(mail); | 509 | msg = mime_message_init(mail); |
510 | mime_message_set_tmpdir(msg,getenv( "HOME" )); | 510 | mime_message_set_tmpdir(msg,getenv( "HOME" )); |
511 | int r = mailmessage_fetch(msg,&data,&size); | 511 | int r = mailmessage_fetch(msg,&data,&size); |
512 | mime_message_detach_mime(msg); | 512 | mime_message_detach_mime(msg); |
513 | mailmessage_free(msg); | 513 | mailmessage_free(msg); |
514 | if (r != MAIL_NO_ERROR || !data) { | 514 | if (r != MAIL_NO_ERROR || !data) { |
515 | if (data) free(data); | 515 | if (data) free(data); |
516 | qDebug("Error fetching mime..."); | 516 | qDebug("Error fetching mime..."); |
517 | return; | 517 | return; |
518 | } | 518 | } |
519 | QString tmp = data; | 519 | QString tmp = data; |
520 | tmp.replace(QRegExp("\r+",true,false),""); | 520 | tmp.replace(QRegExp("\r+",true,false),""); |
521 | msg = 0; | 521 | msg = 0; |
522 | if (later) { | 522 | if (later) { |
523 | storeMail((char*)tmp.data(),tmp.length(),"Outgoing"); | 523 | storeMail((char*)tmp.data(),tmp.length(),"Outgoing"); |
524 | if (data) free( data ); | 524 | if (data) free( data ); |
525 | Config cfg( "mail" ); | 525 | Config cfg( "mail" ); |
526 | cfg.setGroup( "Status" ); | 526 | cfg.setGroup( "Status" ); |
527 | cfg.writeEntry( "outgoing", ++m_queuedMail ); | 527 | cfg.writeEntry( "outgoing", ++m_queuedMail ); |
528 | emit queuedMails( m_queuedMail ); | 528 | emit queuedMails( m_queuedMail ); |
529 | return; | 529 | return; |
530 | } | 530 | } |
531 | from = getFrom( mail ); | 531 | from = getFrom( mail ); |
532 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | 532 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); |
533 | smtpSend(from,rcpts,data,size,smtp); | 533 | smtpSend(from,rcpts,data,size,smtp); |
534 | if (data) {free(data);} | 534 | if (data) {free(data);} |
535 | if (from) {free(from);} | 535 | if (from) {free(from);} |
536 | if (rcpts) smtp_address_list_free( rcpts ); | 536 | if (rcpts) smtp_address_list_free( rcpts ); |
537 | } | 537 | } |
538 | 538 | ||
539 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ) | 539 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ) |
540 | { | 540 | { |
541 | char *server, *user, *pass; | 541 | const char *server, *user, *pass; |
542 | bool ssl; | 542 | bool ssl; |
543 | uint16_t port; | 543 | uint16_t port; |
544 | mailsmtp *session; | 544 | mailsmtp *session; |
545 | int err,result; | 545 | int err,result; |
546 | 546 | ||
547 | result = 1; | 547 | result = 1; |
548 | server = user = pass = 0; | 548 | server = user = pass = 0; |
549 | server = strdup( smtp->getServer().latin1() ); | 549 | server = smtp->getServer().latin1(); |
550 | ssl = smtp->getSSL(); | 550 | ssl = smtp->getSSL(); |
551 | port = smtp->getPort().toUInt(); | 551 | port = smtp->getPort().toUInt(); |
552 | 552 | ||
553 | session = mailsmtp_new( 20, &progress ); | 553 | session = mailsmtp_new( 20, &progress ); |
554 | if ( session == NULL ) goto free_mem; | 554 | if ( session == NULL ) goto free_mem; |
555 | 555 | ||
556 | qDebug( "Servername %s at port %i", server, port ); | 556 | qDebug( "Servername %s at port %i", server, port ); |
557 | if ( ssl ) { | 557 | if ( ssl ) { |
558 | qDebug( "SSL session" ); | 558 | qDebug( "SSL session" ); |
559 | err = mailsmtp_ssl_connect( session, server, port ); | 559 | err = mailsmtp_ssl_connect( session, server, port ); |
560 | } else { | 560 | } else { |
561 | qDebug( "No SSL session" ); | 561 | qDebug( "No SSL session" ); |
562 | err = mailsmtp_socket_connect( session, server, port ); | 562 | err = mailsmtp_socket_connect( session, server, port ); |
563 | } | 563 | } |
564 | if ( err != MAILSMTP_NO_ERROR ) {result = 0;goto free_mem_session;} | 564 | if ( err != MAILSMTP_NO_ERROR ) {qDebug("Error init connection");result = 0;goto free_mem_session;} |
565 | 565 | ||
566 | err = mailsmtp_init( session ); | 566 | err = mailsmtp_init( session ); |
567 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} | 567 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} |
568 | 568 | ||
569 | qDebug( "INIT OK" ); | 569 | qDebug( "INIT OK" ); |
570 | 570 | ||
571 | if ( smtp->getLogin() ) { | 571 | if ( smtp->getLogin() ) { |
572 | qDebug("smtp with auth"); | ||
572 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | 573 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { |
573 | // get'em | 574 | // get'em |
574 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | 575 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); |
575 | login.show(); | 576 | login.show(); |
576 | if ( QDialog::Accepted == login.exec() ) { | 577 | if ( QDialog::Accepted == login.exec() ) { |
577 | // ok | 578 | // ok |
578 | user = strdup( login.getUser().latin1() ); | 579 | user = login.getUser().latin1(); |
579 | pass = strdup( login.getPassword().latin1() ); | 580 | pass = login.getPassword().latin1(); |
580 | } else { | 581 | } else { |
581 | result = 0; goto free_con_session; | 582 | result = 0; goto free_con_session; |
582 | } | 583 | } |
583 | } else { | 584 | } else { |
584 | user = strdup( smtp->getUser().latin1() ); | 585 | user = smtp->getUser().latin1(); |
585 | pass = strdup( smtp->getPassword().latin1() ); | 586 | pass = smtp->getPassword().latin1(); |
586 | } | 587 | } |
587 | qDebug( "session->auth: %i", session->auth); | 588 | qDebug( "session->auth: %i", session->auth); |
588 | err = mailsmtp_auth( session, user, pass ); | 589 | err = mailsmtp_auth( session, (char*)user, (char*)pass ); |
589 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); | 590 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); |
590 | qDebug( "Done auth!" ); | 591 | qDebug( "Done auth!" ); |
592 | } else { | ||
593 | qDebug("SMTP without auth"); | ||
591 | } | 594 | } |
592 | 595 | ||
593 | err = mailsmtp_send( session, from, rcpts, data, size ); | 596 | err = mailsmtp_send( session, from, rcpts, data, size ); |
594 | if ( err != MAILSMTP_NO_ERROR ) { | 597 | if ( err != MAILSMTP_NO_ERROR ) { |
595 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); | 598 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); |
596 | result = 0; goto free_con_session; | 599 | result = 0; goto free_con_session; |
597 | } | 600 | } |
598 | 601 | ||
599 | qDebug( "Mail sent." ); | 602 | qDebug( "Mail sent." ); |
600 | storeMail(data,size,"Sent"); | 603 | storeMail(data,size,"Sent"); |
601 | 604 | ||
602 | free_con_session: | 605 | free_con_session: |
603 | mailsmtp_quit( session ); | 606 | mailsmtp_quit( session ); |
604 | free_mem_session: | 607 | free_mem_session: |
605 | mailsmtp_free( session ); | 608 | mailsmtp_free( session ); |
606 | free_mem: | 609 | free_mem: |
607 | if (server) free( server ); | ||
608 | if ( smtp->getLogin() ) { | ||
609 | free( user ); | ||
610 | free( pass ); | ||
611 | } | ||
612 | return result; | 610 | return result; |
613 | } | 611 | } |
614 | 612 | ||
615 | void SMTPwrapper::sendMail(const Mail&mail,SMTPaccount*aSmtp,bool later ) | 613 | void SMTPwrapper::sendMail(const Mail&mail,SMTPaccount*aSmtp,bool later ) |
616 | { | 614 | { |
617 | mailmime * mimeMail; | 615 | mailmime * mimeMail; |
618 | 616 | ||
619 | SMTPaccount *smtp = aSmtp; | 617 | SMTPaccount *smtp = aSmtp; |
620 | 618 | ||
621 | if (!later && !smtp) { | 619 | if (!later && !smtp) { |
622 | qDebug("Didn't get any send method - giving up"); | 620 | qDebug("Didn't get any send method - giving up"); |
623 | return; | 621 | return; |
624 | } | 622 | } |
625 | mimeMail = createMimeMail(mail ); | 623 | mimeMail = createMimeMail(mail ); |
626 | if ( mimeMail == NULL ) { | 624 | if ( mimeMail == NULL ) { |
627 | qDebug( "sendMail: error creating mime mail" ); | 625 | qDebug( "sendMail: error creating mime mail" ); |
628 | } else { | 626 | } else { |
629 | sendProgress = new progressMailSend(); | 627 | sendProgress = new progressMailSend(); |
630 | sendProgress->show(); | 628 | sendProgress->show(); |
631 | sendProgress->setMaxMails(1); | 629 | sendProgress->setMaxMails(1); |
632 | smtpSend( mimeMail,later,smtp); | 630 | smtpSend( mimeMail,later,smtp); |
633 | qDebug("Clean up done"); | 631 | qDebug("Clean up done"); |
634 | sendProgress->hide(); | 632 | sendProgress->hide(); |
635 | delete sendProgress; | 633 | delete sendProgress; |
636 | sendProgress = 0; | 634 | sendProgress = 0; |
637 | mailmime_free( mimeMail ); | 635 | mailmime_free( mimeMail ); |
638 | } | 636 | } |
639 | } | 637 | } |
640 | 638 | ||
641 | int SMTPwrapper::sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which) | 639 | int SMTPwrapper::sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which) |
642 | { | 640 | { |
643 | size_t curTok = 0; | 641 | size_t curTok = 0; |
644 | mailimf_fields *fields = 0; | 642 | mailimf_fields *fields = 0; |
645 | mailimf_field*ffrom = 0; | 643 | mailimf_field*ffrom = 0; |
646 | clist *rcpts = 0; | 644 | clist *rcpts = 0; |
647 | char*from = 0; | 645 | char*from = 0; |
648 | int res = 0; | 646 | int res = 0; |
649 | 647 | ||
650 | encodedString * data = wrap->fetchRawBody(*which); | 648 | encodedString * data = wrap->fetchRawBody(*which); |
651 | if (!data) return 0; | 649 | if (!data) return 0; |
652 | int err = mailimf_fields_parse( data->Content(), data->Length(), &curTok, &fields ); | 650 | int err = mailimf_fields_parse( data->Content(), data->Length(), &curTok, &fields ); |
653 | if (err != MAILIMF_NO_ERROR) { | 651 | if (err != MAILIMF_NO_ERROR) { |
654 | delete data; | 652 | delete data; |
655 | delete wrap; | 653 | delete wrap; |
656 | return 0; | 654 | return 0; |
657 | } | 655 | } |
658 | 656 | ||
659 | rcpts = createRcptList( fields ); | 657 | rcpts = createRcptList( fields ); |
660 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); | 658 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); |
661 | from = getFrom(ffrom); | 659 | from = getFrom(ffrom); |
662 | 660 | ||
663 | if (rcpts && from) { | 661 | if (rcpts && from) { |
664 | res = smtpSend(from,rcpts,data->Content(),data->Length(),smtp ); | 662 | res = smtpSend(from,rcpts,data->Content(),data->Length(),smtp ); |
665 | } | 663 | } |
666 | if (fields) { | 664 | if (fields) { |
667 | mailimf_fields_free(fields); | 665 | mailimf_fields_free(fields); |
668 | fields = 0; | 666 | fields = 0; |
669 | } | 667 | } |
670 | if (data) { | 668 | if (data) { |
671 | delete data; | 669 | delete data; |
672 | } | 670 | } |
673 | if (from) { | 671 | if (from) { |
674 | free(from); | 672 | free(from); |
675 | } | 673 | } |
676 | if (rcpts) { | 674 | if (rcpts) { |
677 | smtp_address_list_free( rcpts ); | 675 | smtp_address_list_free( rcpts ); |
678 | } | 676 | } |
679 | return res; | 677 | return res; |
680 | } | 678 | } |
681 | 679 | ||
682 | /* this is a special fun */ | 680 | /* this is a special fun */ |
683 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) | 681 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) |
684 | { | 682 | { |
685 | bool returnValue = true; | 683 | bool returnValue = true; |
686 | 684 | ||
687 | if (!smtp) return false; | 685 | if (!smtp) return false; |
688 | 686 | ||
689 | QString localfolders = AbstractMail::defaultLocalfolder(); | 687 | QString localfolders = AbstractMail::defaultLocalfolder(); |
690 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | 688 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); |
691 | if (!wrap) { | 689 | if (!wrap) { |
692 | qDebug("memory error"); | 690 | qDebug("memory error"); |
693 | return false; | 691 | return false; |
694 | } | 692 | } |
695 | QList<RecMail> mailsToSend; | 693 | QList<RecMail> mailsToSend; |
696 | QList<RecMail> mailsToRemove; | 694 | QList<RecMail> mailsToRemove; |
697 | QString mbox("Outgoing"); | 695 | QString mbox("Outgoing"); |
698 | wrap->listMessages(mbox,mailsToSend); | 696 | wrap->listMessages(mbox,mailsToSend); |
699 | if (mailsToSend.count()==0) { | 697 | if (mailsToSend.count()==0) { |
700 | delete wrap; | 698 | delete wrap; |
701 | return false; | 699 | return false; |
702 | } | 700 | } |
703 | mailsToSend.setAutoDelete(false); | 701 | mailsToSend.setAutoDelete(false); |
704 | sendProgress = new progressMailSend(); | 702 | sendProgress = new progressMailSend(); |
705 | sendProgress->show(); | 703 | sendProgress->show(); |
706 | sendProgress->setMaxMails(mailsToSend.count()); | 704 | sendProgress->setMaxMails(mailsToSend.count()); |
707 | 705 | ||
708 | while (mailsToSend.count()>0) { | 706 | while (mailsToSend.count()>0) { |
709 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { | 707 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { |
710 | QMessageBox::critical(0,tr("Error sending mail"), | 708 | QMessageBox::critical(0,tr("Error sending mail"), |
711 | tr("Error sending queued mail - breaking")); | 709 | tr("Error sending queued mail - breaking")); |
712 | returnValue = false; | 710 | returnValue = false; |
713 | break; | 711 | break; |
714 | } | 712 | } |
715 | mailsToRemove.append(mailsToSend.at(0)); | 713 | mailsToRemove.append(mailsToSend.at(0)); |
716 | mailsToSend.removeFirst(); | 714 | mailsToSend.removeFirst(); |
717 | sendProgress->setCurrentMails(mailsToRemove.count()); | 715 | sendProgress->setCurrentMails(mailsToRemove.count()); |
718 | } | 716 | } |
719 | Config cfg( "mail" ); | 717 | Config cfg( "mail" ); |
720 | cfg.setGroup( "Status" ); | 718 | cfg.setGroup( "Status" ); |
721 | m_queuedMail = 0; | 719 | m_queuedMail = 0; |
722 | cfg.writeEntry( "outgoing", m_queuedMail ); | 720 | cfg.writeEntry( "outgoing", m_queuedMail ); |
723 | emit queuedMails( m_queuedMail ); | 721 | emit queuedMails( m_queuedMail ); |
724 | sendProgress->hide(); | 722 | sendProgress->hide(); |
725 | delete sendProgress; | 723 | delete sendProgress; |
726 | sendProgress = 0; | 724 | sendProgress = 0; |
727 | wrap->deleteMails(mbox,mailsToRemove); | 725 | wrap->deleteMails(mbox,mailsToRemove); |
728 | mailsToSend.setAutoDelete(true); | 726 | mailsToSend.setAutoDelete(true); |
729 | delete wrap; | 727 | delete wrap; |
730 | return returnValue; | 728 | return returnValue; |
731 | } | 729 | } |