remove lots of useless checks of the result of ast_strdupa
[asterisk/asterisk.git] / apps / app_page.c
old mode 100755 (executable)
new mode 100644 (file)
index 8db0356..b4eb1a5
@@ -20,6 +20,9 @@
  *
  * \brief page() - Paging application
  *
+ * \author Mark Spencer <markster@digium.com>
+ *
+ * \ingroup applications
  */
 
 #include <stdio.h>
@@ -39,7 +42,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/file.h"
 #include "asterisk/app.h"
-
+#include "asterisk/chanvars.h"
+#include "asterisk/utils.h"
 
 static const char *tdesc = "Page Multiple Phones";
 
@@ -53,20 +57,21 @@ static const char *page_descrip =
 "them into a conference bridge as muted participants.  The original\n"
 "caller is dumped into the conference as a speaker and the room is\n"
 "destroyed when the original caller leaves.  Valid options are:\n"
-"      d - full duplex audio\n"
-"      q - quiet, do not play beep to caller\n"
-"Always returns -1.\n";
+"        d - full duplex audio\n"
+"       q - quiet, do not play beep to caller\n";
 
 STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
 
-#define PAGE_DUPLEX (1 << 0)
-#define PAGE_QUIET  (1 << 1)
+enum {
+       PAGE_DUPLEX = (1 << 0),
+       PAGE_QUIET = (1 << 1),
+} page_opt_flags;
 
-AST_DECLARE_OPTIONS(page_opts,{
-       ['d'] = { PAGE_DUPLEX },
-       ['q'] = { PAGE_QUIET },
+AST_APP_OPTIONS(page_opts, {
+       AST_APP_OPTION('d', PAGE_DUPLEX),
+       AST_APP_OPTION('q', PAGE_QUIET),
 });
 
 struct calloutdata {
@@ -75,13 +80,14 @@ struct calloutdata {
        char tech[64];
        char resource[256];
        char meetmeopts[64];
+       struct ast_variable *variables;
 };
 
 static void *page_thread(void *data)
 {
        struct calloutdata *cd = data;
        ast_pbx_outgoing_app(cd->tech, AST_FORMAT_SLINEAR, cd->resource, 30000,
-               "MeetMe", cd->meetmeopts, NULL, 0, cd->cidnum, cd->cidname, NULL, NULL);
+               "MeetMe", cd->meetmeopts, NULL, 0, cd->cidnum, cd->cidname, cd->variables, NULL);
        free(cd);
        return NULL;
 }
@@ -89,16 +95,40 @@ static void *page_thread(void *data)
 static void launch_page(struct ast_channel *chan, const char *meetmeopts, const char *tech, const char *resource)
 {
        struct calloutdata *cd;
+       const char *varname;
+       struct ast_variable *lastvar = NULL;
+       struct ast_var_t *varptr;
        pthread_t t;
        pthread_attr_t attr;
-       cd = malloc(sizeof(struct calloutdata));
-       if (cd) {
-               memset(cd, 0, sizeof(struct calloutdata));
+       if ((cd = ast_calloc(1, sizeof(*cd)))) {
                ast_copy_string(cd->cidnum, chan->cid.cid_num ? chan->cid.cid_num : "", sizeof(cd->cidnum));
                ast_copy_string(cd->cidname, chan->cid.cid_name ? chan->cid.cid_name : "", sizeof(cd->cidname));
                ast_copy_string(cd->tech, tech, sizeof(cd->tech));
                ast_copy_string(cd->resource, resource, sizeof(cd->resource));
                ast_copy_string(cd->meetmeopts, meetmeopts, sizeof(cd->meetmeopts));
+
+               AST_LIST_TRAVERSE(&chan->varshead, varptr, entries) {
+                       if (!(varname = ast_var_full_name(varptr)))
+                               continue;
+                       if (varname[0] == '_') {
+                               struct ast_variable *newvar = NULL;
+
+                               if (varname[1] == '_') {
+                                       newvar = ast_variable_new(varname, ast_var_value(varptr));
+                               } else {
+                                       newvar = ast_variable_new(&varname[1], ast_var_value(varptr));
+                               }
+
+                               if (newvar) {
+                                       if (lastvar)
+                                               lastvar->next = newvar;
+                                       else
+                                               cd->variables = newvar;
+                                       lastvar = newvar;
+                               }
+                       }
+               }
+
                pthread_attr_init(&attr);
                pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                if (ast_pthread_create(&t, &attr, page_thread, cd)) {
@@ -120,7 +150,7 @@ static int page_exec(struct ast_channel *chan, void *data)
        char *tmp;
        int res=0;
 
-       if (!data || ast_strlen_zero(data)) {
+       if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
                return -1;
        }
@@ -134,15 +164,10 @@ static int page_exec(struct ast_channel *chan, void *data)
        };
 
        options = ast_strdupa(data);
-       if (!options) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               LOCAL_USER_REMOVE(u);
-               return -1;
-       }
 
        tmp = strsep(&options, "|");
        if (options)
-               ast_parseoptions(page_opts, &flags, NULL, options);
+               ast_app_parse_options(page_opts, &flags, NULL, options);
 
        snprintf(meetmeopts, sizeof(meetmeopts), "%ud|%sqxdw", confid, ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m");
        while ((tech = strsep(&tmp, "&"))) {