CHANGES: Update changes log to include r403414 entry
[asterisk/asterisk.git] / apps / app_readfile.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Matt O'Gorman <mogorman@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief ReadFile application -- Reads in a File for you.
22  *
23  * \author Matt O'Gorman <mogorman@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <defaultenabled>no</defaultenabled>
30         <support_level>deprecated</support_level>
31         <replacement>func_env (FILE())</replacement>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/app.h"
42 #include "asterisk/module.h"
43
44 /*** DOCUMENTATION
45         <application name="ReadFile" language="en_US">
46                 <synopsis>
47                         Read the contents of a text file into a channel variable.
48                 </synopsis>
49                 <syntax argsep="=">
50                         <parameter name="varname" required="true">
51                                 <para>Result stored here.</para>
52                         </parameter>
53                         <parameter name="fileparams" required="true">
54                                 <argument name="file" required="true">
55                                         <para>The name of the file to read.</para>
56                                 </argument>
57                                 <argument name="length" required="false">
58                                         <para>Maximum number of characters to capture.</para>
59                                         <para>If not specified defaults to max.</para>
60                                 </argument>
61                         </parameter>
62                 </syntax>
63                 <description>
64                         <para>Read the contents of a text file into channel variable <replaceable>varname</replaceable></para>
65                         <warning><para>ReadFile has been deprecated in favor of Set(varname=${FILE(file,0,length)})</para></warning>
66                 </description>
67                 <see-also>
68                         <ref type="application">System</ref>
69                         <ref type="application">Read</ref>
70                 </see-also>
71         </application>
72  ***/
73
74 static char *app_readfile = "ReadFile";
75
76 static int readfile_exec(struct ast_channel *chan, const char *data)
77 {
78         int res=0;
79         char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
80         int len=0;
81         static int deprecation_warning = 0;
82
83         if (ast_strlen_zero(data)) {
84                 ast_log(LOG_WARNING, "ReadFile require an argument!\n");
85                 return -1;
86         }
87
88         s = ast_strdupa(data);
89
90         varname = strsep(&s, "=");
91         file = strsep(&s, ",");
92         length = s;
93
94         if (deprecation_warning++ % 10 == 0)
95                 ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
96
97         if (!varname || !file) {
98                 ast_log(LOG_ERROR, "No file or variable specified!\n");
99                 return -1;
100         }
101
102         if (length) {
103                 if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) {
104                         ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
105                         len = 0;
106                 }
107         }
108
109         if ((returnvar = ast_read_textfile(file))) {
110                 if (len > 0) {
111                         if (len < strlen(returnvar))
112                                 returnvar[len]='\0';
113                         else
114                                 ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
115                 }
116                 pbx_builtin_setvar_helper(chan, varname, returnvar);
117                 ast_free(returnvar);
118         }
119
120         return res;
121 }
122
123
124 static int unload_module(void)
125 {
126         return ast_unregister_application(app_readfile);
127 }
128
129 static int load_module(void)
130 {
131         return ast_register_application_xml(app_readfile, readfile_exec);
132 }
133
134 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");