home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B99.ZIP / POP.CPP < prev    next >
Text File  |  1999-01-29  |  63KB  |  2,126 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dos.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <ctype.h>
  9. #include <mem.h>
  10. #include <conio.h>
  11. #include <io.h>
  12. #include <share.h>
  13. #include <errno.h>
  14. #include <malloc.h>
  15. #include <dir.h>
  16. extern "C" {
  17. #include "tcp.h"
  18. }
  19.  
  20. #include "version.h"
  21. #include "retcode.h"
  22.  
  23. #undef MAXPATH
  24. #define MAXPATH 160
  25.  
  26. #define POP_PORT 110
  27.  
  28. #define SMTP_PORT 25
  29.  
  30. #define SMTP_STATUS   211
  31. #define SMTP_HELP     214
  32. #define SMTP_READY    220
  33. #define SMTP_BYE      221
  34. #define SMTP_OK       250
  35. #define SMTP_WILL_FWD 251
  36.  
  37. #define SMTP_GIMME    354
  38.  
  39. #define SMTP_OOPS     421
  40. #define SMTP_BUSY     450
  41. #define SMTP_ERROR    451
  42. #define SMTP_SQUEEZED 452
  43.  
  44. #define SMTP_SYNTAX   500
  45. #define SMTP_PARAM    501
  46. #define SMTP_COM_NI   502
  47. #define SMTP_BAD_SEQ  503
  48. #define SMTP_BAD_PARM 504
  49. #define SMTP_ACCESS   550
  50. #define SMTP_YOU_FWD  551
  51. #define SMTP_FULL     552
  52. #define SMTP_BAD_NAM  553
  53. #define SMTP_FAILED   554
  54.  
  55.  
  56. #define POP_OK               200
  57. #define POP_NOT_MSG          400
  58. #define POP_BAD_HOST         500
  59. #define POP_HOST_UNAVAILABLE 501
  60. #define POP_BAD_MBOX         510
  61. #define POP_BAD_PASS         511
  62. #define POP_UNKNOWN          599
  63.  
  64.  
  65. #define POPLIB_OK        200
  66. #define POPLIB_BAD_FILE  401
  67. #define POPLIB_BAD_HOST  510
  68. #define POPLIB_S_TIMEOU  510
  69. #define POPLIB_S_CLOSED  511
  70. #define POPLIB_SMTP_ERR  520
  71. #define POPLIB_POP_ERR   521
  72. #define POPLIB_SMTP_PROB 410
  73. #define POPLIB_POP_PROB  411
  74.  
  75. typedef struct {
  76.   tcp_Socket *sock;
  77. } Mail_Socket;
  78.  
  79. typedef struct {
  80.   char msgid[81];
  81. } Message_ID;
  82.  
  83. typedef struct {
  84.   char popname[40];
  85.   char pophost[60];
  86.   char poppass[40];
  87. } ACCT;
  88.  
  89. ACCT *acct;
  90.  
  91. extern unsigned _stklen = 10000U;
  92.  
  93. typedef struct {
  94.   int width;
  95.   int amount_per_square;
  96.   char square_list[10];
  97.   int empty_space, side_char1, side_char2;
  98.   long total_items, current_item;
  99.   long last_maj_pos, last_min_pos;
  100. } statusbarrec;
  101.  
  102. #define _TEMP_BUFFER_LEN 2048
  103. #define LAST(s) s[strlen(s)-1]
  104.  
  105. #define SHARE_LEVEL 10
  106. #define WAIT_TIME 10
  107. #define TRIES 100
  108.  
  109. struct ts_os_ver {
  110.   int maj;
  111.   int min;
  112. };
  113.  
  114. #define DOS     0
  115. #define OS2     1
  116. #define DV      2
  117. #define WINS    3
  118. #define WIN3    4
  119.  
  120. #define MT_DOS  0x01
  121. #define MT_OS2  0x02
  122. #define MT_DV   0x04
  123. #define MT_WINS 0x08
  124. #define MT_WIN3 0x10
  125.  
  126. struct ts_os_ver t_os_ver[5];
  127. int t_os_type;
  128. int t_os;
  129. char t_os_name[41];
  130.  
  131. #define free_Mail_Socket(SOCK) if (SOCK != NULL) {                              \
  132.   farfree(SOCK->sock); farfree(SOCK); SOCK=NULL; }
  133.  
  134. int POP_Err_Cond, SMTP_Err_Cond;
  135. char from_user[81], netdata[161], net_pkt[21], maindir[160], fdlfn[21], id[81];
  136. char LISTNAME[45], MAILFROM[60], PROXY[40], listaddr[25];
  137. char POPHOST[60], POPNAME[40], POPPASS[20], DOMAIN[60], NODEPASS[20];
  138. int WatTCP_initialized = 0, fdl;
  139. char _temp_buffer[_TEMP_BUFFER_LEN];
  140. static int POP_stat, SMTP_stat;
  141. int aborted, DEBUG = 1, ALLMAIL, compact_ids = 0, SKIP;
  142.  
  143. char *version = "Freeware PPP Project POP/SMTP Client " VERSION;
  144.  
  145. char pktowner[36];
  146.  
  147. #define SOCK_READ_ERR(PROTOCOL, ACTION)                                         \
  148.   sock_err:                                                                     \
  149.     switch (PROTOCOL##_stat) {                                                  \
  150.       case 1 :                                                                  \
  151.         PROTOCOL##_Err_Cond = PROTOCOL##_OK;                                    \
  152.         fprintf(stderr, "\n ! "#PROTOCOL"> Session error : %s",                        \
  153.             sockerr(PROTOCOL##_sock->sock));                                    \
  154.         ACTION;                                                                 \
  155.         aborted = 1;                                                            \
  156.         exit(EXIT_SUCCESS);                                                     \
  157.         return 0;                                                               \
  158.       case -1:                                                                  \
  159.         PROTOCOL##_Err_Cond = PROTOCOL##_OK;                                    \
  160.         fprintf(stderr, "\n ! "#PROTOCOL"> Timeout : %s",                              \
  161.                 sockerr(PROTOCOL##_sock->sock));                                \
  162.         ACTION;                                                                 \
  163.         aborted = 1;                                                            \
  164.         exit(EXIT_SUCCESS);                                                     \
  165.         return 0;                                                               \
  166.     }
  167.  
  168. #define SOCK_GETS(PROTOCOL)                                                     \
  169.   sock_wait_input(PROTOCOL##_sock->sock, sock_delay, NULL, &PROTOCOL##_stat);   \
  170.   sock_gets(PROTOCOL##_sock->sock, _temp_buffer, sizeof(_temp_buffer));         \
  171.   if (DEBUG) fprintf(stderr, "\n"#PROTOCOL"> %s\n", _temp_buffer);                  \
  172.   PROTOCOL##_Err_Cond = atoi(_temp_buffer);                                     \
  173.  
  174. #define SMTP_FAIL_ON(NUM, ACTION)                                               \
  175.   if (SMTP_Err_Cond == NUM) {                                                   \
  176.     if (DEBUG) fprintf(stderr, "\nSMTP Failure> '" #NUM "'\n");                   \
  177.     sock_puts(SMTP_sock->sock, "QUIT");                                         \
  178.     ACTION;                                                                     \
  179.     aborted = 1;                                                                \
  180.     return 0;                                                                   \
  181.   }
  182.  
  183. #define SMTP_RESET_ON(NUM, ACTION)                                              \
  184.   if (SMTP_Err_Cond == NUM) {                                                   \
  185.     if (DEBUG) fprintf(stderr, "\nSMTP Failure> '" #NUM "'\n");                   \
  186.     sock_puts(SMTP_sock->sock, "RSET");                                         \
  187.     sock_wait_input(SMTP_sock->sock, sock_delay, NULL, &SMTP_stat);             \
  188.     sock_gets(SMTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));             \
  189.     ACTION;                                                                     \
  190.     aborted = 1;                                                                \
  191.     return(0);                                                                  \
  192.   }
  193.  
  194. void output(char *fmt,...)
  195. {
  196.   va_list v;
  197.   char s[255];
  198.  
  199.   va_start(v, fmt);
  200.   vsprintf(s, fmt, v);
  201.   va_end(v);
  202.   fputs(s, stderr);
  203. }
  204.  
  205. void go_back(int from, int to)
  206. {
  207.   int i;
  208.  
  209.   for (i = from; i > to; i--)
  210.     output("\b \b");
  211. }
  212.  
  213. int detect_multitask(void)
  214. {
  215.   union REGS t_regs;
  216.  
  217.   t_os_type = 0;
  218.   t_os = 0;
  219.  
  220.   if (_osmajor < 10) {
  221.     t_os_ver[DOS].maj = _osmajor;
  222.     t_os_ver[DOS].min = _osminor;
  223.     t_os_type = t_os_type | MT_DOS;
  224.     strcpy(t_os_name, "DOS");
  225.   } else {
  226.     t_os_type = t_os_type | MT_OS2;
  227.     t_os_ver[OS2].maj = _osmajor / 10;
  228.     t_os_ver[OS2].min = _osminor;
  229.     if (t_os_ver[OS2].maj == 3) {
  230.       strcpy(t_os_name, "OS/2 Warp");
  231.     } else {
  232.       strcpy(t_os_name, "OS/2");
  233.     }
  234.   }
  235.  
  236.   t_regs.x.ax = 0x4680;
  237.   int86(0x2F, &t_regs, &t_regs);
  238.  
  239.   if (t_regs.x.ax == 0x0000) {
  240.     t_os_ver[WINS].maj = 3;
  241.     t_os_ver[WINS].min = 0;
  242.     t_os_type = t_os_type | MT_WINS;
  243.   } else {
  244.     t_regs.x.ax = 0x1600;
  245.     int86(0x2F, &t_regs, &t_regs);
  246.     switch (t_regs.h.al) {
  247.       case 0x00:
  248.       case 0x80:
  249.       case 0x01:
  250.       case 0xFF:
  251.         break;
  252.       default:
  253.         t_os_type = t_os_type | MT_WIN3;
  254.         t_os_ver[WIN3].maj = t_regs.h.al;
  255.         t_os_ver[WIN3].min = t_regs.h.ah;
  256.         if (t_os_ver[WIN3].maj == 4) {
  257.           strcpy(t_os_name, "Windows 95");
  258.           t_os_ver[WIN3].maj = t_os_ver[WIN3].maj - 3;
  259.         } else {
  260.           strcpy(t_os_name, "Windows");
  261.         }
  262.         break;
  263.     }
  264.   }
  265.  
  266.   t_regs.x.cx = 0x4445;
  267.   t_regs.x.dx = 0x5351;
  268.   t_regs.x.ax = 0x2B01;
  269.  
  270.   intdos(&t_regs, &t_regs);
  271.   if (t_regs.h.al != 0xFF) {
  272.     t_os_type = t_os_type | MT_DV;
  273.     t_os_ver[DV].maj = t_regs.h.bh;
  274.     t_os_ver[DV].min = t_regs.h.bl;
  275.     strcpy(t_os_name, "DESQview");
  276.   }
  277.   if (t_os_type & MT_DOS)
  278.     t_os = DOS;
  279.   if (t_os_type & MT_DV)
  280.     t_os = DV;
  281.   if (t_os_type & MT_WINS)
  282.     t_os = WINS;
  283.   if (t_os_type & MT_WIN3)
  284.     t_os = WIN3;
  285.   if (t_os_type & MT_OS2)
  286.     t_os = OS2;
  287.   return (t_os - 1);
  288. }
  289.  
  290. void giveup_timeslice(void)
  291. {
  292.   union REGS t_regs;
  293.  
  294.   switch (t_os) {
  295.     case DOS:
  296.       break;
  297.     case OS2:
  298.     case WIN3:
  299.     case WINS:
  300.       t_regs.x.ax = 0x1680;
  301.       int86(0x2f, &t_regs, &t_regs);
  302.       break;
  303.     case DV:
  304.       t_regs.x.ax = 0x1000;
  305.       int86(0x15, &t_regs, &t_regs);
  306.       break;
  307.   }
  308. }
  309.  
  310. unsigned char *trim(unsigned char *str)
  311. {
  312.   int i;
  313.  
  314.   if (str == NULL)
  315.     return (str);
  316.   for (i = strlen(str) - 1; (i >= 0) && isspace(str[i]); str[i--] = '\0');
  317.   while (isspace(str[0]))
  318.     strcpy(str, str + 1);
  319.   return (str);
  320. }
  321.  
  322. char *fix_quoted_commas(char *string)
  323. {
  324.   char *ptr;
  325.   int quoted = 0;
  326.  
  327.   ptr = string;
  328.   if (ptr) {
  329.     while (*ptr != 0) {
  330.       if (*ptr == '\"')
  331.         quoted = (!quoted);
  332.       if (*ptr == ',' && quoted)
  333.         *ptr = '│';
  334.       ptr = &ptr[1];
  335.     }
  336.   }
  337.   return (string);
  338. }
  339.  
  340. int sh_write(int handle, void *buffer, unsigned long length)
  341. {
  342.   if (handle == -1) {
  343.     return (-1);
  344.   }
  345.   return (write(handle, buffer, (unsigned) length));
  346. }
  347.  
  348. int sh_open(char *path, int file_access, unsigned fmode)
  349. {
  350.   int handle, count, share;
  351.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  352.  
  353.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
  354.     share = SH_DENYRW;
  355.   } else {
  356.     share = SH_DENYWR;
  357.   }
  358.   handle = open(path, file_access | share, fmode);
  359.   if (handle < 0) {
  360.     count = 1;
  361.     fnsplit(path, drive, dir, file, ext);
  362.     if (access(path, 0) != -1) {
  363.       delay(WAIT_TIME);
  364.       handle = open(path, file_access | share, fmode);
  365.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  366.         if (count % 2)
  367.           delay(WAIT_TIME);
  368.         else
  369.           giveup_timeslice();
  370.         count++;
  371.         handle = open(path, file_access | share, fmode);
  372.       }
  373.     }
  374.   }
  375.   return (handle);
  376. }
  377.  
  378. int sh_open1(char *path, int access)
  379. {
  380.   unsigned fmode;
  381.  
  382.   fmode = 0;
  383.   if ((access & O_RDWR) || (access & O_WRONLY))
  384.     fmode |= S_IWRITE;
  385.   if ((access & O_RDWR) || (access & O_RDONLY))
  386.     fmode |= S_IREAD;
  387.   return (sh_open(path, access, fmode));
  388. }
  389.  
  390. int sh_close(int f)
  391. {
  392.   if (f != -1)
  393.     close(f);
  394.   return (-1);
  395. }
  396.  
  397. int sh_read(int handle, void *buf, unsigned length)
  398. {
  399.   if (handle == -1) {
  400.     return (-1);
  401.   }
  402.   return (read(handle, buf, length));
  403. }
  404.  
  405. long sh_lseek(int handle, long offset, int fromwhere)
  406. {
  407.   if (handle == -1) {
  408.     return (-1L);
  409.   }
  410.   return (lseek(handle, offset, fromwhere));
  411. }
  412.  
  413. FILE *fsh_open(char *path, char *fmode)
  414. {
  415.   FILE *f;
  416.   int count, share, md, fd;
  417.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  418.  
  419.   share = SH_DENYWR;
  420.   md = 0;
  421.   if (((char *) _fstrchr(fmode, 'w')) != NULL) {
  422.     share = SH_DENYRD;
  423.     md = O_RDWR | O_CREAT | O_TRUNC;
  424.   } else
  425.     if (((char *) _fstrchr(fmode, 'a')) != NULL) {
  426.     share = SH_DENYRD;
  427.     md = O_RDWR | O_CREAT;
  428.   } else {
  429.     md = O_RDONLY;
  430.   }
  431.   if (((char *) _fstrchr(fmode, 'b')) != NULL) {
  432.     md |= O_BINARY;
  433.   }
  434.   if (((char *) _fstrchr(fmode, '+')) != NULL) {
  435.     md &= ~O_RDONLY;
  436.     md |= O_RDWR;
  437.     share = SH_DENYRD;
  438.   }
  439.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  440.   if (fd < 0) {
  441.     count = 1;
  442.     fnsplit(path, drive, dir, file, ext);
  443.     if ((access(path, 0)) != -1) {
  444.       delay(WAIT_TIME);
  445.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  446.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  447.         delay(WAIT_TIME);
  448.         count++;
  449.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  450.       }
  451.     }
  452.   }
  453.   if (fd > 0) {
  454.     if (((char *) _fstrchr(fmode, 'a')) != NULL)
  455.       sh_lseek(fd, 0L, SEEK_END);
  456.     f = fdopen(fd, fmode);
  457.     if (!f) {
  458.       close(fd);
  459.     }
  460.   } else
  461.     f = 0;
  462.   return (f);
  463. }
  464.  
  465. int log_it(int display, char *fmt,...)
  466. {
  467.   va_list v;
  468.   char s[255], fn[161];
  469.   FILE *fp;
  470.  
  471.   sprintf(fn, "%sNEWS.LOG", netdata);
  472.   if ((fp = fsh_open(fn, "at")) == NULL)
  473.     return 1;
  474.   va_start(v, fmt);
  475.   vsprintf(s, fmt, v);
  476.   va_end(v);
  477.   fputs(s, fp);
  478.   fclose(fp);
  479.   if (display)
  480.     fputs(s, stderr);
  481.   return 0;
  482. }
  483.  
  484. void statusbar(statusbarrec * sb, int now, int tot)
  485. {
  486.   float pos;
  487.   int maj_pos, min_pos, x;
  488.   int total_fractions = (sb->width) * sb->amount_per_square;
  489.  
  490.   if (DEBUG)
  491.     return;
  492.  
  493.   if (sb->current_item == 0) {
  494.     x = 0;
  495.     go_back(wherex(), 1);
  496.     output(" ■ File %3.3d/%3.3d ", now, tot);
  497.     putch(sb->side_char1);
  498.     while (x < sb->width) {
  499.       putch(sb->empty_space);
  500.       ++x;
  501.     }
  502.     putch(sb->side_char2);
  503.     x = 0;
  504.     while (x < sb->width) {
  505.       putch('\b');
  506.       ++x;
  507.     }
  508.     sb->last_maj_pos = 0;
  509.     sb->last_min_pos = 0;
  510.     return;
  511.   }
  512.  
  513.   pos = ((float)sb->current_item / sb->total_items);
  514.   pos = pos * total_fractions;
  515.   maj_pos = pos / sb->amount_per_square;
  516.   min_pos = pos - (maj_pos * sb->amount_per_square);
  517.  
  518.   if (min_pos == 0)
  519.     min_pos = sb->amount_per_square - 1;
  520.   else
  521.     --min_pos;
  522.  
  523.   if (maj_pos == sb->last_maj_pos) {
  524.     if (min_pos == sb->last_min_pos)
  525.       return;
  526.     putch('\b');
  527.     putch(sb->square_list[min_pos]);
  528.     sb->last_min_pos = min_pos;
  529.     return;
  530.   }
  531.   putch('\b');
  532.   putch(sb->square_list[sb->amount_per_square - 1]);
  533.   sb->last_min_pos = min_pos;
  534.  
  535.   ++sb->last_maj_pos;
  536.   while (sb->last_maj_pos < maj_pos) {
  537.     ++sb->last_maj_pos;
  538. //    if (wherex() < 80)
  539.     putch(sb->square_list[sb->amount_per_square - 1]);
  540.   }
  541. //  if (wherex() < 80)
  542.   putch(sb->square_list[min_pos]);
  543.  
  544.   sb->last_maj_pos = maj_pos;
  545.  
  546.   return;
  547. }
  548.  
  549. Mail_Socket *smtp_start(char *host, char *dom)
  550. {
  551.   longword h;
  552.   Mail_Socket *SMTP_sock = NULL;
  553.  
  554.   if (!WatTCP_initialized) {
  555.     sock_init();
  556.     WatTCP_initialized = 1;
  557.   }
  558.   if (!(h = resolve(host))) {
  559.     if (!(h = resolve(host))) {
  560.       SMTP_Err_Cond = SMTP_FAILED;
  561.       log_it(1, "\n ■ Error : Cannot resolve host %s", host);
  562.       return NULL;
  563.     }
  564.   }
  565.   if ((SMTP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
  566.     log_it(1, "\n ■ Insufficient memory to create socket... aborting");
  567.     exit(EXIT_FAILURE);
  568.   }
  569.   if ((SMTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  570.     log_it(1, "\n ■ Insufficient memory to create socket... aborting");
  571.     farfree(SMTP_sock);
  572.     exit(EXIT_FAILURE);
  573.   }
  574.   if (!tcp_open(SMTP_sock->sock, 0, h, SMTP_PORT, NULL)) {
  575.     SMTP_Err_Cond = SMTP_FAILED;
  576.     log_it(1, "\n ■ Error : Unable to connect to %s", host);
  577.     farfree(SMTP_sock);
  578.     return NULL;
  579.   }
  580.   sock_sturdy(SMTP_sock->sock, 100);
  581.   sock_mode(SMTP_sock->sock, TCP_MODE_ASCII);
  582.   sock_wait_established(SMTP_sock->sock, sock_delay, NULL, &SMTP_stat);
  583.  
  584.   sock_wait_input(SMTP_sock->sock, sock_delay, NULL, &SMTP_stat);
  585.   sock_gets(SMTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  586.   log_it(DEBUG, "\n - SMTP> %s", _temp_buffer);
  587.  
  588.   sprintf(_temp_buffer, "HELO %s", dom);
  589.   sock_puts(SMTP_sock->sock, _temp_buffer);
  590.   sock_wait_input(SMTP_sock->sock, sock_delay, NULL, &SMTP_stat);
  591.   while (sock_tbused(SMTP_sock->sock) > 0) {
  592.     SOCK_GETS(SMTP);
  593.     SMTP_FAIL_ON(SMTP_OOPS,);
  594.     SMTP_FAIL_ON(SMTP_SYNTAX,);
  595.     SMTP_FAIL_ON(SMTP_PARAM,);
  596.     SMTP_FAIL_ON(SMTP_BAD_PARM,);
  597.   }
  598.   SOCK_READ_ERR(SMTP,);
  599.   return (SMTP_sock);
  600. }
  601.  
  602. char *smtp_parse_from_line(FILE * f)
  603. {
  604.   char s[161];
  605.   int found = 0, done = 0, beginfocus, endfocus;
  606.  
  607.   rewind(f);
  608.   while (!feof(f) && !done) {
  609.     fgets(s, 160, f);
  610.     if (*s == '\n')
  611.       done = 1;
  612.     else if ((strncmpi(s, "from:", 5) == 0) && (_fstrchr(s, '@') != 0)) {
  613.       found = 1;
  614.       done = 1;
  615.     }
  616.   }
  617.   if (found) {
  618.     if ((beginfocus = _fstrcspn(s, "<")) != strlen(s)) {
  619.       ++beginfocus;
  620.       endfocus = _fstrcspn(s, ">");
  621.       s[endfocus] = NULL;
  622.     } else
  623.       beginfocus = 5;
  624.     return (trim(strdup(&s[beginfocus])));
  625.   }
  626.   return 0;
  627. }
  628.  
  629. char *stripspace(char *str)
  630. {
  631.   char *obuf, *nbuf;
  632.  
  633.   if (str) {
  634.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  635.       if (!isspace(*obuf))
  636.         *nbuf++ = *obuf;
  637.     }
  638.     *nbuf = NULL;
  639.   }
  640.   return (str);
  641. }
  642.  
  643. unsigned char *trimstr1(unsigned char *s)
  644. {
  645.   int i;
  646.   static char *whitespace = " \r\n\t";
  647.  
  648.   i = strlen(s);
  649.   if (i) {
  650.     while ((i > 0) && (_fstrchr(whitespace, s[i - 1])))
  651.       --i;
  652.     while ((i > 0) && (_fstrchr(whitespace, *s))) {
  653.       memmove(s, s + 1, --i);
  654.     }
  655.     s[i] = 0;
  656.   }
  657.   return (s);
  658. }
  659.  
  660.  
  661. int find_listname(FILE * f)
  662. {
  663.   char *ss = NULL, s[161];
  664.   int found = 0, done = 0;
  665.  
  666.   *LISTNAME = 0;
  667.   rewind(f);
  668.   while (!feof(f) && !done) {
  669.     fgets(s, 160, f);
  670.     if (*s == '\n')
  671.       done = 1;
  672.     else
  673.       if ((strnicmp(s, "x-reply-to", 10) == 0) && (_fstrchr(s, '@') != 0) &&
  674.           (_fstrchr(s, '\"') != 0)) {
  675.       found = 1;
  676.       done = 1;
  677.       }
  678.   }
  679.   if (found) {
  680.     ss = strtok(s, "\"");
  681.     if (ss) {
  682.       ss = strtok(NULL, "\"");
  683.       trimstr1(ss);
  684.       strcpy(LISTNAME, ss);
  685.     }
  686.     if (ss)
  687.       return 1;
  688.   }
  689.   return 0;
  690. }
  691.  
  692. char **smtp_parse_to_line(FILE * f)
  693. {
  694.   int i, i1, done = 0, current = 0;
  695.   char **list = NULL;
  696.   char *addr, _temp_addr[120], buf[120];
  697.  
  698.   rewind(f);
  699.   while (!feof(f) && !done) {
  700.     fgets(_temp_buffer, sizeof(_temp_buffer), f);
  701.     if (*_temp_buffer == '\n')
  702.       done = 1;
  703.     else
  704.       if ((strncmpi(_temp_buffer, "to:", 3) == 0) ||
  705.           (strncmpi(_temp_buffer, "cc:", 3) == 0) ||
  706.           (strncmpi(_temp_buffer, "bcc:", 4) == 0)) {
  707.       fix_quoted_commas(_temp_buffer);
  708.       addr = strtok(_temp_buffer, ":");
  709.       addr = strtok(NULL, "\r\n");
  710.       trimstr1(addr);
  711.       strcpy(_temp_addr, addr);
  712.       if ((_fstrchr(_temp_addr, ' ')) || (_fstrchr(_temp_addr, ')')) || (_fstrchr(_temp_addr, '\"'))) {
  713.         *buf = i1 = 0;
  714.         i = _fstrcspn(_temp_addr, "@");
  715.         while ((i > 0) && (_temp_addr[i - 1] != ' ') && (_temp_addr[i - 1] != '<'))
  716.           --i;
  717.         while (*_temp_addr && (_temp_addr[i] != ' ') && (_temp_addr[i] != '>'))
  718.           buf[i1++] = _temp_addr[i++];
  719.         buf[i1] = 0;
  720.         addr = buf;
  721.       }
  722.       list = (char **) farrealloc(list, sizeof(char *) * ((current) + 2));
  723.       list[current] = strdup(addr);
  724.       list[current + 1] = NULL;
  725.       current++;
  726.       }
  727.   }
  728.   return (list);
  729. }
  730.  
  731. int smtp_send_MAIL_FROM_line(Mail_Socket * SMTP_sock, FILE * f)
  732. {
  733.   char *from;
  734.  
  735.   from = smtp_parse_from_line(f);
  736.   if (from) {
  737.     if (DEBUG)
  738.       output("\n - SMTP> Mail From:<%s>", from);
  739.     sprintf(_temp_buffer, "MAIL FROM:<%s>", from);
  740.     strcpy(MAILFROM, from);
  741.     sock_puts(SMTP_sock->sock, _temp_buffer);
  742.     free(from);
  743.     while (sock_tbused(SMTP_sock->sock) > 0) {
  744.       SOCK_GETS(SMTP);
  745.       SMTP_FAIL_ON(SMTP_OOPS,);
  746.     }
  747.   }
  748.   SOCK_READ_ERR(SMTP,);
  749.   return 1;
  750. }
  751.  
  752. #define FREE_ALL for (i=0; to_list[i]!=NULL; i++) if (to_list[i]) free(to_list[i]); if (to_list) free(to_list);
  753.  
  754. int smtp_send_RCPT_TO_line(Mail_Socket * SMTP_sock, FILE * f)
  755. {
  756.   char **to_list;
  757.   int i, done = 0;
  758.  
  759.   to_list = smtp_parse_to_line(f);
  760.   for (i = 0; ((to_list[i] != NULL) && (!done)); i++) {
  761.     if ((_fstrchr(to_list[i], '@') == NULL) || (_fstrchr(to_list[i], '.') == NULL)) {
  762.       log_it(1, "\n ! Invalid recipient - %s - aborting message.", to_list[i]);
  763.       sock_puts(SMTP_sock->sock, "RSET");
  764.       done = 1;
  765.     } else {
  766.       log_it(DEBUG, "\n - SMTP> Rcpt To:<%s>", to_list[i]);
  767.       sprintf(_temp_buffer, "RCPT TO:<%s>", to_list[i]);
  768.       sock_puts(SMTP_sock->sock, _temp_buffer);
  769.     }
  770.     while (sock_tbused(SMTP_sock->sock) > 0) {
  771.       SOCK_GETS(SMTP);
  772.       SMTP_FAIL_ON(SMTP_OOPS, FREE_ALL);
  773.       SMTP_RESET_ON(SMTP_SYNTAX, FREE_ALL);
  774.       SMTP_RESET_ON(SMTP_PARAM, FREE_ALL);
  775.       SMTP_RESET_ON(SMTP_BAD_SEQ, FREE_ALL);
  776.     }
  777.   }
  778.  
  779.   SOCK_READ_ERR(SMTP, FREE_ALL);
  780.  
  781.   FREE_ALL;
  782.  
  783.   return 1;
  784. }
  785.  
  786. #undef FREE_ALL
  787.  
  788. int smtp_sendf(Mail_Socket * SMTP_sock, FILE * fp, long cb, long tb, int cf, int tf)
  789. {
  790.   int pos, in_header = 1, sent_from = 0;
  791.   long nbytes, obytes, rbytes, cbytes;
  792.   char *temp;
  793.   statusbarrec sb;
  794.  
  795.   sb.width = 59;
  796.   sb.amount_per_square = 2;
  797.   sb.square_list[0] = '.';
  798.   sb.square_list[1] = '■';
  799.   sb.empty_space = '.';
  800.   sb.side_char1 = '[';
  801.   sb.side_char2 = ']';
  802.   sb.current_item = 0;
  803.   sb.total_items = tb;
  804.   statusbar(&sb, cf, tf);
  805.   sb.current_item = cb;
  806.   statusbar(&sb, cf, tf);
  807.  
  808.   fseek(fp, 0L, SEEK_END);
  809.   obytes = ftell(fp);
  810.   rewind(fp);
  811.   sock_puts(SMTP_sock->sock, "DATA");
  812.   sock_wait_input(SMTP_sock->sock, sock_delay, NULL, &SMTP_stat);
  813.   while (sock_tbused(SMTP_sock->sock) > 0) {
  814.     SOCK_GETS(SMTP);
  815.     if (DEBUG)
  816.       log_it(DEBUG, "\n - SMTP> %s", _temp_buffer);
  817.     SMTP_FAIL_ON(SMTP_OOPS,);
  818.     SMTP_RESET_ON(SMTP_BAD_SEQ,);
  819.     SMTP_RESET_ON(SMTP_SYNTAX,);
  820.     SMTP_RESET_ON(SMTP_PARAM,);
  821.     SMTP_RESET_ON(SMTP_COM_NI,);
  822.     SMTP_RESET_ON(SMTP_FAILED,);
  823.     SMTP_RESET_ON(SMTP_ERROR,);
  824.   }
  825.   nbytes = 0L;
  826.   rbytes = cbytes = 256L;
  827.   pos = wherex();
  828.   if (DEBUG) {
  829.     output("               ");
  830.     go_back(wherex(), pos);
  831.   }
  832.   while ((feof(fp) == 0) && (fgets(_temp_buffer, sizeof(_temp_buffer), fp))) {
  833.     sb.current_item += strlen(_temp_buffer);
  834.     rip(_temp_buffer);
  835.     trim(temp = strdup(_temp_buffer));
  836.     if (strlen(temp) == 0)
  837.       in_header = 0;
  838.     free(temp);
  839.  
  840.     if (*_temp_buffer == '.') {
  841.       movmem(_temp_buffer, _temp_buffer + 1, sizeof(_temp_buffer) - 1);
  842.       *_temp_buffer = '.';
  843.     }
  844.  
  845.     if ((SKIP) && (*LISTNAME) && (strncmpi(_temp_buffer, "to:", 3) == 0) && (in_header)) {
  846.       if (!sent_from) {
  847.         sprintf(_temp_buffer, "To: \"Multiple Recipients of Mailing List %s\" <%s>",
  848.                 LISTNAME, MAILFROM);
  849.         sent_from = 1;
  850.       } else
  851.         continue;
  852.     }
  853.  
  854.     nbytes += sock_puts(SMTP_sock->sock, _temp_buffer) + 2;
  855.  
  856.     if (nbytes > 2000L)
  857.       cbytes = 512L;
  858.     if (nbytes > 4000L)
  859.       cbytes = 1024L;
  860.     if (nbytes > 16000L)
  861.       cbytes = 2048L;
  862.     if (nbytes > 32000L)
  863.       cbytes = 4096L;
  864.     if (nbytes > 64000L)
  865.       cbytes = 8192L;
  866.     if ((nbytes > rbytes) && (DEBUG)) {
  867.       go_back(wherex(), pos);
  868.       output("%ld/%ld", nbytes, obytes);
  869.       rbytes += cbytes;
  870.     } else
  871.       statusbar(&sb, cf, tf);
  872.  
  873.     if (kbhit()) {
  874.       go_back(wherex(), pos);
  875.       output(" aborted.");
  876.       aborted = 1;
  877.       return 0;
  878.     }
  879.   }
  880.   sock_puts(SMTP_sock->sock, ".");
  881.   sock_wait_input(SMTP_sock->sock, sock_delay, NULL, &SMTP_stat);
  882.   while (sock_tbused(SMTP_sock->sock) > 0) {
  883.     SOCK_GETS(SMTP);
  884.     SMTP_FAIL_ON(SMTP_OOPS,);
  885.     SMTP_RESET_ON(SMTP_ERROR,);
  886.     SMTP_RESET_ON(SMTP_SQUEEZED,);
  887.     SMTP_RESET_ON(SMTP_FULL,);
  888.     SMTP_RESET_ON(SMTP_FAILED,);
  889.   }
  890.   if (*_temp_buffer == '2') {
  891.     log_it(DEBUG, "\n - SMTP> %s", _temp_buffer);
  892.   }
  893.   if (DEBUG) {
  894.     go_back(wherex(), pos);
  895.     output("accepted.");
  896.   }
  897.   return 1;
  898.  
  899.   SOCK_READ_ERR(SMTP,);
  900.   return 0;
  901. }
  902.  
  903.  
  904. int smtp_shutdown(Mail_Socket * SMTP_sock)
  905. {
  906.   if (SMTP_sock->sock) {
  907.     sock_puts(SMTP_sock->sock, "QUIT");
  908.     sock_wait_input(SMTP_sock->sock, sock_delay, NULL, &SMTP_stat);
  909.     sock_gets(SMTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  910.     sock_close(SMTP_sock->sock);
  911.  
  912.   }
  913.   output(" ");
  914.   SOCK_READ_ERR(SMTP, free_Mail_Socket(SMTP_sock));
  915.   return 0;
  916. }
  917.  
  918.  
  919. Mail_Socket *pop_init(char *host)
  920. {
  921.   longword h;
  922.   Mail_Socket *POP_sock = NULL;
  923.  
  924.   if (!WatTCP_initialized) {
  925.     sock_init();
  926.     WatTCP_initialized = 1;
  927.   }
  928.   if (!(h = resolve(host))) {
  929.     if (!(h = resolve(host))) {
  930.       POP_Err_Cond = POP_BAD_HOST;
  931.       log_it(1, "\n ■ Error : Cannot resolve host %s", host);
  932.       return NULL;
  933.     }
  934.   }
  935.   if ((POP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
  936.     log_it(1, "\n ■ Insufficient memory to create socket... aborting.");
  937.     exit(EXIT_FAILURE);
  938.   }
  939.   if ((POP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  940.     log_it(1, "\n ■ Insufficient memory to create socket... aborting.");
  941.     farfree(POP_sock);
  942.     exit(EXIT_FAILURE);
  943.   }
  944.   if (!tcp_open(POP_sock->sock, 0, h, POP_PORT, NULL)) {
  945.     POP_Err_Cond = POP_BAD_HOST;
  946.     log_it(1, "\n ■ Error : Unable to connect to host %s", host);
  947.     return NULL;
  948.   }
  949.   sock_mode(POP_sock->sock, TCP_MODE_ASCII);
  950.   sock_wait_established(POP_sock->sock, sock_delay, NULL, &POP_stat);
  951.   sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  952.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  953.   log_it(DEBUG, "\n - POP> %s", _temp_buffer);
  954.   if (*_temp_buffer != '+') {
  955.     POP_Err_Cond = POP_HOST_UNAVAILABLE;
  956.     log_it(1, "\n ■ Error : Host %s is unavailable.", host);
  957.     return NULL;
  958.   } else {
  959.     POP_Err_Cond = POP_OK;
  960.     log_it(DEBUG, "\n - POP socket initialized.");
  961.     return (POP_sock);
  962.   }
  963.   SOCK_READ_ERR(POP,);
  964.   return (POP_sock);
  965. }
  966.  
  967. int pop_login(Mail_Socket * POP_sock, char *userid, char *password, char *host, int wingate)
  968. {
  969.   if (wingate)
  970.     sprintf(_temp_buffer, "USER %s#%s", userid, host);
  971.   else
  972.     sprintf(_temp_buffer, "USER %s", userid);
  973.   if ((strnicmp(userid, "n1160", 5) == 0) && (strnicmp(host, "edare.ml.org", 12) == 0))
  974.     sprintf(_temp_buffer, "USER %s", userid);
  975.   sock_puts(POP_sock->sock, _temp_buffer);
  976.   sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  977.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  978.   log_it(DEBUG, "\n - POP> %s", _temp_buffer);
  979.   if (*_temp_buffer != '+') {
  980.     POP_Err_Cond = POP_BAD_MBOX;
  981.     log_it(1, "\n ■ Error : host report mailbox %s does not exist", userid);
  982.     if (POP_sock->sock) {
  983.       sock_puts(POP_sock->sock, "QUIT");
  984.       sock_close(POP_sock->sock);
  985.     }
  986.     return 0;
  987.   }
  988.   sprintf(_temp_buffer, "PASS %s", password);
  989.   sock_puts(POP_sock->sock, _temp_buffer);
  990.   sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  991.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  992.   log_it(DEBUG, "\n - POP> %s", _temp_buffer);
  993.   if (*_temp_buffer != '+') {
  994.     POP_Err_Cond = POP_BAD_PASS;
  995.     log_it(1, "\n ■ Error : Host reports password incorrect or account locked.");
  996.     if (POP_sock->sock) {
  997.       sock_puts(POP_sock->sock, "QUIT");
  998.       sock_close(POP_sock->sock);
  999.     }
  1000.     return 0;
  1001.   }
  1002.   SOCK_READ_ERR(POP,);
  1003.   return 1;
  1004. }
  1005.  
  1006. int pop_status(Mail_Socket * POP_sock, unsigned int *count, unsigned long *totallength)
  1007. {
  1008.   char junk[12];
  1009.  
  1010.   sock_puts(POP_sock->sock, "STAT");
  1011.   sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1012.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1013.   if (*_temp_buffer != '+') {
  1014.     POP_Err_Cond = POP_UNKNOWN;
  1015.     log_it(DEBUG, "\n ■ Error : Unknown POP error.");
  1016.     return 0;
  1017.   } else
  1018.     sscanf(_temp_buffer, "%s %u %lu", junk, count, totallength);
  1019.  
  1020.   SOCK_READ_ERR(POP,);
  1021.   return 1;
  1022. }
  1023.  
  1024. long pop_length(Mail_Socket * POP_sock, unsigned int msg_num, unsigned long *size)
  1025. {
  1026.   char junk[21];
  1027.   unsigned int dummy;
  1028.  
  1029.   sprintf(_temp_buffer, "LIST %u", msg_num);
  1030.   sock_puts(POP_sock->sock, _temp_buffer);
  1031.   sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1032.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1033.   if (*_temp_buffer != '+') {
  1034.     POP_Err_Cond = POP_NOT_MSG;
  1035.     log_it(DEBUG, "\n ■ Error : No message #%u", msg_num);
  1036.     return 0;
  1037.   } else
  1038.     sscanf(_temp_buffer, "%s %u %lu", &junk, &dummy, size);
  1039.  
  1040.   SOCK_READ_ERR(POP,);
  1041.   if (*size == 0L) {
  1042.     log_it(1, "\n ■ Mailbox contains a zero byte file -- deleting Message #%u!", msg_num);
  1043.     sprintf(_temp_buffer, "DELE %u", msg_num);
  1044.     sock_puts(POP_sock->sock, _temp_buffer);
  1045.     sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1046.     sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1047.     log_it(DEBUG, "\n - POP> %s", _temp_buffer);
  1048.     if (*_temp_buffer != '+') {
  1049.       POP_Err_Cond = POP_NOT_MSG;
  1050.       log_it(1, "\n ■ Error : No message #%u", msg_num);
  1051.     }
  1052.     sock_puts(POP_sock->sock, "QUIT");
  1053.     sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1054.     sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1055.     if (*_temp_buffer != '+') {
  1056.       POP_Err_Cond = POP_UNKNOWN;
  1057.       log_it(1, "\n ■ Error : Unable to update mailbox.");
  1058.     } else
  1059.       log_it(DEBUG, "\n ■ Close and Updated mailbox.");
  1060.     sock_close(POP_sock->sock);
  1061.   }
  1062.   return (*size);
  1063. }
  1064.  
  1065. char *stristr(char *String, char *Pattern)
  1066. {
  1067.   char *pptr, *sptr, *start;
  1068.   unsigned int slen, plen;
  1069.  
  1070.   for (start = String, pptr = Pattern, slen = strlen(String),
  1071.        plen = strlen(Pattern); slen >= plen; start++, slen--) {
  1072.     while (toupper(*start) != toupper(*Pattern)) {
  1073.       start++;
  1074.       slen--;
  1075.       if (slen < plen)
  1076.         return (NULL);
  1077.     }
  1078.     sptr = start;
  1079.  
  1080.     pptr = Pattern;
  1081.     while (toupper(*sptr) == toupper(*pptr)) {
  1082.       sptr++;
  1083.       pptr++;
  1084.       if ('\0' == *pptr)
  1085.         return (start);
  1086.     }
  1087.   }
  1088.   return (NULL);
  1089. }
  1090.  
  1091. int checkspam(char *text)
  1092. {
  1093.   char fn[161], buf[81], tmp[81];
  1094.   int spam, ok;
  1095.   FILE *fp;
  1096.  
  1097.   spam = 0;
  1098.   sprintf(fn, "%sNOSPAM.TXT", netdata);
  1099.   if ((fp = fsh_open(fn, "r")) != NULL) {
  1100.     while ((!feof(fp)) && (!spam)) {
  1101.       fgets(buf, 80, fp);
  1102.       trimstr1(buf);
  1103.       if (strlen(buf) > 2) {
  1104.         if (buf[0] == '\"') {
  1105.           strcpy(tmp, &(buf[1]));
  1106.           LAST(tmp) = '\0';
  1107.           strcpy(buf, tmp);
  1108.         }
  1109.         if (buf[0] == '[') {
  1110.           if ((strnicmp(buf, "[GLOBAL]", 8) == 0) || (strnicmp(buf, "[MAIL]", 6) == 0))
  1111.             ok = 1;
  1112.           else
  1113.             ok = 0;
  1114.         }
  1115.         if ((ok) && (stristr(text, buf)))
  1116.           spam = 1;
  1117.       }
  1118.     }
  1119.     fclose(fp);
  1120.   }
  1121.   return spam;
  1122. }
  1123.  
  1124. int checkfido(char *text)
  1125. {
  1126.   char fn[161], buf[81], tmp[81];
  1127.   int spam;
  1128.   FILE *fp;
  1129.  
  1130.   spam = 0;
  1131.   sprintf(fn, "%sFIWPKT.TXT", netdata);
  1132.   if ((fp = fsh_open(fn, "r")) != NULL) {
  1133.     while ((!feof(fp)) && (!spam)) {
  1134.       fgets(buf, 80, fp);
  1135.       trimstr1(buf);
  1136.       if (strlen(buf) > 2) {
  1137.         if (buf[0] == '\"') {
  1138.           strcpy(tmp, &(buf[1]));
  1139.           LAST(tmp) = '\0';
  1140.           strcpy(buf, tmp);
  1141.         }
  1142.         if (stristr(text, buf))
  1143.           spam = 1;
  1144.       }
  1145.     }
  1146.   }
  1147.   if (fp != NULL)
  1148.     fclose(fp);
  1149.   return spam;
  1150. }
  1151.  
  1152. #define MAX_IDS 100
  1153.  
  1154. int compact_msgid(void)
  1155. {
  1156.   char fn[161], oldfn[161];
  1157.   int i, f1, f2, num_ids;
  1158.   Message_ID messageid;
  1159.  
  1160.   num_ids = 0;
  1161.   sprintf(oldfn, "%sMSGID.OLD", netdata);
  1162.   unlink(oldfn);
  1163.   sprintf(fn, "%sMSGID.DAT", netdata);
  1164.   rename(fn, oldfn);
  1165.   f1 = sh_open(oldfn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  1166.   if (f1 < 0) {
  1167.     log_it(1, "\n ! Unable to read %s.", oldfn);
  1168.     return 1;
  1169.   }
  1170.   f2 = sh_open(fn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  1171.  
  1172.   if (f2 < 0) {
  1173.     log_it(1, "\n ! Unable to create %s.", fn);
  1174.     return 1;
  1175.   }
  1176.   for (i = 50; i < MAX_IDS; i++) {
  1177.     sh_lseek(f1, ((long) (i)) * ((long) sizeof(Message_ID)), SEEK_SET);
  1178.     sh_read(f1, (void *) &messageid, sizeof(Message_ID));
  1179.     sh_lseek(f2, ((long) (num_ids++)) * ((long) sizeof(Message_ID)), SEEK_SET);
  1180.     sh_write(f2, &messageid, sizeof(Message_ID));
  1181.   }
  1182.   f1 = sh_close(f1);
  1183.   f2 = sh_close(f2);
  1184.   unlink(oldfn);
  1185.   return 0;
  1186. }
  1187.  
  1188. int check_messageid(int add, char *msgid)
  1189. {
  1190.   char fn[MAXPATH];
  1191.   int i, f, dupe, num_ids;
  1192.   Message_ID messageid;
  1193.  
  1194.   num_ids = dupe = 0;
  1195.   sprintf(fn, "%sMSGID.DAT", netdata);
  1196.   f = sh_open(fn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  1197.   if (f < 0) {
  1198.     log_it(1, "\n ! Unable to create %s.", fn);
  1199.     return -1;
  1200.   }
  1201.   num_ids = (int) (filelength(f) / sizeof(Message_ID));
  1202.  
  1203.   if (num_ids > MAX_IDS)
  1204.     compact_ids = 1;
  1205.   if (!add) {
  1206.     log_it(DEBUG, "\n - Scanning previous %d Message-IDs.", num_ids);
  1207.     for (i = 0; ((i < num_ids) && (!dupe)); i++) {
  1208.       sh_lseek(f, ((long) (i)) * ((long) sizeof(Message_ID)), SEEK_SET);
  1209.       sh_read(f, (void *) &messageid, sizeof(Message_ID));
  1210.       if (strcmp(messageid.msgid, msgid) == 0)
  1211.         dupe = 1;
  1212.     }
  1213.   } else {
  1214.     strncpy(messageid.msgid, msgid, 80);
  1215.     messageid.msgid[81] = '\0';
  1216.     log_it(DEBUG, "\n ■ Adding new Message-ID:%s", messageid.msgid);
  1217.     sh_lseek(f, ((long) (num_ids)) * ((long) sizeof(Message_ID)), SEEK_SET);
  1218.     sh_write(f, &messageid, sizeof(Message_ID));
  1219.   }
  1220.   f = sh_close(f);
  1221.   return dupe;
  1222. }
  1223.  
  1224. int pop_top(Mail_Socket * POP_sock, unsigned int msg_num, int usernum)
  1225. {
  1226.   int okpkt, found_from, found_subj, dupe;
  1227.   char *ss, subject[81];
  1228.  
  1229.   sprintf(_temp_buffer, "TOP %u 100", msg_num);
  1230.   sock_puts(POP_sock->sock, _temp_buffer);
  1231.   sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1232.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1233.   if (*_temp_buffer != '+') {
  1234.     POP_Err_Cond = POP_NOT_MSG;
  1235.     log_it(1, "\n ■ Error : No message #%u.", msg_num);
  1236.     return -1;
  1237.   }
  1238.   okpkt = -1;
  1239.  
  1240.   dupe = 0;
  1241.   found_from = found_subj = fdl = 0;
  1242.   net_pkt[0] = 0;
  1243.   while (1) {
  1244.     sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1245.     sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1246.     if (*_temp_buffer == '.' && _temp_buffer[1] == 0)
  1247.       break;
  1248.     if (usernum == 0) {
  1249.       if (strnicmp(_temp_buffer, "NET: ", 5) == 0)
  1250.         strcpy(net_pkt, &_temp_buffer[5]);
  1251.       if ((strnicmp(_temp_buffer, "begin ", 6) == 0) &&
  1252.           (stristr(_temp_buffer, "WINMAIL") == NULL)) {
  1253.         if (okpkt != 4)
  1254.           okpkt = 1;
  1255.         if ((stristr(_temp_buffer, ".ZIP") != NULL) ||
  1256.             (stristr(_temp_buffer, ".ARJ") != NULL) ||
  1257.             (stristr(_temp_buffer, ".LZH") != NULL))
  1258.           okpkt = 2;
  1259.         if ((stristr(_temp_buffer, ".GIF") != NULL) ||
  1260.             (stristr(_temp_buffer, ".JPG") != NULL))
  1261.           okpkt = 3;
  1262.         if ((okpkt == 2) || (okpkt == 3) || (fdl)) {
  1263.           ss = strtok(_temp_buffer, "6");
  1264.           if (ss) {
  1265.             ss = strtok(NULL, " ");
  1266.             if (ss)
  1267.               ss = strtok(NULL, "\r\n");
  1268.           }
  1269.           if (ss) {
  1270.             strcpy(fdlfn, ss);
  1271.             trimstr1(fdlfn);
  1272.           }
  1273.         }
  1274.       }
  1275.       if (strnicmp(_temp_buffer, "FDL Type:", 9) == 0)
  1276.         fdl = 1;
  1277.     }
  1278.     if ((strnicmp(_temp_buffer, "from:", 5) == 0) && (!found_from)) {
  1279.       if (((stristr(_temp_buffer, "mailer-daemon") != NULL) ||
  1280.            (stristr(_temp_buffer, "mail delivery") != NULL) ||
  1281.            (stristr(_temp_buffer, "mdaemon") != NULL) ||
  1282.            (stristr(_temp_buffer, "administrator") != NULL) ||
  1283.            (stristr(_temp_buffer, from_user) != NULL)) && (usernum == 0))
  1284.         okpkt = 4;
  1285.       else {
  1286.         if (_temp_buffer[6] != 0) {
  1287.           strncpy(pktowner, &_temp_buffer[6], 35);
  1288.           trimstr1(pktowner);
  1289.           pktowner[25] = 0;
  1290.         } else
  1291.           strcpy(pktowner, "Unknown");
  1292.       }
  1293.       found_from = 1;
  1294.     }
  1295.     if ((strnicmp(_temp_buffer, "subject:", 8) == 0) && (!found_subj)) {
  1296.       if (_temp_buffer[9] != 0)
  1297.         strncpy(subject, &_temp_buffer[9], 60);
  1298.       else
  1299.         strcpy(subject, "Unknown");
  1300.       found_subj = 1;
  1301.     }
  1302.     if (usernum == 0) {
  1303.       if ((strnicmp(_temp_buffer, "Message-ID:", 11) == 0) && (!found_subj)) {
  1304.         if (_temp_buffer[11] != 0) {
  1305.           strncpy(id, &_temp_buffer[11], 80);
  1306.           id[81] = '\0';
  1307.           if (check_messageid(0, id))
  1308.             dupe = 1;
  1309.         }
  1310.       }
  1311.     }
  1312.   }
  1313.   if (found_from && found_subj) {
  1314.     if (okpkt == -1) {
  1315.       if ((checkspam(pktowner)) || (checkspam(subject)))
  1316.         okpkt = 5;
  1317.       if ((checkfido(subject)))
  1318.         okpkt = 8;
  1319.     }
  1320.   }
  1321.   if (found_subj) {
  1322.     if ((strnicmp(subject, "subscribe", 9) == 0) ||
  1323.         (strnicmp(subject, "unsubscribe", 11) == 0))
  1324.       okpkt = 6;
  1325.   }
  1326.   if (dupe)
  1327.     okpkt = 7;
  1328.   SOCK_READ_ERR(POP,);
  1329.   return okpkt;
  1330. }
  1331.  
  1332. int pop_getf(Mail_Socket * POP_sock, char *fn, unsigned int msg_num, int usernum)
  1333. {
  1334.   unsigned long size;
  1335.   long nbytes, rbytes;
  1336.   int pos, ctld, length;
  1337.   FILE *fp;
  1338.  
  1339.   if (!pop_length(POP_sock, msg_num, &size))
  1340.     return 0;
  1341.   sprintf(_temp_buffer, "RETR %u", msg_num);
  1342.   sock_puts(POP_sock->sock, _temp_buffer);
  1343.   sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1344.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1345.   if (*_temp_buffer != '+') {
  1346.     POP_Err_Cond = POP_NOT_MSG;
  1347.     log_it(1, "\n ■ Error : No message #%u", msg_num);
  1348.     return 0;
  1349.   }
  1350.   nbytes = 0L;
  1351.  
  1352.   rbytes = 1024L;
  1353.   output(" : ");
  1354.   pos = wherex();
  1355.   if ((fp = fsh_open(fn, "w")) == NULL) {
  1356.     log_it(1, "\n ■ Unable to create %s... aborting!", fn);
  1357.     return 0;
  1358.   }
  1359.   if (usernum > 0)
  1360.     fprintf(fp, "0RX-WWIV-User: #%d\n", usernum);
  1361.   else if (usernum == -1)
  1362.     fprintf(fp, "0RX-WWIV-List: *%s\n", listaddr);
  1363.   ctld = 1;
  1364.   while (1) {
  1365.     sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1366.     length = (sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer)));
  1367.     if ((ctld == 1) && (length == 0))
  1368.       ctld = 0;
  1369.     if ((strnicmp(_temp_buffer, "begin ", 6) == 0) &&
  1370.         (stristr(_temp_buffer, "WINMAIL") != NULL))
  1371.       ctld = 2;
  1372.     if ((ctld == 2) && (strnicmp(_temp_buffer, "end", 3) == 0))
  1373.       ctld = 0;
  1374.     if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0)
  1375.       break;
  1376.     if (EOF == (nbytes += fprintf(fp, "%s%s\n", ctld ? "0R" : "", _temp_buffer))) {
  1377.       if (fp != NULL)
  1378.         fclose(fp);
  1379.       return 0;
  1380.     }
  1381.     if (nbytes > rbytes) {
  1382.       go_back(wherex(), pos);
  1383.       output("%ld/%ld", nbytes, size);
  1384.       rbytes += 512L;
  1385.     }
  1386.   }
  1387.   if (fp != NULL)
  1388.     fclose(fp);
  1389.   go_back(wherex(), pos);
  1390.   output("%lu bytes.", size);
  1391.   SOCK_READ_ERR(POP,);
  1392.   return 1;
  1393. }
  1394.  
  1395. int pop_delete(Mail_Socket * POP_sock, unsigned int msg_num)
  1396. {
  1397.   sprintf(_temp_buffer, "DELE %u", msg_num);
  1398.   sock_puts(POP_sock->sock, _temp_buffer);
  1399.   sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1400.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1401.   log_it(DEBUG, "\n - POP> %s", _temp_buffer);
  1402.   if (*_temp_buffer != '+') {
  1403.     POP_Err_Cond = POP_NOT_MSG;
  1404.     log_it(1, "\n ■ Error : No message #%u", msg_num);
  1405.     return 2;
  1406.   }
  1407.   SOCK_READ_ERR(POP,);
  1408.  
  1409.   return 1;
  1410. }
  1411.  
  1412.  
  1413. int pop_shutdown(Mail_Socket * POP_sock)
  1414. {
  1415.   if (POP_sock->sock) {
  1416.     sock_puts(POP_sock->sock, "QUIT");
  1417.     sock_wait_input(POP_sock->sock, sock_delay, NULL, &POP_stat);
  1418.     sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  1419.     if (*_temp_buffer != '+') {
  1420.       POP_Err_Cond = POP_UNKNOWN;
  1421.       log_it(1, "\n ■ Error : Unable to update mailbox.");
  1422.       return 0;
  1423.     } else
  1424.       log_it(DEBUG, "\n ■ Closed and updated mailbox.");
  1425.     sock_close(POP_sock->sock);
  1426.     return 1;
  1427.   }
  1428. sock_err:
  1429.   free_Mail_Socket(POP_sock);
  1430.  
  1431.   return 0;
  1432. }
  1433.  
  1434. int pop_get_nextf(Mail_Socket * POP_sock, char *fn, int msgnum, int usernum)
  1435. {
  1436.   if (!pop_getf(POP_sock, fn, msgnum, usernum)) {
  1437.     unlink(fn);
  1438.     return 0;
  1439.   }
  1440.   return (pop_delete(POP_sock, msgnum));
  1441. }
  1442.  
  1443. int exist(char *s)
  1444. {
  1445.   int i;
  1446.   struct ffblk ff;
  1447.  
  1448.   i = findfirst(s, &ff, FA_HIDDEN);
  1449.   if (i)
  1450.     return 0;
  1451.   else
  1452.     return 1;
  1453. }
  1454.  
  1455. int find_acct(char *username, char *hostname, char *password)
  1456. {
  1457.   char *ss, fn[161], s[121];
  1458.   int num;
  1459.   FILE *fp;
  1460.  
  1461.   num = 0;
  1462.   sprintf(fn, "%sACCT.INI", netdata);
  1463.   if ((fp = fsh_open(fn, "rt")) == NULL)
  1464.     return 0;
  1465.  
  1466.   while ((fgets(s, 120, fp)) && (num == 0)) {
  1467.     if (strnicmp(s, "ACCT", 4) == 0) {
  1468.       if ((_fstrstr(s, username) != 0) && (_fstrstr(s, hostname) != 0) &&
  1469.           (_fstrstr(s, password) != 0)) {
  1470.         ss = strtok(s, "=");
  1471.         if (ss)
  1472.           trimstr1(s);
  1473.         if (s[4] == '-') {
  1474.           num = -1;
  1475.           strcpy(listaddr, &(s[5]));
  1476.           log_it(DEBUG, "\n ■ Checking mailbox %s on %s for list %s.", username,
  1477.                  hostname, listaddr);
  1478.         } else {
  1479.           num = atoi(&(s[4]));
  1480.           log_it(DEBUG, "\n ■ Checking mailbox %s on %s for user #%d.", username,
  1481.                  hostname, num);
  1482.         }
  1483.       }
  1484.     }
  1485.   }
  1486.   if (fp != NULL)
  1487.     fclose(fp);
  1488.   return num;
  1489. }
  1490.  
  1491. int count_accts(int build)
  1492. {
  1493.   FILE *fp;
  1494.   char *ss, s[101], fn[MAXPATH];
  1495.   int accts = 0;
  1496.  
  1497.   sprintf(fn, "%sACCT.INI", netdata);
  1498.   if ((fp = fsh_open(fn, "rt")) == NULL)
  1499.     return 0;
  1500.  
  1501.   while (fgets(s, 100, fp)) {
  1502.     if (strnicmp(s, "ACCT", 4) == 0) {
  1503.       if (build) {
  1504.         ss = strtok(s, "=");
  1505.         if (ss) {
  1506.           ss = strtok(NULL, "@");
  1507.           trimstr1(ss);
  1508.           if (ss) {
  1509.             strcpy(acct[accts].popname, ss);
  1510.             ss = strtok(NULL, " ");
  1511.             trimstr1(ss);
  1512.             if (ss) {
  1513.               strcpy(acct[accts].pophost, ss);
  1514.               ss = strtok(NULL, " \r\n");
  1515.               trimstr1(ss);
  1516.               if (ss)
  1517.                 strcpy(acct[accts].poppass, ss);
  1518.             }
  1519.           }
  1520.           log_it(DEBUG, "\n - Account : %s - %s - %s", acct[accts].pophost,
  1521.                  acct[accts].popname, acct[accts].poppass);
  1522.         }
  1523.       }
  1524.       ++accts;
  1525.     }
  1526.   }
  1527.   if (fp != NULL)
  1528.     fclose(fp);
  1529.   return accts;
  1530. }
  1531.  
  1532. int parse_net_ini(void)
  1533. {
  1534.   char s[MAXPATH], line[121], *ss;
  1535.   FILE *fp;
  1536.  
  1537.   sprintf(s, "%sNET.INI", maindir);
  1538.   if ((fp = fsh_open(s, "rt")) == NULL) {
  1539.     output("\n ■ Unable to open %s.", s);
  1540.     return 1;
  1541.   }
  1542.   *POPHOST = *PROXY = *POPNAME = *POPPASS = *DOMAIN = *NODEPASS = 0;
  1543.   while (fgets(line, 120, fp)) {
  1544.     ss = NULL;
  1545.     stripspace(line);
  1546.     if ((line[0] != ';') && (line[0] != 0) && (line[0] != '\n')) {
  1547.       ss = strtok(line, "=");
  1548.       if (ss) {
  1549.         ss = strtok(NULL, "\r\n");
  1550.         trimstr1(ss);
  1551.         if (strnicmp(line, "POPHOST", 7) == 0) {
  1552.           if (ss) {
  1553.             strcpy(POPHOST, ss);
  1554.             continue;
  1555.           }
  1556.         }
  1557.         if (strnicmp(line, "PROXY", 5) == 0) {
  1558.           if (ss) {
  1559.             strcpy(PROXY, ss);
  1560.             continue;
  1561.           }
  1562.         }
  1563.         if (strnicmp(line, "POPNAME", 7) == 0) {
  1564.           if (ss) {
  1565.             strcpy(POPNAME, ss);
  1566.             continue;
  1567.           }
  1568.         }
  1569.         if (strnicmp(line, "POPPASS", 7) == 0) {
  1570.           if (ss) {
  1571.             strcpy(POPPASS, ss);
  1572.             continue;
  1573.           }
  1574.         }
  1575.         if (strnicmp(line, "DOMAIN", 6) == 0) {
  1576.           if (ss) {
  1577.             strcpy(DOMAIN, ss);
  1578.             continue;
  1579.           }
  1580.         }
  1581.         if (strnicmp(line, "NODEPASS", 8) == 0) {
  1582.           if (ss) {
  1583.             strcpy(NODEPASS, ss);
  1584.             continue;
  1585.           }
  1586.         }
  1587.       }
  1588.     }
  1589.   }
  1590.   if (fp != NULL)
  1591.     fclose(fp);
  1592.   if ((!*POPHOST) || (!*POPNAME) || (!*POPPASS) || (!*DOMAIN))
  1593.     return 1;
  1594.   return 0;
  1595. }
  1596.  
  1597. int copyfile(char *infn, char *outfn)
  1598. {
  1599.   int f1, f2, i;
  1600.   char *b;
  1601.   struct ftime ft;
  1602.  
  1603.   if ((strcmp(infn, outfn) != 0) && (exist(infn)) && (!exist(outfn))) {
  1604.     if ((b = (char *) farmalloc(16400)) == NULL)
  1605.       return 0;
  1606.     f1 = sh_open1(infn, O_RDONLY | O_BINARY);
  1607.     if (!f1) {
  1608.       farfree(b);
  1609.       b = NULL;
  1610.       return 0;
  1611.     }
  1612.     getftime(f1, &ft);
  1613.     f2 = sh_open(outfn, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1614.     if (!f2) {
  1615.       farfree(b);
  1616.       b = NULL;
  1617.       f1 = sh_close(f1);
  1618.       return 0;
  1619.     }
  1620.     i = sh_read(f1, (char *) b, 16384);
  1621.     while (i > 0) {
  1622.       sh_write(f2, (char *) b, i);
  1623.       i = sh_read(f1, (char *) b, 16384);
  1624.     }
  1625.     f1 = sh_close(f1);
  1626.     setftime(f2, &ft);
  1627.     f2 = sh_close(f2);
  1628.     farfree(b);
  1629.     b = NULL;
  1630.   }
  1631.   return 1;
  1632. }
  1633.  
  1634. int move_bad(char *path, char *fn, int why)
  1635. {
  1636.   char src[MAXPATH], dest[MAXPATH];
  1637.  
  1638.   log_it(1, "\n ■ %s failed - SMTP error condition %d", fn, why);
  1639.   sprintf(src, "%s%s", path, fn);
  1640.   sprintf(dest, "%sFAILED\\%s", path, fn);
  1641.   trimstr1(src);
  1642.   trimstr1(dest);
  1643.   fprintf(stderr, "\n! \"%s\" for src\n \"%s\" for dest\n", src, dest);
  1644.   return (copyfile(src, dest));
  1645. }
  1646.  
  1647. int isleap(unsigned yr)
  1648. {
  1649.   return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);
  1650. }
  1651.  
  1652. static unsigned months_to_days(unsigned month)
  1653. {
  1654.   return (month * 3057 - 3007) / 100;
  1655. }
  1656.  
  1657. int jdate(unsigned yr, unsigned mo, unsigned day)
  1658. {
  1659.   int which;
  1660.  
  1661.   which = day + months_to_days(mo);
  1662.   if (mo > 2)
  1663.     which -= isleap(yr) ? 1 : 2;
  1664.  
  1665.   return which;
  1666. }
  1667.  
  1668. main(int argc, char *argv[])
  1669. {
  1670.   char fn[MAXPATH], temp[181], mqueue[MAXPATH], s[21], s1[21];
  1671.   char nodepass[40], nodename[20], host[60], ext[MAXEXT];
  1672.   char pophost[60], poppass[20], popname[40];
  1673.   int failed, ok, f1, i, i1, okpkt, result, usernum, num_accts, accts;
  1674.   int wingate, once, checknode, jdater, jdatec, skipit, firstrun;
  1675.   long total_bytes, current_bytes;
  1676.   int total_files, current_files;
  1677.   unsigned long size;
  1678.   unsigned count;
  1679.   FILE *fp;
  1680.   struct ffblk ff;
  1681.   struct date dt;
  1682.   Mail_Socket *pop_sock = NULL;
  1683.   Mail_Socket *smtp_sock = NULL;
  1684.  
  1685.   detect_multitask();
  1686.  
  1687.   if (strncmpi(argv[1], "-send", strlen(argv[1])) == 0) {
  1688.     if (argc < 5) {
  1689.       output("\n ■ %s", version);
  1690.       output("\n ■ Invalid arguments for %s\n", argv[0]);
  1691.       exit(EXIT_FAILURE);
  1692.     }
  1693.     if (argc >= 6)
  1694.       DEBUG = atoi(argv[5]);
  1695.     if (argc == 7)
  1696.       SKIP = atoi(argv[6]);
  1697.     else
  1698.       SKIP = 0;
  1699.  
  1700.     strcpy(mqueue, argv[4]);
  1701.     strcpy(netdata, argv[4]);
  1702.     LAST(netdata) = '\0';
  1703.     while (LAST(netdata) != '\\')
  1704.       LAST(netdata) = '\0';
  1705.     output("\n");
  1706.     if ((smtp_sock = smtp_start(argv[2], argv[3])) != NULL) {
  1707.       total_bytes = total_files = current_bytes = current_files = failed = count = aborted = skipit = 0;
  1708.       firstrun = 1;
  1709.       sprintf(fn, "%s*.*", mqueue);
  1710.       f1 = findfirst(fn, &ff, FA_ARCH);
  1711.       while (f1 == 0) {
  1712.         total_bytes += ff.ff_fsize;
  1713.         ++total_files;
  1714.         f1 = findnext(&ff);
  1715.       }
  1716.       f1 = findfirst(fn, &ff, FA_ARCH);
  1717.       getdate(&dt);
  1718.       jdater = jdate(dt.da_year, dt.da_mon, dt.da_day);
  1719.       while ((count < 3) && (f1 == 0) && (failed < 5) && (!aborted)) {
  1720.         if (count > 1) {
  1721.           DEBUG = 1;
  1722.           output("\n ■ SMTP pass %d...\n", count);
  1723.         }
  1724.         sprintf(fn, "%s%s", mqueue, ff.ff_name);
  1725.         if ((fp = fsh_open(fn, "r")) != NULL) {
  1726.           SMTP_Err_Cond = SMTP_OK;
  1727.           ok = find_listname(fp);
  1728.           if (DEBUG) {
  1729.             output("\n");
  1730.             if (!ok)
  1731.               output("\r ■ SND : %-12s : %-18.18s : [Space] aborts : ", ff.ff_name, argv[2]);
  1732.             else
  1733.               output("\r ■ SND : %-12s : %-18.18s : [Space] aborts : ", LISTNAME, argv[2]);
  1734.           }
  1735.           ok = 1;
  1736.           if (!smtp_send_MAIL_FROM_line(smtp_sock, fp))
  1737.             ok = 0;
  1738.           if (!smtp_send_RCPT_TO_line(smtp_sock, fp))
  1739.             ok = 0;
  1740.           aborted = result = 0;
  1741.           if (ok) {
  1742.             ++current_files;
  1743.             result = smtp_sendf(smtp_sock, fp, current_bytes, total_bytes, current_files, total_files);
  1744.             if ((!result) || (aborted)) {
  1745.               if (fp != NULL)
  1746.                 fclose(fp);
  1747.               if ((SMTP_Err_Cond == SMTP_FULL) || (SMTP_Err_Cond == SMTP_FAILED) ||
  1748.                   (SMTP_Err_Cond == SMTP_ACCESS) || (SMTP_Err_Cond == SMTP_BAD_NAM) ||
  1749.                   (SMTP_Err_Cond == SMTP_YOU_FWD))
  1750.                 if (move_bad(mqueue, ff.ff_name, SMTP_Err_Cond))
  1751.                   unlink(fn);
  1752.               ++failed;
  1753.             } else {
  1754.               if (fp != NULL)
  1755.                 fclose(fp);
  1756.               if (!skipit)
  1757.                 unlink(fn);
  1758.               current_bytes += ff.ff_fsize;
  1759.             }
  1760.           } else {
  1761.             if (fp != NULL)
  1762.               fclose(fp);
  1763.           }
  1764.         } else
  1765.           log_it(1, "\n ! Unable to open %s.", fn);
  1766.         f1 = findnext(&ff);
  1767.         if ((f1 != 0) && (firstrun)) {
  1768.           sprintf(fn, "%s*.*", mqueue);
  1769.           f1 = findfirst(fn, &ff, FA_ARCH);
  1770.           if (f1 == 0)
  1771.             ++count;
  1772.         }
  1773.         if (f1 != 0) {
  1774.           if (firstrun) {
  1775.             strcat(mqueue, "DIGEST\\");
  1776.             firstrun = 0;
  1777.           }
  1778.           sprintf(fn, "%s*.*", mqueue);
  1779.           f1 = findfirst(fn, &ff, FA_ARCH);
  1780.           skipit = 1;
  1781.           while ((f1 == 0) && (skipit)) {
  1782.             fnsplit(ff.ff_name, NULL, NULL, NULL, ext);
  1783.             jdatec = atoi(&(ext[1]));
  1784.             if (jdatec < jdater) {
  1785.               skipit = 0;
  1786.               break;
  1787.             } else {
  1788.               skipit = 1;
  1789.               log_it(0, "\n ■ Digest %s not ready.", ff.ff_name);
  1790.             }
  1791.             f1 = findnext(&ff);
  1792.           }
  1793.         }
  1794.       }
  1795.       if (failed >= 5)
  1796.         log_it(1, "\n ■ Too many SMTP failures.  Try again later.");
  1797.       smtp_shutdown(smtp_sock);
  1798.     } else
  1799.       log_it(1, "\n ■ SMTP connection failed.");
  1800.     fcloseall();
  1801.   } else if (strncmpi(argv[1], "-r", strlen(argv[1])) == 0) {
  1802.     strcpy(temp, argv[0]);
  1803.     while (LAST(temp) != '\\')
  1804.       LAST(temp) = '\0';
  1805.     strcpy(maindir, temp);
  1806.     if (parse_net_ini()) {
  1807.       output("\n ! Missing critical NET.INI settings!");
  1808.       exit(EXIT_FAILURE);
  1809.     }
  1810.     wingate = 0;
  1811.     strcpy(pophost, POPHOST);
  1812.     strcpy(popname, POPNAME);
  1813.     strcpy(poppass, POPPASS);
  1814.     if (argc < 6) {
  1815.       output("\n ■ %s", version);
  1816.       output("\n ■ Invalid arguments for %s\n", argv[0]);
  1817.       exit(EXIT_FAILURE);
  1818.     }
  1819.     sprintf(from_user, "%s@%s", popname, pophost);
  1820.     DEBUG = atoi(argv[4]);
  1821.     ALLMAIL = atoi(argv[3]);
  1822.     strcpy(netdata, argv[2]);
  1823.     LAST(netdata) = '\0';
  1824.     while (LAST(netdata) != '\\')
  1825.       LAST(netdata) = '\0';
  1826.     POP_Err_Cond = POP_OK;
  1827.     num_accts = accts = usernum = checknode = once = 0;
  1828.     *nodepass = *nodename = 0;
  1829.     if (*NODEPASS) {
  1830.       strcpy(nodepass, NODEPASS);
  1831.       strcpy(nodename, argv[5]);
  1832.       checknode = once = 1;
  1833.     }
  1834.     while ((num_accts >= 0) || (once)) {
  1835.       if (*PROXY) {
  1836.         wingate = 1;
  1837.         strcpy(host, PROXY);
  1838.       } else
  1839.         strcpy(host, pophost);
  1840.       log_it(1, "\n ■ Checking %s... ", pophost);
  1841.       if ((pop_sock = pop_init(host)) != NULL) {
  1842.         if (pop_login(pop_sock, popname, poppass, pophost, wingate)) {
  1843.           if (pop_status(pop_sock, &count, &size)) {
  1844.             okpkt = 0;
  1845.             output("%s has %u message%s (%luK).", popname, count,
  1846.                    count == 1 ? "" : "s", ((size + 1023) / 1024));
  1847.             i1 = 1;
  1848.             pktowner[0] = 0;
  1849.             while (i1 <= count) {
  1850.               okpkt = 0;
  1851.               okpkt = pop_top(pop_sock, i1, usernum);
  1852.               switch (okpkt) {
  1853.                 case -1:
  1854.                   if ((!ALLMAIL) && (!fdl))
  1855.                     log_it(1, "\n ■ Non-network message %d left on server.", i1);
  1856.                   else {
  1857.                     i = 0;
  1858.                     sprintf(temp, "%sUNK-%03d.MSG", argv[2], i);
  1859.                     while (exist(temp))
  1860.                       sprintf(temp, "%sUNK-%03d.MSG", argv[2], ++i);
  1861.                     fnsplit(temp, NULL, NULL, s, s1);
  1862.                     log_it(1, "\n ■ Receive : %3.3d : %-20.20s : %s%s", i1, pktowner[0] == 0 ?
  1863.                            "non-network packet" : pktowner, s, s1);
  1864.                     result = (pop_get_nextf(pop_sock, temp, i1, usernum));
  1865.                     switch (result) {
  1866.                       case 0:
  1867.                         log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  1868.                         fcloseall();
  1869.                         exit(EXIT_FAILURE);
  1870.                       case 1:
  1871.                         break;
  1872.                       case 2:
  1873.                         log_it(1, "\n ■ Unable to delete message %d from host!", i1);
  1874.                         exit(EXIT_FAILURE);
  1875.                     }
  1876.                   }
  1877.                   break;
  1878.                 case 0:
  1879.                   log_it(1, "\n ■ Error accessing message %d", i1);
  1880.                   fcloseall();
  1881.                   exit(EXIT_FAILURE);
  1882.                 case 1:
  1883.                   i = 0;
  1884.                   sprintf(temp, "%sPKT-%03d.UUE", argv[2], i);
  1885.                   while (exist(temp))
  1886.                     sprintf(temp, "%sPKT-%03d.UUE", argv[2], ++i);
  1887.                   fnsplit(temp, NULL, NULL, s, s1);
  1888.                   log_it(1, "\n ■ %s : %3.3d : %-20.20s : %s%s",
  1889.                          *net_pkt ? net_pkt : "Receive", i1, pktowner, s, s1);
  1890.                   result = (pop_get_nextf(pop_sock, temp, i1, usernum));
  1891.                   switch (result) {
  1892.                     case 0:
  1893.                       log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  1894.                       fcloseall();
  1895.                       exit(EXIT_FAILURE);
  1896.                     case 1:
  1897.                       check_messageid(1, id);
  1898.                       break;
  1899.                     case 2:
  1900.                       log_it(1, "\n ■ Unable to delete message %d on host!", i1);
  1901.                       exit(EXIT_FAILURE);
  1902.                   }
  1903.                   break;
  1904.                 case 2:
  1905.                   if ((!ALLMAIL) && (!fdl))
  1906.                     log_it(1, "\n ■ Non-network message %d left on server.", i1);
  1907.                   else {
  1908.                     i = 0;
  1909.                     sprintf(temp, "%sARC-%03d.UUE", argv[2], i);
  1910.                     while (exist(temp))
  1911.                       sprintf(temp, "%sARC-%03d.UUE", argv[2], ++i);
  1912.                     fnsplit(temp, NULL, NULL, s, s1);
  1913.                     if (*fdlfn)
  1914.                       log_it(1, "\n ■ Receive : %3.3d : %-20.20s : %s", i1, "archived file", fdlfn);
  1915.                     else
  1916.                       log_it(1, "\n ■ Receive : %3.3d : %-20.20s : %s%s", i1, "archived file", s, s1);
  1917.                     result = (pop_get_nextf(pop_sock, temp, i1, usernum));
  1918.                     switch (result) {
  1919.                       case 0:
  1920.                         log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  1921.                         fcloseall();
  1922.                         exit(EXIT_FAILURE);
  1923.                       case 1:
  1924.                         check_messageid(1, id);
  1925.                         break;
  1926.                       case 2:
  1927.                         log_it(1, "\n ■ Unable to delete message %d on host!", i1);
  1928.                         exit(EXIT_FAILURE);
  1929.                     }
  1930.                   }
  1931.                   break;
  1932.                 case 3:
  1933.                   if ((!ALLMAIL) && (!fdl))
  1934.                     log_it(1, "\n ■ Non-network message %d left on server.", i1);
  1935.                   else {
  1936.                     i = 0;
  1937.                     sprintf(temp, "%sGIF-%03d.UUE", argv[2], i);
  1938.                     while (exist(temp))
  1939.                       sprintf(temp, "%sGIF-%03d.UUE", argv[2], ++i);
  1940.                     fnsplit(temp, NULL, NULL, s, s1);
  1941.                     log_it(1, "\n ■ Receive : %3.3d : %-20.20s : %s%s", i1, "graphic/image file", s, s1);
  1942.                     result = (pop_get_nextf(pop_sock, temp, i1, usernum));
  1943.                     switch (result) {
  1944.                       case 0:
  1945.                         log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  1946.                         fcloseall();
  1947.                         exit(EXIT_FAILURE);
  1948.                       case 1:
  1949.                         check_messageid(1, id);
  1950.                         break;
  1951.                       case 2:
  1952.                         log_it(1, "\n ■ Unable to delete message %d from host!", i1);
  1953.                         exit(EXIT_FAILURE);
  1954.                     }
  1955.                   }
  1956.                   break;
  1957.                 case 4:
  1958.                   i = 0;
  1959.                   sprintf(temp, "%sBAD-%03d.UUE", argv[2], i);
  1960.                   while (exist(temp))
  1961.                     sprintf(temp, "%sBAD-%03d.UUE", argv[2], ++i);
  1962.                   fnsplit(temp, NULL, NULL, s, s1);
  1963.                   log_it(1, "\n ■ Receive : %3.3d : %-20.20s : %s%s", i1, "mailer-daemon/bounced", s, s1);
  1964.                   result = (pop_get_nextf(pop_sock, temp, i1, usernum));
  1965.                   switch (result) {
  1966.                     case 0:
  1967.                       log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  1968.                       fcloseall();
  1969.                       exit(EXIT_FAILURE);
  1970.                     case 1:
  1971.                       check_messageid(1, id);
  1972.                       break;
  1973.                     case 2:
  1974.                       log_it(1, "\n ■ Unable to delete message %d from host!", i1);
  1975.                       exit(EXIT_FAILURE);
  1976.                   }
  1977.                   break;
  1978.                 case 5:
  1979.                   if ((!ALLMAIL) && (!fdl))
  1980.                     log_it(1, "\n ■ Non-network message %d left on server.", i1);
  1981.                   else {
  1982.                     i = 0;
  1983.                     sprintf(temp, "%sSPM-%03d.MSG", argv[2], i);
  1984.                     while (exist(temp))
  1985.                       sprintf(temp, "%sSPM-%03d.MSG", argv[2], ++i);
  1986.                     fnsplit(temp, NULL, NULL, s, s1);
  1987.                     log_it(1, "\n ■ Receive : %3.3d : %-20.20s : %s%s", i1, "matched NOSPAM.TXT", s, s1);
  1988.                     result = (pop_get_nextf(pop_sock, temp, i1, usernum));
  1989.                     switch (result) {
  1990.                       case 0:
  1991.                         log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  1992.                         fcloseall();
  1993.                         exit(EXIT_FAILURE);
  1994.                       case 1:
  1995.                         check_messageid(1, id);
  1996.                         break;
  1997.                       case 2:
  1998.                         log_it(1, "\n ■ Unable to delete message %d from host!", i1);
  1999.                         exit(EXIT_FAILURE);
  2000.                     }
  2001.                   }
  2002.                   break;
  2003.                 case 6:
  2004.                   if ((!ALLMAIL) && (!fdl))
  2005.                     log_it(1, "\n ■ Non-network message %d left on server.", i1);
  2006.                   else {
  2007.                     i = 0;
  2008.                     sprintf(temp, "%sSUB-%03d.MSG", argv[2], i);
  2009.                     while (exist(temp))
  2010.                       sprintf(temp, "%sSUB-%03d.MSG", argv[2], ++i);
  2011.                     fnsplit(temp, NULL, NULL, s, s1);
  2012.                     log_it(1, "\n ■ Receive : %3.3d : %-20.20s : %s%s", i1, "subscribe request", s, s1);
  2013.                     result = (pop_get_nextf(pop_sock, temp, i1, usernum));
  2014.                     switch (result) {
  2015.                       case 0:
  2016.                         log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  2017.                         fcloseall();
  2018.                         exit(EXIT_FAILURE);
  2019.                       case 1:
  2020.                         check_messageid(1, id);
  2021.                         break;
  2022.                       case 2:
  2023.                         log_it(1, "\n ■ Unable to delete message %d from host!", i1);
  2024.                         exit(EXIT_FAILURE);
  2025.                     }
  2026.                   }
  2027.                   break;
  2028.                 case 7:
  2029.                   if ((!ALLMAIL) && (!fdl))
  2030.                     log_it(1, "\n ■ Duplicate message %d left on server.", i1);
  2031.                   else {
  2032.                     result = (pop_delete(pop_sock, i1));
  2033.                     switch (result) {
  2034.                       case 0:
  2035.                         log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  2036.                         fcloseall();
  2037.                         exit(EXIT_FAILURE);
  2038.                       case 1:
  2039.                         break;
  2040.                       case 2:
  2041.                         log_it(1, "\n ■ Unable to delete message %d from host!", i1);
  2042.                         exit(EXIT_FAILURE);
  2043.                     }
  2044.                   }
  2045.                   break;
  2046.                 case 8:
  2047.                   i = 0;
  2048.                   sprintf(temp, "%sFIW-%03d.MSG", argv[2], i);
  2049.                   while (exist(temp))
  2050.                     sprintf(temp, "%sFIW-%03d.MSG", argv[2], ++i);
  2051.                   fnsplit(temp, NULL, NULL, s, s1);
  2052.                   log_it(1, "\n ■ Receive : %3.3d : %-20.20s : %s%s", i1, pktowner[0] == 0 ?
  2053.                          "non-network packet" : pktowner, s, s1);
  2054.                   result = (pop_get_nextf(pop_sock, temp, i1, usernum));
  2055.                   switch (result) {
  2056.                     case 0:
  2057.                       log_it(1, "\n ■ Unable to retrieve message %d.", i1);
  2058.                       fcloseall();
  2059.                       exit(EXIT_FAILURE);
  2060.                     case 1:
  2061.                       break;
  2062.                     case 2:
  2063.                       log_it(1, "\n ■ Unable to delete message %d from host!", i1);
  2064.                       exit(EXIT_FAILURE);
  2065.                   }
  2066.                   break;
  2067.               }
  2068.               i1++;
  2069.               fcloseall();
  2070.             }
  2071.             if (compact_ids) {
  2072.               log_it(1, "\n ■ Compacting Message-ID database...");
  2073.               compact_msgid();
  2074.               compact_ids = 0;
  2075.             }
  2076.           } else
  2077.             log_it(1, "\n ■ Unknown POP access error - try again later.");
  2078.           pop_shutdown(pop_sock);
  2079.         } else {
  2080.           log_it(1, "\n ■ Unable to log into POP server!");
  2081.           pop_shutdown(pop_sock);
  2082.         }
  2083.       } else
  2084.         log_it(1, "\n ■ POP socket connect failed.");
  2085.       if ((checknode) && (once)) {
  2086.         strcpy(pophost, "mail.filenet.wwiv.net");
  2087.         strcpy(popname, nodename);
  2088.         strcpy(poppass, nodepass);
  2089.         ALLMAIL = 1;
  2090.         once = 0;
  2091.       } else {
  2092.         if (!accts) {
  2093.           num_accts = count_accts(0);
  2094.           log_it(DEBUG, "\n - Found %d extra account%s.", num_accts, num_accts == 1 ? "" : "s");
  2095.           if (num_accts) {
  2096.             acct = (ACCT *) farmalloc(sizeof(ACCT) * num_accts);
  2097.             if (acct != NULL) {
  2098.               num_accts = count_accts(1);
  2099.             } else {
  2100.               log_it(DEBUG, "\n ! Insufficient memory for extra accounts.");
  2101.               num_accts = 0;
  2102.             }
  2103.             accts = 1;
  2104.           }
  2105.         }
  2106.         if (num_accts) {
  2107.           strcpy(pophost, acct[num_accts - 1].pophost);
  2108.           strcpy(popname, acct[num_accts - 1].popname);
  2109.           strcpy(poppass, acct[num_accts - 1].poppass);
  2110.           ALLMAIL = 1;
  2111.           usernum = find_acct(popname, pophost, poppass);
  2112.           --num_accts;
  2113.         } else
  2114.           num_accts = -1;
  2115.       }
  2116.     }
  2117.     if (acct != NULL) {
  2118.       farfree((void *) acct);
  2119.       acct = NULL;
  2120.     }
  2121.     exit(EXIT_SUCCESS);
  2122.   }
  2123.   exit(EXIT_FAILURE);
  2124.   return EXIT_FAILURE;
  2125. }
  2126.