--- ./src/lib-storage/index/imapc/imapc-mail.c.orig	2012-02-12 19:58:17.000000000 +0000
+++ ./src/lib-storage/index/imapc/imapc-mail.c	2012-03-30 20:24:57.668131762 +0000
@@ -2,6 +2,8 @@
 
 #include "lib.h"
 #include "str.h"
+#include "sha1.h"
+#include "istream-header-filter.h"
 #include "istream.h"
 #include "imap-envelope.h"
 #include "imapc-msgmap.h"
@@ -9,6 +11,53 @@
 #include "imapc-client.h"
 #include "imapc-storage.h"
 
+static const char *hdr_hash_skip_headers[] = {
+	"Content-Length",
+	"Status",
+	"X-IMAP",
+	"X-IMAPbase",
+	"X-Keywords",
+	"X-Message-Flag",
+	"X-Status",
+	"X-UID",
+	"X-UIDL"
+};
+
+static int get_hdr_sha1(struct mail *mail, unsigned char sha1[SHA1_RESULTLEN])
+{
+	struct istream *input;
+	const unsigned char *data;
+	size_t size;
+	struct sha1_ctxt sha1_ctx;
+
+	if (mail_get_hdr_stream(mail, NULL, &input) < 0) {
+		i_error("pop3_migration: Failed to get header for msg %u",
+			mail->seq);
+		return -1;
+	}
+	/* hide headers that might change or be different in IMAP vs. POP3 */
+	input = i_stream_create_header_filter(input,
+				HEADER_FILTER_EXCLUDE | HEADER_FILTER_NO_CR,
+				hdr_hash_skip_headers,
+				N_ELEMENTS(hdr_hash_skip_headers),
+				null_header_filter_callback, NULL);
+
+	sha1_init(&sha1_ctx);
+	while (i_stream_read_data(input, &data, &size, 0) > 0) {
+		sha1_loop(&sha1_ctx, data, size);
+		i_stream_skip(input, size);
+	}
+	if (input->stream_errno != 0) {
+		i_error("pop3_migration: Failed to read header for msg %u: %m",
+			mail->seq);
+		i_stream_unref(&input);
+		return -1;
+	}
+	sha1_result(&sha1_ctx, sha1);
+	i_stream_unref(&input);
+	return 0;
+}
+
 struct mail *
 imapc_mail_alloc(struct mailbox_transaction_context *t,
 		 enum mail_fetch_field wanted_fields,
@@ -322,13 +371,17 @@
 imapc_mail_get_special(struct mail *_mail, enum mail_fetch_field field,
 		       const char **value_r)
 {
+	unsigned char sha1[SHA1_RESULTLEN];
 	struct imapc_mailbox *mbox = (struct imapc_mailbox *)_mail->box;
 
 	switch (field) {
 	case MAIL_FETCH_GUID:
 		if (mbox->guid_fetch_field_name == NULL) {
-			/* GUIDs not supported by server */
-			break;
+			if (get_hdr_sha1(_mail, sha1) < 0) {
+				/* GUIDs not supported by server */
+				break;
+			}
+			return sha1;
 		}
 		*value_r = "";
 		return imapc_mail_get_guid(_mail, value_r);
