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