home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B29.ZIP / NETWORK.C < prev    next >
C/C++ Source or Header  |  1997-06-29  |  44KB  |  1,799 lines

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