| 
									
										
										
										
											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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/options.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
										 |  |  | 
 | 
					
						
							|  |  |  | static char *app_readfile = "ReadFile"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *readfile_synopsis = "ReadFile(varname=file,length)"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *readfile_descrip = | 
					
						
							|  |  |  | "ReadFile(varname=file,length)\n" | 
					
						
							|  |  |  | "  Varname - Result stored here.\n" | 
					
						
							|  |  |  | "  File - The name of the file to read.\n" | 
					
						
							| 
									
										
										
										
											2005-05-16 20:58:35 +00:00
										 |  |  | "  Length - Maximum number of characters to capture.\n"; | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int readfile_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res=0; | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	struct ast_module_user *u; | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 	char *s, *varname=NULL, *file=NULL, *length=NULL, *returnvar=NULL; | 
					
						
							|  |  |  | 	int len=0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-08-21 02:11:39 +00:00
										 |  |  | 	u = ast_module_user_add(chan); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-10 13:22:15 +00:00
										 |  |  | 	s = ast_strdupa(data); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	varname = strsep(&s, "="); | 
					
						
							|  |  |  | 	file = strsep(&s, "|"); | 
					
						
							|  |  |  | 	length = s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!varname || !file) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "No file or variable specified!\n"); | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 		ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (length) { | 
					
						
							|  |  |  | 		if ((sscanf(length, "%d", &len) != 1) || (len < 0)) { | 
					
						
							|  |  |  | 			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); | 
					
						
							|  |  |  | 		free(returnvar); | 
					
						
							| 
									
										
										
										
											2005-02-27 19:07:46 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = ast_unregister_application(app_readfile); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	ast_module_user_hangup_all(); | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return res;	 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							|  |  |  | 	return ast_register_application(app_readfile, readfile_exec, readfile_synopsis, readfile_descrip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stores output of file into a variable"); |