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