agent_pref_t _ds_ff_pref_load( config_t config, const char *user, const char *home, void *ignore) { char filename[MAX_FILENAME_LENGTH]; agent_pref_t PTX = malloc(sizeof(agent_attrib_t )*PREF_MAX); char buff[258]; FILE *file; char *p, *q, *bufptr; int i = 0; UNUSED(config); UNUSED(ignore); if (PTX == NULL) { LOG(LOG_CRIT, ERR_MEM_ALLOC); return NULL; } PTX[0] = NULL; if (user == NULL) { snprintf(filename, MAX_FILENAME_LENGTH, "%s/default.prefs", home); } else { _ds_userdir_path (filename, home, user, "prefs"); } file = fopen(filename, "r"); /* Apply default preferences from dspam.conf */ if (file != NULL) { char *ptrptr; while(i<(PREF_MAX-1) && fgets(buff, sizeof(buff), file)!=NULL) { if (buff[0] == '#' || buff[0] == 0) continue; chomp(buff); bufptr = buff; p = strtok_r(buff, "=", &ptrptr); if (p == NULL) continue; q = p + strlen(p)+1; LOGDEBUG("Loading preference '%s' = '%s'", p, q); PTX[i] = _ds_pref_new(p, q); PTX[i+1] = NULL; i++; } fclose(file); } else { free(PTX); return NULL; } return PTX; }
agent_pref_t _ds_pref_aggregate(agent_pref_t STX, agent_pref_t UTX) { agent_pref_t PTX = static_cast<_ds_agent_attribute **>(calloc(1, PREF_MAX*sizeof(agent_attrib_t ))); int i, size = 0; if (STX) { for(i=0;STX[i];i++) { PTX[i] = _ds_pref_new(STX[i]->attribute, STX[i]->value); PTX[i+1] = NULL; size++; } } if (UTX) { for(i=0;UTX[i];i++) { if (_ds_match_attribute(agent_config, "AllowOverride", UTX[i]->attribute)) { int found = 0; int j; for(j=0;PTX[j];j++) { if (!strcasecmp(PTX[j]->attribute, UTX[i]->attribute)) { found = 1; free(PTX[j]->value); PTX[j]->value = strdup(UTX[i]->value); break; } } if (!found) { PTX[size] = _ds_pref_new(UTX[i]->attribute, UTX[i]->value); PTX[size+1] = NULL; size++; } } else { LOG(LOG_ERR, ERR_AGENT_IGNORE_PREF, UTX[i]->attribute); } } } return PTX; }
agent_pref_t pref_config(void) { agent_pref_t PTX = malloc(sizeof(agent_attrib_t)*PREF_MAX); agent_pref_t ptr; attribute_t attrib; char *p, *q; char *ptrptr; int i = 0; if (PTX == NULL) { LOG(LOG_CRIT, ERR_MEM_ALLOC); return NULL; } PTX[0] = NULL; /* Apply default preferences from dspam.conf */ attrib = _ds_find_attribute(agent_config, "Preference"); LOGDEBUG("Loading preferences from dspam.conf"); while(attrib != NULL) { char *pcopy = strdup(attrib->value); p = strtok_r(pcopy, "=", &ptrptr); if (p == NULL) { free(pcopy); continue; } q = p + strlen(p)+1; PTX[i] = _ds_pref_new(p, q); PTX[i+1] = NULL; i++; attrib = attrib->next; free(pcopy); } ptr = realloc(PTX, sizeof(agent_attrib_t)*(i+1)); if (ptr) return ptr; LOG(LOG_CRIT, ERR_MEM_ALLOC); return PTX; }