home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / ios_src / savemsg.c < prev    next >
C/C++ Source or Header  |  1993-01-31  |  23KB  |  1,011 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. /*
  35. **    Save the message. In this part there are many files opened.
  36. **    Messages to local, netmail and trasarea are written down here. Also
  37. **    writing PKT files can be found here. For each of them there are
  38. **    seperated files open. 2 files for the netmail, 2 files for the trasharea
  39. **    16 files for the local area and 10 files for the packets.
  40. **    A total of 30 file channels are opened if using full power at
  41. **    importing.
  42. **
  43. **    It works, it speed up the diskaccess, esspecially if many area's must
  44. **    be imported and there are nodes and points connected to an area.
  45. **    Thanks to Jac that's found out this logic. I've build it up to an
  46. **    greater proportion.
  47. */
  48.  
  49. /*
  50. **    WARNING ........
  51. **    Don't make this function longer, the compiler seems to be
  52. **    have problems to generate an good function of it.
  53. */
  54.  
  55. VOID savemsg(BYTE whereto, MSGHEADER *Hdr, WORD area, BYTE *msg,
  56.                 UWORD zone, UWORD net, UWORD node, UWORD point)
  57. {
  58.     BYTE    Mfile[128],
  59.             Hfile[128],
  60.             system32[128],
  61.             *tbuffer,
  62.             pktpwd[10],
  63.             mtype = TYPE3d,
  64.             htype = FTS_TYPE,
  65.             wrtTOPT = FALSE,
  66.             wrtFMPT = FALSE;
  67.     WORD    i, j,
  68.             tofile;
  69.     UWORD    t_zone, t_net, t_node, t_point,
  70.             f_zone, f_net, f_node, f_point,
  71.             zo, ne, no, dzo, dne, dno, dpo;
  72.     
  73.     Hdr->size = ((UWORD)strlen(msg));
  74.     
  75.     switch (whereto)
  76.     {            
  77.         case TOTRASH:
  78.         case TOPRIVATE:
  79.         
  80.             Hdr->mailer[7] |= SCANNED;
  81.             Hdr->flags |= RECEIVED;
  82.             
  83.             switch (whereto)
  84.             {
  85.                 case TOPRIVATE:
  86.                 
  87.                     if(!strlen(privatebox))
  88.                         log_line(6,Logmessage[M__PVTBOX_UNKNOWN]);
  89.                     
  90.                     sprintf(Hfile, "%s.HDR", privatebox);
  91.                     sprintf(Mfile, "%s.MSG", privatebox);
  92.                     
  93.                     tbuffer = (BYTE *) myalloc(MAXMSGLENGTH + 80);
  94.                     
  95.                     if (area != -1)
  96.                     {
  97.                         sprintf(tbuffer, "\03AREA [%s]\n", Areaname[area]);
  98.                         strcat(tbuffer, msg);
  99.                     }
  100.                     else
  101.                     {
  102.                         sprintf(tbuffer, "\03AREA [MAIL]\n");
  103.                         strcat(tbuffer, msg);
  104.                     }
  105.                     
  106.                     Hdr->size = ((UWORD)strlen(tbuffer));
  107.                     break;
  108.                 
  109.                 case TOTRASH:
  110.                 
  111.                     Hdr->mailer[7] = 0;
  112.                     
  113.                     sprintf(Hfile, "%s.HDR", trasharea);
  114.                     sprintf(Mfile, "%s.MSG", trasharea);
  115.                     
  116.                     tbuffer = (BYTE *) myalloc(strlen(msg)+20);
  117.                     strcpy(tbuffer, msg);
  118.                     break;
  119.             }
  120.             
  121.             /*
  122.             **    Area all open? If not, open it.
  123.             */
  124.             
  125.             if ((MSGHDR = fopen(Hfile, "r+b")) == NULL &&
  126.                 (MSGHDR = fopen(Hfile, "wb")) == NULL)
  127.             {
  128.                 log_line(6,Logmessage[M__CANT_OPEN_HEADER], Hfile);
  129.                 return;
  130.             }
  131.             else fseek(MSGHDR, 0L, SEEK_END);
  132.             
  133.             if ((MSGMSG = fopen(Mfile, "r+b")) == NULL)
  134.             {
  135.                 if ((MSGMSG = fopen(Mfile, "wb")) == NULL)
  136.                 {
  137.                     log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  138.                     fclose(MSGHDR);
  139.                     return;
  140.                 }
  141.                 else Hdr->Mstart = 0L;
  142.             }
  143.             else
  144.             {
  145.                 fseek(MSGMSG, 0L, SEEK_END);
  146.                 Hdr->Mstart = ftell(MSGMSG);
  147.             }
  148. /*            
  149.             hdrwrite(Hdr, sizeof(MSGHEADER), MSGHDR);
  150.             
  151.             msgwrite(tbuffer, (UWORD) strlen(tbuffer), MSGMSG);
  152. */
  153.             Fwrite(fileno(MSGHDR), sizeof(MSGHEADER), Hdr);
  154.             Fwrite(fileno(MSGMSG), strlen(tbuffer), tbuffer);
  155.                     
  156.             fclose(MSGMSG);
  157.             fclose(MSGHDR);
  158.             free(tbuffer);
  159.             return;
  160.                     
  161.         case TOLOCAL:
  162.             
  163. #if defined DEBUG
  164.             printf("*Saving LOCAL\n");
  165. #endif
  166.             
  167.             sprintf(Hfile, "%s.HDR", Areapath[area]);
  168.             sprintf(Mfile, "%s.MSG", Areapath[area]);
  169.             
  170.             Hdr->mailer[7] |= SCANNED;
  171.             
  172.             /*
  173.             **    Searching of this area is already opened.
  174.             **    Speeded up the joyride.
  175.             */
  176.             
  177.             for (i=0; i < N_AREAFILE; i++)
  178.                 if (lastareawritten[i] == area)
  179.                 {
  180.                     tofile = i;
  181.                     break;
  182.                 }
  183.             
  184.             /*
  185.             **    No, it's a brand new one. Open it.
  186.             */
  187.             
  188.             if (i >= N_AREAFILE)
  189.             {
  190.                 for (i=0; i < N_AREAFILE; i++)
  191.                     if (LHDRfile[i] == FILEclosed &&
  192.                         LMSGfile[i] == FILEclosed)
  193.                     {
  194.                         tofile = i;
  195.                         
  196.                         if (!Openarea(i, Hfile, Mfile, Hdr))
  197.                             terminate(10);
  198.                         
  199.                         LHDRfile[i] = FILEopen;
  200.                         LMSGfile[i] = FILEopen;
  201.                         lastareawritten[i] = area;
  202.                         
  203.                         break;
  204.                     }
  205.                 
  206.                 if (i >= N_AREAFILE)
  207.                 {
  208.                     fclose(LHDR[0]);
  209.                     fclose(LMSG[0]);
  210.                     tofile = 0;
  211.                     lastareawritten[0] = area;
  212.                     
  213.                     if (!Openarea(0, Hfile, Mfile, Hdr))
  214.                         terminate(10);
  215.                 }
  216.             }
  217.             else Hdr->Mstart = ftell(LMSG[tofile]);
  218.             
  219.             fseek(LHDR[tofile], 0L, SEEK_END);
  220. /*
  221.             hdrwrite(Hdr, sizeof(MSGHEADER), LHDR[tofile]);
  222.             msgwrite(msg, (UWORD) strlen(msg), LMSG[tofile]);
  223. */
  224.             Fwrite(fileno(LHDR[tofile]), sizeof(MSGHEADER), Hdr);
  225.             Fwrite(fileno(LMSG[tofile]), strlen(msg), msg);
  226.             
  227.             return;
  228.             
  229.         case TONETMAIL:
  230.         
  231.             sprintf(Mfile,"%s.MSG",mailarea);
  232.             sprintf(Hfile,"%s.HDR",mailarea);
  233.             
  234. #if defined DEBUG
  235.             printf("*Saving MAIL\n");
  236. #endif        
  237.             
  238.             /*
  239.             **    If the channel is already opened, we don't have to
  240.             **    open it again. This speed up some things.
  241.             */
  242.                 
  243.             if (!MMSG && !MHDR)
  244.             {
  245.                 if ((MMSG = fopen(Mfile, "r+b")) == NULL)
  246.                     if ((MMSG = fopen(Mfile, "wb")) == NULL)
  247.                     {
  248.                         log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  249.                         return;
  250.                     }
  251.                     else
  252.                         Hdr->Mstart = 0L;
  253.                 else
  254.                 {
  255.                     fseek(MMSG, 0L, SEEK_END);
  256.                     Hdr->Mstart = ftell(MMSG);
  257.                 }
  258.                 
  259.                 if ((MHDR = fopen(Hfile, "r+b")) == NULL &&
  260.                     (MHDR = fopen(Hfile,"wb")) == NULL)
  261.                     {
  262.                         log_line(6,Logmessage[M__CANT_OPEN_AREA], Mfile);
  263.                         return;
  264.                     }
  265.                 else
  266.                     fseek(MHDR, 0L, SEEK_END);
  267.             }
  268.             else
  269.             {
  270.                 fseek(MMSG, 0L, SEEK_END);
  271.                 Hdr->Mstart = ftell(MMSG);
  272.             }
  273. /*            
  274.             hdrwrite(Hdr, sizeof(MSGHEADER), MHDR);
  275.             msgwrite(msg, (UWORD) strlen(msg)+1, MMSG);
  276. */            
  277.             Fwrite(fileno(MHDR), sizeof(MSGHEADER), Hdr);
  278.             Fwrite(fileno(MMSG), strlen(msg)+1, msg);
  279.             
  280.             fflush(MMSG);
  281.             fflush(MHDR);
  282.             
  283.             return;
  284.             
  285.         case TOPACKET:
  286.             
  287.             /*
  288.             **    Get the password of the destination.
  289.             */
  290.             
  291.             mtype = TYPE3d;
  292.             htype = FTS_TYPE;
  293.             
  294.             for (i=0; i < nkey; i++)
  295.                 if (Hdr->Dzone == pwd[i].zone &&
  296.                     Hdr->Dnet == pwd[i].net &&
  297.                     Hdr->Dnode == pwd[i].node &&
  298.                     Hdr->Dpoint == pwd[i].point)
  299.                 {
  300.                     strcpy(pktpwd, pwd[i].pwd);
  301.                     mtype = pwd[i].mtype;
  302.                     htype = pwd[i].htype;
  303.                     break;
  304.                 }
  305.             
  306.             
  307.             /*
  308.             **    If doing secure, watch all passwords mentioned in the
  309.             **    config file. If the destination is not found, break of
  310.             **    and warn the user of an illegal user of this area.
  311.             **    Then return to homebase.
  312.             */
  313.             
  314.             if (dosecure && i >= nkey)
  315.             {
  316.                 if (area >= 0)
  317.                 {
  318.                     log_line(6,Logmessage[M__NODE_UNKNOWN],
  319.                         Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  320.                     log_line(2,Logmessage[M__CHECK_PWDS],
  321.                         Areaname[area]);
  322.                 }
  323.                 else
  324.                 {
  325.                     log_line(6,Logmessage[M__NODE_UNKNOWN_NETMAIL],
  326.                         Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  327.                     log_line(2,Logmessage[M__CHECK_CONFIG]);
  328.                 }
  329.                 
  330.                 return;
  331.             }
  332.             
  333.             dzo = t_zone = Hdr->Dzone;
  334.             dne = t_net = Hdr->Dnet;
  335.             dno = t_node = Hdr->Dnode;
  336.             dpo = t_point = Hdr->Dpoint;
  337.             
  338.             zo = f_zone = Hdr->Ozone;
  339.             ne = f_net = Hdr->Onet;
  340.             no = f_node = Hdr->Onode;
  341.             f_point = Hdr->Opoint;
  342.             
  343.             /*
  344.             **    If point then 3d. Not if point using 4d address.
  345.             **    If netmail, don't change the messageheader.
  346.             **    zo,ne,no,dzo etc. contains the fakenetnumer of
  347.             **    the points.
  348.             */
  349.             
  350.             if (Hdr->Dpoint && mtype == TYPE3d)
  351.             {
  352.                 for (i=0; i < nalias; i++)
  353.                     if (alias[i].zone == Hdr->Dzone &&
  354.                         alias[i].net == Hdr->Dnet &&
  355.                         alias[i].node == Hdr->Dnode)
  356.                     {
  357.                         dzo = Hdr->Dzone;
  358.                         
  359.                         if (area >= 0)
  360.                         {
  361.                             dne = Hdr->Dnet = alias[i].pointnet;
  362.                             dno = Hdr->Dnode = Hdr->Dpoint;
  363.                             Hdr->Dpoint = dpo = 0;
  364.                         }
  365.                         else
  366.                         {
  367.                             dne = alias[i].pointnet;
  368.                             dno = Hdr->Dpoint;
  369.                             dpo = 0;
  370.                         }
  371.                         
  372.                         break;
  373.                     }
  374.                 
  375.                 if (i >= nalias)
  376.                 {
  377.                     htype = FRONTDOOR;
  378.                     mtype = TYPE4d;
  379.                     wrtTOPT = TRUE;
  380.                 }
  381.             }
  382.             
  383.             if (Hdr->Opoint && mtype == TYPE3d)
  384.             {
  385.                 for (i=0; i < nalias; i++)
  386.                     if (alias[i].zone == Hdr->Ozone &&
  387.                         alias[i].net == Hdr->Onet &&
  388.                         alias[i].node == Hdr->Onode)
  389.                     {
  390.                         zo = Hdr->Ozone;
  391.                         
  392.                         if (area >= 0)
  393.                         {
  394.                             ne = Hdr->Onet = alias[i].pointnet;
  395.                             no = Hdr->Onode= Hdr->Opoint;
  396.                             Hdr->Opoint = 0;
  397.                         }
  398.                         else
  399.                         {
  400.                             ne = alias[i].pointnet;
  401.                             no = Hdr->Opoint;
  402.                         }
  403.                         
  404.                         break;
  405.                     }
  406.                 
  407.                 if (i >= nalias)
  408.                 {
  409.                     htype = FRONTDOOR;
  410.                     mtype = TYPE4d;
  411.                     wrtFMPT = TRUE;
  412.                 }
  413.             }
  414.             
  415.             /*
  416.             **    Create filename.
  417.             */
  418.             
  419.             xsprintf(system32, "%02z%03z%03z.%02z", dzo, dne, dno, dpo);
  420.             
  421.             /*
  422.             **    If new file create packet header.
  423.             */
  424.             
  425.             switch (htype)
  426.             {
  427.                 case FTS_TYPE:
  428.                 
  429.                     fts_TYPE:
  430.                     create_pkt_hdr(zo, ne, no, dzo, dne, dno, pktpwd);
  431.                     break;
  432.                     
  433.                 /*
  434.                 **    Create an packetheader for frontdoor.
  435.                 */
  436.                     
  437.                 case FRONTDOOR:
  438.                 
  439.                     create_pkt_fnd(f_zone, f_net, f_node, f_point,
  440.                         t_zone, t_net, t_node, t_point, pktpwd);
  441.                     break;
  442.                 
  443.                 default:
  444.                 
  445.                     log_line(6,Logmessage[M__COMPILER_ERROR]);
  446.                     goto fts_TYPE;
  447.             }
  448.             
  449. #if defined DEBUG
  450.             printf("*Writing PKT for %d:%d/%d.%d\n", Hdr->Dzone, Hdr->Dnet, Hdr->Dnode, Hdr->Dpoint);
  451. #endif        
  452.             
  453.             /*
  454.             **    Trying to find the channel of this node or point.
  455.             **    If not found search for an NULL filepointer.
  456.             */
  457.             
  458.             for (i=0; i < N_PKTFILE; i++)
  459.                 if (PKTzone[i] == t_zone &&
  460.                     PKTnet[i] == t_net &&
  461.                     PKTnode[i] == t_node &&
  462.                     PKTpoint[i] == t_point)
  463.                 {
  464.                     tofile = i;
  465.                     fseek(PKTfile[i], 0L, SEEK_END);
  466.                     break;
  467.                 }
  468.             
  469.             /*
  470.             **    This node or point have no channel.
  471.             **    Searching for an free NULL filepointer.
  472.             */
  473.             
  474.             if (i >= N_PKTFILE)
  475.             {
  476.                 for (i=0; i < N_PKTFILE; i++)
  477.                     if (PKTchannel[i] == FILEclosed)
  478.                     {
  479.                         tofile = i;
  480.                         PKTchannel[i] = FILEopen;
  481.                         if (Openpkt(i, system32, Hdr)) break;
  482.                         return;
  483.                     }
  484.                 
  485.                 /*
  486.                 **    If all files are opened, try to find an channel
  487.                 **    that's not used. Logic :
  488.                 **
  489.                 **    Compare the nodes and points that's are connected
  490.                 **    to this area with the nodes and point that are
  491.                 **    using an channel. If found break of and continue;
  492.                 **    If not close this channel and open it again for
  493.                 **    the new node or point. If all using this channel
  494.                 **    close the first one.
  495.                 **
  496.                 **    I speeded the diskwriting up mother.
  497.                 */
  498.                 
  499.                 if (i >= N_PKTFILE)
  500.                 {
  501.                     if (area >= 0)
  502.                     {
  503.                         for (j=0; j < N_PKTFILE; j++)
  504.                         {
  505.                             for (i=0; Tozone[area][i] != (UWORD)(-1); i++)
  506.                                 if (Tozone[area][i] == PKTzone[j] &&
  507.                                     Tonet[area][i] == PKTnet[j] &&
  508.                                     Tonode[area][i] == PKTnode[j] &&
  509.                                     Topoint[area][i] == PKTpoint[j])
  510.                                     break;
  511.                             
  512.                             if (Tozone[area][i] == (UWORD)(-1))
  513.                             {
  514.                                 putc(0, PKTfile[j]);
  515.                                 putc(0, PKTfile[j]);
  516.                                 fclose(PKTfile[j]);
  517.                                 
  518.                                 tofile = j;
  519.                                 if (!Openpkt(j, system32, Hdr)) return;
  520.                                 break;
  521.                             }
  522.                         }
  523.                     }
  524.                     else j = N_PKTFILE;
  525.                     
  526.                     /*
  527.                     **    Brrrr.... close the first channel. There are
  528.                     **    so many connected.
  529.                     **
  530.                     **    It slow down the process.
  531.                     */
  532.                     
  533.                     if (j >= N_PKTFILE )
  534.                     {
  535.                         putc(0, PKTfile[0]);
  536.                         putc(0, PKTfile[0]);
  537.                         fclose(PKTfile[0]);
  538.                         
  539.                         tofile = 0;
  540.                         if (!Openpkt(0, system32, Hdr)) return;
  541.                     }
  542.                 }
  543.             }
  544.             
  545.             /*
  546.             **    This node or point is now in use of this channel.
  547.             */
  548.             
  549.             PKTzone[tofile] = t_zone;
  550.             PKTnet[tofile] = t_net;
  551.             PKTnode[tofile] = t_node;
  552.             PKTpoint[tofile] = t_point;
  553.             
  554.             pmsg.pm_ver = intel(2);
  555.             pmsg.pm_dnet = intel(Hdr->Dnet);
  556.             pmsg.pm_dnode = intel(Hdr->Dnode);
  557.             pmsg.pm_onet = intel(Hdr->Onet);
  558.             pmsg.pm_onode = intel(Hdr->Onode);
  559.             pmsg.pm_attr = intel((Hdr->flags &= ~(MSGLOCAL|MSGHOLD|KILLSEND|SENT|MSGFWD|ORPHAN)));
  560.             pmsg.pm_cost = 0;
  561.             
  562.             /*
  563.             **    Writing the pkt message header.
  564.             */
  565. /*            
  566.             hdrwrite(&pmsg, sizeof(struct _pktmsg), PKTfile[tofile]);
  567. */
  568.             Fwrite(fileno(PKTfile[tofile]), sizeof(struct _pktmsg), &pmsg);
  569.                         
  570.             pktwrite(Hdr->time, (UWORD) strlen(Hdr->time), PKTfile[tofile]);
  571.             pktwrite(Hdr->to, (UWORD) strlen(Hdr->to), PKTfile[tofile]);
  572.             pktwrite(Hdr->from, (UWORD) strlen(Hdr->from), PKTfile[tofile]);
  573.             pktwrite(Hdr->topic, (UWORD) strlen(Hdr->topic), PKTfile[tofile]);
  574.             
  575.             if (wrtTOPT)
  576.                 fprintf(PKTfile[tofile], "\01TOPT %d\n", Hdr->Dpoint);
  577.             if (wrtFMPT)
  578.                 fprintf(PKTfile[tofile], "\01FMPT %d\n", Hdr->Opoint);
  579.             
  580.             /*
  581.             **    Put the message on disk.
  582.             */
  583.             
  584.             pktwrite(msg, (UWORD) strlen(msg), PKTfile[tofile]);
  585.             
  586.             break;
  587.             
  588.         case NETMAILPACKET:
  589.             
  590.             /*
  591.             **    Get the pasword of the destination.
  592.             */
  593.             
  594.             mtype = TYPE3d;
  595.             htype = FTS_TYPE;
  596.             
  597.             for (i=0; i < nkey; i++)
  598.             {
  599.                 if (zone == pwd[i].zone && net == pwd[i].net &&
  600.                     node == pwd[i].node && point == pwd[i].point)
  601.                 {
  602.                     strcpy(pktpwd, pwd[i].pwd);
  603.                     
  604.                     mtype = pwd[i].mtype;
  605.                     htype = pwd[i].htype;
  606.                     
  607. #if defined OUTDEBUG
  608.                     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);
  609. #endif
  610.                     break;
  611.                 }
  612.             }
  613.             
  614.             /*
  615.             **    If crashmail, delete password.
  616.             */
  617.             
  618.             if (Hdr->flags & CRASH)
  619.                 for (i=0; i < (WORD) strlen(pktpwd); i++)
  620.                     pktpwd[i] = EOS;
  621.             
  622.             if (i >= nkey)
  623.             {
  624. #if defined OUTDEBUG
  625.                 if (dooutdeb) log_line(6,">No password for %d:%d/%d.%d", zone, net, node, point);
  626. #endif
  627.                 *pktpwd = EOS;
  628.             }
  629.             
  630.             /*
  631.             **    If no crashmail, take normal.
  632.             */
  633.             
  634.             if (!(Hdr->flags & CRASH))
  635.             {
  636.                 if (point)
  637.                 {
  638.                     dzo = zone;
  639.                     
  640.                     for (i=0; i < nalias; i++)
  641.                         if (zone == alias[i].zone &&
  642.                             net == alias[i].net &&
  643.                             node == alias[i].node)
  644.                         {
  645.                             dne = alias[i].pointnet;
  646.                             dno = point;
  647.                             dpo = 0;
  648. #if defined OUTDEBUG
  649.                             if (dooutdeb) log_line(6,">Point - dzo, dne, dno %d:%d/%d", dzo, dne, dno);
  650. #endif
  651.                             break;
  652.                         }
  653.                 }
  654.                 else
  655.                 {
  656.                     dzo = zone;
  657.                     dne = net;
  658.                     dno = node;
  659.                     dpo = 0;
  660.                     
  661. #if defined OUTDEBUG
  662.                     if (dooutdeb) log_line(6,">No point - dzo,dne,dno %d:%d/%d", dzo, dne, dno);
  663. #endif
  664.                 }
  665.                 
  666.                 t_zone = zone;
  667.                 t_net = net;
  668.                 t_node = node;
  669.                 t_point = point;
  670.                 
  671.                 /*
  672.                 **    Get origin address from ourself.
  673.                 */
  674.                 
  675.                 for (i=0; i < nalias; i++)
  676.                     if (zone == alias[i].zone &&
  677.                         node == alias[i].node &&
  678.                         net == alias[i].net)
  679.                     {
  680.                         if (!alias[i].point)
  681.                         {
  682.                             zo = alias[i].zone;
  683.                             ne = alias[i].net;
  684.                             no = alias[i].node;
  685.                         }
  686.                         else
  687.                         {
  688.                             zo = alias[i].zone;
  689.                             ne = alias[i].pointnet;
  690.                             no = alias[i].point;
  691.                         }
  692.                         
  693.                         f_zone = alias[i].zone;
  694.                         f_net = alias[i].net;
  695.                         f_node = alias[i].node;
  696.                         f_point = alias[i].point;
  697.                         
  698.                         break;
  699.                     }
  700.                 
  701.                 if (i >= nalias)
  702.                 {
  703.                     for (i=0; i < nalias; i++)
  704.                         if (zone == alias[i].zone)
  705.                         {
  706.                             if (!alias[i].point)
  707.                             {
  708.                                 zo = alias[i].zone;
  709.                                 ne = alias[i].net;
  710.                                 no = alias[i].node;
  711.                             }
  712.                             else
  713.                             {
  714.                                 zo = alias[i].zone;
  715.                                 ne = alias[i].pointnet;
  716.                                 no = alias[i].point;
  717.                             }
  718.                             
  719.                             f_zone = alias[i].zone;
  720.                             f_net  = alias[i].net;
  721.                             f_node = alias[i].node;
  722.                             f_point= alias[i].point;
  723.                             
  724.                             break;
  725.                         }
  726.                     
  727.                     if (i >= nalias)
  728.                         if (!alias[0].point)
  729.                         {
  730.                             zo = f_zone = alias[0].zone;
  731.                             ne = f_net = alias[0].net;
  732.                             no = f_node = alias[0].node;
  733.                             f_point = alias[0].point;
  734.                         }
  735.                         else
  736.                         {
  737.                             zo = f_zone = alias[0].zone;
  738.                             ne = alias[0].pointnet;
  739.                             no = f_point = alias[0].point;
  740.                             f_net = alias[0].net;
  741.                             f_node = alias[0].node;
  742.                         }
  743.                 }    
  744.             }
  745.             else
  746.             {
  747.             
  748.                 /*
  749.                 **    Crashmail.
  750.                 */
  751.                 
  752.                 if (Hdr->Opoint)
  753.                 {
  754.                     for (i=0; i < nalias; i++)
  755.                         if (Hdr->Ozone == alias[i].zone &&
  756.                             Hdr->Onet == alias[i].net &&
  757.                             Hdr->Onode == alias[i].node &&
  758.                             Hdr->Opoint == alias[i].point)
  759.                         {
  760.                             zo = alias[i].zone;
  761.                             ne = alias[i].pointnet;
  762.                             no = alias[i].point;
  763.                             break;
  764.                         }
  765.                     
  766.                     if (i >= nalias)
  767.                     {
  768.                         zo = Hdr->Ozone;
  769.                         ne = Hdr->Onet;
  770.                         no = Hdr->Onode;
  771.                     }
  772.                 }
  773.                 else
  774.                 {
  775.                     zo = Hdr->Ozone;
  776.                     ne = Hdr->Onet;
  777.                     no = Hdr->Onode;
  778.                 }
  779.                 
  780.                 f_zone = Hdr->Ozone;
  781.                 f_net = Hdr->Onet;
  782.                 f_node = Hdr->Onode;
  783.                 f_point = Hdr->Opoint;
  784.                 
  785.                 if (Hdr->Dpoint)
  786.                     for (i=0; i < nalias; i++)
  787.                         if (Hdr->Dnet == alias[i].net &&
  788.                             Hdr->Dnode == alias[i].node &&
  789.                             Hdr->Dzone == alias[i].zone)
  790.                         {
  791.                             log_line(4,Logmessage[M__CANT_CRASH_POINT]);
  792.                             return;
  793.                         }
  794.                 
  795.                 dzo = t_zone = Hdr->Dzone;
  796.                 dne = t_net = Hdr->Dnet;
  797.                 dno = t_node = Hdr->Dnode;
  798.                 dpo = t_point = Hdr->Dpoint;
  799.             }
  800.             
  801. #if defined EXTERNDEBUG
  802.             if (debugflag) log_line(6,">Now have as destination PKT %d:%d/%d.%d", dzo, dne, dno, dpo);
  803. #endif
  804.             
  805.             /*
  806.             **    Create filename.
  807.             */
  808.             
  809.             xsprintf(system32, "%02z%03z%03z.%02z", dzo, dne, dno, dpo);
  810.             
  811.             /*
  812.             **    If new file create packet header.
  813.             */
  814.             
  815.             switch (htype)
  816.             {
  817.                 case FTS_TYPE:
  818.                 
  819.                     FTS_type:
  820.                     create_pkt_hdr(zo, ne, no, dzo, dne, dno, pktpwd);
  821.                     break;
  822.                     
  823.                 /*
  824.                 **    Create an packetheader for frontdoor.
  825.                 */
  826.                 
  827.                 case FRONTDOOR:
  828.                 
  829.                     create_pkt_fnd(f_zone, f_net, f_node, f_point,
  830.                         t_zone, t_net, t_node, t_point, pktpwd);
  831.                     break;
  832.                 
  833.                 default:
  834.                 
  835.                     log_line(6,Logmessage[M__COMPILER_ERROR]);
  836.                     goto FTS_type;
  837.             }
  838.             
  839. #if defined DEBUG
  840.             printf("*Writing PKT for %d:%d/%d\n", Hdr->Dzone, Hdr->Dnet, Hdr->Dnode);
  841. #endif        
  842.             
  843.             /*
  844.             **    Trying to find the channel of this node or point.
  845.             **    If not found search for an NULL filepointer.
  846.             */
  847.             
  848.             for (i=0; i < N_PKTFILE; i++)
  849.                 if (PKTzone[i] == t_zone && PKTnet[i] == t_net &&
  850.                     PKTnode[i] == t_node)
  851.                 {
  852.                     tofile = i;
  853.                     fseek(PKTfile[i], 0L, SEEK_END);
  854.                     break;
  855.                 }
  856.             
  857.             /*
  858.             **    This node or point have no channel.
  859.             **    Searching for an free NULL filepointer.
  860.             */
  861.             
  862.             if (i >= N_PKTFILE)
  863.             {
  864.                 for (i=0; i < N_PKTFILE; i++)
  865.                     if (PKTchannel[i] == FILEclosed)
  866.                     {
  867.                         tofile = i;
  868.                         PKTchannel[i] = FILEopen;
  869.                         
  870.                         if (Openpkt(i, system32, Hdr)) break;
  871.                         return;
  872.                     }
  873.                 
  874.                 if (i >= N_PKTFILE )
  875.                 {
  876.                     putc(0,PKTfile[0]);
  877.                     putc(0,PKTfile[0]);
  878.                     fclose(PKTfile[0]);
  879.                     
  880.                     tofile = 0;
  881.                     if (!Openpkt(0, system32, Hdr)) return;
  882.                 }
  883.             }
  884.             
  885.             /*
  886.             **    This node or point is now in use of this channel.
  887.             */
  888.             
  889.             PKTzone[tofile] = t_zone;
  890.             PKTnet[tofile] = t_net;
  891.             PKTnode[tofile] = t_node;
  892.             PKTpoint[tofile] = t_point;
  893.             
  894.             pmsg.pm_ver = intel(2);
  895.             pmsg.pm_dnet = intel(Hdr->Dnet);
  896.             pmsg.pm_dnode = intel(Hdr->Dnode);
  897.             pmsg.pm_onet = intel(Hdr->Onet);
  898.             pmsg.pm_onode = intel(Hdr->Onode);
  899.             pmsg.pm_attr = intel(Hdr->flags &= ~(MSGLOCAL|MSGHOLD|KILLSEND|SENT|MSGFWD));
  900.             pmsg.pm_cost = 0;
  901.             
  902.             /*
  903.             **    Writing the pkt message header.
  904.             */
  905.             
  906. #if defined OUTDEBUG
  907.             if (dooutdeb) log_line(6,">Writing packet");
  908. #endif
  909. /*
  910.             hdrwrite(&pmsg, sizeof(struct _pktmsg), PKTfile[tofile]);
  911. */
  912.             Fwrite(fileno(PKTfile[tofile]), sizeof(struct _pktmsg), &pmsg);
  913.                         
  914.             pktwrite(Hdr->time, (UWORD) strlen(Hdr->time), PKTfile[tofile]);
  915.             pktwrite(Hdr->to, (UWORD) strlen(Hdr->to), PKTfile[tofile]);
  916.             pktwrite(Hdr->from, (UWORD) strlen(Hdr->from), PKTfile[tofile]);
  917.             pktwrite(Hdr->topic, (UWORD) strlen(Hdr->topic), PKTfile[tofile]);
  918.             
  919.             /*
  920.             **    INTL needed?
  921.             */
  922.             
  923.             if (Hdr->Ozone != f_zone && Hdr->Dzone != t_zone)
  924.             {
  925.                 if (strncmp(msg,"AREA:",5))
  926.                 {
  927.                     if (!get_interzone(msg, &f_zone, &f_net, &f_node, &t_zone, &t_net, &t_node))
  928.                     {
  929.                         write_INTL:
  930.                         
  931. #if defined OUTDEBUG
  932.                         if (dooutdeb) log_line(6,">Writing INTL");
  933. #endif
  934.                         fprintf(PKTfile[tofile], "\01INTL %d:%d/%d %d:%d/%d\n",
  935.                             Hdr->Ozone, Hdr->Onet, Hdr->Onode,
  936.                             Hdr->Dzone, Hdr->Dnet, Hdr->Dnode);
  937.                     }
  938.                     else
  939.                     {
  940.                         if (f_zone != Hdr->Ozone &&
  941.                             t_zone != Hdr->Dzone)
  942.                         {
  943.                             msg = strip_line(msg, "\01INTL ");
  944.                             goto write_INTL;
  945.                         }
  946.                     }
  947.                 }
  948.             }
  949.             
  950.             pktwrite(msg, (UWORD) strlen(msg), PKTfile[tofile]);
  951.             
  952.             break;
  953.         
  954.         default:
  955.         
  956.             log_line(6,Logmessage[M__COMPILER_ERROR]);
  957.             break;
  958.     }
  959. }
  960.  
  961. VOID ToDataBase(MSGHEADER *Hdr, BYTE *msg, WORD area) {
  962.     WORD i;
  963.     BYTE 
  964.          Hfile[128],
  965.          Mfile[128];
  966.     
  967.     SMALLBASE sbase;
  968.     
  969.     for (i=0; i < nfdbase; i++) {
  970.         if (newmatch( (FDcompare[nfdbase] == FD_FROM) ? Hdr->from : Hdr->to, ToBase[nfdbase])) {
  971.             sprintf(Hfile,"%sDATA.HR",FDbase);
  972.             sprintf(Mfile,"%sDATA.MG",FDbase);
  973.             
  974.             if ((MSGHDR = fopen(Hfile,"r+b")) == NULL ) {
  975.                 if ((MSGHDR = fopen(Hfile,"wb"))  == NULL) {
  976.                     log_line(5,Logmessage[M__CANT_OPEN_B_DBASE]);
  977.                     return;
  978.                 }
  979.             } else fseek(MSGHDR, 0L, SEEK_END);
  980.             
  981.             if ((MSGMSG = fopen(Mfile, "r+b")) == NULL) {
  982.                 if ((MSGMSG = fopen(Mfile, "wb")) == NULL){
  983.                     log_line(6,Logmessage[M__CANT_OPEN_M_DBASE]);
  984.                     fclose(MSGHDR);
  985.                     return;
  986.                 }
  987.                 else sbase.Mstart = 0L;
  988.             }
  989.             else {
  990.                 fseek(MSGMSG, 0L, SEEK_END);
  991.                 sbase.Mstart = ftell(MSGMSG);
  992.             }
  993.             
  994.             strcpy (sbase.from, Hdr->from);
  995.             strcpy (sbase.to, Hdr->to);
  996.             strcpy (sbase.topic, Hdr->topic);
  997.             strcpy (sbase.time, Hdr->time);
  998.             strncpy(sbase.area, Areaname[area],39);
  999.             sbase.size = (UWORD) strlen(msg);
  1000.             
  1001.             hdrwrite(&sbase, sizeof(SMALLBASE), MSGHDR);
  1002.             
  1003.             msgwrite(msg, (UWORD) strlen(msg), MSGMSG);
  1004.             
  1005.             fclose(MSGMSG);
  1006.             fclose(MSGHDR);
  1007.             return;
  1008.         }
  1009.     }
  1010. }
  1011.