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

  1. /*********************************************************************
  2.  
  3.                     IOS - tosser
  4.                     
  5.     Program : IOSmail
  6.     Source  : free
  7.     Author  : Rinaldo Visscher
  8.     Date    : 1991
  9.     
  10.     Seek in the inboundfolders for arcmail and-or for files with the
  11.     extension PKT. If found, read those files, and decompress it to
  12.     readable textfile for the messageeditor. Tos all new messages to
  13.     connected people on an partical area. Create packets if on.
  14.     
  15. *********************************************************************/
  16.  
  17.  
  18. /*#define TEST_CRUNCH 1*/
  19.  
  20. #include        <stdio.h>
  21. #include        <time.h>
  22. #include        <ctype.h>
  23. #if !defined LATTICE
  24. #include        <ext.h>
  25. #include         <tos.h>
  26. #include        <process.h>
  27. #include        <vdi.h>
  28. #else
  29. #include        <sys/stat.h>
  30. #include        <dos.h>
  31. #endif
  32. #include        <string.h>
  33. #include        <stdlib.h>
  34. /* #include        <errno.h> */
  35. #include     <mintbind.h>
  36. #include        <atarierr.h>
  37.  
  38. #include        "portab.h"
  39. #include        "defs.h"
  40. #include        "ioslib.h"
  41. #include        "modules.h"
  42. #include        "routing.h"
  43.  
  44. #include        "inmail.h"
  45. #include        "strutil.h"
  46. #include        "crc.h"
  47. #include        "lang.h"
  48.  
  49. #include        "vars.h"
  50.  
  51. #include        "compiler.h"
  52.  
  53. CONST BYTE        *thisprogram = "INMAIL.TTP";
  54. BYTE            first_pass=0;
  55.  
  56. #define PRGNAMELEN 10
  57.  
  58.  
  59.  
  60. MLOCAL BOOLEAN get_secure(WORD area, UWORD zone, UWORD net, UWORD node, UWORD point)
  61. {
  62.     WORD    i;
  63.     
  64.     for (i=0; Tozone[area][i] != (UWORD)-1; i++)
  65.         if (Tozone[area][i] == zone && Tonet[area][i] == net &&
  66.             Tonode[area][i] == node && Topoint[area][i] == point)
  67.                return(TRUE);
  68.     
  69.     return(FALSE);
  70. }
  71.  
  72. /*
  73. **    Scanning the inbound for files. If found jump to arcmail() to
  74. **    find out if this is arcmail. I arcmail returns ARCMAIL otherwise
  75. **    return EMPTY inbound.
  76. */
  77.  
  78. /*
  79.  
  80. BYTE unpack_pkt(VOID)
  81. {
  82.     BYTE            buffer[128];
  83. #if defined LATTICE
  84.     DMABUFFER        blk;
  85. #else
  86.     struct ffblk    blk;
  87. #endif
  88.     
  89.     sprintf(buffer, "%s*.*", inbound);
  90.     
  91.     if (!findfirst(buffer, &blk, 0))
  92.     {
  93.         bundlesize = blk.ff_fsize;
  94.         if (arc_mail(blk.ff_name)) return(ARCMAIL);
  95.         
  96.         while (!findnext(&blk))
  97.         {
  98.             bundlesize = blk.ff_fsize;
  99.             if (arc_mail(blk.ff_name)) return (ARCMAIL);
  100.         }
  101.     }
  102.     
  103.     return(EMPTY);
  104. }
  105.  
  106. /*
  107. **    Searching for arcmail extensions. If found jump to unpack() and
  108. **    extract the packet. If no arcmail return EMPTY otherwise return
  109. **    RCMAIL.
  110. */
  111.  
  112. BYTE arc_mail(BYTE *name)
  113. {
  114.     BYTE    buffer[128],
  115.             *p;
  116.     WORD    i;
  117.     
  118.     if ((p = strchr(name, '.')) != NULL)
  119.     {
  120.         p++;
  121.         
  122.         for (i=0; isuf[i] != NULL; i++)
  123.             if (!strnicmp(p, isuf[i], 2))
  124.             {
  125.                 if (!isdigit(p[strlen(p)-1])) continue;
  126.                 
  127.                 sprintf(buffer, "%s%s", inbound, name);
  128.                 
  129.                 if (unpack(buffer)) return (ARCMAIL);
  130.                 else return (EMPTY);
  131.             }
  132.     }
  133.     
  134.     return (EMPTY);
  135. }
  136.  
  137. VOID isPKT (BYTE *name) {
  138.     BYTE buffer[128];
  139.     FILE *fp;
  140.     
  141.     sprintf(buffer, "%s%s",pkt_temp, name);
  142.     
  143.     if ((fp = fopen(buffer, "r+b")) != NULL) {
  144.         fread (&phdr, sizeof (struct _pkthdr), 1, fp);
  145.         giveTIMEslice();
  146.         fclose (fp);
  147.         if (intel (phdr.ph_ver) != 2) {
  148.             unlink (buffer);
  149.             log_line (6, Logmessage[M__NO_MESSAGE_PACKET],buffer);
  150.             return;
  151.         }
  152.     } else unlink (buffer);
  153.     
  154.     if(strstr(name, ".BKT") == NULL)
  155.         log_line(3, Logmessage[M__IS_MESSAGE_PACKET], buffer);
  156. }
  157.     
  158. /*
  159. **    Upack mail packets. Return EMPTY if error.
  160. */
  161.  
  162. BYTE unpack(BYTE *name)
  163. {
  164.     BYTE    arccmd[128],
  165.             arcprogram[128],
  166.             buffer[128];
  167.     WORD    j, type;
  168.     
  169.     struct ffblk blk;
  170.     
  171.     type = ARCtype(name);
  172.     
  173.     log_line(3,Logmessage[M__RECEIVED], ArcMail[type], strupr(name));
  174.     log_line(3,Logmessage[M__SIZE_OF], bundlesize);
  175.     
  176.     switch (type)
  177.     {
  178.         case isUNKNOWN:
  179.             
  180.             take_default:
  181.             
  182.             if (strlen(Archcmdi))
  183.                 sprintf(arccmd, "%s %s %s*.*", Archcmdi, name, pkt_temp);
  184.             else
  185.                 sprintf(arccmd, "x %s %s*.*", name, pkt_temp);
  186.             
  187.             strcpy(arcprogram, Arch);
  188.             
  189.             break;
  190.             
  191.         case isARCmail:
  192.         
  193.             if (strlen(Arc))
  194.             {
  195.                 if (strlen(ArceC))
  196.                     sprintf(arccmd, "%s %s %s*.*", ArceC, name, pkt_temp);
  197.                 else
  198.                     sprintf(arccmd, "x %s %s*.*", name, pkt_temp);
  199.                 
  200.                 strcpy(arcprogram, Arc);
  201.             }
  202.             else goto take_default;
  203.             
  204.             break;
  205.             
  206.         case isLZHmail:
  207.         
  208.             if (strlen(Lzh))
  209.             {
  210.                 if (strlen(LzheC))
  211.                     sprintf(arccmd, "%s %s %s *.*", LzheC, name, pkt_temp);
  212.                 else
  213.                     sprintf(arccmd, "x %s %s*.*", name, pkt_temp);
  214.                 
  215.                 strcpy(arcprogram, Lzh);
  216.             }
  217.             else goto take_default;
  218.             
  219.             break;
  220.             
  221.         case isZIPmail:
  222.         
  223.             if (strlen(Zip))
  224.             {
  225.                 if (strlen(ZipeC))
  226.                     sprintf(arccmd, "%s %s %s *.*", ZipeC, name, pkt_temp);
  227.                 else
  228.                     sprintf(arccmd, "-x %s %s *.*", name, pkt_temp);
  229.                 
  230.                 strcpy(arcprogram, Zip);
  231.             }
  232.             else goto take_default;
  233.             
  234.             break;
  235.             
  236.         case isARJmail:
  237.         
  238.             if (strlen(Arj))
  239.             {
  240.                 if (strlen(ArjeC))
  241.                     sprintf(arccmd, "%s %s %s *.*", ArjeC, name, pkt_temp);
  242.                 else
  243.                     sprintf(arccmd, "x %s %s *.*", name, pkt_temp);
  244.                 
  245.                 strcpy(arcprogram, Arj);
  246.             }
  247.             else goto take_default;
  248.             
  249.             break;
  250.             
  251.         default:
  252.         
  253.             log_line(6,Logmessage[M__TAKE_DEFAULT]);
  254.             goto take_default;
  255.     }
  256.     
  257.     log_line(5,Logmessage[M__EXECUTE_PRG], arcprogram);
  258.     
  259.     if (exec(arcprogram, arccmd, envp, &j) == -1)
  260.     {
  261.         log_line(6,Logmessage[M__CANT_EXEC_PRG], arcprogram);
  262.         return(EMPTY);
  263.     }
  264.     
  265.     if (!j)
  266.     {
  267.         if (keepmsg) rename_arcmail(name);
  268.         else {
  269.         
  270.         /*
  271.         **    If archiver returns 0, and the packet is still
  272.         **    unpacked, don't delete the packet.
  273.         */
  274.         
  275.             sprintf (arcprogram, "%s*.PKT",pkt_temp);
  276.             if (!findfirst(arcprogram, &blk, 0))
  277.                 unlink(name);
  278.             else {
  279.                 
  280.                 /*
  281.                 **    Unpacket but not an PKT file. Delete them.
  282.                 */
  283.                 
  284.                 sprintf(buffer, "%s*.*", pkt_temp);
  285.                 if (!findfirst(buffer, &blk, 0)) {
  286.                     isPKT (blk.ff_name);
  287.                     while (!findnext (&blk)) {
  288.                         isPKT (blk.ff_name);
  289.                     }
  290.                     return (EMPTY);
  291.                 } else {
  292.                 
  293.                     log_line(6, Logmessage[M__NOT_UNPACKED]);
  294.                     return (EMPTY);
  295.                 }
  296.             }
  297.         }
  298.         return (ARCMAIL);
  299.     }
  300.     else
  301.     {
  302.         log_line(5,Logmessage[M__RETURNS_ERRORCODE], j, ", RENAME");
  303.         rename_arcmail(name);
  304.         
  305.         /*
  306.         **    If the archiver returns an errorcode, but there are packets
  307.         **    in the inbound, return there is nothing happened.
  308.         */
  309.         
  310.         sprintf(arccmd, "%s*.PKT", pkt_temp);
  311.         if (!findfirst(arccmd, &blok, 0)) return(ARCMAIL);
  312.         
  313.         return (EMPTY);
  314.     }
  315. }
  316.  
  317. BYTE rename_arcmail(BYTE *name)
  318. {
  319.     BYTE    newname[129],
  320.             *p;
  321.     
  322.     strcpy(newname, name);
  323.     
  324.     if ((p = strrchr(newname, '.')) != NULL)
  325.     {
  326.         strcpy(++p, "000");
  327.         
  328.         while (rename(name, newname))
  329.         {
  330.             *p += 1;
  331.             
  332.             if (*p == 'z')
  333.             {
  334.                 p++;
  335.                 
  336.                 if (!*p)
  337.                 {
  338.                     log_line(4,Logmessage[M__CANT_RENAME],name);
  339.                     return(FALSE);
  340.                 }
  341.             }
  342.         }
  343.     }
  344.     return(TRUE);
  345. }
  346. */
  347. /*
  348. **    Checking diskspace.
  349. */
  350.  
  351. BYTE check_diskspace(LONG space)
  352. {
  353.  
  354. #if defined LATTICE
  355.     struct DISKINFO dinf;
  356. #else
  357.     DISKINFO    dinf;
  358. #endif
  359.     LONG        free;
  360.  
  361. #if defined LATTICE
  362.     if(getdfs(0,&dinf))
  363. #else    
  364.     if (Dfree(&dinf, 0))
  365. #endif
  366.     {
  367.         log_line(6,Logmessage[M__CANT_ASK_DISKFREE]);
  368.         return(FALSE);
  369.     }
  370.     
  371.     free = (dinf.b_free * dinf.b_clsiz * dinf.b_secsiz) - 1024L;
  372.     free -= space;
  373.     
  374.     if (free <= mindiskspace)
  375.     {
  376.         log_line(6,Logmessage[M__DISKSPACE_EXEEDED]);
  377.         return(FALSE);
  378.     }
  379.     
  380.     return(TRUE);
  381. }
  382.  
  383. /*
  384. **    Scanning inbound on *.PKT If found import PKT.
  385. */
  386.  
  387. VOID import_pkt(BYTE *path)
  388. {
  389.     BYTE    buffer[128];
  390.     LONG    sema_access;
  391.     
  392.     sprintf(buffer, "%s*.PKT", path);
  393.  
  394.     if (MTask == MULTITOS) {
  395.         while (1) {
  396.             sema_access = Psemaphore (0, 0x494f535550L, 0L);
  397.             if (sema_access == EACCDN)
  398.                 delay (1000);            
  399.             else
  400.                 break;
  401.         }
  402.     }
  403.     
  404.     if (!findfirst(buffer, &blok, 0))
  405.     {
  406.         if (dodiskcheck && !check_diskspace(blok.ff_fsize)) return;
  407.         
  408.         pktsize = blok.ff_fsize;
  409.         if (pktsize < sizeof (struct _pkthdr)) {
  410.         
  411.             rename_pkt (strcat (path, blok.ff_name));
  412.         } else {
  413.             have_pkt(blok.ff_name, path);
  414.         }
  415.         
  416.         while (!findnext(&blok))
  417.         {
  418.             if (dodiskcheck && !check_diskspace(blok.ff_fsize)) return;
  419.             pktsize = blok.ff_fsize;
  420.         
  421.             if (pktsize < sizeof (struct _pkthdr)) {
  422.             
  423.                 rename_pkt(blok.ff_name);
  424.                 continue;
  425.             }
  426.             
  427.             have_pkt(blok.ff_name, path);
  428.         }
  429.     }
  430.  
  431.     if (dofastimport) close_PKTS();
  432.     first_pass = 0;
  433.     
  434.     /*
  435.     **    The pkt temp. dir must be empty. If someone sends mail
  436.     **    that isn't an PKT, it could be an person who wants to
  437.     **    destroy the system. So delete everything in the temp dir.
  438.     **    This only if the path is the same as pkt temp. dir.
  439.     */
  440.     
  441.     if (!stricmp(path,pkt_temp)) {
  442.         sprintf(buffer, "%s*.*",path);
  443.  
  444.         if (!findfirst(buffer, &blok, 0)) {
  445.             isPKT (blok.ff_name);
  446.             while(!findnext(&blok)) {
  447.                 isPKT (blok.ff_name);
  448.             }
  449.         }
  450.     }
  451.     sema_access = Psemaphore (1, 0x494f535550L, 0L);
  452. }
  453.  
  454. VOID have_pkt(BYTE *pktname, BYTE *path)
  455. {
  456.     BYTE    buffer[128],
  457.             program[128],
  458.             *p, *q,
  459.             temp[10];
  460.     WORD    i;
  461.     
  462.     
  463.     sprintf(buffer, "%s%s", path, pktname);
  464.     
  465.     if ((PACKET = fopen(buffer, "rb")) == NULL)
  466.     {
  467.         log_line(6,Logmessage[M__CANT_OPEN], buffer);
  468.         return;
  469.     }
  470.     
  471.     hdrread(&phdr, sizeof(struct _pkthdr), PACKET);
  472.     
  473.     /*
  474.     **    Init some vars first.
  475.     */
  476.     
  477.     Fpoint = Tpoint = Ffake = Tfake =
  478.     fzone = fnet = fnode = fpoint =
  479.     tzone = tnet = tnode = tpoint = 0;
  480.     
  481.     org_domain[0] = EOS;
  482.     dest_domain [0] = EOS;
  483.     
  484.     if (strlen(thisprogram) != PRGNAMELEN) FOREVER
  485.     
  486.     if ((i = intel(phdr.ph_ver)) != 2)
  487.     {
  488.         log_line(4,Logmessage[M__PACKET_TYPE_ERROR], pktname, i);
  489.         rename_pkt(buffer);
  490.         return;
  491.     }
  492.     
  493.     if ((i= intel(phdr.ph_rate)) == TYPE2_2) {
  494.         memcpy(&phdr2, &phdr, sizeof(struct _phdr2));
  495.         memset(&phdr, 0, sizeof(struct _pkthdr));
  496.         
  497.         pkttype = TYPE2_2;
  498.  
  499.         log_line(6, Logmessage[M__PACKETTYPE], "2.2");
  500.  
  501.         Fpoint = intel(phdr2.ph_opoint);
  502.         Tpoint = intel(phdr2.ph_dpoint);
  503.         Fzone  = intel(phdr2.ph_ozone);
  504.         Tzone  = intel(phdr2.ph_dzone);
  505.         Fnet   = intel(phdr2.ph_onet);
  506.         Tnet   = intel(phdr2.ph_dnet);
  507.         Fnode  = intel(phdr2.ph_onode);
  508.         Tnode  = intel(phdr2.ph_dnode);
  509.         
  510.         phdr.ph_rev   = phdr2.ph_spec_data[1];
  511.         phdr.ph_revMin= phdr2.ph_spec_data[2];
  512.         phdr.ph_prod  = phdr2.ph_prod;
  513.         
  514.         strncpy(phdr.ph_pwd, phdr2.ph_pwd, 8);
  515.  
  516.     }
  517.     
  518.     if (phdr.ph_prod <= maxPRD && maxPRD && phdr.ph_prod != IOSPRDCODE) {
  519.         strcpy(program, PRDcode[phdr.ph_prod]);
  520.         if (pkttype != TYPE2_2) {
  521.             sprintf(&program[strlen(program)], " %d.%02d",
  522.                 (WORD) phdr.ph_rev, (WORD) phdr.ph_revMin);
  523.         }
  524.     }
  525.     else {
  526.         if (phdr.ph_prod == IOSPRDCODE)
  527.         {
  528.             if (phdr.ph_rev || phdr.ph_revMin) 
  529.             {
  530.                 if (!phdr.ph_revMin && pkttype == TYPE_2_2)
  531.                     sprintf(program, "IOSmail %d.%02d",
  532.                         (WORD)phdr.ph_rev, (WORD)phdr.ph_revMin);
  533.  
  534.                 else
  535.                     sprintf(program, "IOSmail %d.%02d", 
  536.                         (WORD)phdr.ph_rev, (WORD)phdr.ph_revMin);
  537.             }
  538.             else
  539.                 strcpy(program, "IOSmail");
  540.         }
  541.         else
  542.             sprintf(program, "program 0x%x", (WORD)phdr.ph_prod);
  543.     }
  544.     
  545.     if (intel(phdr.ph_capable) == phdr.ph_CWcopy &&
  546.         phdr.ph_capable && intel(phdr.ph_capable) & TYPE2PLUS)
  547.     {
  548.         pkttype = TYPE2PLUS;
  549.         log_line(6, Logmessage[M__PACKETTYPE], "2+");
  550.         Fzone = intel(phdr.ph_ozone);
  551.         Fnet = intel(phdr.ph_onet);
  552.         Fnode = intel(phdr.ph_onode);
  553.         Fpoint = intel(phdr.ph_opoint);
  554.         Tzone = intel(phdr.ph_dzone);
  555.         Tnet = intel(phdr.ph_dnet);
  556.         Tnode = intel(phdr.ph_dnode);
  557.         Tpoint = intel(phdr.ph_dpoint);
  558.         
  559.         if (Fpoint && Fnet == (UWORD)(-1))
  560.             Fnet = intel(phdr.ph_auxnet);
  561.     }
  562.     else if (pkttype == STONEAGE)
  563.     {
  564.         log_line(6, Logmessage[M__PACKETTYPE], "2.0");
  565.         Fzone = intel(phdr.ph_qozone);
  566.         Fnet = intel(phdr.ph_onet);
  567.         Fnode = intel(phdr.ph_onode);
  568.         Fpoint = intel(phdr.ph_opoint);
  569.         Tzone = intel(phdr.ph_qdzone);
  570.         Tnet = intel(phdr.ph_dnet);
  571.         Tnode = intel(phdr.ph_dnode);
  572.         Tpoint = intel(phdr.ph_dpoint);
  573.         
  574.         if (!Fzone) Fzone = intel(phdr.ph_ozone);
  575.         if (!Tzone) Tzone = intel(phdr.ph_dzone);
  576.         
  577.         if (!Fzone)
  578.             for (i=0; i < nkey; i++)
  579.             {
  580.                 if (pwd[i].net == Fnet && pwd[i].node == Fnode)
  581.                 {
  582.                     Fzone = pwd[i].zone;
  583.                     break;
  584.                 }
  585.                 
  586.                 if (i >= nkey) Fzone = alias[0].zone;
  587.             }
  588.         
  589.         if (!Tzone)
  590.         {
  591.             for (i=0; i < nalias; i++)
  592.             {
  593.                 if (!strnicmp (dest_domain, alias[i].Domain, 8))
  594.                 {
  595.                     Tzone = alias[i].zone;
  596.                     break;
  597.                 }
  598.                 
  599.                 if (alias[i].pointnet == Tnet)
  600.                 {
  601.                     Tzone = alias[i].zone;
  602.                     Tnet = alias[i].net;
  603.                     Tpoint = Tnode;
  604.                     Tnode = alias[i].node;
  605.                 }
  606.             }
  607.             
  608.             if (i >= nalias) Tzone = alias[0].zone;
  609.         }
  610.     }
  611.     
  612.     if (pkttype == STONEAGE) {
  613.         if (!Tpoint)
  614.             for (i=0; i < nalias; i++)
  615.                 if (Tnet == alias[i].pointnet)
  616.                 {
  617.                     Tzone = alias[i].zone;
  618.                     Tfake = Tnet;
  619.                     Tnet  = alias[i].net;
  620.                     Tpoint= Tnode;
  621.                     Tnode = alias[i].node;
  622.                     
  623.                     break;
  624.                 }
  625.         
  626.         if (!Fpoint)
  627.             for (i=0; i < nalias; i++)
  628.                 if (Fnet == alias[i].pointnet)
  629.                 {
  630.                     Fzone = alias[i].zone;
  631.                     Fpoint= Fnode;
  632.                     Fnode = alias[i].node;
  633.                     Ffake = Fnet;
  634.                     Fnet  = alias[i].net;
  635.                     
  636.                     break;
  637.                 }
  638.     }
  639.     
  640.     /*
  641.     **    Double check the numbers now. If system is unknown then mail
  642.     **    can only imported in the netmail.
  643.     */
  644.     
  645. #if defined EXTERNDEBUG
  646.     if (debugflag)
  647.     {
  648.         log_line(6,">Packetheader contains :");
  649.         log_line(6,">Org %u:%u/%u.%u -> %u:%u/%u.%u", Fzone, Fnet, Fnode, Fpoint, Tzone, Tnet, Tnode, Tpoint);
  650.         log_line(6,">Tfake = %u, Ffake = %u", Tfake, Tfake);
  651.     }
  652. #endif
  653.     
  654.     Fsystem = Tsystem = OUR_SYSTEM;
  655.     
  656.     if (pkttype != TYPE_2_2) {
  657.         for (i=0;i < nkey; i++) {
  658.             if (pwd[i].zone == Fzone && pwd[i].net == Fnet &&
  659.                 pwd[i].node == Fnode && pwd[i].point == Fpoint) {
  660.                 
  661.                 strcpy (org_domain, pwd[i].Domain);
  662.                 strcpy (dest_domain, org_domain);
  663.                 break;
  664.             }
  665.         }
  666.     }
  667.     
  668.     if (!check_nodes(Fzone, Fnet, Fnode, Fpoint))
  669.     {
  670.         log_line(4,Logmessage[M__IMP_UNKNOWN], Fzone, Fnet, Fnode, Fpoint);
  671.         Fsystem = UNKNOWN_SYSTEM;
  672.     }
  673.     else
  674.         log_line(4,Logmessage[M__IMPORT_MESSAGES], Fzone, Fnet, Fnode, Fpoint);
  675.     
  676.     log_line(4,Logmessage[M__ASSEMBLED_ON],
  677.         pktname, intel(phdr.ph_dy), intel(phdr.ph_mo)+1, intel(phdr.ph_yr),
  678.         intel(phdr.ph_hr), intel(phdr.ph_mn), program);
  679.     log_line(2,Logmessage[M__PACKET_SIZE], pktsize);
  680.     
  681.     if (!check_nodes(Tzone, Tnet, Tnode, Tpoint))
  682.     {
  683.         log_line(6,Logmessage[M__DESTANATION_ERROR], Tzone, Tnet, Tnode, Tpoint);
  684.         
  685.         Tsystem = UNKNOWN_SYSTEM;
  686.         
  687.         if (dosecure >= 3)
  688.         {
  689.             rename_pkt(buffer);
  690.             return;
  691.         }
  692.     }
  693.     
  694.     /*
  695.     **    Check for password in pktheader, but first search for
  696.     **    origin address.
  697.     */
  698.     
  699.     if (Fsystem == OUR_SYSTEM)
  700.     {
  701.         for (i=0; i < nkey; i++)
  702.         {
  703.             if (Fzone == pwd[i].zone &&
  704.                 Fnet == pwd[i].net &&
  705.                 Fnode == pwd[i].node &&
  706.                 Fpoint == pwd[i].point) {
  707.                 
  708.                 break;
  709.             }
  710.         }
  711.         
  712.         if (i >= nkey) i = 0;
  713.     }
  714.     
  715.     p = skip_blanks(phdr.ph_pwd);
  716.     
  717.     if (dosecure >= 2 && !strlen(p) && Fsystem == OUR_SYSTEM &&
  718.         strlen(pwd[i].pwd))
  719.     {
  720.         log_line(6,Logmessage[M__PASSWORD_ERROR], "", pwd[i].pwd);
  721.         rename_pkt(buffer);
  722.         
  723.         return;
  724.     }
  725.     
  726.     /*
  727.     **    Get password from header without spaces
  728.     */
  729.     
  730.     for (q = temp; *p && !isspace(*p); *q++ = *p++);
  731.     *q = EOS;
  732.     
  733.     if (dosecure >= 2 && Fsystem == OUR_SYSTEM)
  734.     {
  735.         if (stricmp (pwd[i].pwd, temp)) {
  736.             log_line(6,Logmessage[M__PASSWORD_ERROR], temp, pwd[i].pwd);
  737.             rename_pkt (buffer);
  738.             return;
  739.         }
  740.         if (strnicmp (pwd[i].Domain, org_domain, 8)) {
  741.             log_line (6, "-Domain is incorrect.");
  742.             rename_pkt (buffer);
  743.             return;
  744.         }
  745.     }
  746.     
  747.     /*
  748.     **    Check deny flags
  749.     */
  750.     
  751.     if (DenyOk (Fzone, Fnet, Fnode, Fpoint) == FALSE && Fsystem == OUR_SYSTEM) {
  752.         log_line (6, "*Packet denied.");
  753.         rename_pkt (buffer);
  754.         return;
  755.     }
  756.  
  757.     /*
  758.     **    Initialize files. Set them all to NULL. if opened then those
  759.     **    files area not NULL.
  760.     */
  761.     
  762.     if (!first_pass)
  763.         init_PKTS();
  764.     else if (dofastimport == FALSE)
  765.         init_PKTS();
  766.     
  767.     first_pass = 1;
  768.     
  769.     while (import_messages(buffer));
  770.     
  771.     if (PACKET) fclose(PACKET);
  772.     
  773.     /*
  774.     **    If there was netmail, close the file.
  775.     */
  776.     
  777. #if !defined TEST_CRUNCH    
  778.     unlink(buffer);
  779. #endif
  780.     
  781.     if (dofastimport == FALSE)
  782.         close_PKTS();    
  783. }
  784.  
  785. /*
  786. **    Rename *.PKT > *.BKT
  787. */
  788.  
  789. VOID rename_pkt(BYTE *oldname)
  790. {
  791.     BYTE    newname[128],
  792.             *p;
  793.     
  794.     if (PACKET) fclose(PACKET);
  795.     
  796.     strcpy(newname, oldname);
  797.     
  798.     if ((p = strrchr(newname, '.')) == NULL )
  799.     {
  800.         log_line(6,Logmessage[M__CANT_RENAME], oldname);
  801.         terminate(10);
  802.     }
  803.     
  804.     strcpy (++p, "BAD");
  805. /*    *++p = 'B';*/
  806.     
  807.     if (rename(oldname, newname))
  808.     {
  809.         log_line(6,Logmessage[M__CANT_RENAME], oldname);
  810.         terminate(10);
  811.     }
  812.     
  813.     log_line(6,Logmessage[M__BAD_PACKET_RENAME], oldname);
  814. }
  815.  
  816. /*
  817. **    Check if the node is an known node.
  818. */
  819.  
  820. BYTE check_nodes(UWORD zone, UWORD net, UWORD node, UWORD point)
  821. {
  822.     WORD    i;
  823.     
  824.     for (i=0; i < nalias; i++)
  825.         if (zone == alias[i].zone &&
  826.             net == alias[i].net &&
  827.             node == alias[i].node &&
  828.             !strnicmp (dest_domain, alias[i].Domain, 8)) return(TRUE);
  829.     
  830.     for (i=0; i < nkey; i++)
  831.         if (zone == pwd[i].zone &&
  832.             net == pwd[i].net &&
  833.             node == pwd[i].node &&
  834.             point == pwd[i].point &&
  835.             !strnicmp (org_domain, pwd[i].Domain, 8)) return(TRUE);
  836.     
  837.     return(FALSE);
  838. }
  839.  
  840. /*
  841. **    import message
  842. */
  843.  
  844. BYTE import_messages(BYTE *pktname)
  845. {
  846.     WORD    i;
  847.     
  848.     hdrread(&pmsg, sizeof(struct _pktmsg), PACKET);
  849.     if (feof(PACKET)) return(FALSE);
  850.     
  851.     if ((i = intel(pmsg.pm_ver)) != 2)
  852.     {
  853.         log_line(5,Logmessage[M__ILLEGAL_MESSAGE_TYPE]);
  854.         
  855.         if (!find_message())
  856.         {
  857.             rename_pkt(pktname);
  858.             return(FALSE);
  859.         }
  860.     }
  861.     
  862.     fnet = intel(pmsg.pm_onet);
  863.     fnode = intel(pmsg.pm_onode);
  864.     
  865.     tnet = intel(pmsg.pm_dnet);
  866.     tnode = intel(pmsg.pm_dnode);
  867.     
  868.     /*
  869.     **    Store the zonenumber what is found in header.
  870.     */
  871.     
  872.     if (!fzone) fzone = Fzone;
  873.     if (!tzone) tzone = Tzone;
  874.     
  875.     fpoint = tpoint = 0;
  876.     
  877.     /*
  878.     **    Get 3d numbers.
  879.     */
  880.     
  881.     for (i=0; i < nalias; i++)
  882.         if (fnet == alias[i].pointnet)
  883.         {
  884.             fzone  = alias[i].zone;
  885.             fpoint = fnode;
  886.             fnet   = alias[i].net;
  887.             fnode  = alias[i].node;
  888.             
  889.             break;
  890.         }
  891.     
  892.     if (i >= nalias && !fpoint) fpoint = Fpoint;
  893.     
  894.     for (i=0; i < nalias; i++)
  895.         if (tnet == alias[i].pointnet)
  896.         {
  897.             tzone    = alias[i].zone;
  898.             tpoint    = tnode;
  899.             tnet    = alias[i].net;
  900.             tnode    = alias[i].node;
  901.             
  902.             break;
  903.         }
  904.     
  905.     if (i >= nalias && !tpoint) tpoint = Tpoint;
  906.     
  907. #if defined EXTERNDEBUG
  908.     if (debugflag)
  909.     {
  910.         log_line(6,">Message header contains :");
  911.         log_line(6,">Org %u:%u/%u.%u -> %u:%u/%u.%u", fzone, fnet, fnode, fpoint, tzone, tnet, tnode, tpoint);
  912.         log_line(6,">Strict : %u/%u -> %u/%u", intel(pmsg.pm_onet), intel(pmsg.pm_onode), intel(pmsg.pm_dnet), intel(pmsg.pm_dnode));
  913.     }
  914. #endif
  915.     
  916.     /*
  917.     **    Get the from, to, topic and time field.
  918.     */
  919.     
  920.     if (!get_fields())
  921.     {
  922.         if (feof(PACKET)) return(FALSE);
  923.         return(TRUE);
  924.     }
  925.     
  926.     /*
  927.     **    Now read the entire message.
  928.     */
  929.  
  930. #if defined EXTERNDEBUG
  931.         if (debugflag)
  932.         {
  933.             log_line(6,">Alloc messagebuffer");
  934.             log_line(6,">Memory %ld", coreleft());
  935.         }
  936. #endif
  937.  
  938.     messagebuffer = (BYTE *) myalloc(MAXMSGLENGTH);
  939.     
  940.     read_message();
  941.     
  942.     messagebody = messagebuffer;
  943.     
  944.     /*
  945.     **    Now handle the message body, and copy them.
  946.     */
  947.     
  948.     handle_message(pktname);
  949.  
  950. #if defined EXTERNDEBUG
  951.         if (debugflag)
  952.         {
  953.             log_line(6,">Free messagebuffer");
  954.             log_line(6,">Memory %ld", coreleft());
  955.         }
  956. #endif
  957.     
  958.     free(messagebody);
  959.     
  960.     if (!PACKET || feof(PACKET)) return(FALSE);
  961.     
  962.     if (dodiskcheck && !check_diskspace(0)) return(FALSE);
  963.     
  964.     return(TRUE);
  965. }
  966.  
  967. BYTE find_field_end(VOID)
  968. {
  969.     WORD    i, j = 0;
  970.     
  971.     while ((i = getc(PACKET)) != 0)
  972.     {
  973.         j++;
  974.         giveTIMEslice();
  975.         
  976.         if (feof(PACKET))
  977.         {
  978.             log_line(6,Logmessage[M__UNEXPECTED_END_PKT]);
  979.             return(FALSE);
  980.         }
  981.         
  982.         if (j > 100)
  983.         {
  984.             log_line(6,Logmessage[M__ERROR_MESSAGE]);
  985.             
  986.             while ((i = getc(PACKET)) != 2)
  987.                 giveTIMEslice();
  988.                 if (feof(PACKET))
  989.                 {
  990.                     log_line(6,Logmessage[M__DAMAGED_PACKET]);
  991.                     return(FALSE);
  992.                 }
  993.             
  994.             j = getc(PACKET);
  995.             
  996.             if (j)
  997.             {
  998.                 log_line(6,Logmessage[M__DAMAGED_PACKET]);
  999.                 return(FALSE);
  1000.             }
  1001.             
  1002.             fseek(PACKET, ftell(PACKET)-2, SEEK_SET);
  1003.             return(FALSE);
  1004.         }
  1005.         
  1006.         if (i == 2)
  1007.         {
  1008.             i = getc(PACKET);
  1009.             
  1010.             if (!i)
  1011.             {
  1012.                 fseek(PACKET, ftell(PACKET)-2, SEEK_SET);
  1013.                 log_line(6,Logmessage[M__UNEXPECTED_END_MSG]);
  1014.                 return(FALSE);
  1015.             }
  1016.         }
  1017.     }
  1018.     
  1019.     return(TRUE);
  1020. }
  1021.  
  1022. /*
  1023. **    Get the from, to, topic and time fields from the packet
  1024. **    into global chars.
  1025. */
  1026.  
  1027. BYTE get_fields(VOID)
  1028. {
  1029.     WORD    i, j = 0;
  1030.     BYTE    temp[80],
  1031.             *q = temp;
  1032.     
  1033.     while ((i = getc(PACKET)) != 0)
  1034.     {
  1035.         *q++ = i;
  1036.         j++;
  1037.         giveTIMEslice();
  1038.         
  1039.         if (j > 19)
  1040.         {
  1041.             if (!find_field_end()) return(FALSE);
  1042.             break;
  1043.         }
  1044.     }
  1045.     
  1046.     *q = EOS;
  1047.     
  1048.     /*
  1049.     **    If no time field, create one.
  1050.     */
  1051.     
  1052.     if (!strlen(temp))
  1053.     {
  1054.         time(&secs);
  1055.         tijd = localtime(&secs);
  1056.         strftime(mtime, 20, "%d %b %y %H:%M:%S", tijd);
  1057.     }
  1058.     else strcpy(mtime,temp);
  1059.     
  1060.     q = temp;
  1061.     j = 0;
  1062.     
  1063.     while ((i = getc(PACKET)) != 0)
  1064.     {
  1065.         *q++ = i;
  1066.         j++;
  1067.         
  1068.         if (j > 31)
  1069.         {
  1070.             if (!find_field_end()) return(FALSE);
  1071.             break;
  1072.         }
  1073.     }
  1074.     
  1075.     *q = EOS;
  1076.     
  1077.     /*
  1078.     **    If no to field, copy 'All' to it.
  1079.     */
  1080.     
  1081.     if (!strlen(temp)) strcpy(mto, "All");
  1082.     else strcpy(mto, temp);
  1083.     
  1084.     q = temp;
  1085.     j = 0;
  1086.     
  1087.     while ((i = getc(PACKET)) != 0)
  1088.     {
  1089.         *q++ = i;
  1090.         j++;
  1091.         
  1092.         if (j > 31)
  1093.         {
  1094.             if (!find_field_end()) return(FALSE);
  1095.             break;
  1096.         }
  1097.     }
  1098.     
  1099.     *q = EOS;
  1100.     
  1101.     /*
  1102.     **    If no from field copy unknown
  1103.     */
  1104.     
  1105.     if (!strlen(temp)) strcpy(mfrom, "Unknown");
  1106.     else strcpy(mfrom, temp);
  1107.     
  1108.     /*
  1109.     **    Now for the topic.
  1110.     */
  1111.     
  1112.     q = temp;
  1113.     j = 0;
  1114.     
  1115.     while ((i = getc(PACKET)) != 0)
  1116.     {
  1117.         *q++ = i;
  1118.         j++;
  1119.         
  1120.         if (j > 71)
  1121.         {
  1122.             if (!find_field_end()) return(FALSE);
  1123.             break;
  1124.         }
  1125.     }
  1126.     
  1127.     *q = EOS;
  1128.     
  1129.     if (strlen(temp)) strcpy(mtopic, temp);
  1130.     else *temp = EOS;
  1131.     
  1132.     return(TRUE);
  1133. }
  1134.  
  1135. /*
  1136. **    Read the message byte for byte.
  1137. */
  1138.  
  1139. VOID read_message(VOID)
  1140. {
  1141.     WORD    i, j, c = 0;
  1142.     BYTE    *q = messagebuffer;
  1143.     
  1144. #if defined DEBUG
  1145.     printf("*Reading message.\n");
  1146. #endif
  1147.     
  1148.     while ((i = getc(PACKET)) != 0 && c < MAXMSGLENGTH)
  1149.     {
  1150.         /*
  1151.         **    Not 0 terminated and end of file reached. Returning with
  1152.         **    we have already.
  1153.         */
  1154.  
  1155.         giveTIMEslice();
  1156.         
  1157.         if (feof(PACKET))
  1158.         {
  1159.             log_line(6,Logmessage[M__UNEXPECTED_END_PKT]);
  1160.             return;
  1161.         }
  1162.         
  1163.         /*
  1164.         **    This tokens are not needed in a message.
  1165.         */
  1166.         
  1167.         if (i == 0x8d || i == 0x8f || i == 0x7f) continue;
  1168.         
  1169.         /*
  1170.         **    If we have found 0x02 then this can be the start of the
  1171.         **    next message header. Maybe they have only 0x02 and the
  1172.         **    next byte is an normal token, not 0, then continue reading.
  1173.         */
  1174.         
  1175.         if (i == 2)
  1176.         {
  1177.             j = getc(PACKET);
  1178.             
  1179.             /*
  1180.             **    It is indeed the start of an next message.
  1181.             */
  1182.             
  1183.             if (!j)
  1184.             {
  1185.                 log_line(6,Logmessage[M__UNEXPECTED_END_MSG]);
  1186.                 fseek(PACKET, ftell(PACKET)-2, SEEK_SET);
  1187.                 *q = EOS;
  1188.                 return;
  1189.             }
  1190.             
  1191.             /*
  1192.             **    Carriage return ? will be linefeed.
  1193.             */
  1194.             
  1195.             if (i != '\n')
  1196.             {
  1197.                 if (i == '\r') i = '\n';
  1198.                 *q++ = i;
  1199.             } 
  1200.             
  1201.             if (j != '\n')
  1202.             {
  1203.                 if (j == '\r') j = '\n';
  1204.                 *q++ = j;
  1205.             }
  1206.             
  1207.             c += 2;
  1208.         }
  1209.         
  1210.         if (i != '\n')
  1211.         {
  1212.             if (i == '\r') i = '\n';
  1213.             *q++ = i;
  1214.         }
  1215.         
  1216.         c++;
  1217.     }
  1218.     
  1219.     if (c >= MAXMSGLENGTH - 200)
  1220.     {
  1221.         log_line(6,Logmessage[M__MSG_TO_LONG]);
  1222.         q = &messagebuffer[MAXMSGLENGTH - 250];
  1223.         q = skip_to_blank(q);
  1224.     }
  1225.     
  1226.     /*
  1227.     **    And terminate the message.
  1228.     */
  1229.     
  1230.     *q = EOS;
  1231.     
  1232. #if defined DEBUG
  1233.     printf("*Got message.\n");
  1234. #endif
  1235. }
  1236.  
  1237. /*
  1238. **    Finding the next message header is a tough thing. Trying to find
  1239. **    the pkt header type 2.
  1240. */
  1241.  
  1242. BYTE find_message(VOID)
  1243. {
  1244.     WORD    i, j;
  1245.     LONG    z = 1;
  1246.     
  1247.     fseek(PACKET, (ftell(PACKET) - sizeof(struct _pktmsg))+1, SEEK_SET);
  1248.     i = getc(PACKET);
  1249.     
  1250.     while (!feof(PACKET))
  1251.     {
  1252.         giveTIMEslice();
  1253.         if (feof(PACKET))
  1254.         {
  1255.             log_line(5,Logmessage[M__END_OF_PACKET], z);
  1256.             return(FALSE);
  1257.         }
  1258.         
  1259.         /*
  1260.         **    If we found it read the PKT header and return 1.
  1261.         */
  1262.                 
  1263.         if (i == 2)
  1264.         {
  1265.             j = getc(PACKET);
  1266.             
  1267.             if (!j)
  1268.             {
  1269.                 log_line(4,Logmessage[M__MSG_FOUND], z);
  1270.                 
  1271.                 fseek(PACKET, (ftell(PACKET) - 2L), SEEK_SET);
  1272.                 hdrread(&pmsg, sizeof(struct _pktmsg), PACKET);
  1273.                 
  1274.                 return(TRUE);
  1275.             }
  1276.             
  1277.             ungetc(j, PACKET);
  1278.         }
  1279.         
  1280.         /*
  1281.         **    And get 1 bytes again.
  1282.         */
  1283.         
  1284.         i = getc(PACKET);
  1285.         z++;
  1286.     }
  1287.     
  1288.     log_line(6,Logmessage[M__NO_MSG_ANYMORE], z);
  1289.     
  1290.     return(FALSE);
  1291. }
  1292.  
  1293. /*
  1294. **    Handle the message and save it.
  1295. */
  1296.  
  1297. VOID handle_message(BYTE *pktname)
  1298. {
  1299.     WORD    i, j,
  1300.             toarea = -1,
  1301.             oldarea;
  1302.     UWORD    fmpt, topt,                        /* FMPT, TOPT        */
  1303.             izone, inet, inode,                /* INTL line?        */
  1304.             mzone, mnet, mnode, mpoint;        /* MSGID            */
  1305.     BYTE    *newmsg,
  1306.             *p, *q,
  1307.             *expmsg,
  1308.             temp[100],
  1309.             oldname[100],
  1310.             buffer[250],
  1311.             bounced_message = FALSE,        /* Message bounced?    */
  1312.             Unknown_area = FALSE,
  1313.             *area;                            /* Areaname            */
  1314.     MSGHEADER    fhdr;                        /* Message header    */
  1315.     
  1316.     memset(&fhdr, 0, sizeof(MSGHEADER));
  1317.     
  1318.     for (i=0; i < nalias; i++)
  1319.         if (fnet == alias[i].pointnet && fzone == alias[i].zone)
  1320.         {
  1321.             fnet = alias[i].net;
  1322.             fnode =alias[i].node;
  1323.             fpoint = fnode;
  1324.             break;
  1325.         }
  1326.     
  1327.     for (i=0; i < nalias; i++)
  1328.         if (tnet == alias[i].pointnet && tzone == alias[i].zone)
  1329.         {
  1330.             tnet = alias[i].net;
  1331.             tnode= alias[i].node;
  1332.             tpoint = tnode;
  1333.             break;
  1334.         }
  1335.     
  1336.     fhdr.Ozone = fzone;
  1337.     fhdr.Onet = fnet;
  1338.     fhdr.Onode = fnode;
  1339.     fhdr.Opoint = fpoint;
  1340.     
  1341.     fhdr.Dzone = tzone;
  1342.     fhdr.Dnet = tnet;
  1343.     fhdr.Dnode = tnode;
  1344.     fhdr.Dpoint = tpoint;
  1345.     
  1346.     strcpy(fhdr.from, mfrom);
  1347.     strcpy(fhdr.to, mto);
  1348.     strcpy(fhdr.topic, mtopic);
  1349.     strcpy(fhdr.time, mtime);
  1350.     
  1351.     fhdr.create = time(NULL);
  1352.     fhdr.flags = intel(pmsg.pm_attr);
  1353.     
  1354.     fhdr.flags &= ~ ALLBITS;
  1355.     
  1356.     /*
  1357.     **    Calculate reply chain CRCs
  1358.     */
  1359.  
  1360. #if defined EXTERNDEBUG
  1361.         if (debugflag)
  1362.         {
  1363.             log_line(6, ">Get reply chain");
  1364.             log_line(6,">Memory %ld", coreleft());
  1365.         }
  1366. #endif
  1367.     
  1368.     reply_chain(messagebody, &fhdr, UPCOST);
  1369.     
  1370.     
  1371.     /*
  1372.     **    Get destination area
  1373.     */
  1374.     
  1375.     area = get_area(messagebody);
  1376.  
  1377. #if defined EXTERNDEBUG
  1378.         if (debugflag)
  1379.         {
  1380.             log_line(6, ">Have area %s", (area != NULL) ? area : "MAIL");
  1381.         }
  1382. #endif
  1383.     
  1384.     if (area != NULL)
  1385.         for (i=0; i < msgareas; i++)
  1386.             if (!stricmp(area, Areaname[i]))
  1387.             {
  1388.                 if (!stricmp(area, "MAIL"))
  1389.                 {
  1390.                     area = NULL;
  1391.                     break;
  1392.                 }
  1393.                 
  1394.                 toarea = i;
  1395.                 hprintf(S_AREA, "%s", area);
  1396.                 hprintf (S_COMMENT, "Importing echomail.");
  1397.                 break;
  1398.             }
  1399.     
  1400.     hprintf(S_DEST, "%u:%u/%u.%u", fhdr.Dzone, fhdr.Dnet,
  1401.         fhdr.Dnode, fhdr.Dpoint);
  1402.     hprintf(S_FROM, "%u:%u/%u.%u", fhdr.Ozone, fhdr.Onet,
  1403.         fhdr.Onode, fhdr.Opoint);
  1404.  
  1405.     /*
  1406.     **    Save it for later. If arealine found and the area is not
  1407.     **    found in the AREAS.BBS save this message to trasharea.
  1408.     */
  1409.     
  1410.     if (area != NULL && i >= msgareas) {
  1411.         Unknown_area = TRUE;
  1412.         toarea = -2;
  1413.     }
  1414.     
  1415.     /*
  1416.     **    If we want the realname and in the message is ^aREALNAME:
  1417.     **    copy it
  1418.     */
  1419.     
  1420.     strcpy(oldname, fhdr.from);
  1421.     
  1422.     if (dorealname & REALIN && (p = strline(messagebody, "\01REALNAME:")) != NULL)
  1423.     {
  1424. #if defined EXTERNDEBUG
  1425.         if (debugflag)
  1426.         {
  1427.             log_line(6, ">Have REALNAME kludge");
  1428.         }
  1429. #endif
  1430.         p = skip_to_token(p, ':');
  1431.         p = skip_blanks(p);
  1432.         
  1433.         for (q = temp; *p && *p != '\n'; *q++ = *p++);
  1434.         *q = EOS;
  1435.         
  1436.         strcpy(fhdr.from, temp);
  1437.     }
  1438.  
  1439.     giveTIMEslice();
  1440.     
  1441.     /*
  1442.     **    This is netmail. Get INTL, TOPT and FMPT.
  1443.     **    -----------------------------------------
  1444.     */
  1445.     
  1446.     if (area == NULL)
  1447.     {
  1448.         /*
  1449.         **    Get From point.
  1450.         */
  1451.         
  1452.         hprintf (S_COMMENT, "Importing NETMAIL");
  1453.         
  1454.         fmpt = get_point(messagebody, "\01FMPT");
  1455.         
  1456.         if (fmpt == (UWORD)-1) fmpt = get_point(messagebody, "FMPT");
  1457.         if (fmpt != (UWORD)-1) fhdr.Opoint = fmpt;
  1458.         
  1459.         /*
  1460.         **    and To point.
  1461.         */
  1462.         
  1463.         topt = get_point(messagebody, "\01TOPT");
  1464.         
  1465.         if (topt == (UWORD)-1) topt = get_point(messagebody, "TOPT");
  1466.         if (topt != (UWORD)-1) fhdr.Dpoint = topt;
  1467.         
  1468.         if (get_interzone(messagebody, &mzone, &mnet, &mnode, &izone, &inet, &inode))
  1469.         {
  1470.             for (i=0; i < nalias; i++)
  1471.                 if (inet == alias[i].pointnet &&
  1472.                     inode == alias[i].point &&
  1473.                     izone == alias[i].zone) break;
  1474.             
  1475.             if (i >= nalias)
  1476.             {
  1477.                 fhdr.Dzone = izone;
  1478.                 fhdr.Dnet = inet;
  1479.                 fhdr.Dnode = inode;
  1480.             }
  1481.             else
  1482.             {
  1483.                 fhdr.Dzone = alias[i].zone;
  1484.                 fhdr.Dnet = alias[i].net;
  1485.                 fhdr.Dnode = alias[i].node;
  1486.                 fhdr.Dpoint = alias[i].point;
  1487.             }
  1488.             
  1489.             for (i=0; i < nalias; i++) {
  1490.                 if (mnet == alias[i].pointnet &&
  1491.                     mzone== alias[i].zone) break;
  1492.             }
  1493.             
  1494.             if (i >= nalias) {
  1495.                 fhdr.Ozone = mzone;
  1496.                 fhdr.Onet = mnet;
  1497.                 fhdr.Onode = mnode;
  1498.             } else {
  1499.                 fhdr.Ozone = alias[i].zone;
  1500.                 fhdr.Onet  = alias[i].net;
  1501.                 fhdr.Onode = alias[i].node;
  1502.                 fhdr.Opoint= mnet;
  1503.             }
  1504.         }
  1505.         
  1506.         if (domsg)
  1507.         {
  1508.             i = get_reply(messagebody, &mzone, &mnet, &mnode, &mpoint);
  1509.             
  1510.             if (i && mzone != fhdr.Ozone && mnet != fhdr.Onet &&
  1511.                 mnode != fhdr.Onode && mpoint != fhdr.Opoint)
  1512.             {
  1513.                 fhdr.Dzone = mzone;
  1514.                 fhdr.Dnet = mnet;
  1515.                 fhdr.Dnode = mnode;
  1516.                 fhdr.Dpoint = mpoint;
  1517.             }
  1518.             
  1519.             i = get_msgid(messagebody, &mzone, &mnet, &mnode, &mpoint);
  1520.             
  1521.             if (i && mzone != fhdr.Dzone && mnet != fhdr.Dnet &&
  1522.                 mnode != fhdr.Dnode && mpoint != fhdr.Dpoint)
  1523.             {
  1524.                 fhdr.Ozone = mzone;
  1525.                 fhdr.Onet = mnet;
  1526.                 fhdr.Onode = mnode;
  1527.                 fhdr.Opoint = mpoint;
  1528.             }
  1529.         }
  1530.         
  1531.         hprintf(S_DEST, "%u:%u/%u.%u", fhdr.Dzone, fhdr.Dnet,
  1532.             fhdr.Dnode, fhdr.Dpoint);
  1533.         hprintf(S_FROM, "%u:%u/%u.%u", fhdr.Ozone, fhdr.Onet,
  1534.             fhdr.Onode, fhdr.Opoint);
  1535.         
  1536.         messagebuffer = strip_cr(messagebuffer);
  1537.         
  1538.         /*
  1539.         **    Check on right route if point is 0 and name is
  1540.         **    not sysop name.
  1541.         */
  1542.         
  1543.         if (docheckname) {
  1544.         /*
  1545.             if (stricmp(fhdr.to, SysOp) && stricmp(fhdr.to, "sysop") &&
  1546.                 !fhdr.Dpoint)
  1547.         */
  1548.             for (i=0; i < nkey; i++)
  1549.             {
  1550.                 if (!stricmp(fhdr.to, pwd[i].Uname) &&
  1551.                     pwd[i].zone == fhdr.Dzone && 
  1552.                     !strnicmp (pwd[i].Domain, dest_domain, 8))
  1553.                 {
  1554.                     fhdr.Dzone = pwd[i].zone;
  1555.                     fhdr.Dnet = pwd[i].net;
  1556.                     fhdr.Dnode = pwd[i].node;
  1557.                     fhdr.Dpoint = pwd[i].point;
  1558.                     break;
  1559.                 }
  1560.             }
  1561.         }
  1562.         
  1563.         /*
  1564.         **    Readdress mail.
  1565.         */
  1566.         
  1567.         DO_readdress (&fhdr, messagebuffer);
  1568.         
  1569.         /*
  1570.         **    And print the numbers.
  1571.         */
  1572.         
  1573.         if (!doniceout && !doquit)
  1574.         {
  1575.             gprintf("%-20.20s %-20.20s MAIL %u:%u/%u.%u > %u:%u/%u.%u\n",
  1576.                 fhdr.from, fhdr.to,
  1577.                 fhdr.Ozone, fhdr.Onet, fhdr.Onode, fhdr.Opoint,
  1578.                 fhdr.Dzone, fhdr.Dnet, fhdr.Dnode, fhdr.Dpoint);
  1579.  
  1580.         }
  1581.         else
  1582.         {
  1583.             log_line(3,Logmessage[M__NETMAIL_MSG],
  1584.                 mfrom, mto, fhdr.Ozone, fhdr.Onet, fhdr.Onode, fhdr.Opoint,
  1585.                 fhdr.Dzone, fhdr.Dnet, fhdr.Dnode, fhdr.Dpoint);
  1586.         }
  1587. /*        
  1588.         if (doquit)
  1589.             hprintf(S_COMMENT, "%-20s %-20s", mfrom, mto);
  1590. */        
  1591.         /*
  1592.         **    Hier stond het killsend gedoe. Verplaatst naar beneden na
  1593.         **    het schrijven van de pkt.
  1594.         */
  1595.                 
  1596.         if (!strlen(messagebuffer))
  1597.         {
  1598.             /*
  1599.             **    Deze berichten, die leeg zijn, bevatten meestal
  1600.             **    een fileattach van een DOS util zoals TIC.
  1601.             **    In de toekomst kan hier een soort filemanager staan.
  1602.             */
  1603.             
  1604.             log_line(4,Logmessage[M__EMPTY_MSG]);
  1605.             strcpy(messagebuffer, "---\n");
  1606.         }
  1607.         
  1608.         strcat(messagebuffer, "\n");
  1609.         
  1610.         if (doknown && dopkt)
  1611.             pack_netmail(&fhdr, messagebuffer);
  1612.  
  1613.         /*
  1614.         **    KILLSEND messages which are not for us or our points
  1615.         **    and not adressed to us of our points.
  1616.         **    If dokillall is true, kill all mail not from/to us.
  1617.         **    Also points.
  1618.         **
  1619.         **    ATTENTIE, onderstaande code is verplaatst.
  1620.         */
  1621.         
  1622.         if ((dokillsent || dokillall) &&
  1623.             ouralias(fhdr.Dzone, fhdr.Dnet, fhdr.Dnode, (dokillall) ? fhdr.Dpoint : 0) &&
  1624.             ouralias(fhdr.Ozone, fhdr.Onet, fhdr.Onode, (dokillall) ? fhdr.Opoint : 0))
  1625.         {
  1626.              if (dopkt)
  1627.                  fhdr.flags |= DELETED;
  1628.              else
  1629.                  fhdr.flags |= KILLSEND;
  1630.         }
  1631.         
  1632.         savemsg(TONETMAIL, &fhdr, 0, messagebuffer, 0, 0, 0, 0);
  1633.         nnetmails++;
  1634.         nimported++;
  1635.         
  1636.         hprintf(S_IMP, "%d", nimported);
  1637.         hprintf(S_NETMAIL, "%d", nnetmails);
  1638.         
  1639.         if (!stricmp(fhdr.to, SysOp) || !stricmp(fhdr.to, "sysop"))
  1640.         {
  1641.             nnet_priv++;
  1642.             nprivate++;
  1643.             
  1644.             hprintf(S_SYSOP, "%d", nprivate);
  1645.             
  1646.             if (dopvtmail)
  1647.                 savemsg(TOPRIVATE, &fhdr, -1, messagebuffer, 0, 0, 0, 0);
  1648.         }
  1649.  
  1650. #if defined AREAFIX
  1651.         if (areafix_on && (!strnicmp (fhdr.to, "areafix", 7) || 
  1652.             !strnicmp (fhdr.to, "IOSmail", 7))) {
  1653.             consult_areafix(&fhdr, messagebuffer);
  1654.         } else 
  1655. #endif
  1656.             if (doareafix && !strnicmp(hdr.to, "areafix", 7)) areafixmsgs ++;
  1657.         
  1658.         return;
  1659.     }
  1660.  
  1661.     /*
  1662.     **    We are importing from inbound ?
  1663.     **    If we are using several inbounds then...
  1664.     */
  1665.     
  1666.     if (we_have_inbound == KNOWNINBOUND) toarea = -2;
  1667.     
  1668.     if (we_have_inbound == NORMALINBOUND)
  1669.     {
  1670.         rename_pkt(pktname);
  1671.         PACKET = NULL;
  1672.         return;
  1673.     }
  1674.     
  1675.     /*
  1676.     **    If it's possible that zone addressing is mixed,we searching
  1677.     **    than for the correct zone.
  1678.     */
  1679.     
  1680.     if (domulti_reg)
  1681.     {
  1682.         
  1683. #if defined EXTERNDEBUG
  1684.         if (debugflag)
  1685.         {
  1686.             log_line(6,">Mixed multizone check:");
  1687.             log_line(6,">Was : %u:%u/%u.%u ->%u:%u/%u.%u", fhdr.Ozone, fhdr.Onet, fhdr.Onode, fhdr.Opoint, fhdr.Dzone, fhdr.Dnet, fhdr.Dnode, fhdr.Dpoint);
  1688.         }
  1689. #endif
  1690.         if (other_zone((UWORD *) &fhdr.Dzone, (UWORD *) &fhdr.Dnet,
  1691.             (UWORD *) &fhdr.Dnode, (UWORD *) &fhdr.Ozone,
  1692.             (UWORD *) &fhdr.Onet, (UWORD *) &fhdr.Onode) == 1)
  1693.             log_line(4,Logmessage[M__ORG_ADDRESS_UNKNOWN]);
  1694.         
  1695. #if defined EXTERNDEBUG
  1696.         if (debugflag) log_line(6,">Is : %u:%u/%u.%u ->%u:%u/%u.%u", fhdr.Ozone, fhdr.Onet, fhdr.Onode, fhdr.Opoint, fhdr.Dzone, fhdr.Dnet, fhdr.Dnode, fhdr.Dpoint);
  1697. #endif
  1698.     }
  1699.     
  1700.     /*
  1701.     **    Starting here to import in the normal echomail area's
  1702.     **    -----------------------------------------------------
  1703.     **
  1704.     **    If the sender using our alias, this could be an 4 dim. address
  1705.     **    with an FMPT line in the message. Only is the destination is
  1706.     **    0, thus no point.
  1707.     */
  1708.     
  1709.     if (!ouralias(fhdr.Ozone, fhdr.Onet, fhdr.Onode, fhdr.Opoint))
  1710.     {
  1711.         /*
  1712.         **    If we are an point, and no pointnumber at our system
  1713.         **    get TOPT.
  1714.         */
  1715.         
  1716.         if (!fhdr.Dpoint)
  1717.         {
  1718.             topt = get_point(messagebody, "\01TOPT");
  1719.             
  1720.             if (topt == (UWORD)-1) topt = get_point(messagebody, "TOPT");
  1721.             if (topt != (UWORD)-1) fhdr.Dpoint = tpoint = topt;
  1722.         }
  1723.         
  1724.         /*
  1725.         **    Origin have point number 0 and the nodenumber is our
  1726.         **    alias.
  1727.         */
  1728.         
  1729.         fmpt = get_point(messagebody, "\01FMPT");
  1730.         
  1731.         if (fmpt == (UWORD)-1)
  1732.             if ((fmpt = get_point(messagebody, "FMPT")) == (UWORD)-1)
  1733.             {
  1734.                 /*
  1735.                 **    We don't find any FMPT line. He is using our node
  1736.                 **    number. Maybe an INTL line ?
  1737.                 */
  1738.                 
  1739.                 if (get_interzone(messagebody, &mzone, &mnet, &mnode,
  1740.                     &izone, &inet, &inode))
  1741.                 {
  1742.                     fhdr.Dzone = izone;
  1743.                     fhdr.Dnet = inet;
  1744.                     fhdr.Dnode = inode;
  1745.                     
  1746.                     fhdr.Ozone = mzone;
  1747.                     fhdr.Onet = mnet;
  1748.                     fhdr.Onode = mnode;
  1749.                 }
  1750.             }
  1751.             else fhdr.Opoint = fpoint = fmpt;
  1752.         else fhdr.Opoint = fpoint = fmpt;
  1753.         
  1754.         /*
  1755.         **    It's impossible that this message is from point to point.
  1756.         **    Check our aliases if this message is from an point.
  1757.         */
  1758.         
  1759.         if (fhdr.Opoint && fhdr.Dpoint)
  1760.         {
  1761.             for (i=0; i < nalias; i++)
  1762.                 if (alias[i].zone == fhdr.Dzone &&
  1763.                     alias[i].net == fhdr.Dnet &&
  1764.                     alias[i].node == fhdr.Dnode &&
  1765.                     alias[i].point == fhdr.Dpoint)
  1766.                 {
  1767.                     /*
  1768.                     **    The destination is a point, not a node.
  1769.                     */
  1770.                 
  1771.                     fhdr.Opoint = 0;
  1772.                     break;
  1773.                 }
  1774.             
  1775.             /*
  1776.             **    The destination is an node, not a point.
  1777.             */
  1778.             
  1779.             if (i >= nalias) fhdr.Dpoint = 0;
  1780.         }
  1781.         hprintf(S_DEST, "%u:%u/%u.%u", fhdr.Dzone, fhdr.Dnet,
  1782.             fhdr.Dnode, fhdr.Dpoint);
  1783.         hprintf(S_FROM, "%u:%u/%u.%u", fhdr.Ozone, fhdr.Onet,
  1784.             fhdr.Onode, fhdr.Opoint);
  1785.     }
  1786.     else
  1787.         if (get_interzone(messagebody, &mzone, &mnet, &mnode, &izone, &inet, &inode))
  1788.             for (i=0; i < nalias; i++)
  1789.                 if ((izone == alias[i].zone && inet == alias[i].net && inode == alias[i].node) ||
  1790.                     (inet == alias[i].pointnet && inode == alias[i].point))
  1791.                 {
  1792.                     fmpt = get_point(messagebody, "\01FMPT");
  1793.                     topt = get_point(messagebody, "\01TOPT");
  1794.                     
  1795.                     if (inet != alias[i].pointnet)
  1796.                     {
  1797.                         fhdr.Dzone = izone;
  1798.                         fhdr.Dnet = inet;
  1799.                         fhdr.Dnode = inode;
  1800.                         
  1801.                         if (topt != (UWORD)-1) fhdr.Dpoint = topt;
  1802.                     }
  1803.                     else
  1804.                     {
  1805.                         fhdr.Dzone = alias[i].zone;
  1806.                         fhdr.Dnet = alias[i].net;
  1807.                         fhdr.Dnode = alias[i].node;
  1808.                         fhdr.Dpoint = inode;
  1809.                     }
  1810.                     
  1811.                     for (j=0; j < nkey; j++)
  1812.                         if (pwd[j].zone == mzone && pwd[j].net == mnet && pwd[j].node == mnode)
  1813.                         {
  1814.                             fhdr.Ozone = mzone;
  1815.                             fhdr.Onet = mnet;
  1816.                             fhdr.Onode = mnode;
  1817.                             
  1818.                             if (fmpt != (UWORD)-1 &&
  1819.                                 fmpt == pwd[j].point) fhdr.Opoint = fmpt;
  1820.                             
  1821.                             break;
  1822.                         }
  1823.                 }
  1824.                     
  1825.     /*
  1826.     **    Security.
  1827.     **
  1828.     **    If system where message(s) come from are echomail
  1829.     **    message(s), then the message(s) are in the trasharea.
  1830.     */
  1831.     
  1832.     if (dosecure && area != NULL && Fsystem == UNKNOWN_SYSTEM)
  1833.         toarea = -2;
  1834.     
  1835.     /*
  1836.     **    If messages in an area where the sender is not connected to
  1837.     **    then his message(s) are in the trasharea.
  1838.     */
  1839.     
  1840.     if (dosecure && area != NULL && toarea != -1 && toarea != -2 &&
  1841.         !get_secure(toarea, fhdr.Ozone, fhdr.Onet, fhdr.Onode, fhdr.Opoint))
  1842.     {
  1843.         log_line(4,Logmessage[M__NOT_CONNECTED],
  1844.             fhdr.Ozone, fhdr.Onet, fhdr.Onode, fhdr.Opoint, area);
  1845.         
  1846.         toarea = -2;
  1847.     }
  1848.     
  1849.     /*
  1850.     **    Get the ^aMSGID an ^aREPLY if wanted.
  1851.     */
  1852.     
  1853.     if (domsg)
  1854.     {
  1855.         i = get_reply(messagebody, &mzone, &mnet, &mnode, &mpoint);
  1856.         
  1857.         if (i)
  1858.         {
  1859.             fhdr.Dzone = mzone;
  1860.             fhdr.Dnet = mnet;
  1861.             fhdr.Dnode = mnode;
  1862.             fhdr.Dpoint = mpoint;
  1863.         }
  1864.         
  1865.         i = get_msgid(messagebody, &mzone, &mnet, &mnode, &mpoint);
  1866.         
  1867.         if (i)
  1868.         {
  1869.             fhdr.Ozone = mzone;
  1870.             fhdr.Onet = mnet;
  1871.             fhdr.Onode = mnode;
  1872.             fhdr.Opoint = mpoint;
  1873.         }
  1874.         hprintf(S_DEST, "%u:%u/%u.%u", fhdr.Dzone, fhdr.Dnet,
  1875.             fhdr.Dnode, fhdr.Dpoint);
  1876.         hprintf(S_FROM, "%u:%u\%u.%u", fhdr.Ozone, fhdr.Onet,
  1877.             fhdr.Onode, fhdr.Opoint);
  1878.     }
  1879.  
  1880.     /*
  1881.     **    Duplicate msg test
  1882.     */
  1883.     
  1884.     if (doNOdupecheck == FALSE) {
  1885.         if (get_dupes(messagebody, toarea) == TRUE) {
  1886.             log_line(4,Logmessage[M__DUP_MSG],fhdr.from, 
  1887.                 (toarea >= 0) ? Areaname[toarea] : "TRASH");
  1888.             dupesave(&fhdr, messagebody);
  1889.             HaveDUPES = TRUE;
  1890.             hprintf(S_COMMENT, "Duplicate message.");
  1891.             return;
  1892.         }
  1893.     }
  1894.     
  1895.     if (!doniceout && !doquit) {
  1896.         gprintf("%-20.20s %-20.20s %20.20s\n", mfrom, mto, area);
  1897.     } else if (StatusScreen)
  1898.         log_line(4,"*%-15.15s > %-15.15s %-15.15s", mfrom, mto, area);
  1899. /*    
  1900.     if (doquit)
  1901.         hprintf (S_COMMENT, "%-20s %-20s", mfrom, mto);
  1902. */    
  1903.     /*
  1904.     **    Not allowed to send echomail? Trash his mail.
  1905.     */
  1906.     
  1907.     for (i=0; i < nkill; i++)
  1908.         if (!stricmp(fhdr.from, Killfrom[i]))
  1909.         {
  1910.             if (!doniceout && !doquit) {
  1911.                 gprintf("Kill");
  1912.             } else
  1913.                 log_line(2,Logmessage[M__TRASH_MSG]);
  1914.             
  1915.             toarea = -1;
  1916.             break;
  1917.         
  1918.         }
  1919.         
  1920.     /*
  1921.     **    Normal mail. Import it in the echomail areas and export it
  1922.     **    if we have nodes and points under us.
  1923.     */
  1924.     
  1925.     if (toarea != -1 && toarea != -2)
  1926.     {
  1927.     
  1928.         /*
  1929.         **    If auto added, start here again.
  1930.         */
  1931.         
  1932.         AUTO_area:
  1933.         
  1934.         /*
  1935.         **    Get the pathlines.
  1936.         */
  1937.         
  1938.         expmsg = skip_to_token(messagebody, '\n');
  1939.         
  1940.         npath = 0;
  1941.         get_path(messagebody, toarea);
  1942.  
  1943.         /*
  1944.         **    If message from our points, check the path on illegal
  1945.         **    PATH number.
  1946.         */
  1947.         
  1948.         if (npath <= 1) redo_PATH();
  1949.         
  1950.         /*
  1951.         **    Check ^aPATH. If message is bounced, don't send
  1952.         **    the message to an other node.
  1953.         */
  1954.         
  1955.         if (dopathcheck && !checkPATH) bounced_message = TRUE;
  1956.         
  1957.         nseenby = 0;
  1958.         
  1959.         get_seen(messagebody, toarea);
  1960.         get_org_and_tearline(messagebody);
  1961.         
  1962.         if (strlen (org_domain))
  1963.             i = ourseen (toarea, 1, &fhdr, org_domain);
  1964.         else
  1965.             i = ourseen(toarea, 1, &fhdr, NULL);
  1966.  
  1967. #if defined EXTERNDEBUG
  1968.         if (debugflag)
  1969.         {
  1970.             log_line(6,">Alloc newmsg");
  1971.             log_line(6,">Memory %ld", coreleft());
  1972.         }
  1973. #endif
  1974.  
  1975.         newmsg = (BYTE *) myalloc(MAXMSGLENGTH);
  1976.  
  1977.         if (dostripINTL) {        
  1978.             expmsg = strip_line(expmsg, "\01FMPT");
  1979.             expmsg = strip_line(expmsg, "\01TOPT");
  1980.             expmsg = strip_line(expmsg, "\01INTL");
  1981.         }
  1982.         
  1983.         strcpy(newmsg, expmsg);
  1984.         
  1985.         newmsg = strip_line(newmsg, "\01EID:");
  1986.         
  1987.         /*
  1988.         ** If the origin line doesn't contain an linefeed,
  1989.         ** add an linefeed. Needed for RA echo.
  1990.         **    Eruit gehaald omdat berichten onder een SEEN-BY niet
  1991.         **    meer te zien waren.
  1992.         */
  1993.  
  1994. /*        
  1995.         p = strstr(newmsg,"SEEN-BY");
  1996.         if (p) {
  1997.             p--;
  1998.             if (*p == '\n' || *p == '\r')
  1999.                 *p = EOS;
  2000.             if (*p == '\01') *p = EOS;
  2001.         }
  2002. */
  2003.         
  2004.         if (newmsg[ strlen(newmsg) - 1 ] != '\n') strcat(newmsg,"\n");
  2005.         
  2006.         Addline(newmsg, (doifna) ? "\01SEEN-BY" : "SEEN-BY:", i-1, tzone);
  2007.         
  2008.         if (strlen (org_domain))
  2009.             i = ourpath (toarea, org_domain);
  2010.         else
  2011.             i = ourpath(toarea, NULL);
  2012.         
  2013.         Addline(newmsg, "\01PATH:", i-1, tzone);
  2014.         
  2015.         Areamsgs[toarea]++;
  2016.         
  2017.         if (!stricmp(fhdr.to, SysOp))
  2018.         {
  2019.             Tosysop[toarea]++;
  2020.             
  2021.             if (toprivate || dopvtmail)
  2022.             {
  2023.                 nprivate++;
  2024.                 hprintf (S_SYSOP, "%d", nprivate);
  2025.                 
  2026.                 if (get_msgid(messagebody, &mzone, &mnet, &mnode, &mpoint))
  2027.                 {
  2028.                     fhdr.Ozone = mzone;
  2029.                     fhdr.Onet = mnet;
  2030.                     fhdr.Onode = mnode;
  2031.                     fhdr.Opoint = mpoint;
  2032.                 }
  2033.                 
  2034.                 savemsg(TOPRIVATE, &fhdr, toarea, newmsg, fzone, fnet, fnode, fpoint);
  2035.             }
  2036.         }
  2037.         
  2038.         /*
  2039.         **    Move this area ?
  2040.         */
  2041.         
  2042.         oldarea = toarea;
  2043.         
  2044.         if (dofastimport == FALSE)        
  2045.             toarea = move_area(toarea, &fhdr);
  2046.  
  2047.         /*
  2048.         **    Save message to local areas.
  2049.         **     If copyarea the copy this area.
  2050.         */
  2051.         
  2052.         ToCopy = 0;
  2053.         
  2054.         if (!Dareas[toarea]) {
  2055.             savemsg (TOLOCAL, &fhdr, toarea, newmsg, fzone, fnet, fnode, fpoint);
  2056.             while ((i = copy_area(toarea, &fhdr)) != FAILURE) {
  2057.                 savemsg (TOLOCAL, &fhdr, i, newmsg, fzone,fnet, fnode, fpoint);
  2058.             }
  2059.         }
  2060.         
  2061.         toarea = oldarea;
  2062.  
  2063.         /*
  2064.         ** Increment of number of imported messages.
  2065.         */
  2066.         
  2067.         nimported++;
  2068.         
  2069.         hprintf(S_IMP, "%d", nimported);
  2070.         
  2071.         /*
  2072.         ** Message belongs also to database ?
  2073.         */
  2074.         
  2075.         if (nfdbase) {
  2076.             for (i=0; i < nfdbase; i++) {
  2077.                 if (FDcompare[i] == FD_FROM) {
  2078.                     if (!stricmp(fhdr.from,ToBase[i])) {
  2079.                         
  2080.                         write_to_database:
  2081.                         
  2082.                         log_line(2,Logmessage[M__MSG_TO_DATABASE]);
  2083.                         ToDataBase(&fhdr, newmsg, toarea);
  2084.                                 
  2085.                         continue;
  2086.                     }
  2087.                 }
  2088.                 if (FDcompare[i] == FD_TO) {
  2089.                     if (!stricmp(fhdr.to,ToBase[i])) {
  2090.                         goto write_to_database;
  2091.                     }
  2092.                 }
  2093.             }
  2094.         }
  2095.         
  2096.         /*
  2097.         **    If doing fastimport, return.
  2098.         */
  2099.         
  2100.         if (dofastimport == TRUE) {
  2101. #if defined EXTERNDEBUG
  2102.         if (debugflag)
  2103.         {
  2104.             log_line(6,">Free newmsg");
  2105.             log_line(6,">Memory %ld", coreleft());
  2106.         }
  2107. #endif
  2108.             free(newmsg);
  2109.             free(area);
  2110.             return;
  2111.         }
  2112.         
  2113.         /*
  2114.         **    If message is bounced back, don't export the message.
  2115.         */
  2116.         
  2117.         if (bounced_message) {
  2118.             free (newmsg);
  2119.             free (area);
  2120. #if defined EXTERNDEBUG
  2121.             if (debugflag) {
  2122.                 log_line(6, ">Free newmsg.");
  2123.                 log_line(6 ,">Memory %ld", coreleft());
  2124.             }
  2125. #endif
  2126.             return;
  2127.         }
  2128.         
  2129.         /*
  2130.         ** Read only area ?
  2131.         */
  2132.         
  2133.         if (Read_only[toarea] == READ_ONLY) {
  2134.             free (newmsg);
  2135.             free (area);
  2136. #if defined EXTERNDEBUG
  2137.             if (debugflag) {
  2138.                 log_line(6, ">Free newmsg.");
  2139.                 log_line(6 ,">Memory %ld", coreleft());
  2140.             }
  2141. #endif
  2142.         
  2143.             return;
  2144.         }
  2145.         
  2146.         if (Read_only[toarea] == REMOTE_ONLY) {
  2147.             if (Not_allowed(messagebody, toarea) == FALSE) {
  2148.                 free (newmsg);
  2149.                 free (area);
  2150.     #if defined EXTERNDEBUG
  2151.                 if (debugflag) {
  2152.                     log_line(6, ">Free newmsg.");
  2153.                     log_line(6 ,">Memory %ld", coreleft());
  2154.                 }
  2155.     #endif
  2156.                 
  2157.                 return;
  2158.             }
  2159.         }
  2160.         
  2161.         /*
  2162.         **    Copy the oldname into the header again.
  2163.         */
  2164.         
  2165.         strcpy(fhdr.from, oldname);
  2166.         
  2167.         /*
  2168.         **    Strip some lines.
  2169.         */
  2170.         
  2171.         if (dostripINTL) {
  2172.             messagebuffer = strip_line(messagebuffer, "\01FMPT");
  2173.             messagebuffer = strip_line(messagebuffer, "\01TOPT");
  2174.             messagebuffer = strip_line(messagebuffer, "\01INTL");
  2175.         }
  2176.         
  2177.         /*
  2178.         **    If we have nodes and points under us then export it to
  2179.         **    them to.
  2180.         */
  2181.         
  2182. #if defined DEBUG
  2183.         printf("*Pack for others.\n");
  2184. #endif        
  2185.  
  2186.         packmail(toarea, nseenby, expmsg, &fhdr);
  2187.         
  2188.         ToCopy = 0;
  2189.         
  2190.         if(dopointcpy == TRUE) {
  2191.             while(( i = copy_area(toarea,&fhdr)) != FAILURE) {
  2192.                 log_line(3,Logmessage[M__COPY_TO],Areaname[i]);
  2193.             
  2194.                 packmail(i, nseenby, expmsg, &fhdr);
  2195.             }
  2196.         }
  2197.         
  2198. #if defined EXTERNDEBUG
  2199.         if (debugflag)
  2200.         {
  2201.             log_line(6,">Free newmsg");
  2202.             log_line(6,">Memory %ld", coreleft());
  2203.         }
  2204. #endif
  2205.         free(newmsg);
  2206.         free(area);
  2207.         
  2208.         return;
  2209.     }
  2210.     
  2211.     /*
  2212.     **    If area was unknown.
  2213.     */
  2214.     
  2215.     if (Unknown_area == TRUE && doAUTOaddareas) {
  2216.         toarea = ADDarea(area);
  2217.         if (toarea > 0)
  2218.             goto AUTO_area;
  2219.     }
  2220.     
  2221.     if (!doniceout && !doquit) {
  2222.         gprintf("Trashed.");
  2223.     }
  2224.     
  2225.     nimported++;
  2226.     ntrashed ++;
  2227.     
  2228.     hprintf(S_IMP, "%d", nimported);
  2229.     hprintf(S_TRASH,"%d",ntrashed);
  2230.     hprintf(S_COMMENT, "Importing trashed.");
  2231.     
  2232.     savemsg(TOTRASH, &fhdr, 0, messagebody, 0, 0, 0, 0);
  2233.     free (area);
  2234. }
  2235.  
  2236. /*
  2237. **    The messageheader doesn't contain an zonenumber. There are
  2238. **    utils that's mixing zonenumbers when using multizone in the
  2239. **    same packet. The Packet header contains an other zonenumber
  2240. **    then the message must be stored. It's not realy right
  2241. **    solution to search for the net and nodenumber. I can take
  2242. **    for example the INTL line, but than we have problems with
  2243. **    D'Bridge. This routine is only used with normal echomail and
  2244. **    not for netmail.
  2245. */
  2246.  
  2247. WORD other_zone(UWORD *tozone, UWORD *tonet, UWORD *tonode,
  2248.                 UWORD *fromzone, UWORD *fromnet, UWORD *fromnode)
  2249. {
  2250.     WORD    i, j, retcode = 0;
  2251.     
  2252.     for (i=0; i < nalias; i++)
  2253.     {
  2254.         if (*tonode == alias[i].node && *tonet == alias[i].net)
  2255.         {
  2256.             /*
  2257.             **    This is our nodenumber.
  2258.             */
  2259.             
  2260.             *tozone = alias[i].zone;
  2261.             
  2262.             if (Tzone != *tozone)
  2263.             {
  2264.                 Tzone = tzone = *tozone;
  2265.                 Tnet = tnet;
  2266.                 Tnode = tnode;
  2267.                 Tpoint = tpoint;
  2268.             }
  2269.             
  2270. #if defined EXTERNDEBUG
  2271.             if (debugflag) log_line(6,">Found Dzone %u at node %u/%u", Tzone, Tnet, Tnode);
  2272. #endif            
  2273.             retcode++;
  2274.             
  2275.             break;
  2276.         }
  2277.     }
  2278.     
  2279.     for (i=0; i < nkey; i++)
  2280.     {
  2281.         if (*fromnet == pwd[i].net && *fromnode == pwd[i].node)
  2282.         {
  2283.             *fromzone = pwd[i].zone;
  2284.             retcode++;
  2285.             
  2286.             if (Fzone != *fromzone)
  2287.             {
  2288.                 Fzone = fzone = *fromzone;
  2289.                 Fnode = fnode;
  2290.                 Fnet = fnet;
  2291.                 Fpoint = fpoint;
  2292.             }
  2293.             
  2294.             /*
  2295.                 Added for D'Bridge.
  2296.                 D'Bridge seems to send his own zonenumber correctly,
  2297.                 but doesn't set the destanation correct and shows an
  2298.                 main zone, in example 2 instead of 66.
  2299.                 66:666/229 to
  2300.                 2:280/301.1
  2301.             */
  2302.             
  2303.             if (tzone != fzone) {
  2304.                 for (j = 0; j < nalias ; j++) {
  2305.                     if (fzone == alias[j].zone) {
  2306.                         Tzone = tzone = alias[j].zone;
  2307.                         Tnet  = tnet  = alias[j].net;
  2308.                         Tnode = tnode = alias[j].node;
  2309.                         Tpoint= tpoint= alias[j].point;
  2310.                         break;
  2311.                     }
  2312.                 }
  2313.             }
  2314.             
  2315. #if defined EXTERNDEBUG
  2316.             if (debugflag) log_line(6,">Found Ozone %u at node %u/%u", Fzone, Fnet, Fnode);
  2317. #endif            
  2318.             break;
  2319.         }
  2320.     }
  2321.     
  2322.     return(retcode);
  2323. }
  2324.  
  2325. /*
  2326. **    Get our alias.
  2327. */
  2328.  
  2329. BYTE ouralias(UWORD zone, UWORD net, UWORD node, UWORD point)
  2330. {
  2331.     WORD    i;
  2332.     
  2333.     /*
  2334.     **    Check aliases.
  2335.     */
  2336.     
  2337.     for (i=0; i < nalias; i++)
  2338.         if (alias[i].zone == zone &&
  2339.             alias[i].net == net &&
  2340.             alias[i].node == node && !point &&
  2341.             !strnicmp (dest_domain, alias[i].Domain, 8)) return(FALSE);
  2342.     
  2343.     return(TRUE);
  2344. }
  2345.  
  2346. BYTE *strip_cr(BYTE *l)
  2347. {
  2348.     while (*l)
  2349.     {
  2350.         if (*l == '\n' || *l == '\r') l++;
  2351.         if (*l != '\n' && *l != '\r' || !*l) break;
  2352.     }
  2353.     
  2354.     return(l);
  2355. }
  2356.  
  2357. /*
  2358. **    Add seen-by. Add our nodenumber first.
  2359. */
  2360.  
  2361. WORD ourseen(WORD area, UWORD point, MSGHEADER *hdr, BYTE *Odomain)
  2362. {
  2363.     WORD    i, j, z, x,
  2364.             used_aka;
  2365.     UWORD    fake,
  2366.             use_zone;
  2367.     
  2368.     /*
  2369.     **    We are working in zone ??
  2370.     */
  2371.     
  2372.     use_zone = Tozone[area][0];
  2373.     
  2374.     for (i=0; i < nalias; i++) {
  2375.         
  2376.         if (use_zone == alias[i].zone) 
  2377.         {
  2378.             
  2379.             if (Odomain != NULL) {
  2380.                 if (strlen (Odomain)) {
  2381.                     if (strncmp (Odomain, alias[i].Domain, 8)) {
  2382.                         continue;
  2383.                     }
  2384.                 }
  2385.             }            
  2386.             
  2387. #if defined EXTERNDEBUG
  2388.             if (debugflag)
  2389.             {
  2390.                 log_line(6,">Add OURSEENS, have zone %u, point = %u", alias[i].zone, point);
  2391.                 log_line(6,">Org %u:%u/%u.%u -> %u:%u/%u.%u", hdr->Ozone, hdr->Onet, hdr->Onode, hdr->Opoint, hdr->Dzone, hdr->Dnet, hdr->Dnode, hdr->Dpoint);
  2392.             }
  2393. #else
  2394.             hdr->Ozone += 0;
  2395. #endif
  2396.             
  2397.             if (alias[i].net == Tnet && alias[i].node == Tnode &&
  2398.                 alias[i].zone == Tzone && alias[i].point== Tpoint)
  2399.             {
  2400.                 fake = alias[i].pointnet;
  2401.                 
  2402. #if defined EXTERNDEBUG
  2403.                 if (debugflag) log_line(6,">OURSEEN using Dest. %u:%u/%u.%u (%u)", alias[i].zone, alias[i].net, alias[i].node, alias[i].point, alias[i].pointnet);
  2404. #endif    
  2405.                 break;
  2406.             }
  2407.             
  2408.             if (alias[i].net == Fnet && alias[i].node == Fnode &&
  2409.                 alias[i].zone == Fzone && alias[i].point== Fpoint)
  2410.             {
  2411.                 fake = alias[i].pointnet;
  2412.                 
  2413. #if defined EXTERNDEBUG
  2414.                 if (debugflag) log_line(6,">OURSEEN using Org. %u:%u/%u.%u (%u)", alias[i].zone, alias[i].net, alias[i].node, alias[i].point, alias[i].pointnet);
  2415. #endif    
  2416.                 break;
  2417.             }
  2418.         }
  2419.     }
  2420.     
  2421.     
  2422.     if (i >= nalias)
  2423.     {
  2424.         for (i = 0; i < nalias; i++) {
  2425.             if (Tozone[area][0] == alias[i].zone) {
  2426.                 fake = alias[i].pointnet;
  2427.                 break;
  2428.             }
  2429.         }
  2430.         
  2431.         if (i >= nalias) 
  2432.             i = 0;
  2433.     
  2434. #if defined EXTERNDEBUG
  2435.         if (debugflag) log_line(6,">OURSEEN using Unkn. %u:%u/%u.%u (%u)", alias[i].zone, alias[i].net, alias[i].node, alias[i].point, alias[i].pointnet);
  2436. #endif    
  2437.     }
  2438.     
  2439. #if defined DEBUG
  2440.     printf("*Adding our SEEN-BY\n");
  2441. #endif
  2442.     
  2443.     /*
  2444.     **    Add our nodenumber. If we are point take our fakenet number.
  2445.     */
  2446.     
  2447.     used_aka = i;
  2448.     
  2449.     SeenZ[nseenby] = alias[i].zone;
  2450.     
  2451.     if (alias[i].point)
  2452.     {
  2453.         if (donopointadd == FALSE) {
  2454.             SeenN[nseenby] = alias[i].pointnet;
  2455.             SeenO[nseenby] = alias[i].point;
  2456.         } else {
  2457.             j = nseenby;
  2458.             goto NOincrement;
  2459.         }
  2460.     }
  2461.     else
  2462.         if (!doadd_AKA)
  2463.         {
  2464.             SeenN[nseenby] = alias[i].net;
  2465.             SeenO[nseenby] = alias[i].node;
  2466.         }
  2467.     
  2468.     j = nseenby + 1;
  2469.     
  2470.     NOincrement:
  2471.     
  2472.     if (doadd_AKA)
  2473.     {
  2474.         if (!alias[i].point) j--;
  2475.         
  2476.         for (i=0; i < nalias; i++) {
  2477.             if (use_zone == alias[i].zone && !alias[i].point)
  2478.             {
  2479.                 if (Odomain != NULL) {
  2480.                     if (strlen (Odomain)) {
  2481.                         if (strncmp (Odomain, alias[i].Domain, 8)) {
  2482.                             continue;
  2483.                         }
  2484.                     }
  2485.                 }
  2486.                 
  2487.                 SeenZ[j] = alias[i].zone;
  2488.                 SeenN[j] = alias[i].net;
  2489.                 SeenO[j++] = alias[i].node;
  2490.                 
  2491. #if defined EXTERNDEBUG
  2492.                 if (debugflag) log_line(6,">Added AKA %u:%u/%u in SEEN's", alias[i].zone, alias[i].net, alias[i].node);
  2493. #endif
  2494.             }
  2495.         }
  2496.     }
  2497.     
  2498.     /*
  2499.     **    Add seen-by. Take adresses from the AREAS.BBS.
  2500.     **    If message to point take fake and pointadresses to, else
  2501.     **    only nodenumbers.
  2502.     */
  2503.     
  2504.     for (i=0; Tozone[area][i] != (UWORD)-1; i++)
  2505.     {
  2506.         SeenZ[j] = Tozone[area][i];
  2507.         
  2508.         if (point && Topoint[area][i])
  2509.         {
  2510.             if (Tozone[area][i] == alias[used_aka].zone ||
  2511.                 Tonet[area][i] == alias[used_aka].net ||
  2512.                 Tonode[area][i] == alias[used_aka].node)
  2513.             {
  2514.                 if (donopointadd == FALSE) {
  2515.                     SeenN[j] = fake;
  2516.                     SeenO[j++]= Topoint[area][i];
  2517.                 }
  2518.             }
  2519.             else
  2520.             {
  2521.                 SeenN[j] = Tonet[area][i];
  2522.                 SeenO[j++] = Tonode[area][i];
  2523.             }
  2524.             
  2525.             continue;
  2526.         }
  2527.         
  2528.         if (!point && !Topoint[area][i])
  2529.         {
  2530.             SeenN[j] = Tonet[area][i];
  2531.             SeenO[j++] = Tonode[area][i];
  2532.             continue;
  2533.         }
  2534.         
  2535.         if (!point && Topoint[area][i]) continue;
  2536.         
  2537.         SeenN[j] = Tonet[area][i];
  2538.         SeenO[j++] = Tonode[area][i];
  2539.         
  2540.         if (j >= N_SEENS - 2)
  2541.         {
  2542.             memcpy(&Tz, &SeenZ, sizeof(WORD) * (N_SEENS-2));
  2543.             memcpy(&Tnt, &SeenN, sizeof(WORD) * (N_SEENS-2));
  2544.             memcpy(&Tne, &SeenO, sizeof(WORD) * (N_SEENS-2));
  2545.             memset(&Tp, 0, sizeof(WORD) * (N_SEENS-1));
  2546.             
  2547.             Tz[N_SEENS-2] = (UWORD)-1;
  2548.             Tne[N_SEENS-2] = (UWORD)-1;
  2549.             Tnt[N_SEENS-2] = (UWORD)-1;
  2550.             
  2551.             sort_connected(N_SEENS-2, 0);
  2552.             
  2553.             for(x = 1; x<j; x++)
  2554.                 if (Tz[x-1] == Tz[x] &&
  2555.                     Tnt[x-1] == Tnt[x] &&
  2556.                     Tne[x-1] == Tne[x])
  2557.                 {
  2558.                     for (z = x; z < (j-1); z++)
  2559.                     {
  2560.                         Tz[z] = Tz[z+1];
  2561.                         Tnt[z] = Tnt[z+1];
  2562.                         Tne[z] = Tne[z+1];
  2563.                     }
  2564.                     
  2565.                     if(--j <= 1) break;
  2566.                     x = 1;
  2567.                 }
  2568.             
  2569.             if (j >= N_SEENS -1)
  2570.             {
  2571.                 log_line(6,"?More than %d nodes in SEEN-BY's, contact author.", N_SEENS-1);
  2572.                 j--;
  2573.                 break;
  2574.             }
  2575.             
  2576.             memcpy(&SeenZ, &Tz, sizeof(WORD) * (N_SEENS-1));
  2577.             memcpy(&SeenN, &Tnt, sizeof(WORD) * (N_SEENS-1));
  2578.             memcpy(&SeenO, &Tne, sizeof(WORD) * (N_SEENS-1));
  2579.         }
  2580.     }
  2581.  
  2582.     SeenZ[j] = -1;
  2583.     SeenN[j] = -1;
  2584.     SeenO[j] = -1;
  2585.     
  2586.     j++;
  2587.     
  2588.     /*
  2589.     **    Copy.
  2590.     */
  2591.     
  2592. #if defined DEBUG
  2593.     printf("*Copying SEENs\n");
  2594. #endif
  2595.     
  2596.     memcpy(&Tz, &SeenZ, sizeof(WORD) * j);
  2597.     memcpy(&Tnt, &SeenN, sizeof(WORD) * j);
  2598.     memcpy(&Tne, &SeenO, sizeof(WORD) * j);
  2599.     memset(&Tp, 0, sizeof(WORD) * (j+1));
  2600.     
  2601.     /*
  2602.     **    Sort the line.
  2603.     */
  2604.     
  2605. #if defined DEBUG
  2606.     printf("*Sorting SEENs\n");
  2607. #endif
  2608.     
  2609.     sort_connected(j, 0);
  2610.     
  2611. #if defined DEBUG
  2612.     printf("*Wipe dups out.\n");
  2613. #endif
  2614.     
  2615.     /*
  2616.     **    Duplicates? Wipe them out.
  2617.     */
  2618.     
  2619.     for (i = 1; Tz[i] != (UWORD)-1; i++)
  2620.         if (Tz[i] == Tz[i-1] &&
  2621.             Tnt[i] == Tnt[i-1] &&
  2622.             Tne[i] == Tne[i-1])
  2623.         {
  2624.             for (z = i; z < (j-1); z++)
  2625.             {
  2626.                 Tz[z] = Tz[z+1];
  2627.                 Tnt[z] = Tnt[z+1];
  2628.                 Tne[z] = Tne[z+1];
  2629.             }
  2630.             
  2631.             i = 1;
  2632.             --j;
  2633.         }
  2634.     
  2635. #if defined DEBUG
  2636.     printf("*Have SEENs\n");
  2637. #endif
  2638.     
  2639.     return(j);
  2640. }
  2641.  
  2642. /*
  2643. **    Add our number in the PATH
  2644. */
  2645.  
  2646. WORD ourpath(WORD area, BYTE *Odomain)
  2647. {
  2648.     WORD    i, z;
  2649.     UWORD    zone,
  2650.             net,
  2651.             node;
  2652.     
  2653.     if (npath == N_SEENS-1)
  2654.     {
  2655.         log_line(5,"?Can't add PATH:, more than %d nodes.", N_SEENS);
  2656.         return(npath-1);
  2657.     }
  2658.     
  2659.     /*
  2660.     **    We are working in zone?? If we are point take our
  2661.     **    fakenet number.
  2662.     */
  2663.     
  2664.     for (i=0; i < nalias; i++) {
  2665.  
  2666.         if (Tozone[area][0] == alias[i].zone)
  2667.         {
  2668.  
  2669.             if (Odomain != NULL) {
  2670.                 if (strlen (Odomain)) {
  2671.                     if (strncmp (Odomain, alias[i].Domain, 8)) {
  2672.                         continue;
  2673.                     }
  2674.                 }
  2675.             }
  2676.             
  2677.             zone = alias[i].zone;
  2678.             
  2679. #if defined EXTERNDEBUG
  2680.             if (debugflag) log_line(6,">OURPATH have areazone %u", zone);
  2681. #endif
  2682.             if ((alias[i].net == Fnet && alias[i].node == Fnode &&
  2683.                 alias[i].zone == Fzone && alias[i].point== Fpoint) ||
  2684.                 (alias[i].net == Tnet && alias[i].node == Tnode &&
  2685.                 alias[i].zone == Tzone && alias[i].point== Tpoint))
  2686.             {
  2687.             
  2688.                 /*
  2689.                 **    Point? Take fakenumber.
  2690.                 */
  2691.                 
  2692.                 if (alias[i].point)
  2693.                 {
  2694.                     if (donopointpath == FALSE)
  2695.                     {
  2696.                         net = alias[i].pointnet;
  2697.                         node = alias[i].point;
  2698.                     }
  2699.                     else
  2700.                     {
  2701.                         net = alias[i].net;
  2702.                         node= alias[i].node;
  2703.                     }
  2704.                     break;
  2705.                 }
  2706.                 
  2707.                 net  = alias[i].net;
  2708.                 node = alias[i].node;
  2709.                 break;
  2710.             }
  2711.         }
  2712.     }
  2713.  
  2714.     if (i >= nalias)
  2715.     {
  2716.         zone = alias[0].zone;
  2717.         
  2718.         if (alias[0].point)
  2719.         {
  2720.             if (donopointpath == FALSE)
  2721.             {
  2722.                 net = alias[0].pointnet;
  2723.                 node = alias[0].point;
  2724.             }
  2725.             else
  2726.             {
  2727.                 net = alias[0].net;
  2728.                 node= alias[0].node;
  2729.             }
  2730.         }
  2731.         else
  2732.         {
  2733.             net = alias[0].net;
  2734.             node = alias[0].node;
  2735.         }
  2736.     }
  2737.     
  2738. #if defined EXTERNDEBUG
  2739.     if (debugflag) log_line(6,">OURPATH now having %u:%u/%u", zone, net, node);
  2740. #endif
  2741.     
  2742. #if defined DEBUG
  2743.     printf("*Adding our PATH:\n");
  2744. #endif
  2745.     
  2746.     Pathzone[npath] = zone;
  2747.     Pathnet[npath] = net;
  2748.     Pathnode[npath++] = node;
  2749.     
  2750.     Pathzone[npath] = -1;
  2751.     Pathnet[npath] = -1;
  2752.     Pathnode[npath++] = -1;
  2753.     
  2754.     memcpy(&Tz, &Pathzone, sizeof(WORD) * npath);
  2755.     memcpy(&Tnt, &Pathnet, sizeof(WORD) * npath);
  2756.     memcpy(&Tne, &Pathnode, sizeof(WORD) * npath);
  2757.     memset(&Tp, 0, sizeof(WORD) * npath);
  2758.     
  2759.     for (i = 1; Tz[i] != (UWORD)-1; i++)
  2760.         if (Tz[i] == Tz [i-1] &&
  2761.             Tnt[i] == Tnt[i-1] &&
  2762.             Tne[i] == Tne[i-1])
  2763.         {
  2764.             for (z = i; z <= npath; z++)
  2765.             {
  2766.                 Tz[z] = Tz[z+1];
  2767.                 Tnt[z] = Tnt[z+1];
  2768.                 Tne[z] = Tne[z+1];
  2769.             }
  2770.             
  2771.             if (--npath <= 1) break;
  2772.             i = 1;
  2773.         }
  2774.     
  2775.     return(++npath);
  2776. }
  2777.  
  2778. BYTE Openarea(WORD file, BYTE *Hfile, BYTE *Mfile, MSGHEADER *Hdr)
  2779. {
  2780. #if defined DEBUG
  2781.     printf("*Opening other area\n");
  2782. #endif
  2783.     
  2784.     if ((LMSG[file] = fopen(Mfile, "r+b")) == NULL)
  2785.         if ((LMSG[file] = fopen(Mfile, "wb")) == NULL)
  2786.         {
  2787.             log_line(5,Logmessage[M__CANT_OPEN_AREA], Mfile);
  2788.             return(FALSE);
  2789.         }
  2790.         else
  2791.             Hdr->Mstart = 0L;
  2792.     else
  2793.     {
  2794.         fseek(LMSG[file], 0L, SEEK_END);
  2795.         Hdr->Mstart = ftell(LMSG[file]);
  2796.     }
  2797.  
  2798.     giveTIMEslice();
  2799.     
  2800.     if ((LHDR[file] = fopen(Hfile, "r+b")) == NULL)
  2801.         if ((LHDR[file] = fopen(Hfile, "wb")) == NULL)
  2802.         {
  2803.             log_line(5,Logmessage[M__CANT_OPEN_HEADER], Hfile);
  2804.             fclose(LMSG[file]);
  2805.             LMSGfile[file] = FILEclosed;
  2806.             
  2807.             return(FALSE);
  2808.         }                
  2809.     else
  2810.         fseek(LHDR[file], 0L, SEEK_END);
  2811.     
  2812.     return(TRUE);
  2813. }
  2814.  
  2815. /*
  2816. **    Open an new packet. If packet doens't exist write an PKT header.
  2817. **    If not new, seek end.
  2818. */
  2819.  
  2820. BYTE Openpkt(WORD file, BYTE *node32, MSGHEADER *Hdr)
  2821. {
  2822.     BYTE            pktname[128],
  2823.                     buffer[128];
  2824. #if defined LATTICE
  2825.     DMABUFFER        blk;
  2826. #else
  2827.     struct ffblk    blk;
  2828. #endif
  2829.     
  2830.     node32[11] = EOS;
  2831.     
  2832.     sprintf(pktname, "%s%sC", mailtemp, node32);
  2833.     
  2834.     if (Hdr->flags & CRASH)
  2835.     {
  2836.         sprintf(buffer, "%s%sH", mailtemp, node32);
  2837.         
  2838.         if (findfirst(buffer, &blk, 0))
  2839.         {
  2840.             sprintf(buffer, "%s%sN", mailtemp, node32);
  2841.             
  2842.             if (!findfirst(buffer, &blk, 0)) rename(buffer, pktname);
  2843.         }
  2844.         else rename(buffer, pktname);
  2845.     
  2846.         giveTIMEslice();
  2847.         
  2848.         if ((PKTfile[file] = fopen(pktname, "r+b")) == NULL)
  2849.         {
  2850.             giveTIMEslice();
  2851.             if ((PKTfile[file] = fopen(pktname, "wb")) == NULL)
  2852.             {
  2853.                 log_line(6,Logmessage[M__CANT_OPEN_PACKET]);
  2854.                 return(FALSE);
  2855.             }            
  2856.             hdrwrite(&phdr, sizeof(struct _pkthdr), PKTfile[file]);
  2857. /*
  2858.             Fwrite(fileno(PKTfile[file]), sizeof(struct _pkthdr), &phdr);
  2859. */
  2860.         }
  2861.         else get_last_byte(PKTfile[file], pktname);
  2862.         
  2863.         return(TRUE);
  2864.     }
  2865.     
  2866.     sprintf (pktname, "%s%sH", mailtemp, node32);
  2867.     
  2868.     if (Hdr->flags & MSGHOLD) {
  2869.         sprintf(buffer, "%s%sN", mailtemp, node32);
  2870.         
  2871.         if (findfirst(buffer, &blk, 0))
  2872.         {
  2873.             sprintf(buffer, "%s%sC", mailtemp, node32);
  2874.             
  2875.             if (!findfirst(buffer, &blk, 0)) 
  2876.                 strcpy (pktname, buffer);
  2877.         }
  2878.         else rename(buffer, pktname);
  2879.     
  2880.         giveTIMEslice();
  2881.         
  2882.         if ((PKTfile[file] = fopen(pktname, "r+b")) == NULL)
  2883.         {
  2884.             giveTIMEslice();
  2885.             if ((PKTfile[file] = fopen(pktname, "wb")) == NULL)
  2886.             {
  2887.                 log_line(6,Logmessage[M__CANT_OPEN_PACKET]);
  2888.                 return(FALSE);
  2889.             }            
  2890.             hdrwrite(&phdr, sizeof(struct _pkthdr), PKTfile[file]);
  2891. /*
  2892.             Fwrite(fileno(PKTfile[file]), sizeof(struct _pkthdr), &phdr);
  2893. */
  2894.         }
  2895.         else get_last_byte(PKTfile[file], pktname);
  2896.         
  2897.         return(TRUE);
  2898.     }
  2899.  
  2900.     sprintf(pktname, "%s%sC", mailtemp, node32);
  2901.         
  2902.     if ((PKTfile[file] = fopen(pktname, "r+b")) == NULL)
  2903.     {
  2904.         sprintf(pktname, "%s%sH", mailtemp, node32);
  2905.         giveTIMEslice();
  2906.         
  2907.         if ((PKTfile[file] = fopen(pktname, "r+b")) == NULL )
  2908.         {
  2909.             sprintf(pktname, "%s%sN", mailtemp, node32);
  2910.             giveTIMEslice();
  2911.             
  2912.             if ((PKTfile[file] = fopen(pktname, "r+b")) == NULL)
  2913.             {
  2914.                 giveTIMEslice();
  2915.                 if ((PKTfile[file] = fopen(pktname, "wb")) == NULL)
  2916.                 {
  2917.                     log_line(6,Logmessage[M__CANT_CREATE_PACKET]);
  2918.                     return(FALSE);
  2919.                 }
  2920.                 else
  2921.                 {
  2922.                     /*
  2923.                     **    New file. Write header at once.
  2924.                     */
  2925.                     
  2926.                     hdrwrite(&phdr, sizeof(struct _pkthdr), PKTfile[file]);
  2927. /*
  2928.                     Fwrite(fileno(PKTfile[file]), sizeof(struct _pkthdr), &phdr);
  2929. */                    
  2930.                 }
  2931.             }
  2932.             else get_last_byte(PKTfile[file], pktname);
  2933.         }
  2934.         else get_last_byte(PKTfile[file], pktname);
  2935.     }
  2936.     else get_last_byte(PKTfile[file], pktname);
  2937.     
  2938.     
  2939.     return(TRUE);
  2940. }
  2941.  
  2942. VOID get_last_byte(FILE *stream, BYTE *filename)
  2943. {
  2944.     WORD            i;
  2945. #if defined LATTICE
  2946.     DMABUFFER        blk;
  2947. #else
  2948.     struct ffblk    blk;
  2949. #endif
  2950.     
  2951.     if (!findfirst(filename, &blk, 0))
  2952.     {
  2953.         giveTIMEslice();
  2954.  
  2955.         fseek(stream, blk.ff_fsize - 3, SEEK_SET);
  2956.         i = getc(stream);
  2957.         if (!i) return;
  2958.         i = getc(stream);
  2959.         if (!i) return;
  2960.         i = getc(stream);
  2961.         if (!i) return;
  2962.         putc(0, stream);
  2963.         giveTIMEslice();
  2964.         
  2965.         return;
  2966.     }
  2967.     
  2968.     if (stream)
  2969.     {
  2970.         fseek(stream, 0L, SEEK_END);
  2971.         return;
  2972.     }
  2973.     
  2974.     log_line(6,Logmessage[M__UNKNOWN_ERROR_PKT]);
  2975. }
  2976.  
  2977. /*
  2978. **    Add an line at the end of the message.
  2979. */
  2980.  
  2981. VOID Addline(BYTE *msg, BYTE *what, WORD numbers, UWORD ourzone)
  2982. {
  2983.     WORD    i;
  2984.     UWORD    zone,
  2985.             net,
  2986.             node,
  2987.             lzone,
  2988.             lnet,
  2989.             lnode;
  2990.     BYTE    line[100];
  2991.     BYTE    *p;
  2992.     
  2993.     /*
  2994.     **    If not one of ourzones then add zonenumber.
  2995.     */
  2996.     
  2997. #if defined DEBUG
  2998.     printf("*Adding line %s\n", what);
  2999. #endif
  3000.     
  3001.     if (ourzone != Tz[0] && !donozone)
  3002.         sprintf(line, "%s %u:%u/%u", what, Tz[0], Tnt[0], Tne[0]);
  3003.     else
  3004.         sprintf(line, "%s %u/%u", what, Tnt[0], Tne[0]);
  3005.     
  3006.     lzone = Tz[0];
  3007.     lnet = Tnt[0];
  3008.     lnode = Tne[0];
  3009.     
  3010.     for (i=1; i < numbers && Tz[i] != (UWORD)-1; i++)
  3011.     {
  3012.         /*
  3013.         **    Don't make it to long daddy.
  3014.         */
  3015.         
  3016.         if (strlen(line) >= 65)
  3017.         {
  3018.             p = &msg[strlen(msg)-1];
  3019.             
  3020.             if (*p != '\n') strcpy(++p, "\n");
  3021.             
  3022.             strcat(msg, line);
  3023.             sprintf(line, what);
  3024.             
  3025.             lnet = -1;
  3026.             lnode = -1;
  3027.             
  3028.             if (fzone != ourzone) lzone = -1;
  3029.         }
  3030.         
  3031.         zone = Tz[i];
  3032.         net = Tnt[i];
  3033.         node = Tne[i];
  3034.         
  3035.         /*
  3036.         **    Find out if this is the same as before.
  3037.         */
  3038.         
  3039.         if (zone == lzone && net == lnet && node == lnode) continue;
  3040.         
  3041.         /*
  3042.         **    If we are polling an other zone than add zone.
  3043.         */
  3044.         
  3045.         if (zone != lzone && !donozone)
  3046.         {
  3047.             sprintf(&line[strlen(line)], " %u:%u/%u", zone, net, node);
  3048.             
  3049.             lzone = zone;
  3050.             lnet = net;
  3051.             lnode= node;
  3052.             
  3053.             continue;
  3054.         }
  3055.                 
  3056.         if (lnet == net && lzone == zone)
  3057.         {
  3058.             sprintf(&line[strlen(line)], " %u", node);
  3059.             continue;
  3060.         }
  3061.         
  3062.         lzone = zone;
  3063.         lnet = net;
  3064.         lnode = node;
  3065.         
  3066.         sprintf(&line[strlen(line)], " %u/%u", net, node);
  3067.     }
  3068.     
  3069.     if (strlen(line) > strlen(what))
  3070.     {
  3071.         p = &msg[strlen(msg)-1];
  3072.         
  3073.         if (*p != '\n') strcat(msg, "\n");
  3074.         strcat(msg, line);
  3075.     }
  3076.     
  3077. #if defined DEBUG
  3078.     printf("*Line added.\n");
  3079. #endif
  3080. }
  3081.  
  3082. /*
  3083. **    Strip a line.
  3084. */
  3085.  
  3086. BYTE *strip_line(BYTE *msg, BYTE *line)
  3087. {
  3088.     BYTE    *p, *q;
  3089.     
  3090.     if ((p = strline(msg, line)) == NULL) return(msg);
  3091.     
  3092.     /*
  3093.     **    Find the end of the line.
  3094.     */
  3095.     
  3096.     q = p;
  3097.     while (*p != '\n') p++;
  3098.     strcpy(q, ++p);
  3099.     
  3100.     return(msg);
  3101. }
  3102.  
  3103. WORD move_area(WORD org_area, MSGHEADER *hdr) {
  3104.     WORD i, j;
  3105.     
  3106.     for(i=0; i < ncopyareas ; i++) {
  3107.         if(match(Areaname[org_area], Copyarea[i]) &&
  3108.         Copy_area[i] == MOVEAREA) {
  3109.             for(j=0; j < msgareas ; j++) {
  3110.                 if(stricmp(Areaname[j], Destarea[i])) {
  3111.                     if (newmatch(hdr->from, Copylegal[i])) {
  3112.                         log_line(3,Logmessage[M__MOVE_AREA],
  3113.                         Areaname[org_area],Areaname[j]);
  3114.                         
  3115.                         return (j);
  3116.                     }
  3117.                 }
  3118.             }
  3119.         }
  3120.     }
  3121.     return(org_area);
  3122. }
  3123.  
  3124. WORD copy_area(WORD org_area, MSGHEADER *hdr) {
  3125.     WORD i, j;
  3126.     
  3127.     if (ToCopy >= ncopyareas) return (FAILURE);
  3128.     
  3129.     for(i=ToCopy; i < ncopyareas ; i++) {
  3130.         if(match(Areaname[org_area], Copyarea[i]) &&
  3131.         Copy_area[i] == COPYAREA) {
  3132.             for(j = 0; j < msgareas ; j++) {
  3133.                 if (!stricmp(Areaname[j], Destarea[i])) {
  3134.                     if(newmatch(hdr->from, Copylegal[i])) {
  3135.                         log_line(3,Logmessage[M__COPY_TO_AREA],
  3136.                         Areaname[org_area],Areaname[j]);
  3137.                         ToCopy = i+1;
  3138.                         return (j);
  3139.                     }
  3140.                 }
  3141.             }
  3142.         }
  3143.     }
  3144.     return (FAILURE);
  3145. }
  3146.  
  3147. VOID packmail(UWORD toarea, UWORD nseenby, BYTE *expmsg, MSGHEADER *fhdr) {
  3148.  
  3149.     WORD j, l, i;
  3150.     
  3151.     BYTE *newmsg;
  3152.  
  3153. #if defined EXTERNDEBUG
  3154.         if (debugflag)
  3155.         {
  3156.             log_line(6,">Alloc newmsg in packmail");
  3157.             log_line(6,">Memory %ld", coreleft());
  3158.         }
  3159. #endif
  3160.     
  3161.     newmsg = (BYTE *) myalloc(MAXMSGLENGTH);
  3162.     
  3163.     for (j=0; Tozone[toarea][j] != (UWORD)-1; j++)
  3164.     {
  3165.         if (Tozone[toarea][j] == fzone &&
  3166.             Tonet[toarea][j] == fnet &&
  3167.             Tonode[toarea][j] == fnode &&
  3168.             Topoint[toarea][j] == fpoint) continue;
  3169.         
  3170.         /*
  3171.         **    Don't export if the node mentioned in the AREAS.BBS
  3172.         **    is 1 of our aliases.
  3173.         */
  3174.         
  3175.         if (!ouralias(Tozone[toarea][j], Tonet[toarea][j],
  3176.                         Tonode[toarea][j], Topoint[toarea][j]))
  3177.             continue;
  3178.         
  3179.         /*
  3180.         **    Check SEEN-BY
  3181.         */
  3182.                     
  3183.         for (l=0; l < nseenby; l++)
  3184.         {
  3185.             if (SeenN[l] == Tonet[toarea][j] && 
  3186.                 SeenO[l] == Tonode[toarea][j] &&
  3187.                 SeenZ[l] == Tozone[toarea][j] &&
  3188.                 !Topoint[toarea][j]) break;
  3189.             
  3190.             i = nalias;
  3191.             
  3192.             if (Topoint[toarea][j])
  3193.                 for (i=0; i < nalias; i++)
  3194.                     if (SeenN[l] == alias[i].pointnet &&
  3195.                         SeenZ[l] == Tozone[toarea][j] &&
  3196.                         SeenO[l] == Topoint[toarea][j]) break;
  3197.             
  3198.             if (i < nalias) break;
  3199.         }
  3200.         
  3201.         if (l < nseenby) continue;
  3202.         
  3203.         nechos++;
  3204.         Ncreated[toarea]++;
  3205.         
  3206.         hprintf (S_EXP, "%d", nechos);
  3207.         
  3208.         fhdr->Dzone = Tozone[toarea][j];
  3209.         fhdr->Dnet = Tonet[toarea][j];
  3210.         fhdr->Dnode = Tonode[toarea][j];
  3211.         fhdr->Dpoint = Topoint[toarea][j];
  3212.                     
  3213.         fhdr->Ozone = Tzone;
  3214.         fhdr->Onet = Tnet;
  3215.         fhdr->Onode = Tnode;
  3216.         fhdr->Opoint = Tpoint;
  3217.         
  3218.         if (fhdr->Ozone != fhdr->Dzone)
  3219.             for (l=0; l < nalias; l++)
  3220.                 if (alias[l].zone == fhdr->Dzone)
  3221.                 {
  3222.                     fhdr->Ozone = alias[l].zone;
  3223.                     fhdr->Onet = alias[l].net;
  3224.                     fhdr->Onode = alias[l].node;
  3225.                     fhdr->Opoint = alias[l].point;
  3226.                     break;
  3227.                 }
  3228.         
  3229.         /*
  3230.         **    Message not sent. On holiday?
  3231.         */
  3232.         
  3233.         if (!capture_msg(fhdr)) continue;
  3234.  
  3235.         hprintf(S_DEST, "%u:%u/%u.%u", fhdr->Dzone, fhdr->Dnet,
  3236.             fhdr->Dnode, fhdr->Dpoint);
  3237.         hprintf(S_FROM, "%u:%u/%u.%u", fhdr->Ozone, fhdr->Onet,
  3238.             fhdr->Onode, fhdr->Opoint);
  3239.         
  3240.         sprintf(newmsg, "AREA:%s\n", Areaname[toarea]);
  3241.         
  3242.         add_point(newmsg, fhdr);
  3243.         
  3244.         strcpy(&newmsg[strlen(newmsg)], expmsg);
  3245.         
  3246.         if (strlen (dest_domain))
  3247.             i = ourseen(toarea, Topoint[toarea][j], fhdr, dest_domain);
  3248.         else
  3249.             i = ourseen(toarea, Topoint[toarea][j], fhdr, NULL);
  3250.  
  3251.         Addline(newmsg, (doifna) ? "\01SEEN-BY" : "SEEN-BY:", i-1, Tozone[toarea][j]);
  3252.         
  3253.         if (strlen (dest_domain))
  3254.             i = ourpath(toarea, dest_domain);
  3255.         else
  3256.             i = ourpath(toarea, NULL);
  3257.         
  3258.         Addline(newmsg, "\01PATH:", i-1, Tozone[toarea][j]);
  3259.         
  3260.         if (!dopkt)
  3261.         {
  3262.             fhdr->flags |= (KILLSEND|MSGLOCAL);
  3263.             fhdr->flags &= ~SENT;
  3264.             fhdr->mailer[7] = 0;
  3265.         }
  3266.         
  3267.         savemsg((dopkt) ? TOPACKET : TONETMAIL, fhdr, toarea,
  3268.             newmsg, Tozone[toarea][j], Tonet[toarea][j],
  3269.             Tonode[toarea][j], Topoint[toarea][j]);
  3270.             
  3271.             
  3272.     }
  3273. #if defined EXTERNDEBUG
  3274.         if (debugflag)
  3275.         {
  3276.             log_line(6,">Free newmsg");
  3277.             log_line(6,">Memory %ld", coreleft());
  3278.         }
  3279. #endif
  3280.     free(newmsg);
  3281. }
  3282.