home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / BBS / MISC / NETS100.ZIP / NETS100.C < prev    next >
Encoding:
Text File  |  1990-06-14  |  41.3 KB  |  1,644 lines

  1. /*******************************************************************/
  2. /* This software was written by Matt Logan over nearly a year for  */
  3. /* the WWIVnet/Fidonet community.  This software may be freely     */
  4. /* modified and re-compiled.  Modified source must accompanied by  */
  5. /* the original source if it is made availiable on any public BBS. */
  6. /* Modified versions on the binaries may not be posted on any      */
  7. /* public BBS.  All documenation, source, and executables must be  */
  8. /* included in any distribution of this software.  All Copyright   */
  9. /* notices must be left intact in the source and included          */
  10. /* documentation files.                                            */
  11. /*                                                                 */
  12. /* The intent here is to provide the WWIV/FIDONET community with   */
  13. /* reliable access to the lastest versions of this software.  I    */
  14. /* would ask that any modifications that prove to be very useful   */
  15. /* be made availiable to me so that I may include them in future   */
  16. /* releases of this software.  This software may not be sold for   */
  17. /* profit for any reason.  I intend to keep NetSEX a Public Domain */
  18. /* utility FOREVER.                                                */
  19. /*                                                                 */
  20. /*                                                                 */
  21. /* Addresses:                                                      */
  22. /*              USENET:  matt@eecs.cs.pdx.edu                      */
  23. /*             WWIVNET:  1@5317                                    */
  24. /*                                                                 */
  25. /*             US MAIL:  Matt Logan                                */
  26. /*                       11790 S.W. Denney Rd.                     */
  27. /*                       Beaverton Oregon 97005                    */
  28. /*                                                                 */
  29. /*                                                                 */
  30. /* Contributions are welcome and will most likely be applied to my */
  31. /* tuition payments for my last year in college as a Physics/Music */
  32. /* major at Portland State University.                             */
  33. /*                                                       06/14/90  */
  34. /*******************************************************************/
  35.  
  36.  
  37.  
  38. /* NETS.c                                                           */
  39. /*                                                                  */
  40. /*    Converts FIDO packets into WWIV packets and vice versa.         */
  41. /*                                                                  */
  42. /*                                                                  */
  43. /* REVISION HISTORY:                                                */
  44. /*                                                                  */
  45. /*    Matt Logan - 09/25/89 Initial version.                        */
  46. /*                                                                  */
  47. /*    Matt Logan - 11/10/89 Added Aliases, Local and Sub Commands.  */
  48. /*                                                                  */
  49. /*    Matt Logan - 11/17/89 Processed fido messages get bit 10 in   */
  50. /*                          the Attribute field set instead of      */
  51. /*                          being deleted.                          */
  52. /*                                                                  */
  53. /*    Matt Logan - 01/05/90 A whole Lotta stuff fixed, optimized    */
  54. /*                          added. Points, OUT_ALIAS.               */
  55. /*                                                                  */
  56. /*    Matt Logan - 02/11/90 Version 0.89 a ^AWWIVGATE: line is now  */
  57. /*                          used to mark messages as processed.  A  */
  58. /*                          few more bugs have been fixed.          */
  59. /*                          P<N>.NET Files are now created instead  */
  60. /*                          of being appended (Dangerous) and the   */
  61. /*                          limit on the number of BBS's has been   */
  62. /*                          removed.                                */
  63.  
  64. /* Version - used in log files and in the startup message */
  65. /* NetSEX prints.  It must be FOUR characters long */
  66. #define Version "1.00"
  67.  
  68. #include "NET.H"
  69. #include "FIDO.H"
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <alloc.h>
  73. #include <sys/stat.h>
  74. #include <dos.h>
  75. #include <dir.h>
  76. #include <time.h>
  77.  
  78. /* De-Comment this line if you want NetSEX to print */
  79. /* Ooodles of debug information,  I mean oodles!    */
  80. /*   |      */
  81. /*     |      */
  82. /*  \|/     */
  83. /*#define DEBUG*/
  84.  
  85. /* Structure used to dynamically store the aliases */
  86. struct Alias {
  87.     char in[37], out[37];
  88. };
  89.  
  90.  
  91. /* Structure used to store the log information */
  92. typedef struct {
  93.     char name[40];
  94.     unsigned short sub_type;
  95.     unsigned short host;
  96.     unsigned long in_messages, out_messages;
  97.     unsigned long in_bytes, out_bytes;
  98. } log_entry;
  99.  
  100. /* Structure used to store BBS info for the "From System" Line */
  101. /* On outbound fidonet mail from wwivnet */
  102. struct Stats {
  103.         unsigned int    sysnum;
  104.         char                    phone[13],
  105.                                     name[41];
  106.     };
  107.  
  108. struct date datep;
  109. struct time timep;
  110. short sub_list[50];
  111. unsigned int sub_type, host_system;
  112. long list_len, Log_Count, Ltime;
  113. log_entry *Ldata;
  114. char Log_Update;
  115. FILE *Net, *Sub, *Log;
  116. char *Path;
  117. char *MailDir;
  118. char *Search;
  119. int NextEnt(), ReadLine(), ProcMess(), Parse(), ParseName();
  120. char Number[10];
  121. unsigned short MyNode, LocalNode;
  122. int FidoNet, FidoNode, FidoZone;
  123. char *Buffer;
  124. char *Banner;
  125. char *wwivpath;
  126. char *MBuf;
  127. long MLength;
  128. net_header_rec header;
  129. struct Alias *Alias;
  130. struct Stats *Stats;
  131. int NumAlias;
  132. int LineCount;
  133. void Import(), Export();
  134. char area[40], bd_flag, export_flag;
  135. int Fidoize();
  136. int destNet, destNode;
  137. char *tname;
  138. void Alloc(), Free();
  139. void Load_Log(), Save_Log(), Faddress();
  140. int Point, ToPoint, Zone, FNet, FNode;
  141. unsigned short CNode, WWIVgate;
  142. char NetString[11];
  143.  
  144. void main( argc, argv)
  145. int argc;
  146. char **argv;
  147. {
  148.     long clock1, clock2;
  149.     time(&clock1);
  150.     printf("NetSEX! Version %s Copyright Matt Logan 1989, 1990\n", Version);
  151.     printf("%s\n", ctime(&clock1));
  152.  
  153.     NetString[0] = 0;
  154.     CNode = 0;
  155.  
  156.     Alloc();
  157.     if ( argc == 2 || ( argc == 3 && strcmp(argv[2], "/WF") == 0 )) {
  158.         printf("Running Import then Export\n");
  159.         Load_Log(argv[1]);
  160.         Import(argv);
  161.         Export(argv);
  162.     } else if ( argc == 3 && strcmp(argv[2], "/FW") == 0 ) {
  163.         printf("Running Export then Import\n");
  164.         Load_Log(argv[1]);
  165.         Export(argv);
  166.         Import(argv);
  167.     } else if ( argc == 3 && strcmp(argv[2], "/W") == 0) {
  168.         printf("Running Import only\n");
  169.         Load_Log(argv[1]);
  170.         Import(argv);
  171.     } else if ( argc == 3 && strcmp(argv[2], "/F") == 0) {
  172.         printf("Running Export only\n");
  173.         Load_Log(argv[1]);
  174.         Export(argv);
  175.     } else {
  176.         printf("Usage: %s <DATAPATH> [/FW]\n", argv[0]);
  177.         Free();
  178.         exit(0);
  179.     }
  180.     Save_Log(argv[1]);
  181.     Free();
  182.     time(&clock2);
  183.     printf("Total time: %ld Seconds\n", clock2 - clock1);
  184. }
  185.  
  186.  
  187. /* Function: Alloc()
  188. **
  189. ** Purpose: Allocate all the dynamic variables
  190. **
  191. ** Inputs: NONE.
  192. **
  193. ** Variables effected:
  194. **
  195. **   MBuf:    The message buffer
  196. **   Buffer:  A buffer for general use.
  197. **   Path:    The string used to point to various directories
  198. **   Maildir:
  199. **   Search:
  200. **   wwivpath:
  201. **   tname:
  202. **
  203. ** Return Value: None.
  204. */
  205.  
  206. void Alloc()
  207. {
  208.  
  209.     /* Huge stuff first in case of low memory */
  210.     MBuf =            (char *)malloc(33000);
  211.     Buffer =        (char *)malloc(240);
  212.     Banner =        (char *)malloc(161);
  213.     Path =            (char *)malloc(81);
  214.     MailDir =        (char *)malloc(81);
  215.     Search =        (char *)malloc(81);
  216.     wwivpath =    (char *)malloc(80);
  217.     tname =            (char *)malloc(80);
  218.  
  219.     if (Buffer == NULL || Banner == NULL || wwivpath == NULL ||
  220.             MBuf == NULL || Path == NULL || MailDir == NULL || Search == NULL ||
  221.             tname == NULL) {
  222.             printf("Can't allocate Memory");
  223.             Free();
  224.             exit(-1);
  225.     }
  226. }
  227.  
  228. /* routine to free the memory allocated */
  229. void Free()
  230. {
  231.     free(Buffer);
  232.     free(Banner);
  233.     free(wwivpath);
  234.     free(MBuf);
  235.     free(Path);
  236.     free(MailDir);
  237.     free(Search);
  238.     free(Alias);
  239.     free(tname);
  240. }
  241.  
  242.  
  243. /* This procedure imports messages from FIDO to WWIV */
  244. void Import(argv)
  245. char **argv;
  246. {
  247.  
  248.     long msg_cnt, byte_cnt;
  249.     FILE *fd1, *fd2;
  250.     int x, y, z, no_end;
  251.     long Position;
  252.     unsigned char c;
  253.     unsigned short syst;
  254.     char Subject[80];
  255.     char ToUser[37];
  256.     char Name[80];
  257.     char AName[80];
  258.     char Date[80];
  259.     struct ffblk ffblk;
  260.     int done;
  261.     int MailFlag;
  262.     unsigned char print_flag;
  263.     char *B2;
  264.  
  265.     printf("\n\nImporting...\n");
  266.     strcpy(wwivpath, argv[1]);
  267.  
  268.     export_flag = 0;
  269.     MailDir[0] = 0;
  270.     LocalNode = 0;
  271.     NumAlias = 0;
  272.     LineCount = 0;
  273.     /* Set Banner to null for now */
  274.     Banner[0] = 0;
  275.  
  276.     sprintf(Buffer, "%s/FIDO.NET", wwivpath);
  277.     Net = fopen(Buffer, "rt");
  278.     if ( Net == NULL) {
  279.         printf("Can't Find %s\n", Buffer);
  280.         exit(-1);
  281.     }
  282.  
  283.     x = 0;
  284.     do {
  285.         x++;
  286.         sprintf( Buffer, "%s/P%d.NET", wwivpath, x);
  287.         fd2 = fopen(Buffer, "rb");
  288.         if (fd2 == NULL) {
  289.             fclose(fd2);
  290.             fd2 = fopen(Buffer, "w+b");
  291.             break;
  292.         } else {
  293.             fclose(fd2);
  294.         }
  295.     } while (x < 10);
  296.  
  297.     if (x >= 10) {
  298.         printf("Error,  couldn't open a P*.NET FILE\n");
  299.     }
  300.  
  301.  
  302. while ( NextEnt() == 0) {
  303.  
  304. /* This Section of code finds the FIDO messages and converts them       */
  305. /*    into WWIV messages                        */
  306.     sprintf(Search, "%s/*.MSG", Path);
  307.     msg_cnt = 0; byte_cnt = 0; print_flag = 0;
  308.     done = findfirst(Search, &ffblk, 0);
  309.  
  310.     while (!done) {
  311.         sprintf( Search,"%s/%s", Path, ffblk.ff_name);
  312.         fd1 = fopen( Search, "r+b");
  313.         if ( fd1 == NULL) {
  314.             printf("Error opening file %s\n", Search);
  315.             exit(-1);
  316.         }
  317.  
  318.         /* Find out if this message has already been processed */
  319.         fseek( fd1, 0, 0);
  320.         fread(AName, 36, 1, fd1);
  321.         fread(tname, 36, 1, fd1);
  322.         fseek(fd1, 166,0);
  323.         fread( &destNode, 2, 1, fd1);
  324.         fseek(fd1, 174, 0);
  325.         fread( &destNet, 2, 1, fd1);
  326.  
  327.         /* If it hasn't been processed,  process it! */
  328.         if ( strncmp ( AName, "QMail", 5) != 0 && (ffblk.ff_fsize < 28000)) {
  329.  
  330. /*            strupr(AName);*/
  331.             msg_cnt++;
  332.             gettime(&timep);
  333.             getdate(&datep);
  334.             header.tosys = host_system;
  335.             header.touser = 0;
  336.             header.fromsys = MyNode;
  337.             header.fromuser = 0;
  338.             header.main_type = main_type_pre_post;
  339.             header.minor_type = sub_type;
  340.             header.list_len = list_len;
  341.             header.daten = dostounix(&datep, &timep);
  342.             header.method = 0;
  343.             header.length = 0;
  344.  
  345.             /* This loop reads the header and gets some info from it */
  346.  
  347.             /* This is only used if running mail */
  348.             fseek( fd1, 36, 0);
  349.             fread(ToUser, 36, 1, fd1);
  350.  
  351.             fread(Subject, 72, 1, fd1);
  352.             fread(Date, 20, 1, fd1);
  353.  
  354.             /* Put the proper Fido Net/Node number in the name */
  355.             fseek(fd1, 168, 0);
  356.             fread(&FNode, 2, 1, fd1);
  357.             fseek(fd1, 172, 0);
  358.             fread(&FNet, 2, 1, fd1);
  359.             fseek( fd1, 190, 0);
  360.  
  361.             Zone = FidoZone;
  362.             Point = 0;
  363.             ToPoint = 0;
  364.             WWIVgate = 0;
  365.             header.length = MLength = ProcMess(fd1);
  366.  
  367. /*            printf("fidonet %d\n", (int)FidoNet);
  368.             printf("Fidonode %d\n", (int)FidoNode);
  369.             printf("destnet %d\n", (int)destNet);
  370.             printf("destnode %d\n", (int)destNode);
  371.             printf("FNode %d\n", (int)FNode);
  372.             printf("FNet %d\n", (int)FNet);
  373.             printf(" What? %d\n", (int)~(22 == 2 && 1 == 1));
  374. */
  375.             if ( ((destNet == FidoNet && destNode == FidoNode
  376.                 && ToPoint == 0) || sub_type !=0)
  377.                 && WWIVgate != LocalNode) {
  378.  
  379.  
  380.                 fseek(fd1, 0, 2L);
  381.                 x = ftell(fd1) - sizeof(struct MessageHdr);
  382.                 B2 = malloc( x);
  383.                 if (B2 == 0) {
  384.                     printf("Error, not enough memory\n");
  385.                     exit(-1);
  386.                 }
  387.  
  388.                 fseek(fd1, sizeof(struct MessageHdr), 0L);
  389.                 fread(B2, x, 1, fd1);
  390.                 fseek(fd1, sizeof(struct MessageHdr), 0L);
  391.                 fprintf( fd1, "%cWWIVGATE: %d%c%c",
  392.                     1, LocalNode, 13, 10);
  393.                 fwrite(B2, x, 1, fd1);
  394.                 free(B2);
  395.  
  396.                 if ( sub_type == 0 ) {
  397.                     if (Point == 0 && Zone == FidoZone) {
  398.                         sprintf( Name, "%s OF %d/%d", AName, FNet, FNode);
  399.                     } else if ( Point != 0 && Zone == FidoZone) {
  400.                         sprintf( Name, "%s OF %d/%d.%d", AName, FNet, FNode, Point);
  401.                     } else if (Point == 0 && Zone != FidoZone) {
  402.                         sprintf( Name, "%s OF %d:%d/%d", AName, Zone, FNet, FNode);
  403.                     } else {
  404.                         sprintf( Name, "%s OF %d:%d/%d.%d", AName, Zone, FNet, FNode, Point);
  405.                     }
  406.                 } else {
  407.                         sprintf( Name, "%s", AName);
  408.                 }
  409.  
  410.                 byte_cnt += header.length;
  411.  
  412.                 if (print_flag == 0) {
  413.                     if (sub_type == 0) {
  414.                         printf("Mail:\n");
  415.                     } else {
  416.                         printf("Sub %ld:\n", (long)sub_type);
  417.                     }
  418.                     printf("Date       Time     %-5s %-13.13s %s\n", "Size", "To", "From");
  419.                     printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  420.                     print_flag = 1;
  421.                 }
  422.  
  423.                 printf("%s %5ld %-13.13s %s\n", Date,
  424.                  (long)header.length, ToUser, Name);
  425.  
  426.  
  427.                 header.length += (strlen(Subject) + 1) + ( strlen(Name) + 2) + (strlen(Date) + 2);
  428.  
  429.                 if (sub_type == 0) {
  430.                     MailFlag = ParseName(ToUser);
  431.                 }
  432.  
  433.                 if ( Banner[0] != 0) {
  434.                     header.length += (strlen(Banner));
  435.                 }
  436.  
  437.                 /* Write Header */
  438.                 fwrite(&header, sizeof(header), 1, fd2);
  439.  
  440.                 if (sub_type == 0 && MailFlag == 1) {
  441.                     fwrite( ToUser, strlen(ToUser), 1, fd2);
  442.                     c = 0;
  443.                     fwrite( &c, 1, 1, fd2);
  444.                 }
  445.  
  446.                 /* Write Title, Name and Date */
  447.                 fwrite( Subject, strlen(Subject), 1, fd2);
  448.                 c = 0;
  449.                 fwrite( &c, 1, 1, fd2 );
  450.  
  451.                 fwrite( Name, strlen(Name), 1, fd2);
  452.                 c = 13;
  453.                 fwrite( &c, 1, 1, fd2 );
  454.                 c = 10;
  455.                 fwrite( &c, 1, 1, fd2 );
  456.  
  457.                 fwrite( Date, strlen(Date), 1, fd2);
  458.                 c = 13;
  459.                 fwrite( &c, 1, 1, fd2 );
  460.                 c = 10;
  461.                 fwrite( &c, 1, 1, fd2 );
  462.  
  463.                 /* Write Body of text */
  464.                 fwrite( MBuf, MLength, 1, fd2);
  465.  
  466.  
  467.                 /* Write Banner if there is one */
  468.                 if ( Banner[0] != 0) {
  469.                     fwrite( Banner, strlen(Banner), 1, fd2);
  470.                 }
  471.             }
  472.         }
  473.         fclose(fd1);
  474.         done = findnext(&ffblk);
  475.     }
  476.     if (print_flag != 0) {
  477.         printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  478.         printf("%ld Messages, %ld Bytes\n\n", msg_cnt, byte_cnt);
  479.         for (x = 0; x != Log_Count; x++) {
  480.             if (strcmp( area, Ldata[x].name) == 0) break;
  481.         }
  482.         if ( x == Log_Count ) {
  483.             Log_Count++;
  484.             if (Ldata == NULL) {
  485.                 Ldata = (log_entry *)malloc(sizeof(log_entry));
  486.             } else {
  487.                 Ldata = (log_entry *)realloc( Ldata, Log_Count * sizeof(log_entry));
  488.             }
  489.             for (y = 0; y != 40; y++) {
  490.                 Ldata[x].name[y] = 0;
  491.             }
  492.             strcpy(Ldata[x].name, area);
  493.             Ldata[x].sub_type = sub_type;
  494.             Ldata[x].host = host_system;
  495.             Ldata[x].in_messages = 0;
  496.             Ldata[x].out_messages = 0;
  497.             Ldata[x].in_bytes = 0;
  498.             Ldata[x].out_bytes = 0;
  499.         }
  500.         Ldata[x].in_messages += msg_cnt;
  501.         Ldata[x].in_bytes += byte_cnt;
  502.     }
  503. }
  504. fclose(fd2);
  505. fclose(Net);
  506.  
  507. }
  508.  
  509. /* This procedure exports messages from WWIV to FIDO */
  510. void Export(argv)
  511. char **argv;
  512. {
  513.     long msg_cnt, byte_cnt;
  514.     FILE *fd1, *fd2;
  515.     int x, y, z, no_end;
  516.     long Position;
  517.     unsigned char c;
  518.     unsigned short syst;
  519.     char Subject[80];
  520.     char ToUser[37];
  521.     char Name[80];
  522.     char Date[80];
  523.     struct ffblk ffblk;
  524.     int done;
  525.     int MailFlag;
  526.     unsigned short Attribute;
  527.     int max_msg;
  528.     long fpos1, fpos2;
  529.     unsigned short didit = 65535;
  530.     struct MessageHdr Fmessage;
  531.     net_system_list_rec bbsData;
  532.     unsigned char print_flag;
  533.     int bbsEntry;
  534.  
  535.     export_flag = 1;
  536.     sprintf(&Fmessage.fill[0], "NETSEX");
  537.     printf("Exporting...\n");
  538.     strcpy(wwivpath, argv[1]);
  539.  
  540.     printf("Loading bbsdata - ");
  541.     sprintf( Buffer, "%s/BBSDATA.NET", wwivpath);
  542.     fd1 = fopen( Buffer, "rb");
  543.     if (fd1 == NULL) {
  544.         printf("Can't find %s\n", Buffer);
  545.         exit(-1);
  546.     }
  547.  
  548.     /* Find Length of BBSDATA.NET */
  549.     fseek(fd1, 0, 2);
  550.     Position = ftell(fd1);
  551.     fseek(fd1, 0, 0);
  552.     x = Position / 68;
  553.  
  554.     Stats =    (struct Stats *)malloc( (x + 1) * sizeof(struct Stats));
  555.  
  556.     if (Stats == NULL) {
  557.         printf("Error, can't allocate memory for BBSDATA.NET\n");
  558.         exit(0);
  559.     }
  560.  
  561.     for ( bbsEntry = 0; bbsEntry != x; bbsEntry++) {
  562.         fread(&Stats[bbsEntry], 56, 1, fd1);
  563.         fread(Buffer, 12, 1, fd1);
  564. #ifdef DEBUG
  565.         printf("Read %s\n", Stats[bbsEntry].name);
  566. #endif
  567.     }
  568.     fclose(fd1);
  569.     printf("%d Entrys\n", bbsEntry);
  570.  
  571.     MailDir[0] = 0;
  572.     LocalNode = 0;
  573.     NumAlias = 0;
  574.     LineCount = 0;
  575.     /* Set Banner to null for now */
  576.     Banner[0] = 0;
  577. #ifdef DEBUG
  578.     printf("Initialized variables\n");
  579. #endif
  580.  
  581.     sprintf(Buffer, "%s/FIDO.NET", wwivpath);
  582.     Net = fopen(Buffer, "rt");
  583.     if ( Net == NULL) {
  584.         printf("Can't Find %s\n", Buffer);
  585.         exit(-1);
  586.     }
  587.  
  588.     while ( NextEnt() == 0) {
  589.     /* This Section of code finds the WWIV messages and converts them       */
  590.     /*    into FIDO messages                        */
  591.  
  592.         print_flag = 0;
  593.  
  594.         if (bd_flag == 1) {
  595.             sprintf(Search, "%s/*.MSG", Path);
  596.             max_msg = 0;
  597.  
  598.             done = findfirst(Search, &ffblk, 0);
  599.  
  600.             while (!done) {
  601.                 if (atol(ffblk.ff_name) > max_msg) max_msg = atol(ffblk.ff_name);
  602.                 done = findnext(&ffblk);
  603.             }
  604.  
  605.             if (max_msg == 0) max_msg = 1;
  606.  
  607. #ifdef DEBUG
  608.             printf("Highest message number = %d\n", max_msg);
  609. #endif
  610.  
  611.             msg_cnt = 0; byte_cnt = 0;
  612.  
  613.             sprintf( Buffer, "%s/s%d.NET", wwivpath, MyNode);
  614.             fd1 = fopen( Buffer, "r+b");
  615.             if (fd1 != NULL) {
  616.                 /* read Header */
  617.                 while (fread( &header, sizeof(header), 1, fd1) == 1){
  618.                     fpos1 = ftell(fd1) - 16;
  619.                     /* Check to see if the message needs to be processed */
  620.                     if (
  621.                     /* This part checks for posts to the sub currently being processed */
  622.                     (header.main_type == main_type_post && header.minor_type == sub_type)
  623.                     /* This part checks for Email */
  624.                     || ( header.main_type == main_type_email_name && sub_type == 0)
  625.                          ) {
  626.                         fpos2 = ftell(fd1);
  627.                         fseek ( fd1, fpos1, 0);
  628.                         /* Mark this message as processed */
  629.                         fwrite( &didit, sizeof(didit), 1, fd1);
  630.                         fseek( fd1, fpos2, 0);
  631.                         if (print_flag == 0) {
  632.                             if (sub_type == 0) {
  633.                                 printf("Mail:\n");
  634.                             } else {
  635.                                 printf("Sub %ld:\n", (long)sub_type);
  636.                             }
  637.                             printf("Date       Time     %-5s %-13.13s %s\n", "Size", "From", "To");
  638.                             printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  639.                             print_flag = 1;
  640.                         }
  641.  
  642.                         Zone = FidoZone;
  643.  
  644.                         x = Fidoize(fd1, &Fmessage);
  645.                         if (Zone != FidoZone) {
  646.                             Fmessage.destNet = FidoZone;
  647.                             Fmessage.destNode = Zone;
  648.                         }
  649.                         max_msg++; msg_cnt++;
  650.                         byte_cnt += x;
  651.                         sprintf( Buffer, "%s\\%d.MSG", Path, max_msg);
  652.                         fd2 = fopen( Buffer, "wb");
  653.                         if ( fd2 == NULL) {
  654.                             printf("Can't write to %s\n", Buffer);
  655.                             exit(-1);
  656.                         }
  657.  
  658.                         /* Write out the message */
  659.                         fwrite( &Fmessage, sizeof(Fmessage), 1, fd2);
  660.  
  661.  
  662.                         if (Point != 0) {
  663.                             fprintf( fd2, "%cTOPT %d%c%c", 1, Point, 13, 10);
  664.                         }
  665.  
  666.  
  667.                         if (Zone != FidoZone) {
  668.                             fprintf( fd2,"%cINTL %d:%d/%d %d:%d/%d%c%c", 1,
  669.                              Zone, FNet, FNode, FidoZone, FidoNet, FidoNode, 13, 10);
  670.                         }
  671.  
  672.                         fprintf(fd2,"%cWWIVGATE: %d %s N%d U%d%c%c",1,LocalNode,
  673.                             NetString,(int)header.fromsys,(int)header.fromuser,13,10);
  674.  
  675.                         if (strncmp(Fmessage.toUserName, "UUCP", 4) != 0 ) {
  676.                             z = 0;
  677.                             while ( Stats[z].sysnum != header.fromsys) {
  678.                                 z++;
  679.                                 if ( z > bbsEntry ) {
  680.                                     z = -1;
  681.                                     break;
  682.                                 }
  683.                             }
  684.                             if (z != -1) {
  685.                                 if (NetString[0] != 0) fprintf( fd2, "%s: ", NetString);
  686.                                 fprintf( fd2, "%s [%s] - Node %d%c%c%c%c",
  687.                                     Stats[z].name, Stats[z].phone, (int)header.fromsys, 13, 10, 13, 10);
  688.                             }
  689.                         }
  690.  
  691.                         fwrite(MBuf, x, 1, fd2);
  692.                         c = 13;
  693.                         fwrite(&c, 1, 1, fd2);
  694.                         c = 10;
  695.                         fwrite(&c, 1, 1, fd2);
  696.                         c = 0;
  697.                         fwrite( &c, 1, 1, fd2);
  698.                         fclose(fd2);
  699.                     } else {
  700.                         fseek( fd1, header.length, 1);
  701.                     }
  702.                 }
  703.                 fclose(fd1);
  704.             }
  705.         }
  706.         if (print_flag != 0) {
  707.             printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  708.             printf("%ld Messages, %ld Bytes\n\n", msg_cnt, byte_cnt);
  709.             for (x = 0; x != Log_Count; x++) {
  710.                 if (strcmp( area, Ldata[x].name) == 0) break;
  711.             }
  712.             if ( x == Log_Count ) {
  713.                 Log_Count++;
  714.                 if (Ldata == NULL) {
  715.                     Ldata = (log_entry *)malloc(sizeof(log_entry));
  716.                 } else {
  717.                     Ldata = (log_entry *)realloc( Ldata, Log_Count * sizeof(log_entry));
  718.                 }
  719.                 for (y = 0; y != 40; y++) {
  720.                     Ldata[x].name[y] = 0;
  721.                 }
  722.                 strcpy(Ldata[x].name, area);
  723.                 Ldata[x].sub_type = sub_type;
  724.                 Ldata[x].host     = host_system;
  725.                 Ldata[x].in_messages = 0;
  726.                 Ldata[x].out_messages = 0;
  727.                 Ldata[x].in_bytes = 0;
  728.                 Ldata[x].out_bytes = 0;
  729.             }
  730.             Ldata[x].out_messages += msg_cnt;
  731.             Ldata[x].out_bytes += byte_cnt;
  732.  
  733.         }
  734.     }
  735.     sprintf( Buffer, "%s/s%d.NET", wwivpath, (int)MyNode);
  736.     unlink( Buffer);
  737.     free(Stats);
  738.     return;
  739. }
  740.  
  741. /* This function returns the info to process the next sub by reading the */
  742. /* FIDO.NET file */
  743. int NextEnt()
  744. {
  745.     char Line[160], Line2[160];
  746.     int x, y, z, done = 0;
  747.     int tot;
  748.     int Res;
  749.  
  750. #ifdef DEBUG
  751.     printf("Entering NextEnt()\n");
  752. #endif
  753.  
  754.     while (done == 0) {
  755.         LineCount++;
  756.         if (fgets(Line, 160, Net) == NULL) {
  757.             /* Bail out if no more to process */
  758.             return(-1);
  759.  
  760.         }
  761.         /* Copy in case we need the EXACT line */
  762.         strcpy(Line2, Line);
  763.         strupr(Line);
  764.  
  765.         if (strncmp("SUB: ", Line, 5) == 0) {
  766. #ifdef DEBUG
  767.             printf("%s", Line);
  768. #endif
  769.             x = 3;
  770.  
  771.             /* Find sub type */
  772.             x = Parse( x, Line);
  773.             if (x == -1) return(-1);
  774.             sub_type = atol(&Line[x]);
  775. #ifdef DEBUG
  776.             printf("Sub type %d\n", sub_type);
  777. #endif
  778.  
  779.             /* Find host system name */
  780.             x = Parse( x, Line);
  781.             if (x == -1) return(-1);
  782.             host_system = atol(&Line[x]);
  783. #ifdef DEBUG
  784.             printf("Host system %ld\n", (long)host_system);
  785. #endif
  786.             /* Get path name for *.msg files */
  787.             x = Parse( x, Line);
  788.             if (x == -1) return(-1);
  789.             y = Parse( x, Line);
  790.             if (y == -1) return(-1);
  791.             strncpy(Path, &Line[x], y - x);
  792.             z = y - x - 1;
  793.             while( Path[ z] == ' ') {
  794.                 Path[z] = 0;
  795.                 z--;
  796.             }
  797. #ifdef DEBUG
  798.             printf("Search path: *%s*\n", Path);
  799. #endif
  800.  
  801.             /* Get name of area for fidonet */
  802.             x = Parse( y, Line);
  803.             if ( x == -1) return(-1);
  804.             strncpy(area, &Line[y], x - y);
  805.             z = x - y - 1;
  806.             while ( area[z] == ' ') {
  807.                 area[z] = 0;
  808.                 z--;
  809.             }
  810. #ifdef DEBUG
  811.             printf("Fidonet area: %s\n", area);
  812. #endif
  813.  
  814.             /* And finally,  find out if this sub is bi-directional */
  815.             if ( Line[x] == 'Y') bd_flag = 1;
  816.             else bd_flag = 0;
  817. #ifdef DEBUG
  818.             if ( bd_flag == 1) printf("Sub is bi-directional\n");
  819. #endif
  820.             if ((export_flag == 0) || (bd_flag == 1) )    done = 1;
  821.             Line[0] = ';';
  822.         } else if (((strncmp("IN_ALIAS: ", Line, 7) == 0) && (export_flag == 0)) ||
  823.                  ((strncmp("OUT_ALIAS: ", Line, 8) == 0) && (export_flag == 1)) ){
  824. /*            if (NumAlias > MAX_ALIASES) {
  825.                 printf("Error, too many aliases. Line %d\n", LineCount);
  826.                 exit(0);
  827.             }
  828. */
  829.             x = 9 + export_flag;
  830.             while( Line[x] != '=' && x < strlen(Line)) {
  831.                 x++;
  832.             }
  833.  
  834.             if ( x >= strlen(Line)) {
  835.                 printf("Error in ALIAS Command Line%d\n%s\n", LineCount, Line);
  836.                 exit(0);
  837.             }
  838.  
  839.             Line[x] = 0;
  840.  
  841.             Line2[strlen(Line2) - 1] = 0;
  842.  
  843.             y = 0;
  844.             while( (strcmp(Alias[y].in, &Line[10 + export_flag]) != 0)
  845.                 && (y <NumAlias)) {
  846.                 y++;
  847.             }
  848.  
  849.             if (y == NumAlias) {
  850. #ifdef DEBUG
  851.                 printf("New Alias\n");
  852. #endif
  853.                 if ( y == 0) {
  854.                     Alias = (struct Alias *)malloc( sizeof(struct Alias));
  855.                 } else {
  856.                     Alias = (struct Alias *)realloc( Alias,
  857.                         (NumAlias + 1)* sizeof(struct Alias));
  858.                 }
  859.                 strcpy (Alias[NumAlias].in, &Line[10 + export_flag]);
  860.                 strcpy (Alias[NumAlias].out, &Line2[x+1]);
  861. #ifdef DEBUG
  862.                 printf("Aliasing %s to %s\n", Alias[NumAlias].in, Alias[NumAlias].out);
  863. #endif
  864.                 NumAlias++;
  865.             } else if (strlen(&Line2[x+1]) == 0) {
  866. #ifdef DEBUG
  867.                 printf("De-Aliasing %s\n", Alias[y].in);
  868. #endif
  869.                 for ( z = y; z!= NumAlias -1; z++) {
  870.                     strcpy(Alias[z].in, Alias[z+1].in);
  871.                     strcpy(Alias[z].out, Alias[z+1].out);
  872.                 }
  873.                 NumAlias--;
  874.                 Alias = (struct Alias *)realloc( Alias, NumAlias * sizeof(struct Alias));
  875.             } else {
  876. #ifdef DEBUG
  877.                 printf("Changing Alias of %s to %s\n", Alias[y].in, &Line2[x+1]);
  878. #endif
  879.                 strcpy(Alias[y].out, &Line2[x+1]);
  880.             }
  881.  
  882.             Line[0] = ';';
  883.         } else if (strncmp("IN_ALIAS: ", Line, 7) == 0 || strncmp("OUT_ALIAS", Line, 8) == 0) {
  884.             Line[0] = ';';
  885.         } else if (strncmp("BANNER: ", Line, 8) == 0) {
  886.             /* Limit Length to 80 chars */
  887.             tot = 0;
  888.             for ( x = 8; x < strlen(Line2); x++) {
  889.                 /* Check for ^P */
  890.                 if (Line2[x] == 3) {
  891.                     x++;
  892.                 } else {
  893.                     tot++;
  894.                 }
  895.                 if ( tot == 80) {
  896.                     Line2[x+1] = 0;
  897.                     break;
  898.                 }
  899.             }
  900.             strcpy (Banner, &Line2[8]);
  901.             Line[0] = ';';
  902. #ifdef DEBUG
  903.             printf("BANNER: %s\n",  Banner);
  904. #endif
  905.         } else if (strncmp("NODE: ", Line, 6) == 0) {
  906.             MyNode = atol(&Line[6]);
  907.             Line[0] = ';';
  908.         } else if (strncmp("FNODE: ", Line, 7) == 0) {
  909.             FidoNode = (unsigned short)atol(&Line[7]);
  910.             Line[0] = ';';
  911.         } else if (strncmp("FNET: ", Line, 6) == 0) {
  912.             FidoNet = (unsigned short)atol(&Line[6]);
  913.             Line[0] = ';';
  914.         } else if (strncmp("FZONE: ", Line, 7) == 0) {
  915.             FidoZone = (unsigned short)atol(&Line[7]);
  916.             Line[0] = ';';
  917.         } else if (strncmp("LOCAL: ", Line, 7) == 0) {
  918.             LocalNode = atol(&Line[7]);
  919.             Line[0] = ';';
  920.         } else if (strncmp("MAIL: ", Line, 6) == 0) {
  921.             strcpy(MailDir, &Line[6]);
  922.             MailDir[strlen(MailDir) - 1] = 0;
  923.             /* Process the mail*/
  924.             strcpy(area, "Mail");
  925.             strcpy(Path,MailDir);
  926.             MailDir[0] = 0;
  927.             sub_type = 0;
  928.             bd_flag = 1;
  929. #ifdef DEBUG
  930.             printf("Processing Mail\n");
  931. #endif
  932.             done = 1;
  933.             Line[0] = ';';
  934.         } else if ( strncmp("COORDINATOR: ", Line, 13) == 0) {
  935.             CNode = atol(&Line[13]);
  936.             Line[0] = ';';
  937.         } else if ( strncmp("NETWORK: ", Line, 9) == 0 ){
  938.                 strcpy( NetString, &Line2[9]);
  939.                 NetString[strlen(NetString)- 1] = 0;
  940.                 Line[0] = ';';
  941.         } else if ( Line[0] != ';' && Line[0] != 10) {
  942.             printf("Error in FIDO.NET file Line %d\n", LineCount);
  943.             return(-1);
  944.         }
  945.     }
  946.     return(0);
  947. }
  948.  
  949. int ReadLine( Buffer, fd)
  950. unsigned char *Buffer;
  951. FILE *fd;
  952. {
  953.     int x = 0;
  954.  
  955.     while( fread(&Buffer[x], 1, 1, fd) == 1) {
  956.         if ( Buffer[x] == 10 || Buffer[x] == 141) {
  957.             ;
  958.         } else if (Buffer[x] == 13) {
  959.             Buffer[x + 1] = 0;
  960.             return(0);
  961.         } else {
  962.             x++;
  963.             if ( x > 718) {
  964.                 Buffer[x] = 0;
  965.                 return(x);
  966.             }
  967.         }
  968.  
  969.     }
  970.     Buffer[x] = 0;
  971.     return(-1);
  972. }
  973.  
  974. /* Process the message, stripping control info */
  975. /* for messages coming into WWIVnet from FIDONET */
  976. int ProcMess(fd)
  977. FILE *fd;
  978. {
  979.     int x, y, z;
  980.     char Buffer[720];
  981.     y = 0;
  982.  
  983.     /* Add the To: line from the fido message to the message text */
  984.     if (sub_type != 0) {
  985.         sprintf( MBuf, "To: %s\r\n\r\n", tname);
  986.         y = strlen(MBuf);
  987.     }
  988.     do {
  989.         x = ReadLine(Buffer, fd);
  990. /*        if ( Buffer[0] == 1 && strncmp("FMPT ", &Buffer[1], 5) == 0) {
  991.             Point = atoi(&Buffer[6]);
  992.         }
  993. */
  994.         /* Ignore ^A Lines and AREA: and SEEN-BY: and EID: */
  995.         if (Buffer[0] != 1
  996.             && strncmp("AREA:", Buffer, 5) != 0
  997.             && strncmp("SEEN-BY:", Buffer, 8) != 0
  998.           && strncmp("EID:", Buffer, 4) != 0
  999.             && strncmp("TOPT ", Buffer, 4) != 0
  1000.             && strncmp("FMPT:", Buffer, 4) != 0
  1001.             && !(Buffer[0] == 13 && y == 0) ){
  1002.  
  1003.             for ( z = 0; z != strlen(Buffer); z++ ) {
  1004.                 if ( Buffer[z] == 13) {
  1005.                     MBuf[y] = 13;
  1006.                     y++;
  1007.                     MBuf[y] = 10;
  1008.                     y++;
  1009.                 } else {
  1010.                     MBuf[y] = Buffer[z];
  1011.                     y++;
  1012.                 }
  1013.             }
  1014.         } else if ( Buffer[0] == 1 && strncmp("FMPT ", &Buffer[1], 5) == 0) {
  1015.             Point = atoi(&Buffer[6]);
  1016.         } else if ( Buffer[0] == 1 && strncmp("TOPT ", &Buffer[1], 5) == 0) {
  1017.             ToPoint = atoi(&Buffer[6]);
  1018.         } else if ( Buffer[0] == 1 && strncmp("INTL ", &Buffer[1], 5) == 0) {
  1019.             z = 6;
  1020.             while (Buffer[z] != ' ') {
  1021.                 z++;
  1022.             }
  1023.             Buffer[z] = 0;
  1024.             Faddress(&Buffer[5]);
  1025.             destNode = FNode;
  1026.             destNet  = FNet;
  1027.             Faddress(&Buffer[z+1]);
  1028.         } else if ( Buffer[0] == 1 && strncmp("WWIVGATE: ", &Buffer[1], 10) == 0) {
  1029.             WWIVgate = atoi(&Buffer[11]);
  1030.             if (WWIVgate == LocalNode) {
  1031.                 return(0);
  1032.             }
  1033.         }
  1034.     } while ( x >= 0);
  1035.     return(y);
  1036.  
  1037. }
  1038.  
  1039. int ParseName(Name)
  1040. char *Name;
  1041. {
  1042.     int Number, Node, x, y;
  1043.     char temp[40];
  1044.  
  1045.     strupr(Name);
  1046.     for (x = 0; x!= NumAlias; x++) {
  1047.         if (strcmp(Name, Alias[x].in) == 0) {
  1048. #ifdef DEBUG
  1049.     printf("%s Aliased to %s.", Name, Alias[x].out);
  1050. #endif
  1051.             strcpy(Name, Alias[x].out);
  1052.             strupr(Name);
  1053.             break;
  1054.         }
  1055.     }
  1056.  
  1057.     if ( Name[0] == 'N') {
  1058.         Node = atol(&Name[1]);
  1059.     } else {
  1060.         Node = 0;
  1061.     }
  1062.  
  1063. #ifdef DEBUG
  1064.     printf(" To: %s\n Node = %d.", Name, Node);
  1065. #endif
  1066.     y = strlen(Name);
  1067.     x = 0;
  1068.     while( (x < y) && (Name[x] != ' ') ) {
  1069.         x++;
  1070.     }
  1071.  
  1072.     /* No Node Number ?*/
  1073.     /* Must be local mail */
  1074.     if ( x >= y || Node <= 0) {
  1075. #ifdef DEBUG
  1076.         printf(" Mail to %s\n", Name);
  1077. #endif
  1078.         header.length += strlen(Name) + 1;
  1079.         header.main_type = main_type_email_name;
  1080.         header.list_len = 0;
  1081.         header.tosys = LocalNode;
  1082.         return(1);
  1083.     }
  1084.  
  1085.     if (Name[x+1] == 'U') {
  1086.         Number = atol(&Name[x+2]);
  1087.     } else {
  1088.         Number = 0;
  1089.     }
  1090.  
  1091. #ifdef DEBUG
  1092.     printf("Number = %d\n", Number);
  1093. #endif
  1094.     /* Mail sent to User Number at Node Number*/
  1095.     if (Number != 0) {
  1096. #ifdef DEBUG
  1097.         printf(" Mail to user # %d @ %d.\n", Number, Node);
  1098. #endif
  1099.         header.main_type = main_type_email;
  1100.         header.list_len = 0;
  1101.         header.tosys = Node;
  1102.         header.touser = Number;
  1103.         return(2);
  1104.     }
  1105.  
  1106.     /* Name E-Mail Bound for another system */
  1107.     if ( (Number == 0) && (Node != 0)) {
  1108.         strcpy(temp, &Name[x+1]);
  1109.         strcpy(Name, temp);
  1110. #ifdef DEBUG
  1111.         printf(" Mail to %s@%d.\n", Name, Node);
  1112. #endif
  1113.         header.length += strlen(Name) + 1;
  1114.         header.main_type = main_type_email_name;
  1115.         header.list_len = 0;
  1116.         header.tosys = 5317;
  1117.         return(1);
  1118.     }
  1119.     return(-1);
  1120. }
  1121.  
  1122. int Parse(opos, buffer)
  1123. int opos;
  1124. char *buffer;
  1125. {
  1126.     int pos;
  1127.     pos = opos;
  1128.  
  1129.     do {
  1130.         pos++;
  1131.         if ( pos >= strlen(buffer) ) {
  1132.             printf ("Error in FIDO.NET file Line %d\n", LineCount);
  1133.             return(-1);
  1134.         }
  1135.     } while ( buffer[pos] != ' ');
  1136.  
  1137.     while (buffer[pos] == ' ') {
  1138.         pos++;
  1139.         if ( pos >= strlen(buffer) ) {
  1140.             printf ("Error in FIDO.NET file Line %d\n", LineCount);
  1141.             return(-1);
  1142.         }
  1143.     }
  1144.     return(pos);
  1145. }
  1146.  
  1147.  
  1148. /* Process messages coming from WWIVnet to Fidonet */
  1149.  
  1150. int Fidoize(file, Fheader)
  1151. FILE *file;
  1152. struct MessageHdr *Fheader;
  1153. {
  1154.     int length,  x, mlength, y, z, x1;
  1155.     char alias_flag = 0;
  1156.     struct tm *tm;
  1157.     unsigned char c;
  1158.     int Line, LineMkr;
  1159.     char *Mon[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  1160.                     "Oct", "Nov", "Dec" };
  1161.  
  1162.     length = 0;
  1163.     mlength = 0;
  1164.  
  1165.     if (sub_type == 0) {
  1166.         /* Read in the TO field */
  1167.         x = 0;
  1168.         do {
  1169.             if (fread( &Fheader->toUserName[x], 1, 1, file) != 1) {
  1170.                 printf("Error in s%ld file\n", MyNode);
  1171.                 exit(-1);
  1172.             }
  1173.             x++; length++;
  1174.         } while (Fheader->toUserName[x-1] != 0);
  1175.         strupr(Fheader->toUserName);
  1176.         for (; x != 36; x++) {
  1177.             Fheader->toUserName[x] = 0;
  1178.         }
  1179.     } else {
  1180.         strcpy(Fheader->toUserName, "All");
  1181.         for ( x = 3; x != 36; x++) {
  1182.             Fheader->toUserName[x] = 0;
  1183.         }
  1184.     }
  1185.  
  1186.     /* Read in the subject field */
  1187.     x = 0;
  1188.     do {
  1189.         if (fread( &Fheader->subject[x], 1, 1, file) != 1) {
  1190.             printf("Error in s%ld file\n", MyNode);
  1191.             exit(-1);
  1192.         }
  1193.         x++; length++;
  1194.     } while ( (Fheader->subject[x-1] != 0) && ( x < 70));
  1195.     for ( x = x; x != 72; x++) {
  1196.         Fheader->subject[x] = 0;
  1197.     }
  1198.  
  1199.     /* Read in the FROM field */
  1200.     x = 0;
  1201.     do {
  1202.         if (fread( &tname[x], 1, 1, file) != 1) {
  1203.             printf("Error in s%ld file\n", MyNode);
  1204.             exit(-1);
  1205.         }
  1206.         x++; length++;
  1207.     } while (tname[x-1] != 13);
  1208.     tname[x-1] = 0;
  1209.     fread(&c , 1, 1, file);
  1210.     length++;
  1211.     while(tname[x] != '#') {
  1212.         x--;
  1213.     }
  1214.     tname[x] = 0;
  1215.     sprintf(Fheader->fromUserName, "N%d U%d", (int)header.fromsys, (int)header.fromuser);
  1216.  
  1217.     for(x = 0; x != NumAlias; x++) {
  1218.         if (strcmp(Fheader->fromUserName, Alias[x].in) == 0) {
  1219.             strcpy(Fheader->fromUserName, Alias[x].out);
  1220.             alias_flag = 1;
  1221.             break;
  1222.         }
  1223.     }
  1224.  
  1225.     for( x = strlen(Fheader->fromUserName); x != 36; x++) {
  1226.         Fheader->fromUserName[x] = 0;
  1227.     }
  1228.  
  1229.  
  1230.  
  1231.     /* Read in the DATE field */
  1232.     x = 0;
  1233.     do {
  1234.         if (fread( &Fheader->dateTime[0], 1, 1, file) != 1) {
  1235.             printf("Error in s%ld file\n", MyNode);
  1236.             exit(-1);
  1237.         }
  1238.         x++; length++;
  1239.     } while (Fheader->dateTime[0] != 13);
  1240.     Fheader->dateTime[x-1] = 0;
  1241.     fread(&c , 1, 1, file);
  1242.     length++;
  1243.     tm = localtime(&(long)header.daten);
  1244.     sprintf(Fheader->dateTime,"%02d %s %02d  %02d:%02d:%02d", tm->tm_mday, Mon[tm->tm_mon],
  1245.             tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
  1246.  
  1247.     /* Read in the text */
  1248.     x = 0;
  1249.     /* If Not going to uucp,  put in another from field */
  1250. #ifdef DEBUG
  1251.     printf("to: %s\n", Fheader->toUserName);
  1252. #endif
  1253.     if (strncmp("UUCP", Fheader->toUserName, 4) != 0 && (alias_flag == 0)) {
  1254.         sprintf( MBuf, "Name: %s\r\n\r\n", tname);
  1255.         x += strlen(MBuf);
  1256.     }
  1257.     LineMkr = x;
  1258.     do {
  1259.         if (fread( &c, 1, 1, file) != 1) {
  1260.             printf("Error in file\n");
  1261.             exit(0);
  1262.         }
  1263.         if ( c == 13 || ( c < 128 && c >31)) {
  1264.             MBuf[x] = c;
  1265.             length++; x++;
  1266.         } else if ( c > 127) {
  1267.             MBuf[x] = ' ';
  1268.             length++; x++;
  1269.             /* WWIV Color Code */
  1270.         } else if ( c == 3) {
  1271.             fread( &c, 1, 1, file);
  1272.             length += 2;
  1273.             /* Word Wrap */
  1274.         } else if ( c == 1) {
  1275.             fread( &c, 1, 1, file);
  1276.             fread( &c, 1, 1, file);
  1277.             length += 3;
  1278.             MBuf[x] = 10;
  1279.             x++;
  1280.         } else {
  1281.             length++;
  1282.         }
  1283.     } while (length < header.length);
  1284.     MBuf[x] = 0;
  1285.  
  1286. #ifdef DEBUG
  1287.     printf("----- Text Follows -----\n%s\n", MBuf);
  1288. #endif
  1289.     mlength += x;
  1290.     Fheader->timesRead = 0;
  1291.     Fheader->destNode = 0;
  1292.     Fheader->origNode = FidoNode;
  1293.     Fheader->cost     = 0; /* This used to mark the message as processed */
  1294.     Fheader->origNet  = FidoNet;
  1295.     Fheader->destNet  = 0;
  1296.     Fheader->replyTo  = 0;
  1297.     Fheader->Attribute = (unsigned short)0;
  1298.     if (sub_type == 0) {
  1299.         Fheader->Attribute = (unsigned short)129 ; /* Private and Kill/Sent bits */
  1300.         x = strlen(Fheader->toUserName);
  1301.         while (Fheader->toUserName[x] != 'O') {
  1302.             x--;
  1303.         }
  1304.         x--;
  1305.         if (strncmp(" OF ", &Fheader->toUserName[x] , 4) == 0) {
  1306.             Fheader->toUserName[x] = 0;
  1307.             Faddress(&Fheader->toUserName[x+4]);
  1308.             Fheader->destNode = FNode;
  1309.             Fheader->destNet = FNet;
  1310. #ifdef DEBUG
  1311.             printf("Headed for %d/%d\n", Fheader->destNet, Fheader->destNode);
  1312. #endif
  1313.         }
  1314.     }
  1315.  
  1316.     /* Check to see if the message has a RE: */
  1317.     if ( strncmp("RE:", &MBuf[LineMkr], 3) == 0 ) {
  1318. #ifdef DEBUG
  1319.         printf("Found a RE:\n");
  1320. #endif
  1321.         /* Find where the "RE:" line ends and block move... */
  1322.         x = LineMkr;
  1323.         while( MBuf[x] != 13) {
  1324.             x++;
  1325.         }
  1326.         /* Add the RE: into the subject line */
  1327.         if ( (strlen(Fheader->subject) + 3 + x - LineMkr ) < 71) {
  1328.             strcat(Fheader->subject, " (");
  1329.             strncat(Fheader->subject, &MBuf[LineMkr], x - LineMkr);
  1330.             strcat(Fheader->subject, ")");
  1331. #ifdef DEBUG
  1332.             printf("Added RE: to the subject Line\n");
  1333. #endif
  1334.         }
  1335.         /* I am assuming that the standard RE: line ends in two <CR> */
  1336.         x += 2;
  1337.         for (length = x; length != mlength; length++) {
  1338.             MBuf[length - ( x - LineMkr)] = MBuf[length];
  1339.         }
  1340.         mlength -= (x - LineMkr);
  1341.     }
  1342.  
  1343.     x = LineMkr;
  1344.     /* Check to see if the message has a To: */
  1345.     if ( sub_type != 0 && strncmp("To:", &MBuf[LineMkr], 3) == 0 ) {
  1346. #ifdef DEBUG
  1347.         printf("Found a To:\n");
  1348. #endif
  1349.         /* Find where the "To:" line ends and block move... */
  1350.         x = LineMkr;
  1351.         while( MBuf[x] != 13) {
  1352.             x++;
  1353.         }
  1354.         /* Add the To: into the to line */
  1355.         if ( (3 + x - LineMkr ) < 35) {
  1356.             strncpy(Fheader->toUserName, &MBuf[LineMkr + 4], x - LineMkr - 4);
  1357. #ifdef DEBUG
  1358.             printf("Change ToUser Field\n");
  1359. #endif
  1360.         }
  1361.         /* I am assuming that the standard To: line ends in two <CR> */
  1362.         x += 1;
  1363.         for (length = x; length != mlength; length++) {
  1364.             MBuf[length - ( x - LineMkr)] = MBuf[length];
  1365.         }
  1366.         mlength -= (x - LineMkr);
  1367.     }
  1368.  
  1369.     /* Going to FIDONET - alternate adressing mode */
  1370.     if (sub_type == 0 && strncmp("FIDONET", Fheader->toUserName, 7) == 0
  1371.             && strncmp("To:", &MBuf[LineMkr], 3) == 0 ) {
  1372.  
  1373. #ifdef DEBUG
  1374.         printf("Found a FIDONET To:\n");
  1375. #endif
  1376.         /* Find where the "To:" line ends and block move... */
  1377.         x = LineMkr;
  1378.         while( MBuf[x] != 13) {
  1379.             x++;
  1380.         }
  1381.         /* Add the To: into the to line */
  1382.         if ( (3 + x - LineMkr ) < 35) {
  1383.             strncpy(Fheader->toUserName, &MBuf[LineMkr + 4], x - LineMkr - 4);
  1384. #ifdef DEBUG
  1385.             printf("Added To: to the subject Line\n");
  1386. #endif
  1387.         }
  1388.         /* I am assuming that the standard To: line ends in two <CR> */
  1389.         x += 1;
  1390.         for (length = x; length != mlength; length++) {
  1391.             MBuf[length - ( x - LineMkr)] = MBuf[length];
  1392.         }
  1393.         mlength -= (x - LineMkr);
  1394.  
  1395.         x1 = strlen(Fheader->toUserName) - 1;
  1396.  
  1397.         while ( x1 > 0 && Fheader->toUserName[x1] != 'O') {
  1398.             x1--;
  1399.         }
  1400.         x1--;
  1401.  
  1402.         if (strncmp(" OF ", &Fheader->toUserName[x1] , 4) == 0) {
  1403.             Fheader->toUserName[x1] = 0;
  1404.             Faddress(&Fheader->toUserName[x1+4]);
  1405.             Fheader->destNode = FNode;
  1406.             Fheader->destNet = FNet;
  1407. #ifdef DEBUG
  1408.             printf("Headed for %d/%d\n", Fheader->destNet, Fheader->destNode);
  1409. #endif
  1410.         }
  1411.  
  1412.     }
  1413.  
  1414.  
  1415.  
  1416.     Fheader->nextReply = 0;
  1417.  
  1418.     printf("%s %5ld %-13.13s %s", Fheader->dateTime,
  1419.              (long)mlength, Fheader->fromUserName, Fheader->toUserName,
  1420.              (long)Fheader->destNet, (long)Fheader->destNode);
  1421.  
  1422.     if (sub_type == 0) {
  1423.         printf(" of %ld/%ld\n", (long)Fheader->destNet, (long)Fheader->destNode);
  1424.     } else {
  1425.         printf("\n");
  1426.     }
  1427.  
  1428. #ifdef DEBUG
  1429.     printf("To: %s ", Fheader->toUserName);
  1430.     printf("From: %s ", Fheader->fromUserName);
  1431.     printf("Subject: %s ", Fheader->subject);
  1432.     printf("Date: %s\n", Fheader->dateTime);
  1433. #endif
  1434.     return(mlength);
  1435. }
  1436.  
  1437. void Load_Log(path)
  1438. char *path;
  1439. {
  1440.     char bb[80];
  1441.     long Ptime;
  1442.     struct stat buff;
  1443.     struct tm *tm2;
  1444.     int x;
  1445.  
  1446.     sprintf( bb, "%s/NETSEX.DAT", path);
  1447.     Log_Update = 0;
  1448.     time(&Ptime);
  1449.  
  1450.     if ( stat (bb, &buff) == 0) {
  1451.         Log = fopen( bb, "rb");
  1452.         fread( &Ltime, sizeof(Ltime), 1, Log);
  1453.         Ldata = (log_entry *)malloc( buff.st_size - sizeof(Ltime));
  1454.         fread( Ldata, buff.st_size - sizeof(Ltime), 1, Log);
  1455.         fclose(Log);
  1456.         Log_Count = (buff.st_size - sizeof(Ltime))/ sizeof(log_entry);
  1457.  
  1458.         tm2 = localtime(&Ptime);
  1459.         /* Send out Log update if it is Sunday, or it has been more */
  1460.         /* Than a week since the last update */
  1461.         if ((Ptime - Ltime >= 604800) ||
  1462.                 ( (tm2->tm_wday == 0) && (Ptime - Ltime) > 86400) ) {
  1463.                     Log_Update = 1;
  1464.         }
  1465.     } else {
  1466.         Log_Count = 0;
  1467.         Ltime = Ptime;
  1468.         Ldata = NULL;
  1469.     }
  1470. }
  1471.  
  1472.  
  1473. /* Function: Save_Log()
  1474. **
  1475. ** Purpose:  To save the NetSEX log information to disk and, if
  1476. **           the proper time interval has passed, to mail a copy
  1477. **           of the log to the NetSEX COORDINATOR.
  1478. **
  1479. ** Inputs: path - a string containing the path to the WWIV data
  1480. **                directory.
  1481. **
  1482. ** Also Uses:
  1483. **
  1484. **   Log_Update
  1485. **   CNode
  1486. **   MyNode
  1487. **   Ltime
  1488. **   LocalNode
  1489. **   FidoNode
  1490. **   FidoNet
  1491. **   FidoZone
  1492. **   Version
  1493. **   Log_Count
  1494. **   Net
  1495. **   Ltime
  1496. **   Ldata
  1497. **
  1498. **
  1499. ** Return Value:
  1500. **
  1501. **   NONE.
  1502. **
  1503. */
  1504.  
  1505.  
  1506. void Save_Log(path)
  1507. char *path;
  1508.  
  1509. {
  1510.     struct stat buff;
  1511.     char c, bb[80];
  1512.     int x, y, z;
  1513.  
  1514.     /* Send Log file off to the Coordinator... */
  1515.     if (Log_Update == 1 && CNode != 0) {
  1516.         printf("Sending out Log file\n");
  1517.         sprintf(bb ,"%s/P1.NET", path);
  1518.         Net = fopen( bb, "a+b");
  1519.         gettime(&timep);
  1520.         getdate(&datep);
  1521.         header.tosys = CNode;
  1522.         header.touser = 0;
  1523.         header.fromsys = MyNode;
  1524.         header.fromuser = 0;
  1525.         header.main_type = main_type_external;
  1526.         header.minor_type = 6969;
  1527.         header.list_len = 0;
  1528.         header.daten = dostounix(&datep, &timep);
  1529.         header.method = 0;
  1530.         header.length = sizeof(long) + sizeof(LocalNode) + sizeof(FidoNode) +
  1531.                                         + sizeof(FidoNet) + sizeof(FidoZone) +4 + sizeof(log_entry) * Log_Count;
  1532.         fwrite( &header, sizeof(header), 1, Net);
  1533.         fwrite( &Ltime, sizeof(long), 1, Net);
  1534.         fwrite( &LocalNode, sizeof(LocalNode), 1, Net);
  1535.         fwrite( &FidoNode, sizeof(FidoNode), 1, Net);
  1536.         fwrite( &FidoNet, sizeof(FidoNet), 1, Net);
  1537.         fwrite( &FidoZone, sizeof(FidoZone), 1, Net);
  1538.         fwrite( Version, 4, 1, Net);
  1539.         fwrite( Ldata, Log_Count * sizeof(log_entry), 1, Net);
  1540.         fclose(Net);
  1541.         sprintf(bb, "%s/NETSEX.DAT", path);
  1542.         time(&Ltime);
  1543.         Net = fopen( bb, "w+b");
  1544.         fwrite(&Ltime, sizeof(long), 1, Net);
  1545.         fclose(Net);
  1546.  
  1547.     } else {
  1548.         sprintf( bb, "%s/NETSEX.DAT", path);
  1549.         Log = fopen( bb, "w+b");
  1550.         fwrite( &Ltime, sizeof(Ltime), 1, Log);
  1551.         fwrite( Ldata,Log_Count * sizeof(log_entry), 1, Log);
  1552.         fclose(Log);
  1553.     }
  1554.  
  1555.     free(Ldata);
  1556. }
  1557.  
  1558.  
  1559.  
  1560. /* Function: Faddress()
  1561. **
  1562. **
  1563. ** Purpose:  Parse a string containing fidonet address information
  1564. **           in the form:   ZONE:NET/NODE.POINT
  1565. **
  1566. ** Inputs:   Buffer - A the string to parse.
  1567. **
  1568. **
  1569. ** External variables effected:
  1570. **
  1571. **   Zone:  Set to the value of the ZONE field in the address string,
  1572. **          Otherwise it is set to the value in the variable FidoZone.
  1573. **
  1574. **   FNet:  Set to the value of the Fido NET field in the address string,
  1575. **          otherwise it is set to the value in the variable FidoNet.
  1576. **
  1577. **   FNode: Set to the value of the Fido NODE field in the address string,
  1578. **          otherwise it is set to the value in the variable FidoNode.
  1579. **
  1580. **   Point: Set to the value of the Fido POINT field in the address string,
  1581. **          otherwise it is set to 0.
  1582. **
  1583. **
  1584. ** Return Value:
  1585. **
  1586. **   NONE.
  1587. */
  1588. void Faddress(Buffer)
  1589. char *Buffer;
  1590. {
  1591.     int x;
  1592.     int y;
  1593.     int g;
  1594.     char Pflag = 0;
  1595.  
  1596.     x = 0;
  1597.     y = x;
  1598.     g = strlen(Buffer);
  1599.  
  1600.     Zone  = FidoZone;
  1601.     FNet  = FidoNet;
  1602.     FNode = FidoNode;
  1603.     Point = 0;
  1604.  
  1605.     do {
  1606.  
  1607.         switch (Buffer[x]) {
  1608.             case ':':
  1609.                 Buffer[x] = 0;
  1610.                 Zone = atoi(&Buffer[y]);
  1611.                 x++;
  1612.                 y = x;
  1613.                 break;
  1614.  
  1615.             case '/':
  1616.                 Buffer[x] = 0;
  1617.                 FNet = atoi(&Buffer[y]);
  1618.                 x++;
  1619.                 y = x;
  1620.                 break;
  1621.  
  1622.             case '.':
  1623.                 Pflag = 1;
  1624.                 Buffer[x] = 0;
  1625.                 FNode = atoi(&Buffer[y]);
  1626.                 x++;
  1627.                 y = x;
  1628.                 break;
  1629.  
  1630.             case 0:
  1631.                 if (Pflag == 0) {
  1632.                     FNode = atoi(&Buffer[y]);
  1633.                 } else { Point = atoi(&Buffer[y]);
  1634.                 }
  1635.                 x++;
  1636.                 y = x;
  1637.                 break;
  1638.             default:
  1639.                 x++;
  1640.                 break;
  1641.         }
  1642.     } while ( g > x-1);
  1643.  
  1644. }