/usr/share/jed/lib/rmail.sl is in jed-common 1:0.99.19-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | % See jed/doc/rmail.txt for more information.
% Mark Olesen's patches added.
%!% variable that controls the location of the mailbox for new mail
!if (is_defined("Rmail_Spool_Mailbox_File"))
{
variable Rmail_Spool_Mailbox_File = getenv ("MAIL");
if (NULL == Rmail_Spool_Mailbox_File)
{
Rmail_Spool_Mailbox_File = "/var/spool/mail";
if (2 != file_status (Rmail_Spool_Mailbox_File))
{
Rmail_Spool_Mailbox_File = "/usr/mail"; % HP-UX
}
Rmail_Spool_Mailbox_File = dircat (Rmail_Spool_Mailbox_File, getenv ("USER"));
}
}
%!% where the JED mail reader should put the mail
!if (is_defined("Rmail_Directory"))
{
variable Rmail_Directory = dircat(getenv("HOME"), "Mail");
}
%!% temporary mailbox where newmail is moved to before parsed and converted.
variable Rmail_Tmp_Mbox = dircat(Rmail_Directory, "#NewMail#");
variable Rmail_Master_Index_File = dircat(Rmail_Directory, "_Root_Index.index");
variable Rmail_Folder_Buffer = Null_String;
variable Rmail_Folder_Name = Null_String;
variable Rmail_Newmail_Folder = "New-Mail";
variable Rmail_Root_Buffer = "Rmail: Folder List";
variable Rmail_Mail_Buffer_Name = Null_String;
!if (is_defined("Rmail_Dont_Reply_To"))
{
% comma separated list of name to not reply to.
variable Rmail_Dont_Reply_To;
$1 = getenv("USER"); if ($1 == NULL) $1 = "";
$2 = getenv("HOST"); if ($2 == NULL) $2 = "";
Rmail_Dont_Reply_To = $1;
if (strlen($1) and strlen ($2))
Rmail_Dont_Reply_To = sprintf("%s,%s@%s", $1, $1, $2);
else
Rmail_Dont_Reply_To = $1;
}
Rmail_Dont_Reply_To = strlow (Rmail_Dont_Reply_To);
variable Rmail_Message_Number = Null_String; %/* this is a string!! */
% check for existence of Rmail directory. Create it if it does not exist.
!if (file_status(Rmail_Directory))
{
if (mkdir(Rmail_Directory, 0700))
error("Unable to create " + Rmail_Directory);
if (chmod(Rmail_Directory, 0700))
error ("chmod failed!");
}
!if (is_defined ("Rmail_Ask_About_Cc"))
{
variable Rmail_Ask_About_Cc = 0;
}
define rmail_buffer_name(folder)
{
"Rmail: " + folder;
}
!if (is_defined ("mime_rfc1522_parse_buffer"))
{
autoload ("mime_rfc1522_parse_buffer", "mime");
autoload ("mime_qp_parse_buffer", "mime");
}
define rmail_folder_file(folder)
{
dircat (Rmail_Directory, folder) + ".index";
}
% set buffer flags so that autosave is off but save anyway is on.
% This way it gets saved in case of a power failure or something.
define rmail_set_buffer_flags()
{
variable flags = getbuf_info();
flags = (flags | 0x80) & ~(0x2); % turn off autosave, turn on save
flags = flags & ~(0x20); % turn off undo
flags = flags | 0x100; % No backup file.
setbuf_info(flags);
() = set_buffer_umask (0077);
}
define rmail_save_buffer_secure ()
{
variable dir, file, flags;
(file, dir,,flags) = getbuf_info();
!if (buffer_modified () and strlen (file))
return;
file = dircat(dir, file);
() = write_buffer(file);
() = chmod(file, 0600);
}
define rmail_update_master_index (folder, n, total, n_unread)
{
variable cbuf = whatbuf(), f, fline;
fline = sprintf("Folder:\t%20s\t(%d/%d) messages. %d unread.\n",
folder, n, total, n_unread);
variable flags;
() = read_file(Rmail_Master_Index_File);
rmail_set_buffer_flags();
push_spot_bob ();
% only save if necessary.
!if (bol_fsearch(fline))
{
!if (bol_fsearch(sprintf("Folder:\t%20s\t(", folder))) eob();
set_readonly(0);
delete_line();
insert(fline);
set_readonly(1);
}
rmail_save_buffer_secure(); % only if modified.
pop_spot();
setbuf(cbuf);
}
define rmail_save_folder (folder, delit)
{
!if (bufferp(Rmail_Folder_Buffer)) return;
variable file = rmail_folder_file(folder), n;
variable total, n_ok, n_unread;
setbuf(Rmail_Folder_Buffer);
set_readonly(0);
n = string(what_line());
% This is a good place to update the master index.
% calculate total messages and number of deleted ones.
eob(); bskip_chars(" \t\n");
total = what_line();
if (bobp ())
{
total = 0; n = "0";
}
bob();
n_ok = total;
n_unread = 0;
forever
{
switch (what_char ())
{ case 'D': n_ok--; }
{ case '-': n_unread++; }
!if (down_1 ()) break;
bol();
}
rmail_update_master_index(folder, n_ok, total, n_unread);
widen();
bob();
go_down_1 ();
if (n != line_as_string ())
{
delete_line();
insert(n);
newline();
}
rmail_save_buffer_secure();
if (delit)
{
delbuf(whatbuf());
return;
}
%
% narrow it back
%
bob();
() = bol_fsearch("\x1F\xC\n"); go_down_1 ();
push_mark_eob (); narrow();
}
define rmail_find_folder (folder)
{
variable file = rmail_folder_file(folder);
rmail_save_folder(Rmail_Folder_Name, 1);
Rmail_Folder_Buffer = rmail_buffer_name(folder);
() = find_file(file);
rmail_set_buffer_flags();
rename_buffer(Rmail_Folder_Buffer);
Rmail_Folder_Name = folder;
% make sure buffer is properly formatted. Routines expect this to be
% widened.
widen();
bob();
if (eobp())
{
% new, format it
insert("0\n1\n\x1F\xC\n");
}
}
define rmail_get_header (header, continue_flag, multi_flag)
{
variable h, dh;
bob();
if (strlow (header) == "date: ")
{
% special treatment for date. It always looks like:
% Wed, 11 jun 1993
% or: Sun Dec 04, 1994 11:05:52 GMT
% The day and , are somtimes missing and 1993 may be just 93.
if (re_fsearch("^Date:[ \t]+[a-zA-Z,]* *0?\\([1-3]?\\d\\) +\\([a-zA-Z]+\\) +"))
return sprintf("%s-%s", regexp_nth_match(1), regexp_nth_match(2));
if (re_fsearch("^Date:[ \t]+[a-zA-Z]+ \\([a-zA-Z]+\\) +0?\\([1-3]?\\d\\)[ ,]+"))
return sprintf("%s-%s", regexp_nth_match(2), regexp_nth_match(1));
return Null_String;
}
h = "";
while (bol_fsearch(header))
{
go_right (strlen(header));
skip_white();
push_mark ();
eol ();
if (continue_flag)
{
while (down_1 ())
{
skip_white ();
if (bolp ())
{
go_left_1 ();
break;
}
}
}
dh = bufsubstr();
if (strlen (h)) h += ",";
h += dh;
!if (multi_flag) break;
}
return strtrim (strtrans (h, "\n", " "));
}
define rmail_narrow_to_headers ()
{
bob ();
push_mark ();
!if (bol_fsearch("\n")) eob();
narrow();
bob ();
}
define rmail_extract_headers ()
{
variable from = Null_String, date = Null_String, subject = Null_String;
push_spot ();
rmail_narrow_to_headers ();
from = rmail_get_header("From: ", 0, 0);
!if (strlen(from)) from = rmail_get_header("From ", 0, 0);
date = rmail_get_header("Date: ", 0, 0);
subject = rmail_get_header("Subject: ", 0, 0);
widen();
pop_spot();
% RAFE (flags, the - means it has not been read.)
% The 'from' string may have multibyte characters, which will not
% will not be handled properly via the printf width specifier. So
% pad it outside the printf call.
variable width = 25;
variable len = strwidth (from);
if (len > width)
{
variable ch, new_from = "";
foreach ch (from) using ("chars")
{
ch = char (ch);
if (strwidth (new_from + ch) > width)
break;
new_from += ch;
}
from = new_from;
len = strwidth (from);
}
loop (width-len) from += " ";
sprintf(" %6s %s %s\n", date, from, subject);
% Note: The column widths may not be correct when UTF-8 characters are
% present.
}
% This routine assumes that the buffer is narrow.
define rmail_make_filename()
{
variable cbuf = whatbuf(), n;
setbuf(Rmail_Folder_Buffer);
bob();
push_mark_eol ();
n = bufsubstr_delete();
if (strlen(n)) n = integer(n); else n = 0;
++n;
n = string(n);
bob();
insert(n);
if (eobp())
{
insert("\n1\n\x1F\xC\n");
}
setbuf(cbuf);
return n;
}
define rmail_create_folder(folder)
{
variable dir = dircat(Rmail_Directory, folder);
if (file_status(dir)) return dir;
if (mkdir(dir, 0700))
error ("Unable to create folder directory.");
() = chmod(dir, 0700);
rmail_update_master_index(folder, 0, 0, 0);
return (dir);
}
define rmail_parse_mime ()
{
variable h;
push_spot ();
rmail_narrow_to_headers ();
if (fsearch ("?Q?"))
mime_rfc1522_parse_buffer ();
h = rmail_get_header ("Content-Transfer-Encoding: ", 1, 0);
eob ();
widen ();
if (is_substr (strlow (h), "quoted-printable"))
{
push_mark ();
eob ();
narrow ();
mime_qp_parse_buffer ();
widen ();
}
pop_spot ();
}
define rmail_output_newmail()
{
variable headers;
variable cbuf = whatbuf();
variable n = rmail_make_filename(), file;
file = rmail_create_folder (Rmail_Folder_Name);
file = dircat(file, n);
bob(); push_mark(); skip_chars("\n \t"); del_region();
push_mark_eob();
() = write_region_to_file(file);
% () = chmod(file, 0600);
rmail_parse_mime ();
headers = rmail_extract_headers();
setbuf(Rmail_Folder_Buffer);
eob();
% If this format is changed, change it in xpunge as well.
vinsert ("-%3s", n); insert(headers);
setbuf(cbuf);
}
define rmail_newmail_narrow (from)
{
variable content_length = 0;
push_mark_eol ();
!if (bol_fsearch_char ('\n'))
eob ();
narrow ();
if (re_bsearch ("^\\CContent-Length:[ \t]+\\(\\d+\\)$"))
content_length = integer (regexp_nth_match (1));
bob ();
widen ();
push_mark_eol ();
if (content_length)
{
if (bol_fsearch_char ('\n'))
{
content_length++;
if ((content_length == right (content_length))
and (skip_chars ("\n"),
(re_looking_at (from) or eobp ())))
{
!if (eobp ()) go_up_1 ();
narrow ();
return;
}
}
pop_mark_1 ();
push_mark_eol ();
}
forever
{
if (re_fsearch (from))
{
if (blooking_at ("\n\n"))
{
go_up_1 ();
break;
}
eol ();
continue;
}
eob ();
break;
}
narrow();
}
private define rmail_process_newmail (mbox, newmail_folder)
{
variable n = 0, from = "^\\cFrom ";
rmail_find_folder(newmail_folder);
eob(); push_spot();
setbuf(" *rmail-newmail*");
erase_buffer();
if (insert_file(mbox) <= 0) error ("File not inserted.");
bob(); push_mark();
if (0 == re_fsearch(from))
{
message ("New mail with no messages!");
pop_mark_1 ();
pop_spot ();
return 0;
}
del_region();
() = set_buffer_umask (0077);
forever
{
rmail_newmail_narrow (from);
++n;
flush (sprintf ("Processing %d messages...", n));
rmail_output_newmail();
erase_buffer();
widen();
if (eobp()) break;
del();
}
% pop back to beginning of the newest messages in the folder.
% in addition, save_folder expects the buffer to be narrowed.
whatbuf();
setbuf(Rmail_Folder_Buffer);
bob();
() = bol_fsearch("\x1F\xC\n");
go_down_1 (); push_mark_eob(); narrow();
pop_spot();
setbuf(());
rmail_save_folder(newmail_folder, 1);
return n;
}
define rmail_get_newmail_from_file (file, tmp_mbox, folder)
{
variable n, st;
variable nomail, cmd;
variable getmail = dircat(Jed_Bin_Dir, "getmail");
nomail = sprintf ("No new mail in %s", file);
st = stat_file (file);
if (
#if (_slang_version >= 20100)
(st == NULL) || (st.st_size <= 0)
#else
orelse{NULL == st}{st.st_size <= 0}
#endif
)
{
message (nomail);
return (0);
}
flush("Getting new mail...");
if (file_status(getmail) != 1)
{
error (getmail + " not found!");
}
cmd = sprintf("%s %s %s", getmail, file, tmp_mbox);
push_mark();
if (pipe_region(cmd))
error ("getmail returned error.");
n = rmail_process_newmail (tmp_mbox, folder);
% delete_file(Rmail_Tmp_Mbox);
return n;
}
define rmail_get_newmail ()
{
rmail_get_newmail_from_file (Rmail_Spool_Mailbox_File, Rmail_Tmp_Mbox, Rmail_Newmail_Folder);
}
define rmail_folder_mode()
{
variable flags;
() = find_file(Rmail_Master_Index_File);
set_readonly(1);
rmail_set_buffer_flags();
rename_buffer(Rmail_Root_Buffer);
bob();
use_keymap("Rmail-Folder");
}
define rmail_exit_folder ()
{
rmail_save_folder(Rmail_Folder_Name, 1);
if (bufferp(Rmail_Root_Buffer)) pop2buf(Rmail_Root_Buffer);
else rmail_folder_mode();
onewindow();
if (bufferp(Rmail_Mail_Buffer_Name))
{
delbuf(Rmail_Mail_Buffer_Name);
Rmail_Message_Number = Null_String;
}
Rmail_Mail_Buffer_Name = Null_String;
clear_message ();
}
variable Rmail_Headers_Hidden = 0;
define rmail_unhide_headers ()
{
variable mark = "\x1F\xC\n";
Rmail_Headers_Hidden = 0;
set_readonly(0);
widen();
bob(); push_mark();
!if (bol_fsearch("\n")) eob();
narrow();
bob();
% make a mark if not found
if (bol_fsearch(mark))
{
go_down_1 (); push_spot();
% delete visible headers
push_mark_eob(); del_region();
% restore old ones
bob(); push_mark(); () = bol_fsearch(mark);
bufsubstr(); eob(); insert();
pop_spot();
}
else
{
insert(mark);
push_mark_eob();
bskip_chars("\n");
bufsubstr();
bob();
insert();
newline();
go_down(2);
}
push_mark(); widen(); eob(); narrow();
bob(); set_readonly(1);
}
define rmail_hide_headers()
{
variable ok_headers = "From,Subj,Cc,Organ,In-Reply,Date,Reply-To,To";
variable header;
rmail_unhide_headers();
Rmail_Headers_Hidden = 1;
widen(); bob(); push_mark();
!if (bol_fsearch("\n")) eob();
narrow();
bob();
!if (bol_fsearch("\x1F\xC\n")) error ("Unable to find marker.");
set_readonly(0);
push_spot();
% Unwrap lines
forever
{
eol (); trim ();
!if (looking_at ("\n ") or looking_at ("\n\t"))
break;
del ();
trim ();
insert_single_space ();
}
goto_spot ();
% mark ok lines
foreach (strtok (ok_headers, ","))
{
header = ();
while (bol_fsearch(header))
{
insert("\xFF");
}
#ifnfalse
while (down_1 () and
not (skip_white (), bolp ()))
{
bol ();
insert ("\xFF");
}
#endif
goto_spot ();
}
pop_spot();
eol(); go_right_1 ();
% delete unmarked lines
while(not(eobp()))
{
bol();
if (looking_at("\xFF"))
{
del();
eol(); go_right_1 ();
continue;
}
push_mark_eol(); skip_chars("\n"); del_region();
eol();
}
%newline();
widen();
bob();
() = bol_fsearch("\x1F\xC\n");
go_down_1 ();
push_mark_eob(); narrow();
bob();
}
% This routines returns the file number of current summary line as a string.
% If the line is invalid, it returns the null string.
define rmail_extract_file_number()
{
bol();
go_right_1 (); skip_white();
push_mark();
skip_chars("0-9");
bufsubstr();
}
define rmail_add_flag(flag)
{
variable n = 13, flags = "RFE";
flag = char(flag);
n += is_substr(flags, flag);
goto_column(n);
!if (looking_at(flag))
{
insert(flag);
del();
}
}
% this routine returns 0 if line does not contain a valid message
% 1 if the message is already in the window, 2 if not but it exists, -1
% if it had to be read in.
define rmail_get_message(flag)
{
variable buf, file, n, n1, pop_buf, ret;
!if (bufferp(Rmail_Folder_Buffer)) return (0);
pop2buf(Rmail_Folder_Buffer);
file = rmail_extract_file_number ();
!if (strlen(file)) return 0;
Rmail_Message_Number = file;
set_readonly(0);
bol(); if (looking_at_char ('-'))
{
del(); insert_single_space ();
}
if (flag) rmail_add_flag(flag);
set_readonly(1);
buf = sprintf("Rmail: %s(%s)", Rmail_Folder_Name, file);
pop_buf = buf;
ret = 2;
if ((buf != Rmail_Mail_Buffer_Name) or not(bufferp(buf)))
{
%
% we have to read it in.
%
ret = -1;
if (bufferp(Rmail_Mail_Buffer_Name))
{
pop_buf = Rmail_Mail_Buffer_Name;
}
}
if (buffer_visible(pop_buf))
{
pop2buf(pop_buf);
if (ret == 2) ret = 1;
}
else
{
% I want summary window at top. There is no nice way to do it
% at present.
onewindow();
n = window_info('r');
pop2buf(pop_buf);
if (TOP_WINDOW_ROW == window_info('t'))
{
pop2buf(Rmail_Folder_Buffer);
sw2buf(pop_buf);
}
n1 = window_info('r');
% we want n to be 4 lines
% now it is:
n -= n1 + 1;
loop (n - 4) enlargewin();
pop2buf(Rmail_Folder_Buffer);
pop2buf(pop_buf);
}
if (ret != -1) return ret;
() = set_buffer_umask (077);
set_readonly(0);
widen();
erase_buffer();
Rmail_Mail_Buffer_Name = buf;
file = dircat(dircat(Rmail_Directory, Rmail_Folder_Name), file);
n = insert_file(file);
ERROR_BLOCK
{
set_readonly(1);
set_buffer_modified_flag(0);
}
if (n < 0)
{
mark_buffer ();
() = write_region_to_file (file);
error ("File not found!");
}
bob();
rename_buffer(buf);
#iftrue
rmail_parse_mime ();
#endif
rmail_hide_headers();
EXECUTE_ERROR_BLOCK;
return (ret);
}
define rmail_toggle_headers()
{
!if (rmail_get_message(0)) return;
if (Rmail_Headers_Hidden)
{
rmail_unhide_headers();
}
else rmail_hide_headers();
set_buffer_modified_flag(0);
set_readonly(1);
pop2buf(Rmail_Folder_Buffer);
}
define rmail_scroll_forward ()
{
ERROR_BLOCK
{
pop2buf(Rmail_Folder_Buffer);
}
if (rmail_get_message(0) == 1)
{
update_sans_update_hook(0);
call("page_down");
}
EXECUTE_ERROR_BLOCK;
}
define rmail_skip_quotes ()
{
variable str = "^ *[>:|=]";
ERROR_BLOCK
{
pop2buf(Rmail_Folder_Buffer);
}
if (1 == rmail_get_message (0))
{
update(0);
goto_top_of_window ();
go_down_1 ();
if (re_fsearch (str))
{
while (
#if (_slang_version >= 20100)
(push_mark (), narrow (), bol (), re_fsearch (str), widen ())
|| (skip_white (), eolp ())
#else
orelse { push_mark (), narrow (), bol (), re_fsearch (str), widen () }
{ skip_white (), eolp ()}
#endif
)
{
!if (down_1 ()) break;
}
recenter (1);
}
}
EXECUTE_ERROR_BLOCK;
}
define rmail_scroll_backward()
{
ERROR_BLOCK
{
pop2buf(Rmail_Folder_Buffer);
}
if (rmail_get_message(0) == 1)
{
update(0);
call("page_up");
}
EXECUTE_ERROR_BLOCK;
}
define rmail_format_mail_buffer ()
{
variable opt_headers = NULL;
if (_NARGS == 1)
opt_headers = ();
pop2buf (Rmail_Folder_Buffer);
mail();
%onewindow();
if (buffer_modified ())
{
!if (get_yes_no("Mail already being composed. Erase it")) return 0;
}
mail_format_buffer (1, opt_headers);
1;
}
define rmail_forward_message ()
{
variable cbuf, subj;
!if (rmail_get_message('F'))
{
beep();
return;
}
cbuf = whatbuf ();
push_spot ();
bob ();
subj = "Fwd: ";
if (bol_fsearch ("Subject: "))
{
go_right (9);
push_mark ();
eol ();
subj = sprintf ("Fwd: [%s]", bufsubstr ());
}
pop_spot ();
!if (rmail_format_mail_buffer ())
return;
eob();
insert ("----Begin Forwarded Message----\n");
insbuf(cbuf);
insert ("----End Forwarded Message----\n");
bob();
if (bol_fsearch ("Subject: "))
{
eol ();
insert (subj);
}
bob ();
eol();
}
% This function assumes that the buffer has been narrowed about the headers.
define rmail_unfold_headers ()
{
variable flags;
flags = getbuf_info ();
setbuf_info (0);
push_spot ();
bob ();
do
{
while (eol (),
(looking_at ("\n ") or looking_at ("\n\t")))
del ();
}
while (down_1 ());
() = getbuf_info ();
setbuf_info (flags);
pop_spot ();
}
define rmail_insert_attribution (from, date, cc)
{
variable you = "you";
if (strlen (cc) and strlen(from)) you = from;
if (strlen (date))
vinsert ("On %s, %s said:\n", date, you);
else
vinsert ("%s said:\n", you);
}
define rmail_parse_email_address (from)
{
variable pos, len;
% First try to remove comments. We have 2 forms to consider:
% "not@a.comment, This is a comment <this@is.not>, this@is_not (This is)"
% Knock out parenthesis form. Try to handle nested ones
while (pos = string_match (from, "\\(([^()]*)\\)", 1), pos)
{
(pos, len) = string_match_nth (1);
from = substrbytes (from, 1, pos) + substrbytes (from, pos + len + 1, -1);
}
% Remove double quote type comments
from = str_uncomment_string (from, "\"", "\"");
% Now try to get rid of ", This is a comment <this is not> bla" form
% In general, we must handle commas:
% 1. "not comment,is comment <not comment> is comment, not comment"
% So we do the following:
% (However, we still incorrectly handle commas in comments!)
while (pos = string_match (from, ",?\\([^,]*<\\([^>]*\\)>[^,]*\\)", 1), pos)
{
variable pos1, len1;
(pos, len) = string_match_nth (1);
(pos1, len1) = string_match_nth (2);
from = substrbytes (from, 1, pos)
+ substrbytes (from, pos1 + 1, len1)
+ substrbytes (from, pos + len + 1, -1);
}
% Now replace all space, tab, and newline by commas
return strcompress (strtrans (strcompress(from, " \t\n") , " ", ","), ",");
}
define rmail_apply_dont_reply_to (from)
{
variable sub_f, n, num;
% add to list, see if any part of it is in the list.
","; % delimiter for create_delimited_string
n = 0;
num = 0;
from = strlow (from);
while (sub_f = extract_element (from, n, ','), sub_f != NULL)
{
n++;
variable pats;
variable matches;
pats = "^" + strchop (Rmail_Dont_Reply_To, ',', '\\') + "$";
matches = array_map (Int_Type, &string_match, sub_f, pats, 1);
!if (any (matches))
{
sub_f;
num++;
}
}
create_delimited_string (num);
}
define rmail_reply ()
{
!if (rmail_get_message('R'))
{
beep();
return;
}
variable subj, cc, to, replyto, from, msgid=NULL;
variable date = "";
variable cbuf = whatbuf ();
push_spot ();
variable headers_hidden = Rmail_Headers_Hidden;
rmail_unhide_headers ();
rmail_narrow_to_headers ();
set_buffer_modified_flag(0);
set_readonly(1);
replyto = rmail_get_header ("Reply-To: ", 1, 0);
!if (strlen (replyto))
replyto = rmail_get_header ("From: ", 1, 0);
to = rmail_get_header ("To: ", 1, 1);
subj = rmail_get_header ("Subject: ", 1, 0);
!if (strlen (subj))
subj = "(No Subject)";
msgid = rmail_get_header ("Message-ID: ", 1, 0);
cc = rmail_get_header ("Cc:", 1, 1);
if (Rmail_Ask_About_Cc and strlen (cc))
{
ERROR_BLOCK
{
cc = "";
_clear_error ();
}
!if (get_yes_no ("Include the cc: header"))
cc = Null_String;
}
cc += "," + to;
cc = rmail_apply_dont_reply_to (rmail_parse_email_address (cc));
replyto = rmail_parse_email_address (replyto);
from = rmail_get_header ("From: ", 1, 0);
eob ();
% get the date for the attribution string
if (re_bsearch("^Date:[ \t]*\\(.*\\)$"))
date = regexp_nth_match (1);
widen();
if (headers_hidden)
{
rmail_hide_headers ();
set_buffer_modified_flag(0);
set_readonly(1);
}
pop_spot();
variable opt_headers = NULL;
if (msgid != "")
opt_headers = sprintf ("In-Reply-To: %s\n", msgid);
!if (rmail_format_mail_buffer (opt_headers))
return;
bob();
do
{
if (looking_at("To:"))
{
eol ();
insert (replyto);
}
else if (looking_at("Cc:"))
{
eol ();
insert (cc);
}
else if (looking_at("Subject:"))
{
eol ();
push_spot ();
insert (subj);
pop_spot ();
!if (looking_at("Re:")) insert ("Re: ");
}
}
while (down_1 () and not (looking_at ("X-Mailer:")));
eob();
if (bol_bsearch ("-- \n")) % find signature
up_1 ();
push_mark();
narrow();
% This trick allows a single undo to remove the later insertion:
erase_buffer();
insbuf(cbuf);
bob();
rmail_insert_attribution (from, date, cc);
push_spot ();
do
{
bol();
insert_char ('>');
}
while (down_1 ());
newline();
pop_spot ();
widen();
!if (strlen (to))
{
bob(); eol();
}
}
define rmail_edit ()
{
variable file;
!if (get_y_or_n ("Are you sure you want to edit")) return;
!if (rmail_get_message ('E'))
return;
% Now we are in the message buffer. The global variable Rmail_Message_Number
% should be correct.
file = dircat(dircat(Rmail_Directory, Rmail_Folder_Name), Rmail_Message_Number);
if (1 != file_status (file))
verror ("Unable to read %s", file);
set_buffer_modified_flag (0);
delbuf (whatbuf ());
() = find_file (file);
text_mode ();
set_buffer_no_backup ();
}
define rmail_find_next_message ()
{
push_mark();
while (down_1 () and looking_at_char ('D'));
if (eobp())
{
pop_mark_1 ();
message("No more undeleted messages.");
return 0;
}
pop_mark_0 ();
1;
}
define rmail_find_prev_message()
{
push_mark();
while (up_1 (), bol(), () and looking_at_char ('D'));
if (bobp() and looking_at_char ('D'))
{
pop_mark_1 ();
message("No more undeleted messages.");
return 0;
}
pop_mark_0 ();
1;
}
define rmail_delete_cmd ()
{
bol();
if (eolp()) return beep();
!if (looking_at_char ('D'))
{
set_readonly(0);
del(); insert_char ('D');
set_readonly(1);
}
}
define rmail_delete_backward ()
{
rmail_delete_cmd ();
() = rmail_find_prev_message ();
}
define rmail_delete_forward ()
{
rmail_delete_cmd ();
() = rmail_find_next_message();
}
define rmail_undelete ()
{
push_mark_eol();
if (bol_bsearch_char ('D'))
{
set_readonly(0);
del(); insert_single_space ();
set_readonly(1);
pop_mark_0 ();
return;
}
pop_mark_1 ();
beep();
}
define rmail_next_message()
{
if (rmail_find_next_message())
{
rmail_scroll_forward();
}
}
define rmail_prev_message()
{
if (rmail_find_prev_message())
{
rmail_scroll_forward();
}
}
define rmail_select_folder (folder)
{
variable n;
rmail_find_folder(folder);
bob();
go_down_1 ();
n = integer(line_as_string ());
() = bol_fsearch("\x1F\xC\n");
go_down_1 ();
push_mark_eob();
narrow();
pop2buf(whatbuf());
goto_line(n); bol();
use_keymap("Rmail-Read");
set_status_line("Jed %b Folder. (%p) %t", 0);
set_readonly(1);
run_mode_hooks ("rmail_folder_hook");
}
define rmail_build_folder_list ()
{
variable slist = Null_String;
variable cbuf = whatbuf ();
setbuf (Rmail_Root_Buffer);
push_spot_bob ();
while (bol_fsearch("Folder:\t"))
{
go_right (8);
skip_white ();
push_mark ();
() = ffind ("\t");
slist += "," + bufsubstr ();
}
pop_spot ();
setbuf (cbuf);
slist;
}
private define folder_exists (folder)
{
folder = dircat(Rmail_Directory, folder);
return file_status(folder) == 2;
}
private define query_create_folder (default_folder)
{
variable folder, new_dir;
folder = read_string_with_completion ("Move to folder:",
default_folder,
rmail_build_folder_list ());
if (not (strlen (folder)))
return NULL;
if (folder == Rmail_Folder_Name)
return folder;
if (0 == folder_exists (folder))
{
if (1 != get_yes_no(sprintf("Folder %s does not exist, create it", folder)))
return NULL;
() = rmail_create_folder(folder);
}
return folder;
}
define rmail_folder_newmail ()
{
variable n;
variable file = Rmail_Spool_Mailbox_File;
variable folder;
folder = Rmail_Newmail_Folder;
if (-1 != prefix_argument (-1))
{
file = read_file_from_mini ("Input MailBox:");
!if (strlen (file)) return;
folder = query_create_folder (folder);
if (folder == NULL)
return;
}
n = rmail_get_newmail_from_file (file, Rmail_Tmp_Mbox, folder);
if (n)
{
flush(sprintf("%d new messages.", n));
rmail_select_folder(folder);
}
}
variable Rmail_Last_Folder = Null_String;
% This routine deletes buffer containing message n if it exists
% it returns 1 if is exists and is visible otherwise it returns 0.
define rmail_validate_message ()
{
variable vis = 0;
variable buf = sprintf("Rmail: %s(%s)", Rmail_Folder_Name, Rmail_Message_Number);
if (bufferp(buf))
{
vis = buffer_visible(buf);
delbuf(buf);
}
return vis;
}
define rmail_update_folder_and_save ()
{
variable new_n;
eob ();
bskip_chars ("\n \t");
bol ();
new_n = rmail_extract_file_number ();
!if (strlen(new_n)) new_n = "0";
widen();
bob(); push_mark_eol();
if (bufsubstr() != new_n)
{
delete_line();
insert(new_n);
newline();
}
() = bol_fsearch("\x1F\xC\n");
go_down_1 (); push_mark_eob(); narrow();
pop_mark_1 ();
rmail_save_folder(Rmail_Folder_Name, 0);
}
define rmail_resequence_folder ()
{
variable n, file, new_file, dir, new_n = "0";
variable update_message = 0;
variable res_fmt = "Resequencing folder [%d/%d]...";
variable nmax;
ERROR_BLOCK
{
rmail_update_folder_and_save ();
pop_spot ();
}
push_spot();
set_readonly(0);
dir = dircat(Rmail_Directory, Rmail_Folder_Name);
n = 0;
eob ();
nmax = what_line () - 1;
bob();
flush (sprintf (res_fmt, n, nmax));
while (file = rmail_extract_file_number (), strlen(file))
{
n++;
new_n = string(n);
!if (n mod 10)
flush (sprintf (res_fmt, n, nmax));
if (file != new_n)
{
file = dircat(dir, file);
new_file = dircat(dir, new_n);
if (file_status (new_file))
verror ("File %s exists. Cannot rename %s to it.",
new_file, file);
if (rename_file(file, new_file))
error ("Error renaming ", file);
bol();
go_right_1 ();
push_mark();
skip_white();
skip_chars("0-9");
del_region();
vinsert ("%3s", new_n);
if (new_n == Rmail_Message_Number)
{
update_message = rmail_validate_message ();
}
}
!if (down_1 ()) break;
}
% update the last file number
rmail_update_folder_and_save ();
pop_spot();
eol (); bskip_chars("\n"); bol ();
set_readonly(1);
if (update_message or (integer(new_n) < integer(Rmail_Message_Number)))
{
Rmail_Message_Number = Null_String;
rmail_scroll_forward ();
}
message("done.");
}
define rmail_output_to_folder()
{
variable folder = NULL, header, old_n, new_n, new_file, old_file;
variable old_folder = Rmail_Folder_Name;
variable buf, vis;
if (_NARGS)
folder = ();
variable new_dir, old_dir = dircat(Rmail_Directory, Rmail_Folder_Name);
old_n = rmail_extract_file_number ();
!if (strlen(old_n)) return;
if (
#if (_slang_version >= 20100)
(folder == NULL) || (0 == folder_exists (folder))
#else
orelse {(folder == NULL)}{0 == folder_exists (folder)}
#endif
)
folder = query_create_folder (Rmail_Last_Folder);
if (folder == NULL)
return;
new_dir = dircat(Rmail_Directory, folder);
% lets get this header
header = line_as_string ();
rmail_find_folder (folder);
%
% generate a new filename
%
new_n = rmail_make_filename();
new_file = dircat(new_dir, new_n);
old_file = dircat(old_dir, old_n);
if (rename_file(old_file, new_file))
{
rmail_select_folder(old_folder);
error("Unable to rename file.");
}
eob();
insert(header);
bol();
go_right_1 ();
push_mark();
skip_white();
skip_chars("0-9");
del_region();
vinsert ("%3s", new_n);
Rmail_Last_Folder = folder;
% Now narrow it so next routine can process it
bob();
() = bol_fsearch("\x1F\xC\n");
go_down_1 ();
push_mark_eob();
narrow();
rmail_select_folder(old_folder);
set_readonly(0);
bob();
() = bol_fsearch(header);
delete_line();
rmail_resequence_folder();
}
define rmail_unhide_deleted ()
{
push_spot ();
mark_buffer ();
set_region_hidden (0);
pop_spot ();
}
define rmail_hide_deleted ()
{
push_spot ();
rmail_unhide_deleted ();
bob ();
while (bol_fsearch_char ('D'))
{
set_line_hidden (1);
eol ();
}
pop_spot ();
}
% This function is assumed to be called from within a folder. It REALLY
% deletes files marked with the D.
define rmail_xpunge_deletions ()
{
variable file, dir, n, new_file;
% variable old_n = Null_String;
flush("Expunging messages...");
push_mark();
ERROR_BLOCK
{
rmail_resequence_folder ();
pop_mark_0 ();
set_readonly(1);
}
set_readonly(0);
dir = dircat(Rmail_Directory, Rmail_Folder_Name);
bob();
while (bol_fsearch_char ('D'))
{
file = rmail_extract_file_number();
!if (strlen(file)) continue;
% !if (strcmp(file, Rmail_Message_Number))
% {
% old_n = file;
% }
file = dircat(dir, file);
if (1 != file_status(file))
error ("Unable to access ", file);
!if (delete_file(file))
error ("Unable to delete ", file);
delete_line ();
}
pop_mark_1 ();
rmail_resequence_folder ();
}
$1 = "Rmail-Read";
!if (keymap_p($1))
{
make_keymap($1);
_for (' ', 127, 1)
{
$2 = char(());
undefinekey($2, $1);
}
definekey("rmail_skip_quotes", "\t", $1);
definekey("rmail_delete_backward", "D", $1);
definekey("rmail_delete_forward", "d", $1);
definekey("rmail_exit_folder", "q", $1);
definekey("rmail_exit_folder", "Q", $1);
definekey("rmail_folder_newmail", "g", $1);
definekey("rmail_folder_newmail", "G", $1);
definekey("rmail_next_message", "n", $1);
definekey("rmail_next_message", "N", $1);
definekey("rmail_prev_message", "p", $1);
definekey("rmail_prev_message", "P", $1);
definekey("rmail_toggle_headers", "t", $1);
definekey("rmail_toggle_headers", "T", $1);
definekey("rmail_scroll_forward", " ", $1);
definekey("rmail_scroll_forward", "\r", $1);
definekey("rmail_scroll_backward", "^?", $1);
definekey("rmail_xpunge_deletions", "x", $1);
definekey("rmail_xpunge_deletions", "X", $1);
definekey("rmail_reply", "r", $1);
definekey("rmail_reply", "R", $1);
definekey("rmail_forward_message", "f", $1);
definekey("rmail_forward_message", "F", $1);
definekey("rmail_output_to_folder", "o", $1);
definekey("rmail_undelete", "u", $1);
definekey("rmail_undelete", "U", $1);
definekey("rmail_edit", "E", $1);
definekey("mail", "m", $1);
definekey("mail", "M", $1);
}
define rmail_select_this_folder ()
{
bol();
!if (looking_at("Folder:\t")) return beep();
() = ffind("\t");
skip_white();
push_mark();
!if (ffind("\t("))
{
pop_mark(0);
beep();
return;
}
rmail_select_folder (bufsubstr());
}
$1 = "Rmail-Folder";
!if (keymap_p($1))
{
make_keymap($1);
_for (' ', 127, 1)
{
$2 = char(());
undefinekey($2, $1);
}
definekey("rmail_folder_newmail", "g", $1);
definekey("rmail_folder_newmail", "G", $1);
definekey("rmail_quit_rmail", "q", $1);
definekey("rmail_quit_rmail", "Q", $1);
definekey("rmail_select_this_folder", " ", $1);
definekey("rmail_select_this_folder", "\r", $1);
definekey("mail", "m", $1);
definekey("mail", "M", $1);
}
% if parameter is 0, unlock it if it is ours. Otherwise, lock or steal it.
% it returns 1 if the lock was made, 0 if not.
define rmail_check_lock_file(lck)
{
variable lpid, this_pid = string(getpid()), do_del;
variable lfn = "___LOCKED___";
variable lock_file = dircat (Rmail_Directory, lfn);
variable ret = 0, write_it = 0;
variable errbuf;
variable ourhost = getenv ("HOST"); if (ourhost == NULL) ourhost = "";
variable lockhost = Null_String;
variable lock_buffer;
variable flags;
lock_buffer = " " + lfn;
ERROR_BLOCK
{
if (bufferp (lock_buffer))
{
setbuf (lock_buffer);
set_buffer_modified_flag(0);
delbuf (lock_buffer);
}
}
EXECUTE_ERROR_BLOCK;
() = read_file(lock_file); clear_message ();
(,flags) = getbuf_info ();
setbuf_info (lock_buffer, flags);
set_buffer_no_backup ();
% extract the pid, it will be null if file was created
bob(); push_mark();
if (ffind_char (':'))
{
lockhost = bufsubstr ();
go_right_1 ();
push_mark ();
}
eol(); lpid = bufsubstr();
delete_line();
vinsert ("%s:%s", ourhost, this_pid);
if (lck)
{
if (strlen (lockhost))
{
if (lockhost != ourhost)
{
verror ("Pid %s on host %s is locking the mail file. Sorry.",
lpid, lockhost);
}
}
% check existence of pid
if (kill(integer (lpid), 0)) lpid = "";
if (strlen (lpid) and (lpid != this_pid))
{
verror ("Pid %s is locking mail directory. Sorry.",
lpid);
}
save_buffer ();
% Now that we have created a lock file, we use a trick to unlock it
% in case jed is exited ungracefully.
erase_buffer ();
rmail_set_buffer_flags ();
return 1;
}
% We get here if we are unlocking the file.
#iffalse
if ((lockhost != ourhost) or (lpid != this_pid))
return 0;
() = delete_file(lock_file);
#else
if ((lockhost == ourhost) and (lpid == this_pid))
() = delete_file(lock_file);
#endif
if (bufferp (lock_buffer))
{
setbuf(lock_buffer);
set_buffer_modified_flag (0);
delbuf (lock_buffer);
}
return 0;
}
define rmail_quit_rmail()
{
rmail_exit_folder();
% This puts us at the root again so save and get out.
rmail_save_buffer_secure();
delbuf(Rmail_Root_Buffer);
() = rmail_check_lock_file(0); % deletes the lock file.
}
define rmail ()
{
variable n;
!if (rmail_check_lock_file(1)) return;
rmail_save_folder(Rmail_Folder_Name, 1);
Rmail_Folder_Buffer = Null_String;
rmail_folder_mode();
n = rmail_get_newmail();
if (n)
{
flush(sprintf("%d new messages.", n));
rmail_select_folder(Rmail_Newmail_Folder);
}
}
|