remove the PBX_ODBC logic from the configure script, and add GENERIC_ODCB logic that...
[asterisk/asterisk.git] / apps / app_voicemail.c
index f888f3e..04ddf1f 100644 (file)
@@ -45,12 +45,10 @@ c-client (http://www.washington.edu/imap/
  ***/
 
 /*** MAKEOPTS
-<category name="MENUSELECT_OPTS_app_voicemail" displayname="Voicemail Build Options" positive_output="yes" remove_on_change="apps/app_voicemail.o apps/app_directory.o">
+<category name="MENUSELECT_OPTS_app_voicemail" displayname="Voicemail Build Options" positive_output="yes" remove_on_change="apps/app_voicemail.o apps/app_voicemail.so apps/app_directory.o apps/app_directory.so">
        <member name="ODBC_STORAGE" displayname="Storage of Voicemail using ODBC">
-               <depend>odbc</depend>
+               <depend>generic_odbc</depend>
                <depend>ltdl</depend>
-               <use>unixodbc</use>
-               <use>iodbc</use>
                <conflict>IMAP_STORAGE</conflict>
                <defaultenabled>no</defaultenabled>
        </member>
@@ -117,6 +115,187 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/res_odbc.h"
 #endif
 
+/*** DOCUMENTATION
+       <application name="VoiceMail" language="en_US">
+               <synopsis>
+                       Leave a Voicemail message.
+               </synopsis>
+               <syntax>
+                       <parameter name="mailboxs" argsep="&amp;" required="true">
+                               <argument name="mailbox1" argsep="@" required="true">
+                                       <argument name="mailbox" required="true" />
+                                       <argument name="context" />
+                               </argument>
+                               <argument name="mailbox2" argsep="@" multiple="true">
+                                       <argument name="mailbox" required="true" />
+                                       <argument name="context" />
+                               </argument>
+                       </parameter>
+                       <parameter name="options">
+                               <optionlist>
+                                       <option name="b">
+                                               <para>Play the <literal>busy</literal> greeting to the calling party.</para>
+                                       </option>
+                                       <option name="d">
+                                               <argument name="c" />
+                                               <para>Accept digits for a new extension in context <replaceable>c</replaceable>,
+                                               if played during the greeting. Context defaults to the current context.</para>
+                                       </option>
+                                       <option name="g">
+                                               <argument name="#" required="true" />
+                                               <para>Use the specified amount of gain when recording the voicemail
+                                               message. The units are whole-number decibels (dB). Only works on supported
+                                               technologies, which is DAHDI only.</para>
+                                       </option>
+                                       <option name="s">
+                                               <para>Skip the playback of instructions for leaving a message to the
+                                               calling party.</para>
+                                       </option>
+                                       <option name="u">
+                                               <para>Play the <literal>unavailable</literal> greeting.</para>
+                                       </option>
+                                       <option name="U">
+                                               <para>Mark message as <literal>URGENT</literal>.</para>
+                                       </option>
+                                       <option name="P">
+                                               <para>Mark message as <literal>PRIORITY</literal>.</para>
+                                       </option>
+                               </optionlist>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>This application allows the calling party to leave a message for the specified
+                       list of mailboxes. When multiple mailboxes are specified, the greeting will be taken from
+                       the first mailbox specified. Dialplan execution will stop if the specified mailbox does not
+                       exist.</para>
+                       <para>The Voicemail application will exit if any of the following DTMF digits are received:</para>
+                       <enumlist>
+                               <enum name="0">
+                                       <para>Jump to the <literal>o</literal> extension in the current dialplan context.</para>
+                               </enum>
+                               <enum name="*">
+                                       <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
+                               </enum>
+                       </enumlist>
+                       <para>This application will set the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="VMSTATUS">
+                                       <para>This indicates the status of the execution of the VoiceMail application.</para>
+                                       <value name="SUCCESS" />
+                                       <value name="USEREXIT" />
+                                       <value name="FAILED" />
+                               </variable>
+                       </variablelist>
+               </description>
+       </application>
+       <application name="VoiceMailMain" language="en_US">
+               <synopsis>
+                       Check Voicemail messages.
+               </synopsis>
+               <syntax>
+                       <parameter name="mailbox" required="true" argsep="@">
+                               <argument name="mailbox" />
+                               <argument name="context" />
+                       </parameter>
+                       <parameter name="options">
+                               <optionlist>
+                                       <option name="p">
+                                               <para>Consider the <replaceable>mailbox</replaceable> parameter as a prefix to
+                                               the mailbox that is entered by the caller.</para>
+                                       </option>
+                                       <option name="g">
+                                               <argument name="#" required="true" />
+                                               <para>Use the specified amount of gain when recording a voicemail message.
+                                               The units are whole-number decibels (dB).</para>
+                                       </option>
+                                       <option name="s">
+                                               <para>Skip checking the passcode for the mailbox.</para>
+                                       </option>
+                                       <option name="a">
+                                               <argument name="folder" required="true" />
+                                               <para>Skip folder prompt and go directly to <replaceable>folder</replaceable> specified.
+                                               Defaults to <literal>INBOX</literal>.</para>
+                                       </option>
+                               </optionlist>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>This application allows the calling party to check voicemail messages. A specific
+                       <replaceable>mailbox</replaceable>, and optional corresponding <replaceable>context</replaceable>,
+                       may be specified. If a <replaceable>mailbox</replaceable> is not provided, the calling party will
+                       be prompted to enter one. If a <replaceable>context</replaceable> is not specified, the
+                       <literal>default</literal> context will be used.</para>
+               </description>
+       </application>
+       <application name="MailboxExists" language="en_US">
+               <synopsis>
+                       Check to see if Voicemail mailbox exists.
+               </synopsis>
+               <syntax>
+                       <parameter name="mailbox" required="true" argsep="@">
+                               <argument name="mailbox" required="true" />
+                               <argument name="context" />
+                       </parameter>
+                       <parameter name="options">
+                               <para>None options.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Check to see if the specified <replaceable>mailbox</replaceable> exists. If no voicemail
+                       <replaceable>context</replaceable> is specified, the <literal>default</literal> context
+                       will be used.</para>
+                       <para>This application will set the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="VMBOXEXISTSSTATUS">
+                                       <para>This will contain the status of the execution of the MailboxExists application.
+                                       Possible values include:</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                               </variable>
+                       </variablelist>
+               </description>
+       </application>
+       <application name="VMAuthenticate" language="en_US">
+               <synopsis>
+                       Authenticate with Voicemail passwords.
+               </synopsis>
+               <syntax>
+                       <parameter name="mailbox" required="true" argsep="@">
+                               <argument name="mailbox" />
+                               <argument name="context" />
+                       </parameter>
+                       <parameter name="options">
+                               <optionlist>
+                                       <option name="s">
+                                               <para>Skip playing the initial prompts.</para>
+                                       </option>
+                               </optionlist>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>This application behaves the same way as the Authenticate application, but the passwords
+                       are taken from <filename>voicemail.conf</filename>. If the <replaceable>mailbox</replaceable> is
+                       specified, only that mailbox's password will be considered valid. If the <replaceable>mailbox</replaceable>
+                       is not specified, the channel variable <variable>AUTH_MAILBOX</variable> will be set with the authenticated
+                       mailbox.</para>
+               </description>
+       </application>
+       <function name="MAILBOX_EXISTS" language="en_US">
+               <synopsis>
+                       Tell if a mailbox is configured.
+               </synopsis>
+               <syntax argsep="@">
+                       <parameter name="mailbox" required="true" />
+                       <parameter name="context" />
+               </syntax>
+               <description>
+                       <para>Returns a boolean of whether the corresponding <replaceable>mailbox</replaceable> exists.
+                       If <replaceable>context</replaceable> is not specified, defaults to the <literal>default</literal>
+                       context.</para>
+               </description>
+       </function>
+ ***/
+
 #ifdef IMAP_STORAGE
 static char imapserver[48];
 static char imapport[8];
@@ -153,6 +332,7 @@ static void mm_parsequota (MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pq
 static void imap_mailbox_name(char *spec, size_t len, struct vm_state *vms, int box, int target);
 static int imap_store_file(char *dir, char *mailboxuser, char *mailboxcontext, int msgnum, struct ast_channel *chan, struct ast_vm_user *vmu, char *fmt, int duration, struct vm_state *vms, const char *flag);
 static void update_messages_by_imapuser(const char *user, unsigned long number);
+static int vm_delete(char *file);
 
 static int imap_remove_file (char *dir, int msgnum);
 static int imap_retrieve_file (const char *dir, const int msgnum, const char *mailbox, const char *context);
@@ -225,6 +405,7 @@ static AST_LIST_HEAD_STATIC(vmstates, vmstate);
 #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 VM_MESSAGEWRAP   (1 << 17)  /*!< Wrap around from the last message to the first, and vice-versa */
+#define VM_FWDURGAUTO    (1 << 18)  /*!< Autoset of Urgent flag on forwarded Urgent messages set globally */
 #define ERROR_LOCK_PATH  -100
 
 
@@ -496,77 +677,6 @@ static char userscontext[AST_MAX_EXTENSION] = "default";
 
 static char *addesc = "Comedian Mail";
 
-static char *synopsis_vm = "Leave a Voicemail message";
-
-static char *descrip_vm =
-       "  VoiceMail(mailbox[@context][&mailbox[@context]][...][,options]): This\n"
-       "application allows the calling party to leave a message for the specified\n"
-       "list of mailboxes. When multiple mailboxes are specified, the greeting will\n"
-       "be taken from the first mailbox specified. Dialplan execution will stop if the\n"
-       "specified mailbox does not exist.\n"
-       "  The Voicemail application will exit if any of the following DTMF digits are\n"
-       "received:\n"
-       "    0 - Jump to the 'o' extension in the current dialplan context.\n"
-       "    * - Jump to the 'a' extension in the current dialplan context.\n"
-       "  This application will set the following channel variable upon completion:\n"
-       "    VMSTATUS - This indicates the status of the execution of the VoiceMail\n"
-       "               application. The possible values are:\n"
-       "               SUCCESS | USEREXIT | FAILED\n\n"
-       "  Options:\n"
-       "    b    - Play the 'busy' greeting to the calling party.\n"
-       "    d([c]) - Accept digits for a new extension in context c, if played during\n"
-       "             the greeting.  Context defaults to the current context.\n"
-       "    g(#) - Use the specified amount of gain when recording the voicemail\n"
-       "           message. The units are whole-number decibels (dB).\n"
-       "           Only works on supported technologies, which is DAHDI only.\n"
-       "    s    - Skip the playback of instructions for leaving a message to the\n"
-       "           calling party.\n"
-       "    u    - Play the 'unavailable' greeting.\n"
-       "    U    - Mark message as Urgent.\n"
-       "    P    - Mark message as PRIORITY.\n";
-
-static char *synopsis_vmain = "Check Voicemail messages";
-
-static char *descrip_vmain =
-       "  VoiceMailMain([mailbox][@context][,options]): This application allows the\n"
-       "calling party to check voicemail messages. A specific mailbox, and optional\n"
-       "corresponding context, may be specified. If a mailbox is not provided, the\n"
-       "calling party will be prompted to enter one. If a context is not specified,\n"
-       "the 'default' context will be used.\n\n"
-       "  Options:\n"
-       "    p    - Consider the mailbox parameter as a prefix to the mailbox that\n"
-       "           is entered by the caller.\n"
-       "    g(#) - Use the specified amount of gain when recording a voicemail\n"
-       "           message. The units are whole-number decibels (dB).\n"
-       "    s    - Skip checking the passcode for the mailbox.\n"
-       "    a(#) - Skip folder prompt and go directly to folder specified.\n"
-       "           Defaults to INBOX\n";
-
-static char *synopsis_vm_box_exists =
-"Check to see if Voicemail mailbox exists";
-
-static char *descrip_vm_box_exists =
-       "  MailboxExists(mailbox[@context][,options]): Check to see if the specified\n"
-       "mailbox exists. If no voicemail context is specified, the 'default' context\n"
-       "will be used.\n"
-       "  This application will set the following channel variable upon completion:\n"
-       "    VMBOXEXISTSSTATUS - This will contain the status of the execution of the\n"
-       "                        MailboxExists application. Possible values include:\n"
-       "                        SUCCESS | FAILED\n\n"
-       "  Options: (none)\n";
-
-static char *synopsis_vmauthenticate = "Authenticate with Voicemail passwords";
-
-static char *descrip_vmauthenticate =
-       "  VMAuthenticate([mailbox][@context][,options]): This application behaves the\n"
-       "same way as the Authenticate application, but the passwords are taken from\n"
-       "voicemail.conf.\n"
-       "  If the mailbox is specified, only that mailbox's password will be considered\n"
-       "valid. If the mailbox is not specified, the channel variable AUTH_MAILBOX will\n"
-       "be set with the authenticated mailbox.\n\n"
-       "  Options:\n"
-       "    s - Skip playing the initial prompts.\n";
-
 /* Leave a message */
 static char *app = "VoiceMail";
 
@@ -578,6 +688,7 @@ static char *app4 = "VMAuthenticate";
 
 static AST_LIST_HEAD_STATIC(users, ast_vm_user);
 static AST_LIST_HEAD_STATIC(zones, vm_zone);
+static char zonetag[80];
 static int maxsilence;
 static int maxmsg;
 static int maxdeletedmsg;
@@ -735,6 +846,7 @@ static void populate_defaults(struct ast_vm_user *vmu)
        ast_copy_string(vmu->callback, callcontext, sizeof(vmu->callback));
        ast_copy_string(vmu->dialout, dialcontext, sizeof(vmu->dialout));
        ast_copy_string(vmu->exit, exitcontext, sizeof(vmu->exit));
+       ast_copy_string(vmu->zonetag, zonetag, sizeof(vmu->zonetag));
        if (vmmaxsecs)
                vmu->maxsecs = vmmaxsecs;
        if (maxmsg)
@@ -1309,7 +1421,7 @@ static const char *mbox(int id)
                "Deleted",
                "Urgent"
        };
-       return (id >= 0 && id < (sizeof(msgs)/sizeof(msgs[0]))) ? msgs[id] : "Unknown";
+       return (id >= 0 && id < ARRAY_LEN(msgs)) ? msgs[id] : "Unknown";
 }
 
 static void free_user(struct ast_vm_user *vmu)
@@ -1348,7 +1460,9 @@ static void vm_imap_delete(int msgnum, struct ast_vm_user *vmu)
                ast_log(LOG_DEBUG, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum);
        /* delete message */
        snprintf (arg, sizeof(arg), "%lu",messageNum);
+       ast_mutex_lock(&vms->lock);
        mail_setflag (vms->mailstream,arg,"\\DELETED");
+       ast_mutex_unlock(&vms->lock);
 }
 
 static int imap_retrieve_greeting (const char *dir, const int msgnum, struct ast_vm_user *vmu)
@@ -1382,10 +1496,12 @@ static int imap_retrieve_greeting (const char *dir, const int msgnum, struct ast
        
        /* Greetings will never have a prepended message */
        *vms_p->introfn = '\0';
-       
+
+       ast_mutex_lock(&vms_p->lock);
        ret = init_mailstream(vms_p, GREETINGS_FOLDER);
        if (!vms_p->mailstream) {
                ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL\n");
+               ast_mutex_unlock(&vms_p->lock);
                return -1;
        }
 
@@ -1397,6 +1513,7 @@ static int imap_retrieve_greeting (const char *dir, const int msgnum, struct ast
                        attachment = ast_strdupa(body->nested.part->next->body.parameter->value);
                } else {
                        ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n");
+                       ast_mutex_unlock(&vms_p->lock);
                        return -1;
                }
                filename = strsep(&attachment, ".");
@@ -1404,9 +1521,11 @@ static int imap_retrieve_greeting (const char *dir, const int msgnum, struct ast
                        ast_copy_string(vms_p->fn, dir, sizeof(vms_p->fn));
                        vms_p->msgArray[vms_p->curmsg] = i + 1;
                        save_body(body, vms_p, "2", attachment, 0);
+                       ast_mutex_unlock(&vms_p->lock);
                        return 0;
                }
        }
+       ast_mutex_unlock(&vms_p->lock);
 
        return -1;
 }
@@ -1472,7 +1591,9 @@ static int imap_retrieve_file(const char *dir, const int msgnum, const char *mai
        }
 
        /* This will only work for new messages... */
+       ast_mutex_lock(&vms->lock);
        header_content = mail_fetchheader (vms->mailstream, vms->msgArray[msgnum]);
+       ast_mutex_unlock(&vms->lock);
        /* 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]);
@@ -1480,8 +1601,10 @@ static int imap_retrieve_file(const char *dir, const int msgnum, const char *mai
                goto exit;
        }
 
+       ast_mutex_lock(&vms->lock);
        mail_fetchstructure (vms->mailstream,vms->msgArray[msgnum],&body);
-       
+       ast_mutex_unlock(&vms->lock);
+
        /* 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);
@@ -1588,6 +1711,12 @@ static int messagecount(const char *context, const char *mailbox, const char *fo
        int fold = folder_int(folder);
        int urgent = 0;
        
+       /* If URGENT, then look at INBOX */
+       if (fold == 11) {
+               fold = NEW_FOLDER;
+               urgent = 1;
+       }
+
        if (ast_strlen_zero(mailbox))
                return 0;
 
@@ -1635,25 +1764,8 @@ static int messagecount(const char *context, const char *mailbox, const char *fo
                vms_p = get_vm_state_by_mailbox(mailbox, context, 0);
        }
 
-       /* If URGENT, then look at INBOX */
-       if (fold == 11) {
-               fold = NEW_FOLDER;
-               urgent = 1;
-       }
-
        if (!vms_p) {
-               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->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);
-               vms_p->updated = 1;
-               ast_copy_string(vms_p->curbox, mbox(fold), sizeof(vms_p->curbox));
-               init_vm_state(vms_p);
-               vmstate_insert(vms_p);
+               vms_p = create_vm_state_from_user(vmu);
        }
        ret = init_mailstream(vms_p, fold);
        if (!vms_p->mailstream) {
@@ -1661,6 +1773,7 @@ static int messagecount(const char *context, const char *mailbox, const char *fo
                return -1;
        }
        if (ret == 0) {
+               ast_mutex_lock(&vms_p->lock);
                pgm = mail_newsearchpgm ();
                hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)mailbox);
                pgm->header = hdr;
@@ -1693,10 +1806,13 @@ static int messagecount(const char *context, const char *mailbox, const char *fo
                        vms_p->urgentmessages = vms_p->vmArrayIndex;
                /*Freeing the searchpgm also frees the searchhdr*/
                mail_free_searchpgm(&pgm);
+               ast_mutex_unlock(&vms_p->lock);
                vms_p->updated = 0;
                return vms_p->vmArrayIndex;
-       } else {  
+       } else {
+               ast_mutex_lock(&vms_p->lock);
                mail_ping(vms_p->mailstream);
+               ast_mutex_unlock(&vms_p->lock);
        }
        return 0;
 }
@@ -1783,14 +1899,21 @@ static int imap_store_file(char *dir, char *mailboxuser, char *mailboxcontext, i
                        *(vmu->email) = '\0';
                return -1;
        }
-       fread(buf, len, 1, p);
+       if (fread(buf, len, 1, p) < len) {
+               if (ferror(p)) {
+                       ast_log(LOG_ERROR, "Short read while reading in mail file.\n");
+                       return -1;
+               }
+       }
        ((char *)buf)[len] = '\0';
        INIT(&str, mail_string, buf, len);
        ret = init_mailstream(vms, NEW_FOLDER);
        if (ret == 0) {
                imap_mailbox_name(mailbox, sizeof(mailbox), vms, NEW_FOLDER, 1);
+               ast_mutex_lock(&vms->lock);
                if(!mail_append_full(vms->mailstream, mailbox, imap_flags, NIL, &str))
                        ast_log(LOG_ERROR, "Error while sending the message to %s\n", mailbox);
+               ast_mutex_unlock(&vms->lock);
                fclose(p);
                unlink(tmp);
                ast_free(buf);
@@ -1954,8 +2077,12 @@ static int copy_message(struct ast_channel *chan, struct ast_vm_user *vmu, int i
                return -1;
        }
        snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]);
-       if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(imbox)) == T))
+       ast_mutex_lock(&sendvms->lock);
+       if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(imbox)) == T)) {
+               ast_mutex_unlock(&sendvms->lock);
                return 0;
+       }
+       ast_mutex_unlock(&sendvms->lock);
        ast_log(LOG_WARNING, "Unable to copy message from mailbox %s to mailbox %s\n", vmu->mailbox, recip->mailbox);
        return -1;
 }
@@ -2091,6 +2218,7 @@ static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
                check_quota(vms,(char *)mbox(box));
        }
 
+       ast_mutex_lock(&vms->lock);
        pgm = mail_newsearchpgm();
 
        /* Check IMAP folder for Asterisk messages only... */
@@ -2122,6 +2250,7 @@ static int open_mailbox(struct vm_state *vms, struct ast_vm_user *vmu, int box)
        vms->lastmsg = vms->vmArrayIndex - 1;
        mail_free_searchpgm(&pgm);
 
+       ast_mutex_unlock(&vms->lock);
        return 0;
 }
 
@@ -2130,7 +2259,11 @@ static void write_file(char *filename, char *buffer, unsigned long len)
        FILE *output;
 
        output = fopen (filename, "w");
-       fwrite (buffer, len, 1, output);
+       if (fwrite(buffer, len, 1, output) != 1) {
+               if (ferror(output)) {
+                       ast_log(LOG_ERROR, "Short write while writing e-mail body: %s.\n", strerror(errno));
+               }
+       }
        fclose (output);
 }
 
@@ -2608,7 +2741,9 @@ static int save_body(BODY *body, struct vm_state *vms, char *section, char *form
        if (!body || body == NIL)
                return -1;
 
+       ast_mutex_lock(&vms->lock);
        body_content = mail_fetchbody(vms->mailstream, vms->msgArray[vms->curmsg], section, &len);
+       ast_mutex_unlock(&vms->lock);
        if (body_content != NIL) {
                snprintf(filename, sizeof(filename), "%s.%s", fn, format);
                /* ast_debug(1,body_content); */
@@ -2631,6 +2766,7 @@ static int save_body(BODY *body, struct vm_state *vms, char *section, char *form
  *
  * Determines the delimiter character that is used by the underlying IMAP based mail store.
  */
+/* MUTEX should already be held */
 static void get_mailbox_delimiter(MAILSTREAM *stream) {
        char tmp[50];
        snprintf(tmp, sizeof(tmp), "{%s}", imapserver);
@@ -2645,6 +2781,7 @@ static void get_mailbox_delimiter(MAILSTREAM *stream) {
  * Calls imap_getquotaroot, which will populate its results into the vm_state vms input structure.
  */
 static void check_quota(struct vm_state *vms, char *mailbox) {
+       ast_mutex_lock(&vms->lock);
        mail_parameters(NULL, SET_QUOTA, (void *) mm_parsequota);
        ast_debug(3, "Mailbox name set to: %s, about to check quotas\n", mailbox);
        if (vms && vms->mailstream != NULL) {
@@ -2652,6 +2789,7 @@ static void check_quota(struct vm_state *vms, char *mailbox) {
        } else {
                ast_log(AST_LOG_WARNING, "Mailstream not available for mailbox: %s\n", mailbox);
        }
+       ast_mutex_unlock(&vms->lock);
 }
 
 #endif /* IMAP_STORAGE */
@@ -2842,7 +2980,9 @@ static int retrieve_file(char *dir, int msgnum)
                                                        }
                                                }
                                        }
-                                       truncate(full_fn, fdlen);
+                                       if (truncate(full_fn, fdlen) < 0) {
+                                               ast_log(LOG_WARNING, "Unable to truncate '%s': %s\n", full_fn, strerror(errno));
+                                       }
                                }
                        } else {
                                res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), NULL);
@@ -3510,6 +3650,7 @@ static void copy_plain_file(char *frompath, char *topath)
        copy(frompath2, topath2);
        ast_variables_destroy(var);
 }
+#endif
 
 /*! 
  * \brief Removes the voicemail sound and information file.
@@ -3536,7 +3677,6 @@ static int vm_delete(char *file)
        unlink(txt);
        return ast_filedelete(file, NULL);
 }
-#endif
 
 /*!
  * \brief utility used by inchar(), for base_encode()
@@ -3870,7 +4010,7 @@ static void make_email_file(FILE *p, char *srcemail, struct ast_vm_user *vmu, in
 
        if (!ast_strlen_zero(fromstring)) {
                struct ast_channel *ast;
-               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
+               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Substitution/voicemail"))) {
                        char *ptr;
                        memset(passdata2, 0, len_passdata2);
                        prep_email_sub_vars(ast, vmu, msgnum + 1, context, mailbox, enc_cidnum, enc_cidname, dur, date, passdata2, len_passdata2, category, flag);
@@ -3914,7 +4054,7 @@ static void make_email_file(FILE *p, char *srcemail, struct ast_vm_user *vmu, in
        }
        if (!ast_strlen_zero(emailsubject)) {
                struct ast_channel *ast;
-               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
+               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Substitution/voicemail"))) {
                        int vmlen = strlen(emailsubject) * 3 + 200;
                        /* Only allocate more space if the previous was not large enough */
                        if (vmlen > len_passdata) {
@@ -3999,7 +4139,7 @@ static void make_email_file(FILE *p, char *srcemail, struct ast_vm_user *vmu, in
        fprintf(p, "Content-Type: text/plain; charset=%s" ENDL "Content-Transfer-Encoding: 8bit" ENDL ENDL, charset);
        if (emailbody) {
                struct ast_channel *ast;
-               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
+               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Substitution/voicemail"))) {
                        char *passdata;
                        int vmlen = strlen(emailbody)*3 + 200;
                        passdata = alloca(vmlen);
@@ -4143,7 +4283,7 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *context, char
 
        if (*pagerfromstring) {
                struct ast_channel *ast;
-               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
+               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Substitution/voicemail"))) {
                        char *passdata;
                        int vmlen = strlen(fromstring)*3 + 200;
                        passdata = alloca(vmlen);
@@ -4159,7 +4299,7 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *context, char
        fprintf(p, "To: %s\n", pager);
        if (pagersubject) {
                struct ast_channel *ast;
-               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
+               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Substitution/voicemail"))) {
                        char *passdata;
                        int vmlen = strlen(pagersubject) * 3 + 200;
                        passdata = alloca(vmlen);
@@ -4181,7 +4321,7 @@ static int sendpage(char *srcemail, char *pager, int msgnum, char *context, char
        ast_strftime(date, sizeof(date), "%A, %B %d, %Y at %r", &tm);
        if (pagerbody) {
                struct ast_channel *ast;
-               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0))) {
+               if ((ast = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Substitution/voicemail"))) {
                        char *passdata;
                        int vmlen = strlen(pagerbody) * 3 + 200;
                        passdata = alloca(vmlen);
@@ -5199,6 +5339,21 @@ static int leave_voicemail(struct ast_channel *chan, char *ext, struct leave_vm_
                ast_log(AST_LOG_WARNING, "No format for saving voicemail?\n");
 leave_vm_out:
        free_user(vmu);
+
+#ifdef IMAP_STORAGE
+       /* expunge message - use UID Expunge if supported on IMAP server*/
+       ast_debug(3, "*** Checking if we can expunge, expungeonhangup set to %d\n",expungeonhangup);
+       if (expungeonhangup == 1) {
+               ast_mutex_lock(&vms->lock);
+#ifdef HAVE_IMAP_TK2006
+               if (LEVELUIDPLUS (vms->mailstream)) {
+                       mail_expunge_full(vms->mailstream,NIL,EX_UID);
+               } else 
+#endif
+                       mail_expunge(vms->mailstream);
+               ast_mutex_unlock(&vms->lock);
+       }
+#endif
        
        return res;
 }
@@ -5217,17 +5372,22 @@ static int save_to_folder(struct ast_vm_user *vmu, struct vm_state *vms, int msg
        /* simple. huh? */
        char sequence[10];
        char mailbox[256];
+       int res;
+
        /* get the real IMAP message number for this message */
        snprintf(sequence, sizeof(sequence), "%ld", vms->msgArray[msg]);
        
        ast_debug(3, "Copying sequence %s to mailbox %s\n", sequence, mbox(box));
+       ast_mutex_lock(&vms->lock);
        if (box == OLD_FOLDER) {
                mail_setflag(vms->mailstream, sequence, "\\Seen");
        } else if (box == NEW_FOLDER) {
                mail_clearflag(vms->mailstream, sequence, "\\Seen");
        }
-       if (!strcasecmp(mbox(NEW_FOLDER), vms->curbox) && (box == NEW_FOLDER || box == OLD_FOLDER))
+       if (!strcasecmp(mbox(NEW_FOLDER), vms->curbox) && (box == NEW_FOLDER || box == OLD_FOLDER)) {
+               ast_mutex_unlock(&vms->lock);
                return 0;
+       }
        /* Create the folder if it don't exist */
        imap_mailbox_name(mailbox, sizeof(mailbox), vms, box, 1); /* Get the full mailbox name */
        ast_debug(5, "Checking if folder exists: %s\n",mailbox);
@@ -5235,7 +5395,9 @@ static int save_to_folder(struct ast_vm_user *vmu, struct vm_state *vms, int msg
                ast_debug(5, "Folder exists.\n");
        else
                ast_log(AST_LOG_NOTICE, "Folder %s created!\n",mbox(box));
-       return !mail_copy(vms->mailstream, sequence, (char *)mbox(box));
+       res = !mail_copy(vms->mailstream, sequence, (char *)mbox(box));
+       ast_mutex_unlock(&vms->lock);
+       return res;
 #else
        char *dir = vms->curdir;
        char *username = vms->username;
@@ -6072,6 +6234,14 @@ static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu,
        manager_event(EVENT_FLAG_CALL, "MessageWaiting", "Mailbox: %s@%s\r\nWaiting: %d\r\nNew: %d\r\nOld: %d\r\n", vmu->mailbox, vmu->context, ast_app_has_voicemail(ext_context, NULL), newmsgs, oldmsgs);
        run_externnotify(vmu->context, vmu->mailbox, flag);
 
+#ifdef IMAP_STORAGE
+       vm_delete(fn);  /* Delete the file, but not the IMAP message */
+       if (ast_test_flag(vmu, VM_DELETE))  { /* Delete the IMAP message if delete = yes */
+               vm_imap_delete(vms->curmsg, vmu);
+               vms->newmessages--;  /* Fix new message count */
+       }
+#endif
+
        return 0;
 }
 
@@ -6122,7 +6292,9 @@ static int forward_message(struct ast_channel *chan, char *context, struct vm_st
        char urgent_str[7] = "";
        char tmptxtfile[PATH_MAX];
 
-       ast_copy_string(urgent_str, urgent ? "Urgent" : "", sizeof(urgent_str));
+       if (ast_test_flag((&globalflags), VM_FWDURGAUTO)) {
+               ast_copy_string(urgent_str, urgent ? "Urgent" : "", sizeof(urgent_str));
+       }
 
        if (vms == NULL) return -1;
        dir = vms->curdir;
@@ -6177,7 +6349,6 @@ static int forward_message(struct ast_channel *chan, char *context, struct vm_st
                        int old_priority;
                        struct ast_app* directory_app;
 
-                       
                        directory_app = pbx_findapp("Directory");
                        if (directory_app) {
                                char vmcontext[256];
@@ -6187,7 +6358,7 @@ static int forward_message(struct ast_channel *chan, char *context, struct vm_st
                                old_priority = chan->priority;
                                
                                /* call the the Directory, changes the channel */
-                               snprintf(vmcontext, sizeof(vmcontext), "%s||v", context ? context : "default");
+                               snprintf(vmcontext, sizeof(vmcontext), "%s,,v", context ? context : "default");
                                res = pbx_exec(chan, directory_app, vmcontext);
                                
                                ast_copy_string(username, chan->exten, sizeof(username));
@@ -6196,7 +6367,6 @@ static int forward_message(struct ast_channel *chan, char *context, struct vm_st
                                memcpy(chan->context, old_context, sizeof(chan->context));
                                memcpy(chan->exten, old_exten, sizeof(chan->exten));
                                chan->priority = old_priority;
-                               
                        } else {
                                ast_log(AST_LOG_WARNING, "Could not find the Directory application, disabling directory_forward\n");
                                ast_clear_flag((&globalflags), VM_DIRECFORWARD);
@@ -6222,6 +6392,14 @@ static int forward_message(struct ast_channel *chan, char *context, struct vm_st
                                AST_LIST_INSERT_HEAD(&extensions, receiver, list);
                                found++;
                        } else {
+                               /* XXX Optimization for the future.  When we encounter a single bad extension,
+                                * bailing out on all of the extensions may not be the way to go.  We should
+                                * probably just bail on that single extension, then allow the user to enter
+                                * several more. XXX
+                                */
+                               while ((receiver = AST_LIST_REMOVE_HEAD(&extensions, list))) {
+                                       free_user(receiver);
+                               }
                                valid_extensions = 0;
                                break;
                        }
@@ -6236,10 +6414,9 @@ static int forward_message(struct ast_channel *chan, char *context, struct vm_st
                                        return res;
                                }
                        } else {
-                               /* Dispose just in case */
-                               DISPOSE(fn, -1);
                                res = ast_say_digit_str(chan, s, ecodes, chan->language);
                        }
+                       DISPOSE(fn, -1);
 
                        s = strsep(&stringp, "*");
                }
@@ -6724,7 +6901,8 @@ static int imap_delete_old_greeting (char *dir, struct vm_state *vms)
                ast_log(AST_LOG_ERROR, "Failed to procure file name from directory passed. You should never see this.\n");
                return -1;
        }
-       
+
+       ast_mutex_lock(&vms->lock);
        for (i = 0; i < vms->mailstream->nmsgs; i++) {
                mail_fetchstructure(vms->mailstream, i + 1, &body);
                /* We have the body, now we extract the file name of the first attachment. */
@@ -6732,6 +6910,7 @@ static int imap_delete_old_greeting (char *dir, struct vm_state *vms)
                        attachment = ast_strdupa(body->nested.part->next->body.parameter->value);
                } else {
                        ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n");
+                       ast_mutex_unlock(&vms->lock);
                        return -1;
                }
                filename = strsep(&attachment, ".");
@@ -6741,6 +6920,7 @@ static int imap_delete_old_greeting (char *dir, struct vm_state *vms)
                }
        }
        mail_expunge(vms->mailstream);
+       ast_mutex_unlock(&vms->lock);
        return 0;
 }
 
@@ -8185,7 +8365,7 @@ static int vm_options(struct ast_channel *chan, struct ast_vm_user *vmu, struct
                                if (cmd < 0)
                                        break;
 
-                               if ((cmd = ast_readstring(chan,newpassword2 + strlen(newpassword2),sizeof(newpassword2)-1,2000,10000,"#"))) {
+                               if ((cmd = ast_readstring(chan,newpassword2 + strlen(newpassword2),sizeof(newpassword2)-1,2000,10000,"#")) < 0) {
                                        break;
                                }
                        }
@@ -9297,12 +9477,14 @@ out:
        /* expunge message - use UID Expunge if supported on IMAP server*/
        ast_debug(3, "*** Checking if we can expunge, deleted set to %d, expungeonhangup set to %d\n",deleted,expungeonhangup);
        if (vmu && deleted == 1 && expungeonhangup == 1 && vms.mailstream != NULL) {
+               ast_mutex_lock(&vms.lock);
 #ifdef HAVE_IMAP_TK2006
                if (LEVELUIDPLUS (vms.mailstream)) {
                        mail_expunge_full(vms.mailstream,NIL,EX_UID);
                } else 
 #endif
                        mail_expunge(vms.mailstream);
+               ast_mutex_unlock(&vms.lock);
        }
        /*  before we delete the state, we should copy pertinent info
         *  back to the persistent model */
@@ -9499,11 +9681,6 @@ static int acf_mailbox_exists(struct ast_channel *chan, const char *cmd, char *a
 
 static struct ast_custom_function mailbox_exists_acf = {
        .name = "MAILBOX_EXISTS",
-       .synopsis = "Tell if a mailbox is configured",
-       .desc =
-"Returns a boolean of whether the corresponding mailbox exists.  If context\n"
-"is not specified, defaults to the \"default\" context.\n",
-       .syntax = "MAILBOX_EXISTS(<vmbox>[@<context>])",
        .read = acf_mailbox_exists,
 };
 
@@ -9577,6 +9754,8 @@ static char *show_users_realtime(int fd, const char *context)
                "=============================================================\n"
                "\n");
 
+       ast_config_destroy(cfg);
+
        return CLI_SUCCESS;
 }
 
@@ -10053,6 +10232,48 @@ static void free_vm_zones(void)
        AST_LIST_UNLOCK(&zones);
 }
 
+static const char *substitute_escapes(const char *value)
+{
+       char *current;
+
+       /* Add 16 for fudge factor */
+       struct ast_str *str = ast_str_thread_get(&global_app_buf, strlen(value) + 16);
+
+       /* Substitute strings \r, \n, and \t into the appropriate characters */
+       for (current = (char *) value; *current; current++) {
+               if (*current == '\\') {
+                       current++;
+                       if (!*current) {
+                               ast_log(AST_LOG_NOTICE, "Incomplete escape at end of value.\n");
+                               break;
+                       }
+                       switch (*current) {
+                       case 'r':
+                               ast_str_append(&str, 0, "\r");
+                               break;
+                       case 'n':
+#ifdef IMAP_STORAGE
+                               if (!str->used || str->str[str->used - 1] != '\r') {
+                                       ast_str_append(&str, 0, "\r");
+                               }
+#endif
+                               ast_str_append(&str, 0, "\n");
+                               break;
+                       case 't':
+                               ast_str_append(&str, 0, "\t");
+                               break;
+                       default:
+                               ast_log(AST_LOG_NOTICE, "Substitution routine does not support this character: \\%c\n", *current);
+                               break;
+                       }
+               } else {
+                       ast_str_append(&str, 0, "%c", *current);
+               }
+       }
+
+       return ast_str_buffer(str);
+}
+
 static int load_config(int reload)
 {
        struct ast_vm_user *current;
@@ -10478,6 +10699,12 @@ static int load_config(int reload)
                }
                ast_set2_flag((&globalflags), ast_true(val), VM_MOVEHEARD);     
 
+               if (!(val = ast_variable_retrieve(cfg, "general", "forward_urgent_auto"))) {
+                       ast_debug(1,"Autoset of Urgent flag on forwarded Urgent messages disabled globally\n");
+                       val = "no";
+               }
+               ast_set2_flag((&globalflags), ast_true(val), VM_FWDURGAUTO);    
+
                if (!(val = ast_variable_retrieve(cfg, "general", "sayduration"))) {
                        ast_debug(1,"Duration info before msg enabled globally\n");
                        val = "yes";
@@ -10657,61 +10884,20 @@ static int load_config(int reload)
                                adsiver = atoi(val);
                        }
                }
-               if ((val = ast_variable_retrieve(cfg, "general", "emailsubject")))
+               if ((val = ast_variable_retrieve(cfg, "general", "tz"))) {
+                       ast_copy_string(zonetag, val, sizeof(zonetag));
+               }
+               if ((val = ast_variable_retrieve(cfg, "general", "emailsubject"))) {
                        emailsubject = ast_strdup(val);
+               }
                if ((val = ast_variable_retrieve(cfg, "general", "emailbody"))) {
-                       char *tmpread, *tmpwrite;
-                       emailbody = ast_strdup(val);
-
-                       /* substitute strings \t and \n into the appropriate characters */
-                       tmpread = tmpwrite = emailbody;
-                       while ((tmpwrite = strchr(tmpread,'\\'))) {
-                               switch (tmpwrite[1]) {
-                               case 'r':
-                                       memmove(tmpwrite + 1, tmpwrite + 2, strlen(tmpwrite + 2) + 1);
-                                       *tmpwrite = '\r';
-                                       break;
-                               case 'n':
-                                       memmove(tmpwrite + 1, tmpwrite + 2, strlen(tmpwrite + 2) + 1);
-                                       *tmpwrite = '\n';
-                                       break;
-                               case 't':
-                                       memmove(tmpwrite + 1, tmpwrite + 2, strlen(tmpwrite + 2) + 1);
-                                       *tmpwrite = '\t';
-                                       break;
-                               default:
-                                       ast_log(AST_LOG_NOTICE, "Substitution routine does not support this character: %c\n", tmpwrite[1]);
-                               }
-                               tmpread = tmpwrite + 1;
-                       }
+                       emailbody = ast_strdup(substitute_escapes(val));
                }
-               if ((val = ast_variable_retrieve(cfg, "general", "pagersubject")))
+               if ((val = ast_variable_retrieve(cfg, "general", "pagersubject"))) {
                        pagersubject = ast_strdup(val);
+               }
                if ((val = ast_variable_retrieve(cfg, "general", "pagerbody"))) {
-                       char *tmpread, *tmpwrite;
-                       pagerbody = ast_strdup(val);
-
-                       /* substitute strings \t and \n into the appropriate characters */
-                       tmpread = tmpwrite = pagerbody;
-                       while ((tmpwrite = strchr(tmpread, '\\'))) {
-                               switch (tmpwrite[1]) {
-                               case 'r':
-                                       memmove(tmpwrite + 1, tmpwrite + 2, strlen(tmpwrite + 2) + 1);
-                                       *tmpwrite = '\r';
-                                       break;
-                               case 'n':
-                                       memmove(tmpwrite + 1, tmpwrite + 2, strlen(tmpwrite + 2) + 1);
-                                       *tmpwrite = '\n';
-                                       break;
-                               case 't':
-                                       memmove(tmpwrite + 1, tmpwrite + 2, strlen(tmpwrite + 2) + 1);
-                                       *tmpwrite = '\t';
-                                       break;
-                               default:
-                                       ast_log(AST_LOG_NOTICE, "Substitution routine does not support this character: %c\n", tmpwrite[1]);
-                               }
-                               tmpread = tmpwrite + 1;
-                       }
+                       pagerbody = ast_strdup(substitute_escapes(val));
                }
                AST_LIST_UNLOCK(&users);
                ast_config_destroy(cfg);
@@ -10760,7 +10946,7 @@ static int unload_module(void)
        res |= ast_unregister_application(app4);
        res |= ast_custom_function_unregister(&mailbox_exists_acf);
        res |= ast_manager_unregister("VoicemailUsersList");
-       ast_cli_unregister_multiple(cli_voicemail, sizeof(cli_voicemail) / sizeof(struct ast_cli_entry));
+       ast_cli_unregister_multiple(cli_voicemail, ARRAY_LEN(cli_voicemail));
        ast_uninstall_vm_functions();
 
        if (poll_thread != AST_PTHREADT_NULL)
@@ -10791,16 +10977,16 @@ static int load_module(void)
        if ((res = load_config(0)))
                return res;
 
-       res = ast_register_application(app, vm_exec, synopsis_vm, descrip_vm);
-       res |= ast_register_application(app2, vm_execmain, synopsis_vmain, descrip_vmain);
-       res |= ast_register_application(app3, vm_box_exists, synopsis_vm_box_exists, descrip_vm_box_exists);
-       res |= ast_register_application(app4, vmauthenticate, synopsis_vmauthenticate, descrip_vmauthenticate);
+       res = ast_register_application_xml(app, vm_exec);
+       res |= ast_register_application_xml(app2, vm_execmain);
+       res |= ast_register_application_xml(app3, vm_box_exists);
+       res |= ast_register_application_xml(app4, vmauthenticate);
        res |= ast_custom_function_register(&mailbox_exists_acf);
        res |= ast_manager_register("VoicemailUsersList", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, manager_list_voicemail_users, "List All Voicemail User Information");
        if (res)
                return res;
 
-       ast_cli_register_multiple(cli_voicemail, sizeof(cli_voicemail) / sizeof(struct ast_cli_entry));
+       ast_cli_register_multiple(cli_voicemail, ARRAY_LEN(cli_voicemail));
 
        ast_install_vm_functions(has_voicemail, inboxcount, inboxcount2, messagecount, sayname);
        ast_realtime_require_field("voicemail", "uniqueid", RQ_UINTEGER3, 11, "password", RQ_CHAR, 10, SENTINEL);