[svn-commits] mmichelson: branch mmichelson/imap_consistency r129564 - /team/mmichelson/ima...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Jul 10 10:43:24 CDT 2008


Author: mmichelson
Date: Thu Jul 10 10:43:23 2008
New Revision: 129564

URL: http://svn.digium.com/view/asterisk?view=rev&rev=129564
Log:
Implement the RETRIEVE macro for IMAP storage. It compiles, and that's
all I guarantee about it right now.

Due up next: the DISPOSE macro. This should be cake.


Modified:
    team/mmichelson/imap_consistency/apps/app_voicemail.c

Modified: team/mmichelson/imap_consistency/apps/app_voicemail.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/imap_consistency/apps/app_voicemail.c?view=diff&rev=129564&r1=129563&r2=129564
==============================================================================
--- team/mmichelson/imap_consistency/apps/app_voicemail.c (original)
+++ team/mmichelson/imap_consistency/apps/app_voicemail.c Thu Jul 10 10:43:23 2008
@@ -402,7 +402,7 @@
 #ifdef ODBC_STORAGE
 static char odbc_database[80];
 static char odbc_table[80];
-#define RETRIEVE(a,b) retrieve_file(a,b)
+#define RETRIEVE(a,b,c) retrieve_file(a,b)
 #define DISPOSE(a,b) remove_file(a,b)
 #define STORE(a,b,c,d,e,f,g,h,i) store_file(a,b,c,d)
 #define EXISTS(a,b,c,d) (message_exists(a,b))
@@ -411,7 +411,7 @@
 #define DELETE(a,b,c) (delete_file(a,b))
 #else
 #ifdef IMAP_STORAGE
-#define RETRIEVE(a,b)
+#define RETRIEVE(a,b,c) imap_retrieve_file(a,b,c)
 #define DISPOSE(a,b)
 #define STORE(a,b,c,d,e,f,g,h,i) (imap_store_file(a,b,c,d,e,f,g,h,i))
 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
@@ -420,7 +420,7 @@
 #define IMAP_DELETE(a,b,c,d) (vm_imap_delete(b,d))
 #define DELETE(a,b,c) (vm_delete(c))
 #else
-#define RETRIEVE(a,b)
+#define RETRIEVE(a,b,c)
 #define DISPOSE(a,b)
 #define STORE(a,b,c,d,e,f,g,h,i)
 #define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
@@ -929,7 +929,7 @@
 }
 
 #endif
-static int make_file(char *dest, int len, char *dir, int num)
+static int make_file(char *dest, const int len, const char *dir, const int num)
 {
 	return snprintf(dest, len, "%s/msg%04d", dir, num);
 }
@@ -1645,6 +1645,109 @@
 	unlink(txt);
 	return ast_filedelete(file, NULL);
 }
+
+#ifdef IMAP_STORAGE
+static int imap_retrieve_file(const char *dir, const int msgnum, const struct ast_vm_user *vmu)
+{
+	/* Let's break this into partz */
+
+	/* Part 1: Get the voice message from the IMAP server
+	 * The logic for this can almost be cut and pasted from
+	 * the IMAP version of play_message
+	 *
+	 * Part 2: Get the headers that describe the message
+	 * and place them in an accompanying text file. Most of
+	 * this can also be lifted from the play_message function.
+	 * 
+	 */
+
+	BODY *body;
+	char *header_content;
+	char todir[PATH_MAX];
+	char *attachedfilefmt;
+	const char *cid_num;
+	const char *duration;
+	const char *context;
+	const char *category;
+	const char *origtime;
+	struct vm_state *vms;
+	char text_file[PATH_MAX];
+	FILE *text_file_ptr;
+	
+	/* Before anything can happen, we need a vm_state so that we can
+	 * actually access the imap server
+	 */
+	vms = get_vm_state_by_mailbox(vmu->mailbox, 2);
+	if (!vms) {
+		/* *sigh*, we have to create it */
+		/*XXX Do this later. */
+	}
+
+	if (option_debug > 2)
+		ast_log (LOG_DEBUG,"Before mail_fetchheaders, curmsg is: %d, imap messages is %lu\n", msgnum, vms->msgArray[msgnum]);
+	if (vms->msgArray[msgnum] == 0) {
+		ast_log (LOG_WARNING,"Trying to access unknown message\n");
+		return -1;
+	}
+
+	/* This will only work for new messages... */
+	header_content = mail_fetchheader (vms->mailstream, vms->msgArray[msgnum]);
+	/* empty string means no valid header */
+	if (ast_strlen_zero(header_content)) {
+		ast_log (LOG_ERROR,"Could not fetch header for message number %ld\n",vms->msgArray[msgnum]);
+		return -1;
+	}
+	snprintf(todir, sizeof(todir), "%s%s/%s/tmp", VM_SPOOL_DIR, vmu->context, vmu->mailbox);
+	make_gsm_file(vms->fn, sizeof(vms->fn), vms->imapuser, todir, msgnum);
+
+	mail_fetchstructure (vms->mailstream,vms->msgArray[msgnum],&body);
+	
+	/* We have the body, now we extract the file name of the first attachment. */
+	if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) {
+		attachedfilefmt = ast_strdupa(body->nested.part->next->body.parameter->value);
+	} else {
+		ast_log(LOG_ERROR, "There is no file attached to this IMAP message.\n");
+		return -1;
+	}
+	
+	/* Find the format of the attached file */
+
+	strsep(&attachedfilefmt, ".");
+	if (!attachedfilefmt) {
+		ast_log(LOG_ERROR, "File format could not be obtained from IMAP message attachment\n");
+		return -1;
+	}
+	
+	save_body(body, vms, "2", attachedfilefmt);
+
+	/* Get info from headers!! */
+	make_file(text_file, sizeof(text_file), dir, msgnum);
+	snprintf(text_file, sizeof(text_file), "%s.%s", text_file, "txt");
+
+	if (!(text_file_ptr = fopen(text_file, "w"))) {
+		ast_log(LOG_WARNING, "Unable to open/create file %s: %s\n", text_file, strerror(errno));
+	}
+
+	fprintf(text_file_ptr, "%s\n", "[message]");
+
+	/* XXX I bet this could be optimized by having a single fprintf of the entire
+	 * contents of the text file. However, it may be that the buffering provided
+	 * by the standard I/O library already optimizes this well enough. Come back later.
+	 */
+	cid_num = get_header_by_tag(header_content, "X-Asterisk-VM-Caller-ID-Num:");
+	fprintf(text_file_ptr, "callerid=%s\n", S_OR(cid_num, ""));
+	context = get_header_by_tag(header_content, "X-Asterisk-VM-Context:");
+	fprintf(text_file_ptr, "context=%s\n", S_OR(context, ""));
+	origtime = get_header_by_tag(header_content, "X-Asterisk-VM-Orig-time:");
+	fprintf(text_file_ptr, "origtime=%s\n", S_OR(origtime, ""));
+	duration = get_header_by_tag(header_content, "X-Asterisk-VM-Duration:");
+	fprintf(text_file_ptr, "duration=%s\n", S_OR(origtime, ""));
+	category = get_header_by_tag(header_content, "X-Asterisk-VM-Category:");
+	fprintf(text_file_ptr, "category=%s\n", S_OR(category, ""));
+
+	return 0;
+}
+#endif
 
 static int inbuf(struct baseio *bio, FILE *fi)
 {
@@ -2150,7 +2253,7 @@
 #ifdef ODBC_STORAGE
 	int success = 
 #endif
-	RETRIEVE(filename, -1);
+	RETRIEVE(filename, -1, vmu);
 	if (ast_fileexists(filename, NULL, NULL) > 0) {
 		res = ast_streamfile(chan, filename, chan->language);
 		if (res > -1) 
@@ -2951,7 +3054,7 @@
 		ast_log(LOG_WARNING, "Failed to make directory (%s)\n", tempfile);
 		return -1;
 	}
-	RETRIEVE(tempfile, -1);
+	RETRIEVE(tempfile, -1, vmu);
 	if (ast_fileexists(tempfile, NULL, NULL) > 0)
 		ast_copy_string(prefile, tempfile, sizeof(prefile));
 	DISPOSE(tempfile, -1);
@@ -4032,7 +4135,7 @@
 			myserveremail = vmu->serveremail;
 		
 		if (attach_user_voicemail)
-			RETRIEVE(todir, msgnum);
+			RETRIEVE(todir, msgnum, vmu);
 
 		/*XXX possible imap issue, should category be NULL XXX*/
 		sendmail(myserveremail, vmu, msgnum, vmu->context, vmu->mailbox, cidnum, cidname, fn, fmt, duration, attach_user_voicemail, chan, category);
@@ -4223,7 +4326,7 @@
 		create_dirpath(vmstmp.curdir, sizeof(vmstmp.curdir), sender->context, vmstmp.username, "tmp");
 		make_file(msgfile, sizeof(msgfile), vmstmp.curdir, curmsg);
 
- 		RETRIEVE(dir, curmsg);
+ 		RETRIEVE(dir, curmsg, sender);
 
 		/* Alter a surrogate file, only */
 		copy_plain_file(origmsgfile, msgfile);
@@ -4742,7 +4845,7 @@
 	/* Retrieve info from VM attribute file */
 	make_file(vms->fn2, sizeof(vms->fn2), vms->curdir, vms->curmsg);
 	snprintf(filename, sizeof(filename), "%s.txt", vms->fn2);
-	RETRIEVE(vms->curdir, vms->curmsg);
+	RETRIEVE(vms->curdir, vms->curmsg, vmu);
 	msg_cfg = ast_config_load(filename);
 	if (!msg_cfg) {
 		ast_log(LOG_WARNING, "No message attribute file?!! (%s)\n", filename);
@@ -6250,7 +6353,7 @@
 	while ((cmd >= 0) && (cmd != 't')) {
 		if (cmd)
 			retries = 0;
-		RETRIEVE(prefile, -1);
+		RETRIEVE(prefile, -1, vmu);
 		if (ast_fileexists(prefile, NULL, NULL) <= 0) {
 #ifndef IMAP_STORAGE
 			play_record_review(chan, "vm-rec-temp", prefile, maxgreet, fmtc, 0, vmu, &duration, NULL, record_gain, NULL);
@@ -8196,7 +8299,7 @@
 
 	make_file(vms->fn2, sizeof(vms->fn2), vms->curdir, vms->curmsg);
 	snprintf(filename,sizeof(filename), "%s.txt", vms->fn2);
-	RETRIEVE(vms->curdir, vms->curmsg);
+	RETRIEVE(vms->curdir, vms->curmsg, vmu);
 	msg_cfg = ast_config_load(filename);
 	DISPOSE(vms->curdir, vms->curmsg);
 	if (!msg_cfg) {




More information about the svn-commits mailing list