Index: camel/providers/local/camel-mbox-summary.c
===================================================================
--- camel/providers/local/camel-mbox-summary.c	(revision 8373)
+++ camel/providers/local/camel-mbox-summary.c	(working copy)
@@ -45,6 +45,7 @@
 
 #define io(x)
 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
+#define debug 0
 
 #define CAMEL_MBOX_SUMMARY_VERSION (1)
 
@@ -72,7 +73,9 @@
 static void camel_mbox_summary_class_init (CamelMboxSummaryClass *klass);
 static void camel_mbox_summary_init       (CamelMboxSummary *obj);
 static void camel_mbox_summary_finalise   (CamelObject *obj);
+static int fix_summary_mismatch (CamelFolderSummary *s, CamelException *ex);
 
+
 #ifdef STATUS_PINE
 /* Which status flags are stored in each separate header */
 #define STATUS_XSTATUS (CAMEL_MESSAGE_FLAGGED|CAMEL_MESSAGE_ANSWERED|CAMEL_MESSAGE_DELETED)
@@ -750,17 +753,34 @@
 
 		if (camel_mime_parser_step(mp, NULL, NULL) != CAMEL_MIME_PARSER_STATE_FROM) {
 			g_warning("Expected a From line here, didn't get it");
-			camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
-					     _("Summary and folder mismatch, even after a sync"));
-			goto error;
+			/*
+			   camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
+			   _("Summary and folder mismatch, even after a sync"));
+			   goto error;
+			 */
+			if (!fix_summary_mismatch (s, ex)) {
+				/* Now go back one level */
+				--i;
+			} else
+				goto error;
+			continue;
 		}
 
 		if (camel_mime_parser_tell_start_from(mp) != info->frompos) {
 			g_warning("Didn't get the next message where I expected (%d) got %d instead",
-				  (int)info->frompos, (int)camel_mime_parser_tell_start_from(mp));
-			camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
-					     _("Summary and folder mismatch, even after a sync"));
-			goto error;
+					(int)info->frompos, (int)camel_mime_parser_tell_start_from(mp));
+			/*
+			   camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
+			   _("Summary and folder mismatch, even after a sync"));
+			   goto error;
+			 */
+
+			if (!fix_summary_mismatch (s, ex)) {
+				/* Now go back one level */
+				--i;
+			} else
+				goto error;
+			continue;
 		}
 
 		if (camel_mime_parser_step(mp, NULL, NULL) == CAMEL_MIME_PARSER_STATE_FROM_END) {
@@ -898,6 +918,71 @@
 	return ((CamelLocalSummaryClass *)camel_mbox_summary_parent)->sync(cls, expunge, changeinfo, ex);
 }
 
+static int 
+fix_summary_mismatch (CamelFolderSummary *s, CamelException *ex)
+{
+	/* Read the .mbox file here and parse the From locations 
+	   Update the summary file with these From locations. */
+
+	int mbox_fd;
+	long frompos;
+#if debug 
+	char *subject = NULL; 
+#endif 
+	int state;
+
+	int hits = 0, total = 0;
+	CamelMimeParser *parser;
+	CamelMboxMessageInfo *info = NULL;
+
+	mbox_fd = g_open(((CamelLocalSummary *)s)->folder_path, O_LARGEFILE | O_RDONLY | O_BINARY, 0);
+	if (mbox_fd == -1) {
+		camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
+				_("Folder %s cannot be opened\n  %s"),
+				((CamelLocalSummary *)s)->folder_path, g_strerror (errno));
+		return -1;
+	} else
+		printf ("\n\aFolder %s opened succesfully \n\a", ((CamelLocalSummary *)s)->folder_path);
+
+	parser = camel_mime_parser_new();
+	camel_mime_parser_init_with_fd(parser, mbox_fd);
+	camel_mime_parser_scan_from(parser, TRUE);
+
+	while ( (state = camel_mime_parser_step(parser, NULL, NULL) ) != CAMEL_MIME_PARSER_STATE_EOF) {
+
+		if (state == CAMEL_MIME_PARSER_STATE_FROM) {
+			frompos = camel_mime_parser_tell_start_from (parser);
+
+			/* This code need to be enabled only on debug builds. 
+			   Otherwise the subject parsing will take so much of time on BIIIG folders */
+
+# if debug
+			camel_mime_parser_step (parser, NULL, NULL);
+			subject = (char *)camel_mime_parser_header (parser, "subject", NULL);
+
+			/* Don't try printing subjects in a billion mails folder */
+			printf("Message @ %ld, Subject: %s\n", (long)frompos, subject);
+#endif
+
+			info = (CamelMboxMessageInfo *)camel_folder_summary_index(s, hits);
+			if (info) {
+				info->frompos = frompos;
+				camel_message_info_free((CamelMessageInfo *)info);
+			}
+			hits ++;
+		} 
+		total ++;
+	}
+
+	printf ("\n\aUpdated summary with the from addresses of %d items via %d parses\n\a", hits, total);
+	camel_object_unref(parser);
+	close (mbox_fd);
+	camel_folder_summary_touch(s);
+	camel_folder_summary_save (s);
+	return 0;
+}
+
+
 int
 camel_mbox_summary_sync_mbox(CamelMboxSummary *cls, guint32 flags, CamelFolderChangeInfo *changeinfo, int fd, int fdout, CamelException *ex)
 {
@@ -949,18 +1034,26 @@
 		}
 
 		if (camel_mime_parser_step(mp, &buffer, &len) != CAMEL_MIME_PARSER_STATE_FROM) {
-			g_warning("Expected a From line here, didn't get it");
-			camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
-					     _("Summary and folder mismatch, even after a sync"));
-			goto error;
+			g_warning("Expected a From line here, didn't get it. SO recovering....\n");
+			/*camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
+			  _("Summary and folder mismatch, even after a sync"));*/
+			if (!fix_summary_mismatch (s, ex)) {
+				/* Now go back one level */
+				--i;
+			} else
+				goto error;
+			continue;
 		}
 
 		if (camel_mime_parser_tell_start_from(mp) != info->frompos) {
 			g_warning("Didn't get the next message where I expected (%d) got %d instead",
-				  (int)info->frompos, (int)camel_mime_parser_tell_start_from(mp));
-			camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
-					     _("Summary and folder mismatch, even after a sync"));
-			goto error;
+					(int)info->frompos, (int)camel_mime_parser_tell_start_from(mp));
+			if (!fix_summary_mismatch (s, ex)) {
+				/* Now go back one level */
+				--i;
+			} else
+				goto error;
+			continue;
 		}
 
 		lastdel = FALSE;
