comparison src/readpst.c @ 257:c947b8812120

rfc2047 and rfc2231 encoding for non-ascii headers and attachment filenames
author Carl Byington <carl@five-ten-sg.com>
date Fri, 24 Dec 2010 19:26:05 -0800
parents ab87f9070ed2
children 8ad8fd1c5451
comparison
equal deleted inserted replaced
256:a863de65e5b8 257:c947b8812120
811 DEBUG_ENT("mk_separate_dir"); 811 DEBUG_ENT("mk_separate_dir");
812 do { 812 do {
813 if (y == 0) 813 if (y == 0)
814 snprintf(dir_name, dirsize, "%s", dir); 814 snprintf(dir_name, dirsize, "%s", dir);
815 else 815 else
816 snprintf(dir_name, dirsize, "%s" SEP_MAIL_FILE_TEMPLATE, dir, y, ""); // enough for 9 digits allocated above 816 snprintf(dir_name, dirsize, "%s" SEP_MAIL_FILE_TEMPLATE, dir, y, ""); // enough for 9 digits allocated above
817 817
818 check_filename(dir_name); 818 check_filename(dir_name);
819 DEBUG_INFO(("about to try creating %s\n", dir_name)); 819 DEBUG_INFO(("about to try creating %s\n", dir_name));
820 if (D_MKDIR(dir_name)) { 820 if (D_MKDIR(dir_name)) {
821 if (errno != EEXIST) { // if there is an error, and it doesn't already exist 821 if (errno != EEXIST) { // if there is an error, and it doesn't already exist
1048 } else { 1048 } else {
1049 fprintf(f_output, "Content-Type: %s\n", attach->mimetype.str); 1049 fprintf(f_output, "Content-Type: %s\n", attach->mimetype.str);
1050 } 1050 }
1051 fprintf(f_output, "Content-Transfer-Encoding: base64\n"); 1051 fprintf(f_output, "Content-Transfer-Encoding: base64\n");
1052 1052
1053 // If there is a long filename (filename2) use that, otherwise 1053 if (attach->filename2.str) {
1054 // use the 8.3 filename (filename1) 1054 // use the long filename, converted to proper encoding if needed.
1055 attach_filename = (attach->filename2.str) ? attach->filename2.str : attach->filename1.str; 1055 // it is already utf8
1056 if (!attach_filename) { 1056 pst_rfc2231(&attach->filename2);
1057 fprintf(f_output, "Content-Disposition: attachment; \n filename*=%s\n\n", attach->filename2.str);
1058 }
1059 else if (attach->filename1.str) {
1060 // short filename never needs encoding
1061 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", attach->filename1.str);
1062 }
1063 else {
1064 // no filename is inline
1057 fprintf(f_output, "Content-Disposition: inline\n\n"); 1065 fprintf(f_output, "Content-Disposition: inline\n\n");
1058 } else {
1059 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", attach_filename);
1060 } 1066 }
1061 1067
1062 (void)pst_attach_to_file_base64(pst, attach, f_output); 1068 (void)pst_attach_to_file_base64(pst, attach, f_output);
1063 fprintf(f_output, "\n\n"); 1069 fprintf(f_output, "\n\n");
1064 DEBUG_RET(); 1070 DEBUG_RET();
1152 int test_base64(char *body) 1158 int test_base64(char *body)
1153 { 1159 {
1154 int b64 = 0; 1160 int b64 = 0;
1155 uint8_t *b = (uint8_t *)body; 1161 uint8_t *b = (uint8_t *)body;
1156 DEBUG_ENT("test_base64"); 1162 DEBUG_ENT("test_base64");
1157 while (*b != 0) { 1163 while (*b) {
1158 if ((*b < 32) && (*b != 9) && (*b != 10)) { 1164 if ((*b < 32) && (*b != 9) && (*b != 10)) {
1159 DEBUG_INFO(("found base64 byte %d\n", (int)*b)); 1165 DEBUG_INFO(("found base64 byte %d\n", (int)*b));
1160 DEBUG_HEXDUMPC(body, strlen(body), 0x10); 1166 DEBUG_HEXDUMPC(body, strlen(body), 0x10);
1161 b64 = 1; 1167 b64 = 1;
1162 break; 1168 break;
1451 int len = strlen(headers); 1457 int len = strlen(headers);
1452 if (len > 0) { 1458 if (len > 0) {
1453 fprintf(f_output, "%s", headers); 1459 fprintf(f_output, "%s", headers);
1454 // make sure the headers end with a \n 1460 // make sure the headers end with a \n
1455 if (headers[len-1] != '\n') fprintf(f_output, "\n"); 1461 if (headers[len-1] != '\n') fprintf(f_output, "\n");
1462 //char *h = headers;
1463 //while (*h) {
1464 // char *e = strchr(h, '\n');
1465 // int d = 1; // normally e points to trailing \n
1466 // if (!e) {
1467 // e = h + strlen(h); // e points to trailing null
1468 // d = 0;
1469 // }
1470 // // we could do rfc2047 encoding here if needed
1471 // fprintf(f_output, "%.*s\n", (int)(e-h), h);
1472 // h = e + d;
1473 //}
1456 } 1474 }
1457 } 1475 }
1458 1476
1459 // create required header fields that are not already written 1477 // create required header fields that are not already written
1460 1478
1461 if (!has_from) { 1479 if (!has_from) {
1462 if (item->email->outlook_sender_name.str){ 1480 if (item->email->outlook_sender_name.str){
1463 fprintf(f_output, "From: \"%s\" <%s>\n", item->email->outlook_sender_name.str, sender); 1481 pst_rfc2047(item, &item->email->outlook_sender_name, 1);
1482 fprintf(f_output, "From: %s <%s>\n", item->email->outlook_sender_name.str, sender);
1464 } else { 1483 } else {
1465 fprintf(f_output, "From: <%s>\n", sender); 1484 fprintf(f_output, "From: <%s>\n", sender);
1466 } 1485 }
1467 } 1486 }
1468 1487
1469 if (!has_subject) { 1488 if (!has_subject) {
1470 if (item->subject.str) { 1489 if (item->subject.str) {
1490 pst_rfc2047(item, &item->subject, 0);
1471 fprintf(f_output, "Subject: %s\n", item->subject.str); 1491 fprintf(f_output, "Subject: %s\n", item->subject.str);
1472 } else { 1492 } else {
1473 fprintf(f_output, "Subject: \n"); 1493 fprintf(f_output, "Subject: \n");
1474 } 1494 }
1475 } 1495 }
1476 1496
1477 if (!has_to && item->email->sentto_address.str) { 1497 if (!has_to && item->email->sentto_address.str) {
1478 pst_convert_utf8(item, &item->email->sentto_address); 1498 pst_rfc2047(item, &item->email->sentto_address, 0);
1479 fprintf(f_output, "To: %s\n", item->email->sentto_address.str); 1499 fprintf(f_output, "To: %s\n", item->email->sentto_address.str);
1480 } 1500 }
1481 1501
1482 if (!has_cc && item->email->cc_address.str) { 1502 if (!has_cc && item->email->cc_address.str) {
1483 pst_convert_utf8(item, &item->email->cc_address); 1503 pst_rfc2047(item, &item->email->cc_address, 0);
1484 fprintf(f_output, "Cc: %s\n", item->email->cc_address.str); 1504 fprintf(f_output, "Cc: %s\n", item->email->cc_address.str);
1485 } 1505 }
1486 1506
1487 if (!has_date && item->email->sent_date) { 1507 if (!has_date && item->email->sent_date) {
1488 char c_time[C_TIME_SIZE]; 1508 char c_time[C_TIME_SIZE];