00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: scriptconfig.c,v 1.11 2008-10-17 15:57:43 sven Exp $";
00022 #endif
00023
00024 #include <eggdrop/eggdrop.h>
00025 #include "egg_script_internal.h"
00026
00027 static void *resolve_path(void *root, char *path)
00028 {
00029 char *save, *work, *period;
00030
00031 if (!root) return(NULL);
00032
00033 work = strdup(path);
00034 save = work;
00035 while ((period = strchr(work, '.'))) {
00036 *period = 0;
00037 root = config_lookup_section(root, work, 0, NULL);
00038 if (!root) goto done;
00039 work = period+1;
00040 }
00041 root = config_lookup_section(root, work, 0, NULL);
00042 done:
00043 free(save);
00044 return(root);
00045 }
00046
00047 static char *script_config_get(char *handle, char *path)
00048 {
00049 void *root;
00050 char *str = NULL;
00051
00052 if (!handle) handle = "eggdrop";
00053
00054 root = config_get_root(handle);
00055 root = resolve_path(root, path);
00056 if (!root) return(NULL);
00057
00058 config_get_str(&str, root, NULL);
00059 return(str);
00060 }
00061
00062 static int script_config_set(char *handle, char *path, char *val)
00063 {
00064 void *root;
00065
00066 if (!handle) handle = "eggdrop";
00067
00068 root = config_get_root(handle);
00069 root = resolve_path(root, path);
00070 if (!root) return(-1);
00071
00072 config_set_str(val, root, NULL);
00073 return(0);
00074 }
00075
00076 static int script_config_exists(char *handle, char *path)
00077 {
00078 void *root;
00079 char *save, *work, *period;
00080 int r = 0;
00081
00082 if (!handle) handle = "eggdrop";
00083
00084 root = config_get_root(handle);
00085 if (!root) return(0);
00086
00087 work = strdup(path);
00088 save = work;
00089 while ((period = strchr(work, '.'))) {
00090 *period = 0;
00091 if (!config_exists(root, work, 0, NULL)) goto notfound;
00092 root = config_lookup_section(root, work, 0, NULL);
00093 if (!root) goto notfound;
00094 work = period+1;
00095 }
00096 r = (config_exists(root, work, 0, NULL) == NULL) ? 0 : 1;
00097
00098 notfound:
00099 free(save);
00100 return(r);
00101 }
00102
00103 static int script_config_load(char *handle, char *fname)
00104 {
00105 void *root;
00106
00107 root = config_load(fname);
00108 if (!root) return(-1);
00109 config_set_root(handle, root);
00110 return(0);
00111 }
00112
00113 script_command_t script_config_cmds[] = {
00114 {"config", "exists", script_config_exists, NULL, 1, "ss", "?handle? path", SCRIPT_INTEGER, SCRIPT_VAR_ARGS|SCRIPT_VAR_FRONT},
00115 {"config", "get", script_config_get, NULL, 1, "ss", "?handle? path", SCRIPT_STRING, SCRIPT_VAR_ARGS|SCRIPT_VAR_FRONT},
00116 {"config", "set", script_config_set, NULL, 2, "sss", "?handle? path value", SCRIPT_INTEGER, SCRIPT_VAR_ARGS|SCRIPT_VAR_FRONT},
00117 {"config", "load", script_config_load, NULL, 2, "ss", "handle filename", SCRIPT_INTEGER, 0},
00118 {"config", "save", config_save, NULL, 2, "ss", "handle filename", SCRIPT_INTEGER, 0},
00119 {0}
00120 };