Thu Jul 9 13:40:20 2009

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: 89512 $")
00031 
00032 #include "asterisk/file.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/app.h"
00036 #include "asterisk/module.h"
00037 
00038 static char *app_readfile = "ReadFile";
00039 
00040 static char *readfile_synopsis = "Read the contents of a text file into a channel variable";
00041 
00042 static char *readfile_descrip =
00043 "ReadFile(varname=file,length)\n"
00044 "  varname  - Result stored here.\n"
00045 "  file     - The name of the file to read.\n"
00046 "  length   - Maximum number of characters to capture.\n";
00047 
00048 
00049 static int readfile_exec(struct ast_channel *chan, void *data)
00050 {
00051    int res=0;
00052    char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
00053    int len=0;
00054    static int deprecation_warning = 0;
00055 
00056    if (ast_strlen_zero(data)) {
00057       ast_log(LOG_WARNING, "ReadFile require an argument!\n");
00058       return -1;
00059    }
00060 
00061    s = ast_strdupa(data);
00062 
00063    varname = strsep(&s, "=");
00064    file = strsep(&s, ",");
00065    length = s;
00066 
00067    if (deprecation_warning++ % 10 == 0)
00068       ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
00069 
00070    if (!varname || !file) {
00071       ast_log(LOG_ERROR, "No file or variable specified!\n");
00072       return -1;
00073    }
00074 
00075    if (length) {
00076       if ((sscanf(length, "%d", &len) != 1) || (len < 0)) {
00077          ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
00078          len = 0;
00079       }
00080    }
00081 
00082    if ((returnvar = ast_read_textfile(file))) {
00083       if (len > 0) {
00084          if (len < strlen(returnvar))
00085             returnvar[len]='\0';
00086          else
00087             ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
00088       }
00089       pbx_builtin_setvar_helper(chan, varname, returnvar);
00090       ast_free(returnvar);
00091    }
00092 
00093    return res;
00094 }
00095 
00096 
00097 static int unload_module(void)
00098 {
00099    return ast_unregister_application(app_readfile);
00100 }
00101 
00102 static int load_module(void)
00103 {
00104    return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip);
00105 }
00106 
00107 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");

Generated on Thu Jul 9 13:40:20 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7