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