Add explanation of strange flag setup in app_meetme (stolen from Mark's message to...
[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 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/file.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/app.h"
36 #include "asterisk/module.h"
37
38 /*** DOCUMENTATION
39         <application name="ReadFile" language="en_US">
40                 <synopsis>
41                         Read the contents of a text file into a channel variable.
42                 </synopsis>
43                 <syntax argsep="=">
44                         <parameter name="varname" required="true">
45                                 <para>Result stored here.</para>
46                         </parameter>
47                         <parameter name="fileparams" required="true">
48                                 <argument name="file" required="true">
49                                         <para>The name of the file to read.</para>
50                                 </argument>
51                                 <argument name="length" required="false">
52                                         <para>Maximum number of characters to capture.</para>
53                                         <para>If not specified defaults to max.</para>
54                                 </argument>
55                         </parameter>
56                 </syntax>
57                 <description>
58                         <para>Read the contents of a text file into channel variable <replaceable>varname</replaceable></para>
59                         <warning><para>ReadFile has been deprecated in favor of Set(varname=${FILE(file,0,length)})</para></warning>
60                 </description>
61                 <see-also>
62                         <ref type="application">System</ref>
63                         <ref type="application">Read</ref>
64                 </see-also>
65         </application>
66  ***/
67
68 static char *app_readfile = "ReadFile";
69
70 static int readfile_exec(struct ast_channel *chan, const char *data)
71 {
72         int res=0;
73         char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL;
74         int len=0;
75         static int deprecation_warning = 0;
76
77         if (ast_strlen_zero(data)) {
78                 ast_log(LOG_WARNING, "ReadFile require an argument!\n");
79                 return -1;
80         }
81
82         s = ast_strdupa(data);
83
84         varname = strsep(&s, "=");
85         file = strsep(&s, ",");
86         length = s;
87
88         if (deprecation_warning++ % 10 == 0)
89                 ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length);
90
91         if (!varname || !file) {
92                 ast_log(LOG_ERROR, "No file or variable specified!\n");
93                 return -1;
94         }
95
96         if (length) {
97                 if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) {
98                         ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length);
99                         len = 0;
100                 }
101         }
102
103         if ((returnvar = ast_read_textfile(file))) {
104                 if (len > 0) {
105                         if (len < strlen(returnvar))
106                                 returnvar[len]='\0';
107                         else
108                                 ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar));
109                 }
110                 pbx_builtin_setvar_helper(chan, varname, returnvar);
111                 ast_free(returnvar);
112         }
113
114         return res;
115 }
116
117
118 static int unload_module(void)
119 {
120         return ast_unregister_application(app_readfile);
121 }
122
123 static int load_module(void)
124 {
125         return ast_register_application_xml(app_readfile, readfile_exec);
126 }
127
128 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable");