home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B42.ZIP / POP.C < prev    next >
Text File  |  1997-09-27  |  34KB  |  1,135 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. #include "tcp.h"
  17. #include "pop.h"
  18. #include "version.h"
  19. #include "retcode.h"
  20.  
  21. #define POP_PORT 110
  22.  
  23. #define SMTP_PORT 25
  24.  
  25. #define _TEMP_BUFFER_LEN 1024
  26.  
  27. #define SHARE_LEVEL 10
  28. #define WAIT_TIME 10
  29. #define TRIES 100
  30.  
  31. #define MT_DESQVIEW 0x01
  32. #define MT_WINDOWS  0x02
  33. #define MT_OS2      0x04
  34. #define MT_NB       0x40
  35.  
  36.  
  37. #define blankline(LINE) (strspn(LINE, " \n\t\r")==strlen(LINE))
  38.  
  39. #define free_Mail_Socket(SOCK) if (SOCK!=NULL) {     \
  40.   farfree(SOCK->sock); farfree(SOCK); SOCK=NULL; }
  41.  
  42. int POP_Err_Cond;
  43. char *POP_Err_Msg = NULL;
  44. static char _POP_err_msg_buf[100] = "";
  45. int SMTP_Err_Cond;
  46. char *SMTP_Err_Msg = NULL;
  47. static char _SMTP_err_msg_buf[100] = "";
  48. char *POPLIB_Err_Msg = NULL;
  49. char from_user[41], POPHOST[81];
  50. int WatTCP_initialized = 0;
  51. static char _temp_buffer[_TEMP_BUFFER_LEN];
  52. int POP_stat;
  53. int SMTP_stat;
  54. int SOCK_DELAY = 60;
  55. int multitasker = 0;
  56. int DEBUG = 1;
  57.  
  58. char pktowner[21];
  59.  
  60. #define SOCK_READ_ERR(PROTOCOL)                                                 \
  61.   sock_err:                                                                     \
  62.   if (DEBUG) {                                                                  \
  63.     switch (PROTOCOL##_stat) {                                                  \
  64.       case 1 : /* foreign host closed */                                        \
  65.         sprintf(PROTOCOL##_Err_Msg, #PROTOCOL " Ok");                           \
  66.         sprintf(POPLIB_Err_Msg, "Connection reset: %s.",                        \
  67.             sockerr(PROTOCOL##_sock->sock));                                    \
  68.         output(#PROTOCOL " > %s\n", POPLIB_Err_Msg);                            \
  69.       case -1: /* timeout */                                                    \
  70.         sprintf(PROTOCOL##_Err_Msg, #PROTOCOL " Ok");                           \
  71.         sprintf(POPLIB_Err_Msg, "Timeout: %s.", sockerr(PROTOCOL##_sock->sock));\
  72.         output(#PROTOCOL " > %s\n", POPLIB_Err_Msg);                            \
  73.     }                                                                           \
  74.   }
  75.  
  76. #define SOCK_GETS(PROTOCOL)                                                     \
  77.   sock_wait_input( PROTOCOL##_sock->sock, SOCK_DELAY, NULL,                     \
  78.        &PROTOCOL##_stat );                                                      \
  79.   sock_gets( PROTOCOL##_sock->sock, _temp_buffer, sizeof( _temp_buffer ));      \
  80.   if (DEBUG)                                                                    \
  81.     output(#PROTOCOL"> %s\n", _temp_buffer);                                    \
  82.   strncpy(PROTOCOL##_Err_Msg, &_temp_buffer[5], 100);
  83.  
  84. #define SMTP_FAIL_ON(NUM, SHUTDOWN)                                             \
  85.   if ( SMTP_Err_Cond == NUM ) {                                                 \
  86.     if (DEBUG)                                                                  \
  87.       output("SMTP Failure > '" #NUM "'\n");                                    \
  88.     sock_puts( SMTP_sock->sock, "QUIT" );                                       \
  89.     sock_close( SMTP_sock->sock );                                              \
  90.     free_Mail_Socket(SMTP_sock);                                                \
  91.     if (DEBUG)                                                                  \
  92.       output("SMTP Error - '" #NUM "'", 100);                                   \
  93.     SHUTDOWN;                                                                   \
  94.     return(0);                                                                  \
  95.   }
  96.  
  97. #define SMTP_FAIL_IF(COND, SHUTDOWN)                                            \
  98.   if ( COND ) {                                                                 \
  99.     if (DEBUG)                                                                  \
  100.       output("SMTP Failure > (" #COND ")\n");                                   \
  101.     sock_puts( SMTP_sock->sock, "QUIT" );                                       \
  102.     sock_close( SMTP_sock->sock );                                              \
  103.     free_Mail_Socket(SMTP_sock);                                                \
  104.     output("SMTP Error - (" #COND ")", 100);                                    \
  105.     SHUTDOWN;                                                                   \
  106.     return(0);                                                                  \
  107.   }
  108.  
  109. #define SMTP_RESET_ON(NUM, SHUTDOWN)                                            \
  110.   if ( SMTP_Err_Cond == NUM ) {                                                 \
  111.     if (DEBUG)                                                                  \
  112.       output("SMTP Problem > '" #NUM "'\n");                                    \
  113.     sock_puts( SMTP_sock->sock, "RSET" );                                       \
  114.     sock_wait_input( SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat );           \
  115.     sock_gets( SMTP_sock->sock, _temp_buffer, sizeof( _temp_buffer ));          \
  116.     if (DEBUG)                                                                  \
  117.       output("SMTP Problem - '" #NUM "'", 100);                                 \
  118.     SHUTDOWN;                                                                   \
  119.     return(0);                                                                  \
  120.   }
  121.  
  122. void output(char *fmt, ...)
  123. {
  124.   va_list v;
  125.   char s[255];
  126.  
  127.   va_start(v, fmt);
  128.   vsprintf(s, fmt, v);
  129.   va_end(v);
  130.   fputs(s, stderr);
  131. }
  132.  
  133. void backline(void)
  134. {
  135.   int i;
  136.  
  137.   i = wherex();
  138.   if (i < 80)
  139.     output(" ");
  140.   else
  141.     i--;
  142.   while (i-- > 0)
  143.     output("\b \b");
  144. }
  145.  
  146.  
  147. int get_dos_version(void)
  148. {
  149.   _AX = 0x3000;
  150.   geninterrupt(0x21);
  151.   if (_AX % 256 >= 10) {
  152.     multitasker |= MT_OS2;
  153.   }
  154.   return (_AX);
  155. }
  156.  
  157. int get_dv_version(void)
  158. {
  159.   int v;
  160.  
  161.   if (multitasker & MT_OS2)
  162.     return 0;
  163.   _AX = 0x2b01;
  164.   _CX = 0x4445;
  165.   _DX = 0x5351;
  166.   geninterrupt(0x21);
  167.   if (_AL == 0xff) {
  168.     return 0;
  169.   } else {
  170.     v = _BX;
  171.     multitasker |= MT_DESQVIEW;
  172.     return v;
  173.   }
  174. }
  175.  
  176. int get_win_version(void)
  177. {
  178.   int v = 0;
  179.  
  180.   __emit__(0x55, 0x06, 0x53);
  181.   _AX = 0x352f;
  182.   geninterrupt(0x21);
  183.   _AX = _ES;
  184.   if (_AX | _BX) {
  185.     _AX = 0x1600;
  186.     geninterrupt(0x2f);
  187.     v = _AX;
  188.     if (v % 256 <= 1)
  189.       v = 0;
  190.   }
  191.   __emit__(0x5b, 0x07, 0x5d);
  192.   if (v != 0)
  193.     multitasker |= MT_WINDOWS;
  194.   return (v);
  195. }
  196.  
  197. int get_nb_version(void)
  198. {
  199.   _AX = 0;
  200.   geninterrupt(0x2A);
  201.   return (_AH);
  202. }
  203.  
  204. void detect_multitask(void)
  205. {
  206.   get_dos_version();
  207.   get_win_version();
  208.   get_dv_version();
  209.   if (multitasker < 2)
  210.     if (get_nb_version())
  211.       multitasker = MT_NB;
  212. }
  213.  
  214. unsigned char *trim(char *str)
  215. {
  216.   int i;
  217.  
  218.   if (str == NULL)
  219.     return (str);
  220.   for (i = strlen(str) - 1; (i >= 0) && isspace(str[i]); str[i--] = '\0');
  221.   while (isspace(str[0]))
  222.     strcpy(str, str + 1);
  223.   return (str);
  224. }
  225.  
  226. unsigned char *strrep(char *str, char old, char New)
  227. {
  228.   int i;
  229.  
  230.   for (i = 0; str[i]; i++)
  231.     if (str[i] == old)
  232.       str[i] = New;
  233.   return (str);
  234. }
  235.  
  236. int getnumbers(unsigned char *ascii, unsigned int *d1, unsigned long *d2)
  237. {
  238.   char *p;
  239.  
  240.   if ((p = (char *) strchr((char *) ascii, ' ')) == NULL)
  241.     return 0;
  242.   while (*p == ' ')
  243.     p++;
  244.   *d1 = atoi(p);
  245.   if ((p = (char *) strchr(p, ' ')) == NULL)
  246.     return 1;
  247.   while (*p == ' ')
  248.     p++;
  249.   *d2 = atol(p);
  250.   return 2;
  251. }
  252.  
  253. char *fix_quoted_commas(char *string)
  254. {
  255.   char *ptr;
  256.   int quoted = 0;
  257.  
  258.   ptr = string;
  259.   if (ptr) {
  260.     while (*ptr != 0) {
  261.       if (*ptr == '\"')
  262.         quoted = (!quoted);
  263.       if (*ptr == ',' && quoted)
  264.         *ptr = '│';
  265.       ptr = &ptr[1];
  266.     }
  267.   }
  268.   return (string);
  269. }
  270.  
  271. long sh_lseek(int handle, long offset, int fromwhere)
  272. {
  273.   if (handle == -1) {
  274.     return (-1L);
  275.   }
  276.   return (lseek(handle, offset, fromwhere));
  277. }
  278.  
  279. FILE *fsh_open(char *path, char *fmode)
  280. {
  281.   FILE *f;
  282.   int count, share, md, fd;
  283.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  284.  
  285.   share = SH_DENYWR;
  286.   md = 0;
  287.   if (((char *) strchr(fmode, 'w')) != NULL) {
  288.     share = SH_DENYRD;
  289.     md = O_RDWR | O_CREAT | O_TRUNC;
  290.   } else
  291.     if (((char *) strchr(fmode, 'a')) != NULL) {
  292.     share = SH_DENYRD;
  293.     md = O_RDWR | O_CREAT;
  294.   } else {
  295.     md = O_RDONLY;
  296.   }
  297.   if (((char *) strchr(fmode, 'b')) != NULL) {
  298.     md |= O_BINARY;
  299.   }
  300.   if (((char *) strchr(fmode, '+')) != NULL) {
  301.     md &= ~O_RDONLY;
  302.     md |= O_RDWR;
  303.     share = SH_DENYRD;
  304.   }
  305.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  306.   if (fd < 0) {
  307.     count = 1;
  308.     fnsplit(path, drive, dir, file, ext);
  309.     if ((access(path, 0)) != -1) {
  310.       delay(WAIT_TIME);
  311.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  312.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  313.         delay(WAIT_TIME);
  314.         count++;
  315.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  316.       }
  317.     }
  318.   }
  319.   if (fd > 0) {
  320.     if (((char *) strchr(fmode, 'a')) != NULL)
  321.       sh_lseek(fd, 0L, SEEK_END);
  322.     f = fdopen(fd, fmode);
  323.     if (!f) {
  324.       close(fd);
  325.     }
  326.   } else
  327.     f = 0;
  328.   return (f);
  329. }
  330.  
  331. Mail_Socket *smtp_start(char *host, char *dom)
  332. {
  333.   longword h;
  334.   Mail_Socket *SMTP_sock = NULL;
  335.  
  336.   if (!SMTP_Err_Msg)
  337.     SMTP_Err_Msg = _SMTP_err_msg_buf;
  338.   if (!WatTCP_initialized) {
  339.     sock_init();
  340.     WatTCP_initialized = 1;
  341.   }
  342.   if ((h = resolve(host)) == 0L) {
  343.     if ((h = resolve(host)) == 0L) {
  344.       SMTP_Err_Cond = SMTP_FAILED;
  345.       if (DEBUG) {
  346.         sprintf(SMTP_Err_Msg, "Cannot resolve host %s", host);
  347.         output("\nSMTP> %s", SMTP_Err_Msg);
  348.       }
  349.       return NULL;
  350.     }
  351.   }
  352.   if ((SMTP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
  353.     output("\n ■ Insufficient memory to create socket... aborting");
  354.     exit(EXIT_FAILURE);
  355.   }
  356.   if ((SMTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  357.     output("\n ■ Insufficient memory to create socket... aborting");
  358.     farfree(SMTP_sock);
  359.     exit(EXIT_FAILURE);
  360.   }
  361.   if (!tcp_open(SMTP_sock->sock, 0, h, SMTP_PORT, NULL)) {
  362.     SMTP_Err_Cond = SMTP_FAILED;
  363.     if (DEBUG) {
  364.       sprintf(SMTP_Err_Msg, "Unable to connect to %s", host);
  365.       output("\nSMTP> %s", SMTP_Err_Msg);
  366.     }
  367.     farfree(SMTP_sock);
  368.     return NULL;
  369.   }
  370.   sock_mode(SMTP_sock->sock, TCP_MODE_ASCII);
  371.   sock_wait_established(SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat);
  372.   while (sock_tbused(SMTP_sock->sock) > 0) {
  373.     SOCK_GETS(SMTP);
  374.     SMTP_FAIL_ON(SMTP_OOPS,);
  375.   }
  376.   sock_printf(SMTP_sock->sock, "HELO %s", dom);
  377.   sock_wait_input(SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat);
  378.   while (sock_tbused(SMTP_sock->sock) > 0) {
  379.     SOCK_GETS(SMTP);
  380.     SMTP_FAIL_ON(SMTP_OOPS,);
  381.     SMTP_FAIL_ON(SMTP_SYNTAX,);
  382.     SMTP_FAIL_ON(SMTP_PARAM,);
  383.     SMTP_FAIL_ON(SMTP_BAD_PARM,);
  384.   }
  385.   return (SMTP_sock);
  386.   SOCK_READ_ERR(SMTP);
  387.   return NULL;
  388. }
  389.  
  390. char *smtp_parse_from_line(FILE * f)
  391. {
  392.   int found = 0, done = 0;
  393.  
  394.   rewind(f);
  395.   while (!feof(f) && !done) {
  396.     fgets(_temp_buffer, sizeof(_temp_buffer), f);
  397.     if (*_temp_buffer == '\n')
  398.       done = 1;
  399.     else
  400.       if (strncmpi(_temp_buffer, "from:", 5) == 0 &&
  401.           strchr(_temp_buffer, '@') != 0)
  402.       found = 1, done = 1;
  403.   }
  404.   if (found)
  405.     return (trim(strdup(&_temp_buffer[5])));
  406.   return 0;
  407. }
  408.  
  409. char **smtp_parse_to_line(FILE * f)
  410. {
  411.   int done = 0, current = 0;
  412.   char **list = NULL;
  413.   char *addr;
  414.  
  415.   rewind(f);
  416.   while (!feof(f) && !done) {
  417.     fgets(_temp_buffer, sizeof(_temp_buffer), f);
  418.     if (*_temp_buffer == '\n')
  419.       done = 1;
  420.     else if ((strncmpi(_temp_buffer, "to:", 3) == 0) ||
  421.           (strncmpi(_temp_buffer, "cc:", 3) == 0) ||
  422.           (strncmpi(_temp_buffer, "bcc:", 4) == 0)) {
  423.       fix_quoted_commas(_temp_buffer);
  424.       addr = strtok(_temp_buffer, ":");
  425.       while ((addr = strtok(NULL, ",\n")) != NULL) {
  426.         strrep(addr, '│', ',');
  427.         list = (char **) realloc(list, sizeof(char *) * ((current) + 2));
  428.         list[current] = strdup(addr);
  429.         list[current + 1] = NULL;
  430.         current++;
  431.       }
  432.     }
  433.   }
  434.   return (list);
  435. }
  436.  
  437. int smtp_send_MAIL_FROM_line(Mail_Socket * SMTP_sock, FILE * f)
  438. {
  439.   char *from;
  440.  
  441.   from = smtp_parse_from_line(f);
  442.   if (from) {
  443.     if (DEBUG)
  444.       output("\nSMTP> Mail From:<%s>\n", from);
  445.     sock_printf(SMTP_sock->sock, "MAIL FROM:<%s>", from);
  446.     free(from);
  447.     while (sock_tbused(SMTP_sock->sock) > 0) {
  448.       SOCK_GETS(SMTP);
  449.       SMTP_FAIL_ON(SMTP_OOPS,);
  450.     }
  451.   }
  452.   return 1;
  453.   SOCK_READ_ERR(SMTP);
  454.   return 0;
  455. }
  456.  
  457. #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);
  458.  
  459. int smtp_send_RCPT_TO_line(Mail_Socket * SMTP_sock, FILE * f)
  460. {
  461.   char **to_list;
  462.   int i;
  463.  
  464.   to_list = smtp_parse_to_line(f);
  465.   for (i = 0; to_list[i] != NULL; i++) {
  466.     if (DEBUG)
  467.       output("SMTP> Rcpt To:<%s>\n", to_list[i]);
  468.     sock_printf(SMTP_sock->sock, "RCPT TO:<%s>", to_list[i]);
  469.     while (sock_tbused(SMTP_sock->sock) > 0) {
  470.       SOCK_GETS(SMTP);
  471.       SMTP_FAIL_ON(SMTP_OOPS, FREE_ALL);
  472.       SMTP_RESET_ON(SMTP_SYNTAX, FREE_ALL);
  473.       SMTP_RESET_ON(SMTP_PARAM, FREE_ALL);
  474.       SMTP_RESET_ON(SMTP_BAD_SEQ, FREE_ALL);
  475.     }
  476.   }
  477.   FREE_ALL;
  478.   return 1;
  479.   SOCK_READ_ERR(SMTP);
  480.   return 0;
  481. }
  482.  
  483. void go_back(int from, int to)
  484. {
  485.   int i;
  486.  
  487.   for (i = from; i > to; i--)
  488.     output("\b \b");
  489. }
  490.  
  491. #undef FREE_ALL
  492.  
  493. int smtp_sendf(Mail_Socket * SMTP_sock, FILE * f)
  494. {
  495.   int in_header = 1, in_bcc = 0, pos;
  496.   long nbytes, obytes, rbytes;
  497.   char *temp, ch;
  498.  
  499.   if (smtp_send_MAIL_FROM_line(SMTP_sock, f) == 0)
  500.     return 0;
  501.   if (smtp_send_RCPT_TO_line(SMTP_sock, f) == 0)
  502.     return 0;
  503.   fseek(f, 0L, SEEK_END);
  504.   obytes = ftell(f);
  505.   rewind(f);
  506.   sock_puts(SMTP_sock->sock, "DATA");
  507.   sock_wait_input(SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat);
  508.   while (sock_tbused(SMTP_sock->sock) > 0) {
  509.     SOCK_GETS(SMTP);
  510.     if (DEBUG)
  511.       output("\nSMTP> %s", _temp_buffer);
  512.     SMTP_FAIL_ON(SMTP_OOPS,);
  513.     SMTP_RESET_ON(SMTP_BAD_SEQ,);
  514.     SMTP_RESET_ON(SMTP_SYNTAX,);
  515.     SMTP_RESET_ON(SMTP_PARAM,);
  516.     SMTP_RESET_ON(SMTP_COM_NI,);
  517.     SMTP_RESET_ON(SMTP_FAILED,);
  518.     SMTP_RESET_ON(SMTP_ERROR,);
  519.   }
  520.   nbytes = 0L;
  521.   rbytes = 1024L;
  522.   output(" - ");
  523.   pos = wherex();
  524.   while (!feof(f)) {
  525.     if (kbhit()) {
  526.       ch = (getch());
  527.       switch (ch) {
  528.         case 27:
  529.         case 32:
  530.           go_back(wherex(), pos);
  531.           return -1;
  532.         default:
  533.           break;
  534.       }
  535.     }
  536.     fgets(_temp_buffer, sizeof(_temp_buffer), f);
  537.     strrep(_temp_buffer, '\n', '\0');
  538.     strrep(_temp_buffer, '\r', '\0');
  539.     temp = trim(strdup(_temp_buffer));
  540.     if (strlen(temp) == 0)
  541.       in_header = 0;
  542.     if (temp)
  543.       free(temp);
  544.     if (in_header && !in_bcc && strncmpi(_temp_buffer, "bcc:", 4) == 0) {
  545.       in_bcc = 1;
  546.       continue;
  547.     }
  548.     if (in_bcc)
  549.       in_bcc = isspace(*_temp_buffer);
  550.     if (in_bcc)
  551.       continue;
  552.     if (*_temp_buffer == '.') {
  553.       movmem(_temp_buffer, _temp_buffer + 1, sizeof(_temp_buffer) - 1);
  554.       *_temp_buffer = '.';
  555.       if (DEBUG)
  556.         output("\nSMTP> %s", _temp_buffer);
  557.     }
  558.     nbytes += sock_puts(SMTP_sock->sock, _temp_buffer);
  559.     nbytes += 2;
  560.     if (nbytes > rbytes) {
  561.       go_back(wherex(), pos);
  562.       output("%ld/%ld", nbytes, obytes);
  563.       rbytes += 512L;
  564.     }
  565.   }
  566.   sock_puts(SMTP_sock->sock, ".");
  567.   sock_wait_input(SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat);
  568.   while (sock_tbused(SMTP_sock->sock) > 0) {
  569.     SOCK_GETS(SMTP);
  570.     SMTP_FAIL_ON(SMTP_OOPS,);
  571.     SMTP_RESET_ON(SMTP_ERROR,);
  572.     SMTP_RESET_ON(SMTP_SQUEEZED,);
  573.     SMTP_RESET_ON(SMTP_FULL,);
  574.     SMTP_RESET_ON(SMTP_FAILED,);
  575.   }
  576.   go_back(wherex(), pos);
  577.   return 1;
  578.   SOCK_READ_ERR(SMTP);
  579.   go_back(wherex(), pos);
  580.   return 0;
  581. }
  582.  
  583. int smtp_shutdown(Mail_Socket * SMTP_sock)
  584. {
  585.   if (SMTP_sock->sock) {
  586.     sock_puts(SMTP_sock->sock, "QUIT");
  587.     sock_close(SMTP_sock->sock);
  588.     return 1;
  589.   }
  590.   free_Mail_Socket(SMTP_sock);
  591.   return 0;
  592. }
  593.  
  594.  
  595. Mail_Socket *pop_init(char *host)
  596. {
  597.   longword h;
  598.   Mail_Socket *POP_sock = NULL;
  599.  
  600.   if (!POP_Err_Msg)
  601.     POP_Err_Msg = _POP_err_msg_buf;
  602.  
  603.   if (!WatTCP_initialized) {
  604.     sock_init();
  605.     WatTCP_initialized = 1;
  606.   }
  607.   if (!(h = resolve(host))) {
  608.     output("\n ■ Cannot resolve %s.", host);
  609.     POP_Err_Cond = POP_BAD_HOST;
  610.     if (DEBUG)
  611.       sprintf(POP_Err_Msg, "Cannot resolve host %s", host);
  612.     return NULL;
  613.   }
  614.   if ((POP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL) {
  615.     output("\n ■ Insufficient memory to create socket... aborting.");
  616.     exit(EXIT_FAILURE);
  617.   }
  618.   if ((POP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
  619.     output("\n ■ Insufficient memory to create socket... aborting.");
  620.     farfree(POP_sock);
  621.     exit(EXIT_FAILURE);
  622.   }
  623.   if (!tcp_open(POP_sock->sock, 0, h, POP_PORT, NULL)) {
  624.     output("\n ■ Unable to connect to %s.", host);
  625.     POP_Err_Cond = POP_BAD_HOST;
  626.     if (DEBUG)
  627.       sprintf(POP_Err_Msg, "Unable to connect to host %s", host);
  628.     return NULL;
  629.   }
  630.   sock_mode(POP_sock->sock, TCP_MODE_ASCII);
  631.   sock_wait_established(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  632.   sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  633.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  634.   if (DEBUG)
  635.     output("\nPOP> %s", _temp_buffer);
  636.   if (*_temp_buffer != '+') {
  637.     output("\n ■ %s is unavailable.", host);
  638.     POP_Err_Cond = POP_HOST_UNAVAILABLE;
  639.     if (DEBUG)
  640.       sprintf(POP_Err_Msg, "%s unavailable", host);
  641.     if (POP_sock->sock) {
  642.       sock_puts(POP_sock->sock, "QUIT");
  643.       sock_close(POP_sock->sock);
  644.     }
  645.     return NULL;
  646.   }
  647.   return (POP_sock);
  648.   SOCK_READ_ERR(POP);
  649.   return NULL;
  650. }
  651.  
  652. int pop_login(Mail_Socket * POP_sock, char *userid, char *password)
  653. {
  654.   sprintf(_temp_buffer, "USER %s", userid);
  655.   sock_puts(POP_sock->sock, _temp_buffer);
  656.   sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  657.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  658.   if (DEBUG)
  659.     output("\nPOP> %s", _temp_buffer);
  660.   if (*_temp_buffer != '+') {
  661.     output("\n ■ Mailbox \"%s\" does not exist!", userid);
  662.     POP_Err_Cond = POP_BAD_MBOX;
  663.     if (DEBUG)
  664.       sprintf(POP_Err_Msg, "Mailbox %s does not exist", userid);
  665.     return 0;
  666.   }
  667.   sprintf(_temp_buffer, "PASS %s", password);
  668.   sock_puts(POP_sock->sock, _temp_buffer);
  669.   sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  670.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  671.   if (DEBUG)
  672.     output("\nPOP> %s", _temp_buffer);
  673.   if (*_temp_buffer != '+') {
  674.     output("\n ■ Password \"%s\" incorrect or account locked.", password);
  675.     POP_Err_Cond = POP_BAD_PASS;
  676.     if (DEBUG)
  677.       sprintf(POP_Err_Msg, "Incorrect password");
  678.     return 0;
  679.   }
  680.   return 1;
  681.   SOCK_READ_ERR(POP);
  682.   return 0;
  683. }
  684.  
  685. int pop_status(Mail_Socket * POP_sock, unsigned int *count, unsigned long *totallength)
  686. {
  687.   sock_puts(POP_sock->sock, "STAT");
  688.   sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  689.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  690.   if (DEBUG)
  691.     output("\nPOP> %s", _temp_buffer);
  692.   if ((*_temp_buffer != '+') ||
  693.       (getnumbers(_temp_buffer, count, totallength) < 2)) {
  694.     POP_Err_Cond = POP_UNKNOWN;
  695.     if (DEBUG) {
  696.       sprintf(POP_Err_Msg, "Unknown POP error");
  697.       output("\n%s", POP_Err_Msg);
  698.     }
  699.     return 0;
  700.   }
  701.   return 1;
  702.   SOCK_READ_ERR(POP);
  703.   return 0;
  704. }
  705.  
  706. long pop_length(Mail_Socket * POP_sock, unsigned int msg_num, unsigned long *size)
  707. {
  708.   unsigned int dummy;
  709.  
  710.   sprintf(_temp_buffer, "LIST %u", msg_num);
  711.   sock_puts(POP_sock->sock, _temp_buffer);
  712.   sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  713.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  714.   if (DEBUG)
  715.     output("\nPOP> %s", _temp_buffer);
  716.   if ((*_temp_buffer != '+') ||
  717.       (getnumbers(_temp_buffer, &dummy, size) < 2)) {
  718.     POP_Err_Cond = POP_NOT_MSG;
  719.     if (DEBUG) {
  720.       sprintf(POP_Err_Msg, "No message #%u", msg_num);
  721.       output("\n%s", POP_Err_Msg);
  722.     }
  723.     return 0;
  724.   }
  725.   return (*size);
  726.   SOCK_READ_ERR(POP);
  727.   return 0;
  728. }
  729.  
  730. char *stristr(char *String, char *Pattern)
  731. {
  732.   char *pptr, *sptr, *start;
  733.   unsigned int slen, plen;
  734.  
  735.   for (start = String, pptr = Pattern, slen = strlen(String),
  736.        plen = strlen(Pattern); slen >= plen; start++, slen--) {
  737.     while (toupper(*start) != toupper(*Pattern)) {
  738.       start++;
  739.       slen--;
  740.       if (slen < plen)
  741.         return (NULL);
  742.     }
  743.     sptr = start;
  744.     pptr = Pattern;
  745.     while (toupper(*sptr) == toupper(*pptr)) {
  746.       sptr++;
  747.       pptr++;
  748.       if ('\0' == *pptr)
  749.         return (start);
  750.     }
  751.   }
  752.   return (NULL);
  753. }
  754.  
  755. int pop_top(Mail_Socket * POP_sock, unsigned int msg_num)
  756. {
  757.   int okpkt;
  758.  
  759.   sprintf(_temp_buffer, "TOP %u 50", msg_num);
  760. /*  sock_printf(POP_sock->sock, "TOP %u 50\n", msg_num); */
  761.   sock_puts(POP_sock->sock, _temp_buffer);
  762.   sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  763.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  764.   if (DEBUG)
  765.     output("\nPOP> %s", _temp_buffer);
  766.   if (*_temp_buffer != '+') {
  767.     POP_Err_Cond = POP_NOT_MSG;
  768.     if (DEBUG) {
  769.       sprintf(POP_Err_Msg, "No message #%u.", msg_num);
  770.       output("\n%s", POP_Err_Msg);
  771.     }
  772.     return -1;
  773.   }
  774.   okpkt = 0;
  775.   while (1) {
  776.     sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  777.     sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  778.     if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0)
  779.       break;
  780.     if ((strnicmp(_temp_buffer, "begin 6", 7) == 0) &&
  781.         (stristr(_temp_buffer, "WINMAIL") == NULL)) {
  782.       if (okpkt != 4)
  783.         okpkt = 1;
  784.       if ((stristr(_temp_buffer, ".ZIP") != NULL) ||
  785.           (stristr(_temp_buffer, ".ARJ") != NULL) ||
  786.           (stristr(_temp_buffer, ".LZH") != NULL))
  787.         okpkt = 2;
  788.       if ((stristr(_temp_buffer, ".GIF") != NULL) ||
  789.           (stristr(_temp_buffer, ".JPG") != NULL))
  790.         okpkt = 3;
  791.     }
  792.     if (strnicmp(_temp_buffer, "from:", 5) == 0) {
  793.       if ((stristr(_temp_buffer, "mailer-daemon") != NULL) ||
  794.           (stristr(_temp_buffer, "mail delivery") != NULL) ||
  795.           (stristr(_temp_buffer, "administrator") != NULL) ||
  796.           (stristr(_temp_buffer, from_user) != NULL))
  797.         okpkt = 4;
  798.       else
  799.         strncpy(pktowner, &_temp_buffer[6], 20);
  800.     }
  801.   }
  802.   return okpkt;
  803.   SOCK_READ_ERR(POP);
  804.   return -1;
  805. }
  806.  
  807. int pop_getf(Mail_Socket * POP_sock, char *fn, unsigned int msg_num)
  808. {
  809.   unsigned long size;
  810.   long nbytes, rbytes;
  811.   int pos, ctld, len;
  812.   FILE *fp;
  813.  
  814.   if (!pop_length(POP_sock, msg_num, &size)) {
  815.     output("\n ■ Unable to retrieve message number %d", msg_num);
  816.     return 0;
  817.   }
  818.   sprintf(_temp_buffer, "RETR %u", msg_num);
  819.   sock_puts(POP_sock->sock, _temp_buffer);
  820.   sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  821.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  822.   if (DEBUG)
  823.     output("\nPOP> %s", _temp_buffer);
  824.   if (*_temp_buffer != '+') {
  825.     POP_Err_Cond = POP_NOT_MSG;
  826.     if (DEBUG) {
  827.       sprintf(POP_Err_Msg, "No message #%u", msg_num);
  828.       output("\n%s", POP_Err_Msg);
  829.     }
  830.     return 0;
  831.   }
  832.   nbytes = 0L;
  833.   rbytes = 1024L;
  834.   output(" - ");
  835.   pos = wherex();
  836.   if ((fp = fsh_open(fn, "w")) == NULL) {
  837.     output("\n ■ Unable to create %s... aborting!", fn);
  838.     return 0;
  839.   }
  840.   ctld = 1;
  841.   while (1) {
  842.     sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  843.     len = sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  844.     if ((ctld == 1) && (len == 0))
  845.       ctld = 0;
  846.     if ((strnicmp(_temp_buffer, "begin ", 6) == 0) &&
  847.         (stristr(_temp_buffer, "WINMAIL") != NULL))
  848.       ctld = 2;
  849.     if ((ctld == 2) && (strnicmp(_temp_buffer, "end", 3) == 0))
  850.       ctld = 0;
  851.     if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0)
  852.       break;
  853.     if (EOF == (nbytes += fprintf(fp, "%s%s\n", ctld ? "0R" : "", _temp_buffer))) {
  854.       if (fp != NULL)
  855.         fclose(fp);
  856.       return 0;
  857.     }
  858.     if (nbytes > rbytes) {
  859.       go_back(wherex(), pos);
  860.       output("%ld/%ld", nbytes, size);
  861.       rbytes += 512L;
  862.     }
  863.   }
  864.   if (fp != NULL)
  865.     fclose(fp);
  866.   go_back(wherex(), pos);
  867.   output("message received!");
  868.   return 1;
  869.   SOCK_READ_ERR(POP);
  870.   if (fp != NULL)
  871.     fclose(fp);
  872.   return 0;
  873. }
  874.  
  875. int pop_delete(Mail_Socket * POP_sock, unsigned int msg_num)
  876. {
  877.   sprintf(_temp_buffer, "DELE %u", msg_num);
  878.   sock_puts(POP_sock->sock, _temp_buffer);
  879.   sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  880.   sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  881.   if (DEBUG)
  882.     output("\nPOP> %s", _temp_buffer);
  883.   if (*_temp_buffer != '+') {
  884.     POP_Err_Cond = POP_NOT_MSG;
  885.     if (DEBUG) {
  886.       sprintf(POP_Err_Msg, "No message #%u", msg_num);
  887.       output("\n%s", POP_Err_Msg);
  888.     }
  889.     return 2;
  890.   }
  891.   return 1;
  892.   SOCK_READ_ERR(POP);
  893.   return 0;
  894. }
  895.  
  896.  
  897. int pop_shutdown(Mail_Socket * POP_sock)
  898. {
  899.   if (POP_sock->sock) {
  900.     sock_puts(POP_sock->sock, "QUIT");
  901.     sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
  902.     sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
  903.     if (*_temp_buffer != '+') {
  904.       POP_Err_Cond = POP_UNKNOWN;
  905.       if (DEBUG) {
  906.         sprintf(POP_Err_Msg, "Unable to update mailbox!");
  907.         output("\n%s", POP_Err_Msg);
  908.     }
  909.       return 0;
  910.     } else
  911.       output("\n ■ Closed and updated mailbox on %s.", POPHOST);
  912.     sock_close(POP_sock->sock);
  913.     return 1;
  914.   }
  915. sock_err:
  916.   free_Mail_Socket(POP_sock);
  917.   return 0;
  918. }
  919.  
  920. int pop_get_nextf(Mail_Socket * POP_sock, char *fn, int msgnum)
  921. {
  922.   if (!pop_getf(POP_sock, fn, msgnum))
  923.     return 0;
  924.   return (pop_delete(POP_sock, msgnum));
  925. }
  926.  
  927. int exist(char *s)
  928. {
  929.   int i;
  930.   struct ffblk ff;
  931.  
  932.   i = findfirst(s, &ff, FA_HIDDEN);
  933.   if (i)
  934.     return 0;
  935.   else
  936.     return 1;
  937. }
  938.  
  939. void main(int argc, char *argv[])
  940. {
  941.   unsigned int count;
  942.   unsigned long size;
  943.   int i, i1, okpkt;
  944.   char temp[181], s[21], s1[21];
  945.   FILE *fp;
  946.   Mail_Socket *pop_sock = NULL;
  947.   Mail_Socket *smtp_sock = NULL;
  948.  
  949.   detect_multitask();
  950.  
  951.   if (strncmpi(argv[1], "-send", strlen(argv[1])) == 0) {
  952.     if (argc < 5)
  953.       exit(EXIT_FAILURE);
  954.     output("- %s - [Space] skips", argv[2]);
  955.     if (argc == 6)
  956.       DEBUG = atoi(argv[5]);
  957.     if ((smtp_sock = smtp_start(argv[2], argv[3])) != NULL) {
  958.       if ((fp = fsh_open(argv[4], "r")) != NULL) {
  959.         switch (smtp_sendf(smtp_sock, fp)) {
  960.           case 1:
  961.             output("message sent!");
  962.             smtp_shutdown(smtp_sock);
  963.             if (fp != NULL)
  964.               fclose(fp);
  965.             exit(EXIT_SUCCESS);
  966.           case 0:
  967.             output(" - SMTP failure!");
  968.             smtp_shutdown(smtp_sock);
  969.             if (fp != NULL)
  970.               fclose(fp);
  971.             break;
  972.           case -1:
  973.             output("SMTP aborted!");
  974.             smtp_shutdown(smtp_sock);
  975.             if (fp != NULL)
  976.               fclose(fp);
  977.             break;
  978.         }
  979.       } else {
  980.         smtp_shutdown(smtp_sock);
  981.         output("\n ■ Error accessing file %s.", argv[4]);
  982.       }
  983.     } else
  984.       output("\n ■ SMTP socket connect failed.");
  985.   } else if (strncmpi(argv[1], "-receive", strlen(argv[1])) == 0) {
  986.     if (argc < 8) {
  987.       output("Received: ");
  988.       for (i = 0; i < argc; i++)
  989.         output("%s ", argv[i]);
  990.       exit(EXIT_FAILURE);
  991.     }
  992.     if (argc == 9)
  993.       DEBUG = atoi(argv[8]);
  994.     sprintf(from_user, "%s@%s", argv[3], argv[7]);
  995.     strcpy(POPHOST, argv[2]);
  996.     if ((pop_sock = pop_init(argv[2])) != NULL) {
  997.       if (pop_login(pop_sock, argv[3], argv[4])) {
  998.         if (pop_status(pop_sock, &count, &size)) {
  999.           okpkt = 0;
  1000.           output("%u message%s (%ldK).\n", count, count == 1 ? "" : "s", ((size + 1023)/ 1024));
  1001.           i1 = 1;
  1002.           while (i1 <= count) {
  1003.             okpkt = -1;
  1004.             okpkt = pop_top(pop_sock, i1);
  1005.             switch (okpkt) {
  1006.               case -1:
  1007.                 output("\n ■ Error accessing message %d", i1);
  1008.                 break;
  1009.               case 0:
  1010.                 i = atoi(argv[6]);
  1011.                 if (i == 0)
  1012.                   output("\n ■ Non-network message %d left on server.", i1);
  1013.                 else {
  1014.                   i = 0;
  1015.                   sprintf(temp, "%sUNK-%03d.MSG", argv[5], i);
  1016.                   while (exist(temp))
  1017.                     sprintf(temp, "%sUNK-%03d.MSG", argv[5], ++i);
  1018.                   fnsplit(temp, NULL, NULL, s, s1);
  1019.                   go_back(wherex(), 0);
  1020.                   output(" ■ RCV : %3.3d - %-20s - %s%s", i1, "non-network packet", s, s1);
  1021.                   switch (pop_get_nextf(pop_sock, temp, i1)) {
  1022.                     case 0:
  1023.                       output("\n ■ Unable to retrieve message %d.", i1);
  1024.                       unlink(temp);
  1025.                       break;
  1026.                     case 1:
  1027.                       break;
  1028.                     case 2:
  1029.                       output("\n ■ Unable to delete message %d from host!", i1);
  1030.                       break;
  1031.                   }
  1032.                 }
  1033.                 break;
  1034.               case 1:
  1035.                 i = 0;
  1036.                 sprintf(temp, "%sPKT-%03d.UUE", argv[5], i);
  1037.                 while (exist(temp))
  1038.                   sprintf(temp, "%sPKT-%03d.UUE", argv[5], ++i);
  1039.                 fnsplit(temp, NULL, NULL, s, s1);
  1040.                 go_back(wherex(), 0);
  1041.                 output(" ■ RCV : %3.3d - %-20s - %s%s", i1, pktowner, s, s1);
  1042.                 switch (pop_get_nextf(pop_sock, temp, i1)) {
  1043.                   case 0:
  1044.                     output("\n ■ Unable to retrieve message %d.", i1);
  1045.                     unlink(temp);
  1046.                     break;
  1047.                   case 1:
  1048.                     break;
  1049.                   case 2:
  1050.                     output("\n ■ Unable to delete message %d on host!", i1);
  1051.                     break;
  1052.                 }
  1053.                 break;
  1054.               case 2:
  1055.                 i = 0;
  1056.                 sprintf(temp, "%sARC-%03d.UUE", argv[5], i);
  1057.                 while (exist(temp))
  1058.                   sprintf(temp, "%sARC-%03d.UUE", argv[5], ++i);
  1059.                 fnsplit(temp, NULL, NULL, s, s1);
  1060.                 go_back(wherex(), 0);
  1061.                 output(" ■ RCV : %3.3d - %-20s - %s%s", i1, "archived file", s, s1);
  1062.                 switch (pop_get_nextf(pop_sock, temp, i1)) {
  1063.                   case 0:
  1064.                     output("\n ■ Unable to retrieve message %d.", i1);
  1065.                     unlink(temp);
  1066.                     break;
  1067.                   case 1:
  1068.                     break;
  1069.                   case 2:
  1070.                     output("\n ■ Unable to delete message %d on host!", i1);
  1071.                     break;
  1072.                 }
  1073.                 break;
  1074.               case 3:
  1075.                 i = 0;
  1076.                 sprintf(temp, "%sGIF-%03d.UUE", argv[5], i);
  1077.                 while (exist(temp))
  1078.                   sprintf(temp, "%sGIF-%03d.UUE", argv[5], ++i);
  1079.                 fnsplit(temp, NULL, NULL, s, s1);
  1080.                 go_back(wherex(), 0);
  1081.                 output(" ■ RCV : %3.3d - %-20s - %s%s", i1, "graphic/image file", s, s1);
  1082.                 switch (pop_get_nextf(pop_sock, temp, i1)) {
  1083.                   case 0:
  1084.                     output("\n ■ Unable to retrieve message %d.", i1);
  1085.                     unlink(temp);
  1086.                     break;
  1087.                   case 1:
  1088.                     break;
  1089.                   case 2:
  1090.                     output("\n ■ Unable to delete message %d from host!", i1);
  1091.                     break;
  1092.                 }
  1093.                 break;
  1094.               case 4:
  1095.                 i = 0;
  1096.                 sprintf(temp, "%sBAD-%03d.UUE", argv[5], i);
  1097.                 while (exist(temp))
  1098.                   sprintf(temp, "%sBAD-%03d.UUE", argv[5], ++i);
  1099.                 fnsplit(temp, NULL, NULL, s, s1);
  1100.                 go_back(wherex(), 0);
  1101.                 output(" ■ RCV : %3.3d - %-20s - %s%s", i1, "mailer-daemon", s, s1);
  1102.                 switch (pop_get_nextf(pop_sock, temp, i1)) {
  1103.                   case 0:
  1104.                     output("\n ■ Unable to retrieve message %d.", i1);
  1105.                     unlink(temp);
  1106.                     break;
  1107.                   case 1:
  1108.                     break;
  1109.                   case 2:
  1110.                     output("\n ■ Unable to delete message %d from host!", i1);
  1111.                     break;
  1112.                 }
  1113.                 break;
  1114.             }
  1115.             i1++;
  1116.           }
  1117.           backline();
  1118.           output("\r ■ Mailbox scan of %d message%s completed.", count,
  1119.             count == 1 ? "" : "s");
  1120.         } else
  1121.           output("\n ■ Unknown POP access error - try again later.");
  1122.         pop_shutdown(pop_sock);
  1123.         fcloseall();
  1124.         exit(EXIT_SUCCESS);
  1125.       } else {
  1126.         output("\n ■ Unable to log into POP server!");
  1127.         pop_shutdown(pop_sock);
  1128.         fcloseall();
  1129.       }
  1130.     } else
  1131.       output("\n ■ POP socket connect failed.");
  1132.     }
  1133.   exit(EXIT_FAILURE);
  1134. }
  1135.