2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Matt O'Gorman <mogorman@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief ReadFile application -- Reads in a File for you.
23 * \author Matt O'Gorman <mogorman@digium.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/options.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/app.h"
43 #include "asterisk/module.h"
45 static char *app_readfile
= "ReadFile";
47 static char *readfile_synopsis
= "ReadFile(varname=file,length)";
49 static char *readfile_descrip
=
50 "ReadFile(varname=file,length)\n"
51 " Varname - Result stored here.\n"
52 " File - The name of the file to read.\n"
53 " Length - Maximum number of characters to capture.\n";
56 static int readfile_exec(struct ast_channel
*chan
, void *data
)
59 struct ast_module_user
*u
;
60 char *s
, *varname
=NULL
, *file
=NULL
, *length
=NULL
, *returnvar
=NULL
;
63 if (ast_strlen_zero(data
)) {
64 ast_log(LOG_WARNING
, "ReadFile require an argument!\n");
68 u
= ast_module_user_add(chan
);
70 s
= ast_strdupa(data
);
72 varname
= strsep(&s
, "=");
73 file
= strsep(&s
, "|");
76 if (!varname
|| !file
) {
77 ast_log(LOG_ERROR
, "No file or variable specified!\n");
78 ast_module_user_remove(u
);
83 if ((sscanf(length
, "%d", &len
) != 1) || (len
< 0)) {
84 ast_log(LOG_WARNING
, "%s is not a positive number, defaulting length to max\n", length
);
89 if ((returnvar
= ast_read_textfile(file
))) {
91 if (len
< strlen(returnvar
))
94 ast_log(LOG_WARNING
, "%s is longer than %d, and %d \n", file
, len
, (int)strlen(returnvar
));
96 pbx_builtin_setvar_helper(chan
, varname
, returnvar
);
99 ast_module_user_remove(u
);
104 static int unload_module(void)
108 res
= ast_unregister_application(app_readfile
);
110 ast_module_user_hangup_all();
115 static int load_module(void)
117 return ast_register_application(app_readfile
, readfile_exec
, readfile_synopsis
, readfile_descrip
);
120 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Stores output of file into a variable");