Back to index

lightning-sunbird  0.9+nobinonly
modlmime.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef _LIBMIME_H_
00039 #define _LIBMIME_H_
00040 
00041 #ifdef XP_UNIX
00042 #undef Bool
00043 #endif
00044 
00045 #include "prtypes.h"
00046 #include "nsString.h"
00047 #include "nsMailHeaders.h"
00048 #include "nsIMimeStreamConverter.h"
00049 #include "nsIUnicodeDecoder.h"
00050 #include "nsIUnicodeEncoder.h"
00051 #include "nsIPrefBranch.h"
00052 #include "mozITXTToHTMLConv.h"
00053 
00054 #define MIME_DRAFTS
00055 
00056 /* Opaque object describing a block of message headers, and a couple of
00057    routines for extracting data from one.
00058  */
00059 
00060 typedef struct MimeHeaders 
00061 {
00062   char *all_headers;               /* A char* of the entire header section. */
00063   PRInt32 all_headers_fp;                 /* The length (it is not NULL-terminated.) */
00064   PRInt32 all_headers_size;        /* The size of the allocated block. */
00065 
00066   PRBool done_p;                          /* Whether we've read the end-of-headers marker
00067                                                            (the terminating blank line.) */
00068 
00069   char **heads;                                  /* An array of length n_headers which points
00070                                                            to the beginning of each distinct header:
00071                                                            just after the newline which terminated
00072                                                            the previous one.  This is to speed search.
00073 
00074                                                            This is not initialized until all the
00075                                                            headers have been read.
00076                                                          */
00077   PRInt32 heads_size;                            /* The length (and consequently, how many
00078                                                            distinct headers are in here.) */
00079 
00080 
00081   char *obuffer;                          /* This buffer is used for output. */
00082   PRInt32 obuffer_size;
00083   PRInt32 obuffer_fp;
00084 
00085   char *munged_subject;                   /* What a hack.  This is a place to write down
00086                                                            the subject header, after it's been
00087                                                            charset-ified and stuff.  Remembered so that
00088                                                            we can later use it to generate the
00089                                                            <TITLE> tag. (Also works for giving names to RFC822 attachments) */
00090 } MimeHeaders;
00091 
00092 class MimeDisplayOptions;
00093 class MimeParseStateObject;
00094 typedef struct MSG_AttachmentData MSG_AttachmentData;
00095 
00096 /* Given the name of a header, returns the contents of that header as
00097    a newly-allocated string (which the caller must free.)  If the header
00098    is not present, or has no contents, NULL is returned.
00099 
00100    If `strip_p' is PR_TRUE, then the data returned will be the first token
00101    of the header; else it will be the full text of the header.  (This is
00102    useful for getting just "text/plain" from "text/plain; name=foo".)
00103 
00104    If `all_p' is PR_FALSE, then the first header encountered is used, and
00105    any subsequent headers of the same name are ignored.  If PR_TRUE, then
00106    all headers of the same name are appended together (this is useful
00107    for gathering up all CC headers into one, for example.)
00108  */
00109 extern char *MimeHeaders_get(MimeHeaders *hdrs,
00110                                                   const char *header_name,
00111                                                   PRBool strip_p,
00112                                                   PRBool all_p);
00113 
00114 /* Given a header of the form of the MIME "Content-" headers, extracts a
00115    named parameter from it, if it exists.  For example,
00116    MimeHeaders_get_parameter("text/plain; charset=us-ascii", "charset")
00117    would return "us-ascii".
00118 
00119    Returns NULL if there is no match, or if there is an allocation failure.
00120 
00121    RFC2231 - MIME Parameter Value and Encoded Word Extensions: Character Sets,
00122    Languages, and Continuations
00123 
00124    RFC2231 has added the character sets, languages, and continuations mechanism.
00125    charset, and language information may also be returned to the caller.
00126    Note that charset and language should be nsMemory::Free()'d while 
00127    the return value (parameter) has to be PR_FREE'd. 
00128 
00129    For example,
00130    MimeHeaders_get_parameter("text/plain; name*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A", "name")
00131    MimeHeaders_get_parameter("text/plain; name*0*=us-ascii'en-us'This%20is%20; CRLFLWSPname*1*=%2A%2A%2Afun%2A%2A%2A", "name")
00132    would return "This is ***fun***" and *charset = "us-ascii", *language = "en-us"
00133  */
00134 extern char *MimeHeaders_get_parameter (const char *header_value,
00135                                                                       const char *parm_name,
00136                                                                       char **charset,
00137                                                                       char **language);
00138 
00139 extern      MimeHeaders *MimeHeaders_copy (MimeHeaders *srcHeaders);
00140 
00141 extern void MimeHeaders_free (MimeHeaders *hdrs);
00142   
00143 typedef enum {
00144   MimeHeadersAll,                           /* Show all headers */
00145   MimeHeadersSome,                        /* Show all "interesting" headers */
00146   MimeHeadersSomeNoRef,            /* Same, but suppress the `References' header
00147                                                                      (for when we're printing this message.) */
00148   MimeHeadersMicro,                       /* Show a one-line header summary */
00149   MimeHeadersMicroPlus,            /* Same, but show the full recipient list as
00150                                                                      well (To, CC, etc.) */
00151   MimeHeadersCitation,             /* A one-line summary geared toward use in a
00152                                                                      reply citation ("So-and-so wrote:") */
00153   MimeHeadersOnly,        /* Just parse and output headers...nothing else! */
00154   MimeHeadersNone         /* Skip showing any headers */
00155 } MimeHeadersState;
00156 
00157 
00158 /* The signature for various callbacks in the MimeDisplayOptions structure.
00159  */
00160 typedef char *(*MimeHTMLGeneratorFunction) (const char *data, void *closure,
00161                                                                              MimeHeaders *headers);
00162 
00163 class MimeDisplayOptions
00164 {
00165 public:
00166   MimeDisplayOptions();
00167   virtual ~MimeDisplayOptions();
00168   mozITXTToHTMLConv   *conv;        // For text conversion...
00169   nsCOMPtr<nsIPrefBranch> m_prefBranch; /* prefBranch-service */
00170   nsMimeOutputType    format_out;   // The format out type
00171   nsCString           charsetForCachedInputDecoder;
00172   nsCOMPtr<nsIUnicodeDecoder>   m_inputCharsetToUnicodeDecoder;
00173   nsCOMPtr<nsIUnicodeEncoder>   m_unicodeToUTF8Encoder;
00174 
00175   const char *url;                 /* Base URL for the document.  This string should
00176                                                     be freed by the caller, after the parser
00177                                                     completes (possibly at the same time as the
00178                                                     MimeDisplayOptions itself.) */
00179 
00180   MimeHeadersState headers; /* How headers should be displayed. */
00181   PRBool fancy_headers_p;   /* Whether to do clever formatting of headers
00182                                                     using tables, instead of spaces. */
00183 
00184   PRBool output_vcard_buttons_p;   /* Whether to output the buttons */
00185                                                                /* on vcards. */
00186 
00187   PRBool fancy_links_p;            /* Whether to insert fancy links, so you can
00188                                                            do things like click on an email address to
00189                                                            add it to your address book.  Something you
00190                                                            don't want to do while printing. */
00191 
00192   PRBool variable_width_plaintext_p;      /* Whether text/plain messages should
00193                                                                          be in variable width, or fixed. */
00194   PRBool wrap_long_lines_p; /* Whether to wrap long lines in text/plain
00195                                                            messages. */
00196 
00197   PRBool rot13_p;                  /* Whether text/plain parts should be rotated
00198                                                     Set by "?rot13=true" */
00199   char *part_to_load;              /* The particular part of the multipart which
00200                                                     we are extracting.  Set by "?part=3.2.4" */
00201 
00202   PRBool write_html_p;             /* Whether the output should be HTML, or raw. */
00203 
00204   PRBool decrypt_p;         /* Whether all traces of xlateion should be
00205                                                     eradicated -- this is only meaningful when
00206                                                     write_html_p is PR_FALSE; we set this when
00207                                                     attaching a message for forwarding, since
00208                                                     forwarding someone else a message that wasn't
00209                                                     xlated for them doesn't work.  We have to
00210                                                     dexlate it before sending it.
00211                                                   */
00212 
00213   PRBool nice_html_only_p;         /* If PR_TRUE, then we only should write html if
00214                                                            it's pretty HTML (stuff that we're willing
00215                                                            to get shipped out in mail messages).  If we
00216                                                            can't generate nice stuff for some part,
00217                                                            then don't say anything at all. */
00218 
00219   PRUint32 whattodo ;                     /* from the prefs, we'll get if the user wants to do glyph or structure substitutions and set this member variable. */
00220 
00221   char *default_charset;    /* If this is non-NULL, then it is the charset to
00222                                                     assume when no other one is specified via a
00223                                                     `charset' parameter.
00224                                                   */
00225   PRBool override_charset;  /* If this is PR_TRUE, then we will assume that
00226                                                     all data is in the default_charset, regardless
00227                                of what the `charset' parameter of that part
00228                                says. (This is to cope with the fact that, in
00229                                the real world, many messages are mislabelled
00230                                with the wrong charset.)
00231                                                   */
00232   PRBool  force_user_charset; /* this is the new strategy to deal with incorrectly
00233                                  labeled attachments */
00234 
00235   /* =======================================================================
00236         Stream-related callbacks; for these functions, the `closure' argument
00237         is what is found in `options->stream_closure'.  (One possible exception
00238         is for output_fn; see "output_closure" below.)
00239    */
00240   void *stream_closure;
00241 
00242   /* For setting up the display stream, so that the MIME parser can inform
00243         the caller of the type of the data it will be getting. */
00244   int (*output_init_fn) (const char *type,
00245                                            const char *charset,
00246                                            const char *name,
00247                                            const char *x_mac_type,
00248                                            const char *x_mac_creator,
00249                                            void *stream_closure);
00250 
00251   /* How the MIME parser feeds its output (HTML or raw) back to the caller. */
00252   int (*output_fn) (const char *buf, PRInt32 size, void *closure);
00253 
00254   /* Closure to pass to the above output_fn.  If NULL, then the
00255         stream_closure is used. */
00256   void *output_closure;
00257 
00258   /* A hook for the caller to perform charset-conversion before HTML is
00259         returned.  Each set of characters which originated in a mail message
00260         (body or headers) will be run through this filter before being converted
00261         into HTML.  (This should return bytes which may appear in an HTML file,
00262         ie, we must be able to scan through the string to search for "<" and
00263         turn it in to "&lt;", and so on.)
00264 
00265         `input' is a non-NULL-terminated string of a single line from the message.
00266         `input_length' is how long it is.
00267         `input_charset' is a string representing the charset of this string (as
00268           specified by MIME headers.)
00269         `output_charset' is the charset to which conversion is desired.
00270         `output_ret' is where a newly-malloced string is returned.  It may be
00271           NULL if no translation is needed.
00272         `output_size_ret' is how long the returned string is (it need not be
00273           NULL-terminated.).
00274    */
00275   int (*charset_conversion_fn) (const char *input_line, 
00276                 PRInt32 input_length, const char *input_charset,
00277                                                         const char *output_charset,
00278                                                         char **output_ret, PRInt32 *output_size_ret,
00279                                                         void *stream_closure, nsIUnicodeDecoder *decoder, nsIUnicodeEncoder *encoder);
00280 
00281   /* If PR_TRUE, perform both charset-conversion and decoding of
00282         MIME-2 header fields (using RFC-1522 encoding.)
00283    */
00284   PRBool rfc1522_conversion_p;
00285 
00286   /* A hook for the caller to turn a file name into a content-type. */
00287   char *(*file_type_fn) (const char *filename, void *stream_closure);
00288 
00289   /* A hook by which the user may be prompted for a password by the security
00290         library.  (This is really of type `SECKEYGetPasswordKey'; see sec.h.) */
00291   void *(*passwd_prompt_fn)(void *arg1, void *arg2);
00292 
00293   /* =======================================================================
00294         Various callbacks; for all of these functions, the `closure' argument
00295         is what is found in `html_closure'.
00296    */
00297   void *html_closure;
00298 
00299   /* For emitting some HTML before the start of the outermost message
00300         (this is called before any HTML is written to layout.) */
00301   MimeHTMLGeneratorFunction generate_header_html_fn;
00302 
00303   /* For emitting some HTML after the outermost header block, but before
00304         the body of the first message. */
00305   MimeHTMLGeneratorFunction generate_post_header_html_fn;
00306 
00307   /* For emitting some HTML at the very end (this is called after libmime
00308         has written everything it's going to write.) */
00309   MimeHTMLGeneratorFunction generate_footer_html_fn;
00310 
00311   /* For turning a message ID into a loadable URL. */
00312   MimeHTMLGeneratorFunction generate_reference_url_fn;
00313 
00314   /* For turning a mail address into a mailto URL. */
00315   MimeHTMLGeneratorFunction generate_mailto_url_fn;
00316 
00317   /* For turning a newsgroup name into a news URL. */
00318   MimeHTMLGeneratorFunction generate_news_url_fn;
00319 
00320   /* =======================================================================
00321      Callbacks to handle the backend-specific inlined image display
00322         (internal-external-reconnect junk.)  For `image_begin', the `closure'
00323         argument is what is found in `stream_closure'; but for all of the
00324         others, the `closure' argument is the data that `image_begin' returned.
00325    */
00326 
00327   /* Begins processing an embedded image; the URL and content_type are of the
00328         image itself. */
00329   void *(*image_begin) (const char *image_url, const char *content_type,
00330                                           void *stream_closure);
00331 
00332   /* Stop processing an image. */
00333   void (*image_end) (void *image_closure, int status);
00334 
00335   /* Dump some raw image data down the stream. */
00336   int (*image_write_buffer) (const char *buf, PRInt32 size, void *image_closure);
00337 
00338   /* What HTML should be dumped out for this image. */
00339   char *(*make_image_html) (void *image_closure);
00340 
00341 
00342   /* =======================================================================
00343         Other random opaque state.
00344    */
00345   MimeParseStateObject *state;            /* Some state used by libmime internals;
00346                                                                   initialize this to 0 and leave it alone.
00347                                                                 */
00348 
00349 
00350 #ifdef MIME_DRAFTS
00351   /* =======================================================================
00352        Mail Draft hooks -- 09-19-1996
00353    */
00354   PRBool decompose_file_p;            /* are we decomposing a mime msg 
00355                                                                   into separate files */
00356   PRBool done_parsing_outer_headers;  /* are we done parsing the outer message
00357                                                                         headers; this is really useful when
00358                                                                         we have multiple Message/RFC822 
00359                                                                         headers */
00360   PRBool is_multipart_msg;            /* are we decomposing a multipart
00361                                                                         message */
00362 
00363   int decompose_init_count;            /* used for non multipart message only
00364                                                                       */
00365 
00366   PRBool signed_p;                                  /* to tell draft this is a signed
00367                                                                         message */
00368 
00369   PRBool caller_need_root_headers;    /* set it to true to receive the message main
00370                                          headers through the callback
00371                                          decompose_headers_info_fn */
00372 
00373   /* Callback to gather the outer most headers so we could use the 
00374         information to initialize the addressing/subject/newsgroups fields
00375         for the composition window. */
00376   int (*decompose_headers_info_fn) (void *closure, 
00377                                                                MimeHeaders *headers);
00378 
00379   /* Callbacks to create temporary files for drafts attachments. */
00380   nsresult (*decompose_file_init_fn) (void *stream_closure, 
00381                                                          MimeHeaders *headers );
00382 
00383   nsresult (*decompose_file_output_fn) (const char *buf, PRInt32 size,
00384                                                            void *stream_closure);
00385 
00386   nsresult (*decompose_file_close_fn) (void *stream_closure);
00387 #endif /* MIME_DRAFTS */
00388 
00389   PRInt32 attachment_icon_layer_id; /* Hackhackhack.  This is zero if we have
00390                                                                 not yet emitted the attachment layer
00391                                                                 stuff.  If we have, then this is the
00392                                                                 id number for that layer, which is a
00393                                                                 unique random number every time, to keep
00394                                                                 evil people from writing javascript code
00395                                                                 to hack it. */
00396 
00397   PRBool missing_parts;     /* Whether or not this message is going to contain
00398                                                     missing parts (from IMAP Mime Parts On Demand) */
00399 
00400   PRBool show_attachment_inline_p; /* Whether or not we should display attachment inline (whatever say
00401                          the content-disposition) */
00402 
00403 };
00404 
00405 #endif /* _MODLMIME_H_ */