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

  1. /*********************************************************************
  2.  
  3.                     Packet saver.
  4.                     
  5.     Program : IOSmail
  6.     Status  : Shareware
  7.     Source  : NOT free
  8.     Author  : Rinaldo Visscher
  9.     Date    : 1991
  10.     
  11.     Moved from inmail.c to this sources at 7-8-1991
  12.     
  13.     Save PKT's to disk.
  14.         
  15. *********************************************************************/
  16.  
  17.  
  18. #include        <stdio.h>
  19. #include        <stdlib.h>
  20. #include        <ext.h>
  21. #include        <string.h>
  22. #include        <errno.h>
  23. #include        <tos.h>
  24. #include     <mintbind.h>
  25.  
  26. #include        "portab.h"
  27. #include        "defs.h"
  28. #include        "ioslib.h"
  29. #include        "modules.h"
  30. #include        "lang.h"
  31. #include        "vars.h"
  32.  
  33. #include        "inmail.h"
  34. #include        "routing.h"
  35.  
  36. #include        "fastecho.h"
  37.  
  38. /*
  39. **    Buffers for msgbase.
  40. */
  41.  
  42. MLOCAL ULONG     h_one_item = 0L;
  43. MLOCAL ULONG    h_headeritems[N_FASTAREAS];
  44. MLOCAL ULONG    h_msgitems[N_FASTAREAS];
  45. MLOCAL ULONG    h_maxheader[N_FASTAREAS];
  46. MLOCAL ULONG    h_maxmsg[N_FASTAREAS];
  47. MLOCAL ULONG    h_Mstart[N_FASTAREAS];
  48. MLOCAL BYTE        *h_headerbuf[N_FASTAREAS];
  49. MLOCAL BYTE        *h_msgbuf[N_FASTAREAS];
  50. MLOCAL ULONG    h_time[N_FASTAREAS];
  51. MLOCAL ULONG    h_diff[N_FASTAREAS];
  52.  
  53. VOID addhdrbuf(VOID *text, UWORD len, WORD to );
  54. VOID addmsgbuf(VOID *text, UWORD len, WORD to );
  55. VOID save_to_local( MSGHEADER *Hdr, BYTE * msg, WORD area);
  56.  
  57. /*
  58. **    Save the message. In this part there are many files opened.
  59. **    Messages to local, netmail and trasarea are written down here. Also
  60. **    writing PKT files can be found here. For each of them there are
  61. **    seperated files open. 2 files for the netmail, 2 files for the trasharea
  62. **    16 files for the local area and 10 files for the packets.
  63. **    A total of 30 file channels are opened if using full power at
  64. **    importing.
  65. **
  66. **    It works, it speed up the diskaccess, esspecially if many area's must
  67. **    be imported and there are nodes and points connected to an area.
  68. **    Thanks to Jac that's found out this logic. I've build it up to an
  69. **    greater proportion.
  70. */
  71.  
  72. /*
  73. **    WARNING ........
  74. **    Don't make this function longer, the compiler seems to be
  75. **    have problems to generate an good function of it.
  76. */
  77.  
  78. VOID savemsg(BYTE whereto, MSGHEADER *Hdr, WORD area, BYTE *msg,
  79.                 UWORD zone, UWORD net, UWORD node, UWORD point)
  80. {
  81.     BYTE    Mfile[128],
  82.             Hfile[128],
  83.             system32[128],
  84.             *tbuffer,
  85.             pktpwd[10],
  86.             mtype = TYPE3d,
  87.             htype = defPKT,
  88.             wrtTOPT = FALSE,
  89.             wrtFMPT = FALSE;
  90.     WORD    i, j,
  91.             tofile;
  92.     UWORD    t_zone, t_net, t_node, t_point,
  93.             f_zone, f_net, f_node, f_point,
  94.             zo, ne, no, dzo, dne, dno, dpo,
  95.             TO_PKTS;
  96.     
  97.     Hdr->size = ((UWORD)strlen(msg));
  98.     
  99.     switch (whereto)
  100.     {            
  101.         case TOTRASH:
  102.         case TOPRIVATE:
  103.         
  104.             Hdr->mailer[7] |= SCANNED;
  105.             Hdr->flags |= RECEIVED;
  106.             
  107.             switch (whereto)
  108.             {
  109.                 case TOPRIVATE:
  110.                 
  111.                     if(!strlen(privatebox))
  112.                         log_line(6,Logmessage[M__PVTBOX_UNKNOWN]);
  113.                     
  114.                     sprintf(Hfile, "%s.HDR", privatebox);
  115.                     sprintf(Mfile, "%s.MSG", privatebox);
  116. #if defined EXTERNDEBUG
  117.         if (debugflag)
  118.         {
  119.             log_line(6,">Alloc tbuffer");
  120.             log_line(6,">Memory %ld", coreleft());
  121.         }
  122. #endif
  123.                     
  124.                     tbuffer = (BYTE *) myalloc(MAXMSGLENGTH + 80);
  125.                     
  126.                     if (area != -1)
  127.                     {
  128.                         sprintf(tbuffer, "\03AREA [%s]\n", Areaname[area]);
  129.                         strcat(tbuffer, msg);
  130.                     }
  131.                     else
  132.                     {
  133.                         sprintf(tbuffer, "\03AREA [MAIL]\n");
  134.                         strcat(tbuffer, msg);
  135.                     }
  136.                     
  137.                     Hdr->size = ((UWORD)strlen(tbuffer));
  138.                     break;
  139.                 
  140.                 case TOTRASH:
  141.                 
  142.                     Hdr->mailer[7] = 0;
  143.                     
  144.                     sprintf(Hfile, "%s.HDR", trasharea);
  145.                     sprintf(Mfile, "%s.MSG", trasharea);
  146. #if defined EXTERNDEBUG
  147.         if (debugflag)
  148.         {
  149.             log_line(6,">Alloc tbuffer");
  150.             log_line(6,">Memory %ld", coreleft());
  151.         }
  152. #endif
  153.                     
  154.                     tbuffer = (BYTE *) myalloc(strlen(msg)+20);
  155.                     strcpy(tbuffer, msg);
  156.                     break;
  157.             }
  158.             
  159.             /*
  160.             **    Area all open? If not, open it.
  161.             */
  162.             
  163.             if ((MSGHDR = fopen(Hfile, "r+b")) == NULL &&
  164.                 (MSGHDR = fopen(Hfile, "wb")) == NULL)
  165.             {
  166.                 log_line(6,Logmessage[M__CANT_OPEN_HEADER], Hfile);
  167.                 return;
  168.             }
  169.             else fseek(MSGHDR, 0L, SEEK_END);
  170.             
  171.             if ((MSGMSG = fopen(Mfile, "r+b")) == NULL)
  172.             {
  173.                 if ((MSGMSG = fopen(Mfile, "wb")) == NULL)
  174.                 {
  175.                     log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  176.                     fclose(MSGHDR);
  177.                     return;
  178.                 }
  179.                 else Hdr->Mstart = 0L;
  180.             }
  181.             else
  182.             {
  183.                 fseek(MSGMSG, 0L, SEEK_END);
  184.                 Hdr->Mstart = ftell(MSGMSG);
  185.             }
  186.             
  187.             hdrwrite(Hdr, sizeof(MSGHEADER), MSGHDR);
  188.             
  189.             msgwrite(tbuffer, (UWORD) strlen(tbuffer), MSGMSG);
  190. /*
  191.             Fwrite(fileno(MSGHDR), sizeof(MSGHEADER), Hdr);
  192.             Fwrite(fileno(MSGMSG), strlen(tbuffer), tbuffer);
  193. */                    
  194.             fclose(MSGMSG);
  195.             fclose(MSGHDR);
  196. #if defined EXTERNDEBUG
  197.         if (debugflag)
  198.         {
  199.             log_line(6,">Free tbuffer");
  200.             log_line(6,">Memory %ld", coreleft());
  201.         }
  202. #endif
  203.             free(tbuffer);
  204.             MSGMSG = NULL;
  205.             MSGHDR = NULL;
  206.             return;
  207.                     
  208.         case TOLOCAL:
  209.             save_to_local(Hdr, msg, area);
  210.                         
  211.             return;
  212.             
  213.         case TONETMAIL:
  214.         
  215.             sprintf(Mfile,"%s.MSG",mailarea);
  216.             sprintf(Hfile,"%s.HDR",mailarea);
  217.             
  218. #if defined DEBUG
  219.             printf("*Saving MAIL\n");
  220. #endif        
  221.             
  222.             /*
  223.             **    If the channel is already opened, we don't have to
  224.             **    open it again. This speed up some things.
  225.             */
  226.                 
  227.             if (!MMSG && !MHDR)
  228.             {
  229.                 if ((MMSG = fopen(Mfile, "r+b")) == NULL)
  230.                     if ((MMSG = fopen(Mfile, "wb")) == NULL)
  231.                     {
  232.                         log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  233.                         return;
  234.                     }
  235.                     else
  236.                         Hdr->Mstart = 0L;
  237.                 else
  238.                 {
  239.                     fseek(MMSG, 0L, SEEK_END);
  240.                     Hdr->Mstart = ftell(MMSG);
  241.                 }
  242.                 
  243.                 if ((MHDR = fopen(Hfile, "r+b")) == NULL &&
  244.                     (MHDR = fopen(Hfile,"wb")) == NULL)
  245.                     {
  246.                         log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  247.                         return;
  248.                     }
  249.                 else
  250.                     fseek(MHDR, 0L, SEEK_END);
  251.             }
  252.             else
  253.             {
  254.                 fseek(MMSG, 0L, SEEK_END);
  255.                 Hdr->Mstart = ftell(MMSG);
  256.             }
  257.             
  258.             hdrwrite(Hdr, sizeof(MSGHEADER), MHDR);
  259.             msgwrite(msg, (UWORD) strlen(msg)+1, MMSG);
  260. /*            
  261.             Fwrite(fileno(MHDR), sizeof(MSGHEADER), Hdr);
  262.             Fwrite(fileno(MMSG), strlen(msg)+1, msg);
  263. */            
  264.             fflush(MMSG);
  265.             fflush(MHDR);
  266.             
  267.             return;
  268.             
  269.         case TOPACKET:
  270.             
  271.             /*
  272.             **    Get the password of the destination.
  273.             */
  274.             
  275.             mtype = TYPE3d;
  276.             htype = defPKT;
  277.             
  278.             for (i=0; i < nkey; i++)
  279.                 if (Hdr->Dzone == pwd[i].zone &&
  280.                     Hdr->Dnet == pwd[i].net &&
  281.                     Hdr->Dnode == pwd[i].node &&
  282.                     Hdr->Dpoint == pwd[i].point)
  283.                 {
  284.                     strcpy(pktpwd, pwd[i].pwd);
  285.                     mtype = pwd[i].mtype;
  286.                     htype = pwd[i].htype;
  287.                     break;
  288.                 }
  289.             
  290.             
  291.             /*
  292.             **    If doing secure, watch all passwords mentioned in the
  293.             **    config file. If the destination is not found, break of
  294.             **    and warn the user of an illegal user of this area.
  295.             **    Then return to homebase.
  296.             */
  297.             
  298.             if (dosecure && i >= nkey)
  299.             {
  300.                 if (area >= 0)
  301.                 {
  302.                     log_line(6,Logmessage[M__NODE_UNKNOWN],
  303.                         Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  304.                     log_line(2,Logmessage[M__CHECK_PWDS],
  305.                         Areaname[area]);
  306.                 }
  307.                 else
  308.                 {
  309.                     log_line(6,Logmessage[M__NODE_UNKNOWN_NETMAIL],
  310.                         Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  311.                     log_line(2,Logmessage[M__CHECK_CONFIG]);
  312.                 }
  313.                 
  314.                 return;
  315.             }
  316.             
  317.             dzo = t_zone = Hdr->Dzone;
  318.             dne = t_net = Hdr->Dnet;
  319.             dno = t_node = Hdr->Dnode;
  320.             dpo = t_point = Hdr->Dpoint;
  321.             
  322.             zo = f_zone = Hdr->Ozone;
  323.             ne = f_net = Hdr->Onet;
  324.             no = f_node = Hdr->Onode;
  325.             f_point = Hdr->Opoint;
  326.             
  327.             /*
  328.             **    If point then 3d. Not if point using 4d address.
  329.             **    If netmail, don't change the messageheader.
  330.             **    zo,ne,no,dzo etc. contains the fakenetnumer of
  331.             **    the points.
  332.             */
  333.             
  334.             if (Hdr->Dpoint && mtype == TYPE3d)
  335.             {
  336.                 for (i=0; i < nalias; i++)
  337.                     if (alias[i].zone == Hdr->Dzone &&
  338.                         alias[i].net == Hdr->Dnet &&
  339.                         alias[i].node == Hdr->Dnode)
  340.                     {
  341.                         dzo = Hdr->Dzone;
  342.                         
  343.                         if (area >= 0)
  344.                         {
  345.                             dne = Hdr->Dnet = alias[i].pointnet;
  346.                             dno = Hdr->Dnode = Hdr->Dpoint;
  347.                             Hdr->Dpoint = dpo = 0;
  348.                         }
  349.                         else
  350.                         {
  351.                             dne = alias[i].pointnet;
  352.                             dno = Hdr->Dpoint;
  353.                             dpo = 0;
  354.                         }
  355.                         
  356.                         break;
  357.                     }
  358.                 
  359.                 if (i >= nalias)
  360.                 {
  361.                     htype = FRONTDOOR;
  362.                     mtype = TYPE4d;
  363.                     wrtTOPT = TRUE;
  364.                 }
  365.             }
  366.             
  367.             if (Hdr->Opoint && mtype == TYPE3d)
  368.             {
  369.                 for (i=0; i < nalias; i++)
  370.                     if (alias[i].zone == Hdr->Ozone &&
  371.                         alias[i].net == Hdr->Onet &&
  372.                         alias[i].node == Hdr->Onode)
  373.                     {
  374.                         zo = Hdr->Ozone;
  375.                         
  376.                         if (area >= 0)
  377.                         {
  378.                             ne = Hdr->Onet = alias[i].pointnet;
  379.                             no = Hdr->Onode= Hdr->Opoint;
  380.                             Hdr->Opoint = 0;
  381.                         }
  382.                         else
  383.                         {
  384.                             ne = alias[i].pointnet;
  385.                             no = Hdr->Opoint;
  386.                         }
  387.                         
  388.                         break;
  389.                     }
  390.                 
  391.                 if (i >= nalias)
  392.                 {
  393.                     htype = FRONTDOOR;
  394.                     mtype = TYPE4d;
  395.                     wrtFMPT = TRUE;
  396.                 }
  397.             }
  398.             
  399.             /*
  400.             **    Create filename.
  401.             */
  402. /*            
  403.             xsprintf(system32, "%02z%03z%03z.%02z", dzo, dne, dno, dpo);
  404. */
  405.             xsprintf(system32, "%02z%03z%03z.%02z", t_zone, t_net, 
  406.                                                     t_node, t_point);
  407.             
  408.             /*
  409.             **    If new file create packet header.
  410.             */
  411.             
  412.             switch (htype)
  413.             {
  414.                 case FTS_TYPE:
  415.                 
  416.                     fts_TYPE:
  417.                     create_pkt_hdr(zo, ne, no, dzo, dne, dno, pktpwd);
  418.                     break;
  419.                     
  420.                 /*
  421.                 **    Create an packetheader for frontdoor.
  422.                 */
  423.                 
  424.                 case TYPE_2_1:
  425.                 case FRONTDOOR:
  426.                 
  427.                     create_pkt_fnd(f_zone, f_net, f_node, f_point,
  428.                         t_zone, t_net, t_node, t_point, pktpwd);
  429.                     break;
  430.  
  431.                 case TYPE_2_2:    
  432.                     create_pkt_2_2(f_zone, f_net, f_node, f_point,
  433.                         t_zone, t_net, t_node, t_point, pktpwd, org_domain, dest_domain);
  434.                     break;
  435.                 
  436.                 default:
  437.                 
  438.                     log_line(6,Logmessage[M__COMPILER_ERROR]);
  439.                     goto fts_TYPE;
  440.             }
  441.             
  442. #if defined DEBUG
  443.             printf("*Writing PKT for %u:%u/%u.%u\n", Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  444. #endif        
  445.  
  446. /*
  447. **    Als MultiTos draait is het misschien mogelijk dat er meerder
  448. **    file kanalen geopend kunnen worden. Als dit hoger is dan het
  449. **    aantal in N_PKTFILE, dan is het maximale N_PKTFILE.
  450. */
  451.             
  452.             if (MTask == MULTITOS) {
  453.                 TO_PKTS = (UWORD) Files_to_open / 3;
  454.                 if (TO_PKTS < (N_PKTFILE / 4) ) {
  455.                     log_line (6, "+Can't progress with a lack of files.");
  456.                     terminate (-2);
  457.                 }
  458.                 if (TO_PKTS > N_MAXPKT) TO_PKTS = N_MAXPKT;
  459.                 if (dofastimport) TO_PKTS = N_PKTFILE / 2;                
  460.             } else {
  461.                 if (dofastimport) TO_PKTS = N_PKTFILE / 2;
  462.                 else TO_PKTS = N_PKTFILE;
  463.             }
  464.  
  465.             /*
  466.             **    Trying to find the channel of this node or point.
  467.             **    If not found search for an NULL filepointer.
  468.             */
  469.             
  470.             for (i=0; i < TO_PKTS; i++)
  471.                 if (PKTzone[i] == t_zone &&
  472.                     PKTnet[i] == t_net &&
  473.                     PKTnode[i] == t_node &&
  474.                     PKTpoint[i] == t_point)
  475.                 {
  476.                     tofile = i;
  477.                     fseek(PKTfile[i], 0L, SEEK_END); 
  478.                     break;
  479.                 }
  480.             
  481.             /*
  482.             **    This node or point have no channel.
  483.             **    Searching for an free NULL filepointer.
  484.             */
  485.             
  486.             if (i >= TO_PKTS)
  487.             {
  488.                 for (i=0; i < TO_PKTS; i++)
  489.                     if (PKTchannel[i] == FILEclosed)
  490.                     {
  491.                         tofile = i;
  492.                         PKTchannel[i] = FILEopen;
  493.                         if (Openpkt(i, system32, Hdr)) 
  494.                             break;
  495.                         return;
  496.                     }
  497.                 
  498.                 /*
  499.                 **    If all files are opened, try to find an channel
  500.                 **    that's not used. Logic :
  501.                 **
  502.                 **    Compare the nodes and points that's are connected
  503.                 **    to this area with the nodes and point that are
  504.                 **    using an channel. If found break of and continue;
  505.                 **    If not close this channel and open it again for
  506.                 **    the new node or point. If all using this channel
  507.                 **    close the first one.
  508.                 **
  509.                 **    I speeded the diskwriting up mother.
  510.                 */
  511.                 
  512.                 if (i >= TO_PKTS)
  513.                 {
  514.                     if (area >= 0)
  515.                     {
  516.                         for (j=0; j < TO_PKTS; j++)
  517.                         {
  518.                             for (i=0; Tozone[area][i] != (UWORD)(-1); i++)
  519.                                 if (Tozone[area][i] == PKTzone[j] &&
  520.                                     Tonet[area][i] == PKTnet[j] &&
  521.                                     Tonode[area][i] == PKTnode[j] &&
  522.                                     Topoint[area][i] == PKTpoint[j])
  523.                                     break;
  524.                             
  525.                             if (Tozone[area][i] == (UWORD)(-1))
  526.                             {
  527.                                 fseek(PKTfile[j], 0L, SEEK_END);
  528.                                 
  529.                                 putc(0, PKTfile[j]);
  530.                                 putc(0, PKTfile[j]);
  531.                                 fclose(PKTfile[j]);
  532.                                 
  533.                                 tofile = j;
  534.                                 if (!Openpkt(j, system32, Hdr)) return;
  535.                                 break;
  536.                             }
  537.                         }
  538.                     }
  539.                     else j = TO_PKTS;
  540.                     
  541.                     /*
  542.                     **    Brrrr.... close the first channel. There are
  543.                     **    so many connected.
  544.                     **
  545.                     **    It slow down the process.
  546.                     */
  547.                     
  548.                     if (j >= TO_PKTS )
  549.                     {
  550.                         fseek(PKTfile[0], 0L, SEEK_END);
  551.                         putc(0, PKTfile[0]);
  552.                         putc(0, PKTfile[0]);
  553.                         fclose(PKTfile[0]);
  554.                         tofile = 0;
  555.                         if (!Openpkt(0, system32, Hdr)) return;
  556.                         
  557.                     }
  558.                 }
  559.             }
  560.             
  561.             /*
  562.             **    This node or point is now in use of this channel.
  563.             */
  564.             
  565.             PKTzone[tofile] = t_zone;
  566.             PKTnet[tofile] = t_net;
  567.             PKTnode[tofile] = t_node;
  568.             PKTpoint[tofile] = t_point;
  569.             
  570.             pmsg.pm_ver = intel(2);
  571.             
  572.             if (MSGheader4d == TRUE || htype == FRONTDOOR ||
  573.                 htype == TYPE_2_1 || htype == TYPE_2_2) {
  574.                 pmsg.pm_dnet = intel(t_net);
  575.                 pmsg.pm_dnode = intel(t_node);
  576.                 pmsg.pm_onet = intel(f_net);
  577.                 pmsg.pm_onode = intel(f_node);
  578.             } else {
  579.                 pmsg.pm_dnet = intel(Hdr->Dnet);
  580.                 pmsg.pm_dnode = intel(Hdr->Dnode);
  581.                 pmsg.pm_onet = intel(Hdr->Onet);
  582.                 pmsg.pm_onode = intel(Hdr->Onode);
  583.             }
  584.             
  585.             pmsg.pm_attr = intel (Hdr->flags &=~ ALLBITS);
  586.             
  587.             pmsg.pm_cost = 0;
  588.             
  589.             /*
  590.             **    Writing the pkt message header.
  591.             */
  592.             
  593.             hdrwrite(&pmsg, sizeof(struct _pktmsg), PKTfile[tofile]);
  594. /*
  595.             Fwrite(fileno(PKTfile[tofile]), sizeof(struct _pktmsg), &pmsg);
  596. */                        
  597.             pktwrite(Hdr->time, (UWORD) strlen(Hdr->time), PKTfile[tofile]);
  598.             pktwrite(Hdr->to, (UWORD) strlen(Hdr->to), PKTfile[tofile]);
  599.             pktwrite(Hdr->from, (UWORD) strlen(Hdr->from), PKTfile[tofile]);
  600.             pktwrite(Hdr->topic, (UWORD) strlen(Hdr->topic), PKTfile[tofile]);
  601.  
  602.             if (wrtTOPT) {
  603.                 fprintf(PKTfile[tofile], "\01TOPT %u\n", Hdr->Dpoint);
  604.             }
  605.             if (wrtFMPT) {
  606.                 fprintf(PKTfile[tofile], "\01FMPT %u\n", Hdr->Opoint);
  607.             }
  608.                         
  609.             /*
  610.             **    Put the message on disk.
  611.             */
  612.             
  613.             strcat (msg, "\n");
  614.             pktwrite(msg, (UWORD) strlen(msg), PKTfile[tofile]);
  615.             
  616.             break;
  617.             
  618.         case NETMAILPACKET:
  619.             
  620.             /*
  621.             **    Get the pasword of the destination.
  622.             */
  623.             
  624.             mtype = TYPE3d;
  625.             htype = defPKT;
  626.             
  627.             for (i=0; i < nkey; i++)
  628.             {
  629.                 if (zone == pwd[i].zone && net == pwd[i].net &&
  630.                     node == pwd[i].node && point == pwd[i].point)
  631.                 {
  632.                     strcpy(pktpwd, pwd[i].pwd);
  633.                     
  634.                     mtype = pwd[i].mtype;
  635.                     htype = pwd[i].htype;
  636.                     
  637. #if defined OUTDEBUG
  638.                     if (dooutdeb) log_line(6,">Have password %u:%u/%u.%u - %s", pwd[i].zone, pwd[i].net, pwd[i].node, pwd[i].point, pwd[i].pwd);
  639. #endif
  640.                     break;
  641.                 }
  642.             }
  643.             
  644.             /*
  645.             **    If crashmail, delete password.
  646.             */
  647.             
  648.             if (Hdr->flags & CRASH && i >= nkey)
  649.                 for (i=0; i < (WORD) strlen(pktpwd); i++)
  650.                     pktpwd[i] = EOS;
  651.             
  652.             if (i >= nkey)
  653.             {
  654. #if defined OUTDEBUG
  655.                 if (dooutdeb) log_line(6,">No password for %u:%u/%u.%u", zone, net, node, point);
  656. #endif
  657.                 *pktpwd = EOS;
  658.                 
  659.                 if (point) {
  660.                     for (i=0; i < nalias; i++) {
  661.                         if (zone == alias[i].zone && 
  662.                         net == alias[i].net &&
  663.                         node == alias[i].node) {
  664.                             log_line (6, Logmessage[M__F_ECHO_POINT_UNKNOWN], point);
  665.                             return;
  666.                         }
  667.                     }
  668.                 }            
  669.             }
  670.             
  671.             /*
  672.             **    If no crashmail, take normal.
  673.             */
  674.             
  675.             if (!(Hdr->flags & CRASH))
  676.             {
  677.                 if (point)
  678.                 {
  679.                     dzo = zone;
  680.                     
  681.                     for (i=0; i < nalias; i++)
  682.                         if (zone == alias[i].zone &&
  683.                             net == alias[i].net &&
  684.                             node == alias[i].node)
  685.                         {
  686.                             dne = alias[i].pointnet;
  687.                             dno = point;
  688.                             dpo = 0;
  689. #if defined OUTDEBUG
  690.                             if (dooutdeb) log_line(6,">Point - dzo, dne, dno %u:%u/%u", dzo, dne, dno);
  691. #endif
  692.                             break;
  693.                         }
  694.                 }
  695.                 else
  696.                 {
  697.                     dzo = zone;
  698.                     dne = net;
  699.                     dno = node;
  700.                     dpo = 0;
  701.                     
  702. #if defined OUTDEBUG
  703.                     if (dooutdeb) log_line(6,">No point - dzo,dne,dno %u:%u/%u", dzo, dne, dno);
  704. #endif
  705.                 }
  706.                 
  707.                 t_zone = zone;
  708.                 t_net = net;
  709.                 t_node = node;
  710.                 t_point = point;
  711.                 
  712.                 /*
  713.                 **    Get origin address from ourself.
  714.                 */
  715.                 
  716.                 for (i=0; i < nalias; i++)
  717.                     if (zone == alias[i].zone &&
  718.                         node == alias[i].node &&
  719.                         net == alias[i].net)
  720.                     {
  721.                         if (!alias[i].point)
  722.                         {
  723.                             zo = alias[i].zone;
  724.                             ne = alias[i].net;
  725.                             no = alias[i].node;
  726.                         }
  727.                         else
  728.                         {
  729.                             zo = alias[i].zone;
  730.                             ne = alias[i].pointnet;
  731.                             no = alias[i].point;
  732.                         }
  733.                         
  734.                         f_zone = alias[i].zone;
  735.                         f_net = alias[i].net;
  736.                         f_node = alias[i].node;
  737.                         f_point = alias[i].point;
  738.                         
  739.                         break;
  740.                     }
  741.                 
  742.                 if (i >= nalias)
  743.                 {
  744.                     for (i=0; i < nalias; i++)
  745.                         if (zone == alias[i].zone)
  746.                         {
  747.                             if (!alias[i].point)
  748.                             {
  749.                                 zo = alias[i].zone;
  750.                                 ne = alias[i].net;
  751.                                 no = alias[i].node;
  752.                             }
  753.                             else
  754.                             {
  755.                                 zo = alias[i].zone;
  756.                                 ne = alias[i].pointnet;
  757.                                 no = alias[i].point;
  758.                             }
  759.                             
  760.                             f_zone = alias[i].zone;
  761.                             f_net  = alias[i].net;
  762.                             f_node = alias[i].node;
  763.                             f_point= alias[i].point;
  764.                             
  765.                             break;
  766.                         }
  767.                     
  768.                     if (i >= nalias)
  769.                         if (!alias[0].point)
  770.                         {
  771.                             zo = f_zone = alias[0].zone;
  772.                             ne = f_net = alias[0].net;
  773.                             no = f_node = alias[0].node;
  774.                             f_point = alias[0].point;
  775.                         }
  776.                         else
  777.                         {
  778.                             zo = f_zone = alias[0].zone;
  779.                             ne = alias[0].pointnet;
  780.                             no = f_point = alias[0].point;
  781.                             f_net = alias[0].net;
  782.                             f_node = alias[0].node;
  783.                         }
  784.                 }    
  785.             }
  786.             else
  787.             {
  788.             
  789.                 /*
  790.                 **    Crashmail.
  791.                 */
  792.                 
  793.                 if (Hdr->Opoint)
  794.                 {
  795.                     for (i=0; i < nalias; i++)
  796.                         if (Hdr->Ozone == alias[i].zone &&
  797.                             Hdr->Onet == alias[i].net &&
  798.                             Hdr->Onode == alias[i].node &&
  799.                             Hdr->Opoint == alias[i].point)
  800.                         {
  801.                             zo = alias[i].zone;
  802.                             ne = alias[i].pointnet;
  803.                             no = alias[i].point;
  804.                             break;
  805.                         }
  806.                     
  807.                     if (i >= nalias)
  808.                     {
  809.                         zo = Hdr->Ozone;
  810.                         ne = Hdr->Onet;
  811.                         no = Hdr->Onode;
  812.                     }
  813.                 }
  814.                 else
  815.                 {
  816.                     zo = Hdr->Ozone;
  817.                     ne = Hdr->Onet;
  818.                     no = Hdr->Onode;
  819.                 }
  820.                 
  821.                 f_zone = Hdr->Ozone;
  822.                 f_net = Hdr->Onet;
  823.                 f_node = Hdr->Onode;
  824.                 f_point = Hdr->Opoint;
  825.                 
  826.                 if (Hdr->Dpoint) {
  827.                     for (i=0; i < nalias; i++) {
  828.                         if (Hdr->Dnet == alias[i].net &&
  829.                             Hdr->Dnode == alias[i].node &&
  830.                             Hdr->Dzone == alias[i].zone)
  831.                         {
  832.                             if (!Hdr->Opoint && Hdr->flags & CRASH) {
  833.                                 if(crashPoint(Hdr->Dzone, Hdr->Dnet,
  834.                                 Hdr->Dnode, Hdr->Dpoint) == FALSE || donewbink == FALSE) {
  835.                                     log_line(4,Logmessage[M__CANT_CRASH_POINT]);
  836.                                     return;
  837.                                 }
  838.                             } else {
  839.                                 log_line(4,Logmessage[M__CANT_CRASH_POINT]);
  840.                                 return;
  841.                             }
  842.                         }
  843.                     }
  844.                 }
  845.                 
  846.                 dzo = t_zone = Hdr->Dzone;
  847.                 dne = t_net = Hdr->Dnet;
  848.                 dno = t_node = Hdr->Dnode;
  849.                 dpo = t_point = Hdr->Dpoint;
  850.             }
  851.             
  852. #if defined EXTERNDEBUG
  853.             if (debugflag) log_line(6,">Now have as destination PKT %u:%u/%u.%u", dzo, dne, dno, dpo);
  854. #endif
  855.             
  856.             /*
  857.             **    Create filename.
  858.             */
  859.             
  860.             xsprintf(system32, "%02z%03z%03z.%02z", dzo, dne, dno, dpo);
  861.             
  862.             /*
  863.             **    If new file create packet header.
  864.             */
  865.             
  866.             switch (htype)
  867.             {
  868.                 case FTS_TYPE:
  869.                 
  870.                     FTS_type:
  871.                     create_pkt_hdr(zo, ne, no, dzo, dne, dno, pktpwd);
  872.                     break;
  873.                     
  874.                 /*
  875.                 **    Create an packetheader for frontdoor.
  876.                 */
  877.                 
  878.                 case TYPE_2_1:
  879.                 case FRONTDOOR:
  880.                 
  881.                     create_pkt_fnd(f_zone, f_net, f_node, f_point,
  882.                         t_zone, t_net, t_node, t_point, pktpwd);
  883.                     break;
  884.  
  885.                 case TYPE_2_2:
  886.                     create_pkt_2_2(f_zone, f_net, f_node, f_point,
  887.                         t_zone, t_net, t_node, t_point, pktpwd, org_domain, dest_domain);
  888.                     break;
  889.                 
  890.                 default:
  891.                 
  892.                     log_line(6,Logmessage[M__COMPILER_ERROR]);
  893.                     goto FTS_type;
  894.             }
  895.             
  896. #if defined DEBUG
  897.             printf("*Writing PKT for %u:%u/%u\n", Hdr->Dzone, Hdr->Dnet, Hdr->Dnode);
  898. #endif        
  899.             
  900.             /*
  901.             **    Trying to find the channel of this node or point.
  902.             **    If not found search for an NULL filepointer.
  903.             */
  904.  
  905.             if (MTask == MULTITOS) {
  906.                 TO_PKTS = (UWORD)Files_to_open / 3;
  907.                 if (TO_PKTS < (N_PKTFILE / 4)) {
  908.                     log_line (6, "-Can't progress with a lack of files.");
  909.                     terminate (-2);
  910.                 }
  911.                 if (TO_PKTS > N_MAXPKT) TO_PKTS = N_MAXPKT;
  912.                 if (dofastimport) TO_PKTS = N_PKTFILE / 2;
  913.             } else {
  914.                 if (dofastimport) TO_PKTS = N_PKTFILE / 2;
  915.                 else TO_PKTS = N_PKTFILE;
  916.             }
  917.             
  918.             for (i=0; i < TO_PKTS; i++)
  919.                 if (PKTzone[i] == t_zone && PKTnet[i] == t_net &&
  920.                     PKTnode[i] == t_node && PKTpoint[i] == t_point)
  921.                 {
  922.                     tofile = i;
  923.                     fseek(PKTfile[i], 0L, SEEK_END);
  924.                     break;
  925.                 }
  926.             
  927.             /*
  928.             **    This node or point have no channel.
  929.             **    Searching for an free NULL filepointer.
  930.             */
  931.             
  932.             if (i >= TO_PKTS)
  933.             {
  934.                 for (i=0; i < TO_PKTS; i++)
  935.                     if (PKTchannel[i] == FILEclosed)
  936.                     {
  937.                         tofile = i;
  938.                         PKTchannel[i] = FILEopen;
  939.                         
  940.                         if (Openpkt(i, system32, Hdr)) 
  941.                             break;
  942.                         return;
  943.                     }
  944.                 
  945.                 if (i >= TO_PKTS )
  946.                 {
  947.                     fseek(PKTfile[0], 0L, SEEK_END);
  948.                     putc(0,PKTfile[0]);
  949.                     putc(0,PKTfile[0]);
  950.                     fclose(PKTfile[0]);
  951.                     
  952.                     tofile = 0;
  953.                     if (!Openpkt(0, system32, Hdr)) return;
  954.                 }
  955.             }
  956.             
  957.             /*
  958.             **    This node or point is now in use of this channel.
  959.             */
  960.             
  961.             PKTzone[tofile] = t_zone;
  962.             PKTnet[tofile] = t_net;
  963.             PKTnode[tofile] = t_node;
  964.             PKTpoint[tofile] = t_point;
  965.             
  966.             pmsg.pm_ver = intel(2);
  967.             pmsg.pm_dnet = intel(Hdr->Dnet);
  968.             pmsg.pm_dnode = intel(Hdr->Dnode);
  969.             pmsg.pm_onet = intel(Hdr->Onet);
  970.             pmsg.pm_onode = intel(Hdr->Onode);
  971.             
  972.             pmsg.pm_attr = intel(Hdr->flags &= ~ALLBITS);
  973.                 
  974.             pmsg.pm_cost = 0;
  975.             
  976.             /*
  977.             **    Writing the pkt message header.
  978.             */
  979.             
  980. #if defined OUTDEBUG
  981.             if (dooutdeb) log_line(6,">Writing packet");
  982. #endif
  983.  
  984.             hdrwrite(&pmsg, sizeof(struct _pktmsg), PKTfile[tofile]);
  985. /*
  986.             Fwrite(fileno(PKTfile[tofile]), sizeof(struct _pktmsg), &pmsg);
  987. */                        
  988.             pktwrite(Hdr->time, (UWORD) strlen(Hdr->time), PKTfile[tofile]);
  989.             pktwrite(Hdr->to, (UWORD) strlen(Hdr->to), PKTfile[tofile]);
  990.             pktwrite(Hdr->from, (UWORD) strlen(Hdr->from), PKTfile[tofile]);
  991.             pktwrite(Hdr->topic, (UWORD) strlen(Hdr->topic), PKTfile[tofile]);
  992.     
  993.             /*
  994.             **    INTL needed?
  995.             */
  996.             
  997.             if (Hdr->Ozone != f_zone && Hdr->Dzone != t_zone)
  998.             {
  999.                 if (strncmp(msg,"AREA:",5))
  1000.                 {
  1001.                     if (!get_interzone(msg, &f_zone, &f_net, &f_node, &t_zone, &t_net, &t_node))
  1002.                     {
  1003.                         write_INTL:
  1004.                         
  1005. #if defined OUTDEBUG
  1006.                         if (dooutdeb) log_line(6,">Writing INTL");
  1007. #endif
  1008.                         fprintf(PKTfile[tofile], "\01INTL %u:%u/%u %u:%u/%u\n",
  1009.                             Hdr->Ozone, Hdr->Onet, Hdr->Onode,
  1010.                             Hdr->Dzone, Hdr->Dnet, Hdr->Dnode);
  1011.  
  1012.                     }
  1013.                     else
  1014.                     {
  1015.                         if (f_zone != Hdr->Ozone &&
  1016.                             t_zone != Hdr->Dzone)
  1017.                         {
  1018.                             msg = strip_line(msg, "\01INTL ");
  1019.                             goto write_INTL;
  1020.                         }
  1021.                     }
  1022.                 }
  1023.             }
  1024.  
  1025.             strcat (msg, "\n");
  1026.             pktwrite(msg, (UWORD) strlen(msg), PKTfile[tofile]);
  1027.             
  1028.             break;
  1029.         
  1030.         default:
  1031.         
  1032.             log_line(6,Logmessage[M__COMPILER_ERROR]);
  1033.             break;
  1034.     }
  1035. }
  1036.  
  1037. VOID ToDataBase(MSGHEADER *Hdr, BYTE *msg, WORD area) {
  1038.     WORD i;
  1039.     BYTE 
  1040.          Hfile[128],
  1041.          Mfile[128];
  1042.     
  1043.     SMALLBASE sbase;
  1044.     
  1045.     for (i=0; i < nfdbase; i++) {
  1046.         if (newmatch( (FDcompare[nfdbase] == FD_FROM) ? Hdr->from : Hdr->to, ToBase[nfdbase])) {
  1047.             sprintf(Hfile,"%sDATA.HR",FDbase);
  1048.             sprintf(Mfile,"%sDATA.MG",FDbase);
  1049.             
  1050.             if ((MSGHDR = fopen(Hfile,"r+b")) == NULL ) {
  1051.                 if ((MSGHDR = fopen(Hfile,"wb"))  == NULL) {
  1052.                     log_line(5,Logmessage[M__CANT_OPEN_B_DBASE]);
  1053.                     return;
  1054.                 }
  1055.             } else fseek(MSGHDR, 0L, SEEK_END);
  1056.             
  1057.             if ((MSGMSG = fopen(Mfile, "r+b")) == NULL) {
  1058.                 if ((MSGMSG = fopen(Mfile, "wb")) == NULL){
  1059.                     log_line(6,Logmessage[M__CANT_OPEN_M_DBASE]);
  1060.                     fclose(MSGHDR);
  1061.                     return;
  1062.                 }
  1063.                 else sbase.Mstart = 0L;
  1064.             }
  1065.             else {
  1066.                 fseek(MSGMSG, 0L, SEEK_END);
  1067.                 sbase.Mstart = ftell(MSGMSG);
  1068.             }
  1069.             
  1070.             strcpy (sbase.from, Hdr->from);
  1071.             strcpy (sbase.to, Hdr->to);
  1072.             strcpy (sbase.topic, Hdr->topic);
  1073.             strcpy (sbase.time, Hdr->time);
  1074.             strncpy(sbase.area, Areaname[area],39);
  1075.             sbase.size = (UWORD) strlen(msg);
  1076.             
  1077.             hdrwrite(&sbase, sizeof(SMALLBASE), MSGHDR);
  1078.             
  1079.             msgwrite(msg, (UWORD) strlen(msg), MSGMSG);
  1080.             
  1081.             fclose(MSGMSG);
  1082.             fclose(MSGHDR);
  1083.             return;
  1084.         }
  1085.     }
  1086. }
  1087.  
  1088.  
  1089. VOID SaveMEMtoDisk (WORD number, BYTE What) {
  1090.     FILE *Hfp, *Mfp;
  1091.     BYTE Hfile[128], Mfile[128];
  1092.     WORD attr;
  1093.  
  1094. /*
  1095. **    Open het kanaal voor deze area.
  1096. */
  1097.  
  1098.     sprintf (Hfile, "%s.HDR", Areapath[ lastareawritten[number] ]);
  1099.     sprintf (Mfile, "%s.MSG", Areapath[ lastareawritten[number] ]);
  1100.  
  1101.     if (What == saveALL || What == saveHDR) {
  1102.         if ((Hfp = fopen (Hfile, "r+b")) == NULL) {
  1103.             if ((Hfp = fopen (Hfile, "wb")) == NULL) {
  1104.                 log_line(5,Logmessage[M__CANT_OPEN_HEADER], Mfile);
  1105.                 terminate (10);
  1106.             }
  1107.         } else {
  1108.             fseek (Hfp, 0L, SEEK_END);
  1109.         }
  1110.     }
  1111.     
  1112.     if (What == saveALL || What == saveMSG) {
  1113.         if ((Mfp = fopen (Mfile, "r+b")) == NULL) {
  1114.             if ((Mfp = fopen (Mfile, "wb")) == NULL) {
  1115.                 log_line(5,Logmessage[M__CANT_OPEN_HEADER], Mfile);
  1116.                 terminate (10);
  1117.             }
  1118.         } else {
  1119.             fseek (Mfp, 0L, SEEK_END);
  1120.         }
  1121.     }
  1122.  
  1123. /*
  1124. **    Schrijf de inhoud van het geheugengebied welke door deze
  1125. **    area is gebruikt op disk.
  1126. */
  1127.     
  1128.     if (What == saveALL || What == saveHDR) {    
  1129.         Fwrite(fileno (Hfp), h_headeritems [number], h_headerbuf[number]);
  1130.         h_headeritems[number] = 0L;
  1131.         fclose (Hfp);
  1132.     }
  1133.     if (What == saveALL || What == saveMSG) {
  1134.         Fwrite(fileno (Mfp), h_msgitems [number], h_msgbuf [number]);
  1135.         h_msgitems [number] = 0L;
  1136.         fclose (Mfp);
  1137.     }
  1138.  
  1139. /*
  1140. **    Zet de attributen voor deze area, zodat deze niet opnieuw
  1141. **    gescand zal worden door IOSmail.
  1142. */
  1143.  
  1144.     if (What == saveALL || What == saveHDR) {
  1145.         attr = Fattrib(Hfile, 0, -1);
  1146.  
  1147.         if (attr != -33 && attr != -34 && attr & FA_ARCHIVE)
  1148.         {
  1149.             if (intel (Sversion()) >= 0x0015U)
  1150.                 Fattrib (Hfile, 1, 0);
  1151.             else
  1152.                 Fattrib (Hfile, 1, FA_ARCHIVE);
  1153.         }
  1154.     }
  1155. }
  1156.  
  1157. VOID addhdrbuf(VOID *text, UWORD len, WORD to ) {
  1158.     BYTE *buffer;
  1159.     
  1160.     if (h_headeritems[to] + len > h_maxheader[to]) {
  1161.         SaveMEMtoDisk (to, saveHDR);
  1162.         
  1163. /*        
  1164.         fseek(LHDR[to] , 0L, SEEK_END);
  1165.         Fwrite(fileno(LHDR[to]), h_headeritems[to], h_headerbuf[to]);
  1166.         h_headeritems[to] = 0L;
  1167. */
  1168.     }
  1169.     
  1170.     buffer = (BYTE *)text;
  1171.     
  1172.     while (len--) {
  1173.         h_headerbuf[to][h_headeritems[to]++] = *buffer++;
  1174.     }
  1175. }
  1176.  
  1177. VOID addmsgbuf(VOID *text, UWORD len, WORD to ) {
  1178.     BYTE *buffer;
  1179.     
  1180.     if (h_msgitems[to] + len > h_maxmsg[to]) {
  1181.         SaveMEMtoDisk (to, saveMSG);
  1182. /*    
  1183.         fseek(LMSG[to] , 0L, SEEK_END);
  1184.         Fwrite(fileno(LMSG[to]), h_msgitems[to], h_msgbuf[to]);
  1185.         h_msgitems[to] = 0L;
  1186. */
  1187.     }
  1188.     
  1189.     buffer = (BYTE *)text;
  1190.     
  1191.     while (len--) {
  1192.         h_msgbuf[to][h_msgitems[to]++] = *buffer++;
  1193.     }
  1194. }
  1195.  
  1196. VOID freemsgbuf() {
  1197.     WORD i;
  1198.     
  1199.     for (i=0; i < N_FASTAREAS; i++) {
  1200.         if (LHDRfile[i] == FILEopen) {
  1201.             if (h_headeritems[i]) {
  1202.                 SaveMEMtoDisk (i, saveHDR);
  1203.             }
  1204. #if defined EXTERNDEBUG
  1205.         if (debugflag)
  1206.         {
  1207.             log_line(6,">Free h_headerbuf[%d]",i);
  1208.             log_line(6,">Memory %ld", coreleft());
  1209.         }
  1210. #endif
  1211.             free(h_headerbuf[i]);
  1212.         }
  1213.         
  1214.         if (LMSGfile[i] == FILEopen) {
  1215.             if (h_msgitems[i]) {
  1216.                 SaveMEMtoDisk (i, saveMSG);
  1217.             }
  1218. #if defined EXTERNDEBUG
  1219.         if (debugflag)
  1220.         {
  1221.             log_line(6,">Free h_msgbuf[%d]",i);
  1222.             log_line(6,">Memory %ld", coreleft());
  1223.         }
  1224. #endif
  1225.             free(h_msgbuf[i]);
  1226.         }
  1227.         
  1228.     }
  1229.     h_one_item = 0L;
  1230. }
  1231.  
  1232. /*
  1233. **    Snelle schrijfroutine. Schrijf de berichten weg in het geheugen.
  1234. **    D.m.v. FastImport in de config word deze routine geaktiveerd.
  1235. */
  1236.  
  1237. void FASTsave (MSGHEADER *Hdr, BYTE *msg, WORD area) {
  1238.     BYTE Mfile [128],
  1239.           buffer[128];
  1240.           
  1241.     WORD    i,
  1242.             j,
  1243.             tofile;
  1244.     
  1245.     struct ffblk blk;
  1246.     
  1247.     Hdr->mailer[7] |= SCANNED;
  1248.     Hdr->flags &=~(DELETED);
  1249.  
  1250.     /*
  1251.     **    Bekijk of deze area al geopend is. Als deze area geopend is
  1252.     **    dan kan er gewoon doorgegaan worden zonder dat er een nieuwe
  1253.     **    area geopend dient te worden.
  1254.     */
  1255.     
  1256.     for (i=0; i < N_FASTAREAS; i++) {
  1257.         if (lastareawritten[i] == area) {
  1258.             tofile = i;
  1259.             break;
  1260.         }
  1261.     }
  1262.     
  1263.     /*
  1264.     **    Als er nog geen geopende file bestaat voor deze area,
  1265.     **    kijk dan of er nog gesloten kanalen zijn die geopend kunnen
  1266.     **    worden.
  1267.     */
  1268.     
  1269.     if (i >= N_FASTAREAS) {
  1270.         for (i=0; i < N_FASTAREAS; i++) {
  1271.             if (LHDRfile[i] == FILEclosed &&
  1272.                 LMSGfile[i] == FILEclosed)
  1273.             {
  1274.             
  1275.             /*
  1276.             **    Er bestaat nog een gesloten kanaal waar deze area
  1277.             **    gebruik van kan maken.
  1278.             */
  1279.             
  1280.                 tofile = i;
  1281.  
  1282.                 LHDRfile[i] = FILEopen;
  1283.                 LMSGfile[i] = FILEopen;
  1284.                 lastareawritten[i] = area;
  1285.  
  1286.                 sprintf (Mfile, "%s.MSG", Areapath [area] );
  1287.                 
  1288.                 if (findfirst (Mfile, &blk, 0)) {
  1289.                     Hdr ->Mstart = 0L;
  1290.                 } else {
  1291.                     Hdr->Mstart = blk.ff_fsize;
  1292.                 }
  1293.                 
  1294.                 /*
  1295.                 **    Alloc geheugen voor deze area.
  1296.                 */
  1297.  
  1298.                 h_Mstart[i] = Hdr->Mstart;
  1299. /*                h_Mstart[i] += (ULONG)Hdr->size; */
  1300.                 if (!h_one_item)
  1301.                     h_one_item = ((coreleft() / N_FASTTOTAL) / 2560 * 2048);
  1302.  
  1303.  
  1304. #if defined EXTERNDEBUG
  1305.         if (debugflag)
  1306.         {
  1307.             log_line(6,">Alloc h_headerbuf[%d]",i);
  1308.             log_line(6,">Alloc h_msgbuf[%d]",i);
  1309.             log_line(6,">Memory %ld", coreleft());
  1310.         }
  1311. #endif
  1312.  
  1313.                 if ( !i && h_one_item <= FAST_MEMORY_NEEDS) {
  1314.                     log_line (6, "-Can't do FastImport, no memory.");
  1315.                     terminate (-32);
  1316.                 } else if ( h_one_item <= FAST_MEMORY_NEEDS) {
  1317.                     log_line (6, "-No memory, switch off FastImport first. Closing channels.");
  1318.                     freemsgbuf();
  1319.                     terminate (-32);
  1320.                 }
  1321.                 
  1322.                 h_headerbuf[i] = (BYTE *)malloc(h_one_item);
  1323.                 h_msgbuf[i] = (BYTE *)malloc(h_one_item);
  1324.                 if (!h_msgbuf[i] || !h_headerbuf[i]) {
  1325.                     terminate (10);
  1326.                 }
  1327.                 
  1328.                 h_headeritems[i] = 0L;
  1329.                 h_msgitems[i] = 0L;
  1330.                 h_maxheader[i] = h_one_item;
  1331.                 h_maxmsg[i] = h_one_item;
  1332.                 break;
  1333.             }
  1334.         }
  1335.  
  1336.     /*
  1337.     **    Als er geen gesloten kanalen meer te vinden zijn,
  1338.     **    sluit dan een kanaal. Eerst dient dit kanaal weggeschreven
  1339.     **    te worden naar de area op disk waar deze zich bevind.
  1340.     **    Zoek naar de area waar het langste geen nieuwe berichten
  1341.     **    voor zijn binnengekomen. Die word het eerste gesloten,
  1342.     **    anders word het alleen aan en afsluiten van 1 kanaal.
  1343.     */
  1344.     
  1345.         if ( i >= N_FASTAREAS) {
  1346.  
  1347.             for (i=0; i < N_FASTAREAS; i++) {
  1348.                 h_diff[i] = time (NULL) - h_time[i];
  1349.             }
  1350.             
  1351.             i = 0;
  1352.             
  1353.             for (j = 1; j < N_FASTAREAS; j++) {
  1354.                 if ( h_diff [i] < h_diff [j]) {
  1355.                     i = j;
  1356.                 } 
  1357.             }
  1358.             
  1359.             SaveMEMtoDisk (i, saveALL);
  1360.  
  1361.         /*
  1362.         **    Zet het areanummer op area waar geen berichten meer van zijn.
  1363.         */
  1364.                     
  1365.             tofile = i;
  1366.             lastareawritten[i] = area;
  1367.  
  1368.             sprintf (Mfile, "%s.MSG", Areapath [area] );
  1369.             
  1370.             if (findfirst (Mfile, &blk, 0)) {
  1371.                 Hdr ->Mstart = 0L;
  1372.             } else {
  1373.                 Hdr->Mstart = blk.ff_fsize;
  1374.             }
  1375.     
  1376.         /*
  1377.         **    Reset de waarden zodat er opnieuw begonnen kan worden.
  1378.         */
  1379.     
  1380.             h_Mstart[i] = Hdr->Mstart;    
  1381.         }
  1382.     }
  1383.  
  1384.     Hdr->Mstart = h_Mstart [tofile];
  1385.     h_Mstart [tofile] += Hdr->size;
  1386.  
  1387.     h_time [tofile] = time (NULL);
  1388.     
  1389.     addhdrbuf(Hdr, (UWORD)sizeof(MSGHEADER), tofile);
  1390.     addmsgbuf(msg, (UWORD)strlen(msg), tofile);
  1391. }
  1392.  
  1393. /*
  1394. **    Schrijf de berichten via de oude methode weg naar disk.
  1395. */
  1396.  
  1397. VOID save_to_local( MSGHEADER *Hdr, BYTE * msg, WORD area) {
  1398.  
  1399.     WORD     i, 
  1400.             TO_AREAS,
  1401.             tofile, attr;
  1402.  
  1403.     BYTE    Mfile[128], Hfile[128],
  1404.             fbuf[128];
  1405.     
  1406.     
  1407. #if defined DEBUG
  1408.     printf("*Saving LOCAL\n");
  1409. #endif
  1410.     
  1411.     if (dofastimport == TRUE) {
  1412.         FASTsave (Hdr, msg, area);
  1413.         return;
  1414.     }
  1415.     
  1416.     sprintf(Hfile, "%s.HDR", Areapath[area]);
  1417.     sprintf(Mfile, "%s.MSG", Areapath[area]);
  1418.     
  1419.     Hdr->mailer[7] |= SCANNED;
  1420.     Hdr->flags &=~(DELETED);
  1421.     
  1422.     /*
  1423.     **    Searching of this area is already opened.
  1424.     **    Speeded up the joyride.
  1425.     */
  1426.     
  1427.     if (MTask == MULTITOS) {
  1428.         TO_AREAS = (UWORD)Files_to_open / 5;
  1429.         if (TO_AREAS < (N_AREAFILE / 5)) {
  1430.             log_line (6, "-Can't process with a lack of files.");
  1431.             terminate (-2);
  1432.         }
  1433.         if (TO_AREAS > N_MAXAREA) TO_AREAS = N_MAXAREA;
  1434.     } else
  1435.         TO_AREAS = N_AREAFILE;
  1436.     
  1437.     for (i=0; i < TO_AREAS; i++) {
  1438.         if (lastareawritten[i] == area)
  1439.         {
  1440.             tofile = i;
  1441.             break;
  1442.         }
  1443.     }
  1444.  
  1445.     /*
  1446.     **    No, it's a brand new one. Open it.
  1447.     */
  1448.  
  1449.     if (i >= TO_AREAS)
  1450.     {
  1451.         for (i=0; i < TO_AREAS; i++) {
  1452.             if (LHDRfile[i] == FILEclosed &&
  1453.                 LMSGfile[i] == FILEclosed)
  1454.             {
  1455.                 tofile = i;
  1456.                 
  1457.                 if (!Openarea(i, Hfile, Mfile, Hdr))
  1458.                     terminate(10);
  1459.                 
  1460.                 LHDRfile[i] = FILEopen;
  1461.                 LMSGfile[i] = FILEopen;
  1462.                 lastareawritten[i] = area;
  1463.                 break;
  1464.             }
  1465.         }
  1466.         
  1467.         if (i >= TO_AREAS)
  1468.         {
  1469.             fclose(LHDR[0]);
  1470.             fclose(LMSG[0]);
  1471.             
  1472.             sprintf (fbuf, "%s.HDR", Areapath[lastareawritten[0]]);
  1473.             
  1474.             attr = Fattrib(fbuf, 0, -1);
  1475.             
  1476.             if (attr != -33 && attr != -34 && attr & FA_ARCHIVE)
  1477.             {
  1478.                 if (intel(Sversion()) >= 0x0015U)
  1479.                     Fattrib(fbuf, 1, 0);
  1480.                 else
  1481.                     Fattrib(fbuf, 1, FA_ARCHIVE);
  1482.             }
  1483.                     
  1484.             tofile = 0;
  1485.             lastareawritten[0] = area;
  1486.             
  1487.             if (!Openarea(0, Hfile, Mfile, Hdr))
  1488.                 terminate(10);
  1489.         }
  1490.     } else {
  1491.         Hdr->Mstart = ftell(LMSG[tofile]);
  1492.     }
  1493.     
  1494.     fseek(LHDR[tofile], 0L, SEEK_END); 
  1495. /*
  1496.     hdrwrite(Hdr, sizeof(MSGHEADER), LHDR[tofile]);
  1497.     msgwrite(msg, (UWORD) strlen(msg), LMSG[tofile]);
  1498. */
  1499.     Fwrite(fileno(LMSG[tofile]), (UWORD)strlen(msg), msg);
  1500.     Fwrite(fileno(LHDR[tofile]), sizeof(MSGHEADER),  Hdr);
  1501.  
  1502.     /*
  1503.     hdrwrite(Hdr, sizeof(MSGHEADER), LHDR[tofile]);
  1504.     msgwrite(msg, (UWORD) strlen(msg), LMSG[tofile]);
  1505.     */
  1506. }
  1507.  
  1508. VOID dupesave(MSGHEADER *Hdr, BYTE *msg)
  1509. {
  1510.     BYTE Hfile[128], Mfile[128];
  1511.     
  1512.     if (!strlen(dupearea)) return;
  1513.     
  1514.     sprintf(Hfile, "%s.HDR", dupearea);
  1515.     sprintf(Mfile, "%s.MSG", dupearea);
  1516.     
  1517.     Hdr->size = (UWORD)strlen(msg);
  1518.     
  1519.     if((MSGHDR = fopen(Hfile, "r+b")) == NULL &&
  1520.         (MSGHDR = fopen(Hfile, "wb")) == NULL) {
  1521.             log_line(6,Logmessage[M__CANT_OPEN_HEADER], Hfile);
  1522.             return;
  1523.     }
  1524.     else fseek(MSGHDR, 0L, SEEK_END);
  1525.     
  1526.     if ((MSGMSG = fopen(Mfile, "r+b")) == NULL )
  1527.     {
  1528.         if ((MSGMSG = fopen(Mfile, "wb")) == NULL)
  1529.         {
  1530.             log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  1531.             fclose(MSGHDR);
  1532.             return;
  1533.         }
  1534.         else Hdr->Mstart = 0L;
  1535.     }
  1536.     else
  1537.     {
  1538.         fseek(MSGMSG, 0L, SEEK_END);
  1539.         Hdr->Mstart = ftell(MSGMSG);
  1540.     }
  1541.         
  1542.     Fwrite(fileno(MSGHDR), sizeof(MSGHEADER), Hdr);
  1543.     Fwrite(fileno(MSGMSG), strlen(msg), msg);
  1544.         
  1545.     fclose(MSGMSG);
  1546.     fclose(MSGHDR);
  1547.     MSGMSG = NULL;
  1548.     MSGHDR = NULL;
  1549. }
  1550.  
  1551. /*
  1552. **    Oude routine van FastImport. Deze is buggy, en laat niet alle 
  1553. **    berichten importeren.
  1554.  
  1555. VOID save_to_local( MSGHEADER *Hdr, BYTE * msg, WORD area) {
  1556.  
  1557.     WORD     i, 
  1558.             TO_AREAS,
  1559.             tofile,
  1560.             attr;
  1561.     
  1562.     BYTE    Hfile[128],
  1563.             Mfile[128],
  1564.             fbuf[128];
  1565.             
  1566. #if defined DEBUG
  1567.     printf("*Saving LOCAL\n");
  1568. #endif
  1569.     
  1570.     sprintf(Hfile, "%s.HDR", Areapath[area]);
  1571.     sprintf(Mfile, "%s.MSG", Areapath[area]);
  1572.     
  1573.     Hdr->mailer[7] |= SCANNED;
  1574.     Hdr->flags &=~(DELETED);
  1575.     
  1576.     /*
  1577.     **    Searching of this area is already opened.
  1578.     **    Speeded up the joyride.
  1579.     */
  1580.     
  1581.         if (dofastimport == TRUE) TO_AREAS = N_FASTAREAS;
  1582.     else TO_AREAS = N_AREAFILE;
  1583.     
  1584.     for (i=0; i < TO_AREAS; i++)
  1585.         if (lastareawritten[i] == area)
  1586.         {
  1587.             tofile = i;
  1588.             break;
  1589.         }
  1590.     
  1591.     /*
  1592.     **    No, it's a brand new one. Open it.
  1593.     */
  1594.     
  1595.     if (i >= TO_AREAS)
  1596.     {
  1597.         for (i=0; i < TO_AREAS; i++)
  1598.             if (LHDRfile[i] == FILEclosed &&
  1599.                 LMSGfile[i] == FILEclosed)
  1600.             {
  1601.                 tofile = i;
  1602.                 
  1603.                 if (!Openarea(i, Hfile, Mfile, Hdr))
  1604.                     terminate(10);
  1605.                 
  1606.                 LHDRfile[i] = FILEopen;
  1607.                 LMSGfile[i] = FILEopen;
  1608.                 lastareawritten[i] = area;
  1609.                 if (dofastimport == TRUE) {
  1610.                     h_Mstart[i] = Hdr->Mstart;
  1611.                     h_Mstart[i] += (ULONG)Hdr->size;
  1612.                     if (!h_one_item)
  1613.                         h_one_item = ((coreleft() / N_FASTTOTAL) / 2560 * 2048);
  1614. #if defined EXTERNDEBUG
  1615.         if (debugflag)
  1616.         {
  1617.             log_line(6,">Alloc h_headerbuf[%d]",i);
  1618.             log_line(6,">Alloc h_msgbuf[%d]",i);
  1619.             log_line(6,">Memory %ld", coreleft());
  1620.         }
  1621. #endif
  1622.                     h_headerbuf[i] = (BYTE *)myalloc(h_one_item);
  1623.                     h_msgbuf[i] = (BYTE *)myalloc(h_one_item);
  1624.                     h_headeritems[i] = 0L;
  1625.                     h_msgitems[i] = 0L;
  1626.                     h_maxheader[i] = h_one_item;
  1627.                     h_maxmsg[i] = h_one_item;
  1628.                 }
  1629.                 break;
  1630.             }
  1631.         
  1632.         if (i >= TO_AREAS)
  1633.         {
  1634.             if (dofastimport == TRUE) {
  1635.                 Fwrite(fileno(LHDR[0]), h_headeritems[0],
  1636.                     h_headerbuf[0]);
  1637.                 Fwrite(fileno(LMSG[0]), h_msgitems[0],
  1638.                     h_msgbuf[0]);
  1639.                 h_headeritems[0] = 0L;
  1640.                 h_msgitems[0] = 0L;
  1641.             }
  1642.             fclose(LHDR[0]);
  1643.             fclose(LMSG[0]);
  1644.             
  1645.             sprintf (fbuf, "%s.HDR", Areapath[lastareawritten[0]]);
  1646.             
  1647.             attr = Fattrib(fbuf, 0, -1);
  1648.             
  1649.             if (attr != -33 && attr != -34 && attr & FA_ARCHIVE)
  1650.             {
  1651.                 if (intel(Sversion()) >= 0x0015U)
  1652.                     Fattrib(fbuf, 1, 0);
  1653.                 else
  1654.                     Fattrib(fbuf, 1, FA_ARCHIVE);
  1655.             }
  1656.                     
  1657.             tofile = 0;
  1658.             lastareawritten[0] = area;
  1659.             
  1660.             if (!Openarea(0, Hfile, Mfile, Hdr))
  1661.                 terminate(10);
  1662.             h_Mstart[0] = Hdr->Mstart;
  1663.             h_Mstart[0] += (ULONG)Hdr->size;
  1664.         }
  1665.     }
  1666.     else {
  1667.         if (dofastimport == TRUE) {
  1668.             Hdr->Mstart = h_Mstart[tofile];
  1669.             h_Mstart[tofile] += (ULONG) Hdr->size;
  1670.         } else {
  1671.             Hdr->Mstart = ftell(LMSG[tofile]);
  1672.         }
  1673.     }
  1674.     
  1675.     if (dofastimport == FALSE)
  1676.         fseek(LHDR[tofile], 0L, SEEK_END); 
  1677. /*
  1678.     hdrwrite(Hdr, sizeof(MSGHEADER), LHDR[tofile]);
  1679.     msgwrite(msg, (UWORD) strlen(msg), LMSG[tofile]);
  1680. */
  1681.     if (dofastimport == FALSE) {
  1682.         Fwrite(fileno(LMSG[tofile]), (UWORD)strlen(msg), msg);
  1683.         Fwrite(fileno(LHDR[tofile]), sizeof(MSGHEADER),  Hdr);
  1684.         /*
  1685.         hdrwrite(Hdr, sizeof(MSGHEADER), LHDR[tofile]);
  1686.         msgwrite(msg, (UWORD) strlen(msg), LMSG[tofile]);
  1687.         */
  1688.     } else {
  1689.         addhdrbuf(Hdr, (UWORD)sizeof(MSGHEADER), tofile);
  1690.         addmsgbuf(msg, (UWORD)strlen(msg), tofile);
  1691.     }
  1692. }
  1693.  
  1694. */
  1695.