/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsISupports.idl"

%{C++

/* Opaque objects used by the encoder/decoder to store state. */
typedef struct MimeDecoderData MimeDecoderData;
typedef struct MimeEncoderData MimeEncoderData;

typedef nsresult (*PR_CALLBACK MimeConverterOutputCallback)
    (const char *buf, PRInt32 size, void *closure);

%}

[ptr] native MimeDecoderDataPtr(MimeDecoderData);
[ptr] native MimeEncoderDataPtr(MimeEncoderData);
native MimeConverterOutputCallback(MimeConverterOutputCallback);

/**
 * Encode/decode mail headers (via libmime).
 */
[scriptable, uuid(879f3aeb-b48a-40c1-ae94-ed8f6ecdbc90)]
interface nsIMimeConverter : nsISupports {
  /**
   * Suggested byte length limit for use when calling
   * encodeMimePartIIStr(_UTF8).
   */
  const long MIME_ENCODED_WORD_SIZE = 72;
  const long MAX_CHARSET_NAME_LENGTH = 64;

  /**
   * Encode an ASCII string into RFC2047 form, but don't use this...
   * use encodeMimePartIIStr_UTF8 for scripting purposes.
   * (The header argument could easily contain non-ASCII characters.)
   *
   * All this routine does is convert header to unicode, then call the
   * UTF8 version anyways.
   */
  string encodeMimePartIIStr(in string  header, 
                             in boolean structured, 
                             in string  mailCharset, 
                             in long    fieldnamelen,
                             in long    encodedWordSize);

  /**
   * Encode a UTF-8 CString into RFC 2047 form ("=?" charset "?" encoding "?"
   * encoded-text "?=", where encoding is either "B" for BASE64 (RFC 2045) or
   * "Q" for quoted-printable-ish (RFC 2045)).
   *
   * @param header UTF-8 header to encode.
   * @param structured Is the header's field-body a "structured" field?
   *     (Encoded words are only allowed in comment/phrase portions of
   *     structured field bodies.)
   * @param mailCharset Charset name to convert (as an ASCII C string).
   * @param fieldnamelen Header field name lengt (ex: "From: " = 6)
   * @param encodedWordSize Byte length limit of the output, usually 72. (Use
   *     MIME_ENCODED_WORD_SIZE.)
   *
   * @return The encoded buffer (as a C string).
   */
  string encodeMimePartIIStr_UTF8(in AUTF8String header,
                                  in boolean     structured,
                                  in string      mailCharset,
                                  in long        fieldnamelen,
                                  in long        encodedWordSize);

  /**
   * Decode a MIME header to UTF-8 if conversion is required.  Marked as
   * noscript because the return value may contain non-ASCII characters.
   *
   * @param header A (possibly encoded) header to decode.
   * @param default_charset The charset to apply to un-labeled non-UTF-8 data.
   * @param override_charset If true, default_charset is used instead of any
   *     charset labeling other than UTF-8.
   * @param eatContinuations If true, unfold headers.
   *
   * @return UTF-8 encoded value if conversion was required, nsnull if no
   *     conversion was required.
   */
  [noscript]
  string decodeMimeHeaderToCharPtr(in string header,
                                   in string default_charset,
                                   in boolean override_charset,
                                   in boolean eatContinuations);

  /**
   * Decode a MIME header to UTF-16.
   *
   * @param header A (possibly encoded) header to decode.
   * @param default_charset The charset to apply to un-labeled non-UTF-8 data.
   * @param override_charset If true, default_charset is used instead of any
   *     charset labeling other than UTF-8.
   * @param eatContinuations If true, unfold headers.
   *
   * @return UTF-16 encoded value as an AString.
   */
  AString decodeMimeHeader(in string header,
                           in string default_charset,
                           in boolean override_charset,
                           in boolean eatContinuations);

  [noscript]
  MimeEncoderDataPtr B64EncoderInit(in MimeConverterOutputCallback output_fn,
                                    in voidPtr closure);

  [noscript]
  MimeEncoderDataPtr QPEncoderInit(in MimeConverterOutputCallback output_fn,
                                   in voidPtr closure);

  [noscript]
  void EncoderDestroy(in MimeEncoderDataPtr data, in boolean abort_p);

  [noscript]
  long EncoderWrite(in MimeEncoderDataPtr data, in string buffer,
                    in long size);

};

