home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B84.ZIP / NETWORK.CPP < prev    next >
C/C++ Source or Header  |  1998-01-04  |  45KB  |  1,825 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <dos.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <ctype.h>
  9. #include <io.h>
  10. #include <conio.h>
  11. #include <share.h>
  12. #include <errno.h>
  13. #include <malloc.h>
  14. #include <dir.h>
  15. #include <time.h>
  16. #include <process.h>
  17. #include <direct.h>
  18. #include "vardec.h"
  19. #include "net.h"
  20. #include "version.h"
  21. #include "retcode.h"
  22.  
  23. extern unsigned _stklen = 15000U;
  24.  
  25. #define NUL '\0'
  26. #define LAST(s) s[strlen(s)-1]
  27.  
  28. #define SHARE_LEVEL 10
  29. #define WAIT_TIME 10
  30. #define TRIES 100
  31.  
  32. #define MT_DESQVIEW 0x01
  33. #define MT_WINDOWS  0x02
  34. #define MT_OS2      0x04
  35. #define MT_NB       0x40
  36.  
  37. #define INI_NETWORK   0x01
  38. #define INI_GENERAL   0x02
  39. #define INI_NEWS      0x04
  40.  
  41. typedef struct {
  42.   unsigned short sysnum;
  43.   char address[60];
  44. } net_address_rec;
  45.  
  46. char *version = "Freeware PPP Project Net Packet Engine " VERSION " (" __DATE__ ")";
  47. char *author = "Contact Frank Reid at edare@ix.netcom.com or 1@8213.WWIVnet for support";
  48.  
  49. char *strings[] = {
  50.   "Unable to create",
  51.   "Unable to read",
  52.   "Unknown error",
  53.   "Unable to write CONTACT.NET!",
  54.   "Unable to write NET.LOG!",
  55.   0L,
  56. };
  57.  
  58. configrec syscfg;
  59. configoverrec syscfgovr;
  60. net_networks_rec netcfg;
  61. net_system_list_rec *netsys;
  62. int multitasker = 0, ini_section = 0, num_addr;
  63. char wwiv_net_no[20], tasker[40], maindir[MAXPATH];
  64. char net_name[16], net_data[MAXPATH];
  65. unsigned short net_sysnum;
  66. int num_sys_list, net_num, netnum;
  67. int instance, lsl_loaded;
  68.  
  69. char SMTPHOST[40], POPHOST[40], NEWSHOST[40], POPNAME[20], POPPASS[20];
  70. char IPADDR[18], NETMASK[18], FWDNAME[20], FWDDOM[40], TIMEHOST[40], DNS[16];
  71. char NODEPASS[20], SDNS[16], DOMAIN[40], GATEWAY[16], temp_dir[MAXPATH];
  72. unsigned int KEEPSENT = 0, ALLMAIL = 1, CLEANUP = 0, MOREINFO = 0;
  73. unsigned int TIMEOUT = 60, SOCK_DELAY = 30, INACTIVE = 60;
  74. unsigned int ONECALL = 0, PURGE = 1, BBSNODE = 1;
  75.  
  76. char *xenviron[20];
  77.  
  78. void dv_pause(void)
  79. {
  80.   __emit__(0xb8, 0x1a, 0x10, 0xcd, 0x15);
  81.   __emit__(0xb8, 0x00, 0x10, 0xcd, 0x15);
  82.   __emit__(0xb8, 0x25, 0x10, 0xcd, 0x15);
  83. }
  84.  
  85. void win_pause(void)
  86. {
  87.   __emit__(0x55, 0xb8, 0x80, 0x16, 0xcd, 0x2f, 0x5d);
  88. }
  89.  
  90. int get_dos_version(void)
  91. {
  92.   _AX = 0x3000;
  93.   geninterrupt(0x21);
  94.   if (_AX % 256 >= 10) {
  95.     multitasker |= MT_OS2;
  96.   }
  97.   return (_AX);
  98. }
  99.  
  100. int get_dv_version(void)
  101. {
  102.   int v;
  103.  
  104.   if (multitasker & MT_OS2)
  105.     return 0;
  106.   _AX = 0x2b01;
  107.   _CX = 0x4445;
  108.   _DX = 0x5351;
  109.   geninterrupt(0x21);
  110.   if (_AL == 0xff) {
  111.     return 0;
  112.   } else {
  113.     v = _BX;
  114.     multitasker |= MT_DESQVIEW;
  115.     return v;
  116.   }
  117. }
  118.  
  119. int get_win_version(void)
  120. {
  121.   int v = 0;
  122.  
  123.   __emit__(0x55, 0x06, 0x53);
  124.   _AX = 0x352f;
  125.   geninterrupt(0x21);
  126.   _AX = _ES;
  127.   if (_AX | _BX) {
  128.     _AX = 0x1600;
  129.     geninterrupt(0x2f);
  130.     v = _AX;
  131.     if (v % 256 <= 1)
  132.       v = 0;
  133.   }
  134.   __emit__(0x5b, 0x07, 0x5d);
  135.   if (v != 0)
  136.     multitasker |= MT_WINDOWS;
  137.   return (v);
  138. }
  139.  
  140. int get_nb_version(void)
  141. {
  142.   _AX = 0;
  143.   geninterrupt(0x2A);
  144.   return (_AH);
  145. }
  146.  
  147. void detect_multitask(void)
  148. {
  149.   get_dos_version();
  150.   get_win_version();
  151.   get_dv_version();
  152.   if (multitasker < 2)
  153.     if (get_nb_version())
  154.       multitasker = MT_NB;
  155. }
  156.  
  157. void giveup_timeslice(void)
  158. {
  159.   if (multitasker) {
  160.     switch (multitasker) {
  161.       case 1:
  162.       case 3:
  163.         dv_pause();
  164.         break;
  165.       case 2:
  166.       case 4:
  167.       case 5:
  168.       case 6:
  169.       case 7:
  170.         win_pause();
  171.         break;
  172.       default:
  173.         break;
  174.     }
  175.   }
  176. }
  177.  
  178. char *stripspace(char *str)
  179. {
  180.   char *obuf, *nbuf;
  181.  
  182.   if (str) {
  183.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  184.       if (!isspace(*obuf))
  185.         *nbuf++ = *obuf;
  186.     }
  187.     *nbuf = NULL;
  188.   }
  189.   return (str);
  190. }
  191.  
  192. int sh_write(int handle, void *buffer, unsigned long len)
  193. {
  194.   if (handle == -1) {
  195.     return (-1);
  196.   }
  197.   return (write(handle, buffer, (unsigned) len));
  198. }
  199.  
  200. int sh_open(char *path, int file_access, unsigned fmode)
  201. {
  202.   int handle, count, share;
  203.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  204.  
  205.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
  206.     share = SH_DENYRW;
  207.   } else {
  208.     share = SH_DENYWR;
  209.   }
  210.   handle = open(path, file_access | share, fmode);
  211.   if (handle < 0) {
  212.     count = 1;
  213.     fnsplit(path, drive, dir, file, ext);
  214.     if (access(path, 0) != -1) {
  215.       delay(WAIT_TIME);
  216.       handle = open(path, file_access | share, fmode);
  217.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  218.         if (count % 2)
  219.           delay(WAIT_TIME);
  220.         else
  221.           giveup_timeslice();
  222.         count++;
  223.         handle = open(path, file_access | share, fmode);
  224.       }
  225.     }
  226.   }
  227.   return (handle);
  228. }
  229.  
  230. int sh_open1(char *path, int access)
  231. {
  232.   unsigned fmode;
  233.  
  234.   fmode = 0;
  235.   if ((access & O_RDWR) || (access & O_WRONLY))
  236.     fmode |= S_IWRITE;
  237.   if ((access & O_RDWR) || (access & O_RDONLY))
  238.     fmode |= S_IREAD;
  239.   return (sh_open(path, access, fmode));
  240. }
  241.  
  242. int sh_close(int f)
  243. {
  244.   if (f != -1)
  245.     close(f);
  246.   return (-1);
  247. }
  248.  
  249. int sh_read(int handle, void *buf, unsigned len)
  250. {
  251.   if (handle == -1) {
  252.     return (-1);
  253.   }
  254.   return (read(handle, buf, len));
  255. }
  256.  
  257. long sh_lseek(int handle, long offset, int fromwhere)
  258. {
  259.   if (handle == -1) {
  260.     return (-1L);
  261.   }
  262.   return (lseek(handle, offset, fromwhere));
  263. }
  264.  
  265. FILE *fsh_open(char *path, char *fmode)
  266. {
  267.   FILE *f;
  268.   int count, share, md, fd;
  269.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  270.  
  271.   share = SH_DENYWR;
  272.   md = 0;
  273.   if (((char *) _fstrchr(fmode, 'w')) != NULL) {
  274.     share = SH_DENYRD;
  275.     md = O_RDWR | O_CREAT | O_TRUNC;
  276.   } else
  277.     if (((char *) _fstrchr(fmode, 'a')) != NULL) {
  278.     share = SH_DENYRD;
  279.     md = O_RDWR | O_CREAT;
  280.   } else {
  281.     md = O_RDONLY;
  282.   }
  283.   if (((char *) _fstrchr(fmode, 'b')) != NULL) {
  284.     md |= O_BINARY;
  285.   }
  286.   if (((char *) _fstrchr(fmode, '+')) != NULL) {
  287.     md &= ~O_RDONLY;
  288.     md |= O_RDWR;
  289.     share = SH_DENYRD;
  290.   }
  291.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  292.   if (fd < 0) {
  293.     count = 1;
  294.     fnsplit(path, drive, dir, file, ext);
  295.     if ((access(path, 0)) != -1) {
  296.       delay(WAIT_TIME);
  297.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  298.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  299.         delay(WAIT_TIME);
  300.         count++;
  301.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  302.       }
  303.     }
  304.   }
  305.   if (fd > 0) {
  306.     if (((char *) _fstrchr(fmode, 'a')) != NULL)
  307.       sh_lseek(fd, 0L, SEEK_END);
  308.     f = fdopen(fd, fmode);
  309.     if (!f) {
  310.       close(fd);
  311.     }
  312.   } else
  313.     f = NULL;
  314.   return (f);
  315. }
  316.  
  317. void output(char *fmt,...)
  318. {
  319.   va_list v;
  320.   char s[255];
  321.  
  322.   va_start(v, fmt);
  323.   vsprintf(s, fmt, v);
  324.   va_end(v);
  325.   fputs(s, stderr);
  326. }
  327.  
  328. int log_it(int display, char *fmt,...)
  329. {
  330.   va_list v;
  331.   char s[255], fn[MAXPATH];
  332.   FILE *fp;
  333.  
  334.   va_start(v, fmt);
  335.   vsprintf(s, fmt, v);
  336.   va_end(v);
  337.   sprintf(fn, "%sNEWS.LOG", net_data);
  338.   if ((fp = fsh_open(fn, "at")) == NULL) {
  339.     output("\n ! Error accessing %s.", fn);
  340.     output("\n ! String = \"%s\".", s);
  341.     return 1;
  342.   }
  343.   fputs(s, fp);
  344.   if (fp != NULL)
  345.     fclose(fp);
  346.   if (display)
  347.     fputs(s, stderr);
  348.   return 0;
  349. }
  350.  
  351. void backline(void)
  352. {
  353.   int i;
  354.  
  355.   output(" ");
  356.   for (i = wherex(); i > 0; i--)
  357.     output("\b \b");
  358. }
  359.  
  360.  
  361. int do_spawn(char *cl)
  362. {
  363.   int i, i1, l;
  364.   char *ss1, *ss[10];
  365.  
  366.   for (i = 0; i < 10; i++)
  367.     ss[i] = NULL;
  368.   ss1 = strdup(cl);
  369.   *ss = ss1;
  370.   i = 1;
  371.   l = strlen(ss1);
  372.   for (i1 = 1; i1 < l; i1++) {
  373.     if (ss1[i1] == 32) {
  374.       ss1[i1] = 0;
  375.       ss[i++] = &(ss1[i1 + 1]);
  376. //      fprintf(stderr, "\n%s", ss[i-1]);
  377.     }
  378.   }
  379.   ss[i] = NULL;
  380.   i = (spawnvpe(P_WAIT, *ss, ss, xenviron) & 0x00ff);
  381.   if (ss1 != NULL)
  382.     free(ss1);
  383.   return i;
  384. }
  385.  
  386. #ifndef PACKET
  387. void unload_klos(void)
  388. {
  389.   char s[160];
  390.  
  391.   sprintf(s, "%s\\IPSTUB U", maindir);
  392.   do_spawn(s);
  393.   sprintf(s, "%s\\PPP U", maindir);
  394.   do_spawn(s);
  395.   if (!lsl_loaded) {
  396.     sprintf(s, "%s\\LSL U", maindir);
  397.     do_spawn(s);
  398.   }
  399. }
  400.  
  401. #endif
  402.  
  403. void cd_to(char *s)
  404. {
  405.   char *s1;
  406.   int i, db;
  407.  
  408.   s1 = s;
  409.   i = strlen(s1) - 1;
  410.   db = (s1[i] == '\\');
  411.   if (i == 0)
  412.     db = 0;
  413.   if ((i == 2) && (s1[1] == ':'))
  414.     db = 0;
  415.   if (db)
  416.     s1[i] = 0;
  417.   chdir(s1);
  418.   if (s[1] == ':')
  419.     setdisk(s[0] - 'A');
  420. }
  421.  
  422. void get_dir(char *s, int be)
  423. {
  424.   strcpy(s, "X:\\");
  425.   s[0] = 'A' + getdisk();
  426.   getcurdir(0, s + 3);
  427.   if (be) {
  428.     if (s[strlen(s) - 1] != '\\')
  429.       strcat(s, "\\");
  430.   }
  431. }
  432.  
  433. char *trimstr1(char *s)
  434. {
  435.   int i;
  436.   static char *whitespace = " \r\n\t";
  437.  
  438.   i = strlen(s);
  439.   if (i) {
  440.     while ((i > 0) && (_fstrchr(whitespace, s[i - 1])))
  441.       --i;
  442.     while ((i > 0) && (_fstrchr(whitespace, *s))) {
  443.       memmove(s, s + 1, --i);
  444.     }
  445.     s[i] = NUL;
  446.   }
  447.   return (s);
  448. }
  449.  
  450. char *make_abs_path(char *checkdir)
  451. {
  452.   char newdir[121];
  453.  
  454.   if ((strlen(checkdir) < 3) || (checkdir[1] != ':') || (checkdir[2] != '\\')) {
  455.     cd_to(maindir);
  456.     cd_to(checkdir);
  457.     if (LAST(checkdir) == '\\')
  458.       get_dir(newdir, 1);
  459.     else
  460.       get_dir(newdir, 0);
  461.     cd_to(maindir);
  462.     strcpy(checkdir, newdir);
  463.   }
  464.   return checkdir;
  465. }
  466.  
  467. int exist(char *s)
  468. {
  469.   int i;
  470.   struct ffblk ff;
  471.  
  472.   i = findfirst(s, &ff, FA_HIDDEN);
  473.   if (i)
  474.     return 0;
  475.   else
  476.     return 1;
  477. }
  478.  
  479. void set_net_num(int n)
  480. {
  481.   char s[121];
  482.   int f;
  483.  
  484.   sprintf(s, "%sNETWORKS.DAT", syscfg.datadir);
  485.   f = sh_open1(s, O_RDONLY | O_BINARY);
  486.   if (f < 0)
  487.     return;
  488.   lseek(f, ((long) (n)) * sizeof(net_networks_rec), SEEK_SET);
  489.   sh_read(f, &netcfg, sizeof(net_networks_rec));
  490.   close(f);
  491.   net_num = n;
  492.   net_sysnum = netcfg.sysnum;
  493.   strcpy(net_name, netcfg.name);
  494.   strcpy(net_data, make_abs_path(netcfg.dir));
  495.   if (LAST(net_data) != '\\')
  496.     strcat(net_data, "\\");
  497.   sprintf(wwiv_net_no, "WWIV_NET=%d", net_num);
  498. }
  499.  
  500. int make_path(char *s)
  501. {
  502.   unsigned int i;
  503.   char drive, current_path[MAXPATH], current_drive, *p, *flp;
  504.   union REGS r;
  505.  
  506.   p = flp = strdup(s);
  507.   _getdcwd(0, current_path, MAXPATH);
  508.   current_drive = *current_path - '@';
  509.   if (LAST(s) == '\\')
  510.     LAST(s) = 0;
  511.   if (p[1] == ':') {
  512.     drive = toupper(p[0]) - 'A' + 1;
  513.     if ((_osmajor == 3 && _osminor >= 1) || (_osmajor > 3)) {
  514.       r.x.ax = 0x4409;
  515.       r.h.bl = drive;
  516.       int86(0x21, &r, &r);
  517.       if (r.x.cflag)
  518.         return -3;
  519.     }
  520.     if (_chdrive(drive)) {
  521.       chdir(current_path);
  522.       _dos_setdrive(current_drive, &i);
  523.       return -2;
  524.     }
  525.     p += 2;
  526.   }
  527.   if (*p == '\\') {
  528.     chdir("\\");
  529.     p++;
  530.   }
  531.   for (; (p = strtok(p, "\\")) != 0; p = 0) {
  532.     if (chdir(p)) {
  533.       if (mkdir(p)) {
  534.         chdir(current_path);
  535.         _dos_setdrive(current_drive, &i);
  536.         return -1;
  537.       }
  538.       chdir(p);
  539.     }
  540.   }
  541.   chdir(current_path);
  542.   if (flp)
  543.     free(flp);
  544.   return 0;
  545. }
  546.  
  547. void process_mail(void)
  548. {
  549.   char s[201];
  550.  
  551.   output("\n ■ Network clean-up... please wait... ");
  552.   sprintf(s, "%s\\FLINK.EXE", maindir);
  553.   if (exist(s))
  554.     do_spawn(s);
  555.   sprintf(s, "%s\\LINKER.EXE", maindir);
  556.   if (exist(s))
  557.     do_spawn(s);
  558.   output("done.");
  559. }
  560.  
  561. int valid_system(unsigned int ts)
  562. {
  563.   int i;
  564.  
  565.   for (i = 0; i < num_sys_list; i++) {
  566.     if (netsys[i].sysnum == ts) {
  567.       if (netsys[i].numhops == 1)
  568.         return (1);
  569.       else
  570.         return (0);
  571.     }
  572.   }
  573.   return (0);
  574. }
  575.  
  576. int create_contact_ppp(void)
  577. {
  578.   char *ss, fn[101], s[81];
  579.   unsigned short node;
  580.   int curfile, i;
  581.   net_address_rec curaddr;
  582.   FILE *fp, *fc;
  583.  
  584.   sprintf(fn, "%sCONTACT.PPP", net_data);
  585.   if ((fc = fsh_open(fn, "wb+")) == NULL) {
  586.     output("\n ! %s %s!\n", strings[0], fn);
  587.     return 1;
  588.   }
  589.   curfile = 1;
  590.   num_addr = 0;
  591.   do {
  592.     sprintf(fn, "%sADDRESS.%d", net_data, curfile);
  593.     if ((fp = fsh_open(fn, "rt")) == NULL)
  594.       curfile = -1;
  595.     while ((curfile > 0) && (fgets(s, 80, fp) != NULL)) {
  596.       if (s[0] != '@')
  597.         continue;
  598.       ss = strtok(s, " \t\r");
  599.       node = atoi(&ss[1]);
  600.       if (node) {
  601.         ss = strtok(NULL, " \t\r");
  602.         trimstr1(ss);
  603.         curaddr.sysnum = node;
  604.         strcpy(curaddr.address, ss);
  605.         if (ss[0]) {
  606.           fwrite(&curaddr, sizeof(net_address_rec), 1, fc);
  607.           ++num_addr;
  608.         }
  609.       }
  610.     }
  611.     if (fp != NULL)
  612.       fclose(fp);
  613.     ++curfile;
  614.   } while (curfile > 0);
  615.   fseek(fc, 0L, SEEK_SET);
  616.   sprintf(fn, "%sADDRESS.0", net_data);
  617.   if ((fp = fsh_open(fn, "rt")) == NULL) {
  618.     if (fc != NULL)
  619.       fclose(fc);
  620.     return (0);
  621.   }
  622.   while (fgets(s, 80, fp) != NULL) {
  623.     if (s[0] != '@')
  624.       continue;
  625.     ss = strtok(s, " \t\r");
  626.     node = atoi(&ss[1]);
  627.     do {
  628.       ss = strtok(NULL, " \t\r");
  629.       switch (ss[0]) {
  630.         case '-':
  631.           for (i = 0; i < num_addr; i++) {
  632.             fseek(fc, (long) i * sizeof(net_address_rec), SEEK_SET);
  633.             fread(&curaddr, sizeof(net_address_rec), 1, fc);
  634.             if (node == curaddr.sysnum) {
  635.               curaddr.sysnum = 0;
  636.               fseek(fc, (long) i * sizeof(net_address_rec), SEEK_SET);
  637.               fwrite(&curaddr, sizeof(net_address_rec), 1, fc);
  638.             }
  639.           }
  640.           break;
  641.       }
  642.     } while (ss);
  643.   }
  644.   if (fp != NULL)
  645.     fclose(fp);
  646.   if (fc != NULL)
  647.     fclose(fc);
  648.   return (0);
  649. }
  650.  
  651. void good_addr(char *address, unsigned short sn)
  652. {
  653.   char fn[101];
  654.   int i;
  655.   net_address_rec curaddr;
  656.   FILE *fc;
  657.  
  658.   address[0] = 0;
  659.   sprintf(fn, "%sCONTACT.PPP", net_data);
  660.   if ((fc = fsh_open(fn, "rb+")) == NULL) {
  661.     log_it(1, "\n ■ %s %s!\n", strings[1], fn);
  662.     return;
  663.   }
  664.   for (i = 0; ((i < num_addr) && (!address[0])); i++) {
  665.     fseek(fc, (long) i * sizeof(net_address_rec), SEEK_SET);
  666.     fread(&curaddr, sizeof(net_address_rec), 1, fc);
  667.     if (sn == curaddr.sysnum)
  668.       strcpy(address, curaddr.address);
  669.   }
  670.   if (fc != NULL)
  671.     fclose(fc);
  672. }
  673.  
  674. int copyfile(char *infn, char *outfn)
  675. {
  676.   int f1, f2, i;
  677.   char *b;
  678.   struct ftime ft;
  679.  
  680.   if ((strcmp(infn, outfn) != 0) && (exist(infn)) && (!exist(outfn))) {
  681.     if ((b = (char *) farmalloc(16400)) == NULL)
  682.       return 0;
  683.     f1 = sh_open1(infn, O_RDONLY | O_BINARY);
  684.     if (!f1) {
  685.       farfree(b);
  686.       b = NULL;
  687.       return 0;
  688.     }
  689.     getftime(f1, &ft);
  690.     f2 = sh_open(outfn, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  691.     if (!f2) {
  692.       farfree(b);
  693.       b = NULL;
  694.       f1 = sh_close(f1);
  695.       return 0;
  696.     }
  697.     i = sh_read(f1, (char *) b, 16384);
  698.     while (i > 0) {
  699.       sh_write(f2, (char *) b, i);
  700.       i = sh_read(f1, (char *) b, 16384);
  701.     }
  702.     f1 = sh_close(f1);
  703.     setftime(f2, &ft);
  704.     f2 = sh_close(f2);
  705.     farfree(b);
  706.     b = NULL;
  707.   }
  708.   return 1;
  709. }
  710.  
  711. void do_exit(int exitlevel)
  712. {
  713.   delay(2000);
  714.   exit(2 - exitlevel);
  715. }
  716.  
  717. int uu(char *fn, char *pktname, char *dest)
  718. {
  719.   int ok;
  720.   char s[121], s1[201], s2[81];
  721.   FILE *fp;
  722.  
  723.   sprintf(s, "%sMQUEUE\\%s.UUE", net_data, pktname);
  724.   if ((fp = fsh_open(s, "wt")) == NULL) {
  725.     log_it(1, "\n ■ %s %s!", strings[0], s);
  726.     do_exit(EXIT_FAILURE);
  727.   } else {
  728.     if (BBSNODE == 2)
  729.       sprintf(s2, "From: n%hd@filenet.ml.org\n", net_sysnum);
  730.     else
  731.       sprintf(s2, "From: %s@%s\n", POPNAME, DOMAIN);
  732.     fprintf(fp, s2);
  733.     sprintf(s2, "To: %s\n", dest);
  734.     fprintf(fp, s2);
  735.     sprintf(s2, "Message-ID: <%s-%s@%s>\n", pktname, POPNAME, DOMAIN);
  736.     fprintf(fp, s2);
  737.     sprintf(s2, "Subject: @%s NET PACKET : %s.UUE\n\n", net_name, pktname);
  738.     fprintf(fp, s2);
  739.   }
  740.   if (fp != NULL)
  741.     fclose(fp);
  742.   sprintf(s1, "%s\\UU.EXE -encode %s %s", maindir, fn, s);
  743.   ok = do_spawn(s1);
  744.   if (MOREINFO)
  745.     output("\n");
  746.   return ok;
  747. }
  748.  
  749. int update_contact(unsigned short sy, unsigned long sb, unsigned long rb)
  750. {
  751.   char s[101];
  752.   int i, i1, f;
  753.   long cur_time, l;
  754.  
  755.   sprintf(s, "%sCONTACT.NET", net_data);
  756.   f = sh_open1(s, O_RDONLY | O_BINARY);
  757.   if (f >= 0) {
  758.     l = filelength(f);
  759.     netcfg.num_ncn = (int) (l / sizeof(net_contact_rec));
  760.     if ((netcfg.ncn = (net_contact_rec *) farmalloc((netcfg.num_ncn + 2) *
  761.                                         sizeof(net_contact_rec))) == NULL) {
  762.       f = sh_close(f);
  763.       return 1;
  764.     }
  765.     sh_lseek(f, 0L, SEEK_SET);
  766.     sh_read(f, (void *) netcfg.ncn, netcfg.num_ncn * sizeof(net_contact_rec));
  767.     f = sh_close(f);
  768.   }
  769.   for (i = 0; i < netcfg.num_ncn; i++)
  770.     if (netcfg.ncn[i].systemnumber == sy)
  771.       i1 = i;
  772.   time(&cur_time);
  773.   netcfg.ncn[i1].lasttry = cur_time;
  774.   ++netcfg.ncn[i1].numcontacts;
  775.   if (!netcfg.ncn[i1].firstcontact)
  776.     netcfg.ncn[i1].firstcontact = cur_time;
  777.   netcfg.ncn[i1].lastcontact = cur_time;
  778.   netcfg.ncn[i1].lastcontactsent = cur_time;
  779.   netcfg.ncn[i1].bytes_sent += sb;
  780.   if (rb)
  781.     netcfg.ncn[i1].bytes_received += rb;
  782.   netcfg.ncn[i1].bytes_waiting = 0L;
  783.   sprintf(s, "%sS%hd.net", net_data, sy);
  784.   if (!exist(s))
  785.     sprintf(s, "%sZ%hd.net", net_data, sy);
  786.   f = sh_open1(s, O_RDONLY | O_BINARY);
  787.   if (f > 0) {
  788.     netcfg.ncn[i1].bytes_waiting += filelength(f);
  789.     close(f);
  790.   }
  791.   sprintf(s, "%sCONTACT.NET", net_data);
  792.   f = sh_open1(s, O_RDWR | O_CREAT | O_BINARY);
  793.   if (f > 0) {
  794.     sh_lseek(f, 0L, SEEK_SET);
  795.     sh_write(f, (void *) netcfg.ncn, netcfg.num_ncn * sizeof(net_contact_rec));
  796.     f = sh_close(f);
  797.   }
  798.   if (netcfg.ncn)
  799.     farfree(netcfg.ncn);
  800.   return 0;
  801. }
  802.  
  803.  
  804. int update_contacts(unsigned short sy, unsigned long sb, unsigned long rb)
  805. {
  806.   int i, i1, rc;
  807.   char fn[101];
  808.   net_address_rec curaddr;
  809.   FILE *fc;
  810.  
  811.   rc = 0;
  812.   sprintf(fn, "%sCONTACT.PPP", net_data);
  813.   if ((fc = fsh_open(fn, "rb+")) == NULL) {
  814.     log_it(1, "\n ■ %s %s!\n", strings[1], fn);
  815.     rc = 1;
  816.     return (rc);
  817.   }
  818.   for (i = 0; i < num_addr; i++) {
  819.     fseek(fc, (long) i * sizeof(net_address_rec), SEEK_SET);
  820.     fread(&curaddr, sizeof(net_address_rec), 1, fc);
  821.     if (curaddr.sysnum != 0) {
  822.       if (curaddr.sysnum == sy)
  823.         i1 = update_contact(curaddr.sysnum, sb, rb);
  824.       else
  825.         i1 = update_contact(curaddr.sysnum, 0, 0);
  826.       if (i1)
  827.         rc = 1;
  828.     }
  829.   }
  830.   if (fc != NULL)
  831.     fclose(fc);
  832.   return (rc);
  833. }
  834.  
  835. int uu_packets(void)
  836. {
  837.   char pktname[21], s[121], s1[121], s2[121], s3[121], temp[121];
  838.   int found, sz, f, f1, i;
  839.   unsigned long sbytes, basename;
  840.   net_address_rec curaddr;
  841.   FILE *fc;
  842.  
  843.   num_sys_list = 0;
  844.   found = 0;
  845.   basename = time(NULL);
  846.   sprintf(s, "%sBBSDATA.NET", net_data);
  847.   f = sh_open1(s, O_RDONLY | O_BINARY);
  848.   if (f > 0) {
  849.     num_sys_list = (int) (filelength(f) / sizeof(net_system_list_rec));
  850.     if ((netsys = (net_system_list_rec *) farmalloc((num_sys_list + 2) *
  851.                                     sizeof(net_system_list_rec))) == NULL) {
  852.       log_it(1, "\n ■ Unable to allocate %d bytes of memory for BBSDATA.NET",
  853.              (int) (num_sys_list + 2) * sizeof(net_system_list_rec));
  854.       sh_close(f);
  855.       return 0;
  856.     }
  857.     sh_lseek(f, 0L, SEEK_SET);
  858.     sh_read(f, (void *) netsys, num_sys_list * sizeof(net_system_list_rec));
  859.     sh_close(f);
  860.   } else
  861.     return 0;
  862.  
  863.   sprintf(s1, "%sCONTACT.PPP", net_data);
  864.   if ((fc = fsh_open(s1, "rb+")) == NULL) {
  865.     log_it(1, "\n ■ %s %s!\n", strings[1], s1);
  866.     farfree(netsys);
  867.     return 0;
  868.   }
  869.   for (i = 0; i < num_addr; i++) {
  870.     fseek(fc, (long) i * sizeof(net_address_rec), SEEK_SET);
  871.     fread(&curaddr, sizeof(net_address_rec), 1, fc);
  872.     if ((valid_system(curaddr.sysnum)) && (curaddr.sysnum != 32767)) {
  873.       sprintf(s1, "%sS%hd.NET", net_data, curaddr.sysnum);
  874.       if (exist(s1)) {
  875.         sz = 0;
  876.         found = 1;
  877.       } else {
  878.         sprintf(s1, "%sZ%hd.NET", net_data, curaddr.sysnum);
  879.         if (exist(s1)) {
  880.           sz = 1;
  881.           found = 1;
  882.         } else
  883.           continue;
  884.       }
  885.       do {
  886.         sprintf(pktname, "%8.8lx", ++basename);
  887.         sprintf(temp, "%sMQUEUE\\%s.UUE", net_data, pktname);
  888.       } while (exist(temp));
  889.       backline();
  890.       output("\r ■ Encoding %s%hd.NET as %s.UUE -",
  891.              sz ? "Z" : "S", curaddr.sysnum, strupr(pktname));
  892.       if ((uu(s1, pktname, curaddr.address)) != EXIT_SUCCESS) {
  893.         output("\n ■ %s %sMQUEUE\\%s.UUE", strings[0], net_data, pktname);
  894.         if (fc != NULL)
  895.           fclose(fc);
  896.         farfree(netsys);
  897.         do_exit(EXIT_FAILURE);
  898.       } else {
  899.         if (KEEPSENT) {
  900.           sprintf(s3, "%sMQUEUE\\%s.UUE", net_data, pktname);
  901.           sprintf(s2, "%sSENT\\%s.SNT", net_data, pktname);
  902.           f1 = sh_open1(s3, O_RDONLY | O_BINARY);
  903.           if (f1 > 0) {
  904.             sbytes = filelength(f1);
  905.             f = sh_close(f1);
  906.           }
  907.           if (!copyfile(s3, s2))
  908.             output("\n ■ %s %s%hd.NET", strings[0], sz ? "Z" : "S", curaddr.sysnum);
  909.         }
  910.         unlink(s1);
  911.         update_contact(curaddr.sysnum, sbytes, 0);
  912.       }
  913.     }
  914.   }
  915.   if (fc != NULL)
  916.     fclose(fc);
  917.   farfree(netsys);
  918.   return found;
  919. }
  920.  
  921. int parse_net_ini(void)
  922. {
  923.   char cursect, fn[MAXPATH], line[121], vanline[121], *ss;
  924.   FILE *fp;
  925.  
  926.   sprintf(fn, "%s\\NET.INI", maindir);
  927.   if ((fp = fsh_open(fn, "rt")) == NULL) {
  928.     output("\n ! Unable to read %s.", fn);
  929.     return (0);
  930.   }
  931.   while (fgets(line, 80, fp)) {
  932.     strcpy(vanline, line);
  933.     stripspace(line);
  934.     ss = NULL;
  935.     ss = strtok(line, "=");
  936.     if (ss)
  937.       ss = strtok(NULL, "\n");
  938.     if ((line[0] == ';') || (line[0] == '\n') || (strlen(line) == 0))
  939.       continue;
  940.     if ((strnicmp(line, "[FILENET]", 9) == 0) ||
  941.         (strnicmp(line, "[PPPNET]", 8) == 0)) {
  942.       cursect = INI_NETWORK;
  943.       ini_section |= INI_NETWORK;
  944.       continue;
  945.     }
  946.     if (strnicmp(line, "[GENERAL]", 9) == 0) {
  947.       cursect = INI_GENERAL;
  948.       ini_section |= INI_GENERAL;
  949.       continue;
  950.     }
  951.     if (strnicmp(line, "[NEWS]", 6) == 0) {
  952.       cursect = INI_NEWS;
  953.       ini_section |= INI_NEWS;
  954.       continue;
  955.     }
  956.     if (cursect & INI_NETWORK) {
  957.       if (strnicmp(line, "TIMEHOST", 8) == 0) {
  958.         if (ss)
  959.           strcpy(TIMEHOST, ss);
  960.         continue;
  961.       }
  962.       if (strnicmp(line, "SMTPHOST", 8) == 0) {
  963.         if (ss)
  964.           strcpy(SMTPHOST, ss);
  965.         continue;
  966.       }
  967.       if (strnicmp(line, "POPHOST", 7) == 0) {
  968.         if (ss)
  969.           strcpy(POPHOST, ss);
  970.         continue;
  971.       }
  972.       if (strnicmp(line, "POPNAME", 7) == 0) {
  973.         if (ss)
  974.           strcpy(POPNAME, ss);
  975.         continue;
  976.       }
  977.       if (strnicmp(line, "POPPASS", 7) == 0) {
  978.         if (ss)
  979.           strcpy(POPPASS, ss);
  980.         continue;
  981.       }
  982.     }
  983.     if (strnicmp(line, "NODEPASS", 7) == 0) {
  984.       if (ss) {
  985.         strcpy(NODEPASS, ss);
  986.         BBSNODE = 2;
  987.       }
  988.       continue;
  989.     }
  990.     if (cursect & INI_GENERAL) {
  991.       if (strnicmp(line, "TIMEOUT", 7) == 0) {
  992.         if (atoi(ss))
  993.           TIMEOUT = atoi(ss);
  994.         continue;
  995.       }
  996.       if (strnicmp(line, "SOCK_DELAY", 10) == 0) {
  997.         if (atoi(ss))
  998.           SOCK_DELAY = atoi(ss);
  999.         continue;
  1000.       }
  1001.       if ((strnicmp(line, "INACTIVE", 8) == 0) || (strnicmp(line, "SOCK_INACTIVE", 13) == 0)) {
  1002.         if (atoi(ss))
  1003.           INACTIVE = atoi(ss);
  1004.         continue;
  1005.       }
  1006.       if (strnicmp(line, "KEEPSENT", 8) == 0) {
  1007.         if (atoi(ss))
  1008.           KEEPSENT = atoi(ss);
  1009.         continue;
  1010.       }
  1011.       if (strnicmp(line, "PURGE", 5) == 0) {
  1012.         if (toupper(ss[0]) == 'N')
  1013.           PURGE = 0;
  1014.         continue;
  1015.       }
  1016.       if (strnicmp(line, "ALLMAIL", 7) == 0) {
  1017.         if (toupper(ss[0]) == 'N')
  1018.           ALLMAIL = 0;
  1019.         continue;
  1020.       }
  1021.       if (strnicmp(line, "ONECALL", 7) == 0) {
  1022.         if (toupper(ss[0]) == 'Y')
  1023.           ONECALL = 1;
  1024.         continue;
  1025.       }
  1026.       if (strnicmp(line, "MOREINFO", 8) == 0) {
  1027.         if (toupper(ss[0]) == 'Y')
  1028.           MOREINFO = 1;
  1029.         continue;
  1030.       }
  1031.       if (strnicmp(line, "CLEANUP", 7) == 0) {
  1032.         if (toupper(ss[0]) == 'Y')
  1033.           CLEANUP = 1;
  1034.         continue;
  1035.       }
  1036.       if (strnicmp(line, "IPADDR", 6) == 0) {
  1037.         if ((ss) && (ss[0] != '0'))
  1038.           strcpy(IPADDR, ss);
  1039.         continue;
  1040.       }
  1041.       if (strnicmp(line, "NETMASK", 7) == 0) {
  1042.         if (ss)
  1043.           strcpy(NETMASK, ss);
  1044.         continue;
  1045.       }
  1046.       if (strnicmp(line, "DNS", 3) == 0) {
  1047.         if (ss)
  1048.           strcpy(DNS, ss);
  1049.         continue;
  1050.       }
  1051.       if (strnicmp(line, "SDNS", 3) == 0) {
  1052.         if (ss)
  1053.           strcpy(SDNS, ss);
  1054.         continue;
  1055.       }
  1056.       if (strnicmp(line, "DOMAIN", 6) == 0) {
  1057.         if (ss)
  1058.           strcpy(DOMAIN, ss);
  1059.         continue;
  1060.       }
  1061.       if (strnicmp(line, "FWDNAME", 7) == 0) {
  1062.         if (ss)
  1063.           strcpy(FWDNAME, ss);
  1064.         continue;
  1065.       }
  1066.       if (strnicmp(line, "FWDDOM", 6) == 0) {
  1067.         if (ss)
  1068.           strcpy(FWDDOM, ss);
  1069.         continue;
  1070.       }
  1071.       if (strnicmp(line, "GATEWAY", 7) == 0) {
  1072.         if ((ss) && (ss[0] != '0'))
  1073.           strcpy(GATEWAY, ss);
  1074.         continue;
  1075.       }
  1076.     }
  1077.     if (cursect & INI_NEWS) {
  1078.       if (strnicmp(line, "NEWSHOST", 8) == 0) {
  1079.         if (ss)
  1080.           strcpy(NEWSHOST, ss);
  1081.         continue;
  1082.       }
  1083.     }
  1084.   }
  1085.   if (!FWDNAME[0])
  1086.     strcpy(FWDNAME, POPNAME);
  1087.   if (!FWDDOM[0])
  1088.     strcpy(FWDDOM, DOMAIN);
  1089.   if (fp != NULL)
  1090.     fclose(fp);
  1091.   return (1);
  1092. }
  1093.  
  1094. int read_networks(void)
  1095. {
  1096.   int f, max;
  1097.   char s[121];
  1098.  
  1099.   sprintf(s, "%sNETWORKS.DAT", syscfg.datadir);
  1100.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1101.   max = 0;
  1102.   if (f > 0)
  1103.     max = (int) (filelength(f) / sizeof(net_networks_rec));
  1104.   sh_close(f);
  1105.   return max;
  1106. }
  1107.  
  1108. void do_it(char *cl)
  1109. {
  1110.   int i, i1, l;
  1111.   char *s, *ss[30];
  1112.  
  1113.   s = _fstrdup(cl);
  1114.   ss[0] = s;
  1115.   i = 1;
  1116.   l = strlen(s);
  1117.   for (i1 = 1; i1 < l; i1++)
  1118.     if (s[i1] == 32) {
  1119.       s[i1] = 0;
  1120.       ss[i++] = &(s[i1 + 1]);
  1121.     }
  1122.   ss[i] = NULL;
  1123.   execvpe(ss[0], ss, xenviron);
  1124.   if (s != NULL)
  1125.     farfree(s);
  1126. }
  1127.  
  1128. int create_wattcp_cfg(void)
  1129. {
  1130.   char s1[101], fn[MAXPATH];
  1131.   FILE *fp;
  1132.  
  1133.   if (IPADDR[0] == 0)
  1134.     strcpy(IPADDR, "0.0.0.0");
  1135.  
  1136.   sprintf(fn, "%s\\WATTCP.CFG", maindir);
  1137.   if ((fp = fsh_open(fn, "wt+")) == NULL) {
  1138.     log_it(1, "\n ■ %s %s\\WATTCP.CFG... aborting!", strings[1], maindir);
  1139.     return 1;
  1140.   }
  1141.   fprintf(fp, "my_ip=%s\n", IPADDR);
  1142.   fprintf(fp, "netmask=%s\n", NETMASK);
  1143.   fprintf(fp, "nameserver=%s\n", DNS);
  1144.   if (SDNS[0] != 0)
  1145.     fprintf(fp, "nameserver=%s\n", SDNS);
  1146.   if (GATEWAY[0] == 0) {
  1147.     strcpy(s1, IPADDR);
  1148.     while (LAST(s1) != '.')
  1149.       LAST(s1) = 0;
  1150.     strcat(s1, "1");
  1151.     strcpy(GATEWAY, s1);
  1152.   }
  1153.   fprintf(fp, "gateway=%s\n", GATEWAY);
  1154.   fprintf(fp, "domains=\"%s\"\n", DOMAIN);
  1155.   fprintf(fp, "hostname=\"%s\"\n", POPNAME);
  1156.   fprintf(fp, "sockdelay=%d\n", SOCK_DELAY);
  1157.   fprintf(fp, "inactive=%d\n", INACTIVE);
  1158.   fclose(fp);
  1159.   return 0;
  1160. }
  1161.  
  1162. int check_encode(char *fn)
  1163. {
  1164.   char s[121];
  1165.   int lines, encoded;
  1166.   FILE *fp;
  1167.  
  1168.   if ((fp = fsh_open(fn, "r")) == NULL)
  1169.     return 0;
  1170.  
  1171.   lines = encoded = 0;
  1172.  
  1173.   while ((fgets(s, 120, fp)) && (++lines < 1000) && (!encoded)) {
  1174.     if (strnicmp(s, "begin 6", 7) == 0) {
  1175.       encoded = 1;
  1176.       break;
  1177.     }
  1178.     if (strnicmp(s, "Content-Type:", 13) == 0) {
  1179.       encoded = 1;
  1180.       break;
  1181.     }
  1182.   }
  1183.  
  1184.   if (fp != NULL)
  1185.     fclose(fp);
  1186.  
  1187.   if (encoded) {
  1188.     log_it(MOREINFO, "\n ■ %s appears to contain encoded data.", fn);
  1189.     return 1;
  1190.   }
  1191.   return 0;
  1192. }
  1193.  
  1194. void check_unk(void)
  1195. {
  1196.   char s[101], fn[MAXPATH];
  1197.   int f, f1;
  1198.   long l;
  1199.   struct ffblk ff;
  1200.  
  1201.   sprintf(fn, "%sSPOOL\\UNK-0*.*", net_data);
  1202.   f1 = findfirst(fn, &ff, 0);
  1203.   while (f1 == 0) {
  1204.     sprintf(fn, "%sINBOUND\\%s", net_data, ff.ff_name);
  1205.     f = sh_open1(fn, O_RDONLY | O_BINARY);
  1206.     l = filelength(f);
  1207.     f = sh_close(f);
  1208.     if (l > 32767L) {
  1209.       if (!check_encode(fn)) {
  1210.         sprintf(s, "%s\\PPPUTIL.EXE CHUNK %s %s", maindir, net_data, ff.ff_name);
  1211.         do_spawn(s);
  1212.       }
  1213.     }
  1214.     f1 = findnext(&ff);
  1215.   }
  1216. }
  1217.  
  1218. void check_exp(void)
  1219. {
  1220.   char s[MAXPATH], cmd[201];
  1221.   int ok;
  1222.  
  1223.   cd_to(maindir);
  1224.   sprintf(s, "%s\\EXP.EXE", maindir);
  1225.   if (!exist(s)) {
  1226.     log_it(1, "\n ! EXPort/Import module not found!");
  1227.     return;
  1228.   }
  1229.   ok = 0;
  1230.   sprintf(s, "%sS32767.NET", net_data);
  1231.   if (exist(s))
  1232.     ok = 1;
  1233.   sprintf(s, "%sINBOUND\\SUB*.*", net_data);
  1234.   if (exist(s))
  1235.     ok = 1;
  1236.   sprintf(s, "%sSPOOL\\UNK*.*", net_data);
  1237.   if (exist(s)) {
  1238.     ok = 1;
  1239.     sprintf(s, "%sSPOOL\\UNK-0*.*", net_data);
  1240.     if (exist(s))
  1241.       check_unk();
  1242.   }
  1243.   if (ok) {
  1244.     sprintf(cmd, "%s\\EXP.EXE S32767.NET %s %hu %s %s %s", maindir, net_data,
  1245.             net_sysnum, FWDNAME, FWDDOM, net_name);
  1246.     if (do_spawn(cmd)) {
  1247.       log_it(1, "\n ! %s during export!", strings[2]);
  1248.       return;
  1249.     }
  1250.   } else
  1251.     log_it(MOREINFO, "\n ■ No Internet mail or newsgroup posts to process.");
  1252. }
  1253.  
  1254. int check_for_lsl(void)
  1255. {
  1256.   union REGS r;
  1257.   struct SREGS s;
  1258.   char *intstr;
  1259.  
  1260.   for (r.h.ah = 0xc0; r.h.ah != 0; r.h.ah++) {
  1261.     r.h.al = 0;
  1262.     int86x(0x2f, &r, &r, &s);
  1263.     if (r.h.al == 0xff) {
  1264.       intstr = (char *) MK_FP(s.es, r.x.si);
  1265.       if (strncmp(intstr, "LINKSUP$", 8) == 0)
  1266.         return 1;
  1267.     }
  1268.   }
  1269.   return 0;
  1270. }
  1271.  
  1272. #ifndef PACKET
  1273. void klos_ppp(void)
  1274. {
  1275.   char s[160];
  1276.   int i;
  1277.  
  1278.   lsl_loaded = check_for_lsl();
  1279.   if (!lsl_loaded) {
  1280.     sprintf(s, "%s\\LSL.COM", maindir);
  1281.     if (exist(s)) {
  1282.       if (MOREINFO)
  1283.         output("\n ■ Loading Link Support Layer and ");
  1284.       do_spawn(s);
  1285.     } else {
  1286.       log_it(1, "\n ■ LSL.COM not in BBS main directory!");
  1287.       do_exit(EXIT_FAILURE);
  1288.     }
  1289.   } else {
  1290.     if (MOREINFO)
  1291.       output("\n ■ LSL already in memory - loading ");
  1292.   }
  1293.   sprintf(s, "%s\\PPP.EXE", maindir);
  1294.   if (exist(s)) {
  1295.     if (MOREINFO)
  1296.       output("PPP packet driver.");
  1297.     do_spawn(s);
  1298.   } else {
  1299.     log_it(1, "\n ■ %s\\PPP.EXE not found!", maindir);
  1300.     do_exit(EXIT_FAILURE);
  1301.   }
  1302.   while (kbhit())
  1303.     getch();
  1304.   sprintf(s, "%s\\PPPSTATE.EXE", maindir);
  1305.   if (exist(s)) {
  1306.     output("\n ■ Dialing %s... timeout %d seconds... ", DOMAIN, TIMEOUT);
  1307.     delay(2000);
  1308.     sprintf(s, "%s\\PPPSTATE.EXE WAIT=%d IP", maindir, TIMEOUT);
  1309.     i = do_spawn(s);
  1310.     switch (i) {
  1311.       case 0:
  1312.         output(" connected!");
  1313.         break;
  1314.       case 1:
  1315.         log_it(1, "\n ■ Unable to establish connection... aborting");
  1316.         sprintf(s, "%s\\PPP U", maindir);
  1317.         do_spawn(s);
  1318.         if (!lsl_loaded) {
  1319.           sprintf(s, "%s\\LSL U", maindir);
  1320.           do_spawn(s);
  1321.         }
  1322.         do_exit(EXIT_FAILURE);
  1323.       case 2:
  1324.         output("\n ■ Unable to find PPP layer... aborting");
  1325.         if (!lsl_loaded) {
  1326.           sprintf(s, "%s\\LSL U", maindir);
  1327.           do_spawn(s);
  1328.         }
  1329.         do_exit(EXIT_FAILURE);
  1330.       default:
  1331.         log_it(1, "\n ■ %s PPPState #%d... aborting", strings[2], i);
  1332.         sprintf(s, "%s\\PPP U", maindir);
  1333.         do_spawn(s);
  1334.         if (!lsl_loaded) {
  1335.           sprintf(s, "%s\\LSL U", maindir);
  1336.           do_spawn(s);
  1337.         }
  1338.         do_exit(EXIT_FAILURE);
  1339.     }
  1340.   } else {
  1341.     log_it(1, "\n ■ %s\\PPPSTATE.EXE not found!", maindir);
  1342.     do_exit(EXIT_FAILURE);
  1343.   }
  1344.   delay(2000);
  1345.   sprintf(s, "%s\\PPPWAT.EXE", maindir);
  1346.   if (exist(s)) {
  1347.     do_spawn(s);
  1348.   } else {
  1349.     log_it(1, "\n ! %s\\PPPWAT.EXE not found!", maindir);
  1350.     do_exit(EXIT_FAILURE);
  1351.   }
  1352.   sprintf(s, "%s\\IPSTUB.EXE", maindir);
  1353.   if (exist(s)) {
  1354.     do_spawn(s);
  1355.   } else {
  1356.     log_it(1, "\n ■ %s\\IPSTUB.EXE not found!", maindir);
  1357.     do_exit(EXIT_FAILURE);
  1358.   }
  1359. }
  1360.  
  1361. #endif
  1362.  
  1363. int main(int argc, char *argv[])
  1364. {
  1365.   char *ss, s[160], s1[MAXPATH], destaddr[101], temp[160], ttotal[21];
  1366.   int i, net_num_max, i1, dv, f, ok, news, totf, send, f1, result;
  1367.   unsigned long sentbytes, recdbytes, rnewsbytes, snewsbytes;
  1368.   unsigned short sy;
  1369.   float dspace;
  1370.   FILE *fp;
  1371.   clock_t starttime, mailtime, newstime;
  1372.   struct tm *time_now;
  1373.   struct ffblk ff;
  1374.   struct diskfree_t df;
  1375.   time_t some;
  1376.  
  1377.   get_dir(maindir, 0);
  1378.  
  1379.   i = i1 = 0;
  1380.   while ((environ[i] != NULL) && (i < 20)) {
  1381.     xenviron[i1++] = environ[i];
  1382.     ++i;
  1383.   }
  1384.   xenviron[i1] = NULL;
  1385.  
  1386.   ss = getenv("WWIV_INSTANCE");
  1387.   if (ss) {
  1388.     instance = atoi(ss);
  1389.     if ((instance <= 0) || (instance > 999)) {
  1390.       output("\n ! WWIV_INSTANCE can only be 1..999!");
  1391.       instance = 1;
  1392.     }
  1393.   } else {
  1394.     instance = 1;
  1395.   }
  1396.   ss = NULL;
  1397.  
  1398.   strcpy(s, "CONFIG.DAT");
  1399.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1400.   if (f < 0) {
  1401.     output("\n ! %s NOT FOUND.", s);
  1402.     return 1;
  1403.   }
  1404.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  1405.   sh_close(f);
  1406.  
  1407.   detect_multitask();
  1408.  
  1409.   switch (multitasker) {
  1410.     case 1:
  1411.       dv = get_dv_version();
  1412.       sprintf(tasker, "DesqView %d.%02d", dv / 256, dv % 256);
  1413.       break;
  1414.     case 2:
  1415.       dv = get_win_version();
  1416.       if (dv >= 4)
  1417.         strcpy(tasker, "Windows 95");
  1418.       else
  1419.         sprintf(tasker, "Windows %d.%02d", dv % 256, dv / 256);
  1420.       break;
  1421.     case 3:
  1422.       dv = get_dv_version();
  1423.       sprintf(tasker, "Windows and DesqView %d.%02d", dv / 256, dv % 256);
  1424.       break;
  1425.     case 4:
  1426.     case 5:
  1427.     case 6:
  1428.     case 7:
  1429.       if ((_osmajor / 10 == 2) && (_osminor >= 30))
  1430.         strcpy(tasker, "OS/2 Warp");
  1431.       else
  1432.         sprintf(tasker, "OS/2 %d.%2.2d", _osmajor / 10, _osminor);
  1433.       break;
  1434.     case 8:
  1435.       sprintf(tasker, "NETBIOS network");
  1436.       multitasker = 1;
  1437.       break;
  1438.     default:
  1439.       strcpy(tasker, "DOS");
  1440.       break;
  1441.   }
  1442.  
  1443.   strcpy(s, "CONFIG.OVR");
  1444.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1445.   if (f < 0) {
  1446.     output("\n ! %s NOT FOUND.", s);
  1447.     return 1;
  1448.   }
  1449.   sh_lseek(f, (instance - 1) * sizeof(configoverrec), SEEK_SET);
  1450.   sh_read(f, &syscfgovr, sizeof(configoverrec));
  1451.   sh_close(f);
  1452.  
  1453.   sprintf(s, "%sINSTANCE.DAT", syscfg.datadir);
  1454.   if (!exist(s)) {
  1455.     output("\n ! %s NOT FOUND.", s);
  1456.     return 1;
  1457.   }
  1458.  
  1459.   net_num_max = read_networks();
  1460.  
  1461.   news = 0;
  1462.   num_addr = 0;
  1463.   ss = argv[argc - 1];
  1464.   ok = 0;
  1465.   if (_fstrstr((char *) ss, ".") != NULL) {
  1466.     if (atoi(&ss[1]) < net_num_max) {
  1467.       netnum = atoi(&ss[1]);
  1468.       ok = 1;
  1469.     }
  1470.   } else {
  1471.     ss = (getenv("WWIV_NET"));
  1472.     netnum = atoi(ss);
  1473.     if (netnum < net_num_max)
  1474.       ok = 1;
  1475.   }
  1476.   if (!ok) {
  1477.     strcpy(s, "WWIV_NET.DAT");
  1478.     if ((fp = fsh_open(s, "rb")) != NULL) {
  1479.       fgets(s, 3, fp);
  1480.       netnum = atoi(s);
  1481.       if (netnum < net_num_max)
  1482.         ok = 1;
  1483.     }
  1484.     if (fp != NULL)
  1485.       fclose(fp);
  1486.   }
  1487.  
  1488.   ss = argv[1];
  1489.   if (strncmpi(ss, "/N", 2))
  1490.     ok = 0;
  1491.   set_net_num(netnum);
  1492.   sprintf(s, "%sADDRESS.1", net_data);
  1493.   if (!exist(s))
  1494.     ok = 0;
  1495.   if (ok) {
  1496.     *SMTPHOST = *POPHOST = *NEWSHOST = *TIMEHOST = *POPNAME = *POPPASS = 0;
  1497.     *IPADDR = *NETMASK = *DNS = *SDNS = *DOMAIN = *GATEWAY = *FWDNAME = *FWDDOM = 0;
  1498.     if (parse_net_ini()) {
  1499.       set_net_num(netnum);
  1500.       if (ini_section & INI_NETWORK) {
  1501.         if (!(ini_section & INI_GENERAL)) {
  1502.           output("\n ! [GENERAL] tag missing from NET.INI!");
  1503.           ok = 0;
  1504.         }
  1505.       } else {
  1506.         ok = 0;
  1507.         output("\n ! [FILENET] tag missing from NET.INI!");
  1508.       }
  1509.     } else {
  1510.       output("\n ! %s NET.INI!", strings[1]);
  1511.       ok = 0;
  1512.     }
  1513.   }
  1514.  
  1515.   if (ok) {
  1516.     if (create_contact_ppp())
  1517.       ok = 0;
  1518.   }
  1519.  
  1520.   if (ok) {
  1521.     log_it(1, "\n\n%s", version);
  1522.     output("\n%s\n", author);
  1523.     time(&some);
  1524.     time_now = localtime(&some);
  1525.     sprintf(s1, "\n ■ Running under %s on instance %d ", tasker, instance);
  1526.     strftime(s, 80, "at %I:%M%p, %d %b %y", time_now);
  1527.     strcat(s1, s);
  1528.     log_it(1, s1);
  1529.     log_it(1, "\n ■ Memory available : %ld bytes -", coreleft());
  1530.     if (net_data[1] == ':')
  1531.       i = net_data[0];
  1532.     else
  1533.       i = maindir[0];
  1534.     i = toupper(i) - 'A' + 1;
  1535.     _dos_getdiskfree(i, &df);
  1536.     dspace = ((float)df.avail_clusters * (float)df.bytes_per_sector *
  1537.              (float)df.sectors_per_cluster);
  1538.     if (dspace < 1024000.00) {
  1539.       log_it(1, "\n ■ Only %.1fK available on drive %c:... aborting!",
  1540.              dspace, i + '@');
  1541.       do_exit(EXIT_FAILURE);
  1542.     } else
  1543.       log_it(1, " Disk space : %c:%.1fM", i + '@', (dspace / 1024000L));
  1544.  
  1545.     set_net_num(netnum);
  1546.     ss = argv[1];
  1547.     sy = atoi(&ss[2]);
  1548.     destaddr[0] = 0;
  1549.     if (sy != 32767)
  1550.       good_addr(destaddr, sy);
  1551.  
  1552.     if ((destaddr[0] == 0) && (sy != 32767)) {
  1553.       output("\n ■ Using direct dial for @%hd.%s\n\n", sy, net_name);
  1554.       ok = 0;
  1555.     } else {
  1556.       ok = 1;
  1557.       if (sy == 32767) {
  1558.         if (ini_section & INI_NEWS)
  1559.           output("\n ■ Initiating newsgroup session...");
  1560.         else {
  1561.           output("\n ■ [NEWS] missing from NET.INI!");
  1562.           do_exit(EXIT_FAILURE);
  1563.         }
  1564.       }
  1565.     }
  1566.   }
  1567.   if (ok) {
  1568.     if (!MOREINFO)
  1569.       freopen(NULL, "w", stdout);
  1570.     if (CLEANUP)
  1571.       process_mail();
  1572.     set_net_num(netnum);
  1573.     sprintf(s, "%sSPOOL", net_data);
  1574.     if ((make_path(s)) < 0) {
  1575.       log_it(1, "\n ■ %s \"%s\"", strings[0], s);
  1576.       do_exit(EXIT_FAILURE);
  1577.     }
  1578.     sprintf(s, "%sMQUEUE", net_data);
  1579.     if ((make_path(s)) < 0) {
  1580.       log_it(1, "\n ■ %s \"%s\"", strings[0], s);
  1581.       do_exit(EXIT_FAILURE);
  1582.     }
  1583.     sprintf(s, "%sOUTBOUND", net_data);
  1584.     if ((make_path(s)) < 0) {
  1585.       log_it(1, "\n ■ %s \"%s\"", strings[0], s);
  1586.       do_exit(EXIT_FAILURE);
  1587.     }
  1588.     sprintf(s, "%sINBOUND", net_data);
  1589.     if ((make_path(s)) < 0) {
  1590.       log_it(1, "\n ■ %s \"%s\"", strings[0], s);
  1591.       do_exit(EXIT_FAILURE);
  1592.     }
  1593.     sprintf(s, "%sSENT", net_data);
  1594.     if ((make_path(s)) < 0) {
  1595.       log_it(1, "\n ■ %s \"%s\"", strings[0], s);
  1596.       do_exit(EXIT_FAILURE);
  1597.     }
  1598.     if (PURGE) {
  1599.       if ((KEEPSENT) && (sy != 32767)) {
  1600.         log_it(1, "\n ■ Purging sent packets older than %d day%s...", KEEPSENT,
  1601.                KEEPSENT == 1 ? "" : "s");
  1602.         sprintf(s, "%s\\PPPUTIL PURGE %s\\SENT\\ %d", maindir, net_data, KEEPSENT);
  1603.         do_spawn(s);
  1604.       }
  1605.     }
  1606.     sprintf(s, "%sCHECKNET", net_data);
  1607.     if ((make_path(s)) < 0) {
  1608.       log_it(1, "\n ■ %s \"%s\"", strings[0], s);
  1609.       do_exit(EXIT_FAILURE);
  1610.     }
  1611.     send = 1;
  1612.  
  1613.     check_exp();
  1614.  
  1615.     cd_to(maindir);
  1616.     if ((sy != 32767) || (ONECALL)) {
  1617.       output("\n ■ Preparing outbound packets...");
  1618.       if (!uu_packets()) {
  1619.         sprintf(s, "%sMQUEUE\\*.*", net_data);
  1620.         if (findfirst(s, &ff, 0) == 0) {
  1621.           output(" already in MQUEUE to be sent.");
  1622.         } else {
  1623.           output(" no mail or packets to send.");
  1624.           send = 0;
  1625.         }
  1626.       } else {
  1627.         backline();
  1628.         output(" ■ All packets prepared for transfer.");
  1629.       }
  1630.     }
  1631.     starttime = clock();
  1632.     sentbytes = recdbytes = snewsbytes = rnewsbytes = 0L;
  1633.     cd_to(maindir);
  1634.  
  1635. if (create_wattcp_cfg())
  1636.     do_exit(EXIT_FAILURE);
  1637.  
  1638. #ifndef PACKET
  1639.     sprintf(s, "%sNET%d.CFG", net_data, instance);
  1640.     if (exist(s)) {
  1641.       sprintf(s1, "%s\\NET.CFG", maindir);
  1642.       unlink(s1);
  1643.       if (!copyfile(s, s1))
  1644.         log_it(1, "\n ■ %s %s from %s.", strings[0], s1, s);
  1645.     }
  1646.     klos_ppp();
  1647. #endif
  1648.  
  1649.     sprintf(temp_dir, "%sINBOUND\\", net_data);
  1650.  
  1651.     ok = 0;
  1652.     time(&some);
  1653.     time_now = localtime(&some);
  1654.     strftime(s, 80, "\n - SMTP/POP on %A, %B %d, %Y at %H:%M %p",
  1655.              time_now);
  1656.     log_it(0, s);
  1657.     sentbytes = recdbytes = 0L;
  1658.     if ((send) && ((sy != 32767) || (ONECALL))) {
  1659.       totf = 0;
  1660.       sprintf(temp, "%sMQUEUE\\*.*", net_data);
  1661.       f1 = findfirst(temp, &ff, FA_ARCH);
  1662.       while (f1 == 0) {
  1663.         ok = 1;
  1664.         ++totf;
  1665.         sentbytes += ff.ff_fsize;
  1666.         f1 = findnext(&ff);
  1667.       }
  1668.       sprintf(s1, "%sMQUEUE\\", net_data);
  1669.       sprintf(s, "%s\\POP -send %s %s %s %d", maindir, SMTPHOST, DOMAIN, s1, MOREINFO);
  1670.       do_spawn(s);
  1671.       f1 = findfirst(temp, &ff, FA_ARCH);
  1672.       while (f1 == 0) {
  1673.         --totf;
  1674.         sentbytes -= ff.ff_fsize;
  1675.         f1 = findnext(&ff);
  1676.       }
  1677.       cd_to(maindir);
  1678.       if (ok) {
  1679.         backline();
  1680.         log_it(0, "\n");
  1681.         log_it(1, " ■ Outbound mail transfer completed : %d message%s (%ldK).",
  1682.                totf, totf == 1 ? "" : "s", ((sentbytes + 1023) / 1024));
  1683.       }
  1684.     }
  1685.     if ((sy != 32767) || (ONECALL)) {
  1686.       do {
  1687.         if (BBSNODE == 2)
  1688.           sprintf(s, "%s\\POP.EXE -receive filenet.ml.org n%hd %s %s 1 filenet.ml.org %d",
  1689.                   maindir, net_sysnum, NODEPASS, temp_dir, MOREINFO);
  1690.         else
  1691.           sprintf(s, "%s\\POP.EXE -receive %s %s %s %s %d %s %d",
  1692.                   maindir, POPHOST, POPNAME, POPPASS,
  1693.                   temp_dir, ALLMAIL, DOMAIN, MOREINFO);
  1694.         result = do_spawn(s);
  1695.         sprintf(s1, "%s*.*", temp_dir);
  1696.         if ((result == EXIT_SUCCESS) || (exist(s1))) {
  1697.           sprintf(temp, "%s*.*", temp_dir);
  1698.           f1 = findfirst(temp, &ff, FA_ARCH);
  1699.           i = 0;
  1700.           while (f1 == 0) {
  1701.             if ((strncmp(ff.ff_name, "BAD", 3) != 0) && (strncmp(ff.ff_name, "SPM", 3) != 0) &&
  1702.                 (strncmp(ff.ff_name, "SUB", 3) != 0) && (strncmp(ff.ff_name, "DUP", 3) != 0) &&
  1703.                 (ff.ff_fsize)) {
  1704.               recdbytes += ff.ff_fsize;
  1705.               sprintf(s1, "%s%s", temp_dir, ff.ff_name);
  1706.               cd_to(maindir);
  1707.               sprintf(temp, "%s\\UU.EXE -decode %s %s %s", maindir, ff.ff_name,
  1708.                       temp_dir, net_data);
  1709.               if (!do_spawn(temp))
  1710.                 unlink(s1);
  1711.             }
  1712.             f1 = findnext(&ff);
  1713.           }
  1714.         } else
  1715.           output("\n ■ No network packets to process");
  1716.         --BBSNODE;
  1717.       } while (BBSNODE);
  1718.  
  1719.     }
  1720.  
  1721.     check_exp();
  1722.  
  1723.     mailtime = clock() - starttime;
  1724.     if ((sy == 32767) || (ONECALL)) {
  1725.       time(&some);
  1726.       time_now = localtime(&some);
  1727.       strftime(s, 80, "\n - NEWS session beginning on %A, %B %d, %Y at %H:%M %p",
  1728.                time_now);
  1729.       log_it(0, s);
  1730.       news = 1;
  1731.       starttime = clock();
  1732.       set_net_num(netnum);
  1733.       sprintf(temp, "%sOUTBOUND\\*.*", net_data);
  1734.       f1 = findfirst(temp, &ff, FA_ARCH);
  1735.       while (f1 == 0) {
  1736.         snewsbytes += ff.ff_fsize;
  1737.         f1 = findnext(&ff);
  1738.       }
  1739.       cd_to(maindir);
  1740.       strcpy(s1, net_data);
  1741.       if (LAST(s1) == '\\')
  1742.         LAST(s1) = 0;
  1743.       sprintf(s, "%s\\NEWS.EXE %s %s %hu", maindir, s1, NEWSHOST, net_sysnum);
  1744.       ok = 1;
  1745.       do_spawn(s);
  1746.       set_net_num(netnum);
  1747.       sprintf(temp, "%sP0*.*", net_data);
  1748.       f1 = findfirst(temp, &ff, 0);
  1749.       while (f1 == 0) {
  1750.         rnewsbytes += ff.ff_fsize;
  1751.         f1 = findnext(&ff);
  1752.       }
  1753.     }
  1754.   } else {
  1755.     output("\n");
  1756.     set_net_num(netnum);
  1757.     strcpy(s, "NETWORK0.EXE");
  1758.     for (i = 1; i < argc; i++) {
  1759.       strcat(s, " ");
  1760.       strcat(s, argv[i]);
  1761.     }
  1762.     s[strlen(s) + 1] = '\0';
  1763.     do_it(s);
  1764.     exit(EXIT_SUCCESS);
  1765.   }
  1766.  
  1767.   sprintf(s1, "%s\\NTIME.EXE", maindir);
  1768.   if ((exist(s1)) && (*TIMEHOST)) {
  1769.     output("\n ■ Polling %s...  ", TIMEHOST);
  1770.     sprintf(s, "%s %s", s1, TIMEHOST);
  1771.     do_spawn(s);
  1772.   }
  1773.  
  1774. #ifndef PACKET
  1775.   unload_klos();
  1776. #endif
  1777.  
  1778.   output("\n ■ Updating network connection records...");
  1779.  
  1780.   if ((sy != 32767) || ((sy == 32767) && (ONECALL))) {
  1781.     recdbytes = ((recdbytes + 1023) / 1024);
  1782.     sentbytes = ((sentbytes + 1023) / 1024);
  1783.     if (mailtime)
  1784.       sprintf(ttotal, "%3.1f", (mailtime / CLK_TCK / 60));
  1785.     else
  1786.       strcpy(ttotal, "0.1");
  1787.     sprintf(s, "%s\\PPPUTIL.EXE NETLOG %hd %lu %lu %s %s",
  1788.         maindir, sy, sentbytes, recdbytes, ttotal, net_name);
  1789.     if (do_spawn(s))
  1790.       log_it(1, "\n ! %s", strings[4]);
  1791.     if (update_contacts(sy, sentbytes, recdbytes))
  1792.       log_it(1, "\n ■ %s", strings[3]);
  1793.   }
  1794.   if (news) {
  1795.     if ((newstime = clock()) == (clock_t) - 1)
  1796.       log_it(1, "\n ■ NEWS time invalid.");
  1797.     else {
  1798.       newstime -= starttime;
  1799.       if (newstime)
  1800.         sprintf(ttotal, "%3.1f", (newstime / CLK_TCK / 60));
  1801.       else
  1802.         strcpy(ttotal, "0.1");
  1803.       rnewsbytes = ((rnewsbytes + 1023) / 1024);
  1804.       if (rnewsbytes > 9999L)
  1805.         rnewsbytes = 9999L;
  1806.       snewsbytes = ((snewsbytes + 1023) / 1024);
  1807.       if (update_contact(32767, snewsbytes, rnewsbytes))
  1808.         log_it(1, "\n ■ %s", strings[3]);
  1809.       sprintf(s, "%s\\PPPUTIL.EXE NETLOG 32767 %lu %lu %s %s", maindir, snewsbytes, rnewsbytes, ttotal, net_name);
  1810.       if (do_spawn(s))
  1811.         log_it(1, "\n ! %s", strings[4]);
  1812.     }
  1813.   }
  1814.   if (CLEANUP) {
  1815.     process_mail();
  1816.     set_net_num(netnum);
  1817.   }
  1818.   sprintf(s, "%s\\PPPUTIL.EXE TRIM %s NEWS.LOG", maindir, net_data);
  1819.   do_spawn(s);
  1820.   cd_to(maindir);
  1821.   set_net_num(netnum);
  1822.   log_it(1, "\n ■ %s completed!\n\n", version);
  1823.   return 0;
  1824. }
  1825.