Applications no longer need to call ast_module_user_add and ast_module_user_remove...
[asterisk/asterisk.git] / apps / app_dictate.c
index dfa1c4f..566ff0f 100644 (file)
  *
  * \brief Virtual Dictation Machine Application For Asterisk
  *
+ * \author Anthony Minessale II <anthmct@yahoo.com>
+ *
  * \ingroup applications
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
-#include <sys/stat.h>  /* for mkdir */
-
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include <sys/stat.h>
 
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
@@ -44,16 +46,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/lock.h"
 #include "asterisk/app.h"
 
-static char *tdesc = "Virtual Dictation Machine";
 static char *app = "Dictate";
 static char *synopsis = "Virtual Dictation Machine";
 static char *desc = "  Dictate([<base_dir>[|<filename>]])\n"
 "Start dictation machine using optional base dir for files.\n";
 
 
-STANDARD_LOCAL_USER;
-LOCAL_USER_DECL;
-
 typedef enum {
        DFLAG_RECORD = (1 << 0),
        DFLAG_PLAY = (1 << 1),
@@ -80,16 +78,19 @@ static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
 
 static int dictate_exec(struct ast_channel *chan, void *data)
 {
-       char *mydata, *argv[3], *path = NULL, filein[256], *filename = "";
+       char *path = NULL, filein[256], *filename = "";
+       char *parse;
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(base);
+               AST_APP_ARG(filename);
+       );
        char dftbase[256];
        char *base;
        struct ast_flags flags = {0};
        struct ast_filestream *fs;
        struct ast_frame *f = NULL;
-       struct localuser *u;
        int ffactor = 320 * 80,
                res = 0,
-               argc = 0,
                done = 0,
                oldr = 0,
                lastop = 0,
@@ -99,26 +100,25 @@ static int dictate_exec(struct ast_channel *chan, void *data)
                len = 0,
                maxlen = 0,
                mode = 0;
-               
-       LOCAL_USER_ADD(u);
        
        snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
-       if (!ast_strlen_zero(data) && (mydata = ast_strdupa(data))) {
-               argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
-       }
+       if (!ast_strlen_zero(data)) {
+               parse = ast_strdupa(data);
+               AST_STANDARD_APP_ARGS(args, parse);
+       } else
+               args.argc = 0;
        
-       if (argc) {
-               base = argv[0];
+       if (args.argc && !ast_strlen_zero(args.base)) {
+               base = args.base;
        } else {
                base = dftbase;
        }
-       if (argc && argv[1]) {
-               filename = argv[1];
+       if (args.argc > 1 && args.filename) {
+               filename = args.filename;
        } 
        oldr = chan->readformat;
        if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
                ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
-               LOCAL_USER_REMOVE(u);
                return -1;
        }
 
@@ -135,7 +135,7 @@ static int dictate_exec(struct ast_channel *chan, void *data)
                        ast_copy_string(filein, filename, sizeof(filein));
                        filename = "";
                }
-               mkdir(base, 0755);
+               ast_mkdir(base, 0755);
                len = strlen(base) + strlen(filein) + 2;
                if (!path || len > maxlen) {
                        path = alloca(len);
@@ -146,7 +146,7 @@ static int dictate_exec(struct ast_channel *chan, void *data)
                }
 
                snprintf(path, len, "%s/%s", base, filein);
-               fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, 0700);
+               fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, AST_FILE_MODE);
                mode = DMODE_PLAY;
                memset(&flags, 0, sizeof(flags));
                ast_set_flag(&flags, DFLAG_PAUSE);
@@ -263,7 +263,8 @@ static int dictate_exec(struct ast_channel *chan, void *data)
                                                if (lastop != DFLAG_PLAY) {
                                                        lastop = DFLAG_PLAY;
                                                        ast_closestream(fs);
-                                                       fs = ast_openstream(chan, path, chan->language);
+                                                       if (!(fs = ast_openstream(chan, path, chan->language)))
+                                                               break;
                                                        ast_seekstream(fs, samples, SEEK_SET);
                                                        chan->stream = NULL;
                                                }
@@ -303,7 +304,7 @@ static int dictate_exec(struct ast_channel *chan, void *data)
                                                } else {
                                                        oflags |= O_APPEND;
                                                }
-                                               fs = ast_writefile(path, "raw", NULL, oflags, 0, 0700);
+                                               fs = ast_writefile(path, "raw", NULL, oflags, 0, AST_FILE_MODE);
                                                if (ast_test_flag(&flags, DFLAG_TRUNC)) {
                                                        ast_seekstream(fs, 0, SEEK_SET);
                                                        ast_clear_flag(&flags, DFLAG_TRUNC);
@@ -325,40 +326,19 @@ static int dictate_exec(struct ast_channel *chan, void *data)
        if (oldr) {
                ast_set_read_format(chan, oldr);
        }
-       LOCAL_USER_REMOVE(u);
-       return res;
+       return 0;
 }
 
-int unload_module(void)
+static int unload_module(void)
 {
        int res;
-
        res = ast_unregister_application(app);
-       
-       STANDARD_HANGUP_LOCALUSERS;
-       
        return res;
 }
 
-int load_module(void)
+static int load_module(void)
 {
        return ast_register_application(app, dictate_exec, synopsis, desc);
 }
 
-char *description(void)
-{
-       return tdesc;
-}
-
-int usecount(void)
-{
-       int res;
-       STANDARD_USECOUNT(res);
-       return res;
-}
-
-char *key()
-{
-       return ASTERISK_GPL_KEY;
-}
-
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Virtual Dictation Machine");