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