00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/types.h>
00034
00035 #include "asterisk/module.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/utils.h"
00040 #include "asterisk/app.h"
00041
00042 static int shell_helper(struct ast_channel *chan, const char *cmd, char *data,
00043 char *buf, size_t len)
00044 {
00045 if (ast_strlen_zero(data)) {
00046 ast_log(LOG_WARNING, "Missing Argument! Example: Set(foo=${SHELL(echo \"bar\")})\n");
00047 return -1;
00048 }
00049
00050 if (len >= 1) {
00051 FILE *ptr;
00052 char plbuff[4096];
00053
00054 ptr = popen(data, "r");
00055 while (fgets(plbuff, sizeof(plbuff), ptr)) {
00056 strncat(buf, plbuff, len - strlen(buf) - 1);
00057 }
00058 pclose(ptr);
00059 }
00060
00061 return 0;
00062 }
00063
00064 static struct ast_custom_function shell_function = {
00065 .name = "SHELL",
00066 .synopsis = "Executes a command as if you were at a shell.",
00067 .syntax = "SHELL(<command>)",
00068 .read = shell_helper,
00069 .desc =
00070 "Returns the value from a system command\n"
00071 " Example: Set(foo=${SHELL(echo \"bar\")})\n"
00072 " Note: When using the SHELL() dialplan function, your \"SHELL\" is /bin/sh,\n"
00073 " which may differ as to the underlying shell, depending upon your production\n"
00074 " platform. Also keep in mind that if you are using a common path, you should\n"
00075 " be mindful of race conditions that could result from two calls running\n"
00076 " SHELL() simultaneously.\n",
00077 };
00078
00079 static int unload_module(void)
00080 {
00081 return ast_custom_function_unregister(&shell_function);
00082 }
00083
00084 static int load_module(void)
00085 {
00086 return ast_custom_function_register(&shell_function);
00087 }
00088
00089 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SHELL dialplan function");
00090