home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimemult.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  17.1 KB  |  577 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* mimemult.h --- definition of the MimeMultipart class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. #include "mimemult.h"
  24.  
  25. #define MIME_SUPERCLASS mimeContainerClass
  26. MimeDefClass(MimeMultipart, MimeMultipartClass,
  27.              mimeMultipartClass, &MIME_SUPERCLASS);
  28.  
  29. static int MimeMultipart_initialize (MimeObject *);
  30. static void MimeMultipart_finalize (MimeObject *);
  31. static int MimeMultipart_parse_line (char *line, int32 length, MimeObject *);
  32. static int MimeMultipart_parse_eof (MimeObject *object, XP_Bool abort_p);
  33.  
  34. static MimeMultipartBoundaryType MimeMultipart_check_boundary(MimeObject *,
  35.                                                               const char *,
  36.                                                               int32);
  37. static int MimeMultipart_create_child(MimeObject *);
  38. static XP_Bool MimeMultipart_output_child_p(MimeObject *, MimeObject *);
  39. static int MimeMultipart_parse_child_line (MimeObject *, char *, int32,
  40.                                            XP_Bool);
  41. static int MimeMultipart_close_child(MimeObject *);
  42.  
  43. extern MimeObjectClass mimeMultipartAlternativeClass;
  44. extern MimeObjectClass mimeMultipartRelatedClass;
  45. extern MimeObjectClass mimeMultipartSignedClass;
  46.  
  47. #if defined(DEBUG) && defined(XP_UNIX)
  48. static int MimeMultipart_debug_print (MimeObject *, FILE *, int32);
  49. #endif
  50.  
  51. static int
  52. MimeMultipartClassInitialize(MimeMultipartClass *class)
  53. {
  54.   MimeObjectClass    *oclass = (MimeObjectClass *)    class;
  55.   MimeMultipartClass *mclass = (MimeMultipartClass *) class;
  56.  
  57.   XP_ASSERT(!oclass->class_initialized);
  58.   oclass->initialize  = MimeMultipart_initialize;
  59.   oclass->finalize    = MimeMultipart_finalize;
  60.   oclass->parse_line  = MimeMultipart_parse_line;
  61.   oclass->parse_eof   = MimeMultipart_parse_eof;
  62.  
  63.   mclass->check_boundary   = MimeMultipart_check_boundary;
  64.   mclass->create_child     = MimeMultipart_create_child;
  65.   mclass->output_child_p   = MimeMultipart_output_child_p;
  66.   mclass->parse_child_line = MimeMultipart_parse_child_line;
  67.   mclass->close_child      = MimeMultipart_close_child;
  68.  
  69. #if defined(DEBUG) && defined(XP_UNIX)
  70.   oclass->debug_print = MimeMultipart_debug_print;
  71. #endif
  72.  
  73.   return 0;
  74. }
  75.  
  76.  
  77. static int
  78. MimeMultipart_initialize (MimeObject *object)
  79. {
  80.   MimeMultipart *mult = (MimeMultipart *) object;
  81.   char *ct;
  82.  
  83.   /* This is an abstract class; it shouldn't be directly instanciated. */
  84.   XP_ASSERT(object->class != (MimeObjectClass *) &mimeMultipartClass);
  85.  
  86.   ct = MimeHeaders_get (object->headers, HEADER_CONTENT_TYPE, FALSE, FALSE);
  87.   mult->boundary = (ct
  88.                     ? MimeHeaders_get_parameter (ct, HEADER_PARM_BOUNDARY)
  89.                     : 0);
  90.   FREEIF(ct);
  91.   mult->state = MimeMultipartPreamble;
  92.   return ((MimeObjectClass*)&MIME_SUPERCLASS)->initialize(object);
  93. }
  94.  
  95.  
  96. static void
  97. MimeMultipart_finalize (MimeObject *object)
  98. {
  99.   MimeMultipart *mult = (MimeMultipart *) object;
  100.  
  101.   object->class->parse_eof(object, FALSE);
  102.  
  103.   FREEIF(mult->boundary);
  104.   if (mult->hdrs)
  105.     MimeHeaders_free(mult->hdrs);
  106.   mult->hdrs = 0;
  107.   ((MimeObjectClass*)&MIME_SUPERCLASS)->finalize(object);
  108. }
  109.  
  110.  
  111. static int
  112. MimeMultipart_parse_line (char *line, int32 length, MimeObject *obj)
  113. {
  114.   MimeMultipart *mult = (MimeMultipart *) obj;
  115.   int status = 0;
  116.   MimeMultipartBoundaryType boundary;
  117.  
  118.   XP_ASSERT(line && *line);
  119.   if (!line || !*line) return -1;
  120.  
  121.   XP_ASSERT(!obj->closed_p);
  122.   if (obj->closed_p) return -1;
  123.  
  124.   /* If we're supposed to write this object, but aren't supposed to convert
  125.      it to HTML, simply pass it through unaltered. */
  126.   if (obj->output_p &&
  127.       obj->options &&
  128.       !obj->options->write_html_p &&
  129.       obj->options->output_fn)
  130.     return MimeObject_write(obj, line, length, TRUE);
  131.  
  132.  
  133.   if (mult->state == MimeMultipartEpilogue)  /* already done */
  134.     boundary = MimeMultipartBoundaryTypeNone;
  135.   else
  136.     boundary = ((MimeMultipartClass *)obj->class)->check_boundary(obj, line,
  137.                                                                   length);
  138.  
  139.   if (boundary == MimeMultipartBoundaryTypeTerminator ||
  140.       boundary == MimeMultipartBoundaryTypeSeparator)
  141.     {
  142.       /* Match!  Close the currently-open part, move on to the next
  143.          state, and discard this line.
  144.        */
  145.       if (mult->state != MimeMultipartPreamble)
  146.         status = ((MimeMultipartClass *)obj->class)->close_child(obj);
  147.       if (status < 0) return status;
  148.  
  149.       if (boundary == MimeMultipartBoundaryTypeTerminator)
  150.         mult->state = MimeMultipartEpilogue;
  151.       else
  152.         {
  153.           mult->state = MimeMultipartHeaders;
  154.  
  155.           /* Reset the header parser for this upcoming part. */
  156.           XP_ASSERT(!mult->hdrs);
  157.           if (mult->hdrs)
  158.             MimeHeaders_free(mult->hdrs);
  159.           mult->hdrs = MimeHeaders_new();
  160.           if (!mult->hdrs)
  161.             return MK_OUT_OF_MEMORY;
  162.         }
  163.  
  164.       /* Now return, to ignore the boundary line itself. */
  165.       return 0;
  166.     }
  167.  
  168.   /* Otherwise, this isn't a boundary string.  So do whatever it is we
  169.      should do with this line (parse it as a header, feed it to the
  170.      child part, ignore it, etc.) */
  171.  
  172.   switch (mult->state)
  173.     {
  174.     case MimeMultipartPreamble:
  175.     case MimeMultipartEpilogue:
  176.       /* Ignore this line. */
  177.       break;
  178.  
  179.     case MimeMultipartHeaders:
  180.       /* Parse this line as a header for the sub-part. */
  181.       {
  182.         status = MimeHeaders_parse_line(line, length, mult->hdrs);
  183.         if (status < 0) return status;
  184.  
  185.         /* If this line is blank, we're now done parsing headers, and should
  186.            now examine the content-type to create this "body" part.
  187.          */
  188.         if (*line == CR || *line == LF)
  189.           {
  190.             status = ((MimeMultipartClass *) obj->class)->create_child(obj);
  191.             if (status < 0) return status;
  192.             XP_ASSERT(mult->state != MimeMultipartHeaders);
  193.           }
  194.         break;
  195.       }
  196.  
  197.     case MimeMultipartPartFirstLine:
  198.       /* Hand this line off to the sub-part. */
  199.       status = (((MimeMultipartClass *) obj->class)->parse_child_line(obj,
  200.                                                                       line,
  201.                                                                       length,
  202.                                                                       TRUE));
  203.       if (status < 0) return status;
  204.       mult->state = MimeMultipartPartLine;
  205.       break;
  206.  
  207.     case MimeMultipartPartLine:
  208.       /* Hand this line off to the sub-part. */
  209.       status = (((MimeMultipartClass *) obj->class)->parse_child_line(obj,
  210.                                                                       line,
  211.                                                                       length,
  212.                                                                       FALSE));
  213.       if (status < 0) return status;
  214.       break;
  215.  
  216.     default:
  217.       XP_ASSERT(0);
  218.       return -1;
  219.     }
  220.  
  221.   return 0;
  222. }
  223.  
  224.  
  225. static MimeMultipartBoundaryType
  226. MimeMultipart_check_boundary(MimeObject *obj, const char *line, int32 length)
  227. {
  228.   MimeMultipart *mult = (MimeMultipart *) obj;
  229.   int32 blen;
  230.   XP_Bool term_p;
  231.  
  232.   if (!mult->boundary ||
  233.       line[0] != '-' ||
  234.       line[1] != '-')
  235.     return MimeMultipartBoundaryTypeNone;
  236.  
  237.   /* This is a candidate line to be a boundary.  Check it out... */
  238.   blen = XP_STRLEN(mult->boundary);
  239.   term_p = FALSE;
  240.  
  241.   /* strip trailing whitespace (including the newline.) */
  242.   while(length > 2 && XP_IS_SPACE(line[length-1]))
  243.     length--;
  244.  
  245.   /* Could this be a terminating boundary? */
  246.   if (length == blen + 4 &&
  247.       line[length-1] == '-' &&
  248.       line[length-2] == '-')
  249.     {
  250.       term_p = TRUE;
  251.       length -= 2;
  252.     }
  253.  
  254.   if (blen == length-2 && !XP_STRNCMP(line+2, mult->boundary, length-2))
  255.     return (term_p
  256.             ? MimeMultipartBoundaryTypeTerminator
  257.             : MimeMultipartBoundaryTypeSeparator);
  258.   else
  259.     return MimeMultipartBoundaryTypeNone;
  260. }
  261.  
  262.  
  263.  
  264. static int
  265. MimeMultipart_create_child(MimeObject *obj)
  266. {
  267.   MimeMultipart *mult = (MimeMultipart *) obj;
  268. #ifdef JS_ATTACHMENT_MUMBO_JUMBO
  269.   MimeContainer *cont = (MimeContainer *) obj;
  270. #endif
  271.   int status;
  272.   char *ct = (mult->hdrs
  273.               ? MimeHeaders_get (mult->hdrs, HEADER_CONTENT_TYPE,
  274.                                  TRUE, FALSE)
  275.               : 0);
  276.   const char *dct = (((MimeMultipartClass *) obj->class)->default_part_type);
  277.   MimeObject *body = NULL;
  278.   MimeObject *parent = NULL;
  279.   XP_Bool showIcon = TRUE;
  280.  
  281.   mult->state = MimeMultipartPartFirstLine;
  282.   /* Don't pass in NULL as the content-type (this means that the
  283.      auto-uudecode-hack won't ever be done for subparts of a
  284.      multipart, but only for untyped children of message/rfc822.
  285.    */
  286.   body = mime_create(((ct && *ct) ? ct : (dct ? dct: TEXT_PLAIN)),
  287.                      mult->hdrs, obj->options);
  288.   FREEIF(ct);
  289.   if (!body) return MK_OUT_OF_MEMORY;
  290.   status = ((MimeContainerClass *) obj->class)->add_child(obj, body);
  291.   if (status < 0)
  292.     {
  293.       mime_free(body);
  294.       return status;
  295.     }
  296.  
  297. #ifdef MIME_DRAFTS
  298.   if ( obj->options && 
  299.        obj->options->decompose_file_p &&
  300.        obj->options->is_multipart_msg &&
  301.        obj->options->decompose_file_init_fn )
  302.     {
  303.       if ( !mime_typep(obj,(MimeObjectClass*)&mimeMultipartRelatedClass) &&
  304.            !mime_typep(obj,(MimeObjectClass*)&mimeMultipartAlternativeClass) &&
  305.            !mime_typep(obj,(MimeObjectClass*)&mimeMultipartSignedClass) &&
  306.            !mime_typep(body, (MimeObjectClass*)&mimeMultipartRelatedClass) &&
  307.            !mime_typep(body, (MimeObjectClass*)&mimeMultipartAlternativeClass) &&
  308.            !mime_typep(body,(MimeObjectClass*)&mimeMultipartSignedClass) )
  309.       {
  310.         status = obj->options->decompose_file_init_fn ( obj->options->stream_closure, mult->hdrs );
  311.         if (status < 0) return status;
  312.       }
  313.     }
  314. #endif /* MIME_DRAFTS */
  315.  
  316.  
  317.   /* Now that we've added this new object to our list of children,
  318.      start its parser going (if we want to display it.)
  319.    */
  320.   body->output_p = (((MimeMultipartClass *) obj->class)
  321.                     ->output_child_p(obj, body));
  322.   if (body->output_p)
  323.     {
  324.       status = body->class->parse_begin(body);
  325.       if (status < 0) return status;
  326. #ifdef JS_ATTACHMENT_MUMBO_JUMBO
  327.       if (cont->nchildren > 1 &&
  328.           obj->options && !obj->options->nice_html_only_p &&
  329.           obj->options->attachment_icon_layer_id) {
  330.         /* This is not the first child, so it's an attachment.  Cause the
  331.            "attachments in this message" icon(s) to become visible.
  332.            Excluding the following types to avoid inline graphics and dull items :
  333.            Headers: Content-Disposition: inline
  334.            Content-Type: text/x-vcard
  335.            Content-Type: text/html
  336.            Content-Type: text/plain
  337.            Content-Type: message/rfc822    */
  338.           char *tmp = NULL;
  339.  
  340.           /* if (strncasestr(body->headers->all_headers, "DISPOSITION: INLINE", 300))
  341.               showIcon = FALSE; */
  342.           if (XP_STRSTR(body->content_type, "text/x-vcard"))
  343.               showIcon = FALSE;
  344.           else if (XP_STRSTR(body->content_type, "text/html"))
  345.               showIcon = FALSE;
  346.           else if (XP_STRSTR(body->content_type, "message/rfc822"))
  347.               showIcon = FALSE;
  348.  
  349.           if (showIcon)
  350.           {
  351.               (obj->class)->showAttachmentIcon = TRUE;
  352.               parent = obj->parent;
  353.               while (parent) {
  354.                   (parent->class)->showAttachmentIcon = TRUE;
  355.                   parent = parent->parent;
  356.               }
  357.               tmp = PR_smprintf("\n\
  358. <SCRIPT>\n\
  359. window.document.layers[\"noattach-%ld\"].visibility = \"hide\";\n\
  360. window.document.layers[\"attach-%ld\"].visibility = \"show\";\n\
  361. </SCRIPT>\n",
  362.                                 (long) obj->options->attachment_icon_layer_id,
  363.                                 (long) obj->options->attachment_icon_layer_id);
  364.           }
  365.           if (tmp) {
  366.             status = MimeObject_write(obj, tmp, XP_STRLEN(tmp), TRUE);
  367.             XP_FREE(tmp);
  368.             if (status < 0)
  369.                 return status;
  370.           }
  371.       }
  372. #endif /* JS_ATTACHMENT_MUMBO_JUMBO */
  373.     }
  374.  
  375.   return 0;
  376. }
  377.  
  378.  
  379. static XP_Bool
  380. MimeMultipart_output_child_p(MimeObject *obj, MimeObject *child)
  381. {
  382.   return TRUE;
  383. }
  384.  
  385.  
  386.  
  387. static int
  388. MimeMultipart_close_child(MimeObject *object)
  389. {
  390.   MimeMultipart *mult = (MimeMultipart *) object;
  391.   MimeContainer *cont = (MimeContainer *) object;
  392.  
  393.   if (!mult->hdrs)
  394.     return 0;
  395.  
  396.   MimeHeaders_free(mult->hdrs);
  397.   mult->hdrs = 0;
  398.  
  399.   XP_ASSERT(cont->nchildren > 0);
  400.   if (cont->nchildren > 0)
  401.     {
  402.       MimeObject *kid = cont->children[cont->nchildren-1];
  403.       if (kid)
  404.         {
  405.           int status;
  406.           status = kid->class->parse_eof(kid, FALSE);
  407.           if (status < 0) return status;
  408.           status = kid->class->parse_end(kid, FALSE);
  409.           if (status < 0) return status;
  410.  
  411. #ifdef MIME_DRAFTS
  412.           if ( object->options &&
  413.                object->options->decompose_file_p &&
  414.                object->options->is_multipart_msg &&
  415.                object->options->decompose_file_close_fn ) 
  416.           {
  417.               if ( !mime_typep(object,(MimeObjectClass*)&mimeMultipartRelatedClass) &&
  418.                    !mime_typep(object,(MimeObjectClass*)&mimeMultipartAlternativeClass) &&
  419.                    !mime_typep(kid,(MimeObjectClass*)&mimeMultipartRelatedClass) &&
  420.                    !mime_typep(kid,(MimeObjectClass*)&mimeMultipartAlternativeClass) &&
  421.                    !mime_typep(object,(MimeObjectClass*)&mimeMultipartSignedClass) &&
  422.                    !mime_typep(kid,(MimeObjectClass*)&mimeMultipartSignedClass) )
  423.                 {
  424.                     status = object->options->decompose_file_close_fn ( object->options->stream_closure );
  425.                     if (status < 0) return status;
  426.                 }
  427.           }
  428. #endif /* MIME_DRAFTS */
  429.  
  430.         }
  431.     }
  432.   return 0;
  433. }
  434.  
  435.  
  436. static int
  437. MimeMultipart_parse_child_line (MimeObject *obj, char *line, int32 length,
  438.                                 XP_Bool first_line_p)
  439. {
  440.   MimeContainer *cont = (MimeContainer *) obj;
  441.   int status;
  442.   MimeObject *kid;
  443.  
  444.   XP_ASSERT(cont->nchildren > 0);
  445.   if (cont->nchildren <= 0)
  446.     return -1;
  447.  
  448.   kid = cont->children[cont->nchildren-1];
  449.   XP_ASSERT(kid);
  450.   if (!kid) return -1;
  451.  
  452. #ifdef MIME_DRAFTS
  453.   if ( obj->options &&
  454.        obj->options->decompose_file_p &&
  455.        obj->options->is_multipart_msg && 
  456.        obj->options->decompose_file_output_fn ) 
  457.   {
  458.     if (!mime_typep(obj,(MimeObjectClass*)&mimeMultipartAlternativeClass) &&
  459.         !mime_typep(obj,(MimeObjectClass*)&mimeMultipartRelatedClass) &&
  460.         !mime_typep(obj,(MimeObjectClass*)&mimeMultipartSignedClass) &&
  461.         !mime_typep(kid,(MimeObjectClass*)&mimeMultipartAlternativeClass) &&
  462.         !mime_typep(kid,(MimeObjectClass*)&mimeMultipartRelatedClass) &&
  463.         !mime_typep(kid,(MimeObjectClass*)&mimeMultipartSignedClass) )
  464.         return obj->options->decompose_file_output_fn (line, length, obj->options->stream_closure);
  465.   }
  466. #endif /* MIME_DRAFTS */
  467.  
  468.   /* The newline issues here are tricky, since both the newlines before
  469.      and after the boundary string are to be considered part of the
  470.      boundary: this is so that a part can be specified such that it
  471.      does not end in a trailing newline.
  472.  
  473.      To implement this, we send a newline *before* each line instead
  474.      of after, except for the first line, which is not preceeded by a
  475.      newline.
  476.    */
  477.  
  478.   /* Remove the trailing newline... */
  479.   if (length > 0 && line[length-1] == LF) length--;
  480.   if (length > 0 && line[length-1] == CR) length--;
  481.  
  482.   if (!first_line_p)
  483.     {
  484.       /* Push out a preceeding newline... */
  485.       char nl[] = LINEBREAK;
  486.       status = kid->class->parse_buffer (nl, LINEBREAK_LEN, kid);
  487.       if (status < 0) return status;
  488.     }
  489.  
  490.   /* Now push out the line sans trailing newline. */
  491.   return kid->class->parse_buffer (line, length, kid);
  492. }
  493.  
  494.  
  495. static int
  496. MimeMultipart_parse_eof (MimeObject *obj, XP_Bool abort_p)
  497. {
  498.   MimeMultipart *mult = (MimeMultipart *) obj;
  499.   MimeContainer *cont = (MimeContainer *) obj;
  500.  
  501.   if (obj->closed_p) return 0;
  502.  
  503.   /* Push out one last newline if part of the last line is still in the
  504.      ibuffer.  If this happens, this object does not end in a trailing newline
  505.      (and the parse_line method will be called with a string with no trailing
  506.      newline, which isn't the usual case.)
  507.    */
  508.   if (!abort_p && obj->ibuffer_fp > 0)
  509.     {
  510.       int status = obj->class->parse_buffer (obj->ibuffer, obj->ibuffer_fp,
  511.                                              obj);
  512.       obj->ibuffer_fp = 0;
  513.       if (status < 0)
  514.         {
  515.           obj->closed_p = TRUE;
  516.           return status;
  517.         }
  518.     }
  519.  
  520.   /* Now call parse_eof for our active child, if there is one.
  521.    */
  522.   if (cont->nchildren > 0 &&
  523.       (mult->state == MimeMultipartPartLine ||
  524.        mult->state == MimeMultipartPartFirstLine))
  525.     {
  526.       MimeObject *kid = cont->children[cont->nchildren-1];
  527.       XP_ASSERT(kid);
  528.       if (kid)
  529.         {
  530.           int status = kid->class->parse_eof(kid, abort_p);
  531.           if (status < 0) return status;
  532.         }
  533.     }
  534.  
  535.   return ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_eof(obj, abort_p);
  536. }
  537.  
  538.  
  539. #if defined(DEBUG) && defined(XP_UNIX)
  540. static int
  541. MimeMultipart_debug_print (MimeObject *obj, FILE *stream, int32 depth)
  542. {
  543.   MimeMultipart *mult = (MimeMultipart *) obj;
  544.   MimeContainer *cont = (MimeContainer *) obj;
  545.   char *addr = mime_part_address(obj);
  546.   int i;
  547.   for (i=0; i < depth; i++)
  548.     fprintf(stream, "  ");
  549.   fprintf(stream, "<%s %s (%d kid%s) boundary=%s 0x%08X>\n",
  550.           obj->class->class_name,
  551.           addr ? addr : "???",
  552.           cont->nchildren, (cont->nchildren == 1 ? "" : "s"),
  553.           (mult->boundary ? mult->boundary : "(none)"),
  554.           (uint32) mult);
  555.   FREEIF(addr);
  556.  
  557. /*
  558.   if (cont->nchildren > 0)
  559.     fprintf(stream, "\n");
  560.  */
  561.  
  562.   for (i = 0; i < cont->nchildren; i++)
  563.     {
  564.       MimeObject *kid = cont->children[i];
  565.       int status = kid->class->debug_print (kid, stream, depth+1);
  566.       if (status < 0) return status;
  567.     }
  568.  
  569. /*
  570.   if (cont->nchildren > 0)
  571.     fprintf(stream, "\n");
  572.  */
  573.  
  574.   return 0;
  575. }
  576. #endif
  577.