Commit afb78dee afb78dee0f7282e456a4fd6238950b84cbd8bfc9 by Sergey Poznyakoff

(restore_sender,restore_date): New functions.

(mbox_append_message0): Use above functions to create sender/date
values if these are not supplied.
1 parent e5747d36
...@@ -1330,6 +1330,79 @@ mbox_append_message (mailbox_t mailbox, message_t msg) ...@@ -1330,6 +1330,79 @@ mbox_append_message (mailbox_t mailbox, message_t msg)
1330 return 0; 1330 return 0;
1331 } 1331 }
1332 1332
1333 int
1334 restore_sender (message_t msg, mbox_data_t mud)
1335 {
1336 header_t hdr;
1337 char *from = NULL;
1338 int rc = 0;
1339
1340 if (message_get_header (msg, &hdr) == 0)
1341 header_aget_value (hdr, MU_HEADER_FROM, &from);
1342
1343 if (from)
1344 {
1345 int status;
1346 address_t addr;
1347
1348 status = address_create (&addr, from);
1349 free (from);
1350 from = NULL;
1351 if (status == 0)
1352 address_aget_email (addr, 1, &from);
1353 address_destroy (&addr);
1354 }
1355
1356 if (!from)
1357 {
1358 from = strdup (PACKAGE);
1359 if (!from)
1360 return ENOMEM;
1361 }
1362
1363 mud->sender = strdup (from);
1364 if (!mud->sender)
1365 rc = ENOMEM;
1366 free (from);
1367 return rc;
1368 }
1369
1370 int
1371 restore_date (message_t msg, mbox_data_t mud)
1372 {
1373 header_t hdr;
1374 char *date = NULL;
1375 time_t t;
1376 int rc = 0;
1377
1378 if (message_get_header (msg, &hdr) == 0)
1379 header_aget_value (hdr, MU_HEADER_DATE, &date);
1380
1381 if (date && mu_parse_date (date, &t, NULL))
1382 {
1383 char datebuf[25];
1384
1385 /* FIXME: 1. Preserve TZ info */
1386 /* FIXME: 2. Do not depend on LC */
1387 strftime (datebuf, sizeof datebuf, "%a %b %d %H:%M:%S %Y",
1388 localtime (&t));
1389 free (date);
1390 date = strdup (datebuf);
1391 }
1392 else
1393 {
1394 time (&t);
1395 free (date);
1396 date = strdup (ctime (&t));
1397 }
1398
1399 mud->date = strdup (date);
1400 if (!mud->date)
1401 rc = ENOMEM;
1402 free (date);
1403 return rc;
1404 }
1405
1333 /* FIXME: We need to escape body line that begins with "From ", this 1406 /* FIXME: We need to escape body line that begins with "From ", this
1334 will required to read the body by line instead of by chuncks hurting 1407 will required to read the body by line instead of by chuncks hurting
1335 perfomance big time when expunging. But should not this be the 1408 perfomance big time when expunging. But should not this be the
...@@ -1369,15 +1442,27 @@ mbox_append_message0 (mailbox_t mailbox, message_t msg, off_t *psize, ...@@ -1369,15 +1442,27 @@ mbox_append_message0 (mailbox_t mailbox, message_t msg, off_t *psize,
1369 envelope_t envelope = NULL; 1442 envelope_t envelope = NULL;
1370 message_get_envelope (msg, &envelope); 1443 message_get_envelope (msg, &envelope);
1371 status = envelope_sender (envelope, mud->sender, 128, &len); 1444 status = envelope_sender (envelope, mud->sender, 128, &len);
1372 if (status != 0) 1445 switch (status) {
1373 { 1446 case 0:
1374 if (status != EAGAIN) 1447 break;
1448
1449 case EAGAIN:
1450 return status;
1451
1452 case MU_ERR_NOENT: /* Envelope headers not found: try to guess */
1453 free (mud->sender);
1454 status = restore_sender (msg, mud);
1455 if (status == 0)
1375 { 1456 {
1457 len = strlen (mud->sender);
1458 break;
1459 }
1460
1461 default:
1376 free (mud->sender); 1462 free (mud->sender);
1377 free (mud->date); 1463 free (mud->date);
1378 mud->date = mud->sender = NULL; 1464 mud->date = mud->sender = NULL;
1379 mud->state = MBOX_NO_STATE; 1465 mud->state = MBOX_NO_STATE;
1380 }
1381 return status; 1466 return status;
1382 } 1467 }
1383 1468
...@@ -1397,15 +1482,27 @@ mbox_append_message0 (mailbox_t mailbox, message_t msg, off_t *psize, ...@@ -1397,15 +1482,27 @@ mbox_append_message0 (mailbox_t mailbox, message_t msg, off_t *psize,
1397 char buffer[1024]; 1482 char buffer[1024];
1398 message_get_envelope (msg, &envelope); 1483 message_get_envelope (msg, &envelope);
1399 status = envelope_date (envelope, mud->date, 128, &len); 1484 status = envelope_date (envelope, mud->date, 128, &len);
1400 if (status != 0) 1485 switch (status) {
1401 { 1486 case 0:
1402 if (status != EAGAIN) 1487 break;
1488
1489 case EAGAIN:
1490 return status;
1491
1492 case MU_ERR_NOENT: /* Envelope headers not found: try to guess */
1493 free (mud->date);
1494 status = restore_date (msg, mud);
1495 if (status == 0)
1403 { 1496 {
1497 len = strlen (mud->date);
1498 break;
1499 }
1500
1501 default:
1404 free (mud->sender); 1502 free (mud->sender);
1405 free (mud->date); 1503 free (mud->date);
1406 mud->date = mud->sender = NULL; 1504 mud->date = mud->sender = NULL;
1407 mud->state = MBOX_NO_STATE; 1505 mud->state = MBOX_NO_STATE;
1408 }
1409 return status; 1506 return status;
1410 } 1507 }
1411 1508
......