[svn-commits] mmichelson: branch mmichelson/trunk-digiumphones r361322 - in /team/mmichelso...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Apr 5 13:38:19 CDT 2012


Author: mmichelson
Date: Thu Apr  5 13:38:16 2012
New Revision: 361322

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=361322
Log:
Put in the initial Digium phones changes.

Yeah, it's all in one commit. I'm that good.


Added:
    team/mmichelson/trunk-digiumphones/funcs/func_presencestate.c
      - copied, made public unchanged from r361321, team/mmichelson/private/phones-trunk/funcs/func_presencestate.c
    team/mmichelson/trunk-digiumphones/include/asterisk/app_voicemail.h
      - copied, made public unchanged from r361321, team/mmichelson/private/phones-trunk/include/asterisk/app_voicemail.h
    team/mmichelson/trunk-digiumphones/include/asterisk/custom_control_frame.h
      - copied, made public unchanged from r361321, team/mmichelson/private/phones-trunk/include/asterisk/custom_control_frame.h
    team/mmichelson/trunk-digiumphones/include/asterisk/presencestate.h
      - copied, made public unchanged from r361321, team/mmichelson/private/phones-trunk/include/asterisk/presencestate.h
    team/mmichelson/trunk-digiumphones/tests/test_config.c
      - copied, made public unchanged from r361321, team/mmichelson/private/phones-trunk/tests/test_config.c
    team/mmichelson/trunk-digiumphones/tests/test_custom_control.c
      - copied, made public unchanged from r361321, team/mmichelson/private/phones-trunk/tests/test_custom_control.c
    team/mmichelson/trunk-digiumphones/tests/test_voicemail_api.c
      - copied, made public unchanged from r361321, team/mmichelson/private/phones-trunk/tests/test_voicemail_api.c
Modified:
    team/mmichelson/trunk-digiumphones/   (props changed)
    team/mmichelson/trunk-digiumphones/apps/app_mixmonitor.c
    team/mmichelson/trunk-digiumphones/apps/app_queue.c
    team/mmichelson/trunk-digiumphones/apps/app_voicemail.c
    team/mmichelson/trunk-digiumphones/apps/app_voicemail.exports.in
    team/mmichelson/trunk-digiumphones/channels/chan_sip.c
    team/mmichelson/trunk-digiumphones/channels/chan_skinny.c
    team/mmichelson/trunk-digiumphones/channels/sip/include/sip.h
    team/mmichelson/trunk-digiumphones/configs/manager.conf.sample
    team/mmichelson/trunk-digiumphones/funcs/func_frame_trace.c
    team/mmichelson/trunk-digiumphones/include/asterisk/app.h
    team/mmichelson/trunk-digiumphones/include/asterisk/config.h
    team/mmichelson/trunk-digiumphones/include/asterisk/event_defs.h
    team/mmichelson/trunk-digiumphones/include/asterisk/file.h
    team/mmichelson/trunk-digiumphones/include/asterisk/frame.h
    team/mmichelson/trunk-digiumphones/include/asterisk/manager.h
    team/mmichelson/trunk-digiumphones/include/asterisk/message.h
    team/mmichelson/trunk-digiumphones/include/asterisk/pbx.h
    team/mmichelson/trunk-digiumphones/main/app.c
    team/mmichelson/trunk-digiumphones/main/asterisk.c
    team/mmichelson/trunk-digiumphones/main/channel.c
    team/mmichelson/trunk-digiumphones/main/config.c
    team/mmichelson/trunk-digiumphones/main/event.c
    team/mmichelson/trunk-digiumphones/main/features.c
    team/mmichelson/trunk-digiumphones/main/file.c
    team/mmichelson/trunk-digiumphones/main/manager.c
    team/mmichelson/trunk-digiumphones/main/message.c
    team/mmichelson/trunk-digiumphones/main/pbx.c

Propchange: team/mmichelson/trunk-digiumphones/
------------------------------------------------------------------------------
    svn:mergeinfo = /team/mmichelson/private/phones-trunk:358764-361321

Modified: team/mmichelson/trunk-digiumphones/apps/app_mixmonitor.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/trunk-digiumphones/apps/app_mixmonitor.c?view=diff&rev=361322&r1=361321&r2=361322
==============================================================================
--- team/mmichelson/trunk-digiumphones/apps/app_mixmonitor.c (original)
+++ team/mmichelson/trunk-digiumphones/apps/app_mixmonitor.c Thu Apr  5 13:38:16 2012
@@ -42,6 +42,7 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/paths.h"	/* use ast_config_AST_MONITOR_DIR */
+#include "asterisk/stringfields.h"
 #include "asterisk/file.h"
 #include "asterisk/audiohook.h"
 #include "asterisk/pbx.h"
@@ -51,6 +52,7 @@
 #include "asterisk/channel.h"
 #include "asterisk/autochan.h"
 #include "asterisk/manager.h"
+#include "asterisk/callerid.h"
 #include "asterisk/mod_format.h"
 #include "asterisk/linkedlists.h"
 
@@ -111,6 +113,11 @@
 					<option name="i">
 						<argument name="chanvar" required="true" />
 						<para>Stores the MixMonitor's ID on this channel variable.</para>
+					</option>
+					<option name="m">
+						<argument name="mailbox" required="true" />
+						<para>Create a copy of the recording as a voicemail in the indicated <emphasis>mailbox</emphasis>(es)
+						separated by commas eg. m(1111 at default,2222 at default,...)</para>
 					</option>
 				</optionlist>
 			</parameter>
@@ -238,6 +245,16 @@
 
 static const char * const mixmonitor_spy_type = "MixMonitor";
 
+/*!
+ * \internal
+ * \brief This struct is a list item holds data needed to find a vm_recipient within voicemail
+ */
+struct vm_recipient {
+	char mailbox[AST_MAX_CONTEXT];
+	char context[AST_MAX_EXTENSION];
+	AST_LIST_ENTRY(vm_recipient) list;
+};
+
 struct mixmonitor {
 	struct ast_audiohook audiohook;
 	struct ast_callid *callid;
@@ -249,6 +266,20 @@
 	unsigned int flags;
 	struct ast_autochan *autochan;
 	struct mixmonitor_ds *mixmonitor_ds;
+
+	/* the below string fields describe data used for creating voicemails from the recording */
+	AST_DECLARE_STRING_FIELDS(
+		AST_STRING_FIELD(call_context);
+		AST_STRING_FIELD(call_macrocontext);
+		AST_STRING_FIELD(call_extension);
+		AST_STRING_FIELD(call_callerchan);
+		AST_STRING_FIELD(call_callerid);
+	);
+	int call_priority;
+
+	/* FUTURE DEVELOPMENT NOTICE
+	 * recipient_list will need locks if we make it editable after the monitor is started */
+	AST_LIST_HEAD_NOLOCK(, vm_recipient) recipient_list;
 };
 
 enum mixmonitor_flags {
@@ -260,7 +291,8 @@
 	MUXFLAG_READ = (1 << 6),
 	MUXFLAG_WRITE = (1 << 7),
 	MUXFLAG_COMBINED = (1 << 8),
-        MUXFLAG_UID = (1 << 9),
+	MUXFLAG_UID = (1 << 9),
+	MUXFLAG_VMRECIPIENTS = (1 << 10),
 };
 
 enum mixmonitor_args {
@@ -269,7 +301,8 @@
 	OPT_ARG_VOLUME,
 	OPT_ARG_WRITENAME,
 	OPT_ARG_READNAME,
-        OPT_ARG_UID,
+	OPT_ARG_UID,
+	OPT_ARG_VMRECIPIENTS,
 	OPT_ARG_ARRAY_SIZE,	/* Always last element of the enum */
 };
 
@@ -282,6 +315,7 @@
 	AST_APP_OPTION_ARG('r', MUXFLAG_READ, OPT_ARG_READNAME),
 	AST_APP_OPTION_ARG('t', MUXFLAG_WRITE, OPT_ARG_WRITENAME),
 	AST_APP_OPTION_ARG('i', MUXFLAG_UID, OPT_ARG_UID),
+	AST_APP_OPTION_ARG('m', MUXFLAG_VMRECIPIENTS, OPT_ARG_VMRECIPIENTS),
 });
 
 struct mixmonitor_ds {
@@ -380,6 +414,64 @@
 		ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);	
 
 	return res;
+}
+
+/*!
+ * \internal
+ * \brief adds recipients to a mixmonitor's recipient list
+ * \param mixmonitor mixmonitor being affected
+ * \param vm_recipients string containing the desired recipients to add
+ */
+static void add_vm_recipients_from_string(struct mixmonitor *mixmonitor, const char *vm_recipients)
+{
+	/* recipients are in a single string with a format format resembling "mailbox at context,mailbox2 at context2, mailbox3 at context3" */
+	char *cur_mailbox = ast_strdupa(vm_recipients);
+	char *cur_context;
+	char *next;
+	int elements_processed = 0;
+
+	while (!ast_strlen_zero(cur_mailbox)) {
+		ast_debug(3, "attempting to add next element %d from %s\n", elements_processed, cur_mailbox);
+		if ((next = strchr(cur_mailbox, ',')) || (next = strchr(cur_mailbox, '&'))) {
+			*(next++) = '\0';
+		}
+
+		if ((cur_context = strchr(cur_mailbox, '@'))) {
+			*(cur_context++) = '\0';
+		} else {
+			cur_context = "default";
+		}
+
+		if (!ast_strlen_zero(cur_mailbox) && !ast_strlen_zero(cur_context)) {
+
+			struct vm_recipient *recipient;
+			if (!(recipient = ast_malloc(sizeof(*recipient)))) {
+				ast_log(LOG_ERROR, "Failed to allocate recipient. Aborting function.\n");
+				return;
+			}
+			ast_copy_string(recipient->context, cur_context, sizeof(recipient->context));
+			ast_copy_string(recipient->mailbox, cur_mailbox, sizeof(recipient->mailbox));
+
+			/* Add to list */
+			ast_verb(5, "Adding %s@%s to recipient list\n", recipient->mailbox, recipient->context);
+			AST_LIST_INSERT_HEAD(&mixmonitor->recipient_list, recipient, list);
+
+		} else {
+			ast_log(LOG_ERROR, "Failed to properly parse extension and/or context from element %d of recipient string: %s\n", elements_processed, vm_recipients);
+		}
+
+		cur_mailbox = next;
+		elements_processed++;
+	}
+}
+
+static void clear_mixmonitor_recipient_list(struct mixmonitor *mixmonitor)
+{
+	struct vm_recipient *current;
+	while ((current = AST_LIST_REMOVE_HEAD(&mixmonitor->recipient_list, list))) {
+		/* Clear list element data */
+		ast_free(current);
+	}
 }
 
 #define SAMPLES_PER_FRAME 160
@@ -397,6 +489,12 @@
 			ast_free(mixmonitor->post_process);
 		}
 
+		/* Free everything in the recipient list */
+		clear_mixmonitor_recipient_list(mixmonitor);
+
+		/* clean stringfields */
+		ast_string_field_free_memory(mixmonitor);
+
 		if (mixmonitor->callid) {
 			ast_callid_unref(mixmonitor->callid);
 		}
@@ -404,10 +502,48 @@
 	}
 }
 
-static void mixmonitor_save_prep(struct mixmonitor *mixmonitor, char *filename, struct ast_filestream **fs, unsigned int *oflags, int *errflag)
+/*!
+ * \internal
+ * \brief Copies the mixmonitor to all voicemail recipients
+ * \param mixmonitor The mixmonitor that needs to forward its file to recipients
+ * \param ext Format of the file that was saved
+ */
+static void copy_to_voicemail(struct mixmonitor *mixmonitor, const char *ext, const char *filename)
+{
+	struct vm_recipient *recipient = NULL;
+	struct ast_vm_recording_data recording_data;
+	if (ast_string_field_init(&recording_data, 512)) {
+		ast_log(LOG_ERROR, "Failed to string_field_init, skipping copy_to_voicemail\n");
+		return;
+	}
+
+	/* Copy strings to stringfields that will be used for all recipients */
+	ast_string_field_set(&recording_data, recording_file, filename);
+	ast_string_field_set(&recording_data, recording_ext, ext);
+	ast_string_field_set(&recording_data, call_context, mixmonitor->call_context);
+	ast_string_field_set(&recording_data, call_macrocontext, mixmonitor->call_macrocontext);
+	ast_string_field_set(&recording_data, call_extension, mixmonitor->call_extension);
+	ast_string_field_set(&recording_data, call_callerchan, mixmonitor->call_callerchan);
+	ast_string_field_set(&recording_data, call_callerid, mixmonitor->call_callerid);
+	/* and call_priority gets copied too */
+	recording_data.call_priority = mixmonitor->call_priority;
+
+	AST_LIST_TRAVERSE(&mixmonitor->recipient_list, recipient, list) {
+		/* context and mailbox need to be set per recipient */
+		ast_string_field_set(&recording_data, context, recipient->context);
+		ast_string_field_set(&recording_data, mailbox, recipient->mailbox);
+		ast_verb(4, "MixMonitor attempting to send voicemail copy to %s@%s\n", recording_data.mailbox,
+			recording_data.context);
+		ast_app_copy_recording_to_vm(&recording_data);
+	}
+
+	/* Free the string fields for recording_data before exiting the function. */
+	ast_string_field_free_memory(&recording_data);
+}
+
+static void mixmonitor_save_prep(struct mixmonitor *mixmonitor, char *filename, struct ast_filestream **fs, unsigned int *oflags, int *errflag, char **ext)
 {
 	/* Initialize the file if not already done so */
-	char *ext = NULL;
 	char *last_slash = NULL;
 	if (!ast_strlen_zero(filename)) {
 		if (!*fs && !*errflag && !mixmonitor->mixmonitor_ds->fs_quit) {
@@ -416,14 +552,14 @@
 
 			last_slash = strrchr(filename, '/');
 
-			if ((ext = strrchr(filename, '.')) && (ext > last_slash)) {
-				*(ext++) = '\0';
+			if ((*ext = strrchr(filename, '.')) && (*ext > last_slash)) {
+				**(ext++) = '\0';
 			} else {
-				ext = "raw";
+				*ext = "raw";
 			}
 
-			if (!(*fs = ast_writefile(filename, ext, NULL, *oflags, 0, 0666))) {
-				ast_log(LOG_ERROR, "Cannot open %s.%s\n", filename, ext);
+			if (!(*fs = ast_writefile(filename, *ext, NULL, *oflags, 0, 0666))) {
+				ast_log(LOG_ERROR, "Cannot open %s.%s\n", filename, *ext);
 				*errflag = 1;
 			} else {
 				struct ast_filestream *tmp = *fs;
@@ -436,6 +572,9 @@
 static void *mixmonitor_thread(void *obj) 
 {
 	struct mixmonitor *mixmonitor = obj;
+	char *fs_ext = "";
+	char *fs_read_ext = "";
+	char *fs_write_ext = "";
 
 	struct ast_filestream **fs = NULL;
 	struct ast_filestream **fs_read = NULL;
@@ -457,9 +596,9 @@
 	fs_write = &mixmonitor->mixmonitor_ds->fs_write;
 
 	ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock);
-	mixmonitor_save_prep(mixmonitor, mixmonitor->filename, fs, &oflags, &errflag);
-	mixmonitor_save_prep(mixmonitor, mixmonitor->filename_read, fs_read, &oflags, &errflag);
-	mixmonitor_save_prep(mixmonitor, mixmonitor->filename_write, fs_write, &oflags, &errflag);
+	mixmonitor_save_prep(mixmonitor, mixmonitor->filename, fs, &oflags, &errflag, &fs_ext);
+	mixmonitor_save_prep(mixmonitor, mixmonitor->filename_read, fs_read, &oflags, &errflag, &fs_read_ext);
+	mixmonitor_save_prep(mixmonitor, mixmonitor->filename_write, fs_write, &oflags, &errflag, &fs_write_ext);
 
 	ast_format_set(&format_slin, ast_format_slin_by_rate(mixmonitor->mixmonitor_ds->samp_rate), 0);
 
@@ -554,6 +693,27 @@
 	}
 
 	ast_verb(2, "End MixMonitor Recording %s\n", mixmonitor->name);
+
+	if (!AST_LIST_EMPTY(&mixmonitor->recipient_list)) {
+		if (ast_strlen_zero(fs_ext)) {
+			ast_log(LOG_ERROR, "No file extension set for Mixmonitor %s. Skipping copy to voicemail.\n",
+				mixmonitor -> name);
+		} else {
+			ast_verb(3, "Copying recordings for Mixmonitor %s to voicemail recipients\n", mixmonitor->name);
+			copy_to_voicemail(mixmonitor, fs_ext, mixmonitor->filename);
+		}
+		if (!ast_strlen_zero(fs_read_ext)) {
+			ast_verb(3, "Copying read recording for Mixmonitor %s to voicemail recipients\n", mixmonitor->name);
+			copy_to_voicemail(mixmonitor, fs_read_ext, mixmonitor->filename_read);
+		}
+		if (!ast_strlen_zero(fs_write_ext)) {
+			ast_verb(3, "Copying write recording for Mixmonitor %s to voicemail recipients\n", mixmonitor->name);
+			copy_to_voicemail(mixmonitor, fs_write_ext, mixmonitor->filename_write);
+		}
+	} else {
+		ast_debug(3, "No recipients to forward monitor to, moving on.\n");
+	}
+
 	mixmonitor_free(mixmonitor);
 	return NULL;
 }
@@ -597,7 +757,8 @@
 static void launch_monitor_thread(struct ast_channel *chan, const char *filename,
 				  unsigned int flags, int readvol, int writevol,
 				  const char *post_process, const char *filename_write,
-				  char *filename_read, const char *uid_channel_var)
+				  char *filename_read, const char *uid_channel_var,
+				  const char *recipients)
 {
 	pthread_t thread;
 	struct mixmonitor *mixmonitor;
@@ -623,6 +784,12 @@
 		return;
 	}
 
+	/* Now that the struct has been calloced, go ahead and initialize the string fields. */
+	if (ast_string_field_init(mixmonitor, 512)) {
+		mixmonitor_free(mixmonitor);
+		return;
+	}
+
 	/* Setup the actual spy before creating our thread */
 	if (ast_audiohook_init(&mixmonitor->audiohook, AST_AUDIOHOOK_TYPE_SPY, mixmonitor_spy_type, 0)) {
 		mixmonitor_free(mixmonitor);
@@ -650,7 +817,6 @@
 	}
 	ast_free(datastore_id);
 
-
 	mixmonitor->name = ast_strdup(ast_channel_name(chan));
 
 	if (!ast_strlen_zero(postprocess2)) {
@@ -667,6 +833,35 @@
 
 	if (!ast_strlen_zero(filename_read)) {
 		mixmonitor->filename_read = ast_strdup(filename_read);
+	}
+
+	if (!ast_strlen_zero(recipients)) {
+		char callerid[256];
+		struct ast_party_connected_line *connected;
+
+		ast_channel_lock(chan);
+
+		/* We use the connected line of the invoking channel for caller ID. */
+
+		connected = ast_channel_connected(chan);
+		ast_debug(3, "Connected Line CID = %d - %s : %d - %s\n", connected->id.name.valid,
+			connected->id.name.str, connected->id.number.valid,
+			connected->id.number.str);
+		ast_callerid_merge(callerid, sizeof(callerid),
+			S_COR(connected->id.name.valid, connected->id.name.str, NULL),
+			S_COR(connected->id.number.valid, connected->id.number.str, NULL),
+			"Unknown");
+
+		ast_string_field_set(mixmonitor, call_context, ast_channel_context(chan));
+		ast_string_field_set(mixmonitor, call_macrocontext, ast_channel_macrocontext(chan));
+		ast_string_field_set(mixmonitor, call_extension, ast_channel_exten(chan));
+		ast_string_field_set(mixmonitor, call_callerchan, ast_channel_name(chan));
+		ast_string_field_set(mixmonitor, call_callerid, callerid);
+		mixmonitor->call_priority = ast_channel_priority(chan);
+
+		ast_channel_unlock(chan);
+
+		add_vm_recipients_from_string(mixmonitor, recipients);
 	}
 
 	ast_set_flag(&mixmonitor->audiohook, AST_AUDIOHOOK_TRIGGER_SYNC);
@@ -723,6 +918,7 @@
         char *uid_channel_var = NULL;
 
 	struct ast_flags flags = { 0 };
+	char *recipients = NULL;
 	char *parse;
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(filename);
@@ -774,6 +970,14 @@
 			}
 		}
 
+		if (ast_test_flag(&flags, MUXFLAG_VMRECIPIENTS)) {
+			if (ast_strlen_zero(opts[OPT_ARG_VMRECIPIENTS])) {
+				ast_log(LOG_WARNING, "No voicemail recipients were specified for the vm copy ('m') option.\n");
+			} else {
+				recipients = ast_strdupa(opts[OPT_ARG_VMRECIPIENTS]);
+			}
+		}
+
 		if (ast_test_flag(&flags, MUXFLAG_WRITE)) {
 			filename_write = ast_strdupa(filename_parse(opts[OPT_ARG_WRITENAME], filename_buffer, sizeof(filename_buffer)));
 		}
@@ -799,7 +1003,16 @@
 	}
 
 	pbx_builtin_setvar_helper(chan, "MIXMONITOR_FILENAME", args.filename);
-	launch_monitor_thread(chan, args.filename, flags.flags, readvol, writevol, args.post_process, filename_write, filename_read, uid_channel_var);
+	launch_monitor_thread(chan,
+			args.filename,
+			flags.flags,
+			readvol,
+			writevol,
+			args.post_process,
+			filename_write,
+			filename_read,
+			uid_channel_var,
+			recipients);
 
 	return 0;
 }

Modified: team/mmichelson/trunk-digiumphones/apps/app_queue.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/trunk-digiumphones/apps/app_queue.c?view=diff&rev=361322&r1=361321&r2=361322
==============================================================================
--- team/mmichelson/trunk-digiumphones/apps/app_queue.c (original)
+++ team/mmichelson/trunk-digiumphones/apps/app_queue.c Thu Apr  5 13:38:16 2012
@@ -1667,12 +1667,18 @@
 	return state;
 }
 
-static int extension_state_cb(const char *context, const char *exten, enum ast_extension_states state, void *data)
+static int extension_state_cb(char *context, char *exten, struct ast_state_cb_info *info, void *data)
 {
 	struct ao2_iterator miter, qiter;
 	struct member *m;
 	struct call_queue *q;
+	int state = info->exten_state;
 	int found = 0, device_state = extensionstate2devicestate(state);
+
+	/* only interested in extension state updates involving device states */
+	if (info->reason != AST_HINT_UPDATE_DEVICE) {
+		return 0;
+	}
 
 	qiter = ao2_iterator_init(queues, 0);
 	while ((q = ao2_t_iterator_next(&qiter, "Iterate through queues"))) {

Modified: team/mmichelson/trunk-digiumphones/apps/app_voicemail.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/trunk-digiumphones/apps/app_voicemail.c?view=diff&rev=361322&r1=361321&r2=361322
==============================================================================
--- team/mmichelson/trunk-digiumphones/apps/app_voicemail.c (original)
+++ team/mmichelson/trunk-digiumphones/apps/app_voicemail.c Thu Apr  5 13:38:16 2012
@@ -113,12 +113,14 @@
 #include "asterisk/module.h"
 #include "asterisk/adsi.h"
 #include "asterisk/app.h"
+#include "asterisk/app_voicemail.h"
 #include "asterisk/manager.h"
 #include "asterisk/dsp.h"
 #include "asterisk/localtime.h"
 #include "asterisk/cli.h"
 #include "asterisk/utils.h"
 #include "asterisk/stringfields.h"
+#include "asterisk/strings.h"
 #include "asterisk/smdi.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/event.h"
@@ -334,6 +336,30 @@
 			</enumlist>
 		</description>
 	</application>
+	<application name="VoiceMailPlayMsg" language="en_US">
+		<synopsis>
+			Play a single voice mail msg from a mailbox by msg id.
+		</synopsis>
+		<syntax>
+			<parameter name="mailbox" required="true" argsep="@">
+				<argument name="mailbox" />
+				<argument name="context" />
+			</parameter>
+			<parameter name="msg_id" required="true">
+				<para>The msg id of the msg to play back. </para>
+			</parameter>
+		</syntax>
+		<description>
+			<para>This application sets the following channel variable upon completion:</para>
+			<variablelist>
+				<variable name="VOICEMAIL_PLAYBACKSTATUS">
+					<para>The status of the playback attempt as a text string.</para>
+					<value name="SUCCESS"/>
+					<value name="FAILED"/>
+				</variable>
+			</variablelist>
+		</description>
+	</application>
 	<application name="VMSayName" language="en_US">
 		<synopsis>
 			Play the name of a voicemail user
@@ -552,7 +578,6 @@
 #define ERROR_LOCK_PATH  -100
 #define OPERATOR_EXIT     300
 
-
 enum vm_box {
 	NEW_FOLDER,
 	OLD_FOLDER,
@@ -599,6 +624,25 @@
 	AST_APP_OPTION('U', OPT_MESSAGE_Urgent),
 	AST_APP_OPTION('P', OPT_MESSAGE_PRIORITY)
 });
+
+static const char * const mailbox_folders[] = {
+#ifdef IMAP_STORAGE
+	imapfolder,
+#else
+	"INBOX",
+#endif
+	"Old",
+	"Work",
+	"Family",
+	"Friends",
+	"Cust1",
+	"Cust2",
+	"Cust3",
+	"Cust4",
+	"Cust5",
+	"Deleted",
+	"Urgent",
+};
 
 static int load_config(int reload);
 #ifdef TEST_FRAMEWORK
@@ -852,6 +896,8 @@
 static char *app3 = "MailboxExists";
 static char *app4 = "VMAuthenticate";
 
+static char *playmsg_app = "VoiceMailPlayMsg";
+
 static char *sayname_app = "VMSayName";
 
 static AST_LIST_HEAD_STATIC(users, ast_vm_user);
@@ -990,6 +1036,7 @@
 static int is_valid_dtmf(const char *key);
 static void read_password_from_file(const char *secretfn, char *password, int passwordlen);
 static int write_password_to_file(const char *secretfn, const char *password);
+struct ast_str *vm_mailbox_snapshot_str(const char *mailbox, const char *context);
 static const char *substitute_escapes(const char *value);
 
 struct ao2_container *inprocess_container;
@@ -1790,25 +1837,6 @@
 	return 0;
 }
 
-static const char * const mailbox_folders[] = {
-#ifdef IMAP_STORAGE
-	imapfolder,
-#else
-	"INBOX",
-#endif
-	"Old",
-	"Work",
-	"Family",
-	"Friends",
-	"Cust1",
-	"Cust2",
-	"Cust3",
-	"Cust4",
-	"Cust5",
-	"Deleted",
-	"Urgent",
-};
-
 static const char *mbox(struct ast_vm_user *vmu, int id)
 {
 #ifdef IMAP_STORAGE
@@ -2266,7 +2294,9 @@
 	check_quota(vms, vmu->imapfolder);
 	if (vms->quota_limit && vms->quota_usage >= vms->quota_limit) {
 		ast_debug(1, "*** QUOTA EXCEEDED!! %u >= %u\n", vms->quota_usage, vms->quota_limit);
-		ast_play_and_wait(chan, "vm-mailboxfull");
+		if (chan) {
+			ast_play_and_wait(chan, "vm-mailboxfull");
+		}
 		return -1;
 	}
 
@@ -2274,8 +2304,10 @@
 	ast_debug(3, "Checking message number quota: mailbox has %d messages, maximum is set to %d, current messages %d\n", msgnum, vmu->maxmsg, inprocess_count(vmu->mailbox, vmu->context, 0));
 	if (msgnum >= vmu->maxmsg - inprocess_count(vmu->mailbox, vmu->context, +1)) {
 		ast_log(LOG_WARNING, "Unable to leave message since we will exceed the maximum number of messages allowed (%u >= %u)\n", msgnum, vmu->maxmsg);
-		ast_play_and_wait(chan, "vm-mailboxfull");
-		pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED");
+		if (chan) {
+			ast_play_and_wait(chan, "vm-mailboxfull");
+			pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED");
+		}
 		return -1;
 	}
 
@@ -2387,8 +2419,8 @@
 	}
 
 	make_email_file(p, myserveremail, vmu, msgnum, vmu->context, vmu->mailbox, "INBOX",
-		S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
-		S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL),
+		chan ? S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL) : NULL,
+		chan ? S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL) : NULL,
 		fn, introfn, fmt, duration, 1, chan, NULL, 1, flag);
 	/* read mail file to memory */
 	len = ftell(p);
@@ -4756,8 +4788,8 @@
 #endif
 		/* flag added for Urgent */
 		fprintf(p, "X-Asterisk-VM-Flag: %s" ENDL, flag);
-		fprintf(p, "X-Asterisk-VM-Priority: %d" ENDL, ast_channel_priority(chan));
-		fprintf(p, "X-Asterisk-VM-Caller-channel: %s" ENDL, ast_channel_name(chan));
+		fprintf(p, "X-Asterisk-VM-Priority: %d" ENDL, chan ? ast_channel_priority(chan) : 0);
+		fprintf(p, "X-Asterisk-VM-Caller-channel: %s" ENDL, chan ? ast_channel_name(chan) : "");
 		fprintf(p, "X-Asterisk-VM-Caller-ID-Num: %s" ENDL, enc_cidnum);
 		fprintf(p, "X-Asterisk-VM-Caller-ID-Name: %s" ENDL, enc_cidname);
 		fprintf(p, "X-Asterisk-VM-Duration: %d" ENDL, duration);
@@ -5438,7 +5470,7 @@
 	if (recipmsgnum < recip->maxmsg - (imbox ? 0 : inprocess_count(vmu->mailbox, vmu->context, 0))) {
 		make_file(topath, sizeof(topath), todir, recipmsgnum);
 #ifndef ODBC_STORAGE
-		if (EXISTS(fromdir, msgnum, frompath, ast_channel_language(chan))) {	
+		if (EXISTS(fromdir, msgnum, frompath, chan ? ast_channel_language(chan) : "")) {
 			COPY(fromdir, msgnum, todir, recipmsgnum, recip->mailbox, recip->context, frompath, topath);
 		} else {
 #endif
@@ -5456,11 +5488,14 @@
 		res = -1;
 	}
 	ast_unlock_path(todir);
-	notify_new_message(chan, recip, NULL, recipmsgnum, duration, fmt,
-		S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
-		S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL),
-		flag);
-	
+	if (chan) {
+		struct ast_party_caller *caller = ast_channel_caller(chan);
+		notify_new_message(chan, recip, NULL, recipmsgnum, duration, fmt,
+			S_COR(caller->id.number.valid, caller->id.number.str, NULL),
+			S_COR(caller->id.name.valid, caller->id.name.str, NULL),
+			flag);
+	}
+
 	return res;
 }
 #endif
@@ -5661,6 +5696,301 @@
 	signed char record_gain;
 	char *exitcontext;
 };
+
+/*!
+ * \internal
+ * \brief Creates a voicemail based on a specified file to a mailbox.
+ * \param recdata A vm_recording_data containing filename and voicemail txt info.
+ * \retval -1 failure
+ * \retval 0 success
+ *
+ * This is installed to the app.h voicemail functions and accommodates all voicemail
+ * storage methods. It should probably be broken out along with leave_voicemail at
+ * some point in the future.
+ *
+ * This function currently only works for a single recipient and only uses the format
+ * specified in recording_ext.
+ */
+static int msg_create_from_file(struct ast_vm_recording_data *recdata)
+{
+	/* voicemail recipient structure */
+	struct ast_vm_user *recipient; /* points to svm once it's been created */
+	struct ast_vm_user svm; /* struct storing the voicemail recipient */
+
+	/* File paths */
+	char tmpdir[PATH_MAX]; /* directory temp files are stored in */
+	char tmptxtfile[PATH_MAX]; /* tmp file for voicemail txt file */
+	char desttxtfile[PATH_MAX]; /* final destination for txt file */
+	char tmpaudiofile[PATH_MAX]; /* tmp file where audio is stored */
+	char dir[PATH_MAX]; /* destination for tmp files on completion */
+	char destination[PATH_MAX]; /* destination with msgXXXX.  Basically <dir>/msgXXXX */
+
+	/* stuff that only seems to be needed for IMAP */
+	#ifdef IMAP_STORAGE
+	struct vm_state *vms = NULL;
+	char ext_context[256] = "";
+	char *fmt = ast_strdupa(recdata->recording_ext);
+	int newmsgs = 0;
+	int oldmsgs = 0;
+	#endif
+
+	/* miscellaneous operational variables */
+	int res = 0; /* Used to store error codes from functions */
+	int txtdes /* File descriptor for the text file used to write the voicemail info */;
+	FILE *txt; /* FILE pointer to text file used to write the voicemail info */
+	char date[256]; /* string used to hold date of the voicemail (only used for ODBC) */
+	int msgnum; /* the 4 digit number designated to the voicemail */
+	int duration = 0; /* Length of the audio being recorded in seconds */
+	struct ast_filestream *recording_fs; /*used to read the recording to get duration data */
+
+	/* We aren't currently doing anything with category, since it comes from a channel variable and
+	 * this function doesn't use channels, but this function could add that as an argument later. */
+	const char *category = NULL; /* pointless for now */
+
+	/* Start by checking to see if the file actually exists... */
+	if (!(ast_fileexists(recdata->recording_file, recdata->recording_ext, NULL))) {
+		ast_log(LOG_ERROR, "File: %s not found.\n", recdata->recording_file);
+		return -1;
+	}
+
+	if (!(recipient = find_user(&svm, recdata->context, recdata->mailbox))) {
+		ast_log(LOG_ERROR, "No entry in voicemail config file for '%s@%s'\n", recdata->mailbox, recdata->context);
+		return -1;
+	}
+
+	/* determine duration in seconds */
+	if ((recording_fs = ast_readfile(recdata->recording_file, recdata->recording_ext, NULL, 0, 0, VOICEMAIL_DIR_MODE))) {
+		if (!ast_seekstream(recording_fs, 0, SEEK_END)) {
+			long framelength = ast_tellstream(recording_fs);
+			struct ast_format result;
+			/* XXX This use of ast_getformatbyname seems incorrect here. The file extension does not necessarily correspond
+			 * to the name of the format. For instance, if "raw" were passed in, I don't think ast_getformatbyname would
+			 * find the slinear format
+			 */
+			duration = (int) (framelength / ast_format_rate(ast_getformatbyname(recdata->recording_ext, &result)));
+		}
+	}
+
+	/* If the duration was below the minimum duration for the user, let's just drop the whole thing now */
+	if (duration < recipient->minsecs) {
+		ast_log(LOG_NOTICE, "Copying recording to voicemail %s@%s skipped because duration was shorter than "
+					"minmessage of recipient\n", recdata->mailbox, recdata->context);
+		return -1;
+	}
+
+	/* Note that this number must be dropped back to a net sum of zero before returning from this function */
+
+	if ((res = create_dirpath(tmpdir, sizeof(tmpdir), recipient->context, recdata->mailbox, "tmp"))) {
+		ast_log(LOG_ERROR, "Failed to make directory.\n");
+	}
+
+	snprintf(tmptxtfile, sizeof(tmptxtfile), "%s/XXXXXX", tmpdir);
+	txtdes = mkstemp(tmptxtfile);
+	if (txtdes < 0) {
+		chmod(tmptxtfile, VOICEMAIL_FILE_MODE & ~my_umask);
+		/* Something screwed up.  Abort. */
+		ast_log(AST_LOG_ERROR, "Unable to create message file: %s\n", strerror(errno));
+		free_user(recipient);
+		return -1;
+	}
+
+	/* Store information */
+	txt = fdopen(txtdes, "w+");
+	if (txt) {
+		char msg_id[256];
+		char msg_id_hash[256];
+
+		/* Every voicemail msg gets its own unique msg id.  The msg id is the originate time
+		 * plus a hash of the extension, context, and callerid of the channel leaving the msg */
+
+		snprintf(msg_id_hash, sizeof(msg_id_hash), "%s%s%s", recdata->call_extension, 
+			recdata->call_context, recdata->call_callerid);
+		snprintf(msg_id, sizeof(msg_id), "%ld-%d", (long) time(NULL), ast_str_hash(msg_id_hash));
+
+		get_date(date, sizeof(date));
+		fprintf(txt,
+			";\n"
+			"; Message Information file\n"
+			";\n"
+			"[message]\n"
+			"origmailbox=%s\n"
+			"context=%s\n"
+			"macrocontext=%s\n"
+			"exten=%s\n"
+			"rdnis=Unknown\n"
+			"priority=%d\n"
+			"callerchan=%s\n"
+			"callerid=%s\n"
+			"origdate=%s\n"
+			"origtime=%ld\n"
+			"category=%s\n"
+			"msg_id=%s\n"
+			"flag=\n" /* flags not supported in copy from file yet */
+			"duration=%d\n", /* Don't have any reliable way to get duration of file. */
+
+			recdata->mailbox,
+			S_OR(recdata->call_context, ""),
+			S_OR(recdata->call_macrocontext, ""),
+			S_OR(recdata->call_extension, ""),
+			recdata->call_priority,
+			S_OR(recdata->call_callerchan, "Unknown"),
+			S_OR(recdata->call_callerid, "Unknown"),
+			date, (long) time(NULL),
+			S_OR(category, ""),
+			msg_id,
+			duration);
+
+		/* Since we are recording from a file, we shouldn't need to do anything else with
+		 * this txt file */
+		fclose(txt);
+
+	} else {
+		ast_log(LOG_WARNING, "Error opening text file for output\n");
+		if (ast_check_realtime("voicemail_data")) {
+			ast_destroy_realtime("voicemail_data", "filename", tmptxtfile, SENTINEL);
+		}
+		free_user(recipient);
+		return -1;
+	}
+
+	/* At this point, the actual creation of a voicemail message should be finished.
+	 * Now we just need to copy the files being recorded into the receiving folder. */
+
+	create_dirpath(dir, sizeof(dir), recipient->context, recipient->mailbox, "INBOX");
+
+#ifdef IMAP_STORAGE
+	/* make recipient info into an inboxcount friendly string */
+	snprintf(ext_context, sizeof(ext_context), "%s@%s", recipient->mailbox, recipient->context);
+
+	/* Is ext a mailbox? */
+	/* must open stream for this user to get info! */
+	res = inboxcount(ext_context, &newmsgs, &oldmsgs);
+	if (res < 0) {
+		ast_log(LOG_NOTICE, "Can not leave voicemail, unable to count messages\n");
+		free_user(recipient);
+		unlink(tmptxtfile);
+		return -1;
+	}
+	if (!(vms = get_vm_state_by_mailbox(recipient->mailbox, recipient->context, 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 = create_vm_state_from_user(recipient))) {
+			ast_log(LOG_ERROR, "Couldn't allocate necessary space\n");
+			free_user(recipient);
+			unlink(tmptxtfile);
+			return -1;
+		}
+	}
+	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);
+	snprintf(destination, sizeof(destination), "%simap/msg%s%04d", VM_SPOOL_DIR, recipient->mailbox, msgnum);
+
+	/* Check to see if we have enough room in the mailbox. If not, spit out an error and end
+	 * Note that imap_check_limits raises inprocess_count if successful */
+	if ((res = imap_check_limits(NULL, vms, recipient, msgnum))) {
+		ast_log(LOG_NOTICE, "Didn't copy to voicemail. Mailbox for %s@%s is full.\n", recipient->mailbox, recipient->context);
+		inprocess_count(recipient->mailbox, recipient->context, -1);
+		free_user(recipient);
+		unlink(tmptxtfile);
+		return -1;
+	}
+
+#else
+
+	/* Check to see if the mailbox is full for ODBC/File storage */
+	ast_debug(3, "mailbox = %d : inprocess = %d\n", count_messages(recipient, dir),
+		inprocess_count(recipient->mailbox, recipient->context, 0));
+	if (count_messages(recipient, dir) > recipient->maxmsg - inprocess_count(recipient->mailbox, recipient->context, +1)) {
+		ast_log(AST_LOG_WARNING, "Didn't copy to voicemail. Mailbox for %s@%s is full.\n", recipient->mailbox, recipient->context);
+		inprocess_count(recipient->mailbox, recipient->context, -1);
+		free_user(recipient);
+		unlink(tmptxtfile);
+		return -1;
+	}
+
+	msgnum = last_message_index(recipient, dir) + 1;
+#endif
+
+	/* Lock the directory receiving the voicemail since we want it to still exist when we attempt to copy the voicemail.
+	 * We need to unlock it before we return. */
+	if (vm_lock_path(dir)) {
+		ast_log(LOG_ERROR, "Couldn't lock directory %s.  Voicemail will be lost.\n", dir);
+		/* Delete files */
+		ast_filedelete(tmptxtfile, NULL);
+		unlink(tmptxtfile);
+		free_user(recipient);
+		return -1;
+	}
+
+	make_file(destination, sizeof(destination), dir, msgnum);
+
+	make_file(tmpaudiofile, sizeof(tmpaudiofile), tmpdir, msgnum);
+
+	if (ast_filecopy(recdata->recording_file, tmpaudiofile, recdata->recording_ext)) {
+		ast_log(LOG_ERROR, "Audio file failed to copy to tmp dir. Probably low disk space.\n");
+
+		inprocess_count(recipient->mailbox, recipient->context, -1);
+		ast_unlock_path(dir);
+		free_user(recipient);
+		unlink(tmptxtfile);
+		return -1;
+	}
+
+	/* Alright, try to copy to the destination folder now. */
+	if (ast_filerename(tmpaudiofile, destination, recdata->recording_ext)) {
+		ast_log(LOG_ERROR, "Audio file failed to move to destination directory. Permissions/Overlap?\n");
+		inprocess_count(recipient->mailbox, recipient->context, -1);
+		ast_unlock_path(dir);
+		free_user(recipient);
+		unlink(tmptxtfile);
+		return -1;
+	}
+
+	snprintf(desttxtfile, sizeof(desttxtfile), "%s.txt", destination);
+	rename(tmptxtfile, desttxtfile);
+
+	if (chmod(desttxtfile, VOICEMAIL_FILE_MODE) < 0) {
+		ast_log(AST_LOG_ERROR, "Couldn't set permissions on voicemail text file %s: %s", desttxtfile, strerror(errno));
+	}
+
+
+	ast_unlock_path(dir);
+	inprocess_count(recipient->mailbox, recipient->context, -1);
+
+	/* If we copied something, we should store it either to ODBC or IMAP if we are using those. The STORE macro allows us
+	 * to do both with one line and is also safe to use with file storage mode. Also, if we are using ODBC, now is a good
+	 * time to create the voicemail database entry. */
+	if (ast_fileexists(destination, NULL, NULL) > 0) {
+		if (ast_check_realtime("voicemail_data")) {
+			get_date(date, sizeof(date));
+			ast_store_realtime("voicemail_data",
+				"origmailbox", recdata->mailbox,
+				"context", S_OR(recdata->context, ""),
+				"macrocontext", S_OR(recdata->call_macrocontext, ""),
+				"exten", S_OR(recdata->call_extension, ""),
+				"priority", recdata->call_priority,
+				"callerchan", S_OR(recdata->call_callerchan, "Unknown"),
+				"callerid", S_OR(recdata->call_callerid, "Unknown"),
+				"origdate", date,
+				"origtime", time(NULL),
+				"category", S_OR(category, ""),
+				"filename", tmptxtfile,
+				"duration", duration,
+				SENTINEL);
+		}
+
+		STORE(dir, recipient->mailbox, recipient->context, msgnum, NULL, recipient, fmt, 0, vms, "");
+	}
+
+	free_user(recipient);
+	unlink(tmptxtfile);
+	return 0;
+}
 
 /*!
  * \brief Prompts the user and records a voicemail to a mailbox.
@@ -6034,6 +6364,14 @@
 		/* Store information */
 		txt = fdopen(txtdes, "w+");
 		if (txt) {
+			char msg_id[256] = "";
+			char msg_id_hash[256] = "";
+
+			/* Every voicemail msg gets its own unique msg id.  The msg id is the originate time
+			 * plus a hash of the extension, context, and callerid of the channel leaving the msg */
+			snprintf(msg_id_hash, sizeof(msg_id_hash), "%s%s%s", ast_channel_exten(chan), ast_channel_context(chan), callerid);
+			snprintf(msg_id, sizeof(msg_id), "%ld-%d", (long) time(NULL), ast_str_hash(msg_id_hash));
+
 			get_date(date, sizeof(date));
 			ast_callerid_merge(callerid, sizeof(callerid),
 				S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL),
@@ -6054,7 +6392,8 @@
 				"callerid=%s\n"
 				"origdate=%s\n"
 				"origtime=%ld\n"
-				"category=%s\n",
+				"category=%s\n"
+				"msg_id=%s\n",
 				ext,
 				ast_channel_context(chan),
 				ast_channel_macrocontext(chan), 
@@ -6065,7 +6404,8 @@
 				ast_channel_name(chan),
 				callerid,
 				date, (long) time(NULL),
-				category ? category : "");
+				category ? category : "",
+				msg_id);
 		} else {
 			ast_log(AST_LOG_WARNING, "Error opening text file for output\n");
 			inprocess_count(vmu->mailbox, vmu->context, -1);
@@ -6270,7 +6610,7 @@
 	return d;
 }
 
-static int save_to_folder(struct ast_vm_user *vmu, struct vm_state *vms, int msg, int box)
+static int save_to_folder(struct ast_vm_user *vmu, struct vm_state *vms, int msg, int box, int *newmsg)
 {
 #ifdef IMAP_STORAGE
 	/* we must use mbox(x) folder names, and copy the message there */
@@ -6345,6 +6685,10 @@
 		COPY(dir, msg, ddir, x, username, context, sfn, dfn);
 	}
 	ast_unlock_path(ddir);
+
+	if (newmsg) {
+		*newmsg = x;
+	}
 	return 0;
 #endif
 }
@@ -8081,7 +8425,7 @@
 			}
 		} else if ((!strcasecmp(vms->curbox, "INBOX") || !strcasecmp(vms->curbox, "Urgent")) && vms->heard[x] && ast_test_flag(vmu, VM_MOVEHEARD) && !vms->deleted[x]) {
 			/* Move to old folder before deleting */
-			res = save_to_folder(vmu, vms, x, 1);
+			res = save_to_folder(vmu, vms, x, 1, NULL);
 			if (res == ERROR_LOCK_PATH) {
 				/* If save failed do not delete the message */
 				ast_log(AST_LOG_WARNING, "Save failed.  Not moving message: %s.\n", res == ERROR_LOCK_PATH ? "unable to lock path" : "destination folder full");
@@ -8091,7 +8435,7 @@
 			}
 		} else if (vms->deleted[x] && vmu->maxdeletedmsg) {
 			/* Move to deleted folder */

[... 6431 lines stripped ...]



More information about the svn-commits mailing list