home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / io / wr_q2drfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  5.3 KB  |  245 lines

  1. /* wr_q2drfile.c: write out a Delivery Notification struct 
  2.     from the info held in the queue and address structures
  3. */
  4.  
  5. # ifndef lint
  6. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/io/RCS/wr_q2drfile.c,v 6.0 1991/12/18 20:22:26 jpo Rel $";
  7. # endif
  8.  
  9. /*
  10.  * $Header: /xtel/pp/pp-beta/Lib/io/RCS/wr_q2drfile.c,v 6.0 1991/12/18 20:22:26 jpo Rel $
  11.  *
  12.  * $Log: wr_q2drfile.c,v $
  13.  * Revision 6.0  1991/12/18  20:22:26  jpo
  14.  * Release 6.0
  15.  *
  16.  */
  17.  
  18.  
  19.  
  20. #include    "head.h"
  21. #include    "q.h"
  22. #include    "dr.h"
  23. #include    "sys.file.h"
  24. #include     <sys/stat.h>
  25. #include    "list_bpt.h"
  26. #include    <sys/time.h>
  27. #include    "or.h"
  28.  
  29. extern char    *dr_file;
  30. extern char    *x400_mta;
  31. static void set_succ_suppl ();
  32. static int wr_dr_info ();
  33.  
  34. /* ---------------------  Begin     Routines  -------------------------------- */
  35.  
  36. int wr_q2dr (qp, msg_id)
  37. register Q_struct    *qp;
  38. char    *msg_id;
  39. {
  40.     return wr_q2drfile (qp, msg_id, 1);
  41. }
  42.  
  43. int wr_q2drfile (qp, path, update)
  44. register Q_struct    *qp;
  45. char    *path;
  46. int    update;
  47. {
  48.     FILE    *fp;
  49.     ADDR    *ap;
  50.     char     odir[MAXPATHLENGTH];
  51.     int    retval=NOTOK,
  52.         dr_file_no = -1,
  53.         dr_required = FALSE;
  54.     struct stat stbuf;
  55.  
  56.     PP_DBG (("Lib/io/wr_q2drfile (dir=%s)", path));
  57.  
  58.     for (ap=qp->Raddress; ap != NULLADDR; ap=ap->ad_next) {
  59.         if (ap->ad_status != AD_STAT_DRREQUIRED)
  60.             continue;
  61.  
  62.         dr_required = TRUE;
  63.  
  64.         dr_file_no = ap->ad_no;
  65.  
  66.         (void) sprintf (odir, "%s/%s%d", path,
  67.                 dr_file, dr_file_no);
  68.  
  69.         PP_DBG(("ap->ad_no = %d file = %s",ap->ad_no, odir));
  70.  
  71.         if (stat (odir, &stbuf) == NOTOK)
  72.             break;
  73.  
  74.  
  75.         if (stbuf.st_size == 0) {
  76.             PP_NOTICE (("Removing empty file %s",
  77.                     odir));
  78.             (void) unlink(odir);
  79.             break;
  80.         }
  81.         PP_OPER (NULLCP,
  82.              ("wr_q2drfile: File %s already exists of size %d",
  83.               odir, stbuf.st_size));
  84.         return RP_LIO;
  85.     }
  86.  
  87.     if (ap == NULLADDR && dr_required == FALSE)
  88.         return (RP_OK);
  89.  
  90.     qp->Oaddress->ad_status = AD_STAT_PEND;
  91.     if (update &&
  92.         rp_isbad(retval = wr_ad_status(qp->Oaddress, qp->Oaddress->ad_status)))
  93.         return retval;
  94.  
  95.     PP_DBG (("Lib/io/wr_q2drfile (opening %s)", odir));
  96.  
  97.     if ((fp = fopen (odir, "w")) == NULLFILE) {
  98.         PP_LOG (LLOG_EXCEPTIONS,
  99.             ("Lib/io/wr_q2drfile (unable to open %s)", odir));
  100.         return (RP_FIO);
  101.     }
  102.  
  103.  
  104.     retval = wr_dr_info (fp, qp, dr_file_no, update);
  105.  
  106.     if (check_close (fp) == NOTOK) {
  107.         PP_SLOG (LLOG_EXCEPTIONS, odir, ("Error writing to file"));
  108.         (void) unlink (odir);
  109.         return RP_FIO;
  110.     }
  111.  
  112.     if (chmod(odir, 0666) == NOTOK)
  113.         PP_SLOG(LLOG_EXCEPTIONS, odir, ("chmod failed"));
  114.  
  115.     return (retval);
  116. }
  117.  
  118.  
  119.  
  120.  
  121. static int wr_dr_info (fp, qp, dr_file_no, update) /* Creates a DR  output */
  122. FILE            *fp;
  123. register Q_struct    *qp;
  124. int            dr_file_no;
  125. int            update;
  126. {
  127.     ADDR        *ap;
  128.     time_t        time();
  129.     int        retval;
  130.     DRmpdu        DeliveryReport, *dr;
  131.     Rrinfo        *nxt, *new;
  132.     Report        *rep;
  133.     char        buf[LINESIZE];
  134.  
  135.  
  136.     PP_DBG (("Lib/io/wr_dr_info()"));
  137.  
  138.     /* -- Initialize -- */
  139.     dr_init (dr = &DeliveryReport);
  140.  
  141.     or_myinit();   /* in case */
  142.  
  143.     /* -- set MPDUid -- */
  144.     dr -> dr_mpduid = (MPDUid *) smalloc (sizeof (*dr->dr_mpduid));
  145.     bzero ((char*) dr->dr_mpduid, sizeof (*dr->dr_mpduid));
  146.     MPDUid_new (dr -> dr_mpduid);
  147.  
  148.     /* -- set Trace -- */
  149.     trace_add (&dr -> dr_trace, trace_new());
  150.     trace_add(&dr -> dr_subject_intermediate_trace, trace_dup(qp -> trace));
  151.     for (ap = qp->Raddress; ap != NULLADDR; ap = ap->ad_next)
  152.        if (ap->ad_status == AD_STAT_DRREQUIRED) {
  153.  
  154.            if (ap -> ad_no == dr_file_no)
  155.                ap->ad_status = AD_STAT_DRWRITTEN;
  156.            else    ap -> ad_status = AD_STAT_DONE;
  157.  
  158.            new = (Rrinfo *) smalloc (sizeof (*new));
  159.            bzero ((char *)new, sizeof (*new));
  160.  
  161.            if (dr -> dr_recip_list != NULL) {
  162.                for (nxt = dr->dr_recip_list; nxt -> rr_next;
  163.                 nxt = nxt->rr_next)
  164.                    continue;
  165.                nxt -> rr_next = new;
  166.            }
  167.            else
  168.                dr -> dr_recip_list = new;
  169.  
  170.  
  171.            new -> rr_recip = ap -> ad_no;
  172.            new -> rr_arrival = utcdup (qp -> queuetime);
  173.            rep = &new -> rr_report;
  174.  
  175.            switch (ap->ad_reason) {
  176.                case DRR_NO_REASON:
  177.                rep->rep_type = DR_REP_SUCCESS;
  178.                rep->rep.rep_dinfo.del_time = utcnow();
  179.                rep->rep.rep_dinfo.del_type = 1;
  180.  
  181.                if (ap -> ad_add_info == NULLCP) {
  182.                    set_succ_suppl (ap, qp->msgtype, buf);
  183.                    new -> rr_supplementary = strdup (buf);
  184.                } else
  185.                    new -> rr_supplementary = strdup(ap -> ad_add_info);
  186.                break;
  187.  
  188.                default:
  189.                rep->rep_type = DR_REP_FAILURE;
  190.                rep->rep.rep_ndinfo.nd_rcode = ap->ad_reason;
  191.                rep->rep.rep_ndinfo.nd_dcode = ap->ad_diagnostic;
  192.                if (ap->ad_add_info != NULLCP)
  193.                    new ->rr_supplementary =
  194.                        strdup (ap->ad_add_info);
  195.  
  196.                new -> rr_converted = (EncodedIT *)
  197.                    calloc (1, sizeof *new -> rr_converted);
  198.                new -> rr_converted->eit_types =
  199.                    list_bpt_dup(qp -> encodedinfo.eit_types);
  200.                break;
  201.            }
  202.  
  203.            if (update &&
  204.                rp_isbad (retval = wr_ad_status(ap, ap -> ad_status)))
  205.                return retval;
  206.        }
  207.  
  208.  
  209.     retval = dr2txt (fp, &DeliveryReport);
  210.  
  211.     dr_free (&DeliveryReport);
  212.  
  213.     return (retval == OK ? RP_OK : RP_FIO);
  214. }
  215.  
  216.  
  217.  
  218.  
  219. static void set_succ_suppl (ap, msgtype, msg_buf)
  220. ADDR    *ap;
  221. int    msgtype;
  222. char    *msg_buf;
  223. {
  224.  
  225.     char    *str;
  226.  
  227.  
  228.     if (ap->ad_outchan && ap->ad_outchan->li_chan &&
  229.         ap->ad_outchan->li_chan->ch_access == CH_MTS) 
  230.         str = "Delivered";
  231.     else
  232.         str = "Relayed";
  233.         
  234.     if (msgtype == MT_PMPDU) { /* probe */
  235.         (void) sprintf (msg_buf, 
  236. "%s to '%s' (Probe acknowledgement by PP gateway at '%s')",
  237.                 str, ap->ad_outchan->li_mta, x400_mta);
  238.     }
  239.     else 
  240.         (void) sprintf (msg_buf, "%s to '%s'",
  241.                 str, ap->ad_outchan->li_mta);
  242.  
  243.     return;
  244. }
  245.