home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B22.ZIP / NETWORK.C < prev    next >
C/C++ Source or Header  |  1997-03-30  |  44KB  |  1,737 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, SOCK_DELAY = 30, SOCK_INACTIVE = 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.   ss[i] = NULL;
  319.   i = (spawnvpe(P_WAIT, ss[0], ss, xenviron) & 0x00ff);
  320.   free(s);
  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, "SOCK_DELAY", 10) == 0) {
  829.         if (atoi(ss))
  830.           SOCK_DELAY = atoi(ss);
  831.         continue;
  832.       }
  833.       if (strnicmp(line, "SOCK_INACTIVE", 13) == 0) {
  834.         if (atoi(ss))
  835.           SOCK_INACTIVE = atoi(ss);
  836.         continue;
  837.       }
  838.       if (strnicmp(line, "KEEPSENT", 8) == 0) {
  839.         if (atoi(ss))
  840.           KEEPSENT = atoi(ss);
  841.         continue;
  842.       }
  843.       if (strnicmp(line, "ALLMAIL", 7) == 0) {
  844.         if (toupper(ss[0]) == 'N')
  845.           ALLMAIL = 0;
  846.         continue;
  847.       }
  848.       if (strnicmp(line, "ONECALL", 7) == 0) {
  849.         if (toupper(ss[0]) == 'Y')
  850.           ONECALL = 1;
  851.         continue;
  852.       }
  853.       if (strnicmp(line, "MOREINFO", 8) == 0) {
  854.         if (toupper(ss[0]) == 'Y')
  855.           MOREINFO = 1;
  856.         continue;
  857.       }
  858.       if (strnicmp(line, "CLEANUP", 7) == 0) {
  859.         if (toupper(ss[0]) == 'Y')
  860.           CLEANUP = 1;
  861.         continue;
  862.       }
  863.       if (strnicmp(line, "IPADDR", 6) == 0) {
  864.         if ((ss) && (ss[0] != '0'))
  865.           strcpy(IPADDR, ss);
  866.         continue;
  867.       }
  868.       if (strnicmp(line, "NETMASK", 7) == 0) {
  869.         if (ss)
  870.           strcpy(NETMASK, ss);
  871.         continue;
  872.       }
  873.       if (strnicmp(line, "DNS", 3) == 0) {
  874.         if (ss)
  875.           strcpy(DNS, ss);
  876.         continue;
  877.       }
  878.       if (strnicmp(line, "DOMAIN", 6) == 0) {
  879.         if (ss)
  880.           strcpy(DOMAIN, ss);
  881.         continue;
  882.       }
  883.       if (strnicmp(line, "GATEWAY", 7) == 0) {
  884.         if ((ss) && (ss[0] != '0'))
  885.           strcpy(GATEWAY, ss);
  886.         continue;
  887.       }
  888.     }
  889.     if (cursect & INI_NEWS) {
  890.       if (strnicmp(line, "NEWSHOST", 8) == 0) {
  891.         if (ss)
  892.           strcpy(NEWSHOST, ss);
  893.         continue;
  894.       }
  895.     }
  896.   }
  897.   fclose(fp);
  898.   return (1);
  899. }
  900.  
  901. int write_contact(unsigned short sy2, unsigned long sb, unsigned long rb, int sent, int news)
  902. {
  903.   char s[101], buf[81];
  904.   int i, i1, f;
  905.   long cur_time, l;
  906.   unsigned short sy;
  907.   FILE *fp;
  908.  
  909.   sprintf(s, "%sCONTACT.NET", net_data);
  910.   f = sh_open1(s, O_RDONLY | O_BINARY);
  911.   if (f >= 0) {
  912.     l = filelength(f);
  913.     netcfg.num_ncn = (int) (l / sizeof(net_contact_rec));
  914.     if ((netcfg.ncn = (net_contact_rec *) malloc((netcfg.num_ncn + 2) *
  915.                                         sizeof(net_contact_rec))) == NULL) {
  916.       fprintf(stderr, "\n ■ Unable to allocate %d bytes of memory to update CONTACT.NET",
  917.               (int) (netcfg.num_ncn + 2) * sizeof(net_contact_rec));
  918.       sh_close(f);
  919.       return 1;
  920.     }
  921.     sh_lseek(f, 0L, SEEK_SET);
  922.     sh_read(f, (void *) netcfg.ncn,
  923.             netcfg.num_ncn * sizeof(net_contact_rec));
  924.     sh_close(f);
  925.   }
  926.   sprintf(s, "%sCALLOUT.NET", net_data);
  927.   fp = fsh_open(s, "rt");
  928.   if (!fp) {
  929.     fprintf(stderr, "\n ■ Unable to access %sCALLOUT.NET", net_data);
  930.     free(netcfg.ncn);
  931.     return 1;
  932.   }
  933.   while (fgets(buf, 80, fp) != NULL) {
  934.     if (buf[0] != '@')
  935.       continue;
  936.     sy = atoi(&buf[1]);
  937.     if ((news) && (sy != 32767))
  938.       continue;
  939.     else {
  940.       if ((!news) && (sy == 32767))
  941.         continue;
  942.     }
  943.     for (i = 0; i < netcfg.num_ncn; i++)
  944.       if (netcfg.ncn[i].systemnumber == sy)
  945.         i1 = i;
  946.     time(&cur_time);
  947.     netcfg.ncn[i1].lasttry = cur_time;
  948.     if (!sent) {
  949.       netcfg.ncn[i1].numfails++;
  950.     } else {
  951.       ++netcfg.ncn[i1].numcontacts;
  952.       if (!netcfg.ncn[i1].firstcontact)
  953.         netcfg.ncn[i1].firstcontact = cur_time;
  954.       netcfg.ncn[i1].lastcontact = cur_time;
  955.       netcfg.ncn[i1].lastcontactsent = cur_time;
  956.       if (sy == sy2) {
  957.         netcfg.ncn[i1].bytes_sent += sb;
  958.         if (rb)
  959.           netcfg.ncn[i1].bytes_received += rb;
  960.       }
  961.       netcfg.ncn[i1].bytes_waiting = 0L;
  962.       sprintf(s, "%sS%hd.net", net_data, sy);
  963.       if (!exist(s))
  964.         sprintf(s, "%sZ%hd.net", net_data, sy);
  965.       f = sh_open1(s, O_RDONLY | O_BINARY);
  966.       if (f > 0) {
  967.         netcfg.ncn[i1].bytes_waiting += filelength(f);
  968.         close(f);
  969.       }
  970.     }
  971.  
  972.     sprintf(s, "%sCONTACT.NET", net_data);
  973.     f = sh_open1(s, O_RDWR | O_CREAT | O_BINARY);
  974.     if (f > 0) {
  975.       sh_lseek(f, 0L, SEEK_SET);
  976.       sh_write(f, (void *) netcfg.ncn, netcfg.num_ncn *
  977.                sizeof(net_contact_rec));
  978.       sh_close(f);
  979.     }
  980.   }
  981.   free(netcfg.ncn);
  982.   fclose(fp);
  983.   return 0;
  984. }
  985.  
  986. long count_lines(char *s)
  987. {
  988.   FILE *fp;
  989.   char fn[121];
  990.   long int nl = 0;
  991.   const int NEWLINE = '\n';
  992.   int c;
  993.  
  994.   strcpy(fn, s);
  995.   fp = fsh_open(fn, "rt");
  996.   if (fp == NULL) {
  997.     printf("\n ■ Cannot open %s", fn);
  998.     return 0;
  999.   }
  1000.   while ((c = getc(fp)) != EOF) {
  1001.     if (c == NEWLINE)
  1002.       ++nl;
  1003.   }
  1004.   fclose(fp);
  1005.   return nl;
  1006. }
  1007.  
  1008. typedef struct {
  1009.   char line[81];
  1010. } nlines;
  1011.  
  1012. nlines *NETLOG;
  1013.  
  1014. int write_netlog(int sn, long sent, long recd, char *tmused)
  1015. {
  1016.   FILE *fp;
  1017.   int i = 0;
  1018.   unsigned lines = 0L;
  1019.   char s[81], s1[81], s2[81], fn[121];
  1020.   struct tm *time_now;
  1021.   time_t some;
  1022.  
  1023.   sprintf(fn, "%sNET.LOG", syscfg.gfilesdir);
  1024.   lines = count_lines(fn);
  1025.   if ((NETLOG = ((nlines *) malloc((lines + 2) * 81))) == NULL) {
  1026.     fprintf(stderr, "\n ■ Unable to allocate %d bytes for NET.LOG entry",
  1027.             (lines + 2) * 81);
  1028.     return 1;
  1029.   }
  1030.   fp = fsh_open(fn, "r");
  1031.   if (!fp)
  1032.     return 1;
  1033.   while (fgets(s, 80, fp)) {
  1034.     strtok(s, "\n");
  1035.     strcpy(NETLOG[i++].line, s);
  1036.   }
  1037.   fclose(fp);
  1038.   time(&some);
  1039.   time_now = localtime(&some);
  1040.   strftime(s1, 35, "%m/%d/%y %H:%M:%S", time_now);
  1041.   if ((sent) || (recd)) {
  1042.     if ((recd) && (!sent))
  1043.       sprintf(s2, "         R:%4ldk,", recd);
  1044.     else {
  1045.       if ((recd) && (sent))
  1046.         sprintf(s2, "S:%4ldk, R:%4ldk,", sent, recd);
  1047.       else
  1048.         sprintf(s2, "S:%4ldk,         ", sent);
  1049.     }
  1050.   } else
  1051.     strcpy(s2, "                 ");
  1052.   fp = fsh_open(fn, "wt+");
  1053.   fprintf(fp, "%s To %5d, %s          %4s min  %s\n", s1, sn, s2,
  1054.           tmused, net_name);
  1055.   for (i = 0; i < lines; i++)
  1056.     fprintf(fp, "%s\n", NETLOG[i].line);
  1057.   free(NETLOG);
  1058.   NETLOG = NULL;
  1059.   fclose(fp);
  1060.   return 0;
  1061. }
  1062.  
  1063. void read_networks(void)
  1064. {
  1065.   int f;
  1066.   char s[121];
  1067.  
  1068.   sprintf(s, "%sNETWORKS.DAT", syscfg.datadir);
  1069.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1070.   net_num_max = 0;
  1071.   if (f > 0) {
  1072.     net_num_max = (int) (filelength(f) / sizeof(net_networks_rec));
  1073.     sh_close(f);
  1074.   }
  1075. }
  1076.  
  1077. void init(void)
  1078. {
  1079.   char s[81], s1[81], *ss;
  1080.   int i, i1, dv, f;
  1081.   struct tm *time_now;
  1082.   time_t some;
  1083.  
  1084.   fprintf(stderr, "\n%s\n%s\n\n", version, author);
  1085.   get_dir(maindir, 0);
  1086.   ss = getenv("WWIV_INSTANCE");
  1087.   if (ss) {
  1088.     instance = atoi(ss);
  1089.     if ((instance <= 0) || (instance > 999)) {
  1090.       fprintf(stderr, "\n ■ WWIV_INSTANCE can only be 1..999!");
  1091.       instance = 1;
  1092.     }
  1093.   } else {
  1094.     instance = 1;
  1095.   }
  1096.   if (get_nb_version())
  1097.     multitasker = 8;
  1098.   else
  1099.     detect_multitask();
  1100.   switch (multitasker) {
  1101.     case 1:
  1102.       dv = get_dv_version();
  1103.       sprintf(s1, "DesqView %d.%02d", dv / 256, dv % 256);
  1104.       break;
  1105.     case 2:
  1106.       dv = get_win_version();
  1107.       if (dv == 4)
  1108.         strcpy(s1, "Windows 95");
  1109.       else
  1110.         sprintf(s1, "Windows %d.%02d", dv % 256, dv / 256);
  1111.       break;
  1112.     case 3:
  1113.       dv = get_dv_version();
  1114.       sprintf(s1, "Windows and DesqView %d.%02d", dv / 256, dv % 256);
  1115.       break;
  1116.     case 4:
  1117.     case 5:
  1118.     case 6:
  1119.     case 7:
  1120.       if ((_osmajor / 10 == 2) && (_osminor >= 30))
  1121.         strcpy(s1, "OS/2 Warp");
  1122.       else
  1123.         sprintf(s1, "OS/2 %d.%2.2d", _osmajor / 10, _osminor);
  1124.       break;
  1125.     case 8:
  1126.       sprintf(s1, "NETBIOS network");
  1127.       multitasker = 1;
  1128.       break;
  1129.     default:
  1130.       strcpy(s1, "DOS");
  1131.       break;
  1132.   }
  1133.   strcpy(s, "CONFIG.DAT");
  1134.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1135.   if (f < 0) {
  1136.     fprintf(stderr, "\n ■ %s NOT FOUND.", s);
  1137.     return;
  1138.   }
  1139.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  1140.   sh_close(f);
  1141.   fprintf(stderr, " ■ Running under %s on instance %d ", s1, instance);
  1142.   time(&some);
  1143.   time_now = localtime(&some);
  1144.   strftime(s, 80, "at %I:%M%p, %d %b %y", time_now);
  1145.   printf(s);
  1146.   strcpy(s, "CONFIG.OVR");
  1147.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1148.   if (f < 0) {
  1149.     fprintf(stderr, "\n ■ %s NOT FOUND.", s);
  1150.     return;
  1151.   }
  1152.   sh_lseek(f, (instance - 1) * sizeof(configoverrec), SEEK_SET);
  1153.   read(f, &syscfgovr, sizeof(configoverrec));
  1154.   f = sh_close(f);
  1155.   sprintf(s, "%sINSTANCE.DAT", syscfg.datadir);
  1156.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1157.   if (f < 0) {
  1158.     fprintf(stderr, "\n ■ %s NOT FOUND.", s);
  1159.     return;
  1160.   }
  1161.   ninst = (int) ((filelength(f) / (long) sizeof(instancerec)));
  1162.   f = sh_close(f);
  1163.   read_networks();
  1164.   i = i1 = 0;
  1165.   while (environ[i] != NULL) {
  1166.     xenviron[i1++] = environ[i];
  1167.     ++i;
  1168.   }
  1169.   xenviron[i1] = NULL;
  1170. }
  1171.  
  1172. long getfctime(char *s)
  1173. {
  1174.   struct stat statbuf;
  1175.  
  1176.   if (stat(s, &statbuf) < 0)
  1177.     return (0L);
  1178.   else
  1179.     return (statbuf.st_atime);
  1180. }
  1181.  
  1182. int process_bbsdata(void)
  1183. {
  1184.   char s[101];
  1185.   unsigned long bbslist_ctime, connect_ctime, callout_ctime, bbsdata_ctime;
  1186.   int tf, n, net3;
  1187.  
  1188.   net3 = 0;
  1189.   sprintf(s, "%sBBSLIST.NET", net_data);
  1190.   bbslist_ctime = getfctime(s);
  1191.   sprintf(s, "%sCONNECT.NET", net_data);
  1192.   connect_ctime = getfctime(s);
  1193.   sprintf(s, "%sCALLOUT.NET", net_data);
  1194.   callout_ctime = getfctime(s);
  1195.   sprintf(s, "%sBBSDATA.NET", net_data);
  1196.   bbsdata_ctime = getfctime(s);
  1197.   sprintf(s, "%sBBSLIST.UPD", net_data);
  1198.   bbslist_ctime = getfctime(s);
  1199.   sprintf(s, "%sCONNECT.UPD", net_data);
  1200.   connect_ctime = getfctime(s);
  1201.  
  1202.   n = 0;
  1203.   tf = ((bbslist_ctime > bbsdata_ctime) || (connect_ctime > bbsdata_ctime));
  1204.   if (tf)
  1205.     n = 1;
  1206.   tf = (tf || (callout_ctime > bbsdata_ctime));
  1207.   if (tf) {
  1208.     sprintf(s, "NETWORK3 .%d", netnum);
  1209.     if (n) {
  1210.       sprintf(s, "NETWORK3 .%d Y", netnum);
  1211.       net3 = 1;
  1212.     }
  1213.     do_spawn(s);
  1214.   }
  1215.   return (net3);
  1216. }
  1217.  
  1218. void do_it(char *cl)
  1219. {
  1220.   int i, i1, l;
  1221.   char *s, *ss[30];
  1222.  
  1223.   s = strdup(cl);
  1224.   ss[0] = s;
  1225.   i = 1;
  1226.   l = strlen(s);
  1227.   for (i1 = 1; i1 < l; i1++)
  1228.     if (s[i1] == 32) {
  1229.       s[i1] = 0;
  1230.       ss[i++] = &(s[i1 + 1]);
  1231.     }
  1232.   ss[i] = NULL;
  1233.   execvpe(ss[0], ss, xenviron);
  1234. }
  1235.  
  1236. int create_wattcp_cfg(void)
  1237. {
  1238.   char s[101], s1[41];
  1239.   FILE *fp;
  1240.  
  1241.   if ((fp = fsh_open("WATTCP.CFG", "wt+")) == NULL) {
  1242.     fprintf(stderr, "\n ■ Can't open %s for output!", s);
  1243.     return 1;
  1244.   }
  1245.   sprintf(s, "my_ip=%s\n", IPADDR);
  1246.   fprintf(fp, s);
  1247.   fprintf(fp, "netmask=%s\n", NETMASK);
  1248.   fprintf(fp, "nameserver=%s\n", DNS);
  1249.   if (GATEWAY[0] == 0) {
  1250.     strcpy(s1, IPADDR);
  1251.     while (LAST(s1) != '.')
  1252.       LAST(s1) = 0;
  1253.     strcat(s1, "1");
  1254.     strcpy(GATEWAY, s1);
  1255.   }
  1256.   sprintf(s, "gateway=%s\n", GATEWAY);
  1257.   fprintf(fp, s);
  1258.   fprintf(fp, "domainslist=\"%s\"\n", DOMAIN);
  1259.   fprintf(fp, "hostname=\"%s\"\n", POPNAME);
  1260.   fprintf(fp, "sockdelay=\"%d\"\n", SOCK_DELAY);
  1261.   fprintf(fp, "sockinactive=\"%d\"\n", SOCK_INACTIVE);
  1262.   fclose(fp);
  1263.   return 0;
  1264. }
  1265.  
  1266. int check_for_lsl(void)
  1267. {
  1268.   union REGS r;
  1269.   struct SREGS s;
  1270.   char *intstr;
  1271.  
  1272.   for (r.h.ah = 0xc0; r.h.ah != 0; r.h.ah++) {
  1273.     r.h.al = 0;
  1274.     int86x(0x2f, &r, &r, &s);
  1275.     if (r.h.al == 0xff) {
  1276.       intstr = (char *) MK_FP(s.es, r.x.si);
  1277.       if (strncmp(intstr, "LINKSUP$", 8) == 0)
  1278.         return 1;
  1279.     }
  1280.   }
  1281.   return 0;
  1282. }
  1283.  
  1284. void klos_ppp(void)
  1285. {
  1286.   char s[121], *ss;
  1287.   int i;
  1288.   FILE *fp;
  1289.  
  1290.   lsl_loaded = check_for_lsl();
  1291.   if (!lsl_loaded) {
  1292.     sprintf(s, "%s\\LSL.COM", maindir);
  1293.     if (exist(s)) {
  1294.       fprintf(stderr, "\n ■ Loading Link Support Layer and ");
  1295.       do_spawn(s);
  1296.     } else {
  1297.       fprintf(stderr, "\n ■ LSL.COM not in BBS main directory!");
  1298.       exit(EXIT_FAILURE);
  1299.     }
  1300.   } else
  1301.     fprintf(stderr, "\n ■ LSL already in memory - loading ");
  1302.   sprintf(s, "%s\\PPP.EXE", maindir);
  1303.   if (exist(s)) {
  1304.     fprintf(stderr, "PPP packet driver... ");
  1305.     do_spawn(s);
  1306.   } else {
  1307.     fprintf(stderr, "\n ■ KLOS PPP.EXE not in BBS main directory!");
  1308.     exit(EXIT_FAILURE);
  1309.   }
  1310.   sprintf(s, "%s\\PPPSTATE.EXE", maindir);
  1311.   if (exist(s)) {
  1312.     fprintf(stderr, "\n ■ Dialing %s and waiting %d seconds for connection... ", DOMAIN, TIMEOUT);
  1313.     sprintf(s, "%s\\PPPSTATE.EXE WAIT=%d IP", maindir, TIMEOUT);
  1314.     i = do_spawn(s);
  1315.     switch (i) {
  1316.       case 0:
  1317.         fprintf(stderr, " connected!");
  1318.         break;
  1319.       case 1:
  1320.         fprintf(stderr, "\n ■ Unable to establish connection... aborting");
  1321.         sprintf(s, "%s\\PPP U", maindir);
  1322.         do_spawn(s);
  1323.         if (!lsl_loaded) {
  1324.           sprintf(s, "%s\\LSL U", maindir);
  1325.           do_spawn(s);
  1326.         }
  1327.         exit(EXIT_FAILURE);
  1328.       case 2:
  1329.         fprintf(stderr, "\n ■ Unable to find PPP layer... aborting");
  1330.         sprintf(s, "%s\\PPP U", maindir);
  1331.         do_spawn(s);
  1332.         if (!lsl_loaded) {
  1333.           sprintf(s, "%s\\LSL U", maindir);
  1334.           do_spawn(s);
  1335.         }
  1336.         exit(EXIT_FAILURE);
  1337.       default:
  1338.         fprintf(stderr, "\n ■ Unknown PPPState Error #%d... aborting", i);
  1339.         sprintf(s, "%s\\PPP U", maindir);
  1340.         do_spawn(s);
  1341.         if (!lsl_loaded) {
  1342.           sprintf(s, "%s\\LSL U", maindir);
  1343.           do_spawn(s);
  1344.         }
  1345.         exit(EXIT_FAILURE);
  1346.     }
  1347.   } else {
  1348.     fprintf(stderr, "\n ■ PPPSTATE.EXE not in BBS main directory!");
  1349.     exit(EXIT_FAILURE);
  1350.   }
  1351.   sprintf(s, "%s\\PPPWAT.EXE", maindir);
  1352.   if (exist(s)) {
  1353.     do_spawn(s);
  1354.   } else {
  1355.     fprintf(stderr, "\n ■ KLOS PPPWAT.EXE not in BBS main directory!");
  1356.     exit(EXIT_FAILURE);
  1357.   }
  1358.   if (!IPADDR[0]) {
  1359.     fp = fsh_open("WATTCP.CFG", "rt");
  1360.     if (fp) {
  1361.       while (fgets(s, 80, fp) != NULL) {
  1362.         if (strnicmp(s, "my_ip", 5) == 0) {
  1363.           ss = strtok(s, "=");
  1364.           ss = strtok(NULL, "\r");
  1365.           trimstr1(ss);
  1366.           strcpy(IPADDR, ss);
  1367.           break;
  1368.         }
  1369.       }
  1370.       fclose(fp);
  1371.     }
  1372.   }
  1373.   if (IPADDR) {
  1374.     create_wattcp_cfg();
  1375.     fprintf(stderr, "\n ■ IP address is %s... ", IPADDR);
  1376.   } else {
  1377.     fprintf(stderr, "\n ■ Could not find IP Address for WATTCP.CFG... aborting!");
  1378.     unload_klos();
  1379.     exit(EXIT_FAILURE);
  1380.   }
  1381.   sprintf(s, "%s\\IPSTUB.EXE", maindir);
  1382.   if (exist(s)) {
  1383.     do_spawn(s);
  1384.   } else {
  1385.     fprintf(stderr, "\n ■ KLOS PPPWAT.EXE not in BBS main directory!");
  1386.     exit(EXIT_FAILURE);
  1387.   }
  1388.   fprintf(stderr, "establishing socket     ");
  1389.   for (i = 5; i >= 0; i--) {
  1390.     fprintf(stderr, "\b\b\b\b%d...", i);
  1391.     sleep(1);
  1392.   }
  1393.   fprintf(stderr, "\b\b\b\b- ready!");
  1394. }
  1395.  
  1396. double freek(int dr)
  1397. {
  1398.   float d;
  1399.   struct dfree df;
  1400.  
  1401.   getdfree(dr, &df);
  1402.   d = (float) df.df_avail;
  1403.   d *= ((float) df.df_bsec);
  1404.   d *= ((float) df.df_sclus);
  1405.   d /= 1024.0;
  1406.   if (df.df_sclus == 0xffff)
  1407.     d = -1.0;
  1408.   return (d);
  1409. }
  1410.  
  1411. int main(int argc, char *argv[])
  1412. {
  1413.   int i, ok, news, send, f1;
  1414.   unsigned short sy;
  1415.   double dspace;
  1416.   char *ss, s[121], s1[MAXPATH], destaddr[121], temp[121];
  1417.   char ttotal[21];
  1418.   clock_t starttime;
  1419.   unsigned long sentbytes, recdbytes, rnewsbytes, snewsbytes;
  1420.   FILE *fp;
  1421.   struct ffblk ff;
  1422.  
  1423.   init();
  1424.   news = 0;
  1425.   ss = argv[argc - 1];
  1426.   ok = 0;
  1427.   if (_fstrstr((char *) ss, ".") != NULL) {
  1428.     if (atoi(&ss[1]) < net_num_max) {
  1429.       netnum = atoi(&ss[1]);
  1430.       ok = 1;
  1431.     }
  1432.   } else {
  1433.     ss = (getenv("WWIV_NET"));
  1434.     netnum = atoi(ss);
  1435.     if (netnum < net_num_max)
  1436.       ok = 1;
  1437.   }
  1438.   if (!ok) {
  1439.     strcpy(s, "WWIV_NET.DAT");
  1440.     if ((fp = fsh_open(s, "rb")) != NULL) {
  1441.       fgets(s, 3, fp);
  1442.       netnum = atoi(s);
  1443.       fclose(fp);
  1444.       if (netnum < net_num_max)
  1445.         ok = 1;
  1446.     }
  1447.   }
  1448.   ss = argv[1];
  1449.   if (strncmpi(ss, "/N", 2))
  1450.     ok = 0;
  1451.   if (ok) {
  1452.     SMTPHOST[0] = 0;
  1453.     POPHOST[0] = 0;
  1454.     NEWSHOST[0] = 0;
  1455.     POPNAME[0] = 0;
  1456.     POPPASS[0] = 0;
  1457.     IPADDR[0] = 0;
  1458.     NETMASK[0] = 0;
  1459.     DNS[0] = 0;
  1460.     DOMAIN[0] = 0;
  1461.     GATEWAY[0] = 0;
  1462.     if (parse_net_ini()) {
  1463.       set_net_num(netnum);
  1464.       if (ini_section & INI_NETWORK) {
  1465.         if (!(ini_section & INI_GENERAL)) {
  1466.           fprintf(stderr, "\n ■ [GENERAL] tag missing from NET.INI!");
  1467.           ok = 0;
  1468.         }
  1469.       } else {
  1470.         ok = 0;
  1471.         fprintf(stderr, "\n ■ [FILENET] tag missing from NET.INI!");
  1472.       }
  1473.     } else {
  1474.       fprintf(stderr, "\n ■ Could not open file NET.INI!");
  1475.       ok = 0;
  1476.     }
  1477.   }
  1478.   if (ok) {
  1479.     fprintf(stderr, "\n ■ Available memory : %lu bytes...", coreleft());
  1480.     if (net_data[1] == ':')
  1481.       i = net_data[0];
  1482.     else
  1483.       i = maindir[0];
  1484.     i = toupper(i) - 'A' + 1;
  1485.     dspace = freek(i);
  1486.     if (dspace < 1024.0) {
  1487.       fprintf(stderr, "\n ■ Only %.1fK available on drive %c:... aborting!",
  1488.               dspace, i + '@');
  1489.       exit(EXIT_FAILURE);
  1490.     } else {
  1491.       fprintf(stderr, " disk space ");
  1492.       if (dspace < 10000.0)
  1493.         fprintf(stderr, ": %c:%.1fK", i + '@', dspace);
  1494.       else
  1495.         fprintf(stderr, ": %c:%.1fM", i + '@', dspace / 1024.0);
  1496.     }
  1497.  
  1498.     set_net_num(netnum);
  1499.     ss = argv[1];
  1500.     sy = atoi(&ss[2]);
  1501.     destaddr[0] = 0;
  1502.     if (sy != 32767)
  1503.       good_addr(destaddr, sy);
  1504.  
  1505.     if ((destaddr[0] == 0) && (sy != 32767)) {
  1506.       fprintf(stderr, "\n ■ Using direct dial for @%hd.%s\n\n", sy, net_name);
  1507.       ok = 0;
  1508.     } else {
  1509.       ok = 1;
  1510.       if (sy == 32767) {
  1511.         if (ini_section & INI_NEWS) {
  1512.           fprintf(stderr, "\n ■ Initiating newsgroup retrieval session...");
  1513.           news = 1;
  1514.         } else {
  1515.           fprintf(stderr, "\n ■ [NEWS] tag missing from NET.INI!");
  1516.           exit(EXIT_FAILURE);
  1517.         }
  1518.       }
  1519.     }
  1520.   }
  1521.   if (ok) {
  1522.     if (!MOREINFO)
  1523.       freopen(NULL, "w", stdout);
  1524.     if (CLEANUP)
  1525.       process_mail();
  1526.     set_net_num(netnum);
  1527.     sprintf(s, "%sSPOOL", net_data);
  1528.     if ((make_path(s)) < 0) {
  1529.       fprintf(stderr, "\n ■ Unable to create \"%s\"", s);
  1530.       exit(EXIT_FAILURE);
  1531.     }
  1532.     sprintf(s, "%sMQUEUE", net_data);
  1533.     if ((make_path(s)) < 0) {
  1534.       fprintf(stderr, "\n ■ Unable to create \"%s\"", s);
  1535.       exit(EXIT_FAILURE);
  1536.     }
  1537.     sprintf(s, "%sOUTBOUND", net_data);
  1538.     if ((make_path(s)) < 0) {
  1539.       fprintf(stderr, "\n ■ Unable to create \"%s\"", s);
  1540.       exit(EXIT_FAILURE);
  1541.     }
  1542.     sprintf(s, "%sINBOUND", net_data);
  1543.     if ((make_path(s)) < 0) {
  1544.       fprintf(stderr, "\n ■ Unable to create \"%s\"", s);
  1545.       exit(EXIT_FAILURE);
  1546.     }
  1547.     sprintf(s, "%sSENT", net_data);
  1548.     if ((make_path(s)) < 0) {
  1549.       fprintf(stderr, "\n ■ Unable to create \"%s\"", s);
  1550.       exit(EXIT_FAILURE);
  1551.     }
  1552.     if ((KEEPSENT) && (sy != 32767)) {
  1553.       fprintf(stderr, "\n ■ Purging sent packets older than %d day%s...", KEEPSENT,
  1554.               KEEPSENT == 1 ? "" : "s");
  1555.       i = purge_sent();
  1556.       fprintf(stderr, " %d packet%s deleted.", i, i == 1 ? "" : "s");
  1557.     }
  1558.     sprintf(s, "%sCHECKNET", net_data);
  1559.     if ((make_path(s)) < 0) {
  1560.       fprintf(stderr, "\n ■ Unable to create \"%s\"", s);
  1561.       exit(EXIT_FAILURE);
  1562.     }
  1563.     send = 1;
  1564.     cd_to(maindir);
  1565.     if ((sy != 32767) || (ONECALL)) {
  1566.       fprintf(stderr, "\n ■ Preparing outbound mail for transmission...");
  1567.       if (!uu_packets()) {
  1568.         sprintf(s, "%sMQUEUE\\*.*", net_data);
  1569.         if (findfirst(s, &ff, 0) == 0) {
  1570.           fprintf(stderr, "\n ■ Outbound packets in MQUEUE to deliver.");
  1571.         } else {
  1572.           fprintf(stderr, " no packets pending.");
  1573.           send = 0;
  1574.         }
  1575.       }
  1576.     }
  1577.     sprintf(s, "%sNET%d.CFG", net_data, instance);
  1578.     if (exist(s)) {
  1579.       sprintf(s1, "%s\\NET.CFG", maindir);
  1580.       unlink(s1);
  1581.       if (!copyfile(s, s1))
  1582.         fprintf(stderr, "\n ■ Unable to copy %s to %s", s, s1);
  1583.       else
  1584.         fprintf(stderr, "\n ■ Using %s as NET.CFG", strlwr(s));
  1585.     }
  1586.     sprintf(s, "%s\\EXP.EXE", maindir);
  1587.     if (!exist(s)) {
  1588.       fprintf(stderr, "\n ■ Export module EXP.EXE not found!");
  1589.     } else {
  1590.       sprintf(s, "%sS32767.NET", net_data);
  1591.       sprintf(s1, "%sSPOOL\\UNK*.*", net_data);
  1592.       if ((exist(s)) || (exist(s1))) {
  1593.         sprintf(s, "EXP.EXE S32767.NET %s %hu %s %s %s",
  1594.                 net_data, net_sysnum, POPNAME, DOMAIN, net_name);
  1595.         if (do_spawn(s))
  1596.           fprintf(stderr, "\n ■ Unknown error during export");
  1597.       } else
  1598.         fprintf(stderr, "\n ■ No inbound or outbound Internet mail or newsgroup posts to process.");
  1599.     }
  1600.  
  1601.     klos_ppp();
  1602.     starttime = clock();
  1603.     sentbytes = recdbytes = snewsbytes = rnewsbytes = 0L;
  1604.  
  1605.     cd_to(maindir);
  1606.     create_wattcp_cfg();
  1607.  
  1608.     sprintf(tempdir, "%sINBOUND\\", net_data);
  1609.  
  1610.     if ((send) && ((sy != 32767) || (ONECALL))) {
  1611.       sprintf(temp, "%sMQUEUE\\*.*", net_data);
  1612.       f1 = findfirst(temp, &ff, FA_ARCH);
  1613.       while (f1 == 0) {
  1614.         sprintf(s1, "%sMQUEUE\\%s", net_data, ff.ff_name);
  1615.         sprintf(s, "%s\\POP -send %s %s %s", maindir, SMTPHOST, DOMAIN, s1);
  1616.         fprintf(stderr, "\n ■ Sending %s ", ff.ff_name);
  1617.         if (do_spawn(s) == EXIT_SUCCESS) {
  1618.           unlink(s1);
  1619.           sentbytes += ff.ff_fsize;
  1620.         } else
  1621.           fprintf(stderr, "\n ■ Unsuccessful!  UUE packets remain in MQUEUE.");
  1622.         f1 = findnext(&ff);
  1623.       }
  1624.       fprintf(stderr, "\n ■ Outbound net packet transfers complete.");
  1625.       send = 0;
  1626.     }
  1627.     if ((sy != 32767) || (ONECALL)) {
  1628.       sprintf(s, "%s\\POP -receive %s %s %s %s %d", maindir, POPHOST, POPNAME, POPPASS,
  1629.               tempdir, ALLMAIL);
  1630.       fprintf(stderr, "\n ■ Checking %s for inbound net packets.", POPHOST);
  1631.       sprintf(s1, "%s*.*", tempdir);
  1632.       if ((do_spawn(s) == EXIT_SUCCESS) || (exist(s1))) {
  1633.         sprintf(temp, "%s*.*", tempdir);
  1634.         f1 = findfirst(temp, &ff, FA_ARCH);
  1635.         i = 0;
  1636.         send = 0;
  1637.         while (f1 == 0) {
  1638.           if (strncmp(ff.ff_name, "BAD", 3) != 0) {
  1639.             recdbytes += ff.ff_fsize;
  1640.             sprintf(s1, "%s%s", tempdir, ff.ff_name);
  1641.             sprintf(temp, "%s\\UU -decode %s %s %s", maindir, ff.ff_name,
  1642.                     tempdir, net_data);
  1643.             if (!do_spawn(temp)) {
  1644.               send = 1;
  1645.               unlink(s1);
  1646.             }
  1647.           }
  1648.           f1 = findnext(&ff);
  1649.         }
  1650.       } else
  1651.         fprintf(stderr, "\n ■ No network packets to process");
  1652.  
  1653.       if ((send) && (CLEANUP)) {
  1654.         fprintf(stderr, "\n ■ Processing received packets... please wait.");
  1655.         process_mail();
  1656.         set_net_num(netnum);
  1657.         process_bbsdata();
  1658.       }
  1659.     }
  1660.     if ((sy == 32767) || (ONECALL)) {
  1661.       set_net_num(netnum);
  1662.       sprintf(temp, "%sOUTBOUND\\*.*", net_data);
  1663.       f1 = findfirst(temp, &ff, FA_ARCH);
  1664.       while (f1 == 0) {
  1665.         snewsbytes += ff.ff_fsize;
  1666.         f1 = findnext(&ff);
  1667.       }
  1668.       cd_to(maindir);
  1669.       strcpy(s1, net_data);
  1670.       if (LAST(s1) == '\\')
  1671.         LAST(s1) = 0;
  1672.       sprintf(s, "%s\\NEWS.EXE %s %s %hu", maindir, s1, NEWSHOST, net_sysnum);
  1673.       ok = 1;
  1674.       if (do_spawn(s) != EXIT_SUCCESS)
  1675.         fprintf(stderr, "\n ■ Unsuccessful news retrieval session...");
  1676.       set_net_num(netnum);
  1677.       sprintf(temp, "%sP0*.*", net_data);
  1678.       f1 = findfirst(temp, &ff, 0);
  1679.       while (f1 == 0) {
  1680.         rnewsbytes += ff.ff_fsize;
  1681.         f1 = findnext(&ff);
  1682.       }
  1683.     }
  1684.   } else {
  1685.     fprintf(stderr, "\n");
  1686.     set_net_num(netnum);
  1687.     strcpy(s, "NETWORK0.EXE");
  1688.     for (i = 1; i < argc; i++) {
  1689.       strcat(s, " ");
  1690.       strcat(s, argv[i]);
  1691.     }
  1692.     s[strlen(s)+1] = '\0';
  1693.     do_it(s);
  1694.     exit(EXIT_SUCCESS);
  1695.   }
  1696.  
  1697.   unload_klos();
  1698.  
  1699.   starttime = clock() - starttime;
  1700.   if (starttime)
  1701.     sprintf(ttotal, "%2.1f", (starttime / CLK_TCK / 60));
  1702.   else
  1703.     strcpy(ttotal, "0.1");
  1704.   recdbytes = ((recdbytes + 1023) / 1024);
  1705.   sentbytes = ((sentbytes + 1023) / 1024);
  1706.   rnewsbytes = ((recdbytes + 1023) / 1024);
  1707.   snewsbytes = ((sentbytes + 1023) / 1024);
  1708.   fprintf(stderr, "\n ■ Updating network connection records...");
  1709.   if (ONECALL) {
  1710.     if (write_contact(sy, sentbytes, recdbytes, 1, 0))
  1711.       fprintf(stderr, "\n ■ Unable to access CONTACT.NET!");
  1712.     if (write_netlog(sy, sentbytes, recdbytes, ttotal))
  1713.       fprintf(stderr, "\n ■ Unable to access NET.LOG!");
  1714.     if (write_contact(32767, snewsbytes, rnewsbytes, 1, 1))
  1715.       fprintf(stderr, "\n ■ Unable to access CONTACT.NET!");
  1716.     if (write_netlog(32767, snewsbytes, rnewsbytes, ttotal))
  1717.       fprintf(stderr, "\n ■ Unable to access NET.LOG!");
  1718.   } else {
  1719.     if (news) {
  1720.       if (write_contact(32767, snewsbytes, rnewsbytes, 1, 1))
  1721.         fprintf(stderr, "\n ■ Unable to access CONTACT.NET!");
  1722.       if (write_netlog(32767, snewsbytes, rnewsbytes, ttotal))
  1723.         fprintf(stderr, "\n ■ Unable to access NET.LOG!");
  1724.     } else {
  1725.       if (write_contact(sy, sentbytes, recdbytes, 1, 0))
  1726.         fprintf(stderr, "\n ■ Unable to access CONTACT.NET!");
  1727.       if (write_netlog(sy, sentbytes, recdbytes, ttotal))
  1728.         fprintf(stderr, "\n ■ Unable to access NET.LOG!");
  1729.     }
  1730.   }
  1731.  
  1732.   cd_to(maindir);
  1733.   set_net_num(netnum);
  1734.   fprintf(stderr, "\n ■ %s completed!\n\n", version);
  1735.   return 0;
  1736. }
  1737.