int decode_rfc2047 (char *, char *, size_t);
void discard (FILE *);
int default_done (int);
+
+/*
+ * Encode a message header using RFC 2047 encoding. If the message contains
+ * no non-ASCII characters, then leave the header as-is.
+ *
+ * Arguments include:
+ *
+ * name - Message header name
+ * value - Message header content; must point to allocated memory
+ * (may be changed if encoding is necessary)
+ * encoding - Encoding type. May be one of CE_UNKNOWN (function chooses
+ * the encoding), CE_BASE64 or CE_QUOTED
+ * charset - Charset used for encoding. If NULL, obtain from system
+ * locale.
+ *
+ * Returns 0 on success, any other value on failure.
+ */
+
+int encode_rfc2047(const char *name, char **value, int encoding,
+ const char *charset);
+
void escape_display_name (char *, size_t);
void escape_local_part (char *, size_t);
int ext_hook(char *, char *, char *);
--- /dev/null
+/*
+ * Routines to encode message headers using RFC 2047-encoding.
+ *
+ * This code is Copyright (c) 2002, by the authors of nmh. See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
+ */
+
+#include <h/mh.h>
+
+/*
+ * Encode a message header using RFC 2047 encoding. We make the assumption
+ * that all characters < 128 are ASCII and as a consequence don't need any
+ * encoding.
+ */
+
+int
+encode_rfc2047(const char *name, char **value, int encoding,
+ const char *charset)
+{
+ char *p;
+
+ /*
+ * First, check to see if we even need to encode the header
+ */
+
+ for (p = *value; *p != '\0'; p++) {
+ if (! isascii((int) *p)
+ goto encode;
+ }
+
+ return 0;
+
+encode:
+ /*
+ * Some rules from RFC 2047:
+ *
+ * - Encoded words cannot be more than 75 characters long
+ * - Multiple "long" encoded words must be on new lines.
+ *
+ * Also, we're not permitted to encode email addresses, so
+ * we need to actually _parse_ email addresses and only encode
+ * the right bits.