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