summaryrefslogtreecommitdiffstats
blob: 63e063d4f5d91be5c0142c845e3220a685c347a5 (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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <string>
#include <ostream>

namespace Swift {
	class JID {
		public:
			enum CompareType {
				WithResource, WithoutResource
			};

			JID(const std::string& = std::string());
			JID(const char*);
			JID(const std::string& node, const std::string& domain);
			JID(const std::string& node, const std::string& domain, const std::string& resource);

			bool isValid() const {
				return !domain_.empty(); /* FIXME */
			}

			const std::string& getNode() const {
				return node_;
			}
			const std::string& getDomain() const {
				return domain_;
			}
			const std::string& getResource() const {
				return resource_;
			}
			bool isBare() const {
				return !hasResource_;
			}

			JID toBare() const {
				JID result(*this);
				result.hasResource_ = false;
				result.resource_ = "";
				return result;
			}

			std::string toString() const;

			bool equals(const JID& o, CompareType compareType) const {
				return compare(o, compareType) == 0;
			}

			int compare(const JID& o, CompareType compareType) const;

			operator std::string() const {
				return toString();
			}

			bool operator<(const Swift::JID& b) const {
				return compare(b, Swift::JID::WithResource) < 0;
			}

			friend std::ostream& operator<<(std::ostream& os, const Swift::JID& j) {
				os << j.toString();
				return os;
			}

			friend bool operator==(const Swift::JID& a, const Swift::JID& b) {
				return a.compare(b, Swift::JID::WithResource) == 0;
			}

			friend bool operator!=(const Swift::JID& a, const Swift::JID& b) {
				return a.compare(b, Swift::JID::WithResource) != 0;
			}

		private:
			void nameprepAndSetComponents(const std::string& node, const std::string& domain, const std::string& resource);
			void initializeFromString(const std::string&);
	
		private:
			std::string node_;
			std::string domain_;
			bool hasResource_;
			std::string resource_;
	};
}