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