---
 check_dbmail_mailbox.c |    2 +-
 dbmail-mailbox.c       |    8 +++++-
 dbmail-mailbox.h       |    2 +-
 export.c               |   51 +++++++++++++++++++++++++++++++++++++----------
 4 files changed, 48 insertions(+), 15 deletions(-)
diff --git a/check_dbmail_mailbox.c b/check_dbmail_mailbox.c
index 2070b36..1d6c4f3 100644
--- a/check_dbmail_mailbox.c
+++ b/check_dbmail_mailbox.c
@@ -159,7 +159,7 @@ START_TEST(test_dbmail_mailbox_dump)
 	int c = 0;
 	FILE *o = fopen("/dev/null","w");
 	struct DbmailMailbox *mb = dbmail_mailbox_new(get_mailbox_id());
-	c = dbmail_mailbox_dump(mb,o);
+	c = dbmail_mailbox_dump(mb,o,NULL);
 	fail_unless(c>=0,"dbmail_mailbox_dump failed");
 	dbmail_mailbox_free(mb);
 //	fprintf(stderr,"dumped [%d] messages\n", c);
diff --git a/dbmail-mailbox.c b/dbmail-mailbox.c
index 7a04c7e..0e3127f 100644
--- a/dbmail-mailbox.c
+++ b/dbmail-mailbox.c
@@ -248,7 +248,7 @@ static size_t dump_message_to_stream(struct DbmailMessage *message, GMimeStream
 	return r;
 }
 
-int dbmail_mailbox_dump(struct DbmailMailbox *self, FILE *file)
+int dbmail_mailbox_dump(struct DbmailMailbox *self, FILE *file, GTree *msgids)
 {
 	unsigned i,j;
 	int count=0;
@@ -269,7 +269,11 @@ int dbmail_mailbox_dump(struct DbmailMailbox *self, FILE *file)
 	t = g_string_new("");
 	ostream = g_mime_stream_file_new(file);
 	
-	ids = g_tree_keys(self->ids);
+	if (msgids)
+		ids = g_tree_keys(msgids);
+	else
+		ids = g_tree_keys(self->ids);
+
 	while (ids) {
 		cids = g_list_append(cids,g_strdup_printf("%llu", *(u64_t *)ids->data));
 		if (! g_list_next(ids))
diff --git a/dbmail-mailbox.h b/dbmail-mailbox.h
index 7dd30cc..097255a 100644
--- a/dbmail-mailbox.h
+++ b/dbmail-mailbox.h
@@ -67,7 +67,7 @@ u64_t dbmail_mailbox_get_id(struct DbmailMailbox *self);
 void dbmail_mailbox_set_uid(struct DbmailMailbox *self, gboolean uid);
 gboolean dbmail_mailbox_get_uid(struct DbmailMailbox *self);
 
-int dbmail_mailbox_dump(struct DbmailMailbox *self, FILE *ostream);
+int dbmail_mailbox_dump(struct DbmailMailbox *self, FILE *ostream, GTree *msgids);
 
 int dbmail_mailbox_remove_uid(struct DbmailMailbox *self, u64_t *id);
 void dbmail_mailbox_map_uid_msn(struct DbmailMailbox *self);
diff --git a/export.c b/export.c
index 3536209..97e7204 100644
--- a/export.c
+++ b/export.c
@@ -43,6 +43,7 @@ int do_showhelp(void) {
 	printf("     -u username   specify a user\n");
 	printf("     -m mailbox    specify a mailbox (default export all mailboxes)\n");
 	printf("     -o outfile    specify the destination mbox (default ./user/mailbox)\n");
+	printf("     -s set        specify the message set in IMAP notation (default 1:*)\n");
 	printf("\n");
 	printf("Summary of options for all modes:\n");
 	printf("\n");
@@ -57,11 +58,12 @@ int do_showhelp(void) {
 	return 0;
 	
 }
-static int mailbox_dump(u64_t mailbox_idnr, const char *outfile)
+static int mailbox_dump(u64_t mailbox_idnr, const char *outfile, const char *set)
 {
 	FILE *ostream;
 	struct DbmailMailbox *mb = NULL;
 	char *dir;
+	GTree *ids = NULL;
 
 	/* 
 	 * For dbmail the usual filesystem semantics don't really 
@@ -71,6 +73,16 @@ static int mailbox_dump(u64_t mailbox_idnr, const char *outfile)
 	 *
 	 * TODO: facilitate maildir type exports
 	 */
+	mb = dbmail_mailbox_new(mailbox_idnr);
+	if (set) {
+		ids = dbmail_mailbox_get_set(mb, set, TRUE);
+		if (! ids || (g_tree_nnodes(ids) == 0)) {
+			qerrorf("Failed. Message set did not match messages in mailbox\n");
+			dbmail_mailbox_free(mb);
+			return 1;
+		}
+	}
+
 	dir = g_path_get_dirname(outfile);
 	if (g_mkdir_with_parents(dir,0700)) {
 		qerrorf("can't create directory [%s]\n", dir);
@@ -79,18 +91,23 @@ static int mailbox_dump(u64_t mailbox_idnr, const char *outfile)
 	}
 	g_free(dir);
 
-	if (! (ostream = fopen(outfile,"a"))) {
+	if (strncmp(outfile,"-",1)==0) {
+		ostream = stdout;
+	} else if (! (ostream = fopen(outfile,"a"))) {
 		int err=errno;
 		qerrorf("opening [%s] failed [%s]\n", outfile, strerror(err));
 		return -1;
 	}
 
-	mb = dbmail_mailbox_new(mailbox_idnr);
-	if (dbmail_mailbox_dump(mb,ostream) < 0)
+
+	if (dbmail_mailbox_dump(mb,ostream, ids) < 0)
 		qerrorf("exporing failed\n");
 
 	if (mb)
 		dbmail_mailbox_free(mb);
+	if (ids)
+		g_tree_destroy(ids);
+
 	return 0;
 }
 	
@@ -100,7 +117,7 @@ int main(int argc, char *argv[])
 	int opt = 0, opt_prev = 0;
 	int show_help = 0;
 	int result = 0;
-	char *user = NULL,*mailbox=NULL, *outfile=NULL;
+	char *user = NULL,*mailbox=NULL, *outfile=NULL, *set=NULL;
 	u64_t useridnr = 0, mailbox_idnr = 0;
 
 	openlog(PNAME, LOG_PID, LOG_MAIL);
@@ -111,7 +128,7 @@ int main(int argc, char *argv[])
 	/* get options */
 	opterr = 0;		/* suppress error message from getopt() */
 	while ((opt = getopt(argc, argv,
-		"-u:m:o:" /* Major modes */
+		"-u:m:o:s:" /* Major modes */
 		"f:qvVh" /* Common options */ )) != -1) {
 		/* The initial "-" of optstring allows unaccompanied
 		 * options and reports them as the optarg to opt 1 (not '1') */
@@ -120,8 +137,7 @@ int main(int argc, char *argv[])
 		opt_prev = opt;
 
 		switch (opt) {
-		/* Major modes of operation
-		 * (exactly one of these is required) */
+		/* export specific options */
 		case 'u':
 			if (optarg && strlen(optarg))
 				user = optarg;
@@ -135,6 +151,14 @@ int main(int argc, char *argv[])
 			if (optarg && strlen(optarg))
 				outfile = optarg;
 			break;
+		case 's':
+			if (optarg && strlen(optarg))
+				set = optarg;
+			else {
+				qprintf("dbmail-mailbox: -s requires a message set\n\n");
+				result = 1;
+			}
+			break;
 
 		/* Common options */
 		case 'f':
@@ -168,7 +192,6 @@ int main(int argc, char *argv[])
 			printf("This is DBMail version %s\n\n%s\n", VERSION, COPYRIGHT);
 			result = 1;
 			break;
-
 		default:
 			/* printf("unrecognized option [%c], continuing...\n",optopt); */
 			break;
@@ -186,6 +209,12 @@ int main(int argc, char *argv[])
 		result = 1;
 		goto freeall;
 	}
+	if (set && (! mailbox) ) {
+		qerrorf("Mailbox required if set is specified.\n");
+		result = 1;
+		goto freeall;
+
+	}
 	/* read the config file */
         if (config_read(configFile) == -1) {
                 qerrorf("Failed. Unable to read config file %s\n", configFile);
@@ -236,7 +265,7 @@ int main(int argc, char *argv[])
 		}
 		
 		qerrorf("  export mailbox /%s/%s -> %s\n", user, mailbox, outfile);
-		mailbox_dump(mailbox_idnr, outfile);
+		mailbox_dump(mailbox_idnr, outfile, set);
 
 	} else {
 		u64_t *children;
@@ -267,7 +296,7 @@ int main(int argc, char *argv[])
 
 			dumpfile = g_strdup_printf("%s/%s.mbox", user, mailbox);
 			qerrorf(" export mailbox /%s/%s -> %s/%s\n", user, mailbox, outfile, dumpfile);
-			if (mailbox_dump(mailbox_idnr, dumpfile)) {
+			if (mailbox_dump(mailbox_idnr, dumpfile, NULL)) {
 				g_free(dumpfile);
 				goto freeall;
 			}
_______________________________________________
Dbmail-dev mailing list
[email protected]
http://twister.fastxs.net/mailman/listinfo/dbmail-dev

Reply via email to