Sat Aug 6 00:39:21 2011

Asterisk developer's documentation


app_readfile.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Matt O'Gorman <mogorman@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief ReadFile application -- Reads in a File for you.
00022  *
00023  * \author Matt O'Gorman <mogorman@digium.com>
00024  *
00025  * \ingroup applications
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211528 $")
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <unistd.h>
00035 #include <string.h>
00036 
00037 #include "asterisk/file.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/options.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/app.h"
00043 #include "asterisk/module.h"
00044 
00045 static char *app_readfile = "ReadFile";
00046 
00047 static char *readfile_synopsis = "ReadFile(varname=file,length)";
00048 
00049 static char *readfile_descrip =
00050 "ReadFile(varname=file,length)\n"
00051 "  Varname - Result stored here.\n"
00052 "  File - The name of the file to read.\n"
00053 "  Length - Maximum number of characters to capture.\n";
00054 
00055 
00056 static int readfile_exec(struct ast_channel *chan, void *data)
00057 {
00058    int res=0;
00059    struct ast_module_user *u;
00060    char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
00061    int len=0;
00062 
00063    if (ast_strlen_zero(data)) {
00064       ast_log(LOG_WARNING, "ReadFile require an argument!\n");
00065       return -1;
00066    }
00067 
00068    u = ast_module_user_add(chan);
00069 
00070    s = ast_strdupa(data);
00071 
00072    varname = strsep(&s, "=");
00073    file = strsep(&s, "|");
00074    length = s;
00075 
00076    if (!varname || !file) {
00077       ast_log(LOG_ERROR, "No file or variable specified!\n");
00078       ast_module_user_remove(u);
00079       return -1;
00080    }
00081 
00082    if (length) {
00083       if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) {
00084          ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
00085          len = 0;
00086       }
00087    }
00088 
00089    if ((returnvar = ast_read_textfile(file))) {
00090       if (len > 0) {
00091          if (len < strlen(returnvar))
00092             returnvar[len]='\0';
00093          else
00094             ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
00095       }
00096       pbx_builtin_setvar_helper(chan, varname, returnvar);
00097       free(returnvar);
00098    }
00099    ast_module_user_remove(u);
00100    return res;
00101 }
00102 
00103 
00104 static int unload_module(void)
00105 {
00106    int res;
00107 
00108    res = ast_unregister_application(app_readfile);
00109    
00110    ast_module_user_hangup_all();
00111 
00112    return res; 
00113 }
00114 
00115 static int load_module(void)
00116 {
00117    return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip);
00118 }
00119 
00120 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");

Generated on Sat Aug 6 00:39:21 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7