| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Matt O'Gorman <mogorman@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief ReadFile application -- Reads in a File for you. | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * \author Matt O'Gorman <mogorman@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup applications | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:28:54 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<defaultenabled>no</defaultenabled> | 
					
						
							|  |  |  | 	<support_level>deprecated</support_level> | 
					
						
							|  |  |  | 	<replacement>func_env (FILE())</replacement> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/app.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | /*** DOCUMENTATION
 | 
					
						
							|  |  |  | 	<application name="ReadFile" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Read the contents of a text file into a channel variable. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax argsep="="> | 
					
						
							|  |  |  | 			<parameter name="varname" required="true"> | 
					
						
							|  |  |  | 				<para>Result stored here.</para> | 
					
						
							|  |  |  | 			</parameter> | 
					
						
							|  |  |  | 			<parameter name="fileparams" required="true"> | 
					
						
							|  |  |  | 				<argument name="file" required="true"> | 
					
						
							|  |  |  | 					<para>The name of the file to read.</para> | 
					
						
							|  |  |  | 				</argument> | 
					
						
							|  |  |  | 				<argument name="length" required="false"> | 
					
						
							|  |  |  | 					<para>Maximum number of characters to capture.</para> | 
					
						
							|  |  |  | 					<para>If not specified defaults to max.</para> | 
					
						
							|  |  |  | 				</argument> | 
					
						
							|  |  |  | 			</parameter> | 
					
						
							|  |  |  | 		</syntax> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para>Read the contents of a text file into channel variable <replaceable>varname</replaceable></para> | 
					
						
							|  |  |  | 			<warning><para>ReadFile has been deprecated in favor of Set(varname=${FILE(file,0,length)})</para></warning> | 
					
						
							|  |  |  | 		</description> | 
					
						
							| 
									
										
										
										
											2008-11-05 12:13:57 +00:00
										 |  |  | 		<see-also> | 
					
						
							|  |  |  | 			<ref type="application">System</ref> | 
					
						
							|  |  |  | 			<ref type="application">Read</ref> | 
					
						
							|  |  |  | 		</see-also> | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	</application> | 
					
						
							|  |  |  |  ***/ | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | static char *app_readfile = "ReadFile"; | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-21 21:13:09 +00:00
										 |  |  | static int readfile_exec(struct ast_channel *chan, const char *data) | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res=0; | 
					
						
							|  |  |  | 	char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL; | 
					
						
							|  |  |  | 	int len=0; | 
					
						
							| 
									
										
										
										
											2007-11-08 17:32:15 +00:00
										 |  |  | 	static int deprecation_warning = 0; | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-26 19:48:14 +00:00
										 |  |  | 	if (ast_strlen_zero(data)) { | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "ReadFile require an argument!\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-10 13:22:15 +00:00
										 |  |  | 	s = ast_strdupa(data); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	varname = strsep(&s, "="); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	file = strsep(&s, ","); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 	length = s; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-08 17:32:15 +00:00
										 |  |  | 	if (deprecation_warning++ % 10 == 0) | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "ReadFile has been deprecated in favor of Set(%s=${FILE(%s,0,%s)})\n", varname, file, length); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 	if (!varname || !file) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "No file or variable specified!\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (length) { | 
					
						
							| 
									
										
										
										
											2009-08-10 19:20:57 +00:00
										 |  |  | 		if ((sscanf(length, "%30d", &len) != 1) || (len < 0)) { | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "%s is not a positive number, defaulting length to max\n", length); | 
					
						
							|  |  |  | 			len = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-23 20:15:01 +00:00
										 |  |  | 	if ((returnvar = ast_read_textfile(file))) { | 
					
						
							|  |  |  | 		if (len > 0) { | 
					
						
							|  |  |  | 			if (len < strlen(returnvar)) | 
					
						
							|  |  |  | 				returnvar[len]='\0'; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "%s is longer than %d, and %d \n", file, len, (int)strlen(returnvar)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		pbx_builtin_setvar_helper(chan, varname, returnvar); | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free(returnvar); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-07-16 14:39:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-07-16 13:35:20 +00:00
										 |  |  | 	return ast_unregister_application(app_readfile); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	return ast_register_application_xml(app_readfile, readfile_exec); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable"); |