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