<commit_before>/*
	Flexisip, a flexible SIP proxy server with media capabilities.
    Copyright (C) 2010  Belledonne Communications SARL.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <cstring>
#include <algorithm>
#include <iostream>

#include "lpconfig.h"
#include "configmanager.hh"
#include "common.hh"

#include <functional>

#include <ctime>
#include <sstream>
#include <fstream>

#include <sofia-sip/su_md5.h>

using namespace::std;

bool ConfigValueListener::sDirty=false;
void ConfigValueListener::onConfigStateChanged(const ConfigValue &conf, ConfigState state) {
	switch (state) {
	case ConfigState::Commited:
		if (sDirty) {
			// Write to disk
			GenericStruct *rootStruct=GenericManager::get()->getRoot();
			ofstream cfgfile;
			cfgfile.open(GenericManager::get()->getConfigFile());
			FileConfigDumper dumper(rootStruct);
			dumper.setDumpDefaultValues(false);
			cfgfile << dumper;
			cfgfile.close();
			LOGI("New configuration wrote to %s .", GenericManager::get()->getConfigFile().c_str());
			sDirty=false;
		}
		break;
	case ConfigState::Changed:
		sDirty=true;
		break;
	case ConfigState::Reset:
		sDirty=false;
		break;
	default:
		break;
	}
	doOnConfigStateChanged(conf, state);
}

static void camelFindAndReplace(const string &needle, string &haystack) {
	size_t pos;
	while ((pos=haystack.find(needle)) != string::npos) {
		haystack.replace(pos, needle.length(), "");
		if (haystack.length() > pos) {
			stringstream ss;
			ss << char(toupper(haystack.at(pos)));
			haystack.replace(pos, 1, ss.str());
		}
	}
}

string GenericEntry::sanitize(const string &str){
	string strnew=str;
	camelFindAndReplace("::", strnew);
	camelFindAndReplace("-", strnew);
	return strnew;
}

void GenericEntry::mibFragment(ostream & ost, string spacing) const{
	string s("OCTET STRING");
	doMibFragment(ost, false, s, spacing);
}

void ConfigBoolean::mibFragment(ostream & ost, string spacing) const{
	string s("INTEGER { true(1),false(0) }");
	doMibFragment(ost, true, s, spacing);
}
void ConfigInt::mibFragment(ostream & ost, string spacing) const{
	string s("Integer32");
	doMibFragment(ost, true, s, spacing);
}
void StatCounter64::mibFragment(ostream & ost, string spacing) const{<commit_msg>Allow SNMP set of string config values.<commit_after>/*
	Flexisip, a flexible SIP proxy server with media capabilities.
    Copyright (C) 2010  Belledonne Communications SARL.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <cstring>
#include <algorithm>
#include <iostream>

#include "lpconfig.h"
#include "configmanager.hh"
#include "common.hh"

#include <functional>

#include <ctime>
#include <sstream>
#include <fstream>

#include <sofia-sip/su_md5.h>

using namespace::std;

bool ConfigValueListener::sDirty=false;
void ConfigValueListener::onConfigStateChanged(const ConfigValue &conf, ConfigState state) {
	switch (state) {
	case ConfigState::Commited:
		if (sDirty) {
			// Write to disk
			GenericStruct *rootStruct=GenericManager::get()->getRoot();
			ofstream cfgfile;
			cfgfile.open(GenericManager::get()->getConfigFile());
			FileConfigDumper dumper(rootStruct);
			dumper.setDumpDefaultValues(false);
			cfgfile << dumper;
			cfgfile.close();
			LOGI("New configuration wrote to %s .", GenericManager::get()->getConfigFile().c_str());
			sDirty=false;
		}
		break;
	case ConfigState::Changed:
		sDirty=true;
		break;
	case ConfigState::Reset:
		sDirty=false;
		break;
	default:
		break;
	}
	doOnConfigStateChanged(conf, state);
}

static void camelFindAndReplace(const string &needle, string &haystack) {
	size_t pos;
	while ((pos=haystack.find(needle)) != string::npos) {
		haystack.replace(pos, needle.length(), "");
		if (haystack.length() > pos) {
			stringstream ss;
			ss << char(toupper(haystack.at(pos)));
			haystack.replace(pos, 1, ss.str());
		}
	}
}

string GenericEntry::sanitize(const string &str){
	string strnew=str;
	camelFindAndReplace("::", strnew);
	camelFindAndReplace("-", strnew);
	return strnew;
}

void GenericEntry::mibFragment(ostream & ost, string spacing) const{
	string s("OCTET STRING");
	doMibFragment(ost, true, s, spacing);
}

void ConfigBoolean::mibFragment(ostream & ost, string spacing) const{
	string s("INTEGER { true(1),false(0) }");
	doMibFragment(ost, true, s, spacing);
}
void ConfigInt::mibFragment(ostream & ost, string spacing) const{
	string s("Integer32");
	doMibFragment(ost, true, s, spacing);
}
void StatCounter64::mibFragment(ostream & ost, string spacing) const{
{'fraction_non_alphanumeric': 0.11802762720755376, 'fraction_numerical': 0.00454624934429096, 'mean_word_length': 4.860655737704918, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '471456', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1787, 'n_words': 545}