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

  1. /*********************************************************************
  2.  
  3.                     IOS - routing manager
  4.                     
  5.     Program : IOSmail
  6.     Source  : free
  7.     Author  : Rinaldo Visscher
  8.     Date    : 1991
  9.  
  10.     Scan netmail and route the mail bundled or unpacked to the
  11.     connected systems. Check the routing .
  12. ***********************************************************************/
  13.  
  14. /*#define TESTCASE    1*/
  15.  
  16. #include        <tos.h>
  17. #include        <stdio.h>
  18. #include        <time.h>
  19. #include        <ctype.h>
  20. #include        <ext.h>
  21. #include        <string.h>
  22. #include        <stdlib.h>
  23. #include        <process.h>
  24. #include        <errno.h>
  25.  
  26. #include        "portab.h"
  27. #include        "defs.h"
  28. #include        "ioslib.h"
  29. #include        "modules.h"
  30. #include        "routing.h"
  31.  
  32. #include        "inmail.h"
  33. #include        "strutil.h"
  34. #include        "lang.h"
  35.  
  36. #include        "vars.h"
  37.  
  38. #define PACKMAIL    0
  39. #define LEAVEPKT    1
  40. #define HIGH_POINT    960
  41.  
  42. BOOLEAN    get_gateway(MSGHEADER *hdr, BYTE *msg);
  43. VOID    prepare_poll(VOID);
  44. VOID    put_hold(BYTE *name, UWORD usedzone, BYTE *dir, WORD what, BYTE Pkt);
  45. VOID    hold_to_poll(WORD what);
  46. VOID    arc_PKTS(BYTE what);
  47. VOID    crunch_it(BYTE *packet, BYTE do_it, LONG size);
  48. VOID     create_pollPKT(BYTE *dir, UWORD zone, UWORD net, UWORD node, UWORD point);
  49. BYTE    check_hold_poll (BYTE *file, BYTE Pkt);
  50.  
  51. /*
  52. **    Pack netmail that is for an known node.
  53. */
  54.  
  55. VOID pack_netmail(MSGHEADER *hdr, BYTE *msgtext)
  56. {
  57.     WORD        i,
  58.                 havekey,
  59.                 retcode;
  60.     UWORD        dpoint=0, opoint=0, fakenet,
  61.                 zone, net, node, point,
  62.                 j;
  63.     BYTE        used_intl = TYPE4d,
  64.                 intl_ever = FALSE,
  65.                 *newmsg,
  66.                 domain[10];
  67.     
  68.     hprintf (S_COMMENT, "Packing NETMAIL");
  69.     
  70.     /*
  71.     **    Crash on this node?
  72.     */
  73.     
  74.     if (crashOk(hdr->Dzone, hdr->Dnet, hdr->Dnode))
  75.         hdr->flags |= CRASH;
  76.     
  77.     /*
  78.     **    If destination is found, break the loop.
  79.     **    Seek destanation is one of out points.
  80.     */
  81.     
  82.     found_an_gateway:
  83.     
  84.     /*
  85.     **    If we have a message to the gateway then start again
  86.     */
  87.     
  88.     for (i=0; i < nkey; i++)
  89.         if (hdr->Dzone == pwd[i].zone &&
  90.             hdr->Dnet == pwd[i].net &&
  91.             hdr->Dnode == pwd[i].node &&
  92.             hdr->Dpoint == pwd[i].point )
  93.         {
  94.             used_intl = pwd[i].intl_type;
  95.             intl_ever = pwd[i].intl_always;
  96.             
  97. #if defined OUTDEBUG
  98.             if (dooutdeb) log_line(6,">Found destination %u:%u/%u.%u '%s'", pwd[i].zone, pwd[i].net, pwd[i].node, pwd[i].point, pwd[i].pwd);
  99. #endif
  100.             havekey = i;
  101.             break;
  102.         }
  103.     
  104.     /*
  105.     **    Destination not found, look if this message is to us.
  106.     */
  107.     
  108.     if (i >= nkey)
  109.     {
  110.         for (i=0; i < nalias; i++)
  111.         {
  112.             if (hdr->Dzone == alias[i].zone &&
  113.                 hdr->Dnet == alias[i].net &&
  114.                 hdr->Dnode == alias[i].node &&
  115.                 hdr->Dpoint == alias[i].point)
  116.             {
  117.                 /*
  118.                 **    This message is to us.
  119.                 */
  120.                 
  121. #if defined OUTDEBUG
  122.                 if (dooutdeb) log_line(6,">Destination is %u:%u/%u.%u", hdr->Dzone, hdr->Dnet, hdr->Dnode, hdr->Dpoint);
  123. #endif                
  124.                 if (!stricmp(hdr->to, "gateway"))
  125.                 {
  126.                     if (!get_gateway(hdr, msgtext)) return;
  127.                     goto found_an_gateway;
  128.                 }
  129.                 
  130.                 return;
  131.             }
  132.         }
  133.         
  134.         /*
  135.         **    Message not for us. Seek message is for 1 of our nodes.
  136.         */
  137.         
  138.         for (i=0; i < nkey; i++)
  139.             if (hdr->Dzone == pwd[i].zone &&
  140.                 hdr->Dnet == pwd[i].net &&
  141.                 hdr->Dnode == pwd[i].node)
  142.             {
  143.                 if (hdr->Dpoint && !hdr->Opoint && hdr->flags & MSGLOCAL) {
  144.                     for (j=0; j < nalias; j++) {
  145.                         if (hdr->Dzone == alias[j].zone &&
  146.                             hdr->Dnet  == alias[j].net &&
  147.                             hdr->Dnode == alias[j].node) {
  148.                             log_line(6, Logmessage[M__POINT_NOT_SYSTEM], hdr->Dpoint);
  149.                             return;
  150.                         }
  151.                     }
  152.                 }
  153.                 used_intl = pwd[i].intl_type;
  154.                 intl_ever = pwd[i].intl_always;
  155.                 
  156. #if defined OUTDEBUG
  157.                 if (dooutdeb) log_line(6,">Found destination %u:%u/%u.%u '%s'", pwd[i].zone, pwd[i].net, pwd[i].node, pwd[i].point, pwd[i].pwd);
  158. #endif
  159.                 havekey = i;
  160.                 break;
  161.             }
  162.         
  163.         /*
  164.         **    Message not for one of our nodes. Find routing.
  165.         */
  166.         
  167.         if (i >= nkey)
  168.         {
  169. #if defined OUTDEBUG
  170.             if (dooutdeb) log_line(6,">Destination not in password");
  171. #endif    
  172.             /*
  173.             **    This message is not to one of our known
  174.             **    nodes, and not to us. Find routing.
  175.             */
  176.             
  177.             if (hdr->flags & FILEATCH) attached_file(hdr);
  178.             
  179.             find_ROUTE(hdr, msgtext);
  180.             
  181.             return;
  182.         }
  183.     }
  184.     
  185.     /*
  186.     **    Als bestemmengs node in de password regels staat en het
  187.     **    bericht is niet gecrashed, dan moet het niet direct naar
  188.     **    die node verzonden worden, maar via de host.
  189.     */
  190.     
  191.     if (!(hdr->flags & CRASH) && havekey < nkey) {
  192.         if (pwd[havekey].crash_to == AT_CRASH) {
  193.             if (hdr->flags & FILEATCH) attached_file (hdr);
  194.             find_ROUTE (hdr, msgtext);
  195.             return;
  196.         }
  197.     }
  198.     
  199.     /*
  200.     **    Message has file attached. Output it.
  201.     */
  202.     
  203.     if (hdr->flags & FILEATCH) attached_file(hdr);
  204. #if defined EXTERNDEBUG
  205.         if (debugflag)
  206.         {
  207.             log_line(6,">Alloc newmsg (Out)");
  208.             log_line(6,">Memory %ld", coreleft());
  209.         }
  210. #endif
  211.     
  212.     newmsg = (BYTE *) myalloc(MAXMSGLENGTH);
  213.     *newmsg = EOS;
  214.     
  215.     /*
  216.     **    AREA: regel in het bericht. Moet in de area geplaatst worden.
  217.     */
  218.     
  219.     if (strncmp(msgtext, "AREA:",5))
  220.     {
  221. #if defined OUTDEBUG
  222.         if (dooutdeb) log_line(6,">Origin zone %u - Destination %u", hdr->Ozone, hdr->Dzone);
  223. #endif
  224.  
  225.         /*
  226.         **    Plaats een INTL regel als het moet.
  227.         */
  228.         
  229.         if (hdr->Ozone != hdr->Dzone || intl_ever == TRUE)
  230.         
  231.         /*
  232.         **    3D INTL regel ?
  233.         */
  234.         
  235.             if (used_intl == TYPE3d && hdr->Dpoint)
  236.             {
  237.                 for (i=0; i < nalias; i++)
  238.                     if (alias[i].zone == hdr->Dzone &&
  239.                         alias[i].net == hdr->Dnet &&
  240.                         alias[i].node== hdr->Dnode)
  241.                     {
  242.                         fakenet = alias[i].pointnet;
  243. #if defined OUTDEBUG
  244.                         if (dooutdeb) log_line(6,">Destination fakeaddress is %u/%u", fakenet, hdr->Dpoint);
  245. #endif
  246.                         break;
  247.                     }
  248.                 
  249.                 /*
  250.                 **    Point niet gevonden, ga naar 4D INTL regel.
  251.                 **    
  252.                 */
  253.                 
  254.                 if (i >= nalias) goto INTL_4d;
  255.                 
  256.                 /*
  257.                 ** Schrijf 3D INTL regel weg in buffer.
  258.                 */
  259.                 
  260.                 if (!hdr->Opoint && hdr->Dpoint) {
  261.                     sprintf (newmsg, "\01INTL%u:%u/%u %u:%u/%u\n",
  262.                         hdr->Dzone, fakenet, hdr->Dpoint,
  263.                         hdr->Ozone, hdr->Onet, hdr->Onode);
  264.                 } else if (hdr->Dpoint && hdr->Opoint) {
  265.                     sprintf (newmsg, "\01INTL%u:%u/%u %u:%u/%u\n",
  266.                         hdr->Dzone, fakenet, hdr->Dpoint,
  267.                         hdr->Ozone, fakenet, hdr->Opoint);
  268.                 } else if (!hdr->Dpoint && hdr->Opoint) {
  269.                     sprintf (newmsg, "\01INTL%u:%u/%u %u:%u/%u\n",
  270.                         hdr->Dzone, hdr->Dnet, hdr->Dnode,
  271.                         hdr->Ozone, fakenet, hdr->Opoint);
  272.                 } else {
  273.                     goto INTL_4d;
  274.                 }
  275.                 
  276.             }
  277.             else
  278.             {
  279.                 /*
  280.                 **    4D INTL regel maken.
  281.                 */
  282.                 
  283.                 INTL_4d:
  284.                 
  285.                 used_intl = TYPE4d;
  286.                 
  287.                 sprintf(newmsg, "\01INTL %u:%u/%u %u:%u/%u\n",
  288.                     hdr->Dzone, hdr->Dnet, hdr->Dnode, hdr->Ozone,
  289.                     hdr->Onet, hdr->Onode);
  290.             }
  291.         
  292.         /*
  293.         **    Als point, dan moet er een TOPT regel bij.
  294.         */
  295.         
  296.         if (hdr->Dpoint && used_intl == TYPE4d)
  297.         {
  298. #if defined OUTDEBUG
  299.                 if (dooutdeb) log_line(6,">Adding TOPT %u", hdr->Dpoint);
  300. #endif
  301.                 sprintf(&newmsg[strlen(newmsg)], "\01TOPT %u\n", hdr->Dpoint);
  302.                 dpoint = hdr->Dpoint;
  303.         }
  304.         
  305.         /*
  306.         **    En ook een FMPT regel als het moet.
  307.         */
  308.         
  309.         if (hdr->Opoint && used_intl == TYPE4d)
  310.         {
  311. #if defined OUTDEBUG
  312.             if (dooutdeb) log_line(6,">Adding FMPT %u", hdr->Opoint);
  313. #endif                
  314.             sprintf(&newmsg[strlen(newmsg)], "\01FMPT %u\n", hdr->Opoint);
  315.             opoint = hdr->Opoint;
  316.         }
  317.     }
  318.     
  319.     /*
  320.     **    Einde van AREA: regel afvraag routine.
  321.     */
  322.     
  323.     /*
  324.     **    Bericht te groot, breek af.
  325.     */
  326.     
  327.     if ((strlen(newmsg) + strlen(msgtext)) >= MAXMSGLENGTH -1)
  328.     {
  329.         log_line(4,Logmessage[M__NETMAIL_MSG_TO_BIG]);
  330. #if defined EXTERNDEBUG
  331.         if (debugflag)
  332.         {
  333.             log_line(6,">Free newmsg (Out)");
  334.             log_line(6,">Memory %ld", coreleft());
  335.         }
  336. #endif
  337.         free(newmsg);
  338.         return;
  339.     }
  340.     
  341.     strcat(newmsg, msgtext);
  342.     
  343.     /*
  344.     **    Zoek eventuele zone nummer uit dat gebruikt gaat worden in
  345.     **    de Via regel.
  346.     */
  347.     
  348.     for (i=0; i < nalias; i++)
  349.         if (hdr->Ozone == alias[i].zone &&
  350.             hdr->Onet == alias[i].net &&
  351.             hdr->Onode == alias[i].node) break;
  352.     
  353.     if (i >= nalias)
  354.     {
  355.         for (i=0; i < nalias; i++)
  356.             if (hdr->Ozone == alias[i].zone) break;
  357.         
  358.         if(i>=nalias) i=0;
  359.     }
  360.  
  361. #if defined OUTDEBUG
  362.     if (dooutdeb) log_line(6,">Using Via %u:%u/%u.%u i = %d", alias[i].zone, alias[i].net, alias[i].node, alias[i].point, i);
  363. #endif
  364.     
  365.     /*
  366.     **    Verkrijg huidige tijd.
  367.     */
  368.     
  369.     time(&secs);
  370.     tijd = localtime(&secs);
  371.     strftime(tm_now, 39, "%b %d %Y at %H:%M", tijd);
  372.     
  373.     OutMail[0] = 'O';
  374.     
  375.     /*
  376.     **    Als er geen AREA: regel instaat, plaats Via regel.
  377.     */
  378.     
  379.     if (strncmp(msgtext, "AREA:", 5)) sprintf(&newmsg[strlen(newmsg)],
  380.         "\n\01Via %s %u:%u/%u.%u, %s\n", OutMail, alias[i].zone, alias[i].net,
  381.         alias[i].node, alias[i].point, tm_now);
  382.         
  383.     zone    =    hdr->Dzone;
  384.     net    =    hdr->Dnet;
  385.     node    =    hdr->Dnode;
  386.     point    =    hdr->Dpoint;
  387.     
  388.     /*
  389.     **    Als niet gecrashed,
  390.     */
  391.     
  392.     if (!(hdr->flags & CRASH)) {
  393.     /*
  394.     **    en de key regel is niet gevonden, zoek de routing uit.
  395.     */
  396.     
  397.         if (havekey >= nkey)
  398.         {
  399.             retcode = routeOk(hdr);
  400.             
  401.             if (retcode != -1) address(RouteDef[retcode].destnode,
  402.                 &zone, &net, &node, &point, &j, domain);
  403.         }
  404.         else
  405.         {
  406.             zone    =    pwd[havekey].zone;
  407.             net    =    pwd[havekey].net;
  408.             node    =    pwd[havekey].node;
  409.             point    =    pwd[havekey].point;
  410.         }
  411.     }
  412.     
  413. #if defined OUTDEBUG
  414.     if (dooutdeb) log_line(6,">Save message to %u:%u/%u.%u", zone, net, node, point);
  415. #endif
  416.     
  417.     savemsg(NETMAILPACKET, hdr, -1, newmsg, zone, net, node, point);
  418.     
  419.     if (hdr->flags & KILLSEND)
  420.         hdr->flags |= DELETED;
  421.     else
  422.         hdr->flags |= SENT;
  423.  
  424. /*    
  425.     if (opoint) hdr->Opoint = opoint;
  426.     if (dpoint) hdr->Dpoint = dpoint;
  427. */
  428.     
  429.     nechos++;
  430.     
  431.     hprintf(S_EXP, "%d", nechos);
  432.     
  433. #if defined EXTERNDEBUG
  434.         if (debugflag)
  435.         {
  436.             log_line(6,">Free newmsg (Out)");
  437.             log_line(6,">Memory %ld", coreleft());
  438.         }
  439. #endif
  440.     
  441.     free(newmsg);
  442. }
  443.  
  444. VOID pack_mail(VOID)
  445. {
  446.     UWORD    zone, net, node, point;
  447.     WORD    i;
  448.     BYTE    Mfile[128], Hfile[128],
  449.             areabuf[128],
  450.             *msgbuf, *p, *q,
  451.             buffer[100];
  452.     LONG    tell;
  453.     
  454.     if (donopack) arc_PKTS(PACKMAIL);
  455.     
  456.     hprintf(S_AREA, "NETMAIL");
  457.     
  458.     log_line(3,Logmessage[M__NETMAIL_READ]);
  459.     
  460.     init_PKTS();
  461.     
  462.     sprintf(Mfile, "%s.MSG", mailarea);
  463.     sprintf(Hfile, "%s.HDR", mailarea);
  464.     
  465.     if ((FHDR = fopen(Hfile, "r+b")) == NULL)
  466.     {
  467.         log_line(6,Logmessage[M__CANT_OPEN_NETMAIL]);
  468.         return;
  469.     }
  470.     
  471.     hprintf (S_COMMENT, "Scanning NETMAIL");
  472.     
  473.     while (!feof(FHDR))
  474.     {
  475.         hdrread(&hdr, sizeof(MSGHEADER), FHDR);
  476.         
  477.         if (feof(FHDR)) break;
  478.         if (!(hdr.flags & MSGLOCAL) || hdr.flags & DELETED || hdr.flags & SENT)
  479.             continue;
  480.         
  481.         if ((tell = ftell(FHDR)) < sizeof(MSGHEADER))
  482.         {
  483.             if (tell > 0L)
  484.                 log_line(6,Logmessage[M__FILE_PROBLEM], tell);
  485.             
  486.             break;
  487.         }
  488.         
  489.         if (hdr.flags & KILLSEND)
  490.             hdr.flags |= DELETED;
  491.         else
  492.             hdr.flags |= SENT;
  493.         
  494.         if ((FMSG = fopen(Mfile, "rb")) != NULL)
  495.         {
  496.             fseek(FMSG, hdr.Mstart, SEEK_SET);
  497.             
  498.             if (ferror(FMSG))
  499.             {
  500.                 log_line(6,Logmessage[M__SEEK_ERROR]);
  501.                 fclose(FMSG);
  502.                 continue;
  503.             }
  504.                         
  505. #if defined EXTERNDEBUG
  506.         if (debugflag)
  507.         {
  508.             log_line(6,">Alloc msgbuf (Out)");
  509.             log_line(6,">Memory %ld", coreleft());
  510.         }
  511. #endif
  512.             msgbuf = (BYTE *) myalloc(MAXMSGLENGTH);
  513.             msgread(msgbuf, hdr.size, FMSG);
  514.             fclose(FMSG);
  515.             
  516.             check_HDR();
  517.             
  518. #if !defined TESTCASE
  519.             fseek(FHDR, ftell(FHDR) - sizeof(MSGHEADER), SEEK_SET);
  520.             hdrwrite(&hdr, sizeof(MSGHEADER), FHDR);
  521.             fflush(FHDR);
  522. #endif
  523.  
  524.             hdr.flags &= ~(DELETED); 
  525.  
  526.             hprintf(S_DEST, "%u:%u/%u.%u", hdr.Dzone, hdr.Dnet,
  527.                 hdr.Dnode, hdr.Dpoint);
  528.             hprintf(S_FROM, "%u:%u/%u.%u", hdr.Ozone, hdr.Onet,
  529.                 hdr.Onode, hdr.Opoint);
  530.             
  531.             log_line(3,"*%-20.20s (%u:%u/%u.%u) > %-20.20s (%u:%u/%u.%u)",
  532.                 hdr.from, hdr.Ozone, hdr.Onet, hdr.Onode, hdr.Opoint,
  533.                 hdr.to, hdr.Dzone, hdr.Dnet, hdr.Dnode, hdr.Dpoint);
  534.             
  535.             i = 0;
  536.                     
  537.             if (!strncmp(msgbuf, "AREA:",5))
  538.             {
  539.                 p = msgbuf;
  540.                 while (*p && !isspace(*p)) areabuf[i++] = *p++;
  541.                 areabuf[i] = EOS;
  542.                 msgbuf = strip_line(msgbuf, "AREA:");
  543.                 if ((p = strline (msgbuf, "--- ")) != NULL) {
  544.                     q = skip_to_token (p, '\n');
  545.                     --q;
  546.                     if (q) {
  547.                         strncpy (TEAR_buffer, q, 511);
  548.                         strcpy (q, " [IOS ");
  549.                         strcpy (buffer, &ScanMail[strlen (ScanMail) - 5]);
  550.                         strcat (q, buffer);
  551.                         strcat (q, "]");
  552.                         strcat (q, TEAR_buffer);
  553.                     }
  554.                 }
  555.             }
  556.             else
  557.             {
  558.                 if (dostripPID)
  559.                     msgbuf = strip_line(msgbuf, "\01PID:");
  560.             }
  561.             
  562.             /*
  563.             **    Change MSGID.
  564.             */
  565.             
  566.             if (get_msgid(msgbuf, &zone, &net, &node, &point) &&
  567.                 zone != hdr.Ozone && net != hdr.Onet && node != hdr.Onode)
  568.                 msgbuf = change_msgid(msgbuf, &hdr);
  569.             
  570.             if (i) msgbuf = add_a_line(areabuf, "", msgbuf);
  571.             
  572.             pack_netmail(&hdr, msgbuf);
  573. #if defined EXTERNDEBUG
  574.         if (debugflag)
  575.         {
  576.             log_line(6,">Free msgbuf (Out)");
  577.             log_line(6,">Memory %ld", coreleft());
  578.         }
  579. #endif
  580.             
  581.             free(msgbuf);
  582.         }
  583.     }
  584.     
  585.     fclose(FHDR);
  586.     
  587.     close_PKTS();
  588.     arc_PKTS(donopack);
  589.     
  590.     hold_to_poll(HOLDPOLL);
  591.     hold_to_poll(HOLDMAX);
  592.     hold_to_poll(HOLDDEL);
  593.     prepare_poll();
  594.     
  595.     return;
  596. }
  597.  
  598. VOID arc_PKTS(BYTE what)
  599. {
  600.     struct ffblk    blk;
  601.     BYTE            buffer[128],
  602.                     Mail[4];
  603.     WORD            i;
  604.     
  605.     log_line(4,Logmessage[M__ARC_PACKETS]);
  606.  
  607.     sprintf(buffer, "%s*.???", mailtemp);
  608.     strcpy (Mail, "HNC");
  609.     
  610.     for (i = 0; i < 3; i++) {
  611.     
  612.         /*instal_s_scr();*/
  613.         
  614.         buffer [ (WORD) strlen (buffer) - 1] = Mail[i];
  615.         
  616.         if (!findfirst(buffer, &blk, 0))
  617.         {
  618.             crunch_it(blk.ff_name, what, blk.ff_fsize);
  619.             
  620.             while (!findnext(&blk))
  621.                 crunch_it(blk.ff_name, what, blk.ff_fsize);
  622.         }
  623.         
  624.         /*end_screen();*/
  625.     }
  626. }
  627.  
  628. VOID crunch_it(BYTE *packet, BYTE do_it, LONG size)
  629. {
  630.     BYTE        buffer[256],
  631.                 folder[128],
  632.                 fname[128],
  633.                 dummy[128],
  634.                 program[128],
  635.                 cmds[256],
  636.                 temp[128],
  637.                 mailtype,
  638.                 arctype,
  639.                 testarc,
  640.                 *p, *q,
  641.                 *currentfolder,
  642.                 *COPYbuf,
  643.                 domain[10];
  644.     WORD        i, x,
  645.                 ret;
  646.     UWORD        j,
  647.                 zone, net, node, point,
  648.                 t_zone, t_net, t_node, t_point,
  649.                 netdiff,nodediff,
  650.                 pointnet = 0;
  651.     LONG        secs_now;
  652.     BOOLEAN        hold,
  653.                     Set_old_BT=FALSE;
  654.                     
  655.     struct ffblk blk;
  656.     
  657. #if defined OUTDEBUG
  658.     log_line(6,">Arc packet %s", packet);
  659. #endif
  660.     
  661.     strcpy(buffer, packet);
  662.     
  663.     hprintf (S_COMMENT, "Bundling packets");
  664.     
  665.     if ((p = strchr(buffer, '.')) != NULL)
  666.     {
  667.         mailtype = p[3];
  668.         p[3] = EOS;
  669.     }
  670.     
  671.     /*
  672.     **    Check for PKT files
  673.     */
  674.     
  675.     if (mailtype == 'T') return;
  676.     
  677.     if (minpktsize && mailtype != 'C' && size < minpktsize)
  678.     {
  679.         log_line(3,Logmessage[M__PACKET_TO_SMALL]);
  680.         return;
  681.     }
  682.     
  683.     if ((i = (WORD)strlen(buffer)) != 11)
  684.     {
  685.         log_line(4,Logmessage[M__FILENAME_PROBLEM]);
  686.         return;
  687.     }
  688.  
  689. /*    
  690.     get4Daddress(buffer, &zone, &net, &node, &point);
  691. */
  692.  
  693. /*
  694. **    Lees de packetheader voor de bestemming. De filenaam methode is
  695. **    onbetrouwbaar, in zoverre dat hoge nummers niet kunnen worden
  696. **    weergegeven.
  697. */
  698.  
  699.     sprintf (fname, "%s%s%c", mailtemp, buffer, mailtype);
  700.     
  701.     if ((FDUMMY = fopen (fname, "rb")) == NULL) {
  702.         log_line (6, "+Can't open packet.");
  703.         terminate (10);
  704.     }
  705.     
  706.     
  707.     fread (&phdr, sizeof (struct _pkthdr), 1, FDUMMY);
  708.     fclose (FDUMMY);
  709.  
  710.     if ((i= intel(phdr.ph_rate)) == TYPE2_2) {
  711.         memcpy(&phdr2, &phdr, sizeof(struct _phdr2));
  712.         memset(&phdr, 0, sizeof(struct _pkthdr));
  713.         
  714.         zone = intel (phdr2.ph_dzone);
  715.         net  = intel (phdr2.ph_dnet);
  716.         node = intel (phdr2.ph_dnode);
  717.         point= intel (phdr2.ph_dpoint);
  718.     } else if (intel(phdr.ph_capable) == phdr.ph_CWcopy &&
  719.             phdr.ph_capable && intel(phdr.ph_capable) & TYPE2PLUS) {
  720.         
  721.         zone = intel (phdr.ph_dzone);
  722.         net  = intel (phdr.ph_dnet);
  723.         node = intel (phdr.ph_dnode);
  724.         point= intel (phdr.ph_dpoint);
  725.     } else {
  726.         zone = intel (phdr.ph_qdzone);
  727.         net  = intel (phdr.ph_dnet);
  728.         node = intel (phdr.ph_dnode);
  729.         point= intel (phdr.ph_dpoint);
  730.         for (i = 0; i < nalias; i++) {
  731.             if (net == alias[i].pointnet && zone == alias[i].zone) {
  732.                 if (!alias[i].pointnet) {
  733.                     log_line (6, "-Can't send 3D packets without an pointnet.");
  734.                     return;
  735.                 }
  736.                 point = node;
  737.                 node  = alias[i].node;
  738.                 net   = alias[i].net;
  739.                 break;
  740.             }
  741.         }
  742.     }        
  743.  
  744.     hprintf(S_DEST, "%u:%u/%u.%u", zone, net, node, point);
  745.     
  746. #if defined OUTDEBUG
  747.     if (dooutdeb) log_line(6,">Getaddress returns %u:%u/%u.%u", zone, net, node, point);
  748. #endif
  749.     
  750.     /*
  751.     **    Check for pointnet.
  752.     */
  753.     
  754.     for (i=0; i < nalias; i++)
  755.         if (net == alias[i].pointnet)
  756.         {
  757.             t_zone = alias[i].zone;
  758.             t_net = alias[i].net;
  759.             t_node = alias[i].node;
  760.             t_point = node;
  761.             
  762.             break;
  763.         }
  764.     
  765.     if (i >= nalias)
  766.     {
  767.         t_zone = zone;
  768.         t_net = net;
  769.         t_node = node;
  770.         t_point = point;
  771.         
  772.         i = 0;
  773.     }
  774.     
  775.     hprintf (S_FROM, "%u:%u/%u.%u", alias[i].zone, alias[i].net, 
  776.         alias[i].node, alias[i].point);
  777.     
  778. #if defined OUTDEBUG
  779.     if (dooutdeb) log_line(6,">Destination %u:%u/%u.%u", t_zone, t_net, t_node, t_point);
  780. #endif    
  781.     
  782.     /*
  783.     **    Verplaats naar ander nodenummer.
  784.     */
  785.     
  786.     if ( mailtype != 'C' )
  787.         ret = moveOk(t_zone, t_net, t_node, t_point);
  788.     else ret = -1;
  789.     
  790.     if (ret != -1)
  791.     {
  792.         address(RouteDef[ret].destnode, &t_zone, &t_net, &t_node, &t_point, &j, domain);
  793.         log_line(4,Logmessage[M__MSG_MOVED], t_zone, t_net, t_node, t_point);
  794.     }
  795.     
  796.     if (mailtype != 'C')
  797.         hold = holdOk(t_zone, t_net, t_node, t_point);
  798.     else hold = FALSE;
  799.     
  800.     if (mailtype == 'H' && donewbink == TRUE)
  801.         hold = TRUE;
  802.     
  803.     time(&secs_now);
  804.     tijd = localtime(&secs_now);
  805.     
  806.     if (t_point)
  807.     {
  808.         for (i=0; i < nalias; i++)
  809.             if (t_zone == alias[i].zone && t_net == alias[i].net &&
  810.                 t_node == alias[i].node)
  811.             {
  812.                 zone = alias[i].zone;
  813.                 net = alias[i].pointnet;
  814.                 node = t_point;
  815.                 
  816.                 break;
  817.             }
  818.         
  819.         if (i >= nalias)
  820.         {
  821.             for (i=0; i < nkey ; i++) {
  822.                 if (t_zone == pwd[i].zone && t_net == pwd[i].net &&
  823.                     t_node == pwd[i].node && t_point == pwd[i].point) {
  824.                     
  825.                     if ( crashPoint(t_zone, t_net, t_node, t_point) == FALSE || donewbink == FALSE) {
  826.                         i = nkey;
  827.                         break;
  828.                     }
  829.                     break;
  830.                 }
  831.             }
  832.             if (i >= nkey) {
  833.                 i = 0;
  834.                 log_line(6,Logmessage[M__UNKNOWN_POINT], t_zone, t_net, t_node, t_point);
  835.                 t_point = 0;
  836.             }
  837.         }
  838.     }
  839.     else
  840.     {
  841.         zone = t_zone;
  842.         net = t_net;
  843.         node = t_node;
  844.         
  845.         for (i=0; i < nalias; i++)
  846.             if (t_zone == alias[i].zone) break;
  847.         
  848.         if (i >= nalias) i = 0;
  849.     }
  850.  
  851.     hprintf (S_FROM, "%u:%u/%u.%u", alias[i].zone, alias[i].net, 
  852.         alias[i].node, alias[i].point);
  853.     
  854.     /*
  855.     **    Pack message
  856.     */
  857.     
  858.     if (arcOk(t_zone, t_net, t_node, t_point) && do_it == PACKMAIL) {
  859.  
  860.         if(!alias[i].point)  {
  861.             netdiff = alias[i].net - net;
  862.             nodediff= alias[i].node- node;
  863.             pointnet= alias[i].pointnet;
  864.         } else {
  865.             netdiff = alias[i].pointnet - net;
  866.             nodediff= alias[i].node - alias[i].point;
  867.             pointnet= alias[i].pointnet;
  868.         }
  869.  
  870.         /*
  871.         **    Outbound dir is ....
  872.         */
  873.         
  874.         currentfolder = outbound_dir(t_zone, t_net, t_node, t_point);
  875.         strcpy(folder, currentfolder);
  876.         
  877.         if ((q = strrchr(folder, '\\')) != NULL) *q = EOS;
  878.         
  879.         /*
  880.         **    Rename to PKT
  881.         */
  882.         
  883.         sprintf(buffer, "%s%s", mailtemp, packet);
  884.         sprintf(temp, "%02d%02d%02d%02d.PKT", tijd->tm_mday,
  885.             tijd->tm_hour, tijd->tm_min, tijd->tm_sec);
  886.         sprintf(fname, "%s%s", mailtemp, temp);
  887.         
  888.         rename(buffer, fname);
  889.         
  890.         if (folder[strlen(folder)-1] == '\\')
  891.             folder[strlen(folder)-1] = EOS;
  892.  
  893. /*
  894. **    Als de point een hoger pointnummer heeft dan 960, ga dan naar de
  895. **    oude FLO etc. filenamen.
  896. */
  897.  
  898.         if (t_point > HIGH_POINT    && donewbink == TRUE) {
  899.             donewbink = FALSE;
  900.             Set_old_BT = TRUE;
  901.         }
  902.         
  903.         /*
  904.         **    Get packetname
  905.         */
  906.         
  907.         if(donewbink == TRUE && dothebox == TRUE) {
  908.             log_line(3,Logmessage[M__MORE_MAILERS]);
  909.             dothebox = FALSE;
  910.         }
  911.         
  912.         if(donewbink == TRUE || dothebox == TRUE) {
  913.             if(donewbink) 
  914.                 xsprintf(buffer, "%s\\%03z%03z%02z.", folder, t_net, t_node, t_point);
  915.             else {
  916.                 if (t_point) {
  917.                     for (i = 0; i < nalias; i++) {
  918.                         if (t_zone == alias[i].zone && t_net == alias[i].net &&
  919.                             t_node == alias[i].node) {
  920.                                 if (!alias[i].pointnet) {
  921.                                     log_line (6, "-Can't send. No pointnet.");
  922.                                     return;
  923.                                 }
  924.                                 net = alias[i].pointnet;
  925.                                 node= t_point;
  926.                                 break;
  927.                         }
  928.                     }
  929.                 }
  930.                 xsprintf(buffer, "%s\\%02z%03z%03z.", folder, zone, net, node);
  931.             }
  932.             if (hold) {
  933.                 if (check_hold_poll(buffer, ARC_PKTS) == FAILURE) {
  934.                     strcat (buffer,(donewbink == TRUE) ? "HAT" : "HA");
  935.                 } else {
  936.                     strcat (buffer,(donewbink == TRUE) ? "OAT" : "NA");
  937.                 }
  938.             }
  939.             else {
  940.                 if (mailtype == 'C')
  941.                     strcat(buffer, (donewbink == TRUE) ? "CAT" : "CA");
  942.                 else
  943.                     strcat(buffer, (donewbink == TRUE) ? "OAT" : "NA");
  944.             }
  945.         }
  946.         else 
  947.             sprintf(buffer, "%s\\%04x%04x.%s0",folder,
  948.             netdiff, nodediff, isuf[tijd->tm_wday]);
  949.             
  950.         strcpy(dummy, buffer);
  951.         
  952. #if defined OUTDEBUG
  953.         if (dooutdeb) log_line(6,">File: %s", dummy);
  954. #endif
  955.         
  956.         sprintf(&buffer[strlen(buffer)], " %s", fname);
  957.         
  958.         /*
  959.         **    ARC-Typ for this system
  960.         */
  961.                 
  962.         arctype = isUNKNOWN;
  963.         
  964.         for (j=0; j < nkey; j++)
  965.             if (t_zone == pwd[j].zone && t_net == pwd[j].net &&
  966.                 t_node == pwd[j].node && t_point == pwd[j].point)
  967.             {
  968.                 arctype = pwd[j].arctype;
  969.                 break;
  970.             }
  971.         
  972.         if (j >= nkey) arctype = isUNKNOWN;
  973.         
  974.         /*
  975.         **    Check al aanwezige packets voor veranderingen.
  976.         */
  977.         
  978.         if (!findfirst(dummy, &blk, 0))
  979.         {
  980.             testarc = ARCtype(dummy);
  981.             
  982.             if (testarc != arctype)
  983.             {
  984.                 log_line(3,Logmessage[M__PACKER_CHANGED], ArcMail[testarc], ArcMail[arctype]);
  985.                 log_line(3,Logmessage[M__TAKE_OLD]);
  986.                 
  987.                 arctype = testarc;
  988.             }
  989.         }
  990.         
  991.         switch (arctype)
  992.         {
  993.             case isLZHmail:
  994.             
  995.                 if (strlen(Lzh))
  996.                 {
  997.                     strcpy(program, Lzh);
  998.                     
  999.                     if (strlen(LzheA)) strcpy(cmds,LzheA);
  1000.                     else strcpy(cmds, "a");
  1001.                 }
  1002.                 else
  1003.                 {
  1004.                     log_line(5,Logmessage[M__NO_PACKER_SPEC], "LZH");
  1005.                     goto Default_archive;
  1006.                 }
  1007.                 
  1008.                 break;
  1009.                 
  1010.             case isARCmail:
  1011.             
  1012.                 if (strlen(Arc))
  1013.                 {
  1014.                     strcpy(program, Arc);
  1015.                     
  1016.                     if (strlen(ArceA)) strcpy(cmds,ArceA);
  1017.                     else strcpy(cmds, "a");
  1018.                 }
  1019.                 else
  1020.                 {
  1021.                     log_line(5,Logmessage[M__NO_PACKER_SPEC], "ARC");
  1022.                     goto Default_archive;
  1023.                 }
  1024.                 
  1025.                 break;
  1026.                 
  1027.             case isZIPmail:
  1028.             
  1029.                 if (strlen(Zip))
  1030.                 {
  1031.                     strcpy(program, Zip);
  1032.                     
  1033.                     if(strlen(ZipeA)) strcpy(cmds,ZipeA);
  1034.                     else strcpy(cmds, "-a");
  1035.                 }
  1036.                 else
  1037.                 {
  1038.                     log_line(5,Logmessage[M__NO_PACKER_SPEC], "ZIP");
  1039.                     goto Default_archive;
  1040.                 }
  1041.                 
  1042.                 break;
  1043.                 
  1044.             case isARJmail:
  1045.             
  1046.                 if (strlen(Arj))
  1047.                 {
  1048.                     strcpy(program, Arj);
  1049.                     
  1050.                     if (strlen(ArjeA)) strcpy(cmds, ArjeA);
  1051.                     else strcpy(cmds, "a");
  1052.                 }
  1053.                 else
  1054.                 {
  1055.                     log_line(5,Logmessage[M__NO_PACKER_SPEC], "ARJ");
  1056.                     goto Default_archive;
  1057.                 }
  1058.                 
  1059.                 break;
  1060.                 
  1061.             case isUNKNOWN:
  1062.             
  1063.                 Default_archive:
  1064.                 
  1065.                 if (strlen(Arch))
  1066.                 {
  1067.                     strcpy(program, Arch);
  1068.                     
  1069.                     if (strlen(Archcmdo)) strcpy(cmds,Archcmdo);
  1070.                     else strcpy(cmds, "a");
  1071.                 }
  1072.                 else log_line(5,Logmessage[M__NO_PACKER_SPEC], "");
  1073.                 
  1074.                 break;
  1075.                 
  1076.             default:
  1077.             
  1078.                 log_line(4,Logmessage[M__UNKNOWN_ERROR]);
  1079.                 break;
  1080.         }
  1081.         
  1082.         sprintf(&cmds[strlen(cmds)], " %s", buffer);
  1083.  
  1084.         printf("\033Y%c%c\033J\033Y%c%c", S_START+' ', ' ', S_START+' ', ' ');
  1085.         
  1086.         LOG_line = S_START + 1;
  1087.         
  1088.         log_line(3,Logmessage[M__SEND_MAIL], t_zone, t_net, t_node,
  1089.             t_point, ArcMail[arctype], (hold) ? "Hold" : "Poll");
  1090.         
  1091.         /*
  1092.         **    Run arc program
  1093.         */
  1094.                 
  1095.         x = exec(program, cmds, "", &i);
  1096.  
  1097.         printf("\033Y%c%c\033J\033Y%c%c", S_START+' ', ' ', S_START+' ', ' ');
  1098.         
  1099.         LOG_line = S_START + 1;
  1100.         
  1101.         if (i) log_line(6,Logmessage[M__RETURNS_ERRORCODE], i);
  1102.         
  1103.         if (x != FAILURE)
  1104.         {
  1105.             if (findfirst(dummy, &blk, 0) || i)
  1106.             {
  1107.                 log_line(6,Logmessage[M__PACKET_NOT_ARCED]);
  1108.                 sprintf(dummy, "%s%s", mailtemp, temp);
  1109.             }
  1110.             else {
  1111.                 
  1112.                 sprintf(buffer, "%s%s", mailtemp, temp);
  1113.                 unlink(buffer);
  1114.                 
  1115.                 if (Set_old_BT == TRUE)
  1116.                     donewbink = Set_old_BT;
  1117.                 
  1118.                 if(donewbink == TRUE || dothebox == TRUE) {
  1119.                     return;
  1120.                 }
  1121.             }
  1122.         }
  1123.         else
  1124.         {
  1125.             log_line(6,Logmessage[M__CANT_EXEC_PRG], program);
  1126.             strcpy (packet, temp);
  1127.             goto unbundled;
  1128.             /*
  1129.             sprintf(dummy, "%s\\%s", mailtemp, temp);
  1130.             */
  1131.         }
  1132.         
  1133.         if(t_point ) {
  1134.             for (i = 0; i< nalias; i++) {
  1135.                 if (alias[i].zone == t_zone &&
  1136.                     alias[i].net  == t_net &&
  1137.                     alias[i].node == t_node) {
  1138.                         if (!alias[i].point) {
  1139.                             if (!alias[i].pointnet) {
  1140.                                 log_line (6, "-Can't send. Pointnet unknown.");
  1141.                                 if (Set_old_BT == TRUE)
  1142.                                     donewbink = Set_old_BT;
  1143.                                 return;
  1144.                             }
  1145.                             sprintf(buffer,"%s\\%04x%04x.",folder,
  1146.                             alias[i].pointnet,t_point);
  1147.                         }
  1148.                         else
  1149.                             sprintf(buffer,"%s\\%04x%04x.",folder,
  1150.                             t_net,t_node);
  1151.                         break;
  1152.                 }
  1153.             }
  1154.             if (i >= nalias) 
  1155.                 sprintf(buffer,"%s\\%04x%04x.",folder,t_net,t_node);
  1156.         }
  1157.         else
  1158.             sprintf(buffer,"%s\\%04x%04x.",folder,t_net,t_node);
  1159.             
  1160.         if(hold) {
  1161.             strcat(buffer,"HLO");
  1162.         } else {
  1163.             if(mailtype == 'C') {
  1164.                 strcat(buffer,"CLO");
  1165.             } else {
  1166.                 strcat(buffer,"FLO");
  1167.             }
  1168.         }
  1169.         
  1170.         if((PACKET = fopen(buffer,"r+")) == NULL) {
  1171.             if((PACKET = fopen(buffer,"w")) == NULL) {
  1172.                 log_line(6,Logmessage[M__CANT_CREATE_POLL]);
  1173.                 return;
  1174.             }
  1175.         } else {
  1176.             while(fgets(temp,128,PACKET)) {
  1177.                 if(!strncmp(&temp[1],dummy,(int)strlen(dummy))) {
  1178.                     fclose(PACKET);
  1179.                     return;
  1180.                 }
  1181.             }
  1182.         }
  1183.         fseek(PACKET,0L,SEEK_END);
  1184.         fprintf(PACKET,"^%s ; Packet for %u:%u/%u.%u\n",dummy,
  1185.         t_zone,t_net,t_node,t_point);
  1186.         fclose(PACKET);
  1187.         if (Set_old_BT == TRUE)
  1188.             donewbink = Set_old_BT;
  1189.         return;
  1190.     }
  1191.     
  1192.     /*    
  1193.     **    buffer = <mailtempdir><filename>.<NM..>
  1194.     **    dummy  = <filename>.<OPT..>
  1195.     **    temp   = <outbounddir><filename>.<OPT..>
  1196.     */
  1197.     
  1198.     unbundled:
  1199.     
  1200.     hprintf (S_COMMENT, "Send unbundled packet.");
  1201.     
  1202.     if (t_point > HIGH_POINT && donewbink == TRUE) {
  1203.         donewbink = FALSE;
  1204.         Set_old_BT = TRUE;
  1205.     }
  1206.     
  1207.     if(donewbink == TRUE) {
  1208.         sprintf(buffer, "%s%s", mailtemp, packet);
  1209.         xsprintf(dummy, "%03z%03z%02z.", t_net, t_node, t_point);
  1210.         sprintf(temp, "%s%s", outbound, dummy);
  1211.     } else {
  1212.         sprintf(buffer,"%s%s",mailtemp,packet);
  1213.         if (!t_point) {
  1214.             sprintf(dummy,"%04x%04x.",t_net,t_node);
  1215.         } else {
  1216.             for (i=0; i <nalias; i++) {
  1217.                 if (alias[i].zone == t_zone &&
  1218.                     alias[i].net  == t_net  &&
  1219.                     alias[i].node == t_node) {
  1220.                         if (!alias[i].point) {
  1221.                             if (!alias[i].pointnet) {
  1222.                                 log_line (6, "-Can't send. Pointnet unknown.");
  1223.                                 if (Set_old_BT == TRUE);
  1224.                                     donewbink = Set_old_BT;
  1225.                                 return;
  1226.                             }
  1227.                             sprintf(dummy,"%04x%04x.", alias[i].pointnet,
  1228.                             t_point);
  1229.                         }
  1230.                         else
  1231.                             sprintf(buffer,"%s\\%04x%04x.",folder,
  1232.                             t_net,t_node);
  1233.                         break;
  1234.                     
  1235.                 }
  1236.             }
  1237.             if (i >= nalias)
  1238.                 sprintf(buffer,"%s\\%04x%04x.",folder,t_net,t_node);
  1239.         }
  1240.         sprintf(temp,"%s%s",outbound,dummy);
  1241.     }
  1242.  
  1243.     
  1244.     if (hold)
  1245.     {
  1246.         if(donewbink == TRUE) {
  1247.             strcat(temp, "HPT");
  1248.             strcat(dummy, "HPT");
  1249.         } else {
  1250.             strcat(temp, "HUT");
  1251.             strcat(dummy,"HUT");
  1252.         }
  1253.     }
  1254.     else
  1255.     {
  1256.         if (mailtype == 'C')
  1257.         {
  1258.             if(donewbink == TRUE) {
  1259.                 strcat(temp, "CPT");
  1260.                 strcat(dummy, "CPT");
  1261.             } else {
  1262.                 strcat(temp, "CUT");
  1263.                 strcat(dummy, "CUT");
  1264.             }
  1265.         }
  1266.         else
  1267.         {
  1268.             if(donewbink == TRUE) {
  1269.                 strcat(temp,"OPT");
  1270.                 strcat(dummy, "OPT");
  1271.             } else {
  1272.                 strcat(temp, "OUT");
  1273.                 strcat(dummy, "OUT");
  1274.             }
  1275.         }
  1276.     }
  1277.     
  1278.     /*
  1279.     **    p = Domain folder + filename.
  1280.     */
  1281.     
  1282.     p = outbound_dir(t_zone, t_net, t_node, t_point);
  1283.     q = strrchr(p, '\\');
  1284.     
  1285.     if (q)
  1286.     {
  1287.         sprintf(++q, "%s", dummy);
  1288.         strcpy(dummy, p);
  1289.     }
  1290.     else
  1291.         sprintf(dummy, "%s%s", outbound, dummy);
  1292.  
  1293.     if (hold) {
  1294.         strcpy (temp, dummy);
  1295.         q = strrchr (temp, '.');
  1296.         
  1297.         if (q) {
  1298.             *++q = EOS;
  1299.             if (check_hold_poll (temp, NORM_PKTS) == SUCCESS) {
  1300.                 q = strrchr (dummy, '.');
  1301.                 strcpy (++q, "OFT");
  1302.             }
  1303.         }
  1304.     }
  1305.     
  1306.     log_line(4,Logmessage[M__SEND_UNPACKED], t_zone, t_net, t_node, t_point);
  1307.     
  1308.     findfirst (buffer, &blk, 0);
  1309.     
  1310.     if ((PACKET = fopen(buffer, "r+b")) != NULL)
  1311.     {
  1312.         if ((MMSG = fopen(dummy, "r+b")) == NULL)
  1313.         {
  1314.             if ((MMSG = fopen(dummy, "wb")) == NULL)
  1315.             {
  1316.                 log_line(4,Logmessage[M__CANT_MOVE_PKT]);
  1317.                 
  1318.                 fclose(PACKET);
  1319.                 free(p);
  1320.                 if (Set_old_BT == TRUE)
  1321.                     donewbink = Set_old_BT;
  1322.                 return;
  1323.             }
  1324.         }
  1325.         else
  1326.         {
  1327.             /*
  1328.             **    If already exist, append mail.
  1329.             */
  1330.             
  1331.             fseek(PACKET, (LONG)sizeof(struct _pkthdr), SEEK_SET);
  1332.             fseek(MMSG, 0L, SEEK_END);
  1333.             fseek(MMSG, ftell(MMSG) - 2L, SEEK_SET);
  1334.         }
  1335.         
  1336.         /*
  1337.         **    Check size of file, fits in memory ?
  1338.         */
  1339.         
  1340.         COPYbuf = (BYTE *) myalloc (blk.ff_fsize + 10);
  1341.         
  1342.         j = 0;
  1343.         
  1344.         /*
  1345.         **    Copy packet to current dir.
  1346.         */
  1347.         
  1348.         if (!COPYbuf) {
  1349.             while (!feof(PACKET))
  1350.             {
  1351.                 i = fgetc(PACKET);
  1352.                 if (feof(PACKET)) break;
  1353.                 
  1354.                 if ((j = ferror(PACKET)) != 0)
  1355.                 {
  1356.                     log_line(6,Logmessage[M__READ_ERROR], errno);
  1357.                     break;
  1358.                 }
  1359.                 
  1360.                 putc(i, MMSG);
  1361.                 
  1362.                 if ((j = ferror(MMSG)) != 0) w_error("copying packet");
  1363.             }
  1364.         } else {
  1365.             fread (COPYbuf, blk.ff_fsize, 1, PACKET);
  1366.             fwrite(COPYbuf, blk.ff_fsize, 1, MMSG);
  1367.             
  1368.             free (COPYbuf);
  1369.         }
  1370.         
  1371.         fclose(PACKET);
  1372.         fclose(MMSG);
  1373.         
  1374.         if (!j) unlink(buffer);
  1375.         
  1376.         free(p);
  1377.         if (Set_old_BT == TRUE)
  1378.             donewbink = Set_old_BT;
  1379.         return;
  1380.     }
  1381.     if (Set_old_BT == TRUE)
  1382.         donewbink = Set_old_BT;
  1383. }
  1384.  
  1385. BOOLEAN get_gateway(MSGHEADER *hdr, BYTE *msg)
  1386. {
  1387.     WORD    i;
  1388.     UWORD    zone, net, node, point,
  1389.             wzone, wnet, wnode, wpoint,
  1390.             j;
  1391.     BYTE    *p, *q, *a,
  1392.             *gatename = strline(msg, "&To:"),
  1393.             temp[100],
  1394.             wasfrom[40],
  1395.             newname[40],
  1396.             domain[10],
  1397.             *newmsg;
  1398.     
  1399.     if (!gatename)
  1400.     {
  1401.         hdr->flags |= SENT;
  1402.         return(FALSE);
  1403.     }
  1404.     
  1405.     p = gatename;
  1406.     
  1407.     for (q = temp; *p && *p != '\n'; *q++ = *p++);
  1408.     *q = EOS;
  1409.     
  1410. #if defined OUTDEBUG
  1411.     if (dooutdeb) log_line(6,">Temp has %s", temp);
  1412. #endif
  1413.     
  1414.     for (q = temp, i=0; i < 4; i++) q++;
  1415.     q = skip_blanks(q);
  1416.     
  1417.     if (!*q)
  1418.     {
  1419.         hdr->flags |= SENT;
  1420.         return(FALSE);
  1421.     }
  1422.     
  1423.     for (a = newname; *q && *q != '@'; *a++ = *q++);
  1424.     *a = EOS;
  1425.     
  1426. #if defined OUTDEBUG
  1427.     if (dooutdeb) log_line(6,">Newname %s", newname);
  1428. #endif
  1429.     
  1430.     if (!*q)
  1431.     {
  1432.         hdr->flags |= SENT;
  1433.         return(FALSE);
  1434.     }
  1435.     
  1436.     q = skip_blanks(++q);
  1437.     
  1438.     if (!*q)
  1439.     {
  1440.         hdr->flags |= SENT;
  1441.         return(FALSE);
  1442.     }
  1443.     
  1444.     address(q, &zone, &net, &node, &point, &j, domain);
  1445.     
  1446.     log_line(4,Logmessage[M__GATEWAY_MSG], newname, zone, net, node, point);
  1447.  
  1448.     strcpy(wasfrom, hdr->from);
  1449.     
  1450.     wzone = hdr->Ozone;
  1451.     wnet = hdr->Onet;
  1452.     wnode = hdr->Onode;
  1453.     wpoint = hdr->Opoint;
  1454.     
  1455.     for (i=0; i < nalias; i++)
  1456.         if (zone == alias[i].zone)
  1457.         {
  1458.             hdr->Ozone = alias[i].zone;
  1459.             hdr->Onet = alias[i].net;
  1460.             hdr->Onode = alias[i].node;
  1461.             hdr->Opoint = alias[i].point;
  1462.             
  1463.             break;
  1464.         }
  1465.     
  1466.     if (i >= nalias)
  1467.     {
  1468.         log_line(3,Logmessage[M__NO_GATEWAY], zone);
  1469.         hdr->flags |= SENT;
  1470.         return(FALSE);
  1471.     }
  1472.     
  1473.     strcpy(hdr->from, "GateWay");
  1474.     strcpy(hdr->to, newname);
  1475.     
  1476.     sprintf(temp, "%u:%u/%u", hdr->Dzone, hdr->Dnet, hdr->Dnode);
  1477.     
  1478.     hdr->Dzone = zone;
  1479.     hdr->Dnet = net;
  1480.     hdr->Dnode = node;
  1481.     hdr->Dpoint = point;
  1482.     
  1483. #if defined EXTERNDEBUG
  1484.         if (debugflag)
  1485.         {
  1486.             log_line(6,">Alloc newmsg (Out gate)");
  1487.             log_line(6,">Memory %ld", coreleft());
  1488.         }
  1489. #endif
  1490.  
  1491.     newmsg = (BYTE *) myalloc(MAXMSGLENGTH);
  1492.     
  1493.     sprintf(newmsg, "&From: %s@%u:%u/%u.%u\n", wasfrom, wzone, wnet, wnode, wpoint);
  1494.     
  1495.     if ((strlen(newmsg) + strlen(msg)) > (MAXMSGLENGTH - 100))
  1496.     {
  1497. #if defined EXTERNDEBUG
  1498.         if (debugflag)
  1499.         {
  1500.             log_line(6,">Free newmsg (Out gate)");
  1501.             log_line(6,">Memory %ld", coreleft());
  1502.         }
  1503. #endif
  1504.         free(newmsg);
  1505.         
  1506.         log_line(5,Logmessage[M__MSG_TO_BIG]);
  1507.         
  1508.         hdr->flags |= SENT;
  1509.         return(FALSE);
  1510.     }
  1511.     
  1512.     strcat(newmsg, msg);
  1513.     
  1514.     time(&secs);
  1515.     tijd = localtime(&secs);
  1516.     strftime(tm_now, 39, "%b %d %Y at %H:%M", tijd);
  1517.     
  1518.     OutMail[0] = 'O';
  1519.     
  1520.     sprintf(&newmsg[strlen(newmsg)], "\01Via %s GateWay %s, %s\n",
  1521.         OutMail, temp, tm_now);
  1522.     
  1523.     strcpy(msg, newmsg);
  1524. #if defined EXTERNDEBUG
  1525.         if (debugflag)
  1526.         {
  1527.             log_line(6,">Free newmsg (Out gate)");
  1528.             log_line(6,">Memory %ld", coreleft());
  1529.         }
  1530. #endif
  1531.     
  1532.     free(newmsg);
  1533.     return(TRUE);
  1534. }
  1535.  
  1536. MLOCAL VOID prepare_poll(VOID)
  1537. {
  1538.     WORD    i, x;
  1539.     BYTE    *outdir, *p, SEND_PKT = FALSE,
  1540.             domain[10];
  1541.     UWORD    zone, net, node, point,
  1542.             j;
  1543.     
  1544.     for (i=0; i < nRouteDef; i++)
  1545.     {
  1546.         if (RouteDef[i].flags & POLLNODE)
  1547.         {
  1548.             address(RouteDef[i].destnode, &zone, &net, &node, &point, &j, domain);
  1549.             outdir = outbound_dir(zone, net, node, point);
  1550.             if ((p = strrchr(outdir, '\\')) == NULL) {
  1551.                 log_line(6,Logmessage[M__CANT_CREATE_POLL]);
  1552.                 return;
  1553.             }
  1554.             
  1555.             for (x = 0;x < nkey; x++) {
  1556.                 if (pwd[x].zone == zone && pwd[x].net == net &&
  1557.                     pwd[x].node == node && pwd[x].point==point) {
  1558.                     SEND_PKT = pwd[x]. Send_PKT == TRUE;
  1559.                     break;
  1560.                 }
  1561.             }
  1562.             
  1563.             if (dothebox) {
  1564.                 xsprintf(++p, "%02z%03z%03z.NF",zone,net,node);
  1565.             } else if (donewbink == FALSE) {
  1566.                 sprintf(++p, "%04x%04x.FLO",net,node);
  1567.             } else {
  1568.                 if(dosentpkt || SEND_PKT == TRUE) {
  1569.                     create_pollPKT(outdir, zone, net, node, point);
  1570.                     continue;
  1571.                 }
  1572.                 else
  1573.                     strcat(outdir, ".OFT");
  1574.             }
  1575.             
  1576.             if ((FDUMMY = fopen(outdir, "r+")) == NULL)
  1577.                 if ((FDUMMY = fopen(outdir, "w")) == NULL)
  1578.                     log_line(6,Logmessage[M__CANT_CREATE_POLL_FILE], zone, net, node, point);
  1579.                 else
  1580.                 {
  1581.                     log_line(3,Logmessage[M__CREATED_POLL], zone, net, node, point);
  1582.                     fclose(FDUMMY);
  1583.                 }
  1584.             else fclose(FDUMMY);
  1585.             
  1586.             free(outdir);
  1587.         }
  1588.     }
  1589. }
  1590.  
  1591. MLOCAL VOID create_pollPKT(BYTE *dir, UWORD zone, UWORD net, UWORD node, UWORD point)
  1592. {
  1593.     WORD     i;
  1594.     UWORD    f_zone, f_net, f_node, f_point, f_pnet;
  1595.     BYTE     type = defPKT,
  1596.             fname[128],
  1597.             pktpwd[10];
  1598.     FILE    *fp;
  1599.     
  1600.     for (i=0; i < nkey; i++) {
  1601.         if (zone == pwd[i].zone && net == pwd[i].net &&
  1602.             node == pwd[i].node && point == pwd[i].point) {
  1603.             
  1604.             type = pwd[i].htype;
  1605.             strncpy(pktpwd, pwd[i].pwd, 8);
  1606.             break;
  1607.         }
  1608.     }
  1609.     
  1610.     if (i >= nkey) {
  1611.         pktpwd[0] = EOS;
  1612.     }
  1613.     
  1614.     for (i=0; i <nalias; i++) {
  1615.         if (zone == alias[i].zone) {
  1616.             f_zone = alias[i].zone;
  1617.             f_net  = alias[i].net;
  1618.             f_node = alias[i].net;
  1619.             f_point= alias[i].point;
  1620.             f_pnet = alias[i].pointnet;
  1621.             break;
  1622.         }
  1623.     }
  1624.     
  1625.     if ( i >= nalias) {
  1626.         i = 0;
  1627.         f_zone = alias[i].zone;
  1628.         f_net  = alias[i].net;
  1629.         f_node = alias[i].net;
  1630.         f_point= alias[i].point;
  1631.         f_pnet = alias[i].pointnet;
  1632.     }
  1633.         
  1634.     switch (type)
  1635.     {
  1636.         case FTS_TYPE:
  1637.         
  1638.             ftsp_TYPE:
  1639.             if (f_point)
  1640.                 create_pkt_hdr(f_zone, f_pnet, f_point,
  1641.                      zone, net, node, pktpwd);
  1642.             else
  1643.                 create_pkt_hdr(f_zone, f_net, f_node,
  1644.                      zone, net, node, pktpwd);
  1645.             
  1646.             break;
  1647.             
  1648.         /*
  1649.         **    Create an packetheader for frontdoor.
  1650.         */
  1651.         
  1652.         case TYPE_2_1:
  1653.         case FRONTDOOR:
  1654.         
  1655.             create_pkt_fnd(f_zone, f_net, f_node, f_point,
  1656.                 zone, net, node, point, pktpwd);
  1657.             break;
  1658.  
  1659.         case TYPE_2_2:    
  1660.             create_pkt_2_2(f_zone, f_net, f_node, f_point,
  1661.                 zone, net, node, point, pktpwd, org_domain, dest_domain);
  1662.             break;
  1663.         
  1664.         default:
  1665.         
  1666.             log_line(6,Logmessage[M__COMPILER_ERROR]);
  1667.             goto ftsp_TYPE;
  1668.     }
  1669.  
  1670.     strcpy(fname, dir);
  1671.     strcat(fname, ".OPT");
  1672.     
  1673.     if ((fp = fopen(fname, "r+b")) == NULL) {
  1674.         if ((fp = fopen(fname, "wb")) == NULL) {
  1675.             log_line(6,Logmessage[M__CANT_CREATE_POLL_FILE], zone, net, node, point);
  1676.             return;
  1677.         }
  1678.         fwrite(&phdr, sizeof(struct _pkthdr), 1, fp);
  1679.         putc(0, fp);
  1680.         putc(0, fp);
  1681.     }
  1682.     fclose(fp);
  1683.     log_line(3,Logmessage[M__CREATED_POLL], zone, net, node, point);
  1684. }    
  1685.         
  1686. MLOCAL VOID put_hold(BYTE *name, UWORD usedzone, BYTE *dir, WORD what, BYTE Pkt)
  1687. {
  1688.     WORD    i,
  1689.             ret;
  1690.     UWORD    zone = usedzone,
  1691.             net, node, point = 0;
  1692.     BYTE    buffer[128],
  1693.             buffer1[128],
  1694.             temp[80],
  1695.             *p;
  1696.     LONG    maxsize;
  1697.     
  1698.     strcpy(temp, name);
  1699.     
  1700.     p = strrchr(temp, '.');
  1701.     if (!p) return;
  1702.     *p = EOS;
  1703.     
  1704.     getBTaddress(temp, &net, &node, &point);
  1705.     
  1706.     for (i=0; i < nalias; i++)
  1707.         if (net == alias[i].pointnet)
  1708.         {
  1709.             point = node;
  1710.             net = alias[i].net;
  1711.             node = alias[i].node;
  1712.             break;
  1713.         }
  1714.     
  1715.     if ((ret = hold_pollOk(zone, net, node, point, what)) != -1)
  1716.     {
  1717.         if (what & HOLDPOLL)
  1718.             log_line(4,Logmessage[M__HOLD_TO_POLL], zone, net, node, point);
  1719.         else
  1720.             log_line(4,Logmessage[M__CHECK_SIZE], zone, net, node, point);
  1721.         
  1722.         strcpy(buffer, dir);
  1723.         strcpy(buffer1, dir);
  1724.         strcat(buffer, name);
  1725.         strcat(buffer1, name);
  1726.         
  1727.         if (what & HOLDMAX || what & HOLDDEL)
  1728.         {
  1729.             maxsize = atol(RouteDef[ret].fromnode) * 1024L;
  1730.             
  1731.             if (!findfirst(buffer, &blok, 0))
  1732.             {
  1733.                 if (blok.ff_fsize < maxsize) return;
  1734.                 
  1735.                 if (what & HOLDMAX)
  1736.                 {
  1737.                     if (point)
  1738.                         log_line(6,Logmessage[M__HOLD_TO_POLL_POINT]);
  1739.                     
  1740.                     log_line(4,Logmessage[M__CHANGE_TO_POLL]);
  1741.                 }
  1742.                 else
  1743.                 {
  1744.                     log_line(4,Logmessage[M__DELETE_PACKET], zone, net, node, point);
  1745.                     unlink(buffer);
  1746.                     return;
  1747.                 }
  1748.             }
  1749.         }
  1750.         
  1751.         p = strrchr(buffer, '.');
  1752.         
  1753.         if (!p)
  1754.         {
  1755.             log_line(5,Logmessage[M__UNKNOWN_ERR_CHANGE]);
  1756.             return;
  1757.         }
  1758.         
  1759.         if (Pkt == ARC_PKTS)
  1760.             strcpy(++p, "OAT");
  1761.         else
  1762.             strcpy(++p, "OPT");
  1763.         
  1764.         if (rename(buffer1, buffer))
  1765.             log_line(6,Logmessage[M__CANT_RENAME_POLL], buffer1, buffer);
  1766.     }
  1767. }
  1768.  
  1769. MLOCAL VOID hold_to_poll(WORD what)
  1770. {
  1771.     WORD    i;
  1772.     BYTE    dir[128],
  1773.             *path,
  1774.             *p;
  1775.     
  1776.     for (i=0; i < nRouteDef; i++)
  1777.         if (RouteDef[i].flags & what) break;
  1778.     
  1779.     if (i >= nRouteDef) return;
  1780.     
  1781.     if (what & HOLDDEL)
  1782.         log_line(4,Logmessage[M__CHECK_HOLD]);
  1783.     else
  1784.         log_line(4,Logmessage[M__HOLD_TO_POLL_FILE],
  1785.             (what & HOLDMAX) ? " on maximum size" : "");
  1786.     
  1787.     sprintf(dir, "%s*.HAT", outbound);
  1788.     
  1789.     if (!findfirst(dir, &blok, 0))
  1790.     {
  1791.         put_hold(blok.ff_name, alias[0].zone, outbound, what, ARC_PKTS);
  1792.         
  1793.         while (!findnext(&blok))
  1794.             put_hold(blok.ff_name, alias[0].zone, outbound, what, ARC_PKTS);
  1795.     }
  1796.     
  1797.     sprintf (dir, "%s*.HPT", outbound);
  1798.  
  1799.     if (!findfirst(dir, &blok, 0))
  1800.     {
  1801.         put_hold(blok.ff_name, alias[0].zone, outbound, what, NORM_PKTS);
  1802.         
  1803.         while (!findnext(&blok))
  1804.             put_hold(blok.ff_name, alias[0].zone, outbound, what, NORM_PKTS);
  1805.     }
  1806.     
  1807.     for (i=0; i < domain; i++)
  1808.     {
  1809.         if (Appl[i].zone == alias[0].zone) continue;
  1810.         
  1811.         path = outbound_dir(Appl[i].zone, 0, 0, 0);
  1812.         p = strrchr(path, '\\');
  1813.         
  1814.         if (!p)
  1815.         {
  1816.             free(path);
  1817.             continue;
  1818.         }
  1819.         
  1820.         p++;
  1821.         *p = EOS;
  1822.         
  1823.         sprintf(dir, "%s*.HAT", path);
  1824.         
  1825.         if (!findfirst(dir, &blok, 0))
  1826.         {
  1827.             put_hold(blok.ff_name, Appl[i].zone, path, what, ARC_PKTS);
  1828.             
  1829.             while (!findnext(&blok))
  1830.                 put_hold(blok.ff_name, Appl[i].zone, path, what, ARC_PKTS);
  1831.         }
  1832.         
  1833.         sprintf (dir, "%s*.HPT", path);
  1834.  
  1835.         if (!findfirst(dir, &blok, 0))
  1836.         {
  1837.             put_hold(blok.ff_name, Appl[i].zone, path, what, NORM_PKTS);
  1838.             
  1839.             while (!findnext(&blok))
  1840.                 put_hold(blok.ff_name, Appl[i].zone, path, what, NORM_PKTS);
  1841.         }
  1842.         
  1843.         free(path);
  1844.     }
  1845. }
  1846.  
  1847. BYTE check_hold_poll (BYTE *file, BYTE Pkt) {
  1848.     BYTE    buffer[128];
  1849.     
  1850.     strcpy (buffer, file);
  1851.     
  1852.     if (Pkt == ARC_PKTS) {
  1853.         strcat (buffer, (donewbink == TRUE) ? "OAT" : "NA");
  1854.     } else {
  1855.         strcat (buffer, (donewbink == TRUE) ? "OPT" : "NM");
  1856.     }
  1857.     
  1858.     if (!findfirst (buffer, &blok, 0)) {
  1859.         return (SUCCESS);
  1860.     }
  1861.     return (FAILURE);
  1862. }
  1863.