changeset 6120:00d1a94a94c6 HEAD

Added mail_get_first_header_utf8() and mail_get_headers_utf8().
author Timo Sirainen <tss@iki.fi>
date Fri, 20 Jul 2007 12:36:38 +0300
parents 9607369b6bce
children 157e31742eeb
files src/lib-storage/index/index-mail-headers.c src/lib-storage/index/index-mail.h src/lib-storage/mail-storage-private.h src/lib-storage/mail-storage.h src/lib-storage/mail.c
diffstat 5 files changed, 85 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/index-mail-headers.c	Fri Jul 20 12:11:51 2007 +0300
+++ b/src/lib-storage/index/index-mail-headers.c	Fri Jul 20 12:36:38 2007 +0300
@@ -7,6 +7,7 @@
 #include "str.h"
 #include "message-date.h"
 #include "message-parser.h"
+#include "message-header-decode.h"
 #include "istream-tee.h"
 #include "istream-header-filter.h"
 #include "imap-envelope.h"
@@ -547,9 +548,9 @@
 	return array_idx(&header_values, 0);
 }
 
-const char *const *index_mail_get_headers(struct mail *_mail, const char *field)
+static const char *const *
+index_mail_get_raw_headers(struct index_mail *mail, const char *field)
 {
-	struct index_mail *mail = (struct index_mail *)_mail;
 	const char *headers[2], *value;
 	struct mailbox_header_lookup_ctx *headers_ctx;
 	unsigned char *data;
@@ -620,11 +621,62 @@
 	return array_idx(&header_values, 0);
 }
 
-const char *index_mail_get_first_header(struct mail *mail, const char *field)
+static const char *const *
+index_mail_headers_decode(struct index_mail *mail, const char *const *list,
+			  unsigned int max_count)
 {
-	const char *const *list = index_mail_get_headers(mail, field);
+	const char **decoded_list;
+	unsigned int i, count;
+	buffer_t *buf;
+
+	count = strarray_length(list);
+	if (count > max_count)
+		count = max_count;
+	decoded_list = p_new(mail->data_pool, const char *, count + 1);
+
+	t_push();
+	buf = buffer_create_dynamic(pool_datastack_create(), 512);
+
+	for (i = 0; i < count; i++) {
+		buffer_set_used_size(buf, 0);
+		if (!message_header_decode_utf8((const unsigned char *)list[i],
+						strlen(list[i]), buf, FALSE))
+			decoded_list[i] = list[i];
+		else {
+			decoded_list[i] = p_strndup(mail->data_pool,
+						    buf->data, buf->used);
+		}
+	}
+	t_pop();
+	return decoded_list;
+}
 
-	return list == NULL ? NULL : list[0];
+const char *const *index_mail_get_headers(struct mail *_mail, const char *field,
+					  bool decode_to_utf8)
+{
+	struct index_mail *mail = (struct index_mail *)_mail;
+	const char *const *list;
+
+	list = index_mail_get_raw_headers(mail, field);
+	if (!decode_to_utf8 || list == NULL || *list == NULL)
+		return list;
+
+	return index_mail_headers_decode(mail, list, (unsigned int)-1);
+}
+
+const char *index_mail_get_first_header(struct mail *_mail, const char *field,
+					bool decode_to_utf8)
+{
+	struct index_mail *mail = (struct index_mail *)_mail;
+	const char *const *list;
+
+	list = index_mail_get_raw_headers(mail, field);
+	if (list == NULL || *list == NULL)
+		return NULL;
+
+	if (decode_to_utf8)
+		list = index_mail_headers_decode(mail, list, 1);
+	return list[0];
 }
 
 static void header_cache_callback(struct message_header_line *hdr,
--- a/src/lib-storage/index/index-mail.h	Fri Jul 20 12:11:51 2007 +0300
+++ b/src/lib-storage/index/index-mail.h	Fri Jul 20 12:36:38 2007 +0300
@@ -146,9 +146,11 @@
 			     struct mailbox_header_lookup_ctx *headers);
 void index_mail_headers_get_envelope(struct index_mail *mail);
 
-const char *index_mail_get_first_header(struct mail *_mail, const char *field);
+const char *index_mail_get_first_header(struct mail *_mail, const char *field,
+					bool decode_to_utf8);
 const char *const *
-index_mail_get_headers(struct mail *_mail, const char *field);
+index_mail_get_headers(struct mail *_mail, const char *field,
+		       bool decode_to_utf8);
 struct istream *
 index_mail_get_header_stream(struct mail *_mail,
 			     struct mailbox_header_lookup_ctx *headers);
--- a/src/lib-storage/mail-storage-private.h	Fri Jul 20 12:11:51 2007 +0300
+++ b/src/lib-storage/mail-storage-private.h	Fri Jul 20 12:36:38 2007 +0300
@@ -208,8 +208,10 @@
 	uoff_t (*get_virtual_size)(struct mail *mail);
 	uoff_t (*get_physical_size)(struct mail *mail);
 
-	const char *(*get_first_header)(struct mail *mail, const char *field);
-	const char *const *(*get_headers)(struct mail *mail, const char *field);
+	const char *(*get_first_header)(struct mail *mail, const char *field,
+					bool decode_to_utf8);
+	const char *const *(*get_headers)(struct mail *mail, const char *field,
+					  bool decode_to_utf8);
 	struct istream *
 		(*get_header_stream)(struct mail *mail,
 				     struct mailbox_header_lookup_ctx *headers);
--- a/src/lib-storage/mail-storage.h	Fri Jul 20 12:11:51 2007 +0300
+++ b/src/lib-storage/mail-storage.h	Fri Jul 20 12:36:38 2007 +0300
@@ -424,8 +424,12 @@
 
 /* Get value for single header field */
 const char *mail_get_first_header(struct mail *mail, const char *field);
+/* Like mail_get_first_header(), but decode MIME encoded words to UTF-8 */
+const char *mail_get_first_header_utf8(struct mail *mail, const char *field);
 /* Return a NULL-terminated list of values for each found field. */
 const char *const *mail_get_headers(struct mail *mail, const char *field);
+/* Like mail_get_headers(), but decode MIME encoded words to UTF-8 */
+const char *const *mail_get_headers_utf8(struct mail *mail, const char *field);
 /* Returns stream containing specified headers. */
 struct istream *
 mail_get_header_stream(struct mail *mail,
--- a/src/lib-storage/mail.c	Fri Jul 20 12:11:51 2007 +0300
+++ b/src/lib-storage/mail.c	Fri Jul 20 12:36:38 2007 +0300
@@ -93,14 +93,28 @@
 {
 	struct mail_private *p = (struct mail_private *)mail;
 
-	return p->v.get_first_header(mail, field);
+	return p->v.get_first_header(mail, field, FALSE);
+}
+
+const char *mail_get_first_header_utf8(struct mail *mail, const char *field)
+{
+	struct mail_private *p = (struct mail_private *)mail;
+
+	return p->v.get_first_header(mail, field, TRUE);
 }
 
 const char *const *mail_get_headers(struct mail *mail, const char *field)
 {
 	struct mail_private *p = (struct mail_private *)mail;
 
-	return p->v.get_headers(mail, field);
+	return p->v.get_headers(mail, field, FALSE);
+}
+
+const char *const *mail_get_headers_utf8(struct mail *mail, const char *field)
+{
+	struct mail_private *p = (struct mail_private *)mail;
+
+	return p->v.get_headers(mail, field, TRUE);
 }
 
 struct istream *