home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / ios_src / fastsave.c < prev    next >
C/C++ Source or Header  |  1993-01-31  |  31KB  |  1,340 lines

  1. /*********************************************************************
  2.  
  3.                     Packet saver.
  4.                     
  5.     Program : IOSmail
  6.     Source  : free
  7.     Author  : Rinaldo Visscher
  8.     Date    : 1991
  9.     
  10.     Moved from inmail.c to this sources at 7-8-1991
  11.     
  12.     Save PKT's to disk.
  13.         
  14. *********************************************************************/
  15.  
  16.  
  17. #include        <stdio.h>
  18. #include        <stdlib.h>
  19. #include        <ext.h>
  20. #include        <string.h>
  21. #include        <errno.h>
  22. #include        <tos.h>
  23.  
  24. #include        "portab.h"
  25. #include        "defs.h"
  26. #include        "ioslib.h"
  27. #include        "modules.h"
  28. #include        "lang.h"
  29. #include        "vars.h"
  30.  
  31. #include        "inmail.h"
  32.  
  33. /*
  34. **    Buffers for packets
  35. */
  36.  
  37. MLOCAL ULONG     h_pktitems[N_PKTFILE];
  38. MLOCAL BYTE      *h_pktbuf[N_PKTFILE];
  39. MLOCAL ULONG     h_maxitems[N_PKTFILE];
  40. MLOCAL ULONG     h_one_item = 0L;
  41.  
  42. /*
  43. **    Buffers for msgbase.
  44. */
  45.  
  46. MLOCAL ULONG    h_headeritems[N_FASTAREAS];
  47. MLOCAL ULONG    h_msgitems[N_FASTAREAS];
  48. MLOCAL ULONG    h_maxheader[N_FASTAREAS];
  49. MLOCAL ULONG    h_maxmsg[N_FASTAREAS];
  50. MLOCAL ULONG    h_Mstart[N_FASTAREAS];
  51. MLOCAL BYTE        *h_headerbuf[N_FASTAREAS];
  52. MLOCAL BYTE        *h_msgbuf[N_FASTAREAS];
  53. MLOCAL VOID     seek_byte(FILE *fp) ;
  54. VOID addhdrbuf(VOID *text, UWORD len, WORD to );
  55. VOID addmsgbuf(VOID *text, UWORD len, WORD to );
  56. VOID save_to_local( MSGHEADER *Hdr, BYTE * msg, WORD area);
  57.  
  58.  
  59. /*
  60. **    Save the message. In this part there are many files opened.
  61. **    Messages to local, netmail and trasarea are written down here. Also
  62. **    writing PKT files can be found here. For each of them there are
  63. **    seperated files open. 2 files for the netmail, 2 files for the trasharea
  64. **    16 files for the local area and 10 files for the packets.
  65. **    A total of 30 file channels are opened if using full power at
  66. **    importing.
  67. **
  68. **    It works, it speed up the diskaccess, esspecially if many area's must
  69. **    be imported and there are nodes and points connected to an area.
  70. **    Thanks to Jac that's found out this logic. I've build it up to an
  71. **    greater proportion.
  72. */
  73.  
  74. /*
  75. **    WARNING ........
  76. **    Don't make this function longer, the compiler seems to be
  77. **    have problems to generate an good function of it.
  78. */
  79.  
  80. VOID savemsg(BYTE whereto, MSGHEADER *Hdr, WORD area, BYTE *msg,
  81.                 UWORD zone, UWORD net, UWORD node, UWORD point)
  82. {
  83.     BYTE    Mfile[128],
  84.             Hfile[128],
  85.             system32[128],
  86.             *tbuffer,
  87.             pktpwd[10],
  88.             mtype = TYPE3d,
  89.             htype = defPKT,
  90.             wrtTOPT = FALSE,
  91.             wrtFMPT = FALSE;
  92.     WORD    i, j,
  93.             tofile,
  94.             TO_PKTS;
  95.     UWORD    t_zone, t_net, t_node, t_point,
  96.             f_zone, f_net, f_node, f_point,
  97.             zo, ne, no, dzo, dne, dno, dpo;
  98.     
  99.     Hdr->size = ((UWORD)strlen(msg));
  100.     
  101.     switch (whereto)
  102.     {            
  103.         case TOTRASH:
  104.         case TOPRIVATE:
  105.         
  106.             Hdr->mailer[7] |= SCANNED;
  107.             Hdr->flags |= RECEIVED;
  108.             
  109.             switch (whereto)
  110.             {
  111.                 case TOPRIVATE:
  112.                 
  113.                     if(!strlen(privatebox))
  114.                         log_line(6,Logmessage[M__PVTBOX_UNKNOWN]);
  115.                     
  116.                     sprintf(Hfile, "%s.HDR", privatebox);
  117.                     sprintf(Mfile, "%s.MSG", privatebox);
  118.                     
  119.                     tbuffer = (BYTE *) myalloc(MAXMSGLENGTH + 80);
  120.                     
  121.                     if (area != -1)
  122.                     {
  123.                         sprintf(tbuffer, "\03AREA [%s]\n", Areaname[area]);
  124.                         strcat(tbuffer, msg);
  125.                     }
  126.                     else
  127.                     {
  128.                         sprintf(tbuffer, "\03AREA [MAIL]\n");
  129.                         strcat(tbuffer, msg);
  130.                     }
  131.                     
  132.                     Hdr->size = ((UWORD)strlen(tbuffer));
  133.                     break;
  134.                 
  135.                 case TOTRASH:
  136.                 
  137.                     Hdr->mailer[7] = 0;
  138.                     
  139.                     sprintf(Hfile, "%s.HDR", trasharea);
  140.                     sprintf(Mfile, "%s.MSG", trasharea);
  141.                     
  142.                     tbuffer = (BYTE *) myalloc(strlen(msg)+20);
  143.                     strcpy(tbuffer, msg);
  144.                     break;
  145.             }
  146.             
  147.             /*
  148.             **    Area all open? If not, open it.
  149.             */
  150.             
  151.             if ((MSGHDR = fopen(Hfile, "r+b")) == NULL &&
  152.                 (MSGHDR = fopen(Hfile, "wb")) == NULL)
  153.             {
  154.                 log_line(6,Logmessage[M__CANT_OPEN_HEADER], Hfile);
  155.                 return;
  156.             }
  157.             else fseek(MSGHDR, 0L, SEEK_END);
  158.             
  159.             if ((MSGMSG = fopen(Mfile, "r+b")) == NULL)
  160.             {
  161.                 if ((MSGMSG = fopen(Mfile, "wb")) == NULL)
  162.                 {
  163.                     log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  164.                     fclose(MSGHDR);
  165.                     return;
  166.                 }
  167.                 else Hdr->Mstart = 0L;
  168.             }
  169.             else
  170.             {
  171.                 fseek(MSGMSG, 0L, SEEK_END);
  172.                 Hdr->Mstart = ftell(MSGMSG);
  173.             }
  174.             
  175.             hdrwrite(Hdr, sizeof(MSGHEADER), MSGHDR);
  176.             
  177.             msgwrite(tbuffer, (UWORD) strlen(tbuffer), MSGMSG);
  178. /*
  179.             Fwrite(fileno(MSGHDR), sizeof(MSGHEADER), Hdr);
  180.             Fwrite(fileno(MSGMSG), strlen(tbuffer), tbuffer);
  181. */                    
  182.             fclose(MSGMSG);
  183.             fclose(MSGHDR);
  184.             free(tbuffer);
  185.             MSGMSG = NULL;
  186.             MSGHDR = NULL;
  187.             return;
  188.                     
  189.         case TOLOCAL:
  190.             save_to_local(Hdr, msg, area);
  191.                         
  192.             return;
  193.             
  194.         case TONETMAIL:
  195.         
  196.             sprintf(Mfile,"%s.MSG",mailarea);
  197.             sprintf(Hfile,"%s.HDR",mailarea);
  198.             
  199. #if defined DEBUG
  200.             printf("*Saving MAIL\n");
  201. #endif        
  202.             
  203.             /*
  204.             **    If the channel is already opened, we don't have to
  205.             **    open it again. This speed up some things.
  206.             */
  207.                 
  208.             if (!MMSG && !MHDR)
  209.             {
  210.                 if ((MMSG = fopen(Mfile, "r+b")) == NULL)
  211.                     if ((MMSG = fopen(Mfile, "wb")) == NULL)
  212.                     {
  213.                         log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  214.                         return;
  215.                     }
  216.                     else
  217.                         Hdr->Mstart = 0L;
  218.                 else
  219.                 {
  220.                     fseek(MMSG, 0L, SEEK_END);
  221.                     Hdr->Mstart = ftell(MMSG);
  222.                 }
  223.                 
  224.                 if ((MHDR = fopen(Hfile, "r+b")) == NULL &&
  225.                     (MHDR = fopen(Hfile,"wb")) == NULL)
  226.                     {
  227.                         log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  228.                         return;
  229.                     }
  230.                 else
  231.                     fseek(MHDR, 0L, SEEK_END);
  232.             }
  233.             else
  234.             {
  235.                 fseek(MMSG, 0L, SEEK_END);
  236.                 Hdr->Mstart = ftell(MMSG);
  237.             }
  238. /*            
  239.             hdrwrite(Hdr, sizeof(MSGHEADER), MHDR);
  240.             msgwrite(msg, (UWORD) strlen(msg)+1, MMSG);
  241. */            
  242.             Fwrite(fileno(MHDR), sizeof(MSGHEADER), Hdr);
  243.             Fwrite(fileno(MMSG), strlen(msg)+1, msg);
  244.             
  245.             fflush(MMSG);
  246.             fflush(MHDR);
  247.             
  248.             return;
  249.             
  250.         case TOPACKET:
  251.             
  252.             /*
  253.             **    Get the password of the destination.
  254.             */
  255.             
  256.             mtype = TYPE3d;
  257.             htype = defPKT;
  258.             
  259.             for (i=0; i < nkey; i++)
  260.                 if (Hdr->Dzone == pwd[i].zone &&
  261.                     Hdr->Dnet == pwd[i].net &&
  262.                     Hdr->Dnode == pwd[i].node &&
  263.                     Hdr->Dpoint == pwd[i].point)
  264.                 {
  265.                     strcpy(pktpwd, pwd[i].pwd);
  266.                     mtype = pwd[i].mtype;
  267.                     htype = pwd[i].htype;
  268.                     break;
  269.                 }
  270.             
  271.             
  272.             /*
  273.             **    If doing secure, watch all passwords mentioned in the
  274.             **    config file. If the destination is not found, break of
  275.             **    and warn the user of an illegal user of this area.
  276.             **    Then return to homebase.
  277.             */
  278.             
  279.             if (dosecure && i >= nkey)
  280.             {
  281.                 if (area >= 0)
  282.                 {
  283.                     log_line(6,Logmessage[M__NODE_UNKNOWN],
  284.                         Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  285.                     log_line(2,Logmessage[M__CHECK_PWDS],
  286.                         Areaname[area]);
  287.                 }
  288.                 else
  289.                 {
  290.                     log_line(6,Logmessage[M__NODE_UNKNOWN_NETMAIL],
  291.                         Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  292.                     log_line(2,Logmessage[M__CHECK_CONFIG]);
  293.                 }
  294.                 
  295.                 return;
  296.             }
  297.             
  298.             dzo = t_zone = Hdr->Dzone;
  299.             dne = t_net = Hdr->Dnet;
  300.             dno = t_node = Hdr->Dnode;
  301.             dpo = t_point = Hdr->Dpoint;
  302.             
  303.             zo = f_zone = Hdr->Ozone;
  304.             ne = f_net = Hdr->Onet;
  305.             no = f_node = Hdr->Onode;
  306.             f_point = Hdr->Opoint;
  307.             
  308.             /*
  309.             **    If point then 3d. Not if point using 4d address.
  310.             **    If netmail, don't change the messageheader.
  311.             **    zo,ne,no,dzo etc. contains the fakenetnumer of
  312.             **    the points.
  313.             */
  314.             
  315.             if (Hdr->Dpoint && mtype == TYPE3d)
  316.             {
  317.                 for (i=0; i < nalias; i++)
  318.                     if (alias[i].zone == Hdr->Dzone &&
  319.                         alias[i].net == Hdr->Dnet &&
  320.                         alias[i].node == Hdr->Dnode)
  321.                     {
  322.                         dzo = Hdr->Dzone;
  323.                         
  324.                         if (area >= 0)
  325.                         {
  326.                             dne = Hdr->Dnet = alias[i].pointnet;
  327.                             dno = Hdr->Dnode = Hdr->Dpoint;
  328.                             Hdr->Dpoint = dpo = 0;
  329.                         }
  330.                         else
  331.                         {
  332.                             dne = alias[i].pointnet;
  333.                             dno = Hdr->Dpoint;
  334.                             dpo = 0;
  335.                         }
  336.                         
  337.                         break;
  338.                     }
  339.                 
  340.                 if (i >= nalias)
  341.                 {
  342.                     htype = FRONTDOOR;
  343.                     mtype = TYPE4d;
  344.                     wrtTOPT = TRUE;
  345.                 }
  346.             }
  347.             
  348.             if (Hdr->Opoint && mtype == TYPE3d)
  349.             {
  350.                 for (i=0; i < nalias; i++)
  351.                     if (alias[i].zone == Hdr->Ozone &&
  352.                         alias[i].net == Hdr->Onet &&
  353.                         alias[i].node == Hdr->Onode)
  354.                     {
  355.                         zo = Hdr->Ozone;
  356.                         
  357.                         if (area >= 0)
  358.                         {
  359.                             ne = Hdr->Onet = alias[i].pointnet;
  360.                             no = Hdr->Onode= Hdr->Opoint;
  361.                             Hdr->Opoint = 0;
  362.                         }
  363.                         else
  364.                         {
  365.                             ne = alias[i].pointnet;
  366.                             no = Hdr->Opoint;
  367.                         }
  368.                         
  369.                         break;
  370.                     }
  371.                 
  372.                 if (i >= nalias)
  373.                 {
  374.                     htype = FRONTDOOR;
  375.                     mtype = TYPE4d;
  376.                     wrtFMPT = TRUE;
  377.                 }
  378.             }
  379.             
  380.             /*
  381.             **    Create filename.
  382.             */
  383.             
  384.             xsprintf(system32, "%02z%03z%03z.%02z", dzo, dne, dno, dpo);
  385.             
  386.             /*
  387.             **    If new file create packet header.
  388.             */
  389.             
  390.             switch (htype)
  391.             {
  392.                 case FTS_TYPE:
  393.                 
  394.                     fts_TYPE:
  395.                     create_pkt_hdr(zo, ne, no, dzo, dne, dno, pktpwd);
  396.                     break;
  397.                     
  398.                 /*
  399.                 **    Create an packetheader for frontdoor.
  400.                 */
  401.                 
  402.                 case TYPE_2_1:
  403.                 case FRONTDOOR:
  404.                 
  405.                     create_pkt_fnd(f_zone, f_net, f_node, f_point,
  406.                         t_zone, t_net, t_node, t_point, pktpwd);
  407.                     break;
  408.  
  409.                 case TYPE_2_2:    
  410.                     create_pkt_2_2(f_zone, f_net, f_node, f_point,
  411.                         t_zone, t_net, t_node, t_point, pktpwd);
  412.                     break;
  413.                 
  414.                 default:
  415.                 
  416.                     log_line(6,Logmessage[M__COMPILER_ERROR]);
  417.                     goto fts_TYPE;
  418.             }
  419.             
  420. #if defined DEBUG
  421.             printf("*Writing PKT for %d:%d/%d.%d\n", Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  422. #endif        
  423.             
  424.             /*
  425.             **    Trying to find the channel of this node or point.
  426.             **    If not found search for an NULL filepointer.
  427.             */
  428.             
  429.             if (dofastimport) TO_PKTS = N_PKTFILE / 2;
  430.             else TO_PKTS = N_PKTFILE;
  431.             
  432.             for (i=0; i < TO_PKTS; i++)
  433.                 if (PKTzone[i] == t_zone &&
  434.                     PKTnet[i] == t_net &&
  435.                     PKTnode[i] == t_node &&
  436.                     PKTpoint[i] == t_point)
  437.                 {
  438.                     tofile = i;
  439. /*                    fseek(PKTfile[i], 0L, SEEK_END); */
  440.                     break;
  441.                 }
  442.             
  443.             /*
  444.             **    This node or point have no channel.
  445.             **    Searching for an free NULL filepointer.
  446.             */
  447.             
  448.             if (i >= TO_PKTS)
  449.             {
  450.                 for (i=0; i < TO_PKTS; i++)
  451.                     if (PKTchannel[i] == FILEclosed)
  452.                     {
  453.                         tofile = i;
  454.                         PKTchannel[i] = FILEopen;
  455.                         if (Openpkt(i, system32, Hdr)) {
  456.                             if (!h_one_item) {
  457.                                 h_one_item = ((coreleft() / N_PKTFILE) / 2560 * 2048);
  458.                             }
  459.                             h_maxitems[i] = h_one_item;
  460.                             h_pktbuf[i] = (BYTE *)myalloc(h_maxitems[i]);
  461.                             h_pktitems[i] = 0L;
  462.                             break;
  463.                         }
  464.                         return;
  465.                     }
  466.                 
  467.                 /*
  468.                 **    If all files are opened, try to find an channel
  469.                 **    that's not used. Logic :
  470.                 **
  471.                 **    Compare the nodes and points that's are connected
  472.                 **    to this area with the nodes and point that are
  473.                 **    using an channel. If found break of and continue;
  474.                 **    If not close this channel and open it again for
  475.                 **    the new node or point. If all using this channel
  476.                 **    close the first one.
  477.                 **
  478.                 **    I speeded the diskwriting up mother.
  479.                 */
  480.                 
  481.                 if (i >= TO_PKTS)
  482.                 {
  483.                     if (area >= 0)
  484.                     {
  485.                         for (j=0; j < TO_PKTS; j++)
  486.                         {
  487.                             for (i=0; Tozone[area][i] != (UWORD)(-1); i++)
  488.                                 if (Tozone[area][i] == PKTzone[j] &&
  489.                                     Tonet[area][i] == PKTnet[j] &&
  490.                                     Tonode[area][i] == PKTnode[j] &&
  491.                                     Topoint[area][i] == PKTpoint[j])
  492.                                     break;
  493.                             
  494.                             if (Tozone[area][i] == (UWORD)(-1))
  495.                             {
  496.                                 fseek(PKTfile[j], 0L, SEEK_END);
  497.                                 Fwrite(fileno(PKTfile[j]), 
  498.                                     h_pktitems[j], h_pktbuf[i]);
  499.                                 
  500.                                 putc(0, PKTfile[j]);
  501.                                 putc(0, PKTfile[j]);
  502.                                 fclose(PKTfile[j]);
  503.                                 
  504.                                 tofile = j;
  505.                                 if (!Openpkt(j, system32, Hdr)) return;
  506.                                 h_pktitems[j] = 0L;
  507.                                 break;
  508.                             }
  509.                         }
  510.                     }
  511.                     else j = TO_PKTS;
  512.                     
  513.                     /*
  514.                     **    Brrrr.... close the first channel. There are
  515.                     **    so many connected.
  516.                     **
  517.                     **    It slow down the process.
  518.                     */
  519.                     
  520.                     if (j >= TO_PKTS )
  521.                     {
  522.                         fseek(PKTfile[0], 0L, SEEK_END);
  523.                         Fwrite(fileno(PKTfile[0]), h_pktitems[0],
  524.                             h_pktbuf[0]);
  525.                         putc(0, PKTfile[0]);
  526.                         putc(0, PKTfile[0]);
  527.                         fclose(PKTfile[0]);
  528.                         h_pktitems[0] = 0L;
  529.                         tofile = 0;
  530.                         if (!Openpkt(0, system32, Hdr)) return;
  531.                         
  532.                     }
  533.                 }
  534.             }
  535.             
  536.             /*
  537.             **    This node or point is now in use of this channel.
  538.             */
  539.             
  540.             PKTzone[tofile] = t_zone;
  541.             PKTnet[tofile] = t_net;
  542.             PKTnode[tofile] = t_node;
  543.             PKTpoint[tofile] = t_point;
  544.             
  545.             pmsg.pm_ver = intel(2);
  546.             pmsg.pm_dnet = intel(Hdr->Dnet);
  547.             pmsg.pm_dnode = intel(Hdr->Dnode);
  548.             pmsg.pm_onet = intel(Hdr->Onet);
  549.             pmsg.pm_onode = intel(Hdr->Onode);
  550.             pmsg.pm_attr = intel((Hdr->flags &= ~(MSGLOCAL|MSGHOLD|KILLSEND|SENT|MSGFWD|ORPHAN)));
  551.             pmsg.pm_cost = 0;
  552.             
  553.             /*
  554.             **    Writing the pkt message header.
  555.             */
  556. /*            
  557.             hdrwrite(&pmsg, sizeof(struct _pktmsg), PKTfile[tofile]);
  558.  
  559.             Fwrite(fileno(PKTfile[tofile]), sizeof(struct _pktmsg), &pmsg);
  560.                         
  561.             pktwrite(Hdr->time, (UWORD) strlen(Hdr->time), PKTfile[tofile]);
  562.             pktwrite(Hdr->to, (UWORD) strlen(Hdr->to), PKTfile[tofile]);
  563.             pktwrite(Hdr->from, (UWORD) strlen(Hdr->from), PKTfile[tofile]);
  564.             pktwrite(Hdr->topic, (UWORD) strlen(Hdr->topic), PKTfile[tofile]);
  565. */
  566.             
  567.             addpktbuf(&pmsg, (UWORD)sizeof(struct _pktmsg), tofile, 0);
  568.  
  569.             addpktbuf(Hdr->time, (UWORD) strlen(Hdr->time)+1, tofile, 0);
  570.             addpktbuf(Hdr->to, (UWORD) strlen(Hdr->to)+1, tofile, 0);
  571.             addpktbuf(Hdr->from, (UWORD) strlen(Hdr->from)+1, tofile, 0);
  572.             addpktbuf(Hdr->topic, (UWORD) strlen(Hdr->topic)+1, tofile, 0);
  573.                         
  574.             if (wrtTOPT) {
  575.                 sprintf(Mfile, "\01TOPT %d\n", Hdr->Dpoint);
  576.                 addpktbuf(Mfile, (UWORD)strlen(Mfile), tofile, 1);
  577.             }
  578.             if (wrtFMPT) {
  579.                 sprintf(Mfile, "\01FMPT %d\n", Hdr->Opoint);
  580.                 addpktbuf(Mfile, (UWORD)strlen(Mfile), tofile, 1);
  581.             }
  582.                         
  583.             /*
  584.             **    Put the message on disk.
  585.             */
  586.             
  587.             addpktbuf(msg, (UWORD) strlen(msg)+1, tofile, 1);
  588.             
  589.             break;
  590.             
  591.         case NETMAILPACKET:
  592.             
  593.             /*
  594.             **    Get the pasword of the destination.
  595.             */
  596.             
  597.             mtype = TYPE3d;
  598.             htype = defPKT;
  599.             
  600.             for (i=0; i < nkey; i++)
  601.             {
  602.                 if (zone == pwd[i].zone && net == pwd[i].net &&
  603.                     node == pwd[i].node && point == pwd[i].point)
  604.                 {
  605.                     strcpy(pktpwd, pwd[i].pwd);
  606.                     
  607.                     mtype = pwd[i].mtype;
  608.                     htype = pwd[i].htype;
  609.                     
  610. #if defined OUTDEBUG
  611.                     if (dooutdeb) log_line(6,">Have password %d:%d/%d.%d - %s", pwd[i].zone, pwd[i].net, pwd[i].node, pwd[i].point, pwd[i].pwd);
  612. #endif
  613.                     break;
  614.                 }
  615.             }
  616.             
  617.             /*
  618.             **    If crashmail, delete password.
  619.             */
  620.             
  621.             if (Hdr->flags & CRASH)
  622.                 for (i=0; i < (WORD) strlen(pktpwd); i++)
  623.                     pktpwd[i] = EOS;
  624.             
  625.             if (i >= nkey)
  626.             {
  627. #if defined OUTDEBUG
  628.                 if (dooutdeb) log_line(6,">No password for %d:%d/%d.%d", zone, net, node, point);
  629. #endif
  630.                 *pktpwd = EOS;
  631.             }
  632.             
  633.             /*
  634.             **    If no crashmail, take normal.
  635.             */
  636.             
  637.             if (!(Hdr->flags & CRASH))
  638.             {
  639.                 if (point)
  640.                 {
  641.                     dzo = zone;
  642.                     
  643.                     for (i=0; i < nalias; i++)
  644.                         if (zone == alias[i].zone &&
  645.                             net == alias[i].net &&
  646.                             node == alias[i].node)
  647.                         {
  648.                             dne = alias[i].pointnet;
  649.                             dno = point;
  650.                             dpo = 0;
  651. #if defined OUTDEBUG
  652.                             if (dooutdeb) log_line(6,">Point - dzo, dne, dno %d:%d/%d", dzo, dne, dno);
  653. #endif
  654.                             break;
  655.                         }
  656.                 }
  657.                 else
  658.                 {
  659.                     dzo = zone;
  660.                     dne = net;
  661.                     dno = node;
  662.                     dpo = 0;
  663.                     
  664. #if defined OUTDEBUG
  665.                     if (dooutdeb) log_line(6,">No point - dzo,dne,dno %d:%d/%d", dzo, dne, dno);
  666. #endif
  667.                 }
  668.                 
  669.                 t_zone = zone;
  670.                 t_net = net;
  671.                 t_node = node;
  672.                 t_point = point;
  673.                 
  674.                 /*
  675.                 **    Get origin address from ourself.
  676.                 */
  677.                 
  678.                 for (i=0; i < nalias; i++)
  679.                     if (zone == alias[i].zone &&
  680.                         node == alias[i].node &&
  681.                         net == alias[i].net)
  682.                     {
  683.                         if (!alias[i].point)
  684.                         {
  685.                             zo = alias[i].zone;
  686.                             ne = alias[i].net;
  687.                             no = alias[i].node;
  688.                         }
  689.                         else
  690.                         {
  691.                             zo = alias[i].zone;
  692.                             ne = alias[i].pointnet;
  693.                             no = alias[i].point;
  694.                         }
  695.                         
  696.                         f_zone = alias[i].zone;
  697.                         f_net = alias[i].net;
  698.                         f_node = alias[i].node;
  699.                         f_point = alias[i].point;
  700.                         
  701.                         break;
  702.                     }
  703.                 
  704.                 if (i >= nalias)
  705.                 {
  706.                     for (i=0; i < nalias; i++)
  707.                         if (zone == alias[i].zone)
  708.                         {
  709.                             if (!alias[i].point)
  710.                             {
  711.                                 zo = alias[i].zone;
  712.                                 ne = alias[i].net;
  713.                                 no = alias[i].node;
  714.                             }
  715.                             else
  716.                             {
  717.                                 zo = alias[i].zone;
  718.                                 ne = alias[i].pointnet;
  719.                                 no = alias[i].point;
  720.                             }
  721.                             
  722.                             f_zone = alias[i].zone;
  723.                             f_net  = alias[i].net;
  724.                             f_node = alias[i].node;
  725.                             f_point= alias[i].point;
  726.                             
  727.                             break;
  728.                         }
  729.                     
  730.                     if (i >= nalias)
  731.                         if (!alias[0].point)
  732.                         {
  733.                             zo = f_zone = alias[0].zone;
  734.                             ne = f_net = alias[0].net;
  735.                             no = f_node = alias[0].node;
  736.                             f_point = alias[0].point;
  737.                         }
  738.                         else
  739.                         {
  740.                             zo = f_zone = alias[0].zone;
  741.                             ne = alias[0].pointnet;
  742.                             no = f_point = alias[0].point;
  743.                             f_net = alias[0].net;
  744.                             f_node = alias[0].node;
  745.                         }
  746.                 }    
  747.             }
  748.             else
  749.             {
  750.             
  751.                 /*
  752.                 **    Crashmail.
  753.                 */
  754.                 
  755.                 if (Hdr->Opoint)
  756.                 {
  757.                     for (i=0; i < nalias; i++)
  758.                         if (Hdr->Ozone == alias[i].zone &&
  759.                             Hdr->Onet == alias[i].net &&
  760.                             Hdr->Onode == alias[i].node &&
  761.                             Hdr->Opoint == alias[i].point)
  762.                         {
  763.                             zo = alias[i].zone;
  764.                             ne = alias[i].pointnet;
  765.                             no = alias[i].point;
  766.                             break;
  767.                         }
  768.                     
  769.                     if (i >= nalias)
  770.                     {
  771.                         zo = Hdr->Ozone;
  772.                         ne = Hdr->Onet;
  773.                         no = Hdr->Onode;
  774.                     }
  775.                 }
  776.                 else
  777.                 {
  778.                     zo = Hdr->Ozone;
  779.                     ne = Hdr->Onet;
  780.                     no = Hdr->Onode;
  781.                 }
  782.                 
  783.                 f_zone = Hdr->Ozone;
  784.                 f_net = Hdr->Onet;
  785.                 f_node = Hdr->Onode;
  786.                 f_point = Hdr->Opoint;
  787.                 
  788.                 if (Hdr->Dpoint)
  789.                     for (i=0; i < nalias; i++)
  790.                         if (Hdr->Dnet == alias[i].net &&
  791.                             Hdr->Dnode == alias[i].node &&
  792.                             Hdr->Dzone == alias[i].zone)
  793.                         {
  794.                             log_line(4,Logmessage[M__CANT_CRASH_POINT]);
  795.                             return;
  796.                         }
  797.                 
  798.                 dzo = t_zone = Hdr->Dzone;
  799.                 dne = t_net = Hdr->Dnet;
  800.                 dno = t_node = Hdr->Dnode;
  801.                 dpo = t_point = Hdr->Dpoint;
  802.             }
  803.             
  804. #if defined EXTERNDEBUG
  805.             if (debugflag) log_line(6,">Now have as destination PKT %d:%d/%d.%d", dzo, dne, dno, dpo);
  806. #endif
  807.             
  808.             /*
  809.             **    Create filename.
  810.             */
  811.             
  812.             xsprintf(system32, "%02z%03z%03z.%02z", dzo, dne, dno, dpo);
  813.             
  814.             /*
  815.             **    If new file create packet header.
  816.             */
  817.             
  818.             switch (htype)
  819.             {
  820.                 case FTS_TYPE:
  821.                 
  822.                     FTS_type:
  823.                     create_pkt_hdr(zo, ne, no, dzo, dne, dno, pktpwd);
  824.                     break;
  825.                     
  826.                 /*
  827.                 **    Create an packetheader for frontdoor.
  828.                 */
  829.                 
  830.                 case TYPE_2_1:
  831.                 case FRONTDOOR:
  832.                 
  833.                     create_pkt_fnd(f_zone, f_net, f_node, f_point,
  834.                         t_zone, t_net, t_node, t_point, pktpwd);
  835.                     break;
  836.  
  837.                 case TYPE_2_2:
  838.                     create_pkt_2_2(f_zone, f_net, f_node, f_point,
  839.                         t_zone, t_net, t_node, t_point, pktpwd);
  840.                     break;
  841.                 
  842.                 default:
  843.                 
  844.                     log_line(6,Logmessage[M__COMPILER_ERROR]);
  845.                     goto FTS_type;
  846.             }
  847.             
  848. #if defined DEBUG
  849.             printf("*Writing PKT for %d:%d/%d\n", Hdr->Dzone, Hdr->Dnet, Hdr->Dnode);
  850. #endif        
  851.             
  852.             /*
  853.             **    Trying to find the channel of this node or point.
  854.             **    If not found search for an NULL filepointer.
  855.             */
  856.             
  857.             if (dofastimport) TO_PKTS = N_PKTFILE / 2;
  858.             else TO_PKTS = N_PKTFILE;
  859.             
  860.             for (i=0; i < TO_PKTS; i++)
  861.                 if (PKTzone[i] == t_zone && PKTnet[i] == t_net &&
  862.                     PKTnode[i] == t_node && PKTpoint[i] == t_point)
  863.                 {
  864.                     tofile = i;
  865. /*                    fseek(PKTfile[i], 0L, SEEK_END);    */
  866.                     break;
  867.                 }
  868.             
  869.             /*
  870.             **    This node or point have no channel.
  871.             **    Searching for an free NULL filepointer.
  872.             */
  873.             
  874.             if (i >= TO_PKTS)
  875.             {
  876.                 for (i=0; i < TO_PKTS; i++)
  877.                     if (PKTchannel[i] == FILEclosed)
  878.                     {
  879.                         tofile = i;
  880.                         PKTchannel[i] = FILEopen;
  881.                         
  882.                         if (Openpkt(i, system32, Hdr)) {
  883.                             if (!h_one_item) {
  884.                                 h_one_item = ((coreleft() / N_PKTFILE) / 2560 * 2048);
  885.                             }
  886.                             h_maxitems[i] = h_one_item;
  887.                             h_pktbuf[i] = (BYTE *)myalloc(h_maxitems[i]);
  888.                             h_pktitems[i] = 0L;
  889.                             break;
  890.                         }
  891.                         return;
  892.                     }
  893.                 
  894.                 if (i >= TO_PKTS )
  895.                 {
  896.                     fseek(PKTfile[0], 0L, SEEK_END);
  897.                     Fwrite(fileno(PKTfile[i]), h_pktitems[0],
  898.                         h_pktbuf[0]);
  899.                     putc(0,PKTfile[0]);
  900.                     putc(0,PKTfile[0]);
  901.                     fclose(PKTfile[0]);
  902.                     h_pktitems[0] = 0L;
  903.                     
  904.                     tofile = 0;
  905.                     if (!Openpkt(0, system32, Hdr)) return;
  906.                 }
  907.             }
  908.             
  909.             /*
  910.             **    This node or point is now in use of this channel.
  911.             */
  912.             
  913.             PKTzone[tofile] = t_zone;
  914.             PKTnet[tofile] = t_net;
  915.             PKTnode[tofile] = t_node;
  916.             PKTpoint[tofile] = t_point;
  917.             
  918.             pmsg.pm_ver = intel(2);
  919.             pmsg.pm_dnet = intel(Hdr->Dnet);
  920.             pmsg.pm_dnode = intel(Hdr->Dnode);
  921.             pmsg.pm_onet = intel(Hdr->Onet);
  922.             pmsg.pm_onode = intel(Hdr->Onode);
  923.             pmsg.pm_attr = intel(Hdr->flags &= ~(MSGLOCAL|MSGHOLD|KILLSEND|SENT|MSGFWD));
  924.             pmsg.pm_cost = 0;
  925.             
  926.             /*
  927.             **    Writing the pkt message header.
  928.             */
  929.             
  930. #if defined OUTDEBUG
  931.             if (dooutdeb) log_line(6,">Writing packet");
  932. #endif
  933. /*
  934.             hdrwrite(&pmsg, sizeof(struct _pktmsg), PKTfile[tofile]);
  935.             Fwrite(fileno(PKTfile[tofile]), sizeof(struct _pktmsg), &pmsg);
  936.                         
  937.             pktwrite(Hdr->time, (UWORD) strlen(Hdr->time), PKTfile[tofile]);
  938.             pktwrite(Hdr->to, (UWORD) strlen(Hdr->to), PKTfile[tofile]);
  939.             pktwrite(Hdr->from, (UWORD) strlen(Hdr->from), PKTfile[tofile]);
  940.             pktwrite(Hdr->topic, (UWORD) strlen(Hdr->topic), PKTfile[tofile]);
  941. */
  942.             addpktbuf(&pmsg, (UWORD)sizeof(struct _pktmsg), tofile, 0);
  943.  
  944.             addpktbuf(Hdr->time, (UWORD) strlen(Hdr->time)+1, tofile, 0);
  945.             addpktbuf(Hdr->to, (UWORD) strlen(Hdr->to)+1, tofile, 0);
  946.             addpktbuf(Hdr->from, (UWORD) strlen(Hdr->from)+1, tofile, 0);
  947.             addpktbuf(Hdr->topic, (UWORD) strlen(Hdr->topic)+1, tofile, 0);
  948.                         
  949.             /*
  950.             **    INTL needed?
  951.             */
  952.             
  953.             if (Hdr->Ozone != f_zone && Hdr->Dzone != t_zone)
  954.             {
  955.                 if (strncmp(msg,"AREA:",5))
  956.                 {
  957.                     if (!get_interzone(msg, &f_zone, &f_net, &f_node, &t_zone, &t_net, &t_node))
  958.                     {
  959.                         write_INTL:
  960.                         
  961. #if defined OUTDEBUG
  962.                         if (dooutdeb) log_line(6,">Writing INTL");
  963. #endif
  964.                         sprintf(Hfile,"\01INTL %d:%d/%d %d:%d/%d\n",
  965.                             Hdr->Ozone, Hdr->Onet, Hdr->Onode,
  966.                             Hdr->Dzone, Hdr->Dnet, Hdr->Dnode);
  967.                         
  968.                         addpktbuf(Hfile, (UWORD)strlen(Hfile), tofile, 1);
  969.  
  970.                     }
  971.                     else
  972.                     {
  973.                         if (f_zone != Hdr->Ozone &&
  974.                             t_zone != Hdr->Dzone)
  975.                         {
  976.                             msg = strip_line(msg, "\01INTL ");
  977.                             goto write_INTL;
  978.                         }
  979.                     }
  980.                 }
  981.             }
  982.             
  983.             addpktbuf(msg, (UWORD) strlen(msg), tofile, 1);
  984.             
  985.             break;
  986.         
  987.         default:
  988.         
  989.             log_line(6,Logmessage[M__COMPILER_ERROR]);
  990.             break;
  991.     }
  992. }
  993.  
  994. VOID ToDataBase(MSGHEADER *Hdr, BYTE *msg, WORD area) {
  995.     WORD i;
  996.     BYTE 
  997.          Hfile[128],
  998.          Mfile[128];
  999.     
  1000.     SMALLBASE sbase;
  1001.     
  1002.     for (i=0; i < nfdbase; i++) {
  1003.         if (newmatch( (FDcompare[nfdbase] == FD_FROM) ? Hdr->from : Hdr->to, ToBase[nfdbase])) {
  1004.             sprintf(Hfile,"%sDATA.HR",FDbase);
  1005.             sprintf(Mfile,"%sDATA.MG",FDbase);
  1006.             
  1007.             if ((MSGHDR = fopen(Hfile,"r+b")) == NULL ) {
  1008.                 if ((MSGHDR = fopen(Hfile,"wb"))  == NULL) {
  1009.                     log_line(5,Logmessage[M__CANT_OPEN_B_DBASE]);
  1010.                     return;
  1011.                 }
  1012.             } else fseek(MSGHDR, 0L, SEEK_END);
  1013.             
  1014.             if ((MSGMSG = fopen(Mfile, "r+b")) == NULL) {
  1015.                 if ((MSGMSG = fopen(Mfile, "wb")) == NULL){
  1016.                     log_line(6,Logmessage[M__CANT_OPEN_M_DBASE]);
  1017.                     fclose(MSGHDR);
  1018.                     return;
  1019.                 }
  1020.                 else sbase.Mstart = 0L;
  1021.             }
  1022.             else {
  1023.                 fseek(MSGMSG, 0L, SEEK_END);
  1024.                 sbase.Mstart = ftell(MSGMSG);
  1025.             }
  1026.             
  1027.             strcpy (sbase.from, Hdr->from);
  1028.             strcpy (sbase.to, Hdr->to);
  1029.             strcpy (sbase.topic, Hdr->topic);
  1030.             strcpy (sbase.time, Hdr->time);
  1031.             strncpy(sbase.area, Areaname[area],39);
  1032.             sbase.size = (UWORD) strlen(msg);
  1033.             
  1034.             hdrwrite(&sbase, sizeof(SMALLBASE), MSGHDR);
  1035.             
  1036.             msgwrite(msg, (UWORD) strlen(msg), MSGMSG);
  1037.             
  1038.             fclose(MSGMSG);
  1039.             fclose(MSGHDR);
  1040.             return;
  1041.         }
  1042.     }
  1043. }
  1044.  
  1045. MLOCAL VOID seek_byte(FILE *fp) {
  1046.     WORD i;
  1047.     
  1048.     fseek(fp, 0L, SEEK_END);
  1049.     if (ftell(fp) <= sizeof(struct _pkthdr)) return;
  1050.     
  1051.     fseek(fp, ftell(fp) -3L, SEEK_SET);
  1052.     
  1053.     i = getc(fp);
  1054.     if (!i) return;
  1055.     i = getc(fp);
  1056.     if (!i) return;
  1057.     i = getc(fp);
  1058.     if (!i) return;
  1059.     putc(0, fp);
  1060. }
  1061.  
  1062.  
  1063. VOID addpktbuf(VOID *text, UWORD len, WORD to , BYTE what) {
  1064.     BYTE *buffer;
  1065.     
  1066.     if (h_pktitems[to] + len > h_maxitems[to]) {
  1067.     
  1068.         seek_byte(PKTfile[to]);
  1069.         Fwrite(fileno(PKTfile[to]), h_pktitems[to], h_pktbuf[to]);
  1070.         h_pktitems[to] = 0L;
  1071.     }
  1072.     
  1073.     buffer = (BYTE *)text;
  1074.     
  1075.     while (len--) {
  1076.         if (what) {
  1077.             if (*buffer == '\n') *buffer = '\r';
  1078.         }
  1079.         h_pktbuf[to][h_pktitems[to]++] = *buffer++;
  1080.     }
  1081. }
  1082.  
  1083. VOID addhdrbuf(VOID *text, UWORD len, WORD to ) {
  1084.     BYTE *buffer;
  1085.     
  1086.     if (h_headeritems[to] + len > h_maxheader[to]) {
  1087.     
  1088.         fseek(LHDR[to] , 0L, SEEK_END);
  1089.         Fwrite(fileno(LHDR[to]), h_headeritems[to], h_headerbuf[to]);
  1090.         /*
  1091.         hdrwrite(h_headerbuf[to], h_headeritems[to], LHDR[to]);
  1092.         */
  1093.         h_headeritems[to] = 0L;
  1094.     }
  1095.     
  1096.     buffer = (BYTE *)text;
  1097.     
  1098.     while (len--) {
  1099.         h_headerbuf[to][h_headeritems[to]++] = *buffer++;
  1100.     }
  1101. }
  1102.  
  1103. VOID addmsgbuf(VOID *text, UWORD len, WORD to ) {
  1104.     BYTE *buffer;
  1105.     
  1106.     if (h_msgitems[to] + len > h_maxmsg[to]) {
  1107.     
  1108.         fseek(LMSG[to] , 0L, SEEK_END);
  1109.         Fwrite(fileno(LMSG[to]), h_msgitems[to], h_msgbuf[to]);
  1110.         /*
  1111.         msgwrite(h_msgbuf[to], h_msgitems[to], LMSG[to]);
  1112.         */
  1113.         h_msgitems[to] = 0L;
  1114.     }
  1115.     
  1116.     buffer = (BYTE *)text;
  1117.     
  1118.     while (len--) {
  1119.         h_msgbuf[to][h_msgitems[to]++] = *buffer++;
  1120.     }
  1121. }
  1122.  
  1123. VOID freepktbuf() {
  1124.     WORD i;
  1125.     
  1126.     for (i=0; i < N_PKTFILE; i++) {
  1127.         if (PKTchannel[i] == FILEopen) {
  1128.             if (h_pktitems[i]) {
  1129.                 seek_byte(PKTfile[i]);
  1130.                 Fwrite(fileno(PKTfile[i]), h_pktitems[i], h_pktbuf[i]);
  1131.             }
  1132.             free(h_pktbuf[i]);
  1133.         }
  1134.     }
  1135.     h_one_item = 0L;
  1136. }
  1137.  
  1138. VOID freemsgbuf() {
  1139.     WORD i;
  1140.     
  1141.     for (i=0; i < N_FASTAREAS; i++) {
  1142.         if (LHDRfile[i] == FILEopen) {
  1143.             if (h_headeritems[i]) {
  1144.                 fseek(LHDR[i], 0L, SEEK_END);
  1145.                 /*
  1146.                 hdrwrite(h_headerbuf[i], h_headeritems[i], LHDR[i]);
  1147.                 */
  1148.                 Fwrite(fileno(LHDR[i]), h_headeritems[i], h_headerbuf[i]);
  1149.  
  1150.             }
  1151.             free(h_headerbuf[i]);
  1152.         }
  1153.         if (LMSGfile[i] == FILEopen) {
  1154.             if (h_msgitems[i]) {
  1155.                 fseek(LMSG[i], 0L, SEEK_END);
  1156.                 Fwrite(fileno(LMSG[i]), h_msgitems[i], h_msgbuf[i]);
  1157.                 /*
  1158.                 msgwrite(h_msgbuf[i], h_msgitems[i], LMSG[i]);
  1159.                 */
  1160.             }
  1161.             free(h_headerbuf[i]);
  1162.         }
  1163.         
  1164.     }
  1165.     h_one_item = 0L;
  1166. }
  1167.  
  1168. VOID save_to_local( MSGHEADER *Hdr, BYTE * msg, WORD area) {
  1169.  
  1170.     WORD     i, 
  1171.             TO_AREAS,
  1172.             tofile,
  1173.             attr;
  1174.     
  1175.     BYTE    Hfile[128],
  1176.             Mfile[128],
  1177.             fbuf[128];
  1178.             
  1179. #if defined DEBUG
  1180.     printf("*Saving LOCAL\n");
  1181. #endif
  1182.     
  1183.     sprintf(Hfile, "%s.HDR", Areapath[area]);
  1184.     sprintf(Mfile, "%s.MSG", Areapath[area]);
  1185.     
  1186.     Hdr->mailer[7] |= SCANNED;
  1187.     
  1188.     /*
  1189.     **    Searching of this area is already opened.
  1190.     **    Speeded up the joyride.
  1191.     */
  1192.     
  1193.     if (dofastimport == TRUE) TO_AREAS = N_FASTAREAS;
  1194.     else TO_AREAS = N_AREAFILE;
  1195.     
  1196.     for (i=0; i < TO_AREAS; i++)
  1197.         if (lastareawritten[i] == area)
  1198.         {
  1199.             tofile = i;
  1200.             break;
  1201.         }
  1202.     
  1203.     /*
  1204.     **    No, it's a brand new one. Open it.
  1205.     */
  1206.     
  1207.     if (i >= TO_AREAS)
  1208.     {
  1209.         for (i=0; i < TO_AREAS; i++)
  1210.             if (LHDRfile[i] == FILEclosed &&
  1211.                 LMSGfile[i] == FILEclosed)
  1212.             {
  1213.                 tofile = i;
  1214.                 
  1215.                 if (!Openarea(i, Hfile, Mfile, Hdr))
  1216.                     terminate(10);
  1217.                 
  1218.                 LHDRfile[i] = FILEopen;
  1219.                 LMSGfile[i] = FILEopen;
  1220.                 lastareawritten[i] = area;
  1221.                 if (dofastimport == TRUE) {
  1222.                     h_Mstart[i] = Hdr->Mstart;
  1223.                     h_Mstart[i] += (ULONG)Hdr->size;
  1224.                     if (!h_one_item)
  1225.                         h_one_item = ((coreleft() / N_FASTTOTAL) / 2560 * 2048);
  1226.                     h_headerbuf[i] = (BYTE *)myalloc(h_one_item);
  1227.                     h_msgbuf[i] = (BYTE *)myalloc(h_one_item);
  1228.                     h_headeritems[i] = 0L;
  1229.                     h_msgitems[i] = 0L;
  1230.                     h_maxheader[i] = h_one_item;
  1231.                     h_maxmsg[i] = h_one_item;
  1232.                 }
  1233.                 break;
  1234.             }
  1235.         
  1236.         if (i >= TO_AREAS)
  1237.         {
  1238.             if (dofastimport == TRUE) {
  1239.                 Fwrite(fileno(LHDR[0]), h_headeritems[0],
  1240.                     h_headerbuf[0]);
  1241.                 Fwrite(fileno(LMSG[0]), h_msgitems[0],
  1242.                     h_msgbuf[0]);
  1243.                 h_headeritems[0] = 0L;
  1244.                 h_msgitems[0] = 0L;
  1245.             }
  1246.             fclose(LHDR[0]);
  1247.             fclose(LMSG[0]);
  1248.             
  1249.             sprintf (fbuf, "%s.HDR", Areapath[lastareawritten[0]]);
  1250.             
  1251.             attr = Fattrib(fbuf, 0, -1);
  1252.             
  1253.             if (attr != -33 && attr != -34 && attr & FA_ARCHIVE)
  1254.             {
  1255.                 if (intel(Sversion()) >= 0x0015U)
  1256.                     Fattrib(fbuf, 1, 0);
  1257.                 else
  1258.                     Fattrib(fbuf, 1, FA_ARCHIVE);
  1259.             }
  1260.                     
  1261.             tofile = 0;
  1262.             lastareawritten[0] = area;
  1263.             
  1264.             if (!Openarea(0, Hfile, Mfile, Hdr))
  1265.                 terminate(10);
  1266.             h_Mstart[0] = Hdr->Mstart;
  1267.             h_Mstart[0] += (ULONG)Hdr->size;
  1268.         }
  1269.     }
  1270.     else {
  1271.         if (dofastimport == TRUE) {
  1272.             Hdr->Mstart = h_Mstart[tofile];
  1273.             h_Mstart[tofile] += (ULONG) Hdr->size;
  1274.         } else {
  1275.             Hdr->Mstart = ftell(LMSG[tofile]);
  1276.         }
  1277.     }
  1278.     
  1279.     if (dofastimport == FALSE)
  1280.         fseek(LHDR[tofile], 0L, SEEK_END); 
  1281. /*
  1282.     hdrwrite(Hdr, sizeof(MSGHEADER), LHDR[tofile]);
  1283.     msgwrite(msg, (UWORD) strlen(msg), LMSG[tofile]);
  1284. */
  1285.     if (dofastimport == FALSE) {
  1286.         Fwrite(fileno(LMSG[tofile]), (UWORD)strlen(msg), msg);
  1287.         Fwrite(fileno(LHDR[tofile]), sizeof(MSGHEADER),  Hdr);
  1288.         /*
  1289.         hdrwrite(Hdr, sizeof(MSGHEADER), LHDR[tofile]);
  1290.         msgwrite(msg, (UWORD) strlen(msg), LMSG[tofile]);
  1291.         */
  1292.     } else {
  1293.         addhdrbuf(Hdr, (UWORD)sizeof(MSGHEADER), tofile);
  1294.         addmsgbuf(msg, (UWORD)strlen(msg), tofile);
  1295.     }
  1296. }
  1297.  
  1298. VOID dupesave(MSGHEADER *Hdr, BYTE *msg)
  1299. {
  1300.     BYTE Hfile[128], Mfile[128];
  1301.     
  1302.     if (!strlen(dupearea)) return;
  1303.     
  1304.     sprintf(Hfile, "%s.HDR", dupearea);
  1305.     sprintf(Mfile, "%s.MSG", dupearea);
  1306.     
  1307.     Hdr->size = (UWORD)strlen(msg);
  1308.     
  1309.     if((MSGHDR = fopen(Hfile, "r+b")) == NULL &&
  1310.         (MSGHDR = fopen(Hfile, "wb")) == NULL) {
  1311.             log_line(6,Logmessage[M__CANT_OPEN_HEADER], Hfile);
  1312.             return;
  1313.     }
  1314.     else fseek(MSGHDR, 0L, SEEK_END);
  1315.     
  1316.     if ((MSGMSG = fopen(Mfile, "r+b")) == NULL )
  1317.     {
  1318.         if ((MSGMSG = fopen(Mfile, "wb")) == NULL)
  1319.         {
  1320.             log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  1321.             fclose(MSGHDR);
  1322.             return;
  1323.         }
  1324.         else Hdr->Mstart = 0L;
  1325.     }
  1326.     else
  1327.     {
  1328.         fseek(MSGMSG, 0L, SEEK_END);
  1329.         Hdr->Mstart = ftell(MSGMSG);
  1330.     }
  1331.         
  1332.     Fwrite(fileno(MSGHDR), sizeof(MSGHEADER), Hdr);
  1333.     Fwrite(fileno(MSGMSG), strlen(msg), msg);
  1334.         
  1335.     fclose(MSGMSG);
  1336.     fclose(MSGHDR);
  1337.     MSGMSG = NULL;
  1338.     MSGHDR = NULL;
  1339. }
  1340.