1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
|
/* -*-mode:c; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
*
* Copyright (C) 1997-2016 Stuart Parmenter and others,
* See the file AUTHORS for a list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#define _DEFAULT_SOURCE 1
#define _POSIX_C_SOURCE 199309L
#include "send.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "libbalsa.h"
#include "libbalsa_private.h"
#include "server.h"
#include "misc.h"
#include "missing.h"
#include "information.h"
#include "net-client-smtp.h"
#include "gmime-filter-header.h"
#include "smtp-server.h"
#include "identity.h"
#include "libbalsa-progress.h"
#include "libbalsa-gpgme.h"
#include <glib/gi18n.h>
#ifdef G_LOG_DOMAIN
# undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "send"
typedef struct _MessageQueueItem MessageQueueItem;
typedef struct _SendMessageInfo SendMessageInfo;
struct _MessageQueueItem {
SendMessageInfo *smsg_info;
LibBalsaMessage *orig;
GMimeStream *stream;
NetClientSmtpMessage *smtp_msg;
};
struct _SendMessageInfo {
LibBalsaSmtpServer *smtp_server;
LibBalsaMailbox *outbox;
NetClientSmtp *session;
LibBalsaFccboxFinder finder;
GList *items; /* of MessageQueueItem */
gboolean no_dialog;
gchar *progress_id;
gint64 total_size;
gint64 total_sent;
gint last_report;
guint msg_count;
guint curr_msg;
};
typedef struct _SendQueueInfo SendQueueInfo;
struct _SendQueueInfo {
LibBalsaMailbox *outbox;
LibBalsaFccboxFinder finder;
GtkWindow *parent;
};
static GMutex send_messages_lock;
static gint sending_threads = 0; /* how many sending threads are active, access via g_atomic_* */
static GSourceFunc auto_send_cb = NULL;
static gboolean send_mail_auto = FALSE;
static guint send_mail_time = 0U;
static guint send_mail_timer_id = 0U;
static gint retrigger_send = 0; /* # of messages added to outbox while the smtp server was locked, access via g_atomic_* */
static ProgressDialog send_progress_dialog;
/* end of state variables section */
/* Stop the the auto-send timer, and start it if start and send_mail_auto are TRUE, and a callback is defined. In order to catch
* weird cases (e.g. user unflags or undeletes a message in the outbox) we ignore if the outbox is actually empty or does not
* contain any ready-to-send messages, and start the timer anyway. The overhead of the callback just doing nothing should be
* insignificant. */
static void
update_send_timer(gboolean start)
{
if (send_mail_timer_id != 0U) {
g_source_remove(send_mail_timer_id);
send_mail_timer_id = 0U;
}
if (start && send_mail_auto && (auto_send_cb != NULL)) {
send_mail_timer_id = g_timeout_add_seconds(send_mail_time, auto_send_cb, NULL);
}
}
void
libbalsa_auto_send_init(GSourceFunc auto_send_handler)
{
auto_send_cb = auto_send_handler;
update_send_timer(FALSE);
}
void
libbalsa_auto_send_config(gboolean enable,
guint timeout_minutes)
{
send_mail_auto = enable;
send_mail_time = timeout_minutes * 60U;
update_send_timer(enable);
}
gboolean
libbalsa_is_sending_mail(void)
{
return g_atomic_int_get(&sending_threads) > 0;
}
static MessageQueueItem *
msg_queue_item_new(SendMessageInfo *smi)
{
MessageQueueItem *mqi;
mqi = g_new0(MessageQueueItem, 1U);
mqi->smsg_info = smi;
return mqi;
}
static void
msg_queue_item_destroy(MessageQueueItem *mqi)
{
if (mqi->smtp_msg != NULL) {
net_client_smtp_msg_free(mqi->smtp_msg);
}
if (mqi->stream != NULL) {
g_object_unref(mqi->stream);
}
if (mqi->orig != NULL) {
g_object_unref(mqi->orig);
}
g_free(mqi);
}
static SendMessageInfo *
send_message_info_new(LibBalsaSmtpServer *smtp_server,
LibBalsaMailbox *outbox,
LibBalsaFccboxFinder finder,
NetClientSmtp *session)
{
SendMessageInfo *smi;
smi = g_new0(SendMessageInfo, 1);
smi->session = session;
smi->outbox = g_object_ref(outbox);
smi->finder = finder;
smi->smtp_server = g_object_ref(smtp_server);
smi->progress_id = g_strdup_printf(_("SMTP server %s"), libbalsa_smtp_server_get_name(smtp_server));
return smi;
}
static void
send_message_info_destroy(SendMessageInfo *smi)
{
if (smi->outbox != NULL) {
g_object_unref(smi->outbox);
}
if (smi->session != NULL) {
g_object_unref(smi->session);
}
if (smi->items != NULL) {
g_list_free_full(smi->items, (GDestroyNotify) msg_queue_item_destroy);
}
if (smi->progress_id != NULL) {
g_free(smi->progress_id);
}
g_object_unref(smi->smtp_server);
g_free(smi);
}
static LibBalsaMsgCreateResult libbalsa_create_rfc2440_buffer(LibBalsaMessage *message,
GMimePart *mime_part,
GtkWindow *parent,
GError **error);
static LibBalsaMsgCreateResult do_multipart_crypto(LibBalsaMessage *message,
GMimeObject **mime_root,
GtkWindow *parent,
GError **error);
static GMimePart *lb_create_pubkey_part(LibBalsaMessage *message,
GtkWindow *parent,
GError **error);
static gpointer balsa_send_message_real(SendMessageInfo *info);
static LibBalsaMsgCreateResult create_mime_message(LibBalsaMessage *message,
gboolean flow,
gboolean postponing,
GMimeMessage **return_message,
GError **error);
static LibBalsaMsgCreateResult libbalsa_create_msg(LibBalsaMessage *message,
gboolean flow,
GError **error);
static LibBalsaMsgCreateResult libbalsa_fill_msg_queue_item_from_queu(LibBalsaMessage *message,
MessageQueueItem *mqi);
static void
lbs_set_content(GMimePart *mime_part,
gchar *content)
{
GMimeStream *stream;
GMimeDataWrapper *wrapper;
stream = g_mime_stream_mem_new();
g_mime_stream_write(stream, content, strlen(content));
wrapper =
g_mime_data_wrapper_new_with_stream(stream,
GMIME_CONTENT_ENCODING_DEFAULT);
g_object_unref(stream);
g_mime_part_set_content(mime_part, wrapper);
g_object_unref(wrapper);
}
static GMimeObject *
add_mime_body_plain(LibBalsaMessageBody *body,
gboolean flow,
gboolean postpone,
guint use_gpg_mode,
LibBalsaMsgCreateResult *crypt_res,
GError **error)
{
GMimePart *mime_part;
const gchar *charset;
GtkWindow *parent = g_object_get_data(G_OBJECT(body->message), "parent-window");
g_return_val_if_fail(body, NULL);
charset = body->charset;
if (body->content_type != NULL) {
/* Use the supplied mime type */
gchar *type, *subtype;
/* FIXME: test sending with different mime types */
g_debug("path active");
type = g_strdup (body->content_type);
if ((subtype = strchr (type, '/')) != NULL) {
*subtype++ = 0;
mime_part = g_mime_part_new_with_type(type, subtype);
} else {
mime_part = g_mime_part_new_with_type("text", "plain");
}
g_free (type);
} else {
mime_part = g_mime_part_new_with_type("text", "plain");
}
g_mime_object_set_disposition(GMIME_OBJECT(mime_part), GMIME_DISPOSITION_INLINE);
g_mime_part_set_content_encoding(mime_part, GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE);
g_mime_object_set_content_type_parameter(GMIME_OBJECT(mime_part), "charset",
charset != NULL ? charset : "us-ascii");
if (flow) {
g_mime_object_set_content_type_parameter(GMIME_OBJECT(mime_part),
"DelSp", "Yes");
g_mime_object_set_content_type_parameter(GMIME_OBJECT(mime_part),
"Format", "Flowed");
}
if ((charset != NULL) &&
(g_ascii_strcasecmp(charset, "UTF-8") != 0) &&
(g_ascii_strcasecmp(charset, "UTF8") != 0)) {
GMimeStream *stream, *filter_stream;
GMimeFilter *filter;
GMimeDataWrapper *wrapper;
stream = g_mime_stream_mem_new();
filter_stream = g_mime_stream_filter_new(stream);
filter = g_mime_filter_charset_new("UTF-8", charset);
g_mime_stream_filter_add(GMIME_STREAM_FILTER(filter_stream), filter);
g_object_unref(filter);
g_mime_stream_write(filter_stream, body->buffer, strlen(body->buffer));
g_object_unref(filter_stream);
wrapper =
g_mime_data_wrapper_new_with_stream(stream,
GMIME_CONTENT_ENCODING_DEFAULT);
g_object_unref(stream);
g_mime_part_set_content(mime_part, wrapper);
g_object_unref(wrapper);
} else {
lbs_set_content(mime_part, body->buffer);
}
/* rfc 2440 sign/encrypt if requested */
if (use_gpg_mode != 0) {
*crypt_res =
libbalsa_create_rfc2440_buffer(body->message,
GMIME_PART(mime_part),
parent, error);
if (*crypt_res != LIBBALSA_MESSAGE_CREATE_OK) {
g_object_unref(mime_part);
return NULL;
}
}
/* if requested, add a text/html version in a multipart/alternative */
if (body->html_buffer && !postpone) {
GMimeMultipart *mpa = g_mime_multipart_new_with_subtype("alternative");
g_mime_multipart_add(mpa, GMIME_OBJECT(mime_part));
g_object_unref(mime_part);
mime_part = g_mime_part_new_with_type("text", "html");
g_mime_multipart_add(mpa, GMIME_OBJECT(mime_part));
g_object_unref(mime_part);
g_mime_object_set_disposition(GMIME_OBJECT(mime_part), GMIME_DISPOSITION_INLINE);
g_mime_part_set_content_encoding(mime_part, GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE);
g_mime_object_set_content_type_parameter(GMIME_OBJECT(mime_part),
"charset", "UTF-8");
lbs_set_content(mime_part, body->html_buffer);
if ((use_gpg_mode != 0) &&
((use_gpg_mode & LIBBALSA_PROTECT_MODE) != LIBBALSA_PROTECT_SIGN)) {
*crypt_res =
libbalsa_create_rfc2440_buffer(body->message,
GMIME_PART(mime_part),
parent, error);
if (*crypt_res != LIBBALSA_MESSAGE_CREATE_OK) {
g_object_unref(mpa);
return NULL;
}
}
return GMIME_OBJECT(mpa);
} else {
return GMIME_OBJECT(mime_part);
}
}
#if 0
/* you never know when you will need this one... */
static void
dump_queue(const char *msg)
{
MessageQueueItem *mqi = message_queue;
g_debug("dumping message queue at %s:", msg);
while (mqi) {
g_debug("item: %p", mqi);
mqi = mqi->next_message;
}
}
#endif
/* libbalsa_message_queue:
places given message in the outbox.
*/
static void libbalsa_set_message_id(GMimeMessage *mime_message);
static LibBalsaMsgCreateResult
lbs_message_queue_real(LibBalsaMessage *message,
LibBalsaMailbox *outbox,
LibBalsaMailbox *fccbox,
LibBalsaSmtpServer *smtp_server,
gboolean flow,
GError **error)
{
LibBalsaMsgCreateResult result;
GMimeMessage *mime_msg;
guint big_message;
gboolean rc;
g_assert(error != NULL);
g_return_val_if_fail(message, LIBBALSA_MESSAGE_CREATE_ERROR);
if ((result = libbalsa_create_msg(message, flow, error)) !=
LIBBALSA_MESSAGE_CREATE_OK) {
return result;
}
mime_msg = libbalsa_message_get_mime_message(message);
if (fccbox != NULL) {
g_mime_object_set_header(GMIME_OBJECT(mime_msg), "X-Balsa-Fcc",
libbalsa_mailbox_get_url(fccbox), NULL);
}
g_mime_object_set_header(GMIME_OBJECT(mime_msg), "X-Balsa-DSN",
libbalsa_message_get_request_dsn(message) ? "1" : "0", NULL);
g_mime_object_set_header(GMIME_OBJECT(mime_msg), "X-Balsa-SmtpServer",
libbalsa_smtp_server_get_name(smtp_server), NULL);
big_message = libbalsa_smtp_server_get_big_message(smtp_server);
if (big_message > 0) {
GMimeMessage **mime_msgs;
size_t nparts;
guint i;
mime_msgs =
g_mime_message_partial_split_message(mime_msg, big_message,
&nparts);
g_object_ref(mime_msg);
rc = TRUE;
for (i = 0; i < nparts; ++i) {
if (nparts > 1) {
/* RFC 2046, 5.2.2: "...it is specified that entities of
* type "message/partial" must always have a content-
* transfer-encoding of 7bit (the default)" */
g_mime_part_set_content_encoding(GMIME_PART
(mime_msgs[i]->mime_part),
GMIME_CONTENT_ENCODING_7BIT);
libbalsa_set_message_id(mime_msgs[i]);
}
if (rc) {
/* Temporarily modify message by changing its mime_msg: */
libbalsa_message_set_mime_message(message, mime_msgs[i]);
rc = libbalsa_message_copy(message, outbox, error);
}
g_object_unref(mime_msgs[i]);
}
g_free(mime_msgs);
/* Restore message's original mime_msg: */
libbalsa_message_set_mime_message(message, mime_msg);
g_object_unref(mime_msg);
} else {
rc = libbalsa_message_copy(message, outbox, error);
}
return rc ? LIBBALSA_MESSAGE_CREATE_OK : LIBBALSA_MESSAGE_QUEUE_ERROR;
}
LibBalsaMsgCreateResult
libbalsa_message_queue(LibBalsaMessage *message,
LibBalsaMailbox *outbox,
LibBalsaMailbox *fccbox,
LibBalsaSmtpServer *smtp_server,
gboolean flow,
GError **error)
{
LibBalsaMsgCreateResult result;
update_send_timer(FALSE);
result = lbs_message_queue_real(message, outbox, fccbox, smtp_server, flow, error);
if (g_atomic_int_get(&sending_threads) == 0) {
update_send_timer(TRUE);
}
return result;
}
/* libbalsa_message_send:
send the given messsage (if any, it can be NULL) and all the messages
in given outbox.
*/
static void lbs_process_queue(LibBalsaMailbox *outbox,
LibBalsaFccboxFinder finder,
LibBalsaSmtpServer *smtp_server,
GtkWindow *parent);
static gboolean lbs_process_queue_real(LibBalsaSmtpServer *smtp_server,
SendQueueInfo *send_info);
LibBalsaMsgCreateResult
libbalsa_message_send(LibBalsaMessage *message,
LibBalsaMailbox *outbox,
LibBalsaMailbox *fccbox,
LibBalsaFccboxFinder finder,
LibBalsaSmtpServer *smtp_server,
gboolean show_progress,
GtkWindow *parent,
gboolean flow,
GError **error)
{
LibBalsaMsgCreateResult result = LIBBALSA_MESSAGE_CREATE_OK;
g_return_val_if_fail(smtp_server != NULL,
LIBBALSA_MESSAGE_SERVER_ERROR);
if (message != NULL) {
update_send_timer(FALSE);
result = lbs_message_queue_real(message, outbox, fccbox,
smtp_server, flow, error);
if (result == LIBBALSA_MESSAGE_CREATE_OK) {
if (libbalsa_smtp_server_trylock(smtp_server)) {
lbs_process_queue(outbox, finder, smtp_server, show_progress ? parent : NULL);
} else {
g_atomic_int_inc(&retrigger_send);
}
} else if (g_atomic_int_get(&sending_threads) == 0) {
update_send_timer(TRUE);
} else {
/* nothing to do */
}
}
return result;
}
static void
add_recipients(NetClientSmtpMessage *message,
InternetAddressList *recipient_list,
gboolean request_dsn)
{
if (recipient_list != NULL) {
const InternetAddress *ia;
NetClientSmtpDsnMode dsn_mode;
int i;
/* XXX - It would be cool if LibBalsaAddress could contain DSN options
for a particular recipient. For the time being, just use a switch */
if (request_dsn) {
dsn_mode = NET_CLIENT_SMTP_DSN_SUCCESS + NET_CLIENT_SMTP_DSN_FAILURE +
NET_CLIENT_SMTP_DSN_DELAY;
} else {
dsn_mode = NET_CLIENT_SMTP_DSN_NEVER;
}
for (i = 0; i < internet_address_list_length(recipient_list); i++) {
ia = internet_address_list_get_address(recipient_list, i);
if (INTERNET_ADDRESS_IS_MAILBOX(ia)) {
net_client_smtp_msg_add_recipient(message,
INTERNET_ADDRESS_MAILBOX(ia)->addr,
dsn_mode);
} else {
add_recipients(message, INTERNET_ADDRESS_GROUP(ia)->members, request_dsn);
}
}
}
}
static gssize
send_message_data_cb(gchar *buffer,
gsize count,
gpointer user_data,
GError **error)
{
ssize_t read_res;
MessageQueueItem *mqi = (MessageQueueItem *) user_data;
SendMessageInfo *smi = mqi->smsg_info;
read_res = g_mime_stream_read(mqi->stream, buffer, count);
if (!smi->no_dialog && (smi->total_size > 0) && (read_res > 0)) {
gdouble fraction;
gint ipercent;
smi->total_sent += read_res;
fraction = (gdouble) smi->total_sent / (gdouble) smi->total_size;
g_debug("%s: s=%lu t=%lu %g", __func__, (unsigned long) smi->total_sent, (unsigned long) smi->total_size, fraction);
if (fraction > 1.0) {
fraction = 1.0;
}
ipercent = (gint) (100.0 * (fraction + 0.5));
if (!smi->no_dialog && (ipercent > smi->last_report)) {
libbalsa_progress_dialog_update(&send_progress_dialog, smi->progress_id, FALSE, fraction,
_("Message %u of %u"), smi->curr_msg, smi->msg_count);
smi->last_report = ipercent;
}
}
return read_res;
}
static void
lbs_check_reachable_cb(GObject *object,
gboolean can_reach,
gpointer cb_data)
{
LibBalsaSmtpServer *smtp_server = LIBBALSA_SMTP_SERVER(object);
SendQueueInfo *send_info = (SendQueueInfo *) cb_data;
gboolean thread_started = FALSE;
if (can_reach) {
thread_started = lbs_process_queue_real(smtp_server, send_info);
} else {
libbalsa_information(LIBBALSA_INFORMATION_WARNING,
_("Cannot reach SMTP server %s (%s), any queued message will remain in %s."),
libbalsa_smtp_server_get_name(smtp_server),
libbalsa_server_get_host(LIBBALSA_SERVER(smtp_server)),
libbalsa_mailbox_get_name(send_info->outbox));
}
if (!thread_started) {
/* if the thread has been started, it will take care of unlocking the server and re-starting the timer */
libbalsa_smtp_server_unlock(smtp_server);
update_send_timer(TRUE);
}
g_object_unref(send_info->outbox);
if (send_info->parent != NULL) {
g_object_unref(send_info->parent);
}
g_free(send_info);
}
/* note: the following function is called with the passed smtp server being locked
* parent != NULL indicates that the progress dialogue shall be shown */
static void
lbs_process_queue(LibBalsaMailbox *outbox,
LibBalsaFccboxFinder finder,
LibBalsaSmtpServer *smtp_server,
GtkWindow *parent)
{
SendQueueInfo *send_info;
send_info = g_new(SendQueueInfo, 1U);
send_info->outbox = g_object_ref(outbox);
send_info->finder = finder;
if (parent != NULL) {
send_info->parent = g_object_ref(parent);
} else {
send_info->parent = NULL;
}
libbalsa_server_test_can_reach(LIBBALSA_SERVER(smtp_server), lbs_check_reachable_cb, send_info);
}
static void
lbs_process_queue_msg(guint msgno,
SendMessageInfo *send_message_info)
{
MessageQueueItem* new_message;
LibBalsaMessage* msg;
const gchar* smtp_server_name;
LibBalsaMsgCreateResult created;
const gchar *dsn_header;
/* Skip this message if it either FLAGGED or DELETED: */
if (!libbalsa_mailbox_msgno_has_flags(send_message_info->outbox, msgno, 0,
(LIBBALSA_MESSAGE_FLAG_FLAGGED | LIBBALSA_MESSAGE_FLAG_DELETED))) {
return;
}
msg = libbalsa_mailbox_get_message(send_message_info->outbox, msgno);
if (!msg) {
/* error? */
return;
}
/* check the smtp server */
libbalsa_message_body_ref(msg, TRUE);
smtp_server_name = libbalsa_message_get_user_header(msg, "X-Balsa-SmtpServer");
if (!smtp_server_name) {
smtp_server_name = libbalsa_smtp_server_get_name(NULL);
}
if (strcmp(smtp_server_name, libbalsa_smtp_server_get_name(send_message_info->smtp_server)) != 0) {
libbalsa_message_body_unref(msg);
g_object_unref(msg);
return;
}
dsn_header = libbalsa_message_get_user_header(msg, "X-Balsa-DSN");
libbalsa_message_set_request_dsn(msg, dsn_header != NULL ? atoi(dsn_header) != 0 : FALSE);
new_message = msg_queue_item_new(send_message_info);
created = libbalsa_fill_msg_queue_item_from_queu(msg, new_message);
libbalsa_message_body_unref(msg);
if (created != LIBBALSA_MESSAGE_CREATE_OK) {
msg_queue_item_destroy(new_message);
} else {
gboolean request_dsn;
LibBalsaMessageHeaders *headers;
InternetAddressList *from;
const InternetAddress* ia;
const gchar* mailbox;
libbalsa_message_change_flags(msg, LIBBALSA_MESSAGE_FLAG_FLAGGED, 0);
send_message_info->items = g_list_prepend(send_message_info->items, new_message);
new_message->smtp_msg = net_client_smtp_msg_new(send_message_data_cb, new_message);
request_dsn = libbalsa_message_get_request_dsn(msg);
if (request_dsn) {
const gchar *message_id = libbalsa_message_get_message_id(msg);
net_client_smtp_msg_set_dsn_opts(new_message->smtp_msg,
message_id, FALSE);
}
/* Add the sender info */
headers = libbalsa_message_get_headers(msg);
from = headers->from;
if (from != NULL &&
(ia = internet_address_list_get_address(from, 0)) != NULL) {
while (ia != NULL && INTERNET_ADDRESS_IS_GROUP(ia)) {
ia = internet_address_list_get_address(INTERNET_ADDRESS_GROUP(
ia)->members, 0);
}
mailbox = ia ? INTERNET_ADDRESS_MAILBOX(ia)->addr : "";
} else {
mailbox = "";
}
net_client_smtp_msg_set_sender(new_message->smtp_msg, mailbox);
/* Now need to add the recipients to the message. */
add_recipients(new_message->smtp_msg, headers->to_list, request_dsn);
add_recipients(new_message->smtp_msg, headers->cc_list, request_dsn);
add_recipients(new_message->smtp_msg, headers->bcc_list, request_dsn);
/* Estimate the size of the message. This need not be exact but it's better to err
* on the large side since some message headers may be altered during the transfer. */
send_message_info->total_size += g_mime_stream_length(new_message->stream);
send_message_info->msg_count++;
}
g_object_unref(msg);
}
static NetClientSmtp *
lbs_process_queue_init_session(LibBalsaServer* server)
{
NetClientCryptMode security;
const gchar *host;
NetClientSmtp* session;
security = libbalsa_server_get_security(server);
host = libbalsa_server_get_host(server);
if (security == NET_CLIENT_CRYPT_ENCRYPTED) {
session = net_client_smtp_new(host, 465U, security);
} else {
// FIXME - submission (587) is the standard, but most isp's use 25...
session = net_client_smtp_new(host, 587U, security);
}
net_client_smtp_set_auth_mode(session, libbalsa_server_get_auth_mode(server));
/* connect signals */
g_signal_connect(session, "cert-check", G_CALLBACK(libbalsa_server_check_cert), session);
g_signal_connect(session, "auth", G_CALLBACK(libbalsa_server_get_auth), server);
/* load client certificate if configured */
if (libbalsa_server_get_client_cert(server)) {
const gchar *cert_file = libbalsa_server_get_cert_file(server);
GError* error = NULL;
g_signal_connect(session, "cert-pass", G_CALLBACK(libbalsa_server_get_cert_pass), server);
if (!net_client_set_cert_from_file(NET_CLIENT(session), cert_file, &error)) {
libbalsa_information(LIBBALSA_INFORMATION_ERROR, _("Cannot load certificate file %s: %s"), cert_file,
error->message);
/* bad certificate private key password: clear it */
if (error->code == NET_CLIENT_ERROR_CERT_KEY_PASS) {
libbalsa_server_set_password(server, NULL, TRUE);
}
g_error_free(error);
g_object_unref(session);
session = NULL;
}
}
return session;
}
/* libbalsa_process_queue:
treats given mailbox as a set of messages to send. Loads them up and
launches sending thread/routine.
NOTE that we do not close outbox after reading. send_real/thread message
handler does that.
Returns if the thread has been launched
Note: the function is always called with the respective SMTP server being locked, so unlock if we don't start the sending
thread
*/
static gboolean
lbs_process_queue_real(LibBalsaSmtpServer *smtp_server, SendQueueInfo *send_info)
{
gboolean thread_started = FALSE;
g_mutex_lock(&send_messages_lock);
if (libbalsa_mailbox_open(send_info->outbox, NULL)) {
NetClientSmtp *session;
/* create the SMTP session */
session = lbs_process_queue_init_session(LIBBALSA_SERVER(smtp_server));
if (session != NULL) {
SendMessageInfo *send_message_info;
guint msgno;
send_message_info = send_message_info_new(smtp_server, send_info->outbox, send_info->finder, session);
for (msgno = libbalsa_mailbox_total_messages(send_info->outbox); msgno > 0U; msgno--) {
lbs_process_queue_msg(msgno, send_message_info);
}
/* launch the thread for sending the messages only if we collected any */
if (send_message_info->items != NULL) {
GThread *send_mail;
if (send_info->parent != NULL) {
libbalsa_progress_dialog_ensure(&send_progress_dialog, _("Sending Mail"), send_info->parent,
send_message_info->progress_id);
send_message_info->no_dialog = FALSE;
} else {
send_message_info->no_dialog = TRUE;
}
g_atomic_int_inc(&sending_threads);
send_mail = g_thread_new("balsa_send_message_real", (GThreadFunc) balsa_send_message_real, send_message_info);
g_thread_unref(send_mail);
thread_started = TRUE;
} else {
send_message_info_destroy(send_message_info);
}
}
if (!thread_started) {
libbalsa_mailbox_close(send_info->outbox, TRUE);
}
}
g_mutex_unlock(&send_messages_lock);
return thread_started;
}
void
libbalsa_process_queue(LibBalsaMailbox *outbox,
LibBalsaFccboxFinder finder,
GSList *smtp_servers,
gboolean show_progress,
GtkWindow *parent)
{
if (libbalsa_mailbox_open(outbox, NULL)) {
guint msgno;
guint pending = 0U;
update_send_timer(FALSE);
for (msgno = libbalsa_mailbox_total_messages(outbox); msgno > 0U; msgno--) {
if (libbalsa_mailbox_msgno_has_flags(outbox, msgno, 0,
(LIBBALSA_MESSAGE_FLAG_FLAGGED | LIBBALSA_MESSAGE_FLAG_DELETED))) {
pending++;
}
}
if (pending > 0U) {
for (; smtp_servers; smtp_servers = smtp_servers->next) {
LibBalsaSmtpServer *smtp_server = LIBBALSA_SMTP_SERVER(smtp_servers->data);
if (libbalsa_smtp_server_trylock(smtp_server)) {
lbs_process_queue(outbox, finder, smtp_server, show_progress ? parent : NULL);
}
}
} else {
update_send_timer(TRUE);
g_debug("%s: no messages pending", __func__);
}
libbalsa_mailbox_close(outbox, FALSE);
}
}
/* balsa_send_message_real:
does the actual message sending.
This function may be called as a thread.
Also, structure info should be freed before exiting.
*/
static gboolean
balsa_send_message_real_idle_cb(LibBalsaMailbox *outbox)
{
if (g_atomic_int_and(&retrigger_send, 0U) != 0U) {
g_idle_add(auto_send_cb, GINT_TO_POINTER(1));
} else {
update_send_timer(TRUE);
}
libbalsa_mailbox_close(outbox, TRUE);
g_object_unref(outbox);
return FALSE;
}
#define ERROR_IS_TRANSIENT(error) \
(g_error_matches((error), NET_CLIENT_ERROR_QUARK, NET_CLIENT_ERROR_CONNECTION_LOST) || \
g_error_matches((error), NET_CLIENT_SMTP_ERROR_QUARK, NET_CLIENT_ERROR_SMTP_TRANSIENT))
static inline void
balsa_send_message_success(MessageQueueItem *mqi,
SendMessageInfo *info)
{
LibBalsaMailbox *mailbox;
mailbox = mqi->orig != NULL ? libbalsa_message_get_mailbox(mqi->orig) : NULL;
/* sending message successful */
if (mailbox != NULL) {
gboolean remove = TRUE;
const gchar *fccurl = libbalsa_message_get_user_header(mqi->orig, "X-Balsa-Fcc");
if (fccurl != NULL) {
LibBalsaMailbox *fccbox = info->finder(fccurl);
const gchar *fccname = libbalsa_mailbox_get_name(fccbox);
GError *err = NULL;
if (!info->no_dialog) {
libbalsa_progress_dialog_update(&send_progress_dialog, info->progress_id, FALSE, NAN,
_("Save message in %sā¦"), fccname);
}
libbalsa_message_change_flags(mqi->orig, 0, LIBBALSA_MESSAGE_FLAG_NEW | LIBBALSA_MESSAGE_FLAG_FLAGGED);
libbalsa_mailbox_sync_storage(mailbox, FALSE);
remove = libbalsa_message_copy(mqi->orig, fccbox, &err);
if (!remove) {
libbalsa_information(LIBBALSA_INFORMATION_ERROR,
_("Saving sent message to %s failed: %s"),
fccname,
err != NULL ? err->message : "?");
g_clear_error(&err);
}
}
/* If copy failed, mark the message again as flagged - otherwise it will get
* resent again. And again, and again... */
libbalsa_message_change_flags(mqi->orig, remove ? LIBBALSA_MESSAGE_FLAG_DELETED : LIBBALSA_MESSAGE_FLAG_FLAGGED, 0);
}
}
static inline void
balsa_send_message_error(MessageQueueItem *mqi,
GError *error)
{
LibBalsaMailbox *mailbox;
mailbox = mqi->orig != NULL ? libbalsa_message_get_mailbox(mqi->orig) : NULL;
/* sending message failed */
if (mailbox != NULL) {
if (ERROR_IS_TRANSIENT(error)) {
/* Mark it as:
* - neither flagged nor deleted, so it can be resent later
* without changing flags. */
libbalsa_message_change_flags(mqi->orig, 0, LIBBALSA_MESSAGE_FLAG_FLAGGED | LIBBALSA_MESSAGE_FLAG_DELETED);
} else {
/* Mark it as:
* - flagged, so it will not be sent again until the error is fixed
* and the user manually clears the flag;
* - undeleted, in case it was already deleted. */
libbalsa_message_change_flags(mqi->orig, LIBBALSA_MESSAGE_FLAG_FLAGGED, LIBBALSA_MESSAGE_FLAG_DELETED);
}
}
libbalsa_information(LIBBALSA_INFORMATION_ERROR,
_("Sending message to SMTP server %s failed: %s\nMessage left in your outbox."),
libbalsa_smtp_server_get_name(mqi->smsg_info->smtp_server), error->message);
}
static void
balsa_send_message_syslog(const gchar *smtp_server,
const MessageQueueItem *mqi,
gboolean result,
const gchar *server_response,
const GError *server_error)
{
GString *syslog_msg;
syslog_msg = g_string_new(NULL);
g_string_append_printf(syslog_msg, "[%d:%s] SMTP=%s Message-ID=%s", (int) getpid(), g_get_user_name(), smtp_server,
libbalsa_message_get_message_id(mqi->orig));
if (result) {
syslog(LOG_MAIL | LOG_INFO, "%s Result='%s'", syslog_msg->str, server_response);
} else {
syslog(LOG_MAIL | LOG_NOTICE, "%s Error='%s'", syslog_msg->str,
(server_error != NULL) ? server_error->message : "unknown");
}
g_string_free(syslog_msg, TRUE);
}
static gpointer
balsa_send_message_real(SendMessageInfo *info)
{
gboolean result;
GError *error = NULL;
gchar *greeting = NULL;
g_debug("%s: starting", __func__);
/* connect the SMTP server */
if (!info->no_dialog) {
libbalsa_progress_dialog_update(&send_progress_dialog, info->progress_id, FALSE, INFINITY,
_("Connecting %sā¦"), net_client_get_host(NET_CLIENT(info->session)));
}
result = net_client_smtp_connect(info->session, &greeting, &error);
g_debug("%s: connect = %d [%p]: '%s'", __func__, result, info->items, greeting);
g_free(greeting);
if (result) {
GList *this_msg;
if (!info->no_dialog) {
libbalsa_progress_dialog_update(&send_progress_dialog, info->progress_id, FALSE, 0.0,
_("Connected to %s"), net_client_get_host(NET_CLIENT(info->session)));
}
for (this_msg = info->items; this_msg != NULL; this_msg = this_msg->next) {
MessageQueueItem *mqi = (MessageQueueItem *) this_msg->data;
gboolean send_res;
gchar *server_reply = NULL;
LibBalsaMailbox *mailbox;
mailbox = mqi->orig != NULL ? libbalsa_message_get_mailbox(mqi->orig) : NULL;
info->curr_msg++;
g_debug("%s: %u/%u mqi = %p", __func__, info->msg_count, info->curr_msg, mqi);
/* send the message */
send_res = net_client_smtp_send_msg(info->session, mqi->smtp_msg, &server_reply, &error);
balsa_send_message_syslog(net_client_get_host(NET_CLIENT(info->session)), mqi, send_res, server_reply, error);
g_free(server_reply);
g_mutex_lock(&send_messages_lock);
if (mailbox != NULL) {
libbalsa_message_change_flags(mqi->orig, 0, LIBBALSA_MESSAGE_FLAG_FLAGGED);
} else {
g_debug("mqi: %p mqi->orig: %p mailbox: %p",
mqi, mqi->orig, mailbox);
}
if (send_res) {
/* sending message successful */
balsa_send_message_success(mqi, info);
} else {
/* sending message failed */
balsa_send_message_error(mqi, error);
g_clear_error(&error);
result = FALSE;
}
/* free data */
g_mutex_unlock(&send_messages_lock);
}
} else {
if (ERROR_IS_TRANSIENT(error) || (error->code == NET_CLIENT_ERROR_SMTP_AUTHFAIL)) {
GList *this_msg;
/* Mark all messages as neither flagged nor deleted, so they can be resent later
* without changing flags. */
for (this_msg = info->items; this_msg != NULL; this_msg = this_msg->next) {
MessageQueueItem *mqi = (MessageQueueItem *) this_msg->data;
LibBalsaMailbox *mailbox;
mailbox = mqi->orig != NULL ?
libbalsa_message_get_mailbox(mqi->orig) : NULL;
if (mailbox != NULL) {
libbalsa_message_change_flags(mqi->orig,
0,
LIBBALSA_MESSAGE_FLAG_FLAGGED |
LIBBALSA_MESSAGE_FLAG_DELETED);
}
}
if (error->code == NET_CLIENT_ERROR_SMTP_AUTHFAIL) {
/* authentication failed: clear password */
libbalsa_server_set_password(LIBBALSA_SERVER(info->smtp_server), NULL, FALSE);
}
}
libbalsa_information(LIBBALSA_INFORMATION_ERROR,
_("Connecting SMTP server %s (%s) failed: %s"),
libbalsa_smtp_server_get_name(info->smtp_server),
net_client_get_host(NET_CLIENT(info->session)),
error->message);
g_error_free(error);
}
/* close outbox in an idle callback, as it might affect the display */
g_idle_add((GSourceFunc) balsa_send_message_real_idle_cb, g_object_ref(info->outbox));
/* finalise the SMTP session (which may be slow) */
g_object_unref(info->session);
info->session = NULL;
/* clean up */
if (!info->no_dialog) {
libbalsa_progress_dialog_update(&send_progress_dialog, info->progress_id, TRUE, 1.0, _("Finished"));
} else if (result) {
libbalsa_information(LIBBALSA_INFORMATION_MESSAGE,
ngettext("Transmitted %u message to %s", "Transmitted %u messages to %s", info->msg_count),
info->msg_count, libbalsa_smtp_server_get_name(info->smtp_server));
} else {
/* no dialogue and error: information already displayed, nothing to do */
}
libbalsa_smtp_server_unlock(info->smtp_server);
send_message_info_destroy(info);
(void) g_atomic_int_dec_and_test(&sending_threads);
return NULL;
}
static void
message_add_references(LibBalsaMessage *message,
GMimeMessage *msg)
{
GList *list;
/* If the message has references set, add them to the envelope */
list = libbalsa_message_get_references(message);
if (list != NULL) {
GString *str = g_string_new(NULL);
do {
if (str->len > 0) {
g_string_append_c(str, ' ');
}
g_string_append_printf(str, "<%s>", (gchar *) list->data);
} while ((list = list->next) != NULL);
g_mime_object_set_header(GMIME_OBJECT(msg), "References", str->str, NULL);
g_string_free(str, TRUE);
}
list = libbalsa_message_get_in_reply_to(message);
if (list != NULL) {
/* There's no specific header function for In-Reply-To */
g_mime_object_set_header(GMIME_OBJECT(msg), "In-Reply-To", list->data, NULL);
}
}
static GList *
get_mailbox_names(GList *list,
InternetAddressList *address_list)
{
gint i, len;
len = internet_address_list_length(address_list);
for (i = 0; i < len; i++) {
InternetAddress *ia =
internet_address_list_get_address(address_list, i);
if (INTERNET_ADDRESS_IS_MAILBOX(ia)) {
list = g_list_append(list, g_strdup(((InternetAddressMailbox *) ia)->addr));
} else {
list = get_mailbox_names(list, ((InternetAddressGroup *) ia)->members);
}
}
return list;
}
static inline gboolean
ia_list_not_empty(InternetAddressList *ial)
{
return (ial != NULL) && (internet_address_list_length(ial) > 0);
}
static LibBalsaMsgCreateResult
create_mime_message(LibBalsaMessage *message,
gboolean flow,
gboolean postponing,
GMimeMessage **return_message,
GError **error)
{
GMimeObject *mime_root = NULL;
GMimeMessage *mime_message;
LibBalsaMessageBody *body;
LibBalsaMessageHeaders *headers;
gchar *tmp;
GList *list;
gboolean attach_pubkey = FALSE;
GtkWindow *parent = g_object_get_data(G_OBJECT(message), "parent-window");
#ifdef ENABLE_AUTOCRYPT
LibBalsaIdentity *identity;
#endif /* ENABLE_AUTOCRYPT */
GDateTime *datetime;
/* attach the public key only if we send the message, not if we just postpone it */
if (!postponing &&
libbalsa_message_get_attach_pubkey(message) &&
((libbalsa_message_get_crypt_mode(message) & LIBBALSA_PROTECT_PROTOCOL) != 0)) {
attach_pubkey = TRUE;
}
body = libbalsa_message_get_body_list(message);
if ((body != NULL) && ((body->next != NULL) || attach_pubkey)) {
mime_root =
GMIME_OBJECT(g_mime_multipart_new_with_subtype(libbalsa_message_get_subtype(message)));
}
while (body != NULL) {
GMimeObject *mime_part;
mime_part = NULL;
if ((body->file_uri != NULL) || (body->filename != NULL)) {
gchar **mime_type;
if (body->content_type != NULL) {
mime_type = g_strsplit_set(body->content_type, "/;", 3);
} else {
const gchar *mt = libbalsa_vfs_get_mime_type(body->file_uri);
mime_type = g_strsplit(mt, "/", 2);
}
if (body->attach_mode == LIBBALSA_ATTACH_AS_EXTBODY) {
GMimeContentType *content_type =
g_mime_content_type_new("message", "external-body");
mime_part = g_mime_object_new_type(libbalsa_parser_options(),
"message", "external-body");
g_mime_object_set_content_type(mime_part, content_type);
g_mime_part_set_content_encoding(GMIME_PART(mime_part),
GMIME_CONTENT_ENCODING_7BIT);
if (body->filename && !strncmp(body->filename, "URL", 3)) {
g_mime_object_set_content_type_parameter(mime_part,
"access-type", "URL");
g_mime_object_set_content_type_parameter(mime_part,
"URL", body->filename + 4);
} else {
g_mime_object_set_content_type_parameter(mime_part,
"access-type", "local-file");
g_mime_object_set_content_type_parameter(mime_part,
"name",
libbalsa_vfs_get_uri_utf8(body->
file_uri));
}
lbs_set_content(GMIME_PART(mime_part),
"Note: this is _not_ the real body!\n");
} else if (g_ascii_strcasecmp(mime_type[0], "message") == 0) {
GMimeStream *stream;
GMimeParser *parser;
GMimeMessage *mime_msg;
GError *err = NULL;
stream = libbalsa_vfs_create_stream(body->file_uri, 0, FALSE, &err);
if (!stream) {
if (err != NULL) {
gchar *msg =
err->message
? g_strdup_printf(_("Cannot read %s: %s"),
libbalsa_vfs_get_uri_utf8(body->file_uri),
err->message)
: g_strdup_printf(_("Cannot read %s"),
libbalsa_vfs_get_uri_utf8(body->file_uri));
g_set_error(error, err->domain, err->code, "%s", msg);
g_clear_error(&err);
g_free(msg);
}
g_strfreev(mime_type);
return LIBBALSA_MESSAGE_CREATE_ERROR;
}
parser = g_mime_parser_new_with_stream(stream);
g_mime_parser_set_format(parser, GMIME_FORMAT_MESSAGE);
g_object_unref(stream);
mime_msg = g_mime_parser_construct_message(parser, libbalsa_parser_options());
g_object_unref(parser);
mime_part =
GMIME_OBJECT(g_mime_message_part_new_with_message
(mime_type[1], mime_msg));
g_object_unref(mime_msg);
} else {
const gchar *charset = NULL;
GMimeStream *stream;
GMimeDataWrapper *content;
GError *err = NULL;
if ((g_ascii_strcasecmp(mime_type[0], "text") == 0)
&& ((charset = body->charset) == NULL)) {
charset = libbalsa_vfs_get_charset(body->file_uri);
if (charset == NULL) {
static const gchar default_type[] =
"application/octet-stream";
libbalsa_information(LIBBALSA_INFORMATION_WARNING,
_("Cannot determine character set "
"for text file ā%sā; "
"sending as MIME type ā%sā"),
libbalsa_vfs_get_uri_utf8(body->file_uri),
default_type);
g_strfreev(mime_type);
mime_type = g_strsplit(default_type, "/", 2);
}
}
/* use BASE64 encoding for non-text mime types
use 8BIT for message */
mime_part =
GMIME_OBJECT(g_mime_part_new_with_type(mime_type[0],
mime_type[1]));
g_mime_object_set_disposition(mime_part,
body->attach_mode == LIBBALSA_ATTACH_AS_INLINE ?
GMIME_DISPOSITION_INLINE : GMIME_DISPOSITION_ATTACHMENT);
if (g_ascii_strcasecmp(mime_type[0], "text") != 0) {
g_mime_part_set_content_encoding(GMIME_PART(mime_part),
GMIME_CONTENT_ENCODING_BASE64);
} else {
/* is text */
g_mime_object_set_content_type_parameter(mime_part, "charset", charset);
}
g_mime_part_set_filename(GMIME_PART(mime_part),
libbalsa_vfs_get_basename_utf8(body->file_uri));
stream = libbalsa_vfs_create_stream(body->file_uri, 0, FALSE, &err);
if (!stream) {
if (err != NULL) {
gchar *msg =
err->message
? g_strdup_printf(_("Cannot read %s: %s"),
libbalsa_vfs_get_uri_utf8(body->file_uri),
err->message)
: g_strdup_printf(_("Cannot read %s"),
libbalsa_vfs_get_uri_utf8(body->file_uri));
g_set_error(error, err->domain, err->code, "%s", msg);
g_clear_error(&err);
g_free(msg);
}
g_object_unref(mime_part);
g_strfreev(mime_type);
return LIBBALSA_MESSAGE_CREATE_ERROR;
}
content = g_mime_data_wrapper_new_with_stream(stream,
GMIME_CONTENT_ENCODING_DEFAULT);
g_object_unref(stream);
g_mime_part_set_content(GMIME_PART(mime_part),
content);
g_object_unref(content);
}
g_strfreev(mime_type);
} else if (body->buffer != NULL) {
guint gpg_mode;
guint use_gpg_mode;
LibBalsaMsgCreateResult crypt_res = LIBBALSA_MESSAGE_CREATE_OK;
/* in '2440 mode, touch *only* the first body! */
if (!postponing &&
(body == libbalsa_message_get_body_list(body->message)) &&
((gpg_mode = libbalsa_message_get_crypt_mode(message)) != 0) &&
((gpg_mode & LIBBALSA_PROTECT_OPENPGP) != 0)) {
use_gpg_mode = gpg_mode;
} else {
use_gpg_mode = 0;
}
mime_part = add_mime_body_plain(body, flow, postponing, use_gpg_mode,
&crypt_res, error);
if (!mime_part) {
if (mime_root != NULL) {
g_object_unref(mime_root);
}
return crypt_res;
}
}
if (mime_root != NULL) {
g_mime_multipart_add(GMIME_MULTIPART(mime_root),
GMIME_OBJECT(mime_part));
g_object_unref(mime_part);
} else {
mime_root = mime_part;
}
body = body->next;
}
if (attach_pubkey) {
GMimePart *pubkey_part;
pubkey_part = lb_create_pubkey_part(message, parent, error);
if (pubkey_part == NULL) {
if (mime_root != NULL) {
g_object_unref(mime_root);
}
return LIBBALSA_MESSAGE_CREATE_ERROR;
}
if (mime_root != NULL) {
g_mime_multipart_add(GMIME_MULTIPART(mime_root), GMIME_OBJECT(pubkey_part));
g_object_unref(pubkey_part);
} else {
mime_root = GMIME_OBJECT(pubkey_part);
}
}
if ((libbalsa_message_get_body_list(message) != NULL) && !postponing) {
LibBalsaMsgCreateResult crypt_res =
do_multipart_crypto(message, &mime_root, parent, error);
if (crypt_res != LIBBALSA_MESSAGE_CREATE_OK) {
return crypt_res;
}
}
mime_message = g_mime_message_new(TRUE);
if (mime_root != NULL) {
GList *param = libbalsa_message_get_parameters(message);
while (param != NULL) {
gchar **vals = (gchar **)param->data;
g_mime_object_set_content_type_parameter(GMIME_OBJECT(mime_root),
vals[0], vals[1]);
param = param->next;
}
g_mime_message_set_mime_part(mime_message, mime_root);
g_object_unref(mime_root);
}
message_add_references(message, mime_message);
headers = libbalsa_message_get_headers(message);
if (ia_list_not_empty(headers->from)) {
InternetAddressList *from;
from = g_mime_message_get_from(mime_message);
internet_address_list_append(from, headers->from);
}
if (ia_list_not_empty(headers->reply_to)) {
InternetAddressList *reply_to;
reply_to = g_mime_message_get_reply_to(mime_message);
internet_address_list_append(reply_to, headers->reply_to);
}
if (LIBBALSA_MESSAGE_GET_SUBJECT(message)) {
g_mime_message_set_subject(mime_message,
LIBBALSA_MESSAGE_GET_SUBJECT(message), NULL);
}
datetime = g_date_time_new_from_unix_local(headers->date);
g_mime_message_set_date(mime_message, datetime);
g_date_time_unref(datetime);
if (ia_list_not_empty(headers->to_list)) {
InternetAddressList *addresses =
g_mime_message_get_addresses(mime_message,
GMIME_ADDRESS_TYPE_TO);
internet_address_list_append(addresses, headers->to_list);
}
if (ia_list_not_empty(headers->cc_list)) {
InternetAddressList *addresses =
g_mime_message_get_addresses(mime_message,
GMIME_ADDRESS_TYPE_CC);
internet_address_list_append(addresses, headers->cc_list);
}
if (ia_list_not_empty(headers->bcc_list)) {
InternetAddressList *addresses =
g_mime_message_get_addresses(mime_message,
GMIME_ADDRESS_TYPE_BCC);
internet_address_list_append(addresses, headers->bcc_list);
}
if (ia_list_not_empty(headers->dispnotify_to)) {
tmp = internet_address_list_to_string(headers->dispnotify_to, NULL, TRUE);
if (tmp != NULL) {
g_mime_object_append_header(GMIME_OBJECT(mime_message),
"Disposition-Notification-To", tmp, NULL);
g_free(tmp);
}
}
for (list = headers->user_hdrs; list; list = list->next) {
gchar **pair = list->data;
g_strchug(pair[1]);
g_mime_object_append_header(GMIME_OBJECT(mime_message), pair[0], pair[1], NULL);
g_debug("adding header '%s:%s'", pair[0], pair[1]);
}
tmp = g_strdup_printf("Balsa %s", VERSION);
g_mime_object_append_header(GMIME_OBJECT(mime_message), "X-Mailer", tmp, NULL);
g_free(tmp);
#ifdef ENABLE_AUTOCRYPT
/* add Autocrypt header if requested */
if (!postponing && ((identity = libbalsa_message_get_identity(message)) != NULL) &&
(libbalsa_identity_get_autocrypt_mode(identity) != AUTOCRYPT_DISABLE) &&
!autocrypt_ignore(g_mime_object_get_content_type(mime_root))) {
tmp = autocrypt_header(identity, error);
if (tmp == NULL) {
g_object_unref(mime_message);
return LIBBALSA_MESSAGE_CREATE_ERROR;
}
g_mime_object_append_header(GMIME_OBJECT(mime_message), "Autocrypt", tmp, NULL);
g_free(tmp);
}
#endif
libbalsa_message_set_mime_message(message, mime_message);
/* message now holds a reference to mime_message, so we do not add one here: */
if (return_message != NULL)
*return_message = mime_message;
g_object_unref(mime_message);
return LIBBALSA_MESSAGE_CREATE_OK;
}
/* When we postpone a message in the compose window, we lose track of
* the message we were replying to. We *could* save some identifying
* information in a dummy header, but it could still be hard to track it
* down: it might have been filed in another mailbox, for instance. For
* now, we'll just let it go...
*/
gboolean
libbalsa_message_postpone(LibBalsaMessage *message,
LibBalsaMailbox *draftbox,
gchar **extra_headers,
gboolean flow,
GError **error)
{
GMimeMessage *mime_message;
mime_message = libbalsa_message_get_mime_message(message);
if (mime_message == NULL) {
LibBalsaMsgCreateResult res =
create_mime_message(message, flow, TRUE, &mime_message, error);
if (res != LIBBALSA_MESSAGE_CREATE_OK)
return FALSE;
}
if (extra_headers != NULL) {
GMimeObject *object = GMIME_OBJECT(mime_message);
gint i;
for (i = 0; extra_headers[i] && extra_headers[i + 1]; i += 2)
g_mime_object_set_header(object, extra_headers[i], extra_headers[i + 1], NULL);
}
return libbalsa_message_copy(message, draftbox, error);
}
static inline gchar
base32_char(guint8 val)
{
val &= 0x1f;
if (val <= 25) {
return val + 'A';
} else {
return val + '2' - 26;
}
}
/* Create a message-id and set it on the mime message.
*/
static void
libbalsa_set_message_id(GMimeMessage *mime_message)
{
static GMutex mutex; /* as to make me thread-safe... */
static GRand *rand = NULL;
static struct {
gint64 now_monotonic;
gdouble randval;
char user_name[16];
char host_name[16];
} id_data;
GHmac *msg_id_hash;
guint8 buffer[32];
gsize buflen;
gchar *message_id;
guint8 *src;
gchar *dst;
g_mutex_lock(&mutex);
if (rand == NULL) {
/* initialise some stuff on first-time use... */
rand = g_rand_new_with_seed((guint32) time(NULL));
strncpy(id_data.user_name, g_get_user_name(),
sizeof(id_data.user_name) - 1);
id_data.user_name[sizeof(id_data.user_name) - 1] = '\0';
strncpy(id_data.host_name, g_get_host_name(),
sizeof(id_data.host_name) - 1);
id_data.host_name[sizeof(id_data.host_name) - 1] = '\0';
}
/* get some randomness... */
id_data.now_monotonic = g_get_monotonic_time();
id_data.randval = g_rand_double(rand);
/* hash the buffer */
msg_id_hash =
g_hmac_new(G_CHECKSUM_SHA256, (const guchar *) &id_data,
sizeof(id_data));
buflen = sizeof(buffer);
g_hmac_get_digest(msg_id_hash, buffer, &buflen);
g_hmac_unref(msg_id_hash);
g_mutex_unlock(&mutex);
/* create a msg id string as base32-encoded string from the first
* 30 bytes of the hashed result, and separate the groups by '.'
* or '@' */
message_id = dst = g_malloc0(54U); /* = (32 / 5) * 9 */
src = buffer;
while (buflen >= 5U) {
*dst++ = base32_char(src[0] >> 3);
*dst++ = base32_char((src[0] << 2) + (src[1] >> 6));
*dst++ = base32_char(src[1] >> 1);
*dst++ = base32_char((src[1] << 4) + (src[2] >> 4));
*dst++ = base32_char((src[2] << 1) + (src[3] >> 7));
*dst++ = base32_char(src[3] >> 2);
*dst++ = base32_char((src[3] << 3) + (src[4] >> 5));
*dst++ = base32_char(src[4]);
src = &src[5];
buflen -= 5U;
if (dst == &message_id[(54U / 2U) - 1U]) {
*dst++ = '@';
} else if (buflen >= 5U) {
*dst++ = '.';
}
}
g_mime_message_set_message_id(mime_message, message_id);
g_free(message_id);
}
/* balsa_create_msg:
copies message to msg.
*/
static LibBalsaMsgCreateResult
libbalsa_create_msg(LibBalsaMessage *message,
gboolean flow,
GError **error)
{
GMimeMessage *mime_message;
mime_message = libbalsa_message_get_mime_message(message);
if (mime_message == NULL) {
LibBalsaMsgCreateResult res =
create_mime_message(message, flow, FALSE, &mime_message, error);
if (res != LIBBALSA_MESSAGE_CREATE_OK)
return res;
}
libbalsa_set_message_id(mime_message);
return LIBBALSA_MESSAGE_CREATE_OK;
}
static LibBalsaMsgCreateResult
libbalsa_fill_msg_queue_item_from_queu(LibBalsaMessage *message,
MessageQueueItem *mqi)
{
GMimeStream *msg_stream;
LibBalsaMsgCreateResult result = LIBBALSA_MESSAGE_CREATE_ERROR;
mqi->orig = message;
if (libbalsa_message_get_mime_message(message) != NULL) {
LibBalsaMailbox *mailbox = libbalsa_message_get_mailbox(message);
msg_stream = g_mime_stream_mem_new();
libbalsa_mailbox_lock_store(mailbox);
g_mime_object_write_to_stream(GMIME_OBJECT(libbalsa_message_get_mime_message(message)), NULL, msg_stream);
libbalsa_mailbox_unlock_store(mailbox);
g_mime_stream_reset(msg_stream);
} else {
msg_stream = libbalsa_message_stream(message);
}
if (msg_stream != NULL) {
GMimeStream *filter_stream;
GMimeFilter *filter;
filter_stream = g_mime_stream_filter_new(msg_stream);
/* filter out unwanted headers */
filter = g_mime_filter_header_new();
g_mime_stream_filter_add(GMIME_STREAM_FILTER(filter_stream), filter);
g_object_unref(filter);
/* add CRLF */
filter = g_mime_filter_unix2dos_new(FALSE);
g_mime_stream_filter_add(GMIME_STREAM_FILTER(filter_stream), filter);
g_object_unref(filter);
/* encode dot */
filter = g_mime_filter_smtp_data_new();
g_mime_stream_filter_add(GMIME_STREAM_FILTER(filter_stream), filter);
g_object_unref(filter);
/* write to a new stream */
mqi->stream = g_mime_stream_mem_new();
g_mime_stream_write_to_stream(filter_stream, mqi->stream);
g_object_unref(filter_stream);
g_mime_stream_reset(mqi->stream);
g_object_unref(msg_stream);
g_object_ref(mqi->orig);
result = LIBBALSA_MESSAGE_CREATE_OK;
}
return result;
}
/*
* If the identity contains a forced key ID for the passed protocol, return the key ID. Otherwise, return the email address of the
* "From:" address list to let GpeME automagically select the proper key.
*/
static const gchar *
lb_send_from(LibBalsaMessage * message, gpgme_protocol_t protocol)
{
const gchar *from_id;
const gchar *key_id;
if ((protocol == GPGME_PROTOCOL_OpenPGP) &&
((key_id =
libbalsa_identity_get_force_gpg_key_id(libbalsa_message_get_identity(message))) != NULL)
&& (key_id[0] != '\0')) {
from_id = key_id;
} else if ((protocol == GPGME_PROTOCOL_CMS) &&
((key_id =
libbalsa_identity_get_force_smime_key_id(libbalsa_message_get_identity(message))) != NULL)
&& (key_id[0] != '\0')) {
from_id = key_id;
} else {
InternetAddress *ia =
internet_address_list_get_address(libbalsa_message_get_headers(message)->from, 0);
while (INTERNET_ADDRESS_IS_GROUP(ia)) {
ia = internet_address_list_get_address(((InternetAddressGroup
*) ia)->members, 0);
}
from_id = ((InternetAddressMailbox *) ia)->addr;
}
return from_id;
}
static GMimePart *
lb_create_pubkey_part(LibBalsaMessage *message,
GtkWindow *parent,
GError **error)
{
const gchar *key_id;
gchar *keybuf;
GMimePart *mime_part = NULL;
key_id = lb_send_from(message, GPGME_PROTOCOL_OpenPGP);
keybuf = libbalsa_gpgme_get_pubkey(GPGME_PROTOCOL_OpenPGP, key_id, parent, error);
if (keybuf != NULL) {
GMimeStream *stream;
GMimeDataWrapper *wrapper;
gchar *filename;
mime_part = g_mime_part_new_with_type("application", "pgp-keys");
filename = g_strconcat(key_id, ".asc", NULL);
g_mime_object_set_content_type_parameter(GMIME_OBJECT(mime_part), "name", filename);
g_mime_object_set_disposition(GMIME_OBJECT(mime_part), GMIME_DISPOSITION_ATTACHMENT);
g_mime_object_set_content_disposition_parameter(GMIME_OBJECT(mime_part), "filename", filename);
g_free(filename);
g_mime_part_set_content_encoding(mime_part, GMIME_CONTENT_ENCODING_7BIT);
stream = g_mime_stream_mem_new();
g_mime_stream_write(stream, keybuf, strlen(keybuf));
g_free(keybuf);
wrapper = g_mime_data_wrapper_new();
g_mime_data_wrapper_set_stream(wrapper, stream);
g_object_unref(stream);
g_mime_part_set_content(mime_part, wrapper);
g_object_unref(wrapper);
}
return mime_part;
}
static LibBalsaMsgCreateResult
libbalsa_create_rfc2440_buffer(LibBalsaMessage *message,
GMimePart *mime_part,
GtkWindow *parent,
GError **error)
{
guint mode = libbalsa_message_get_crypt_mode(message);
gboolean always_trust = libbalsa_message_get_always_trust(message);
switch (mode & LIBBALSA_PROTECT_MODE) {
case LIBBALSA_PROTECT_SIGN: /* sign only */
if (!libbalsa_rfc2440_sign_encrypt(mime_part,
lb_send_from(message, GPGME_PROTOCOL_OpenPGP),
NULL, FALSE,
parent, error)) {
return LIBBALSA_MESSAGE_SIGN_ERROR;
}
break;
case LIBBALSA_PROTECT_ENCRYPT:
case LIBBALSA_PROTECT_SIGN | LIBBALSA_PROTECT_ENCRYPT:
{
LibBalsaMessageHeaders *headers = libbalsa_message_get_headers(message);
GList *encrypt_for = NULL;
gboolean result;
/* build a list containing the addresses of all to:, cc:
and the from: address. Note: don't add bcc: addresses
as they would be visible in the encrypted block. */
encrypt_for = get_mailbox_names(encrypt_for,
headers->to_list);
encrypt_for = get_mailbox_names(encrypt_for,
headers->cc_list);
encrypt_for = get_mailbox_names(encrypt_for,
headers->from);
if ((headers->bcc_list != NULL) &&
(internet_address_list_length(headers->bcc_list) > 0)) {
libbalsa_information
(LIBBALSA_INFORMATION_WARNING,
ngettext("This message will not be encrypted "
"for the BCC: recipient.",
"This message will not be encrypted "
"for the BCC: recipients.",
internet_address_list_length
(headers->bcc_list)));
}
if (mode & LIBBALSA_PROTECT_SIGN) {
result =
libbalsa_rfc2440_sign_encrypt(mime_part,
lb_send_from(message, GPGME_PROTOCOL_OpenPGP),
encrypt_for,
always_trust,
parent, error);
} else {
result =
libbalsa_rfc2440_sign_encrypt(mime_part,
NULL,
encrypt_for,
always_trust,
parent, error);
}
g_list_free_full(encrypt_for, g_free);
if (!result) {
return LIBBALSA_MESSAGE_ENCRYPT_ERROR;
}
}
break;
default:
g_assert_not_reached();
}
return LIBBALSA_MESSAGE_CREATE_OK;
}
/* handle rfc2633 and rfc3156 signing and/or encryption of a message */
static LibBalsaMsgCreateResult
do_multipart_crypto(LibBalsaMessage *message,
GMimeObject **mime_root,
GtkWindow *parent,
GError **error)
{
guint mode;
gpgme_protocol_t protocol;
gboolean always_trust;
mode = libbalsa_message_get_crypt_mode(message);
/* check if we shall do any protection */
if ((mode & LIBBALSA_PROTECT_MODE) == 0) {
return LIBBALSA_MESSAGE_CREATE_OK;
}
/* check which protocol should be used */
if (mode & LIBBALSA_PROTECT_RFC3156) {
protocol = GPGME_PROTOCOL_OpenPGP;
} else if (mode & LIBBALSA_PROTECT_SMIME) {
protocol = GPGME_PROTOCOL_CMS;
} else if (mode & LIBBALSA_PROTECT_OPENPGP) {
return LIBBALSA_MESSAGE_CREATE_OK; /* already done... */
} else {
return LIBBALSA_MESSAGE_ENCRYPT_ERROR; /* hmmm.... */
}
always_trust = libbalsa_message_get_always_trust(message);
/* sign and/or encrypt */
switch (mode & LIBBALSA_PROTECT_MODE) {
case LIBBALSA_PROTECT_SIGN: /* sign message */
if (!libbalsa_sign_mime_object(mime_root,
lb_send_from(message, protocol),
protocol, parent, error)) {
return LIBBALSA_MESSAGE_SIGN_ERROR;
}
break;
case LIBBALSA_PROTECT_ENCRYPT:
case LIBBALSA_PROTECT_ENCRYPT | LIBBALSA_PROTECT_SIGN:
{
LibBalsaMessageHeaders *headers = libbalsa_message_get_headers(message);
GList *encrypt_for = NULL;
gboolean success;
/* build a list containing the addresses of all to:, cc:
and the from: address. Note: don't add bcc: addresses
as they would be visible in the encrypted block. */
encrypt_for = get_mailbox_names(encrypt_for,
headers->to_list);
encrypt_for = get_mailbox_names(encrypt_for,
headers->cc_list);
encrypt_for = g_list_append(encrypt_for,
g_strdup(lb_send_from(message, protocol)));
if (headers->bcc_list
&& (internet_address_list_length(headers->
bcc_list) > 0)) {
libbalsa_information
(LIBBALSA_INFORMATION_WARNING,
ngettext("This message will not be encrypted "
"for the BCC: recipient.",
"This message will not be encrypted "
"for the BCC: recipients.",
internet_address_list_length
(headers->bcc_list)));
}
if ((libbalsa_message_get_crypt_mode(message) & LIBBALSA_PROTECT_SIGN) != 0) {
success =
libbalsa_sign_encrypt_mime_object(mime_root,
lb_send_from(message, protocol),
encrypt_for, protocol,
always_trust, parent,
error);
} else {
success =
libbalsa_encrypt_mime_object(mime_root, encrypt_for,
protocol, always_trust,
parent, error);
}
g_list_free_full(encrypt_for, (GDestroyNotify) g_free);
if (!success) {
return LIBBALSA_MESSAGE_ENCRYPT_ERROR;
}
break;
}
default:
g_error("illegal gpg_mode %d (" __FILE__ " line %d)", mode, __LINE__);
}
return LIBBALSA_MESSAGE_CREATE_OK;
}
|