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