[asterisk-commits] jrothenberger: branch jrothenberger/asterisk-urgent r104531 - in /team/jrothe...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Feb 27 09:15:03 CST 2008


Author: jrothenberger
Date: Wed Feb 27 09:15:02 2008
New Revision: 104531

URL: http://svn.digium.com/view/asterisk?view=rev&rev=104531
Log:
Fixed problem with file-based urgent message ordering.


Modified:
    team/jrothenberger/asterisk-urgent/Makefile
    team/jrothenberger/asterisk-urgent/apps/app_voicemail.c

Modified: team/jrothenberger/asterisk-urgent/Makefile
URL: http://svn.digium.com/view/asterisk/team/jrothenberger/asterisk-urgent/Makefile?view=diff&rev=104531&r1=104530&r2=104531
==============================================================================
--- team/jrothenberger/asterisk-urgent/Makefile (original)
+++ team/jrothenberger/asterisk-urgent/Makefile Wed Feb 27 09:15:02 2008
@@ -135,8 +135,8 @@
   ASTVARRUNDIR=/var/run/asterisk
   ASTMANDIR=/opt/asterisk/man
 else
-  ASTETCDIR=$(sysconfdir)/asterisk
-  ASTLIBDIR=$(libdir)/asterisk
+  ASTETCDIR=/usr/local/etc/asterisk
+  ASTLIBDIR=/usr/local/lib/asterisk
   ASTHEADERDIR=$(includedir)/asterisk
   ASTBINDIR=$(bindir)
   ASTSBINDIR=$(sbindir)

Modified: team/jrothenberger/asterisk-urgent/apps/app_voicemail.c
URL: http://svn.digium.com/view/asterisk/team/jrothenberger/asterisk-urgent/apps/app_voicemail.c?view=diff&rev=104531&r1=104530&r2=104531
==============================================================================
--- team/jrothenberger/asterisk-urgent/apps/app_voicemail.c (original)
+++ team/jrothenberger/asterisk-urgent/apps/app_voicemail.c Wed Feb 27 09:15:02 2008
@@ -157,7 +157,7 @@
 static int imap_retrieve_file (char *dir, int msgnum, const char *mailbox, char *context);
 static int imap_delete_old_greeting (char *dir, struct vm_state *vms);
 static void check_quota(struct vm_state *vms, char *mailbox);
-static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu,int box, int urgent);
+static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
 struct vmstate {
 	struct vm_state *vms;
 	AST_LIST_ENTRY(vmstate) list;
@@ -222,7 +222,6 @@
 #define VM_TEMPGREETWARN (1 << 15)  /*!< Remind user tempgreeting is set */
 #define VM_MOVEHEARD     (1 << 16)  /*!< Move a "heard" message to Old after listening to it */
 #define ERROR_LOCK_PATH  -100
-#define ERROR_MAILBOX_FULL	-200
 
 
 enum {
@@ -448,7 +447,7 @@
 #define RETRIEVE(a,b,c,d) (imap_retrieve_file(a,b,c,d ))
 #define DISPOSE(a,b) (imap_remove_file(a,b))
 #define STORE(a,b,c,d,e,f,g,h,i,j) (imap_store_file(a,b,c,d,e,f,g,h,i,j))
-#define EXISTS(a,b,c,d) (ast_fileexists(c, NULL, d) > 0)
+#define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
 #define COPY(a,b,c,d,e,f,g,h) (copy_file(g,h));
 #define IMAP_DELETE(a,b,c,d) (vm_imap_delete(b,d))
@@ -457,9 +456,9 @@
 #define RETRIEVE(a,b,c,d)
 #define DISPOSE(a,b)
 #define STORE(a,b,c,d,e,f,g,h,i,j)
-#define EXISTS(a,b,c,d) (ast_fileexists(c, NULL, d) > 0)
+#define EXISTS(a,b,c,d) (ast_fileexists(c,NULL,d) > 0)
 #define RENAME(a,b,c,d,e,f,g,h) (rename_file(g,h));
-#define COPY(a,b,c,d,e,f,g,h) (copy_plain_file(g,h));
+#define COPY(a,b,c,d,e,f,g,h) (copy_file(g,h));
 #define DELETE(a,b,c) (vm_delete(c))
 #endif
 #endif
@@ -662,7 +661,7 @@
 static char emaildateformat[32] = "%A, %B %d, %Y at %r";
 
 /* Forward declarations - generic */
-static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu,int box, int urgent);
+static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box);
 static int advanced_options(struct ast_channel *chan, struct ast_vm_user *vmu, struct vm_state *vms, int msg, int option, signed char record_gain);
 static int dialout(struct ast_channel *chan, struct ast_vm_user *vmu, char *num, char *outgoing_context);
 static int play_record_review(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime,
@@ -674,9 +673,6 @@
 static void make_email_file(FILE *p, char *srcemail, struct ast_vm_user *vmu, int msgnum, char *context, char *mailbox, char *cidnum, char *cidname, char *attach, char *format, int duration, int attach_user_voicemail, struct ast_channel *chan, const char *category, int imap, char *flag);
 static void apply_options(struct ast_vm_user *vmu, const char *options);
 static int is_valid_dtmf(const char *key);
-#if !(defined(IMAP_STORAGE))
-static int check_if_urgent(char *dir, char *filename);
-#endif
 
 #if !(defined(ODBC_STORAGE) || defined(IMAP_STORAGE))
 static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit);
@@ -722,31 +718,31 @@
 #endif
 	} else if (!strcasecmp(var, "delete") || !strcasecmp(var, "deletevoicemail")) {
 		ast_set2_flag(vmu, ast_true(value), VM_DELETE);	
-	} else if (!strcasecmp(var, "saycid")) {
+	} else if (!strcasecmp(var, "saycid")){
 		ast_set2_flag(vmu, ast_true(value), VM_SAYCID);	
-	} else if (!strcasecmp(var, "sendvoicemail")) {
+	} else if (!strcasecmp(var,"sendvoicemail")){
 		ast_set2_flag(vmu, ast_true(value), VM_SVMAIL);	
-	} else if (!strcasecmp(var, "review")) {
+	} else if (!strcasecmp(var, "review")){
 		ast_set2_flag(vmu, ast_true(value), VM_REVIEW);
-	} else if (!strcasecmp(var, "tempgreetwarn")) {
+	} else if (!strcasecmp(var, "tempgreetwarn")){
 		ast_set2_flag(vmu, ast_true(value), VM_TEMPGREETWARN);	
-	} else if (!strcasecmp(var, "operator")) {
+	} else if (!strcasecmp(var, "operator")){
 		ast_set2_flag(vmu, ast_true(value), VM_OPERATOR);	
-	} else if (!strcasecmp(var, "envelope")) {
+	} else if (!strcasecmp(var, "envelope")){
 		ast_set2_flag(vmu, ast_true(value), VM_ENVELOPE);	
-	} else if (!strcasecmp(var, "moveheard")) {
+	} else if (!strcasecmp(var, "moveheard")){
 		ast_set2_flag(vmu, ast_true(value), VM_MOVEHEARD);
-	} else if (!strcasecmp(var, "sayduration")) {
+	} else if (!strcasecmp(var, "sayduration")){
 		ast_set2_flag(vmu, ast_true(value), VM_SAYDURATION);	
-	} else if (!strcasecmp(var, "saydurationm")) {
+	} else if (!strcasecmp(var, "saydurationm")){
 		if (sscanf(value, "%d", &x) == 1) {
 			vmu->saydurationm = x;
 		} else {
 			ast_log(LOG_WARNING, "Invalid min duration for say duration\n");
 		}
-	} else if (!strcasecmp(var, "forcename")) {
+	} else if (!strcasecmp(var, "forcename")){
 		ast_set2_flag(vmu, ast_true(value), VM_FORCENAME);	
-	} else if (!strcasecmp(var, "forcegreetings")) {
+	} else if (!strcasecmp(var, "forcegreetings")){
 		ast_set2_flag(vmu, ast_true(value), VM_FORCEGREET);	
 	} else if (!strcasecmp(var, "callback")) {
 		ast_copy_string(vmu->callback, value, sizeof(vmu->callback));
@@ -903,7 +899,7 @@
 static struct ast_vm_user *find_user(struct ast_vm_user *ivm, const char *context, const char *mailbox)
 {
 	/* This function could be made to generate one from a database, too */
-	struct ast_vm_user *vmu = NULL, *cur;
+	struct ast_vm_user *vmu=NULL, *cur;
 	AST_LIST_LOCK(&users);
 
 	if (!context && !ast_test_flag((&globalflags), VM_SEARCH))
@@ -949,11 +945,11 @@
 
 static void vm_change_password(struct ast_vm_user *vmu, const char *newpassword)
 {
-	struct ast_config *cfg = NULL;
-	struct ast_variable *var = NULL;
-	struct ast_category *cat = NULL;
-	char *category = NULL, *value = NULL, *new = NULL;
-	const char *tmp = NULL;
+	struct ast_config   *cfg=NULL;
+	struct ast_variable *var=NULL;
+	struct ast_category *cat=NULL;
+	char *category=NULL, *value=NULL, *new=NULL;
+	const char *tmp=NULL;
 	struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS };
 	if (!change_password_realtime(vmu, newpassword))
 		return;
@@ -966,13 +962,13 @@
 					ast_log(LOG_WARNING, "We could not find the mailbox.\n");
 					break;
 				}
-				value = strstr(tmp, ",");
+				value = strstr(tmp,",");
 				if (!value) {
 					ast_log(LOG_WARNING, "variable has bad format.\n");
 					break;
 				}
-				new = alloca(strlen(value) + strlen(newpassword) + 1);
-				sprintf(new, "%s%s", newpassword, value);
+				new = alloca((strlen(value)+strlen(newpassword)+1));
+				sprintf(new,"%s%s", newpassword, value);
 				if (!(cat = ast_category_get(cfg, category))) {
 					ast_log(LOG_WARNING, "Failed to get category structure.\n");
 					break;
@@ -998,7 +994,7 @@
 					ast_debug(3, "looks like we need to make vmsecret!\n");
 					var = ast_variable_new("vmsecret", newpassword, "");
 				} 
-				new = alloca(strlen(newpassword) + 1);
+				new = alloca(strlen(newpassword)+1);
 				sprintf(new, "%s", newpassword);
 				if (!(cat = ast_category_get(cfg, category))) {
 					ast_debug(4, "failed to get category!\n");
@@ -1020,7 +1016,7 @@
 static void vm_change_password_shell(struct ast_vm_user *vmu, char *newpassword)
 {
 	char buf[255];
-	snprintf(buf, sizeof(buf), "%s %s %s %s", ext_pass_cmd, vmu->context, vmu->mailbox, newpassword);
+	snprintf(buf,255,"%s %s %s %s",ext_pass_cmd,vmu->context,vmu->mailbox,newpassword);
 	if (!ast_safe_system(buf)) {
 		ast_copy_string(vmu->password, newpassword, sizeof(vmu->password));
 		/* Reset the password in memory, too */
@@ -1054,13 +1050,13 @@
 
 	messageNum = vms->msgArray[msgnum];
 	if (messageNum == 0) {
-		ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n", msgnum, messageNum);
+		ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n",msgnum,messageNum);
 		return;
 	}
-	ast_debug(3, "deleting msgnum %d, which is mailbox message %lu\n", msgnum, messageNum);
+	ast_debug(3, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum);
 	/* delete message */
-	snprintf(arg, sizeof(arg), "%lu", messageNum);
-	mail_setflag(vms->mailstream, arg, "\\DELETED");
+	snprintf (arg, sizeof(arg), "%lu",messageNum);
+	mail_setflag (vms->mailstream,arg,"\\DELETED");
 }
 
 #endif
@@ -1139,13 +1135,13 @@
 {
 	int x = 0;
 	int res;
-	int fd = -1;
+	int fd=-1;
 	size_t fdlen = 0;
 	void *fdm = MAP_FAILED;
-	SQLSMALLINT colcount = 0;
+	SQLSMALLINT colcount=0;
 	SQLHSTMT stmt;
 	char sql[PATH_MAX];
-	char fmt[80] = "";
+	char fmt[80]="";
 	char *c;
 	char coltitle[256];
 	SQLSMALLINT collen;
@@ -1154,7 +1150,7 @@
 	SQLSMALLINT nullable;
 	SQLULEN colsize;
 	SQLLEN colsize2;
-	FILE *f = NULL;
+	FILE *f=NULL;
 	char rowdata[80];
 	char fn[PATH_MAX];
 	char full_fn[PATH_MAX];
@@ -1171,7 +1167,7 @@
 			*c = '\0';
 		if (!strcasecmp(fmt, "wav49"))
 			strcpy(fmt, "WAV");
-		snprintf(msgnums, sizeof(msgnums), "%d", msgnum);
+		snprintf(msgnums, sizeof(msgnums),"%d", msgnum);
 		if (msgnum > -1)
 			make_file(fn, sizeof(fn), dir, msgnum);
 		else
@@ -1184,7 +1180,7 @@
 		}
 		
 		snprintf(full_fn, sizeof(full_fn), "%s.%s", fn, fmt);
-		snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE dir=? AND msgnum=?", odbc_table);
+		snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE dir=? AND msgnum=?",odbc_table);
 		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
 		if (!stmt) {
 			ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
@@ -1219,7 +1215,7 @@
 		}
 		if (f) 
 			fprintf(f, "[message]\n");
-		for (x = 0; x < colcount; x++) {
+		for (x=0;x<colcount;x++) {
 			rowdata[0] = '\0';
 			collen = sizeof(coltitle);
 			res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen, 
@@ -1235,7 +1231,7 @@
 				res = SQLGetData(stmt, x + 1, SQL_BINARY, rowdata, 0, &colsize2);
 				fdlen = colsize2;
 				if (fd > -1) {
-					char tmp[1] = "";
+					char tmp[1]="";
 					lseek(fd, fdlen - 1, SEEK_SET);
 					if (write(fd, tmp, 1) != 1) {
 						close(fd);
@@ -1320,7 +1316,7 @@
 	struct odbc_obj *obj;
 	obj = ast_odbc_request_obj(odbc_database, 0);
 	if (obj) {
-		snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=?", odbc_table);
+		snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=?",odbc_table);
 		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
 		if (!stmt) {
 			ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
@@ -1366,7 +1362,7 @@
 	obj = ast_odbc_request_obj(odbc_database, 0);
 	if (obj) {
 		snprintf(msgnums, sizeof(msgnums), "%d", msgnum);
-		snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=? AND msgnum=?", odbc_table);
+		snprintf(sql, sizeof(sql), "SELECT COUNT(*) FROM %s WHERE dir=? AND msgnum=?",odbc_table);
 		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
 		if (!stmt) {
 			ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
@@ -1414,7 +1410,7 @@
 	obj = ast_odbc_request_obj(odbc_database, 0);
 	if (obj) {
 		snprintf(msgnums, sizeof(msgnums), "%d", smsg);
-		snprintf(sql, sizeof(sql), "DELETE FROM %s WHERE dir=? AND msgnum=?", odbc_table);
+		snprintf(sql, sizeof(sql), "DELETE FROM %s WHERE dir=? AND msgnum=?",odbc_table);
 		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
 		if (!stmt)
 			ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
@@ -1441,7 +1437,7 @@
 	if (obj) {
 		snprintf(msgnums, sizeof(msgnums), "%d", smsg);
 		snprintf(msgnumd, sizeof(msgnumd), "%d", dmsg);
-		snprintf(sql, sizeof(sql), "INSERT INTO %s (dir, msgnum, context, macrocontext, callerid, origtime, duration, recording, mailboxuser, mailboxcontext) SELECT ?,?,context,macrocontext,callerid,origtime,duration,recording,?,? FROM %s WHERE dir=? AND msgnum=?", odbc_table, odbc_table);
+		snprintf(sql, sizeof(sql), "INSERT INTO %s (dir, msgnum, context, macrocontext, callerid, origtime, duration, recording, mailboxuser, mailboxcontext) SELECT ?,?,context,macrocontext,callerid,origtime,duration,recording,?,? FROM %s WHERE dir=? AND msgnum=?",odbc_table,odbc_table);
 		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
 		if (!stmt)
 			ast_log(LOG_WARNING, "SQL Execute error!\n[%s] (You probably don't have MySQL 4.1 or later installed)\n\n", sql);
@@ -1466,11 +1462,11 @@
 	char msgnums[20];
 	char fn[PATH_MAX];
 	char full_fn[PATH_MAX];
-	char fmt[80] = "";
+	char fmt[80]="";
 	char *c;
-	const char *context = "", *macrocontext = "", *callerid = "", *origtime = "", *duration = "";
+	const char *context="", *macrocontext="", *callerid="", *origtime="", *duration="";
 	const char *category = "";
-	struct ast_config *cfg = NULL;
+	struct ast_config *cfg=NULL;
 	struct odbc_obj *obj;
 	struct ast_flags config_flags = { CONFIG_FLAG_NOCACHE };
 
@@ -1483,7 +1479,7 @@
 			*c = '\0';
 		if (!strcasecmp(fmt, "wav49"))
 			strcpy(fmt, "WAV");
-		snprintf(msgnums, sizeof(msgnums), "%d", msgnum);
+		snprintf(msgnums, sizeof(msgnums),"%d", msgnum);
 		if (msgnum > -1)
 			make_file(fn, sizeof(fn), dir, msgnum);
 		else
@@ -1514,7 +1510,7 @@
 		fdlen = lseek(fd, 0, SEEK_END);
 		lseek(fd, 0, SEEK_SET);
 		printf("Length is %zd\n", fdlen);
-		fdm = mmap(NULL, fdlen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+		fdm = mmap(NULL, fdlen, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0);
 		if (fdm == MAP_FAILED) {
 			ast_log(LOG_WARNING, "Memory map failed!\n");
 			ast_odbc_release_obj(obj);
@@ -1527,9 +1523,9 @@
 			goto yuck;
 		}
 		if (!ast_strlen_zero(category)) 
-			snprintf(sql, sizeof(sql), "INSERT INTO %s (dir,msgnum,recording,context,macrocontext,callerid,origtime,duration,mailboxuser,mailboxcontext,category) VALUES (?,?,?,?,?,?,?,?,?,?,?)", odbc_table);
+			snprintf(sql, sizeof(sql), "INSERT INTO %s (dir,msgnum,recording,context,macrocontext,callerid,origtime,duration,mailboxuser,mailboxcontext,category) VALUES (?,?,?,?,?,?,?,?,?,?,?)",odbc_table); 
 		else
-			snprintf(sql, sizeof(sql), "INSERT INTO %s (dir,msgnum,recording,context,macrocontext,callerid,origtime,duration,mailboxuser,mailboxcontext) VALUES (?,?,?,?,?,?,?,?,?,?)", odbc_table);
+			snprintf(sql, sizeof(sql), "INSERT INTO %s (dir,msgnum,recording,context,macrocontext,callerid,origtime,duration,mailboxuser,mailboxcontext) VALUES (?,?,?,?,?,?,?,?,?,?)",odbc_table);
 		res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
 		if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 			ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
@@ -1586,7 +1582,7 @@
 	if (obj) {
 		snprintf(msgnums, sizeof(msgnums), "%d", smsg);
 		snprintf(msgnumd, sizeof(msgnumd), "%d", dmsg);
-		snprintf(sql, sizeof(sql), "UPDATE %s SET dir=?, msgnum=?, mailboxuser=?, mailboxcontext=? WHERE dir=? AND msgnum=?", odbc_table);
+		snprintf(sql, sizeof(sql), "UPDATE %s SET dir=?, msgnum=?, mailboxuser=?, mailboxcontext=? WHERE dir=? AND msgnum=?",odbc_table);
 		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
 		if (!stmt)
 			ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
@@ -1600,8 +1596,7 @@
 
 #else
 #ifndef IMAP_STORAGE
-/* For now, only count URGENT messages in INBOX */
-static int count_messages(struct ast_vm_user *vmu, char *dir, int urgent)
+static int count_messages(struct ast_vm_user *vmu, char *dir)
 {
 	/* Find all .txt files - even if they are not in sequence from 0000 */
 
@@ -1615,13 +1610,7 @@
 	if ((vmdir = opendir(dir))) {
 		while ((vment = readdir(vmdir))) {
 			if (strlen(vment->d_name) > 7 && !strncmp(vment->d_name + 7, ".txt", 4)) {
-				if (urgent == -1) { /* Count all messages */
-					vmcount++;
-				} else {
-					if (urgent == check_if_urgent(dir, vment->d_name)) {  /* count urgent or non-urgent as specified */
-						vmcount++;
-					}
-				}
+				vmcount++;
 			}
 		}
 		closedir(vmdir);
@@ -1635,7 +1624,7 @@
 {
 	char stxt[PATH_MAX];
 	char dtxt[PATH_MAX];
-	ast_filerename(sfn, dfn, NULL);
+	ast_filerename(sfn,dfn,NULL);
 	snprintf(stxt, sizeof(stxt), "%s.txt", sfn);
 	snprintf(dtxt, sizeof(dtxt), "%s.txt", dfn);
 	if (ast_check_realtime("voicemail_data")) {
@@ -1643,42 +1632,6 @@
 	}
 	rename(stxt, dtxt);
 }
-#endif
-
-#ifndef IMAP_STORAGE
-/*! \brief
- * A negative return value indicates an error.
- * \note Should always be called with a lock already set on dir.
- */
-static int last_message_index(struct ast_vm_user *vmu, char *dir)
-{
-	int x;
-	unsigned char map[MAXMSGLIMIT] = "";
-	DIR *msgdir;
-	struct dirent *msgdirent;
-	int msgdirint;
-
-	/* Reading the entire directory into a file map scales better than
-	 * doing a stat repeatedly on a predicted sequence.  I suspect this
-	 * is partially due to stat(2) internally doing a readdir(2) itself to
-	 * find each file. */
-	msgdir = opendir(dir);
-	while ((msgdirent = readdir(msgdir))) {
-		if (sscanf(msgdirent->d_name, "msg%d", &msgdirint) == 1 && msgdirint < MAXMSGLIMIT)
-			map[msgdirint] = 1;
-	}
-	closedir(msgdir);
-
-	for (x = 0; x < vmu->maxmsg; x++) {
-		if (map[x] == 0)
-			break;
-	}
-
-	return x - 1;
-}
-
-#endif /*#ifndef IMAP_STORAGE*/
-#endif /*#else of #ifdef ODBC_STORAGE*/
 
 static int copy(char *infile, char *outfile)
 {
@@ -1730,10 +1683,10 @@
 #endif
 }
 
-static void copy_plain_file(char *frompath, char *topath)
+static void copy_file(char *frompath, char *topath)
 {
 	char frompath2[PATH_MAX], topath2[PATH_MAX];
-	struct ast_variable *tmp, *var = NULL;
+	struct ast_variable *tmp,*var = NULL;
 	const char *origmailbox = NULL, *context = NULL, *macrocontext = NULL, *exten = NULL, *priority = NULL, *callerchan = NULL, *callerid = NULL, *origdate = NULL, *origtime = NULL, *category = NULL, *duration = NULL;
 	ast_filecopy(frompath, topath, NULL);
 	snprintf(frompath2, sizeof(frompath2), "%s.txt", frompath);
@@ -1771,13 +1724,46 @@
 	copy(frompath2, topath2);
 	ast_variables_destroy(var);
 }
-
+/*! \brief
+ * A negative return value indicates an error.
+ * \note Should always be called with a lock already set on dir.
+ */
+static int last_message_index(struct ast_vm_user *vmu, char *dir)
+{
+	int x;
+	unsigned char map[MAXMSGLIMIT] = "";
+	DIR *msgdir;
+	struct dirent *msgdirent;
+	int msgdirint;
+
+	/* Reading the entire directory into a file map scales better than
+	 * doing a stat repeatedly on a predicted sequence.  I suspect this
+	 * is partially due to stat(2) internally doing a readdir(2) itself to
+	 * find each file. */
+	msgdir = opendir(dir);
+	while ((msgdirent = readdir(msgdir))) {
+		if (sscanf(msgdirent->d_name, "msg%d", &msgdirint) == 1 && msgdirint < MAXMSGLIMIT)
+			map[msgdirint] = 1;
+	}
+	closedir(msgdir);
+
+	for (x = 0; x < vmu->maxmsg; x++) {
+		if (map[x] == 0)
+			break;
+	}
+
+	return x - 1;
+}
+
+#endif /*#ifndef IMAP_STORAGE*/
+#endif /*#else of #ifdef ODBC_STORAGE*/
+#ifndef ODBC_STORAGE
 static int vm_delete(char *file)
 {
 	char *txt;
 	int txtsize = 0;
 
-	txtsize = (strlen(file) + 5) * sizeof(char);
+	txtsize = (strlen(file) + 5)*sizeof(char);
 	txt = alloca(txtsize);
 	/* Sprintf here would safe because we alloca'd exactly the right length,
 	 * but trying to eliminate all sprintf's anyhow
@@ -1789,6 +1775,7 @@
 	unlink(txt);
 	return ast_filedelete(file, NULL);
 }
+#endif
 
 static int inbuf(struct baseio *bio, FILE *fi)
 {
@@ -1797,7 +1784,7 @@
 	if (bio->ateof)
 		return 0;
 
-	if ((l = fread(bio->iobuf, 1, BASEMAXINLINE, fi)) <= 0) {
+	if ((l = fread(bio->iobuf,1,BASEMAXINLINE,fi)) <= 0) {
 		if (ferror(fi))
 			return -1;
 
@@ -1805,15 +1792,15 @@
 		return 0;
 	}
 
-	bio->iolen = l;
-	bio->iocp = 0;
+	bio->iolen= l;
+	bio->iocp= 0;
 
 	return 1;
 }
 
 static int inchar(struct baseio *bio, FILE *fi)
 {
-	if (bio->iocp >= bio->iolen) {
+	if (bio->iocp>=bio->iolen) {
 		if (!inbuf(bio, fi))
 			return EOF;
 	}
@@ -1824,13 +1811,13 @@
 static int ochar(struct baseio *bio, int c, FILE *so)
 {
 	if (bio->linelength >= BASELINELEN) {
-		if (fputs(eol, so) == EOF)
+		if (fputs(eol,so) == EOF)
 			return -1;
 
 		bio->linelength= 0;
 	}
 
-	if (putc(((unsigned char)c), so) == EOF)
+	if (putc(((unsigned char)c),so) == EOF)
 		return -1;
 
 	bio->linelength++;
@@ -1844,7 +1831,7 @@
 		'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
 		'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0',
 		'1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
-	int i, hiteof = 0;
+	int i,hiteof= 0;
 	FILE *fi;
 	struct baseio bio;
 
@@ -1856,42 +1843,42 @@
 		return -1;
 	}
 
-	while (!hiteof) {
+	while (!hiteof){
 		unsigned char igroup[3], ogroup[4];
-		int c, n;
-
-		igroup[0] = igroup[1] = igroup[2] = 0;
-
-		for (n = 0; n < 3; n++) {
+		int c,n;
+
+		igroup[0]= igroup[1]= igroup[2]= 0;
+
+		for (n= 0;n<3;n++) {
 			if ((c = inchar(&bio, fi)) == EOF) {
-				hiteof = 1;
+				hiteof= 1;
 				break;
 			}
 
-			igroup[n] = (unsigned char)c;
-		}
-
-		if (n > 0) {
-			ogroup[0] = dtable[igroup[0] >> 2];
-			ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
-			ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
-			ogroup[3] = dtable[igroup[2] & 0x3F];
-
-			if (n < 3) {
-				ogroup[3] = '=';
-
-				if (n < 2)
-					ogroup[2] = '=';
-			}
-
-			for (i = 0; i < 4; i++)
+			igroup[n]= (unsigned char)c;
+		}
+
+		if (n> 0) {
+			ogroup[0]= dtable[igroup[0]>>2];
+			ogroup[1]= dtable[((igroup[0]&3)<<4) | (igroup[1]>>4)];
+			ogroup[2]= dtable[((igroup[1]&0xF)<<2) | (igroup[2]>>6)];
+			ogroup[3]= dtable[igroup[2]&0x3F];
+
+			if (n<3) {
+				ogroup[3]= '=';
+
+				if (n<2)
+					ogroup[2]= '=';
+			}
+
+			for (i= 0;i<4;i++)
 				ochar(&bio, ogroup[i], so);
 		}
 	}
 
 	fclose(fi);
 	
-	if (fputs(eol, so) == EOF)
+	if (fputs(eol,so)==EOF)
 		return 0;
 
 	return 1;
@@ -1991,7 +1978,7 @@
 #define ENDL "\n"
 #endif
 
-	gethostname(host, sizeof(host) - 1);
+	gethostname(host, sizeof(host)-1);
 
 	if (strchr(srcemail, '@'))
 		ast_copy_string(who, srcemail, sizeof(who));
@@ -2013,7 +2000,7 @@
 		struct ast_channel *ast;
 		if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
 			char *passdata;
-			int vmlen = strlen(fromstring) * 3 + 200;
+			int vmlen = strlen(fromstring)*3 + 200;
 			passdata = alloca(vmlen);
 			memset(passdata, 0, vmlen);
 			prep_email_sub_vars(ast, vmu, msgnum + 1, context, mailbox, cidnum, cidname, dur, date, passdata, vmlen, category, flag);
@@ -2098,7 +2085,7 @@
 		struct ast_channel *ast;
 		if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
 			char *passdata;
-			int vmlen = strlen(emailbody) * 3 + 200;
+			int vmlen = strlen(emailbody)*3 + 200;
 			passdata = alloca(vmlen);
 			memset(passdata, 0, vmlen);
 			prep_email_sub_vars(ast, vmu, msgnum + 1, context, mailbox, cidnum, cidname, dur, date, passdata, vmlen, category, flag);
@@ -2107,7 +2094,7 @@
 			ast_channel_free(ast);
 		} else
 			ast_log(LOG_WARNING, "Cannot allocate the channel for variables substitution\n");
-	} else if (msgnum > -1) {
+	} else if (msgnum > -1){
 		fprintf(p, "Dear %s:" ENDL ENDL "\tJust wanted to let you know you were just left a %s long %s message (number %d)" ENDL
 		"in mailbox %s from %s, on %s so you might" ENDL
 		"want to check it when you get a chance.  Thanks!" ENDL ENDL "\t\t\t\t--Asterisk" ENDL ENDL, vmu->fullname, 
@@ -2160,7 +2147,7 @@
 
 static int sendmail(char *srcemail, struct ast_vm_user *vmu, int msgnum, char *context, char *mailbox, char *cidnum, char *cidname, char *attach, char *format, int duration, int attach_user_voicemail, struct ast_channel *chan, const char *category, char *flag)
 {
-	FILE *p = NULL;
+	FILE *p=NULL;
 	char tmp[80] = "/tmp/astmail-XXXXXX";
 	char tmp2[256];
 
@@ -2201,7 +2188,7 @@
 		ast_log(LOG_WARNING, "Unable to launch '%s' (can't create temporary file)\n", mailcmd);
 		return -1;
 	}
-	gethostname(host, sizeof(host) - 1);
+	gethostname(host, sizeof(host)-1);
 	if (strchr(srcemail, '@'))
 		ast_copy_string(who, srcemail, sizeof(who));
 	else 
@@ -2214,7 +2201,7 @@
 		struct ast_channel *ast;
 		if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
 			char *passdata;
-			int vmlen = strlen(fromstring) * 3 + 200;
+			int vmlen = strlen(fromstring)*3 + 200;
 			passdata = alloca(vmlen);
 			memset(passdata, 0, vmlen);
 			prep_email_sub_vars(ast, vmu, msgnum + 1, context, mailbox, cidnum, cidname, dur, date, passdata, vmlen, category, flag);
@@ -2282,55 +2269,36 @@
 	return ast_strftime(s, len, "%a %b %e %r UTC %Y", &tm);
 }
 
-static int play_greeting(struct ast_channel *chan, struct ast_vm_user *vmu, char *filename, char *ecodes)
-{
-	int res = -2;
-	
-#ifdef ODBC_STORAGE
-	int success = 
-#endif
-	RETRIEVE(filename, -1, vmu->mailbox, vmu->context);
-	if (ast_fileexists(filename, NULL, NULL) > 0) {
-		res = ast_streamfile(chan, filename, chan->language);
-		if (res > -1) 
-			res = ast_waitstream(chan, ecodes);
-#ifdef ODBC_STORAGE
-		if (success == -1) {
-			/* We couldn't retrieve the file from the database, but we found it on the file system. Let's put it in the database. */
-			ast_debug(1, "Greeting not retrieved from database, but found in file storage. Inserting into database\n");
-			store_file(filename, vmu->mailbox, vmu->context, -1);
-		}
-#endif
-	}
-	DISPOSE(filename, -1);
-
-	return res;
-}
-
-static int invent_message(struct ast_channel *chan, struct ast_vm_user *vmu, char *ext, int busy, char *ecodes)
+static int invent_message(struct ast_channel *chan, char *context, char *ext, int busy, char *ecodes)
 {
 	int res;
 	char fn[PATH_MAX];
 	char dest[PATH_MAX];
 
-	snprintf(fn, sizeof(fn), "%s%s/%s/greet", VM_SPOOL_DIR, vmu->context, ext);
-
-	if ((res = create_dirpath(dest, sizeof(dest), vmu->context, ext, ""))) {
+	snprintf(fn, sizeof(fn), "%s%s/%s/greet", VM_SPOOL_DIR, context, ext);
+
+	if ((res = create_dirpath(dest, sizeof(dest), context, ext, ""))) {
 		ast_log(LOG_WARNING, "Failed to make directory(%s)\n", fn);
 		return -1;
 	}
 
-	res = play_greeting(chan, vmu, fn, ecodes);
-	if (res == -2) {
-		/* File did not exist */
+	RETRIEVE(fn, -1, ext, context);
+	if (ast_fileexists(fn, NULL, NULL) > 0) {
+		res = ast_stream_and_wait(chan, fn, ecodes);
+		if (res) {
+			DISPOSE(fn, -1);
+			return res;
+		}
+	} else {
+		/* Dispose just in case */
+		DISPOSE(fn, -1);
 		res = ast_stream_and_wait(chan, "vm-theperson", ecodes);
 		if (res)
 			return res;
 		res = ast_say_digit_str(chan, ext, ecodes, chan->language);
-	}
-	if (res)
-		return res;
-
+		if (res)
+			return res;
+	}
 	res = ast_stream_and_wait(chan, busy ? "vm-isonphone" : "vm-isunavail", ecodes);
 	return res;
 }
@@ -2366,8 +2334,9 @@
 		"Cust4",
 		"Cust5",
 		"Deleted",
+		"URGENT"
 	};
-	return (id >= 0 && id < (sizeof(msgs) / sizeof(msgs[0]))) ? msgs[id] : "tmp";
+	return (id >= 0 && id < (sizeof(msgs)/sizeof(msgs[0]))) ? msgs[id] : "Unknown";
 }
 #ifdef IMAP_STORAGE
 static int folder_int(const char *folder)
@@ -2568,7 +2537,7 @@
 	char fn[PATH_MAX];
 	char mailbox[256];
 	char *stringp;
-	FILE *p = NULL;
+	FILE *p=NULL;
 	char tmp[80] = "/tmp/astmail-XXXXXX";
 	long len;
 	void *buf;
@@ -2627,8 +2596,8 @@
 	/* read mail file to memory */		
 	len = ftell(p);
 	rewind(p);
-	if (!(buf = ast_malloc(len + 1))) {
-		ast_log(LOG_ERROR, "Can't allocate %ld bytes to read message\n", len + 1);
+	if (!(buf = ast_malloc(len+1))) {
+		ast_log(LOG_ERROR, "Can't allocate %ld bytes to read message\n", len+1);
 		fclose(p);
 		if (tempcopy)
 			*(vmu->email) = '\0';
@@ -2675,30 +2644,30 @@
 		return 0;
 
 	/* We have to get the user before we can open the stream! */
-	/* ast_log(LOG_DEBUG, "Before find_user, context is %s and mailbox is %s\n", context, mailbox); */
+	/* ast_log (LOG_DEBUG,"Before find_user, context is %s and mailbox is %s\n",context,mailbox); */
 	vmu = find_user(&vmus, context, mailbox);
 	if (!vmu) {
-		ast_log(LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailbox, context);
+		ast_log (LOG_ERROR,"Couldn't find mailbox %s in context %s\n",mailbox,context);
 		return -1;
 	} else {
 		/* No IMAP account available */
 		if (vmu->imapuser[0] == '\0') {
-			ast_log(LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
+			ast_log (LOG_WARNING,"IMAP user not set for mailbox %s\n",vmu->mailbox);
 			return -1;
 		}
 	}
 	
 	/* No IMAP account available */
 	if (vmu->imapuser[0] == '\0') {
-		ast_log(LOG_WARNING, "IMAP user not set for mailbox %s\n", vmu->mailbox);
+		ast_log (LOG_WARNING,"IMAP user not set for mailbox %s\n",vmu->mailbox);
 		free_user(vmu);
 		return -1;
  	}
 
 	/* check if someone is accessing this box right now... */
-	vms_p = get_vm_state_by_imapuser(vmu->imapuser, 1);
+	vms_p = get_vm_state_by_imapuser(vmu->imapuser,1);
 	if (!vms_p) {
-		vms_p = get_vm_state_by_mailbox(mailbox, 1);
+		vms_p = get_vm_state_by_mailbox(mailbox,1);
 	}
 	if (vms_p) {
 		ast_debug(3, "Returning before search - user is logged in\n");
@@ -2714,33 +2683,29 @@
 	}
 
 	/* add one if not there... */
-	vms_p = get_vm_state_by_imapuser(vmu->imapuser, 0);
+	vms_p = get_vm_state_by_imapuser(vmu->imapuser,0);
 	if (!vms_p) {
-		vms_p = get_vm_state_by_mailbox(mailbox, 0);
+		vms_p = get_vm_state_by_mailbox(mailbox,0);
 	}
 
 	if (!vms_p) {
-		ast_debug(3, "Adding new vmstate for %s\n", vmu->imapuser);
+		ast_debug(3,"Adding new vmstate for %s\n",vmu->imapuser);
 		if (!(vms_p = ast_calloc(1, sizeof(*vms_p)))) {
 			return -1;
 		}
-		ast_copy_string(vms_p->imapuser, vmu->imapuser, sizeof(vms_p->imapuser));
+		ast_copy_string(vms_p->imapuser,vmu->imapuser, sizeof(vms_p->imapuser));
 		ast_copy_string(vms_p->username, mailbox, sizeof(vms_p->username)); /* save for access from interactive entry point */
 		vms_p->mailstream = NIL; /* save for access from interactive entry point */
-		ast_debug(3, "Copied %s to %s\n", vmu->imapuser, vms_p->imapuser);
+		ast_debug(3, "Copied %s to %s\n",vmu->imapuser,vms_p->imapuser);
 		vms_p->updated = 1;
 		/* set mailbox to INBOX! */
 		ast_copy_string(vms_p->curbox, mbox(fold), sizeof(vms_p->curbox));
 		init_vm_state(vms_p);
 		vmstate_insert(vms_p);
 	}
-	if (fold == 11) /* open INBOX for urgent messages */
-		ret = init_mailstream(vms_p, 0);
-	else
-		ret = init_mailstream(vms_p, fold);
-
+	ret = init_mailstream(vms_p, fold);
 	if (!vms_p->mailstream) {
-		ast_log(LOG_ERROR, "Houston we have a problem - IMAP mailstream is NULL\n");
+		ast_log (LOG_ERROR,"Houston we have a problem - IMAP mailstream is NULL\n");
 		return -1;
 	}
 	if (ret == 0) {
@@ -2797,7 +2762,7 @@
 	if (urgentmsgs)
 		*urgentmsgs = 0;
 
-	ast_debug(3, "Mailbox is set to %s\n", mailbox_context);
+	ast_debug(3,"Mailbox is set to %s\n",mailbox_context);
 	/* If no mailbox, return immediately */
 	if (ast_strlen_zero(mailbox_context))
 		return 0;
@@ -2898,7 +2863,11 @@
 
 	ast_log(LOG_NOTICE, "Copying message from %s@%s to %s@%s\n", vmu->mailbox, vmu->context, recip->mailbox, recip->context);
 
-	create_dirpath(todir, sizeof(todir), recip->context, recip->mailbox, "INBOX");
+	if (strcmp(flag,"URGENT") == 0) { /* If urgent, copy to URGENT folder */
+		create_dirpath(todir, sizeof(todir), recip->context, recip->mailbox, "URGENT");
+	} else {
+		create_dirpath(todir, sizeof(todir), recip->context, recip->mailbox, "INBOX");
+	}
 	
 	if (!dir)
 		make_dir(fromdir, sizeof(fromdir), vmu->context, vmu->mailbox, frombox);
@@ -2930,34 +2899,12 @@
 	return __has_voicemail(context, mailbox, folder, 0);
 }
 
-static int check_if_urgent(char *dir, char *filename)
-{
-	const char *flag;
-	char path[256]; 
-	struct ast_config *msg_cfg;
-	struct ast_flags config_flags = { CONFIG_FLAG_NOCACHE };
-
-	snprintf(path, sizeof(path), "%s/%s", dir, filename);
-	msg_cfg = ast_config_load(path, config_flags);
-        if (!msg_cfg) {
-                ast_log(LOG_WARNING, "No message attribute file?!! (%s)\n", path);
-                return 0;
-        }
-        flag = ast_variable_retrieve(msg_cfg, "message", "flag");
-	if (!ast_strlen_zero(flag) && !strcmp(flag,"URGENT")) {
-		ast_debug(5, "Found URGENT flag %s in file %s\n", flag, path);
-		return 1;
-	}
-	return 0;
-}
-
 static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit)
 {
 	DIR *dir;
 	struct dirent *de;
 	char fn[256];
 	int ret = 0;
-	int urgent = 0, is_urgent = 0;
 
 	/* If no mailbox, return immediately */
 	if (ast_strlen_zero(mailbox))
@@ -2968,12 +2915,6 @@
 	if (ast_strlen_zero(context))
 		context = "default";
 
-	/* check if we are looking for URGENT messages */
-	if (strcmp(folder,"URGENT") == 0) {
-		folder = "INBOX";
-		urgent = 1;
-	}
-
 	snprintf(fn, sizeof(fn), "%s%s/%s/%s", VM_SPOOL_DIR, context, mailbox, folder);
 
 	if (!(dir = opendir(fn)))
@@ -2981,22 +2922,24 @@
 
 	while ((de = readdir(dir))) {
 		if (!strncasecmp(de->d_name, "msg", 3)) {
-			if (shortcircuit && urgent != 1) {
+			if (shortcircuit) {
 				ret = 1;
 				break;
 			} else if (!strncasecmp(de->d_name + 8, "txt", 3)) {
-				is_urgent = check_if_urgent(fn, de->d_name);
-				if (is_urgent == urgent) {	/* count urgent or non-urgent as specified */
-					if (shortcircuit && is_urgent == 1) return 1;
-					ret++;
-				}
+				if (shortcircuit) return 1;
+				ret++;
 			}
 		}
 	}
 
 	closedir(dir);
 
-	return ret;
+	/* If we are checking INBOX, we should check URGENT as well */
+	if (strcmp(folder,"INBOX") == 0) {
+		return (ret + __has_voicemail(context, mailbox, "URGENT", shortcircuit));
+	} else {
+		return ret;
+	}
 }
 
 static int has_voicemail(const char *mailbox, const char *folder)
@@ -3126,6 +3069,8 @@
 #ifdef IMAP_STORAGE
 	int newmsgs, oldmsgs, urgentmsgs;
 	struct vm_state *vms = NULL;
+#else
+	char urgdir[PATH_MAX];
 #endif
 	char txtfile[PATH_MAX], tmptxtfile[PATH_MAX];
 	char callerid[256];
@@ -3245,12 +3190,25 @@
 
 	/* Play the beginning intro if desired */
 	if (!ast_strlen_zero(prefile)) {
-		res = play_greeting(chan, vmu, prefile, ecodes);
-		if (res == -2) {
-			/* The file did not exist */
+#ifdef ODBC_STORAGE
+		int success = 
+#endif
+			RETRIEVE(prefile, -1, ext, context);
+		if (ast_fileexists(prefile, NULL, NULL) > 0) {
+			if (ast_streamfile(chan, prefile, chan->language) > -1) 
+				res = ast_waitstream(chan, ecodes);
+#ifdef ODBC_STORAGE
+			if (success == -1) {
+				/* We couldn't retrieve the file from the database, but we found it on the file system. Let's put it in the database. */
+				ast_debug(1, "Greeting not retrieved from database, but found in file storage. Inserting into database\n");
+				store_file(prefile, vmu->mailbox, vmu->context, -1);
+			}
+#endif
+		} else {
 			ast_debug(1, "%s doesn't exist, doing what we can\n", prefile);
-			res = invent_message(chan, vmu, ext, ast_test_flag(options, OPT_BUSY_GREETING), ecodes);
-		}
+			res = invent_message(chan, vmu->context, ext, ast_test_flag(options, OPT_BUSY_GREETING), ecodes);
+		}
+		DISPOSE(prefile, -1);
 		if (res < 0) {
 			ast_debug(1, "Hang up during prefile playback\n");
 			free_user(vmu);
@@ -3331,10 +3289,10 @@
 		/* must open stream for this user to get info! */
 		res = inboxcount(ext_context, &urgentmsgs, &newmsgs, &oldmsgs);
 		if (res < 0) {
-			ast_log(LOG_NOTICE, "Can not leave voicemail, unable to count messages\n");
+			ast_log(LOG_NOTICE,"Can not leave voicemail, unable to count messages\n");
 			return -1;
 		}
-		if (!(vms = get_vm_state_by_mailbox(ext, 0))) {
+		if (!(vms = get_vm_state_by_mailbox(ext,0))) {
 		/*It is possible under certain circumstances that inboxcount did not create a vm_state when it was needed. This is a catchall which will
 		 * rarely be used*/
 			if (!(vms = ast_calloc(1, sizeof(*vms)))) {
@@ -3345,17 +3303,17 @@
 			ast_copy_string(vms->username, ext, sizeof(vms->username));
 			vms->mailstream = NIL;
 			ast_debug(3, "Copied %s to %s\n", vmu->imapuser, vms->imapuser);
-			vms->updated = 1;
+			vms->updated=1;
 			ast_copy_string(vms->curbox, mbox(0), sizeof(vms->curbox));
 			init_vm_state(vms);
 			vmstate_insert(vms);
-			vms = get_vm_state_by_mailbox(ext, 0);
+			vms = get_vm_state_by_mailbox(ext,0);
 		}
 		vms->newmessages++;
 		
 		/* here is a big difference! We add one to it later */
 		msgnum = newmsgs + oldmsgs;
-		ast_debug(3, "Messagecount set to %d\n", msgnum);
+		ast_debug(3, "Messagecount set to %d\n",msgnum);
 		snprintf(fn, sizeof(fn), "%s/imap/msg%s%04d", VM_SPOOL_DIR, vmu->mailbox, msgnum);
 		/* set variable for compatibility */
 		pbx_builtin_setvar_helper(chan, "VM_MESSAGEFILE", "IMAP_STORAGE");
@@ -3376,7 +3334,7 @@
 		}
 
 #else
-		if (count_messages(vmu, dir, -1) >= vmu->maxmsg) {
+		if (count_messages(vmu, dir) >= vmu->maxmsg) {
 			res = ast_streamfile(chan, "vm-mailboxfull", chan->language);
 			if (!res)
 				res = ast_waitstream(chan, "");
@@ -3452,7 +3410,8 @@
 			fprintf(txt, "flag=%s\n", flag);
 			if (duration < vmminsecs) {
 				fclose(txt);
-				ast_verb(3, "Recording was %d seconds long but needs to be at least %d - abandoning\n", duration, vmminsecs);
+				if (option_verbose > 2) 
+					ast_verbose( VERBOSE_PREFIX_3 "Recording was %d seconds long but needs to be at least %d - abandoning\n", duration, vmminsecs);
 				ast_filedelete(tmptxtfile, NULL);
 				unlink(tmptxtfile);
 				if (ast_check_realtime("voicemail_data")) {
@@ -3526,13 +3485,30 @@
 							free_user(recip);
 						}
 					}
-					/* Notification and disposal needs to happen after the copy, though. */
+					/* Notification needs to happen after the copy, though. */
 					if (ast_fileexists(fn, NULL, NULL)) {
 #ifdef IMAP_STORAGE
 						notify_new_message(chan, vmu, vms, msgnum, duration, fmt, S_OR(chan->cid.cid_num, NULL), S_OR(chan->cid.cid_name, NULL), flag);
 #else
 						notify_new_message(chan, vmu, NULL, msgnum, duration, fmt, S_OR(chan->cid.cid_num, NULL), S_OR(chan->cid.cid_name, NULL), flag);
 #endif
+					}
+#ifndef IMAP_STORAGE
+					if (strcmp(flag,"URGENT") == 0) { /* If this is an URGENT message */
+						/* Move the message from INBOX to URGENT folder if this is urgent! */
+						char sfn[PATH_MAX];
+						char dfn[PATH_MAX];
+						/* It's easier just to try to make it than to check for its existence */
+						create_dirpath(urgdir, sizeof(urgdir), vmu->context, ext, "URGENT");
+						ast_debug(5, "Created an URGENT message, moving file from %s to %s.\n",sfn,dfn);
+						int x = last_message_index(vmu, urgdir) + 1;
+						make_file(sfn, sizeof(sfn), dir, msgnum);
+						make_file(dfn, sizeof(dfn), urgdir, x);
+						RENAME(dir, x, vmu->mailbox, vmu->context, dir, dest, sfn, dfn);
+					}
+#endif
+					/* Disposal needs to happen after the optional move and copy */
+					if (ast_fileexists(fn, NULL, NULL)) {
 						DISPOSE(dir, msgnum);
 					}
 				}
@@ -3561,7 +3537,7 @@
 {
 	/* we know max messages, so stop process when number is hit */
 
-	int x, dest;
+	int x,dest;
 	char sfn[PATH_MAX];
 	char dfn[PATH_MAX];
 
@@ -3648,7 +3624,7 @@
 	} else {

[... 2010 lines stripped ...]



More information about the asterisk-commits mailing list