static int config (oconfig_item_t *ci) { int status = 0; int i; oconfig_item_t *lci = NULL; /* legacy config */ for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("Instance", child->key) == 0 && child->children_num > 0) config_add (child); else { /* legacy mode - convert to <Instance ...> config */ if (lci == NULL) { lci = malloc (sizeof(*lci)); if (lci == NULL) { ERROR ("apache plugin: malloc failed."); return (-1); } memset (lci, '\0', sizeof (*lci)); } lci->children_num++; lci->children = realloc (lci->children, lci->children_num * sizeof (*child)); if (lci->children == NULL) { ERROR ("apache plugin: realloc failed."); return (-1); } memcpy (&lci->children[lci->children_num-1], child, sizeof (*child)); } } /* for (ci->children) */ if (lci) { /* create a <Instance ""> entry */ lci->key = "Instance"; lci->values_num = 1; lci->values = (oconfig_value_t *) malloc (lci->values_num * sizeof (oconfig_value_t)); lci->values[0].type = OCONFIG_TYPE_STRING; lci->values[0].value.string = ""; status = config_add (lci); sfree (lci->values); sfree (lci->children); sfree (lci); } return status; } /* int config */
void read_config_options(avl_tree_t *config_tree, const char *prefix) { list_node_t *node, *next; size_t prefix_len = prefix ? strlen(prefix) : 0; for(node = cmdline_conf->tail; node; node = next) { config_t *orig_cfg, *cfg = (config_t *)node->data; next = node->prev; if(!prefix) { if(strchr(cfg->variable, '.')) continue; node->data = NULL; list_unlink_node(cmdline_conf, node); } else { if(strncmp(prefix, cfg->variable, prefix_len) || cfg->variable[prefix_len] != '.') continue; /* Because host configuration is parsed again when reconnecting, nodes must not be freed when a prefix is given. */ orig_cfg = cfg; cfg = new_config(); cfg->variable = xstrdup(orig_cfg->variable + prefix_len + 1); cfg->value = xstrdup(orig_cfg->value); cfg->file = NULL; cfg->line = orig_cfg->line; } config_add(config_tree, cfg); } }
bool node_write_devclass(meshlink_handle_t *mesh, node_t *n) { if(n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) return false; bool result = false; splay_tree_t *config_tree; init_configuration(&config_tree); // ignore read errors; in case the file does not exist we will create it read_host_config(mesh, config_tree, n->name); config_t* cnf = lookup_config(config_tree, "DeviceClass"); if(!cnf) { cnf = new_config(); cnf->variable = xstrdup("DeviceClass"); config_add(config_tree, cnf); } set_config_int(cnf, n->devclass); if(!write_host_config(mesh, config_tree, n->name)) goto fail; result = true; fail: exit_configuration(&config_tree); return result; }
void config_init(){ if(config_init_status == 1) return; config_init_status = 1; FILE *fp; long len; char *data; fp = fopen("config.json", "rb"); if (fp == NULL) return; fseek(fp, 0, SEEK_END); len = ftell(fp); fseek(fp, 0, SEEK_SET); data= (char*) safeMalloc(len + 1); fread(data, 1, len, fp); fclose(fp); cJSON *json = cJSON_Parse(data); cJSON *subitem = json->child; safeFree(data); while (subitem){ if(subitem->type == cJSON_String) config_add(subitem->string, subitem->valuestring); if(subitem->type == cJSON_Number){ sprintf(data, "%d", subitem->valueint); config_add(subitem->string, data); } if(subitem->type == cJSON_True) config_add(subitem->string, "1"); if(subitem->type == cJSON_False) config_add(subitem->string, "0"); subitem = subitem->next; } cJSON_Delete(json); }
/* Parse a configuration file and put the results in the configuration tree starting at *base. */ bool read_config_file(avl_tree_t *config_tree, const char *fname) { FILE *fp; char buffer[MAX_STRING_SIZE]; char *line; int lineno = 0; bool ignore = false; config_t *cfg; bool result = false; fp = fopen(fname, "r"); if(!fp) { logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno)); return false; } for(;;) { line = readline(fp, buffer, sizeof buffer); if(!line) { if(feof(fp)) result = true; break; } lineno++; if(!*line || *line == '#') continue; if(ignore) { if(!strncmp(line, "-----END", 8)) ignore = false; continue; } if(!strncmp(line, "-----BEGIN", 10)) { ignore = true; continue; } cfg = parse_config_line(line, fname, lineno); if (!cfg) break; config_add(config_tree, cfg); } fclose(fp); return result; }
int config_load(struct config* conf, const char* filename) { config_init(conf); FILE* f=fopen(filename, "r"); if(!f) return -1; int r=1; char* line=NULL; size_t s=0; char header[ITEM_MAXLEN]; char left[ITEM_MAXLEN]; char right[ITEM_MAXLEN]; while((r = getline(&line, &s, f)) != -1) { int h=sscanf(line, "[%[^\n]", header); if(!h) { h=sscanf(line, "%*[ \t]%[^=]=%[^\n]", left, right); if(h==1) { h=sscanf(line, "%*[ \t]%[^\n]", left); if(h==1) config_add(conf, header, left, NULL); else return -1; } else if(h==2) config_add(conf, header, left, right); else return -1; } else { size_t hlen = strnlen(header, ITEM_MAXLEN)-1; if(header[hlen]==']') header[hlen]=0; else return -1; } } free(line); fclose(f); return 0; }
void config_add(struct config* conf, const char* section, const char* key, const char* val) { struct configsection* sect; if((sect=config_find_section(conf, section))) { struct configitem* item; if((item=config_find_item(conf, key, section))) { if(item->val) free(item->val); if(val) item->val=strdup(val); else item->val=NULL; } else item_add(sect, key, val); } else { section_new(conf, section); config_add(conf, section, key, val); } }
static int config (oconfig_item_t *ci) { int status = 0; int i; for (i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp ("Instance", child->key) == 0) config_add (child); else WARNING ("apache plugin: The configuration option " "\"%s\" is not allowed here. Did you " "forget to add an <Instance /> block " "around the configuration?", child->key); } /* for (ci->children) */ return (status); } /* int config */
void bot_parsemsg(struct bot* b, char* msg) { printf("S> %s <E\n", msg); if(bot_pingpong(b, msg)<0) { /* * TODO: Adjust sizes to something less stupid. */ char host[256]; char nick[64]; char cmd[256]; char channel[64]; char message[512]; if(bot_parsecmd(msg, host, nick, cmd, channel, message)==0) { printf("HOST: %s\nNICK: %s\nCMD: %s\nCHANNEL: %s\nMSG: %s\n", host, nick, cmd, channel, message); if(strcmp(cmd, "PRIVMSG")==0) { int allowedtoexec=0; int hasarguments=0; const char** authedusers = config_getvals(b->conf, "authorized_users"); if(!*authedusers[0] && strcmp(message, "iamyourfather")==0) { #ifdef DEBUG printf("Checking authed users.\n"); #endif config_add(b->conf, "authorized_users", host); CMD(b->conn, "PRIVMSG", nick, "Admin user added."); allowedtoexec=1; } for(int i=0; authedusers[i]; ++i) { if(strcmp(host, authedusers[i])==0) { allowedtoexec=1; break; } } #ifdef DEBUG printf("Allowed to exec: %d\n", allowedtoexec); #endif if(allowedtoexec && is_publiccommand(message, NULL)!=0) { if(is_cbotcommand(message)==0) { char* ccmd = message+6; if(strcmp(ccmd, "DIE")==0) { bot_disconnect(b); return; } else if(strncmp(ccmd, "join", 4)==0) { CMD(b->conn, "JOIN", NULL, ccmd+4); } else CMD(b->conn, cmd, NULL, ccmd); } } else if(is_publiccommand(message, &hasarguments)==0) { if(strncmp(message, "!unicafe", 8)==0) { const char* func; if(hasarguments) { if(strncmp(message+9, "--keskusta", 10)==0 || strncmp(message+9, "-k", 2)==0) func="unicafe_centre"; } else func="unicafe"; #ifdef CBOT_PYTHON struct python_module p; memset(&p, 0, sizeof(p)); char* p1=(char*)python_module_call(&p, "unicafe.py", func); char* p2=p1; int i=0; int len=strlen(p1); while(i<len) { while(*p2!='\n'){++p2;++i;} *p2=0; ++p2; ++i; CMD(b->conn, "PRIVMSG", channel, p1); p1=p2; usleep(500000); } #else fprintf(stderr, "Python module is not compiled.\n"); #endif } } else if(strcmp(channel, *config_getvals(b->conf, "nick"))==0 && !allowedtoexec) CMD(b->conn, "PRIVMSG", nick, "Access denied!"); //bot_execcmd(b, message); } } } }