home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimei.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  17.3 KB  |  457 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. /* mimei.h --- class definitions for the MIME parser, version 2.
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. #ifndef _MIMEI_H_
  24. #define _MIMEI_H_
  25.  
  26. /*
  27.   This module, libmime, implements a general-purpose MIME parser.
  28.   One of the methods provided by this parser is the ability to emit
  29.   an HTML representation of it.
  30.  
  31.   All Mozilla-specific code is (and should remain) isolated in the
  32.   file mimemoz.c.  Generally, if the code involves images, netlib
  33.   streams, or MWContexts, it should be in mimemoz.c instead of in
  34.   the main body of the MIME parser.
  35.  
  36.   The parser is object-oriented and fully buzzword-compliant.
  37.   There is a class for each MIME type, and each class is responsible
  38.   for parsing itself, and/or handing the input data off to one of its
  39.   child objects.
  40.  
  41.   The class hierarchy is:
  42.  
  43.      MimeObject (abstract)
  44.       |
  45.       |--- MimeContainer (abstract)
  46.       |     |
  47.       |     |--- MimeMultipart (abstract)
  48.       |     |     |
  49.       |     |     |--- MimeMultipartMixed
  50.       |     |     |
  51.       |     |     |--- MimeMultipartDigest
  52.       |     |     |
  53.       |     |     |--- MimeMultipartParallel
  54.       |     |     |
  55.       |     |     |--- MimeMultipartAlternative
  56.       |     |     |
  57.       |     |     |--- MimeMultipartRelated
  58.       |     |     |
  59.       |     |     |--- MimeMultipartAppleDouble
  60.       |     |     |
  61.       |     |     |--- MimeSunAttachment
  62.       |     |     |
  63.       |     |     |--- MimeMultipartSigned (abstract)
  64.       |     |          |
  65.       |     |          |--- 
  66.       |     |
  67.       |     |--- MimeEncrypted (abstract)
  68.       |     |     |
  69.       |     |     |--- 
  70.       |     |
  71.       |     |--- MimeMessage
  72.       |     |
  73.       |     |--- MimeUntypedText
  74.       |
  75.       |--- MimeLeaf (abstract)
  76.       |     |
  77.       |     |--- MimeInlineText (abstract)
  78.       |     |     |
  79.       |     |     |--- MimeInlineTextPlain
  80.       |     |     |
  81.       |     |     |--- MimeInlineTextHTML
  82.       |     |     |
  83.       |     |     |--- MimeInlineTextRichtext
  84.       |     |     |     |
  85.       |     |     |     |--- MimeInlineTextEnriched
  86.       |     |      |
  87.       |     |      |--- MimeInlineTextVCard
  88.       |     |
  89.       |     |--- MimeInlineImage
  90.       |     |
  91.       |     |--- MimeExternalObject
  92.       |
  93.       |--- MimeExternalBody
  94.  
  95.   =========================================================================
  96.   The definition of these classes is somewhat idiosyncratic, since I defined
  97.   my own small object system, instead of giving the C++ virus another foothold.
  98.   (I would have liked to have written this in Java, but our runtime isn't
  99.   quite ready for prime time yet.)
  100.  
  101.   There is one header file and one source file for each class (for example,
  102.   the MimeInlineText class is defined in "mimetext.h" and "mimetext.c".)
  103.   Each header file follows the following boiler-plate form:
  104.  
  105.   TYPEDEFS: these come first to avoid circular dependencies.
  106.  
  107.       typedef struct FoobarClass FoobarClass;
  108.       typedef struct Foobar      Foobar;
  109.  
  110.   CLASS DECLARATION:
  111.   Theis structure defines the callback routines and other per-class data
  112.   of the class defined in this module.
  113.  
  114.       struct FoobarClass {
  115.         ParentClass superclass;
  116.         ...any callbacks or class-variables...
  117.       };
  118.  
  119.   CLASS DEFINITION:
  120.   This variable holds an instance of the one-and-only class record; the
  121.   various instances of this class point to this object.  (One interrogates
  122.   the type of an instance by comparing the value of its class pointer with
  123.   the address of this variable.)
  124.  
  125.       extern FoobarClass foobarClass;
  126.  
  127.   INSTANCE DECLARATION:
  128.   Theis structure defines the per-instance data of an object, and a pointer
  129.   to the corresponding class record.
  130.  
  131.       struct Foobar {
  132.         Parent parent;
  133.         ...any instance variables...
  134.       };
  135.  
  136.   Then, in the corresponding .c file, the following structure is used:
  137.  
  138.   CLASS DEFINITION:
  139.   First we pull in the appropriate include file (which includes all necessary
  140.   include files for the parent classes) and then we define the class object
  141.   using the MimeDefClass macro:
  142.  
  143.       #include "foobar.h"
  144.       #define MIME_SUPERCLASS parentlClass
  145.       MimeDefClass(Foobar, FoobarClass, foobarClass, &MIME_SUPERCLASS);
  146.  
  147.   The definition of MIME_SUPERCLASS is just to move most of the knowlege of the
  148.   exact class hierarchy up to the file's header, instead of it being scattered
  149.   through the various methods; see below.
  150.  
  151.   METHOD DECLARATIONS:
  152.   We will be putting function pointers into the class object, so we declare
  153.   them here.  They can generally all be static, since nobody outside of this
  154.   file needs to reference them by name; all references to these routines should
  155.   be through the class object.
  156.  
  157.       extern int FoobarMethod(Foobar *);
  158.       ...etc...
  159.  
  160.   CLASS INITIALIZATION FUNCTION:
  161.   The MimeDefClass macro expects us to define a function which will finish up
  162.   any initialization of the class object that needs to happen before the first
  163.   time it is instantiated.  Its name must be of the form "<class>Initialize",
  164.   and it should initialize the various method slots in the class as
  165.   appropriate.  Any methods or class variables which this class does not wish
  166.   to override will be automatically inherited from the parent class (by virtue
  167.   of its class-initialization function having been run first.)  Each class
  168.   object will only be initialized once.
  169.  
  170.       static int
  171.       FoobarClassInitialize(FoobarClass *class)
  172.       {
  173.         class->method = FoobarMethod.
  174.         ...etc...
  175.       }
  176.  
  177.   METHOD DEFINITIONS:
  178.   Next come the definitions of the methods we referred to in the class-init
  179.   function.  The way to access earlier methods (methods defined on the
  180.   superclass) is to simply extract them from the superclass's object.
  181.   But note that you CANNOT get at methods by indirecting through
  182.   object->class->superclass: that will only work to one level, and will
  183.   go into a loop if some subclass tries to continue on this method.
  184.  
  185.   The easiest way to do this is to make use of the MIME_SUPERCLASS macro that
  186.   was defined at the top of the file, as shown below.  The alternative to that
  187.   involves typing the literal name of the direct superclass of the class
  188.   defined in this file, which will be a maintenance headache if the class
  189.   hierarchy changes.  If you use the MIME_SUPERCLASS idiom, then a textual
  190.   change is required in only one place if this class's superclass changes.
  191.  
  192.       static void
  193.       Foobar_finalize (MimeObject *object)
  194.       {
  195.         ((MimeObjectClass*)&MIME_SUPERCLASS)->finalize(object);  //  RIGHT
  196.         parentClass.whatnot.object.finalize(object);             //  (works...)
  197.         object->class->superclass->finalize(object);             //  WRONG!!
  198.       }
  199.  */
  200.  
  201. #include "libmime.h"
  202. #include "mimehdrs.h"
  203.  
  204. typedef struct MimeObject      MimeObject;
  205. typedef struct MimeObjectClass MimeObjectClass;
  206.  
  207. #ifdef XP_WIN16
  208.  /* Those winners who brought us the Win16 compiler seemed to be under
  209.     the impression that C is a case-insensitive language.  How very.
  210.   */
  211. # define mimeObject                  mimeObject16
  212. # define mimeContainer              mimeContainer16
  213. # define mimeMultipart              mimeMultipart16
  214. # define mimeMultipartMixed          mimeMultipartMixed16
  215. # define mimeMultipartDigest      mimeMultipartDigest16
  216. # define mimeMultipartParallel      mimeMultipartParallel16
  217. # define mimeMultipartAlternative mimeMultipartAlternative16
  218. # define mimeMultipartRelated      mimeMultipartRelated16
  219. # define mimeMultipartAppleDouble mimeMultipartAppleDouble16
  220. # define mimeSunAttachment          mimeSunAttachment16
  221. # define mimeMultipartSigned      mimeMultipartSigned16
  222. # define mimeMultipartSignedPKCS7 mimeMultipartSignedPKCS716
  223. # define mimeEncrypted              mimeEncrypted16
  224. # define mimeEncryptedPKCS7          mimeEncryptedPKCS716
  225. # define mimeMessage              mimeMessage16
  226. # define mimeUntypedText          mimeUntypedText16
  227. # define mimeLeaf                  mimeLeaf16
  228. # define mimeInlineText              mimeInlineText16
  229. # define mimeInlineTextPlain      mimeInlineTextPlain16
  230. # define mimeInlineTextHTML          mimeInlineTextHTML16
  231. # define mimeInlineTextRichtext      mimeInlineTextRichtext16
  232. # define mimeInlineTextEnriched      mimeInlineTextEnriched16
  233. # define mimeInlineTextVCard      mimeInlineTextVCard16
  234. # define mimeInlineImage          mimeInlineImage16
  235. # define mimeExternalObject          mimeExternalObject16
  236. # define mimeExternalBody          mimeExternalBody16
  237. #endif /* XP_WIN16 */
  238.  
  239.  
  240. /* (I don't pretend to understand this.) */
  241. #define cpp_stringify_noop_helper(x)#x
  242. #define cpp_stringify(x) cpp_stringify_noop_helper(x)
  243.  
  244.  
  245. /* Macro used for setting up class definitions.
  246.  */
  247. #define MimeDefClass(ITYPE,CTYPE,CVAR,CSUPER) \
  248.  static int CTYPE##Initialize(CTYPE *); \
  249.  CTYPE CVAR = { cpp_stringify(ITYPE), sizeof(ITYPE), \
  250.                 (MimeObjectClass *) CSUPER, \
  251.                 (int (*) (MimeObjectClass *)) CTYPE##Initialize, 0, }
  252.  
  253.  
  254. /* Creates a new (subclass of) MimeObject of the given class, with the
  255.    given headers (which are copied.)
  256.  */
  257. extern MimeObject *mime_new (MimeObjectClass *class, MimeHeaders *hdrs,
  258.                              const char *override_content_type);
  259.  
  260.  
  261. /* Destroys a MimeObject (or subclass) and all data associated with it.
  262.  */
  263. extern void mime_free (MimeObject *object);
  264.  
  265. /* Given a content-type string, finds and returns an appropriate subclass
  266.    of MimeObject.  A class object is returned.  If `exact_match_p' is true,
  267.    then only fully-known types will be returned; that is, if it is true,
  268.    then "text/x-unknown" will return MimeInlineTextPlainType, but if it is
  269.    false, it will return NULL.
  270.  */
  271. extern MimeObjectClass *mime_find_class (const char *content_type,
  272.                                          MimeHeaders *hdrs,
  273.                                          MimeDisplayOptions *opts,
  274.                                          XP_Bool exact_match_p);
  275.  
  276. /* Given a content-type string, creates and returns an appropriate subclass
  277.    of MimeObject.  The headers (from which the content-type was presumably
  278.    extracted) are copied.
  279.  */
  280. extern MimeObject *mime_create (const char *content_type, MimeHeaders *hdrs,
  281.                                 MimeDisplayOptions *opts);
  282.  
  283.  
  284. /* Querying the type hierarchy */
  285. extern XP_Bool mime_subclass_p(MimeObjectClass *child,
  286.                                MimeObjectClass *parent);
  287. extern XP_Bool mime_typep(MimeObject *obj, MimeObjectClass *class);
  288.  
  289. /* Returns a string describing the location of the part (like "2.5.3").
  290.    This is not a full URL, just a part-number.
  291.  */
  292. extern char *mime_part_address(MimeObject *obj);
  293.  
  294. /* Puts a part-number into a URL.  If append_p is true, then the part number
  295.    is appended to any existing part-number already in that URL; otherwise,
  296.    it replaces it.
  297.  */
  298. extern char *mime_set_url_part(const char *url, char *part, XP_Bool append_p);
  299.  
  300.  
  301. /* Given a part ID, looks through the MimeObject tree for a sub-part whose ID
  302.    number matches, and returns the MimeObject (else NULL.)
  303.    (part is not a URL -- it's of the form "1.3.5".)
  304.  */
  305. extern MimeObject *mime_address_to_part(const char *part, MimeObject *obj);
  306.  
  307.  
  308. /* Given a part ID, looks through the MimeObject tree for a sub-part whose ID
  309.    number matches; if one is found, returns the Content-Name of that part.
  310.    Else returns NULL.  (part is not a URL -- it's of the form "1.3.5".)
  311.  */
  312. extern char *mime_find_suggested_name_of_part(const char *part,
  313.                                               MimeObject *obj);
  314.  
  315. /* Given a part ID, looks through the MimeObject tree for a sub-part whose ID
  316.    number matches; if one is found, returns the Content-Name of that part.
  317.    Else returns NULL.  (part is not a URL -- it's of the form "1.3.5".)
  318.  */
  319. extern char *mime_find_content_type_of_part(const char *part, MimeObject *obj);
  320.  
  321. /* Given a part ID, looks through the MimeObject tree for a sub-part whose ID
  322.    number matches; if one is found, and if it represents a PKCS7-encrypted
  323.    object, returns information about the security status of that object.
  324.  
  325.    `part' is not a URL -- it's of the form "1.3.5" and is interpreted relative
  326.    to the `obj' argument.
  327.  */
  328. extern void mime_find_security_info_of_part(const char *part, MimeObject *obj,
  329.                                       void **pkcs7_encrypt_content_info_return,
  330.                                          void **pkcs7_sign_content_info_return,
  331.                                             char **sender_email_addr_return,
  332.                                             int32 *decode_error_return,
  333.                                             int32 *verify_error_return);
  334.  
  335.  
  336. /* Parse the various "?" options off the URL and into the options struct.
  337.  */
  338. extern int mime_parse_url_options(const char *url, MimeDisplayOptions *);
  339.  
  340.  
  341. /* Asks whether the given object is one of the cryptographically signed
  342.    or encrypted objects that we know about.  (MimeMessageClass uses this
  343.    to decide if the headers need to be presented differently.)
  344.  */
  345. extern XP_Bool mime_crypto_object_p(MimeHeaders *, XP_Bool clearsigned_counts);
  346.  
  347. /* Tells whether the given MimeObject is a message which has been encrypted
  348.    or signed.  (Helper for MIME_GetMessageCryptoState()). 
  349.  */
  350. extern void mime_get_crypto_state (MimeObject *obj,
  351.                                    XP_Bool *signed_p, XP_Bool *encrypted_p,
  352.                                    XP_Bool *signed_ok, XP_Bool *encrypted_ok);
  353.  
  354.  
  355. /* Whether the given object has written out the HTML version of its headers
  356.    in such a way that it will have a "crypto stamp" next to the headers.  If
  357.    this is true, then the child must write out its HTML slightly differently
  358.    to take this into account...
  359.  */
  360. extern XP_Bool mime_crypto_stamped_p(MimeObject *obj);
  361.  
  362. /* How the crypto code tells the MimeMessage object what the crypto stamp
  363.    on it says. */
  364. extern void mime_set_crypto_stamp(MimeObject *obj,
  365.                                   XP_Bool signed_p, XP_Bool encrypted_p);
  366.  
  367.  
  368. struct MimeParseStateObject {
  369.  
  370.   MimeObject *root;                /* The outermost parser object. */
  371.  
  372.   XP_Bool separator_queued_p;    /* Whether a separator should be written out
  373.                                    before the next text is written (this lets
  374.                                    us write separators lazily, so that one
  375.                                    doesn't appear at the end, and so that more
  376.                                    than one don't appear in a row.) */
  377.  
  378.   XP_Bool separator_suppressed_p; /* Whether the currently-queued separator
  379.                                    should not be printed; this is a kludge to
  380.                                    prevent seps from being printed just after
  381.                                    a header block... */
  382.  
  383.   XP_Bool first_part_written_p;    /* State used for the `Show Attachments As
  384.                                    Links' kludge. */
  385.  
  386.   XP_Bool post_header_html_run_p; /* Whether we've run the
  387.                                      options->generate_post_header_html_fn */
  388.  
  389.   XP_Bool first_data_written_p;    /* State used for Mozilla lazy-stream-
  390.                                    creation evilness. */
  391.  
  392.   XP_Bool decrypted_p;            /* If options->decrypt_p is true, then this
  393.                                    will be set to indicate whether any
  394.                                    decryption did in fact occur.
  395.                                  */
  396. };
  397.  
  398.  
  399.  
  400. /* Some output-generation utility functions...
  401.  */
  402. extern int MimeObject_output_init(MimeObject *obj, const char *content_type);
  403.  
  404. /* The `user_visible_p' argument says whether the output that has just been
  405.    written will cause characters or images to show up on the screen, that
  406.    is, it should be FALSE if the stuff being written is merely structural
  407.    HTML or whitespace ("<P>", "</TABLE>", etc.)  This information is used
  408.    when making the decision of whether a separating <HR> is needed.
  409.  */
  410. extern int MimeObject_write(MimeObject *, char *data, int32 length,
  411.                             XP_Bool user_visible_p);
  412. extern int MimeOptions_write(MimeDisplayOptions *,
  413.                              char *data, int32 length,
  414.                              XP_Bool user_visible_p);
  415.  
  416. /* Writes out the right kind of HR (or rather, queues it for writing.) */
  417. extern int MimeObject_write_separator(MimeObject *);
  418.  
  419.  
  420. /* Random junk
  421.  */
  422.  
  423. extern int MK_OUT_OF_MEMORY;
  424.  
  425. #ifdef FREEIF
  426. # undef FREEIF
  427. #endif
  428. #define FREEIF(obj) do { if (obj) { XP_FREE (obj); obj = 0; }} while (0)
  429.  
  430.  
  431. #ifndef MOZILLA_30
  432. /* Turn this on if you want to play with the idea of displaying icons in the
  433.    headers to represent attachments, and put icons next to each attachment so
  434.    you can easily save them without having to bring up the "as links" view.
  435.    Right now, this is all really half-baked, half-implemented,
  436.    half-thought-out, and so on.  But the current "attachment panel" needs to be
  437.    destroyed, and this is the only hope. */
  438. #define JS_ATTACHMENT_MUMBO_JUMBO
  439. #endif /* MOZILLA_30 */
  440.  
  441.  
  442. /* #### These ought to be in libxp or nspr, not libmsg...
  443.  */
  444. extern int msg_GrowBuffer (int32 desired_size, int32 element_size,
  445.                            int32 quantum, char **buffer, int32 *size);
  446.  
  447. extern int msg_LineBuffer (const char *net_buffer, int32 net_buffer_size,
  448.                            char **bufferP, int32 *buffer_sizeP,
  449.                            int32 *buffer_fpP,
  450.                            XP_Bool convert_newlines_p,
  451.                            int (*per_line_fn) (char *line,
  452.                                                int32 line_length,
  453.                                                void *closure),
  454.                            void *closure);
  455.  
  456. #endif /* _MIMEI_H_ */
  457.