summaryrefslogtreecommitdiffabout
path: root/kmicromail/libmailwrapper/generatemail.cpp
Unidiff
Diffstat (limited to 'kmicromail/libmailwrapper/generatemail.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libmailwrapper/generatemail.cpp6
1 files changed, 4 insertions, 2 deletions
diff --git a/kmicromail/libmailwrapper/generatemail.cpp b/kmicromail/libmailwrapper/generatemail.cpp
index 2d213fe..ddad21b 100644
--- a/kmicromail/libmailwrapper/generatemail.cpp
+++ b/kmicromail/libmailwrapper/generatemail.cpp
@@ -1,105 +1,107 @@
1#include "generatemail.h" 1#include "generatemail.h"
2#include "mailwrapper.h" 2#include "mailwrapper.h"
3 3
4#include <libetpan/libetpan.h> 4#include <libetpan/libetpan.h>
5 5
6//#include <qt.h> 6//#include <qt.h>
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9#include <qfileinfo.h> 9#include <qfileinfo.h>
10//Added by qt3to4:
11#include <Q3CString>
10 12
11using namespace Opie::Core; 13using namespace Opie::Core;
12const char* Generatemail::USER_AGENT="KOpieMail 33 1/3"; 14const char* Generatemail::USER_AGENT="KOpieMail 33 1/3";
13 15
14Generatemail::Generatemail() 16Generatemail::Generatemail()
15{ 17{
16 mCharset = "iso-8859-1"; 18 mCharset = "iso-8859-1";
17} 19}
18 20
19Generatemail::~Generatemail() 21Generatemail::~Generatemail()
20{ 22{
21} 23}
22 24
23void Generatemail::addRcpts( clist *list, mailimf_address_list *addr_list ) { 25void Generatemail::addRcpts( clist *list, mailimf_address_list *addr_list ) {
24 clistiter *it, *it2; 26 clistiter *it, *it2;
25 27
26 for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { 28 for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) {
27 mailimf_address *addr; 29 mailimf_address *addr;
28 addr = (mailimf_address *) it->data; 30 addr = (mailimf_address *) it->data;
29 31
30 if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { 32 if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) {
31 esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); 33 esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL );
32 } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { 34 } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) {
33 clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; 35 clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list;
34 for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { 36 for ( it2 = clist_begin( l ); it2; it2 = it2->next ) {
35 mailimf_mailbox *mbox; 37 mailimf_mailbox *mbox;
36 mbox = (mailimf_mailbox *) it2->data; 38 mbox = (mailimf_mailbox *) it2->data;
37 esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); 39 esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL );
38 } 40 }
39 } 41 }
40 } 42 }
41} 43}
42 44
43char *Generatemail::getFrom( mailimf_field *ffrom) { 45char *Generatemail::getFrom( mailimf_field *ffrom) {
44 char *from = NULL; 46 char *from = NULL;
45 if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) 47 if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM)
46 && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { 48 && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) {
47 clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; 49 clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list;
48 clistiter *it; 50 clistiter *it;
49 for ( it = clist_begin( cl ); it; it = it->next ) { 51 for ( it = clist_begin( cl ); it; it = it->next ) {
50 mailimf_mailbox *mb = (mailimf_mailbox *) it->data; 52 mailimf_mailbox *mb = (mailimf_mailbox *) it->data;
51 from = strdup( mb->mb_addr_spec ); 53 from = strdup( mb->mb_addr_spec );
52 } 54 }
53 } 55 }
54 56
55 return from; 57 return from;
56} 58}
57 59
58char *Generatemail::getFrom( mailmime *mail ) { 60char *Generatemail::getFrom( mailmime *mail ) {
59 /* no need to delete - its just a pointer to structure content */ 61 /* no need to delete - its just a pointer to structure content */
60 mailimf_field *ffrom = 0; 62 mailimf_field *ffrom = 0;
61 ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); 63 ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM );
62 return getFrom(ffrom); 64 return getFrom(ffrom);
63} 65}
64 66
65mailimf_field *Generatemail::getField( mailimf_fields *fields, int type ) { 67mailimf_field *Generatemail::getField( mailimf_fields *fields, int type ) {
66 mailimf_field *field; 68 mailimf_field *field;
67 clistiter *it; 69 clistiter *it;
68 70
69 it = clist_begin( fields->fld_list ); 71 it = clist_begin( fields->fld_list );
70 while ( it ) { 72 while ( it ) {
71 field = (mailimf_field *) it->data; 73 field = (mailimf_field *) it->data;
72 if ( field->fld_type == type ) { 74 if ( field->fld_type == type ) {
73 return field; 75 return field;
74 } 76 }
75 it = it->next; 77 it = it->next;
76 } 78 }
77 79
78 return NULL; 80 return NULL;
79} 81}
80 82
81mailimf_address_list *Generatemail::parseAddresses(const QString&addr ) { 83mailimf_address_list *Generatemail::parseAddresses(const QString&addr ) {
82 mailimf_address_list *addresses; 84 mailimf_address_list *addresses;
83 85
84 if ( addr.isEmpty() ) 86 if ( addr.isEmpty() )
85 return NULL; 87 return NULL;
86 88
87 addresses = mailimf_address_list_new_empty(); 89 addresses = mailimf_address_list_new_empty();
88 90
89 bool literal_open = false; 91 bool literal_open = false;
90 unsigned int startpos = 0; 92 unsigned int startpos = 0;
91 QStringList list; 93 QStringList list;
92 QString s; 94 QString s;
93 unsigned int i = 0; 95 unsigned int i = 0;
94 for (; i < addr.length();++i) { 96 for (; i < addr.length();++i) {
95 switch (addr[i]) { 97 switch (addr[i]) {
96 case '\"': 98 case '\"':
97 literal_open = !literal_open; 99 literal_open = !literal_open;
98 break; 100 break;
99 case ',': 101 case ',':
100 if (!literal_open) { 102 if (!literal_open) {
101 s = addr.mid(startpos,i-startpos); 103 s = addr.mid(startpos,i-startpos);
102 if (!s.isEmpty()) { 104 if (!s.isEmpty()) {
103 list.append(s); 105 list.append(s);
104 } 106 }
105 // !!!! this is a MUST BE! 107 // !!!! this is a MUST BE!
@@ -135,217 +137,217 @@ mailmime *Generatemail::buildFilePart(const QString&filename,const QString&mimet
135 int err; 137 int err;
136 int pos = filename.findRev( '/' ); 138 int pos = filename.findRev( '/' );
137 139
138 if (filename.length()>0) { 140 if (filename.length()>0) {
139 QString tmp = filename.right( filename.length() - ( pos + 1 ) ); 141 QString tmp = filename.right( filename.length() - ( pos + 1 ) );
140 name = strdup( tmp.latin1() ); // just filename 142 name = strdup( tmp.latin1() ); // just filename
141 file = strdup( filename.latin1() ); // full name with path 143 file = strdup( filename.latin1() ); // full name with path
142 } 144 }
143 145
144 int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; 146 int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT;
145 int mechanism = MAILMIME_MECHANISM_BASE64; 147 int mechanism = MAILMIME_MECHANISM_BASE64;
146 148
147 if ( mimetype.startsWith( "text/" ) ) { 149 if ( mimetype.startsWith( "text/" ) ) {
148 param = mailmime_parameter_new( strdup( "charset" ), 150 param = mailmime_parameter_new( strdup( "charset" ),
149 strdup( mCharset.latin1() ) ); 151 strdup( mCharset.latin1() ) );
150 mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; 152 mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE;
151 } 153 }
152 154
153 fields = mailmime_fields_new_filename( 155 fields = mailmime_fields_new_filename(
154 disptype, name, 156 disptype, name,
155 mechanism ); 157 mechanism );
156 content = mailmime_content_new_with_str( (char*)mimetype.latin1() ); 158 content = mailmime_content_new_with_str( (char*)mimetype.latin1() );
157 if (content!=0 && fields != 0) { 159 if (content!=0 && fields != 0) {
158 if (param) { 160 if (param) {
159 clist_append(content->ct_parameters,param); 161 clist_append(content->ct_parameters,param);
160 param = 0; 162 param = 0;
161 } 163 }
162 if (filename.length()>0) { 164 if (filename.length()>0) {
163 QFileInfo f(filename); 165 QFileInfo f(filename);
164 param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); 166 param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1()));
165 clist_append(content->ct_parameters,param); 167 clist_append(content->ct_parameters,param);
166 param = 0; 168 param = 0;
167 } 169 }
168 filePart = mailmime_new_empty( content, fields ); 170 filePart = mailmime_new_empty( content, fields );
169 } 171 }
170 if (filePart) { 172 if (filePart) {
171 if (filename.length()>0) { 173 if (filename.length()>0) {
172 err = mailmime_set_body_file( filePart, file ); 174 err = mailmime_set_body_file( filePart, file );
173 } else { 175 } else {
174 err = mailmime_set_body_text(filePart,strdup( TextContent.utf8().data()),TextContent.utf8().length()); 176 err = mailmime_set_body_text(filePart,strdup( TextContent.utf8().data()),TextContent.utf8().length());
175 //err = mailmime_set_body_text(filePart,strdup( TextContent.latin1()),TextContent.length()); 177 //err = mailmime_set_body_text(filePart,strdup( TextContent.latin1()),TextContent.length());
176 } 178 }
177 if (err != MAILIMF_NO_ERROR) { 179 if (err != MAILIMF_NO_ERROR) {
178 qDebug("Error setting body with file "); 180 qDebug("Error setting body with file ");
179 mailmime_free( filePart ); 181 mailmime_free( filePart );
180 filePart = 0; 182 filePart = 0;
181 } 183 }
182 } 184 }
183 185
184 if (!filePart) { 186 if (!filePart) {
185 if ( param != NULL ) { 187 if ( param != NULL ) {
186 mailmime_parameter_free( param ); 188 mailmime_parameter_free( param );
187 } 189 }
188 if (content) { 190 if (content) {
189 mailmime_content_free( content ); 191 mailmime_content_free( content );
190 } 192 }
191 if (fields) { 193 if (fields) {
192 mailmime_fields_free( fields ); 194 mailmime_fields_free( fields );
193 } else { 195 } else {
194 if (name) { 196 if (name) {
195 free( name ); 197 free( name );
196 } 198 }
197 if (file) { 199 if (file) {
198 free( file ); 200 free( file );
199 } 201 }
200 } 202 }
201 } 203 }
202 return filePart; // Success :) 204 return filePart; // Success :)
203 205
204} 206}
205 207
206void Generatemail::addFileParts( mailmime *message,const QList<Attachment>&files ) { 208void Generatemail::addFileParts( mailmime *message,const QList<Attachment>&files ) {
207 const Attachment *it; 209 const Attachment *it;
208 unsigned int count = files.count(); 210 unsigned int count = files.count();
209 for ( unsigned int i = 0; i < count; ++i ) { 211 for ( unsigned int i = 0; i < count; ++i ) {
210 mailmime *filePart; 212 mailmime *filePart;
211 int err; 213 int err;
212 it = ((QList<Attachment>)files).at(i); 214 it = ((QList<Attachment>)files).at(i);
213 215
214 filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); 216 filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" );
215 if ( filePart == NULL ) { 217 if ( filePart == NULL ) {
216 continue; 218 continue;
217 } 219 }
218 err = mailmime_smart_add_part( message, filePart ); 220 err = mailmime_smart_add_part( message, filePart );
219 if ( err != MAILIMF_NO_ERROR ) { 221 if ( err != MAILIMF_NO_ERROR ) {
220 mailmime_free( filePart ); 222 mailmime_free( filePart );
221 } 223 }
222 } 224 }
223} 225}
224 226
225mailmime *Generatemail::buildTxtPart(const QString&str ) { 227mailmime *Generatemail::buildTxtPart(const QString&str ) {
226 mailmime *txtPart; 228 mailmime *txtPart;
227 mailmime_fields *fields; 229 mailmime_fields *fields;
228 mailmime_content *content; 230 mailmime_content *content;
229 mailmime_parameter *param; 231 mailmime_parameter *param;
230 int err; 232 int err;
231 QCString __str; 233 Q3CString __str;
232 //qDebug(" Generatemail::buildTxtPart %s", str.latin1()); 234 //qDebug(" Generatemail::buildTxtPart %s", str.latin1());
233 param = mailmime_parameter_new( strdup( "charset" ), 235 param = mailmime_parameter_new( strdup( "charset" ),
234 strdup( mCharset.latin1() ) ); 236 strdup( mCharset.latin1() ) );
235 if ( param == NULL ) 237 if ( param == NULL )
236 goto err_free; 238 goto err_free;
237 239
238 content = mailmime_content_new_with_str( "text/plain" ); 240 content = mailmime_content_new_with_str( "text/plain" );
239 if ( content == NULL ) 241 if ( content == NULL )
240 goto err_free_param; 242 goto err_free_param;
241 243
242 err = clist_append( content->ct_parameters, param ); 244 err = clist_append( content->ct_parameters, param );
243 if ( err != MAILIMF_NO_ERROR ) 245 if ( err != MAILIMF_NO_ERROR )
244 goto err_free_content; 246 goto err_free_content;
245 247
246 fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT); 248 fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT);
247 if ( fields == NULL ) 249 if ( fields == NULL )
248 goto err_free_content; 250 goto err_free_content;
249 251
250 txtPart = mailmime_new_empty( content, fields ); 252 txtPart = mailmime_new_empty( content, fields );
251 if ( txtPart == NULL ) 253 if ( txtPart == NULL )
252 goto err_free_fields; 254 goto err_free_fields;
253 { 255 {
254 //__str = str.utf8(); 256 //__str = str.utf8();
255 __str = QCString (str.latin1()); 257 __str = Q3CString (str.latin1());
256 err = mailmime_set_body_text( txtPart, strdup(__str.data()), __str.length() ); 258 err = mailmime_set_body_text( txtPart, strdup(__str.data()), __str.length() );
257 } 259 }
258 if ( err != MAILIMF_NO_ERROR ) 260 if ( err != MAILIMF_NO_ERROR )
259 goto err_free_txtPart; 261 goto err_free_txtPart;
260 262
261 return txtPart; // Success :) 263 return txtPart; // Success :)
262 264
263err_free_txtPart: 265err_free_txtPart:
264 mailmime_free( txtPart ); 266 mailmime_free( txtPart );
265err_free_fields: 267err_free_fields:
266 mailmime_fields_free( fields ); 268 mailmime_fields_free( fields );
267err_free_content: 269err_free_content:
268 mailmime_content_free( content ); 270 mailmime_content_free( content );
269err_free_param: 271err_free_param:
270 mailmime_parameter_free( param ); 272 mailmime_parameter_free( param );
271err_free: 273err_free:
272 ; 274 ;
273 275
274 return NULL; // Error :( 276 return NULL; // Error :(
275} 277}
276 278
277mailimf_mailbox *Generatemail::newMailbox(const QString&name, const QString&mail ) { 279mailimf_mailbox *Generatemail::newMailbox(const QString&name, const QString&mail ) {
278 return mailimf_mailbox_new( strdup( name.latin1() ), 280 return mailimf_mailbox_new( strdup( name.latin1() ),
279 strdup( mail.latin1() ) ); 281 strdup( mail.latin1() ) );
280} 282}
281 283
282mailimf_fields *Generatemail::createImfFields(const Opie::Core::OSmartPointer<Mail>&mail ) 284mailimf_fields *Generatemail::createImfFields(const Opie::Core::OSmartPointer<Mail>&mail )
283{ 285{
284 mailimf_fields *fields = NULL; 286 mailimf_fields *fields = NULL;
285 mailimf_field *xmailer = NULL; 287 mailimf_field *xmailer = NULL;
286 mailimf_mailbox *sender=0,*fromBox=0; 288 mailimf_mailbox *sender=0,*fromBox=0;
287 mailimf_mailbox_list *from=0; 289 mailimf_mailbox_list *from=0;
288 mailimf_address_list *to=0, *cc=0, *bcc=0, *reply=0; 290 mailimf_address_list *to=0, *cc=0, *bcc=0, *reply=0;
289 clist*in_reply_to = 0; 291 clist*in_reply_to = 0;
290 char *subject = strdup( mail->getSubject().latin1() ); 292 char *subject = strdup( mail->getSubject().latin1() );
291 int err; 293 int err;
292 int res = 1; 294 int res = 1;
293 295
294 sender = newMailbox( mail->getName(), mail->getMail() ); 296 sender = newMailbox( mail->getName(), mail->getMail() );
295 if ( sender == NULL ) { 297 if ( sender == NULL ) {
296 res = 0; 298 res = 0;
297 } 299 }
298 300
299 if (res) { 301 if (res) {
300 fromBox = newMailbox( mail->getName(), mail->getMail() ); 302 fromBox = newMailbox( mail->getName(), mail->getMail() );
301 } 303 }
302 if ( fromBox == NULL ) { 304 if ( fromBox == NULL ) {
303 res = 0; 305 res = 0;
304 } 306 }
305 307
306 if (res) { 308 if (res) {
307 from = mailimf_mailbox_list_new_empty(); 309 from = mailimf_mailbox_list_new_empty();
308 } 310 }
309 if ( from == NULL ) { 311 if ( from == NULL ) {
310 res = 0; 312 res = 0;
311 } 313 }
312 314
313 if (res && from) { 315 if (res && from) {
314 err = mailimf_mailbox_list_add( from, fromBox ); 316 err = mailimf_mailbox_list_add( from, fromBox );
315 if ( err != MAILIMF_NO_ERROR ) { 317 if ( err != MAILIMF_NO_ERROR ) {
316 res = 0; 318 res = 0;
317 } 319 }
318 } 320 }
319 321
320 if (res) to = parseAddresses( mail->getTo() ); 322 if (res) to = parseAddresses( mail->getTo() );
321 if (res) cc = parseAddresses( mail->getCC() ); 323 if (res) cc = parseAddresses( mail->getCC() );
322 if (res) bcc = parseAddresses( mail->getBCC() ); 324 if (res) bcc = parseAddresses( mail->getBCC() );
323 if (res) reply = parseAddresses( mail->getReply() ); 325 if (res) reply = parseAddresses( mail->getReply() );
324 326
325 if (res && mail->Inreply().count()>0) { 327 if (res && mail->Inreply().count()>0) {
326 in_reply_to = clist_new(); 328 in_reply_to = clist_new();
327 char*c_reply; 329 char*c_reply;
328 unsigned int nsize = 0; 330 unsigned int nsize = 0;
329 for (QStringList::ConstIterator it=mail->Inreply().begin(); 331 for (QStringList::ConstIterator it=mail->Inreply().begin();
330 it != mail->Inreply().end();++it) { 332 it != mail->Inreply().end();++it) {
331 if ((*it).isEmpty()) 333 if ((*it).isEmpty())
332 continue; 334 continue;
333 QString h((*it)); 335 QString h((*it));
334 while (h.length()>0 && h[0]=='<') { 336 while (h.length()>0 && h[0]=='<') {
335 h.remove(0,1); 337 h.remove(0,1);
336 } 338 }
337 while (h.length()>0 && h[h.length()-1]=='>') { 339 while (h.length()>0 && h[h.length()-1]=='>') {
338 h.remove(h.length()-1,1); 340 h.remove(h.length()-1,1);
339 } 341 }
340 if (h.isEmpty()) continue; 342 if (h.isEmpty()) continue;
341 nsize = strlen(h.latin1()); 343 nsize = strlen(h.latin1());
342 /* yes! must be malloc! */ 344 /* yes! must be malloc! */
343 c_reply = (char*)malloc( (nsize+1)*sizeof(char)); 345 c_reply = (char*)malloc( (nsize+1)*sizeof(char));
344 memset(c_reply,0,nsize+1); 346 memset(c_reply,0,nsize+1);
345 memcpy(c_reply,h.latin1(),nsize); 347 memcpy(c_reply,h.latin1(),nsize);
346 clist_append(in_reply_to,c_reply); 348 clist_append(in_reply_to,c_reply);
347 } 349 }
348 } 350 }
349 351
350 if (res) { 352 if (res) {
351 fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, 353 fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc,