summaryrefslogtreecommitdiffstats
blob: 4e61aa9544b3b5bd217a600c2a5cd734118965b3 (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
/*
 * Copyright (c) 2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Disco/FeatureOracle.h>

#include <algorithm>
#include <iterator>
#include <vector>

#include <Swiften/Base/foreach.h>
#include <Swiften/Disco/EntityCapsProvider.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/FileTransfer/FileTransferManager.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Presence/PresenceOracle.h>

namespace Swift {

FeatureOracle::FeatureOracle(EntityCapsProvider* capsProvider, PresenceOracle* presenceOracle) : capsProvider_(capsProvider), presenceOracle_(presenceOracle) {

}

Tristate FeatureOracle::isFileTransferSupported(const JID& jid) {
	DiscoInfo::ref discoInfo = getDiscoResultForJID(jid);
	if (discoInfo) {
		return FileTransferManager::isSupportedBy(discoInfo) ? Yes : No;
	}
	else {
		return Maybe;
	}
}

Tristate FeatureOracle::isMessageReceiptsSupported(const JID& jid) {
	return isFeatureSupported(jid, DiscoInfo::MessageDeliveryReceiptsFeature);
}

Tristate FeatureOracle::isMessageCorrectionSupported(const JID& jid) {
	return isFeatureSupported(jid, DiscoInfo::MessageCorrectionFeature);
}

DiscoInfo::ref FeatureOracle::getDiscoResultForJID(const JID& jid) {
	DiscoInfo::ref discoInfo;
	if (jid.isBare()) {
		// Calculate the common subset of disco features of all available results and return that.
		std::vector<Presence::ref> availablePresences =  presenceOracle_->getAllPresence(jid);

		bool commonFeaturesInitialized = false;
		std::vector<std::string> commonFeatures;
		foreach(Presence::ref presence, availablePresences) {
			DiscoInfo::ref presenceDiscoInfo = capsProvider_->getCaps(presence->getFrom());
			if (presenceDiscoInfo) {
				std::vector<std::string> features = presenceDiscoInfo->getFeatures();
				if (!commonFeaturesInitialized) {
					commonFeatures = features;
					commonFeaturesInitialized = true;
				}
				else {
					std::vector<std::string> featuresToRemove;
					foreach(const std::string& feature, commonFeatures) {
						if (std::find(features.begin(), features.end(), feature) == features.end()) {
							featuresToRemove.push_back(feature);
						}
					}
					foreach(const std::string& featureToRemove, featuresToRemove) {
						commonFeatures.erase(std::remove(commonFeatures.begin(), commonFeatures.end(), featureToRemove), commonFeatures.end());
					}
				}
			}
		}
		discoInfo = boost::make_shared<DiscoInfo>();

		foreach(const std::string& commonFeature, commonFeatures) {
			discoInfo->addFeature(commonFeature);
		}
	}
	else {
		// Return the disco result of the full JID.
		discoInfo = capsProvider_->getCaps(jid);
	}

	return discoInfo;
}

Tristate FeatureOracle::isFeatureSupported(const JID& jid, const std::string& feature) {
	DiscoInfo::ref discoInfo = getDiscoResultForJID(jid);
	if (discoInfo) {
		return discoInfo->hasFeature(feature) ? Yes : No;
	}
	else {
		return Maybe;
	}
}

}