home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC15B21.ZIP / NETWORK.C < prev    next >
C/C++ Source or Header  |  1997-03-29  |  43KB  |  1,725 lines

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