/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 2006, Digium, Inc. * Copyright (C) 2006, Claude Patry * * 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. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. *//*! \file * * \Shell function to return the value of a system call. * * \ Inspiration from Russell! Thank You! * * \author Brandon Kruse <bkruse@digium.com> */#include "asterisk.h"ASTERISK_FILE_VERSION(__FILE__, "$Revision: 40722 $")#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include "asterisk/module.h"#include "asterisk/channel.h"#include "asterisk/pbx.h"#include "asterisk/logger.h"#include "asterisk/utils.h"#include "asterisk/app.h"static int shell_helper(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len){ if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Missing Argument! Example: Set(foo=${SHELL(echo \"bar\")})\n"); return -1; } if (len >= 1) { FILE *ptr; char to[100]; ptr = popen(data, "r");// fgets (buf, len, ptr); while (feof(ptr) == 0) { fgets (to, len, ptr); strncat(buf, to, len -strlen(buf) -1); ast_log(LOG_NOTICE, "data is %s\n", to); } } return 0;}static int shell_error(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)// struct ast_channel *chan, char *cmd, char *parse, const char *value){ *buf = '\0'; ast_log(LOG_WARNING, "Invalid assignment! Correct Example: Set(foo=${SHELL(echo \"bar\")})\n"); return -1;}static struct ast_custom_function shell_function = { .name = "SHELL", .synopsis = "Executes a command as if you were at a shell.", .syntax = "Shell(<command>)", .read = shell_helper, .write = shell_error, .desc = "Returns the value from a system command\n" " Example: Set(foo=${SHELL(echo \"bar\")})\n" " Example: Noop(${foo}) will yield \"bar\"\n",};static int unload_module(void){ return ast_custom_function_unregister(&shell_function);}static int load_module(void){ return ast_custom_function_register(&shell_function);}AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Shell dialplan function");