00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "asterisk.h"
00025
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00027
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <sys/types.h>
00032 #include <sys/stat.h>
00033
00034 #include "asterisk/module.h"
00035 #include "asterisk/channel.h"
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/logger.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/app.h"
00040
00041 static int env_read(struct ast_channel *chan, char *cmd, char *data,
00042 char *buf, size_t len)
00043 {
00044 char *ret = NULL;
00045
00046 *buf = '\0';
00047
00048 if (data)
00049 ret = getenv(data);
00050
00051 if (ret)
00052 ast_copy_string(buf, ret, len);
00053
00054 return 0;
00055 }
00056
00057 static int env_write(struct ast_channel *chan, char *cmd, char *data,
00058 const char *value)
00059 {
00060 if (!ast_strlen_zero(data)) {
00061 if (!ast_strlen_zero(value)) {
00062 setenv(data, value, 1);
00063 } else {
00064 unsetenv(data);
00065 }
00066 }
00067
00068 return 0;
00069 }
00070
00071 static int stat_read(struct ast_channel *chan, char *cmd, char *data,
00072 char *buf, size_t len)
00073 {
00074 char *action;
00075 struct stat s;
00076
00077 ast_copy_string(buf, "0", len);
00078
00079 action = strsep(&data, "|");
00080 if (stat(data, &s)) {
00081 return 0;
00082 } else {
00083 switch (*action) {
00084 case 'e':
00085 strcpy(buf, "1");
00086 break;
00087 case 's':
00088 snprintf(buf, len, "%d", (unsigned int) s.st_size);
00089 break;
00090 case 'f':
00091 snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
00092 break;
00093 case 'd':
00094 snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
00095 break;
00096 case 'M':
00097 snprintf(buf, len, "%d", (int) s.st_mtime);
00098 break;
00099 case 'A':
00100 snprintf(buf, len, "%d", (int) s.st_mtime);
00101 break;
00102 case 'C':
00103 snprintf(buf, len, "%d", (int) s.st_ctime);
00104 break;
00105 case 'm':
00106 snprintf(buf, len, "%o", (int) s.st_mode);
00107 break;
00108 }
00109 }
00110
00111 return 0;
00112 }
00113
00114 static struct ast_custom_function env_function = {
00115 .name = "ENV",
00116 .synopsis = "Gets or sets the environment variable specified",
00117 .syntax = "ENV(<envname>)",
00118 .read = env_read,
00119 .write = env_write,
00120 };
00121
00122 static struct ast_custom_function stat_function = {
00123 .name = "STAT",
00124 .synopsis = "Does a check on the specified file",
00125 .syntax = "STAT(<flag>,<filename>)",
00126 .read = stat_read,
00127 .desc =
00128 "flag may be one of the following:\n"
00129 " d - Checks if the file is a directory\n"
00130 " e - Checks if the file exists\n"
00131 " f - Checks if the file is a regular file\n"
00132 " m - Returns the file mode (in octal)\n"
00133 " s - Returns the size (in bytes) of the file\n"
00134 " A - Returns the epoch at which the file was last accessed\n"
00135 " C - Returns the epoch at which the inode was last changed\n"
00136 " M - Returns the epoch at which the file was last modified\n",
00137 };
00138
00139 static int unload_module(void)
00140 {
00141 int res = 0;
00142
00143 res |= ast_custom_function_unregister(&env_function);
00144 res |= ast_custom_function_unregister(&stat_function);
00145
00146 return res;
00147 }
00148
00149 static int load_module(void)
00150 {
00151 int res = 0;
00152
00153 res |= ast_custom_function_register(&env_function);
00154 res |= ast_custom_function_register(&stat_function);
00155
00156 return res;
00157 }
00158
00159 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");