summaryrefslogtreecommitdiffstats
blob: 2baf87c31c007003456ff587f86d4e4fd393794f (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (c) 2015-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Disco/FeatureOracle.h>

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

#include <Swiften/Base/Log.h>
#include <Swiften/Disco/EntityCapsProvider.h>
#include <Swiften/Elements/Idle.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) {
    Tristate fileTransferSupported = No;

    auto isYesOrMaybe = [](Tristate tristate) { return tristate == Yes || tristate == Maybe; };
    auto isYes = [](Tristate tristate) { return tristate == Yes; };

    auto supportedFeatures = getFeaturesForJID(jid);

    auto jingleSupported = isFeatureSupported(supportedFeatures, DiscoInfo::JingleFeature);
    auto jingleFTSupported = isFeatureSupported(supportedFeatures, DiscoInfo::JingleFTFeature);
    auto jingleTransportIBBSupported = isFeatureSupported(supportedFeatures, DiscoInfo::JingleTransportsIBBFeature);
    auto jingleTransportS5BSupported = isFeatureSupported(supportedFeatures, DiscoInfo::JingleTransportsS5BFeature);

    if (isYes(jingleSupported) && isYes(jingleFTSupported) && (isYes(jingleTransportIBBSupported) || isYes(jingleTransportS5BSupported))) {
        fileTransferSupported = Yes;
    }
    else if (isYesOrMaybe(jingleSupported) && isYesOrMaybe(jingleFTSupported) && (isYesOrMaybe(jingleTransportIBBSupported) || isYesOrMaybe(jingleTransportS5BSupported))) {
        fileTransferSupported = Maybe;
    }

    return fileTransferSupported;
}

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

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

Tristate FeatureOracle::isWhiteboardSupported(const JID& jid) {
    return isFeatureSupported(getFeaturesForJID(jid), DiscoInfo::WhiteboardFeature);
}

class PresenceFeatureAvailablityComparator {
    public:
        static int preferenceFromStatusShow(StatusShow::Type showType) {
            switch (showType) {
                case StatusShow::FFC:
                    return 5;
                case StatusShow::Online:
                    return 4;
                case StatusShow::DND:
                    return 3;
                case StatusShow::Away:
                    return 2;
                case StatusShow::XA:
                    return 1;
                case StatusShow::None:
                    return 0;
            }
            assert(false);
            return -1;
        }

        static int compareWithoutResource(const Presence::ref& a, const Presence::ref& b) {
            int aPreference = preferenceFromStatusShow(a->getShow());
            int bPreference = preferenceFromStatusShow(b->getShow());

            if (aPreference != bPreference) {
                return aPreference < bPreference ? 1 : -1;
            }

            Idle::ref aIdle = a->getPayload<Idle>();
            Idle::ref bIdle = b->getPayload<Idle>();

            if (aIdle && !bIdle) {
                return -1;
            }
            else if (!aIdle && bIdle) {
                return 1;
            }

            if (a->getPriority() != b->getPriority()) {
                return a->getPriority() < b->getPriority() ? 1 : -1;
            }

            return 0;
        }

        /*
         * This method returns true, if \ref Presence \p b is more available than
         * \ref Presence \p a.
         * Going by \ref StatusShow::Type first, then by idle state, then by
         * presence priority and finally by resource name.
         */
        bool operator()(const Presence::ref& a, const Presence::ref& b) {
            int aMoreAvailableThanB = compareWithoutResource(a, b);
            if (aMoreAvailableThanB < 0) {
                return true;
            }
            else if (aMoreAvailableThanB > 0) {
                return false;
            }

            return a->getFrom().getResource() < b->getFrom().getResource();
        }
};

JID FeatureOracle::getMostAvailableClientForFileTrasfer(const JID& bareJID) {
    JID fullJID;
    assert(bareJID.isBare());

    std::vector<Presence::ref> allPresences =  presenceOracle_->getAllPresence(bareJID);
    std::sort(allPresences.begin(), allPresences.end(), PresenceFeatureAvailablityComparator());

    for (const auto& presence : allPresences) {
        if (presence->isAvailable()) {
            if (isFileTransferSupported(presence->getFrom()) == Yes) {
                fullJID = presence->getFrom();
                break;
            }
        }
    }

    SWIFT_LOG_ASSERT(!fullJID.isBare(), error);
    return fullJID;
}

std::unordered_map<std::string, Tristate> FeatureOracle::getFeaturesForJID(const JID& jid) {
    std::unordered_map<std::string, Tristate> supportedFeatures;
    if (jid.isBare()) {
        // Calculate the union of disco features of all most available results and return that.
        std::vector<DiscoInfo::ref> onlineDiscoInfos;
        std::unordered_set<std::string> features;

        // Collect relevant disco info results and the set of features.
        for (auto&& presence : presenceOracle_->getAllPresence(jid)) {
            if (presence->getType() == Presence::Available) {
                DiscoInfo::ref presenceDiscoInfo = capsProvider_->getCaps(presence->getFrom());
                if (presenceDiscoInfo) {
                    onlineDiscoInfos.push_back(presenceDiscoInfo);
                    features.insert(presenceDiscoInfo->getFeatures().begin(), presenceDiscoInfo->getFeatures().end());
                }
            }
        }

        // Calculate supportedFeaturesMap.
        for (auto&& feature : features) {
            Tristate supported = Yes;
            for (auto&& discoInfo : onlineDiscoInfos) {
                if (!discoInfo->hasFeature(feature)) {
                    supported = Maybe;
                    break;
                }
            }
            supportedFeatures[feature] = supported;
        }
    }
    else {
        // Return the disco result of the full JID.
        auto discoInfo = capsProvider_->getCaps(jid);
        if (discoInfo) {
            for (auto&& feature : discoInfo->getFeatures()) {
                supportedFeatures[feature] = Yes;
            }
        }
    }

    return supportedFeatures;
}

Tristate FeatureOracle::isFeatureSupported(const std::unordered_map<std::string, Tristate>& supportedFeatures, const std::string& feature) {
    Tristate supported = No;
    auto lookupResult = supportedFeatures.find(feature);
    if (lookupResult != supportedFeatures.end()) {
        supported = lookupResult->second;
    }
    return supported;
}

}