belcard_communication.cpp 5.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
	belcard_communication.cpp
	Copyright (C) 2015  Belledonne Communications SARL

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU 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 General Public License for more details.

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

19
#include <bctoolbox/parser.h>
20
#include "belcard/belcard.hpp"
21

22 23 24 25 26

using namespace::std;
using namespace::belr;
using namespace::belcard;

Sylvain Berfini's avatar
Sylvain Berfini committed
27
shared_ptr<BelCardPhoneNumber> BelCardPhoneNumber::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
28
	return BelCardProperty::parseProperty<BelCardPhoneNumber>("TEL", input);
29 30
}

Sylvain Berfini's avatar
Sylvain Berfini committed
31
void BelCardPhoneNumber::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
32
	parser->setHandler("TEL", make_fn(BelCardGeneric::create<BelCardPhoneNumber>))
33 34 35 36 37 38 39 40
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("TYPE-param", make_sfn(&BelCardProperty::setTypeParam))
			->setCollector("PID-param", make_sfn(&BelCardProperty::setParamIdParam))
			->setCollector("PREF-param", make_sfn(&BelCardProperty::setPrefParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("TEL-value", make_sfn(&BelCardProperty::setValue));
41 42
}

Sylvain Berfini's avatar
Sylvain Berfini committed
43
BelCardPhoneNumber::BelCardPhoneNumber() : BelCardProperty() {
44 45 46 47
	setName("TEL");
}

shared_ptr<BelCardEmail> BelCardEmail::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
48
	return BelCardProperty::parseProperty<BelCardEmail>("EMAIL", input);
49 50 51
}

void BelCardEmail::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
52
	parser->setHandler("EMAIL", make_fn(BelCardGeneric::create<BelCardEmail>))
53 54 55 56 57 58 59 60
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("PID-param", make_sfn(&BelCardProperty::setParamIdParam))
			->setCollector("PREF-param", make_sfn(&BelCardProperty::setPrefParam))
			->setCollector("TYPE-param", make_sfn(&BelCardProperty::setTypeParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("EMAIL-value", make_sfn(&BelCardProperty::setValue));
61 62 63 64 65 66 67
}

BelCardEmail::BelCardEmail() : BelCardProperty() {
	setName("EMAIL");
}

shared_ptr<BelCardImpp> BelCardImpp::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
68
	return BelCardProperty::parseProperty<BelCardImpp>("IMPP", input);
69 70 71
}

void BelCardImpp::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
72
	parser->setHandler("IMPP", make_fn(BelCardGeneric::create<BelCardImpp>))
73 74 75 76 77 78 79 80 81
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("PID-param", make_sfn(&BelCardProperty::setParamIdParam))
			->setCollector("PREF-param", make_sfn(&BelCardProperty::setPrefParam))
			->setCollector("TYPE-param", make_sfn(&BelCardProperty::setTypeParam))
			->setCollector("MEDIATYPE-param", make_sfn(&BelCardProperty::setMediaTypeParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("IMPP-value", make_sfn(&BelCardProperty::setValue));
82 83 84 85 86 87
}

BelCardImpp::BelCardImpp() : BelCardProperty() {
	setName("IMPP");
}

88 89 90
void BelCardImpp::setValue(const string &value) {
	bctbx_noescape_rules_t uri = {0};
	bctbx_noescape_rules_add_alfanums(uri);
91
	bctbx_noescape_rules_add_list(uri, ":@.-_~%!$&'()*+,;=");
92 93 94 95 96 97 98 99 100 101 102 103

	// Escape characters if required
	char * escaped_value = bctbx_escape(value.c_str(), uri);
	_escaped_value = string(escaped_value);
	bctbx_free(escaped_value);

	// Unescape previously escaped characters
	char * unescaped_value = bctbx_unescaped_string(value.c_str());
	string new_value = string(unescaped_value);
	bctbx_free(unescaped_value);

	BelCardProperty::setValue(new_value);
104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

void BelCardImpp::serialize(ostream& output) const {
	if (getGroup().length() > 0) {
		output << getGroup() << ".";
	}

	output << getName();
	for (auto it = getParams().begin(); it != getParams().end(); ++it) {
		output << ";" << (**it);
	}
	output << ":" << _escaped_value << "\r\n";
}

118
shared_ptr<BelCardLang> BelCardLang::parse(const string& input) {
Sylvain Berfini's avatar
Sylvain Berfini committed
119
	return BelCardProperty::parseProperty<BelCardLang>("LANG", input);
120 121 122
}

void BelCardLang::setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
Sylvain Berfini's avatar
Sylvain Berfini committed
123
	parser->setHandler("LANG", make_fn(BelCardGeneric::create<BelCardLang>))
124 125 126 127 128 129 130 131
			->setCollector("group", make_sfn(&BelCardProperty::setGroup))
			->setCollector("any-param", make_sfn(&BelCardProperty::addParam))
			->setCollector("VALUE-param", make_sfn(&BelCardProperty::setValueParam))
			->setCollector("PID-param", make_sfn(&BelCardProperty::setParamIdParam))
			->setCollector("PREF-param", make_sfn(&BelCardProperty::setPrefParam))
			->setCollector("TYPE-param", make_sfn(&BelCardProperty::setTypeParam))
			->setCollector("ALTID-param", make_sfn(&BelCardProperty::setAlternativeIdParam))
			->setCollector("LANG-value", make_sfn(&BelCardProperty::setValue));
132 133 134 135 136
}

BelCardLang::BelCardLang() : BelCardProperty() {
	setName("LANG");
}