home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B78.ZIP / NETWORK.CPP < prev    next >
Text File  |  1997-12-28  |  55KB  |  2,226 lines

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