home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B87.ZIP / POP.CPP < prev    next >
Text File  |  1998-01-18  |  52KB  |  1,723 lines

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