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
|
/*
* ratStdFolder.c --
*
* This file contains code which implements standard c-client folders.
* This means ONLY filefolders at this moment.
*
* TkRat software and its included text is Copyright 1996,1997,1998
* by Martin Forssn
*
* Postilion software and its included text and images
* Copyright (C) 1998 Nic Bernstein
*
* The full text of the legal notices is contained in the files called
* COPYING and COPYRIGHT.TkRat, included with this distribution.
*
* 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
* of the License, 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "ratStdFolder.h"
#include <tcl.h>
/*
* This is the private part of a std folder info structure.
*/
typedef struct StdFolderInfo {
MAILSTREAM *stream; /* Handler to c-client entity */
int referenceCount; /* Number of entities referencing ths entry */
int exists; /* Number of messages which actually exists
in this folder */
int isNotYet; /* True if the mailbox file doesn't exist yet */
char *origName; /* The original name of this folder */
RatStdFolderType type; /* The exact type of this folder */
char *host; /* The host it resides on (if networked) */
char *user; /* The username (if networked) */
} StdFolderInfo;
/*
* We use this structure to keep a list of open connections
*/
typedef struct Connection {
MAILSTREAM *stream; /* Handler to c-client entity */
RatStdFolderType type; /* Type of folder */
char *host; /* Host we are connected to */
char *user; /* User we are connected as */
int closing; /* True if this connection isunused and
waiting to be closed */
Tcl_TimerToken token; /* Timer token for closing timer */
struct Connection *next; /* Struct linkage */
} Connection;
/*
* Use this list to remember passwords
*/
typedef struct MMPasswd {
char *host;
char *user;
char *password;
RatStdFolderType type;
struct MMPasswd *next;
Tcl_TimerToken token;
} MMPasswd;
/*
* Remember if we must initialize the package
*/
static int initialize = 1;
/*
* List of open connections
*/
static Connection *connListPtr = NULL;
/*
* List of remembered passwords
*/
static MMPasswd *passwdListPtr = NULL;
static MMPasswd *passwdCandidate = NULL;
/*
* Interpreter used for notifying the user (calls to RatLog).
*/
static Tcl_Interp *stdInterp;
/*
* Global table of folders.
*/
static Tcl_HashTable folderTable;
/*
* The values below are used to catch calls to mm_log. That is when you
* want to handle the message internally.
*/
RatLogLevel logLevel;
char *logMessage = NULL;
/*
* These variables are used by mm_login
*/
static char *loginUser = NULL;
/*
* List of flag names
*/
static char *stdFlagNames[] = {
RAT_SEEN_STR,
RAT_DELETED_STR,
RAT_FLAGGED_STR,
RAT_ANSWERED_STR
};
/*
* The types of folders. This list is closely related to RatStdFolderType
* in ratStdFolder.h
*/
static char *ratStdTypeNames[] = {"berkely", "imap", "pop3", "mh"};
/*
* This is used to build a list of found mailboxes when listing
*/
typedef struct Mailbox {
char *name; /* The nameof this mailbox */
char *spec; /* The specificaation to send to c-client */
long attributes; /* The attrubutes from c-client */
struct Mailbox *children; /* Pointer to the list of children */
struct Mailbox *next; /* Pointer to the next mailbox on this level */
} Mailbox;
static Mailbox *mailboxListPtr = NULL;
static void BuildIMAPList(Mailbox *mPtr, Tcl_DString *dsptr);
/*
* Procedures private to this module.
*/
static RatCloseProc Std_CloseProc;
static RatUpdateProc Std_UpdateProc;
static RatInsertProc Std_InsertProc;
static RatSetFlagProc Std_SetFlagProc;
static RatGetFlagProc Std_GetFlagProc;
static RatInfoProc Std_InfoProc;
static RatCreateProc Std_CreateProc;
static MAILSTREAM *OpenAndConvert(Tcl_Interp *interp, RatStdFolderType type,
char *host, char *name, char *user);
static Tcl_CmdProc StdListIMAPFolders;
static Tcl_TimerProc CloseConnection;
static Tcl_TimerProc ErasePasswd;
static Tcl_CmdProc StdJudgeFolder;
/*
*----------------------------------------------------------------------
*
* RatStdFolderInit --
*
* Initializes the file folder command.
*
* Results:
* The return value is normally TCL_OK; if something goes wrong
* TCL_ERROR is returned and an error message will be left in
* interp->result.
*
* Side effects:
* The C-client library is initialized and the apropriate mail drivers
* are linked.
*
*
*----------------------------------------------------------------------
*/
int
RatStdFolderInit(Tcl_Interp *interp)
{
stdInterp = interp;
Tcl_InitHashTable(&folderTable, TCL_ONE_WORD_KEYS);
mail_link(&unixdriver);
mail_link(&mmdfdriver);
mail_link(&dummydriver);
mail_link(&imapdriver);
mail_link(&pop3driver);
mail_link(&mhdriver);
auth_link(&auth_log);
Tcl_CreateCommand(interp, "RatListIMAP", StdListIMAPFolders, NULL, NULL);
Tcl_CreateCommand(interp, "RatImapCreateFolder", StdJudgeFolder,
(ClientData)1, NULL);
Tcl_CreateCommand(interp, "RatImapDeleteFolder", StdJudgeFolder,
(ClientData)0, NULL);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* OpenAndConvert --
*
* Opens a standard c-client folder and if it is a filefolder and
* is of an incompatible format (unfortunately generated by an older
* version of this program) we convert it.
*
* Results:
* The mail stream.
*
* Side effects:
* None.
*
*
*----------------------------------------------------------------------
*/
static MAILSTREAM*
OpenAndConvert(Tcl_Interp *interp, RatStdFolderType type, char *host,
char *name, char *user)
{
MAILSTREAM *stream = NULL;
Connection *connPtr;
int doCache = 0;
if ('{' == name[0]) {
for (connPtr = connListPtr; connPtr; connPtr = connPtr->next) {
if (connPtr->closing && !strcmp(host, connPtr->host)
&& !strcmp(connPtr->user, user)) {
break;
}
}
if (connPtr) {
stream = connPtr->stream;
if (connPtr->token) {
Tcl_DeleteTimerHandler(connPtr->token);
}
connPtr->closing = 0;
} else {
Tcl_GetBoolean(stdInterp,
Tcl_GetVar2(stdInterp, "option", "cache_passwd",
TCL_GLOBAL_ONLY), &doCache);
if (doCache) {
passwdCandidate = (MMPasswd*)malloc(sizeof(MMPasswd)+
strlen(host)+1);
passwdCandidate->host = (char*)passwdCandidate+sizeof(MMPasswd);
strcpy(passwdCandidate->host, host);
passwdCandidate->type = type;
passwdCandidate->password = NULL;
}
}
}
stream = mail_open(stream, name, NIL);
if (NIL == stream && name[0] == '/') {
FILE *fpIn = NULL, *fpOut;
struct stat statbuf;
char tmp[1024], buf[1024];
char *re = "^From ([^ @]+)(@[^ @]+)? (Mon|Tue|Wed|Thu|Fri|Sat|Sun), ([0-9]+) (Jan|Feb|Mar|Jun|Jul|Aug|Sep|Okt|Nov|Dec) (19[0-9][0-9]) ([0-9][0-9]:[0-9][0-9]:[0-9][0-9]) ([-+0-9]*)\n";
char *startPtr, *endPtr, c;
Tcl_RegExp regexp;
int converted = 0;
sprintf(tmp, "%s.tmp", name);
if ((fpIn = fopen(name, "r"))
&& !fstat(fileno(fpIn), &statbuf)
&& S_ISREG(statbuf.st_mode)
&& statbuf.st_size > 0
&& stat(tmp, &statbuf)
&& (fpOut = fopen(tmp, "w"))) {
regexp = Tcl_RegExpCompile(interp, re);
while (fgets(buf, sizeof(buf), fpIn), !feof(fpIn)) {
if (Tcl_RegExpExec(interp, regexp, buf, buf)) {
converted = 1;
Tcl_RegExpRange(regexp, 1, &startPtr, &endPtr);
c = *endPtr; *endPtr = '\0';
fprintf(fpOut, "From %s", startPtr);
*endPtr = c;
Tcl_RegExpRange(regexp, 2, &startPtr, &endPtr);
if (startPtr) {
*endPtr = '\0';
fprintf(fpOut, startPtr);
}
Tcl_RegExpRange(regexp, 3, &startPtr, &endPtr);
*endPtr = '\0';
fprintf(fpOut, " %s", startPtr);
Tcl_RegExpRange(regexp, 5, &startPtr, &endPtr);
*endPtr = '\0';
fprintf(fpOut, " %s", startPtr);
Tcl_RegExpRange(regexp, 4, &startPtr, &endPtr);
*endPtr = '\0';
fprintf(fpOut, " %s", startPtr);
Tcl_RegExpRange(regexp, 7, &startPtr, &endPtr);
*endPtr = '\0';
fprintf(fpOut, " %s", startPtr);
Tcl_RegExpRange(regexp, 6, &startPtr, &endPtr);
*endPtr = '\0';
fprintf(fpOut, " %s", startPtr);
Tcl_RegExpRange(regexp, 8, &startPtr, &endPtr);
*endPtr = '\0';
fprintf(fpOut, " %s\n", startPtr);
} else {
if (!converted) {
break;
}
fputs(buf, fpOut);
}
}
fclose(fpIn);
if (0 == fclose(fpOut) && converted) {
logLevel = RAT_BABBLE;
rename(tmp, name);
stream = mail_open(NULL, name, 0);
}
} else {
if (fpIn) {
fclose(fpIn);
}
}
}
if (RAT_IMAP == type && NIL != stream) {
Tcl_GetBoolean(stdInterp,
Tcl_GetVar2(stdInterp, "option", "cache_conn",
TCL_GLOBAL_ONLY), &doCache);
if (doCache) {
connPtr = (Connection*)malloc(sizeof(Connection));
connPtr->stream = stream;
connPtr->type = type;
connPtr->host = cpystr(host);
connPtr->user = cpystr(user);
connPtr->closing = 0;
connPtr->next = connListPtr;
connListPtr = connPtr;
}
}
if (passwdCandidate) {
if (passwdCandidate->password && NIL != stream) {
int timeout;
passwdCandidate->next = passwdListPtr;
passwdListPtr = passwdCandidate;
Tcl_GetInt(interp,
Tcl_GetVar2(interp, "option", "cache_passwd_timeout",
TCL_GLOBAL_ONLY), &timeout);
if (timeout) {
passwdCandidate->token = Tcl_CreateTimerHandler(timeout*1000,
ErasePasswd, (ClientData)passwdCandidate);
} else {
passwdCandidate->token = NULL;
}
} else {
free(passwdCandidate);
}
passwdCandidate = NULL;
}
return stream;
}
/*
*----------------------------------------------------------------------
*
* OpenStdFolder --
*
* Opens a standard c-client folder and if it is a filefolder and
* is of an incompatible format (unfortunately generated by an older
* version of this program) we convert it.
*
* Results:
* The mail stream.
*
* Side effects:
* None.
*
*
*----------------------------------------------------------------------
*/
MAILSTREAM*
OpenStdFolder(Tcl_Interp *interp, char *name, char *prot, char *user,
void *voidPtr)
{
MAILSTREAM *stream = NULL;
RatStdFolderType type;
StdFolderInfo *stdPtr = (StdFolderInfo*)voidPtr;
char *host, *cPtr;
struct stat sbuf;
int i, isNotYet = 0;
loginUser = user;
if ( '{' == name[0] ) {
for (i=1; '}' != name[i] && '/' != name[i]; i++);
host = (char*)malloc(i);
strncpy(host, &name[1], i-1);
host[i-1] = '\0';
if (!strcasecmp(prot, "pop3")) {
type = RAT_POP;
} else {
type = RAT_IMAP;
}
} else {
host = NULL;
if ('#' == name[0]) {
type = RAT_MH;
} else {
type = RAT_BERKELY;
}
}
if ('/' == name[0] && stat(name, &sbuf) && ENOENT == errno) {
/*
* This entry points to a file which doesn't exist. Check if the
* directory exists and in that case we accept.
*/
for (cPtr = name+strlen(name); '/' != *cPtr; cPtr--);
*cPtr = '\0';
if (stat(name, &sbuf) || !S_ISDIR(sbuf.st_mode)) {
*cPtr = '/';
Tcl_AppendResult(interp, "Failed to open std mailbox \"",
name, "\"", (char *) NULL);
return NULL;
}
isNotYet = 1;
*cPtr = '/';
} else {
logLevel = RAT_BABBLE;
stream = OpenAndConvert(interp, type, host, name, user);
if (logLevel > RAT_WARN) {
if (host) {
free(host);
}
Tcl_SetResult(interp, logMessage, TCL_VOLATILE);
return NULL;
}
if (NIL == stream) {
if (host) {
free(host);
}
Tcl_AppendResult(interp, "Failed to open std mailbox \"",
name, "\"", (char *) NULL);
return NULL;
}
}
if (stdPtr) {
stdPtr->stream = stream;
stdPtr->referenceCount = 1;
stdPtr->exists = isNotYet ? 0 : stream->nmsgs;
stdPtr->isNotYet = isNotYet;
stdPtr->origName = cpystr(name);
stdPtr->type = type;
stdPtr->host = host;
stdPtr->user = loginUser ? cpystr(loginUser) : NULL;
} else if (host && *host) {
free(host);
}
return stream;
}
/*
*----------------------------------------------------------------------
*
* RatStdFolderSetSortMethod --
*
* Sets, clears, or resets the folder's sort method.
*
* Results:
* Nothing.
*
* Side effects:
* The folder's sort method is changed.
*
*
*----------------------------------------------------------------------
*/
void
RatStdFolderSetSortMethod(RatFolderInfo *infoPtr, char *sort_method)
{
/*
* Free any existing sortMethod, so we don't leak memory.
*/
if (infoPtr->sortMethod != NULL)
ckfree(infoPtr->sortMethod);
/*
* Allow both NULL and the empty string to mean, a null setting.
*/
if (sort_method != NULL && *sort_method)
infoPtr->sortMethod = cpystr(sort_method);
else
infoPtr->sortMethod = NULL;
}
/*
*----------------------------------------------------------------------
*
* RatStdFolderCreate --
*
* Creates a std folder entity.
*
* Results:
* The return value is normally TCL_OK; if something goes wrong
* TCL_ERROR is returned and an error message will be left in
* interp->result.
*
* Side effects:
* A std folder is created.
*
*
*----------------------------------------------------------------------
*/
RatFolderInfo*
RatStdFolderCreate(Tcl_Interp *interp, int argc, char *argv[])
{
Tcl_HashEntry *entryPtr;
RatFolderInfo *infoPtr;
StdFolderInfo *stdPtr;
MAILSTREAM *stream = NULL;
int new;
char *filename;
char *sort_method = NULL;
char *user = NULL;
char *protocol = NULL;
/*
* Now it is time to initialize things
*/
if (initialize) {
env_parameters(SET_LOCALHOST, currentHost);
initialize = 0;
}
if (argc != 4 && argc != 6) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" ", argv[1], " mailbox-filename sort-method [user prot]\"",
(char *) NULL);
return (RatFolderInfo *) NULL;
}
stdPtr = (StdFolderInfo *) ckalloc(sizeof(*stdPtr));
filename = argv[2];
sort_method = argv[3];
if (6 == argc) {
user = argv[4];
protocol = argv[5];
}
stream = OpenStdFolder(interp, filename, protocol, user, stdPtr);
if (!stream && !stdPtr->isNotYet) {
free(stdPtr);
return NULL;
}
infoPtr = (RatFolderInfo *) ckalloc(sizeof(*infoPtr));
infoPtr->name = cpystr(argv[2]);
infoPtr->type = "std";
infoPtr->size = -1;
/*
* Make sure sortMethod is initialised properly. (I don't know
* if ckalloc() zeroes the memory it has allocated.)
*/
infoPtr->sortMethod = NULL;
RatStdFolderSetSortMethod(infoPtr, sort_method);
if (stdPtr->isNotYet) {
infoPtr->number = 0;
} else {
infoPtr->number = stream->nmsgs;
entryPtr = Tcl_CreateHashEntry(&folderTable, (char*)stream, &new);
Tcl_SetHashValue(entryPtr, (ClientData) stdPtr);
}
infoPtr->closeProc = Std_CloseProc;
infoPtr->updateProc = Std_UpdateProc;
infoPtr->insertProc = Std_InsertProc;
infoPtr->setFlagProc = Std_SetFlagProc;
infoPtr->getFlagProc = Std_GetFlagProc;
infoPtr->infoProc = Std_InfoProc;
infoPtr->createProc = Std_CreateProc;
infoPtr->private = (ClientData) stdPtr;
loginUser = NULL;
return infoPtr;
}
/*
*----------------------------------------------------------------------
*
* Std_CloseProc --
*
* See the documentation for closeProc in ratFolder.h
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the documentation for closeProc in ratFolder.h
*
*
*----------------------------------------------------------------------
*/
void
CloseStdFolder(Tcl_Interp *interp, MAILSTREAM *stream)
{
Connection **connPtrPtr;
for (connPtrPtr = &connListPtr;
*connPtrPtr && stream != (*connPtrPtr)->stream;
connPtrPtr = &(*connPtrPtr)->next);
if (*connPtrPtr) {
int timeout;
Tcl_GetInt(interp, Tcl_GetVar2(interp, "option", "cache_conn_timeout",
TCL_GLOBAL_ONLY), &timeout);
(*connPtrPtr)->closing = 1;
if (timeout) {
(*connPtrPtr)->token = Tcl_CreateTimerHandler(timeout*1000,
CloseConnection, (ClientData)*connPtrPtr);
} else {
(*connPtrPtr)->token = NULL;
}
} else {
mail_close_full(stream, NIL);
}
}
/*
*----------------------------------------------------------------------
*
* Std_CloseProc --
*
* See the documentation for closeProc in ratFolder.h
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the documentation for closeProc in ratFolder.h
*
*
*----------------------------------------------------------------------
*/
static int
Std_CloseProc(RatFolderInfoPtr infoPtr, Tcl_Interp *interp)
{
StdFolderInfo *stdPtr = (StdFolderInfo *) infoPtr->private;
int expunge;
if (stdPtr->isNotYet) {
/* LK: Why doesn't this free stdPtr->origName etc.? */
ckfree(stdPtr);
infoPtr->private = NULL;
return TCL_OK;
}
Tcl_GetBoolean(interp,
Tcl_GetVar2(interp, "option", "expunge_on_close", TCL_GLOBAL_ONLY),
&expunge);
if (expunge) {
mail_expunge(stdPtr->stream);
}
CloseStdFolder(interp, stdPtr->stream);
if (0 == --stdPtr->referenceCount) {
Tcl_HashEntry *entryPtr;
if ((entryPtr=Tcl_FindHashEntry(&folderTable, (char*)stdPtr->stream))) {
Tcl_DeleteHashEntry(entryPtr);
}
if (stdPtr->host) {
ckfree(stdPtr->host);
ckfree(stdPtr->user);
}
ckfree(stdPtr->origName);
ckfree(stdPtr);
infoPtr->private = NULL;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Std_UpdateProc --
*
* See the documentation for updateProc in ratFolder.h
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the documentation for updateProc in ratFolder.h
*
*
*----------------------------------------------------------------------
*/
static int
Std_UpdateProc(RatFolderInfoPtr infoPtr, Tcl_Interp *interp, int expunge)
{
StdFolderInfo *stdPtr = (StdFolderInfo *) infoPtr->private;
if (stdPtr->isNotYet) {
Tcl_HashEntry *entryPtr;
struct stat sbuf;
int new;
if (stat(stdPtr->origName, &sbuf)) {
return 0;
}
logLevel = RAT_BABBLE;
stdPtr->stream = OpenAndConvert(interp, stdPtr->type, stdPtr->host,
stdPtr->origName, NULL);
if (logLevel > RAT_WARN) {
Tcl_SetResult(interp, logMessage, TCL_VOLATILE);
return 0;
}
if (NIL == stdPtr->stream) {
Tcl_AppendResult(interp, "Failed to open std mailbox \"",
stdPtr->origName, "\"", (char *) NULL);
return 0;
}
stdPtr->exists = stdPtr->stream->nmsgs;
entryPtr = Tcl_CreateHashEntry(&folderTable,(char*)stdPtr->stream,&new);
Tcl_SetHashValue(entryPtr, (ClientData) stdPtr);
stdPtr->isNotYet = 0;
} else {
if (expunge) {
MESSAGECACHE *cachePtr;
char sequence[16];
int i, offset = 0;
if (infoPtr->number) {
sprintf(sequence, "1:%d", infoPtr->number);
mail_fetchfast_full(stdPtr->stream, sequence, NIL);
for (i=0; i<infoPtr->number; i++) {
cachePtr = mail_elt(stdPtr->stream, i+1);
if (cachePtr->deleted) {
if (-1 != infoPtr->size) {
infoPtr->size -= cachePtr->rfc822_size;
}
if (infoPtr->msgCmdPtr[i]) {
RatMessageDelete(interp, infoPtr->msgCmdPtr[i]);
}
offset++;
} else if (offset) {
infoPtr->msgCmdPtr[i-offset] = infoPtr->msgCmdPtr[i];
infoPtr->privatePtr[i-offset] = infoPtr->privatePtr[i];
if (infoPtr->msgCmdPtr[i]) {
RatMessageSetIndex(interp, infoPtr->msgCmdPtr[i],
i - offset);
}
}
}
for (i=infoPtr->number-offset; i<infoPtr->number; i++) {
infoPtr->msgCmdPtr[i] = NULL;
infoPtr->privatePtr[i] = NULL;
}
}
mail_expunge(stdPtr->stream);
infoPtr->number = stdPtr->exists;
} else {
if (T != mail_ping(stdPtr->stream)) {
Tcl_SetResult(interp, "Mailbox stream died, closing browser.",
TCL_STATIC);
return -2;
}
}
}
return stdPtr->exists;
}
/*
*----------------------------------------------------------------------
*
* Std_InsertProc --
*
* See the documentation for insertProc in ratFolder.h
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the documentation for insertProc in ratFolder.h
*
*
*----------------------------------------------------------------------
*/
static int
Std_InsertProc(RatFolderInfoPtr infoPtr, Tcl_Interp *interp, int argc,
char *argv[])
{
StdFolderInfo *stdPtr = (StdFolderInfo *) infoPtr->private;
char flags[128], date[128];
Tcl_CmdInfo cmdInfo;
Tcl_DString ds;
STRING string;
int i;
if (stdPtr->isNotYet) {
Tcl_HashEntry *entryPtr;
int new, perm;
Tcl_GetInt(interp, Tcl_GetVar2(interp, "option", "permissions",
TCL_GLOBAL_ONLY), &perm);
close(open(stdPtr->origName, O_CREAT, perm));
logLevel = RAT_BABBLE;
stdPtr->stream = OpenAndConvert(interp, stdPtr->type, stdPtr->host,
stdPtr->origName, NULL);
if (logLevel > RAT_WARN) {
Tcl_SetResult(interp, logMessage, TCL_VOLATILE);
return TCL_ERROR;
}
if (NIL == stdPtr->stream) {
Tcl_AppendResult(interp, "Failed to open std mailbox \"",
argv[2], "\"", (char *) NULL);
return TCL_ERROR;
}
stdPtr->exists = stdPtr->stream->nmsgs;
entryPtr = Tcl_CreateHashEntry(&folderTable,(char*)stdPtr->stream,&new);
Tcl_SetHashValue(entryPtr, (ClientData) stdPtr);
stdPtr->isNotYet = 0;
}
Tcl_DStringInit(&ds);
for (i=0; i<argc; i++) {
Tcl_GetCommandInfo(interp, argv[i], &cmdInfo);
RatMessageGet(interp, (MessageInfo*)cmdInfo.clientData, &ds,flags,date);
INIT(&string,mail_string,Tcl_DStringValue(&ds),Tcl_DStringLength(&ds));
if (!mail_append_full(stdPtr->stream, stdPtr->origName, flags, date,
&string)){
fprintf(stderr, "mail_append failed\n");
}
Tcl_DStringSetLength(&ds, 0);
if (!stdPtr->exists) {
mail_ping(stdPtr->stream);
}
}
Tcl_DStringFree(&ds);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Std_SetFlagProc --
*
* See the documentation for setFlagProc in ratFolder.h
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the documentation for setFlagProc in ratFolder.h
*
*
*----------------------------------------------------------------------
*/
static int
Std_SetFlagProc(RatFolderInfoPtr infoPtr, Tcl_Interp *interp, int index,
RatFlag flag, int value)
{
StdFolderInfo *stdPtr = (StdFolderInfo *) infoPtr->private;
char sequence[8];
if (stdPtr->stream->rdonly) {
return TCL_OK;
}
sprintf(sequence, "%d", index+1);
if (value) {
mail_setflag_full(stdPtr->stream, sequence, stdFlagNames[flag], NIL);
} else {
mail_clearflag_full(stdPtr->stream, sequence, stdFlagNames[flag], NIL);
}
if (logLevel > RAT_WARN) {
Tcl_SetResult(interp, logMessage, TCL_VOLATILE);
return TCL_ERROR;
} else {
return TCL_OK;
}
}
/*
*----------------------------------------------------------------------
*
* Std_GetFlagProc --
*
* See the documentation for getFlagProc in ratFolder.h
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the documentation for getFlagProc in ratFolder.h
*
*
*----------------------------------------------------------------------
*/
static int
Std_GetFlagProc(RatFolderInfoPtr infoPtr, Tcl_Interp *interp, int index,
RatFlag flag)
{
StdFolderInfo *stdPtr = (StdFolderInfo *) infoPtr->private;
MESSAGECACHE *cachePtr;
char sequence[8];
int value = 0;
sprintf(sequence, "%d", index+1);
logLevel = RAT_BABBLE;
(void)mail_fetchstructure_full(stdPtr->stream, index+1, NIL, NIL);
cachePtr = mail_elt(stdPtr->stream, index+1);
switch (flag) {
case RAT_SEEN: value = cachePtr->seen; break;
case RAT_DELETED: value = cachePtr->deleted; break;
case RAT_FLAGGED: value = cachePtr->flagged; break;
case RAT_ANSWERED: value = cachePtr->answered; break;
}
return value;
}
/*
*----------------------------------------------------------------------
*
* Std_InfoProc --
*
* See the documentation for infoProc in ratFolder.h
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the documentation for infoProc in ratFolder.h
*
*
*----------------------------------------------------------------------
*/
static char*
Std_InfoProc(Tcl_Interp *interp, ClientData clientData, RatFolderInfoType type,
int index)
{
RatFolderInfo *infoPtr = (RatFolderInfo*)clientData;
int i = infoPtr->presentationOrder[index];
if (NULL == infoPtr->privatePtr[i]) {
infoPtr->msgCmdPtr[i] = Std_CreateProc(infoPtr, interp, i);
}
return Std_GetInfoProc(interp, (ClientData)infoPtr->privatePtr[i], type, 0);
}
/*
*----------------------------------------------------------------------
*
* Std_CreateProc --
*
* See the documentation for createProc in ratFolder.h
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the documentation for createProc in ratFolder.h
*
*
*----------------------------------------------------------------------
*/
static char*
Std_CreateProc(RatFolderInfoPtr infoPtr, Tcl_Interp *interp, int index)
{
StdFolderInfo *stdPtr = (StdFolderInfo *) infoPtr->private;
return RatStdMessageCreate(interp, infoPtr, stdPtr->stream, index,
stdPtr->type, stdPtr->host, stdPtr->user);
}
/*
*----------------------------------------------------------------------
*
* StdListIMAPFolders --
*
* List the IMAP folders on an remote server
*
* Results:
* The folders found are returned as a list in interp->result;
*
* Side effects:
* RatLogin may be called.
*
*
*----------------------------------------------------------------------
*/
static int
StdListIMAPFolders(ClientData dummy, Tcl_Interp *interp, int argc, char *argv[])
{
Connection *connPtr;
MAILSTREAM *stream = NIL;
int doClose = 0;
Tcl_DString ds;
char *spec;
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" host user pattern\"", (char *) NULL);
return TCL_ERROR;
}
spec = (char*)malloc(strlen(argv[1])+strlen(argv[3])+3);
/*
* Find any open stream that matches, if not then we have to open one.
*/
for (connPtr = connListPtr; !stream && connPtr; connPtr = connPtr->next) {
if (!strcmp(connPtr->host, argv[1]) && !strcmp(connPtr->user, argv[2])){
stream = connPtr->stream;
}
}
if (!stream) {
loginUser = argv[2];
sprintf(spec, "{%s}", argv[1]);
stream = mail_open(NIL, spec, NIL);
loginUser = NULL;
doClose = 1;
}
if (!stream) {
Tcl_SetResult(interp, "Failed to open connection", TCL_STATIC);
return TCL_ERROR;
}
/*
* Do the listing
*/
sprintf(spec, "{%s}%s", argv[1], argv[3]);
mail_list(stream, NIL, spec);
free(spec);
if (doClose) {
mail_close_full(stream, NIL);
}
/*
* Build the return string
*/
Tcl_DStringInit(&ds);
BuildIMAPList(mailboxListPtr, &ds);
mailboxListPtr = NULL;
Tcl_DStringResult(interp, &ds);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* BuildIMAPList --
*
* This function calls itself recursively to build the list of
* folders found.
*
* Results:
* None.
*
* Side effects:
* The given dstring is modified.
*
*
*----------------------------------------------------------------------
*/
static void
BuildIMAPList(Mailbox *mPtr, Tcl_DString *dsPtr)
{
Mailbox *nPtr;
for (; mPtr; mPtr = nPtr) {
Tcl_DStringStartSublist(dsPtr);
Tcl_DStringAppendElement(dsPtr, mPtr->name);
if (mPtr->children) {
Tcl_DStringAppendElement(dsPtr, "0");
Tcl_DStringStartSublist(dsPtr);
if (!(LATT_NOSELECT & mPtr->attributes)) {
Tcl_DStringStartSublist(dsPtr);
Tcl_DStringAppendElement(dsPtr, mPtr->name);
Tcl_DStringAppendElement(dsPtr, "1");
Tcl_DStringStartSublist(dsPtr);
Tcl_DStringAppendElement(dsPtr, mPtr->spec);
Tcl_DStringEndSublist(dsPtr);
Tcl_DStringEndSublist(dsPtr);
}
BuildIMAPList(mPtr->children, dsPtr);
Tcl_DStringEndSublist(dsPtr);
} else {
Tcl_DStringAppendElement(dsPtr, "1");
Tcl_DStringStartSublist(dsPtr);
Tcl_DStringAppendElement(dsPtr, mPtr->spec);
Tcl_DStringEndSublist(dsPtr);
}
Tcl_DStringEndSublist(dsPtr);
nPtr = mPtr->next;
free(mPtr->name);
if (mPtr->spec) {
free(mPtr->spec);
}
free(mPtr);
}
}
/*
*----------------------------------------------------------------------
*
* ErasePasswd --
*
* Removes a passwd from the cache
*
* Results:
* None.
*
* Side effects:
* The connection list is modified.
*
*
*----------------------------------------------------------------------
*/
static void
ErasePasswd(ClientData clientData)
{
MMPasswd **pwdPtrPtr, *pwdPtr = (MMPasswd*)clientData;
for (pwdPtrPtr = &passwdListPtr; *pwdPtrPtr != pwdPtr;
pwdPtrPtr = &(*pwdPtrPtr)->next);
*pwdPtrPtr = pwdPtr->next;
free(pwdPtr->user);
memset(pwdPtr->password, '\0', strlen(pwdPtr->password));
free(pwdPtr->password);
free(pwdPtr);
}
/*
*----------------------------------------------------------------------
*
* CloseConnection --
*
* Closes a connection.
*
* Results:
* None.
*
* Side effects:
* The connection list is modified.
*
*
*----------------------------------------------------------------------
*/
static void
CloseConnection(ClientData clientData)
{
Connection **connPtrPtr, *connPtr = (Connection*)clientData;
mail_close_full(connPtr->stream, NIL);
for (connPtrPtr = &connListPtr; *connPtrPtr != connPtr;
connPtrPtr = &(*connPtrPtr)->next);
*connPtrPtr = connPtr->next;
free(connPtr->host);
free(connPtr->user);
free(connPtr);
}
/*
*----------------------------------------------------------------------
*
* StdJudgeFolder --
*
* Create or delete c-client folders
*
* Results:
* A standard Tcl-result
*
* Side effects:
* A folder may be created or deleted
*
*
*----------------------------------------------------------------------
*/
static int
StdJudgeFolder(ClientData op, Tcl_Interp *interp, int argc, char *argv[])
{
MAILSTREAM *stream = NULL;
Connection *connPtr;
int doCachePw, doCacheConn;
char *host, *cPtr;
if (3 != argc) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" folder user\"", (char *) NULL);
return TCL_ERROR;
}
if ('{' != argv[1][0]) {
Tcl_SetResult(interp, "You can only use this on IMAP-folders",
TCL_STATIC);
return TCL_ERROR;
}
/*
* Look for reusable stream
*/
for (cPtr = host = cpystr(argv[1]);
*cPtr && '/' != *cPtr && ':' != *cPtr && '}' != *cPtr;
cPtr++);
*cPtr++ = '\0';
for (connPtr = connListPtr; connPtr; connPtr = connPtr->next) {
if (connPtr->closing && !strcmp(host, connPtr->host)
&& !strcmp(connPtr->user, argv[2])) {
break;
}
}
if (connPtr) {
stream = connPtr->stream;
if (connPtr->token) {
Tcl_DeleteTimerHandler(connPtr->token);
}
connPtr->closing = 0;
} else {
Tcl_GetBoolean(stdInterp,
Tcl_GetVar2(stdInterp, "option", "cache_passwd",
TCL_GLOBAL_ONLY), &doCachePw);
if (doCachePw) {
passwdCandidate = (MMPasswd*)malloc(sizeof(MMPasswd)+
strlen(host)+1);
passwdCandidate->host = (char*)passwdCandidate+sizeof(MMPasswd);
strcpy(passwdCandidate->host, host);
passwdCandidate->type = RAT_IMAP;
passwdCandidate->password = NULL;
}
}
/*
* Open stream (if needed)
*/
loginUser = argv[2];
if (!stream) {
if ( NULL == (stream = mail_open(NULL, argv[1], OP_HALFOPEN))) {
Tcl_SetResult(interp, "Failed to open stream", TCL_STATIC);
free(host);
if (passwdCandidate) {
free(passwdCandidate);
passwdCandidate = NULL;
}
return TCL_ERROR;
}
}
/*
* Do operation
*/
if (op) {
(void)mail_create(stream, argv[1]);
} else {
(void)mail_delete(stream, argv[1]);
}
Tcl_GetBoolean(stdInterp,
Tcl_GetVar2(stdInterp, "option", "cache_conn",
TCL_GLOBAL_ONLY), &doCacheConn);
if (doCacheConn) {
int timeout;
Tcl_GetInt(interp, Tcl_GetVar2(interp, "option", "cache_conn_timeout",
TCL_GLOBAL_ONLY), &timeout);
connPtr = (Connection*)malloc(sizeof(Connection));
connPtr->stream = stream;
connPtr->type = RAT_IMAP;
connPtr->host = cpystr(host);
connPtr->user = cpystr(loginUser);
connPtr->closing = 1;
connPtr->next = connListPtr;
if (timeout) {
connPtr->token = Tcl_CreateTimerHandler(timeout*1000,
CloseConnection, (ClientData)connPtr);
} else {
connPtr->token = NULL;
}
connListPtr = connPtr;
} else {
mail_close_full(stream, NIL);
}
if (passwdCandidate) {
if (passwdCandidate->password) {
int timeout;
passwdCandidate->next = passwdListPtr;
passwdListPtr = passwdCandidate;
Tcl_GetInt(interp,
Tcl_GetVar2(interp, "option", "cache_passwd_timeout",
TCL_GLOBAL_ONLY), &timeout);
if (timeout) {
passwdCandidate->token = Tcl_CreateTimerHandler(timeout*1000,
ErasePasswd, (ClientData)passwdCandidate);
} else {
passwdCandidate->token = NULL;
}
} else {
free(passwdCandidate);
}
passwdCandidate = NULL;
}
free(host);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* AppendToIMAP --
*
* Append the given message to an IMAP folder
*
* Results:
* None.
*
* Side effects:
* The specified folder will be modified
*
*
*----------------------------------------------------------------------
*/
void
AppendToIMAP(Tcl_Interp *interp, char *mailboxSpec, char *user, char *flags,
char *date, char *msg, int length)
{
char buf[1024], *mailbox, *crlfMsg;
Connection *connPtr;
MAILSTREAM *stream;
int i, doClose;
STRING msgString;
unsigned long crlfLength, buflen = length*1.05;
crlfMsg = (char*)malloc(buflen);
mailbox = RatLindex(interp, mailboxSpec, 0);
for (i=0; mailbox[i+1] != '}'; i++) {
buf[i] = mailbox[i+1];
}
buf[i] = '\0';
for (connPtr = connListPtr; connPtr; connPtr = connPtr->next) {
if (!strcmp(buf, connPtr->host) && !strcmp(connPtr->user, user)) {
break;
}
}
if (connPtr) {
stream = connPtr->stream;
doClose = 0;
} else {
loginUser = user;
stream = OpenAndConvert(interp, RAT_IMAP, buf, mailbox, user);
if (NULL == stream) {
return;
}
doClose = 1;
}
crlfLength = strcrlfcpy(&crlfMsg, &buflen, msg, length);
INIT(&msgString, mail_string, crlfMsg, crlfLength);
mail_append_full(stream, mailbox, flags, date, &msgString);
free(crlfMsg);
if (doClose) {
Connection **connPtrPtr;
for (connPtrPtr = &connListPtr;
*connPtrPtr && stream != (*connPtrPtr)->stream;
connPtrPtr = &(*connPtrPtr)->next);
if (*connPtrPtr) {
int timeout;
Tcl_GetInt(interp, Tcl_GetVar2(interp, "option",
"cache_conn_timeout", TCL_GLOBAL_ONLY), &timeout);
(*connPtrPtr)->closing = 1;
(*connPtrPtr)->token = Tcl_CreateTimerHandler(timeout*1000,
CloseConnection, (ClientData)*connPtrPtr);
} else {
mail_close_full(stream, NIL);
}
}
}
/*
*----------------------------------------------------------------------
*
* ClearStdPasswds --
*
* Clear the standard passwords from memory. If freethem is non
* null we also try to deallocate the memory.
*
* Results:
* None.
*
* Side effects:
* The specified folder will be modified
*
*
*----------------------------------------------------------------------
*/
void
ClearStdPasswds(int freethem)
{
MMPasswd *pwPtr, *nextPtr;
for (pwPtr = passwdListPtr; pwPtr; pwPtr = nextPtr) {
memset(pwPtr->password, '\0', strlen(pwPtr->password));
nextPtr = pwPtr->next;
if (freethem) {
free(pwPtr->user);
free(pwPtr->password);
free(pwPtr);
}
}
}
/*
*----------------------------------------------------------------------
*
* mm_*
*
* The functions below are called from the C-client library. They
* are docuemnted in Internal.DOC.
*
*----------------------------------------------------------------------
*/
void mm_searched (MAILSTREAM *stream,unsigned long number)
{
}
void mm_exists (MAILSTREAM *stream,unsigned long number)
{
Tcl_HashEntry *entryPtr;
StdFolderInfo *stdPtr;
if ((entryPtr = Tcl_FindHashEntry(&folderTable, (char*)stream))) {
stdPtr = (StdFolderInfo *)Tcl_GetHashValue(entryPtr);
stdPtr->exists = (int)number;
}
}
void mm_expunged (MAILSTREAM *stream,unsigned long number)
{
Tcl_HashEntry *entryPtr;
StdFolderInfo *stdPtr;
if ((entryPtr = Tcl_FindHashEntry(&folderTable, (char*)stream))) {
stdPtr = (StdFolderInfo *)Tcl_GetHashValue(entryPtr);
stdPtr->exists -= 1;
}
}
void mm_mailbox (char *string)
{
}
void mm_bboard (char *string)
{
}
void mm_notify (MAILSTREAM *stream,char *string,long errflg)
{
long flag = errflg;
if (flag == BYE) {
flag = NIL;
}
mm_log(string, flag);
}
void mm_log (char *string,long errflg)
{
switch(errflg) {
case NIL: logLevel = RAT_BABBLE; break;
case PARSE: logLevel = RAT_PARSE; break;
case WARN: logLevel = RAT_WARN; break;
case BYE: logLevel = RAT_FATAL; break;
case ERROR: /* fallthrough */
default: logLevel = RAT_ERROR; break;
}
if (logMessage) {
free(logMessage);
}
logMessage = cpystr(string);
RatLog(stdInterp, logLevel, string, 0);
if (logLevel > RAT_ERROR) {
fprintf(stderr, "c-client error. TkRat is aborting...\n");
RatDbClose();
exit(1);
}
}
void mm_dlog (char *string)
{
RatLog(stdInterp, RAT_BABBLE, string, 0);
}
void mm_login (NETMBX *mbPtr, char *user, char *pwd, long trial)
{
char **largv, buf[1024];
MMPasswd *pwPtr;
int largc, timeout;
Tcl_DString ds;
/*
* Check for cached entry
*/
sprintf(buf, "%s:%ld", mbPtr->host, mbPtr->port);
for (pwPtr = passwdListPtr; pwPtr; pwPtr = pwPtr->next) {
if ((!strcmp(pwPtr->host, mbPtr->host) || !strcmp(pwPtr->host, buf))
&& !strcmp(pwPtr->user, loginUser)
&& !strcasecmp(ratStdTypeNames[pwPtr->type], mbPtr->service)) {
strcpy(user, pwPtr->user);
strcpy(pwd, pwPtr->password);
if (pwPtr->token) {
Tcl_DeleteTimerHandler(pwPtr->token);
}
Tcl_GetInt(stdInterp,
Tcl_GetVar2(stdInterp, "option", "cache_passwd_timeout",
TCL_GLOBAL_ONLY), &timeout);
if (timeout) {
pwPtr->token = Tcl_CreateTimerHandler(timeout*1000,
ErasePasswd, (ClientData)pwPtr);
} else {
pwPtr->token = NULL;
}
return;
}
}
sprintf(buf, "%lx", trial);
Tcl_DStringInit(&ds);
Tcl_DStringAppendElement(&ds, "RatLogin");
Tcl_DStringAppendElement(&ds, mbPtr->host);
Tcl_DStringAppendElement(&ds, buf);
Tcl_DStringAppendElement(&ds, loginUser?loginUser:"");
Tcl_DStringAppendElement(&ds, mbPtr->service?mbPtr->service:"");
if (TCL_OK != Tcl_Eval(stdInterp, Tcl_DStringValue(&ds))
|| TCL_OK != Tcl_SplitList(stdInterp, stdInterp->result,
&largc,&largv)
|| 2 != largc) {
Tcl_DStringFree(&ds);
return;
}
strcpy(user, largv[0]);
loginUser = cpystr(largv[0]);
strcpy(pwd, largv[1]);
if (passwdCandidate) {
passwdCandidate->password = cpystr(largv[1]);
passwdCandidate->user = loginUser;
}
Tcl_DStringFree(&ds);
ckfree(largv);
}
void mm_critical (MAILSTREAM *stream)
{
}
void mm_nocritical (MAILSTREAM *stream)
{
}
long mm_diskerror (MAILSTREAM *stream,long errcode,long serious)
{
char buf[64];
sprintf(buf, "Disk error: %ld", errcode);
RatLog(stdInterp, RAT_FATAL, buf, 0);
return 1;
}
void mm_fatal (char *string)
{
RatLog(stdInterp, RAT_FATAL, string, 0);
}
void mm_flags (MAILSTREAM *stream,unsigned long number)
{
}
void
mm_list(MAILSTREAM *stream, int delimiter, char *name,long attributes)
{
Mailbox **mPtrPtr = &mailboxListPtr;
char buf[1024], *cPtr, *elem;
int found;
strcpy(buf, strchr(name, '}')+1);
/*
* Teoretically this code could fail to import a folder if
* it has the same name as a previous found structure. I do
* not think this can happen... (famous last words?)
*/
elem = buf;
if (delimiter) {
cPtr = strchr(buf, delimiter);
} else {
cPtr = NULL;
}
while (1) {
found = 0;
if (cPtr) {
*cPtr = '\0';
}
for (; *mPtrPtr; mPtrPtr = &(*mPtrPtr)->next) {
if (!strcmp((*mPtrPtr)->name, elem)) {
found = 1;
break;
}
}
if (!found) {
*mPtrPtr = (Mailbox*)malloc(sizeof(Mailbox));
(*mPtrPtr)->name = cpystr(elem);
if (cPtr) {
(*mPtrPtr)->attributes = LATT_NOSELECT;
(*mPtrPtr)->spec = NULL;
} else {
(*mPtrPtr)->attributes = attributes;
if (attributes & LATT_NOSELECT) {
(*mPtrPtr)->spec = NULL;
} else {
(*mPtrPtr)->spec = cpystr(name);
}
}
(*mPtrPtr)->children = NULL;
(*mPtrPtr)->next = NULL;
}
mPtrPtr = &(*mPtrPtr)->children;
if (!cPtr) {
break;
}
elem = cPtr+1;
cPtr = strchr(elem, delimiter);
}
}
void
mm_lsub (MAILSTREAM *stream, int delimiter, char *name, long attributes)
{
mm_list(stream, delimiter, name, attributes);
}
void mm_status (MAILSTREAM *stream, char *mailbox, MAILSTATUS *status)
{
}
|