summaryrefslogtreecommitdiffstats
blob: 5fd75245a44eadbb62ccaaeff0d4371b4625b9db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#define SWIFTEN_CACHE_JID_PREP

#include <vector>
#include <iostream>

#include <string>
#ifdef SWIFTEN_CACHE_JID_PREP
#include <boost/unordered_map.hpp>
#endif
#include <boost/algorithm/string/replace.hpp>
#include <stringprep.h>

#include <Swiften/Base/String.h>
#include "Swiften/JID/JID.h"
#include "Swiften/IDN/StringPrep.h"

#ifdef SWIFTEN_CACHE_JID_PREP
typedef boost::unordered_map<std::string, std::string> PrepCache;

static PrepCache nodePrepCache;
static PrepCache domainPrepCache;
static PrepCache resourcePrepCache;
#endif

namespace Swift {

JID::JID(const char* jid) {
	initializeFromString(std::string(jid));
}

JID::JID(const std::string& jid) {
	initializeFromString(jid);
}

JID::JID(const std::string& node, const std::string& domain) : hasResource_(false) {
	nameprepAndSetComponents(node, domain, "");
}

JID::JID(const std::string& node, const std::string& domain, const std::string& resource) : hasResource_(true) {
	nameprepAndSetComponents(node, domain, resource);
}

void JID::initializeFromString(const std::string& jid) {
	if (String::beginsWith(jid, '@')) {
		return;
	}

	std::string bare, resource;
	size_t slashIndex = jid.find('/');
	if (slashIndex != jid.npos) {
		hasResource_ = true;
		bare = jid.substr(0, slashIndex);
		resource = jid.substr(slashIndex + 1, jid.npos);
	}
	else {
		hasResource_ = false;
		bare = jid;
	}
	std::pair<std::string,std::string> nodeAndDomain = String::getSplittedAtFirst(bare, '@');
	if (nodeAndDomain.second.empty()) {
		nameprepAndSetComponents("", nodeAndDomain.first, resource);
	}
	else {
		nameprepAndSetComponents(nodeAndDomain.first, nodeAndDomain.second, resource);
	}
}


void JID::nameprepAndSetComponents(const std::string& node, const std::string& domain, const std::string& resource) {
#ifndef SWIFTEN_CACHE_JID_PREP
	node_ = StringPrep::getPrepared(node, StringPrep::NamePrep);
	domain_ = StringPrep::getPrepared(domain, StringPrep::XMPPNodePrep);
	resource_ = StringPrep::getPrepared(resource, StringPrep::XMPPResourcePrep);
#else
	std::pair<PrepCache::iterator, bool> r;

	r = nodePrepCache.insert(std::make_pair(node, std::string()));
	if (r.second) {
		r.first->second = StringPrep::getPrepared(node, StringPrep::NamePrep);
	}
	node_ = r.first->second;

	r = domainPrepCache.insert(std::make_pair(domain, std::string()));
	if (r.second) {
		r.first->second = StringPrep::getPrepared(domain, StringPrep::XMPPNodePrep);
	}
	domain_ = r.first->second;

	r = resourcePrepCache.insert(std::make_pair(resource, std::string()));
	if (r.second) {
		r.first->second = StringPrep::getPrepared(resource, StringPrep::XMPPResourcePrep);
	}
	resource_ = r.first->second;
#endif
}

std::string JID::toString() const {
	std::string string;
	if (!node_.empty()) {
		string += node_ + "@";
	}
	string += domain_;
	if (!isBare()) {
		string += "/" + resource_;
	}
	return string;
}

int JID::compare(const Swift::JID& o, CompareType compareType) const {
	if (node_ < o.node_) { return -1; }
	if (node_ > o.node_) { return 1; }
	if (domain_ < o.domain_) { return -1; }
	if (domain_ > o.domain_) { return 1; }
	if (compareType == WithResource) {
		if (hasResource_ != o.hasResource_) {
			return hasResource_ ? 1 : -1;
		}
		if (resource_ < o.resource_) { return -1; }
		if (resource_ > o.resource_) { return 1; }
	}
	return 0;
}

std::string JID::getEscapedNode(const std::string& node) {
	std::string escaped = node;
	
	boost::algorithm::replace_all(escaped, "\\", "\\5c");
	boost::algorithm::replace_all(escaped, " ", "\\20");
	boost::algorithm::replace_all(escaped, "\"", "\\22");
	boost::algorithm::replace_all(escaped, "&", "\\26");
	boost::algorithm::replace_all(escaped, "'", "\\27");
	boost::algorithm::replace_all(escaped, "/", "\\2f");
	boost::algorithm::replace_all(escaped, "<", "\\3c");
	boost::algorithm::replace_all(escaped, ">", "\\3e");
	boost::algorithm::replace_all(escaped, "@", "\\40");
	boost::algorithm::replace_all(escaped, ":", "\\3a");

	return escaped;
}

std::string JID::getUnescapedNode() const {
	std::string unescaped = node_;

	boost::algorithm::replace_all(unescaped, "\\20", " ");
	boost::algorithm::replace_all(unescaped, "\\22", "\"");
	boost::algorithm::replace_all(unescaped, "\\26", "&");
	boost::algorithm::replace_all(unescaped, "\\27", "'");
	boost::algorithm::replace_all(unescaped, "\\2f", "/");
	boost::algorithm::replace_all(unescaped, "\\3c", "<");
	boost::algorithm::replace_all(unescaped, "\\3e", ">");
	boost::algorithm::replace_all(unescaped, "\\40", "@");
	boost::algorithm::replace_all(unescaped, "\\3a", ":");
	boost::algorithm::replace_all(unescaped, "\\5c", "\\");
	

	return unescaped;
}

} // namespace Swift