home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B98.ZIP / POP.CPP < prev    next >
Text File  |  1998-11-16  |  63KB  |  2,123 lines

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