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

#include <Swiften/LinkLocal/LinkLocalServiceInfo.h>

#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include <Swiften/Base/Algorithm.h>
#include <Swiften/Base/Concat.h>
#include <Swiften/Base/Log.h>

namespace Swift {

boost::optional<ByteArray> LinkLocalServiceInfo::toTXTRecord() const {
    try {
        ByteArray result(getEncoded("txtvers=1"));
        if (!firstName.empty()) {
            append(result, getEncoded("1st=" + firstName));
        }
        if (!lastName.empty()) {
            append(result, getEncoded("last=" + lastName));
        }
        if (!email.empty()) {
            append(result, getEncoded("email=" + email));
        }
        if (jid.isValid()) {
            append(result, getEncoded("jid=" + jid.toString()));
        }
        if (!message.empty()) {
            append(result, getEncoded("msg=" + message));
        }
        if (!nick.empty()) {
            append(result, getEncoded("nick=" + nick));
        }
        if (port) {
            append(result, getEncoded("port.p2pj=" + std::string(std::to_string(*port))));
        }

        switch (status) {
            case Available: append(result, getEncoded("status=avail")); break;
            case Away: append(result, getEncoded("status=away")); break;
            case DND: append(result, getEncoded("status=dnd")); break;
        }

        return result;
    }
    catch (const std::exception& e) {
        SWIFT_LOG(warning) << "Failed to create TXT record for link local service info: " << e.what() << std::endl;
        return boost::none;
    }
}

ByteArray LinkLocalServiceInfo::getEncoded(const std::string& s) {
    ByteArray sizeByte;
    sizeByte.resize(1);
    sizeByte[0] = boost::numeric_cast<unsigned char>(s.size());
    return concat(sizeByte, createByteArray(s));
}

LinkLocalServiceInfo LinkLocalServiceInfo::createFromTXTRecord(const ByteArray& record) {
    LinkLocalServiceInfo info;
    size_t i = 0;
    size_t recordCount = record.size();
    while (i < recordCount) {
        std::pair<std::string,std::string> entry = readEntry(record, &i);
        if (entry.first.empty()) {
            break;
        }
        else if (entry.first == "1st") {
            info.setFirstName(entry.second);
        }
        else if (entry.first == "last") {
            info.setLastName(entry.second);
        }
        else if (entry.first == "email") {
            info.setEMail(entry.second);
        }
        else if (entry.first == "jid") {
            info.setJID(JID(entry.second));
        }
        else if (entry.first == "msg") {
            info.setMessage(entry.second);
        }
        else if (entry.first == "nick") {
            info.setNick(entry.second);
        }
        else if (entry.first == "port.p2pj") {
            try {
                info.setPort(boost::numeric_cast<unsigned short>(boost::lexical_cast<int>(entry.second)));
            }
            catch (const boost::bad_lexical_cast&) {
            }
            catch (const boost::numeric::bad_numeric_cast&) {
            }
        }
        else if (entry.first == "status") {
            if (entry.second == "away") {
                info.setStatus(Away);
            }
            else if (entry.second == "dnd") {
                info.setStatus(DND);
            }
        }
    }
    return info;
}

std::pair<std::string,std::string> LinkLocalServiceInfo::readEntry(const ByteArray& record, size_t* index) {
    size_t& i = *index;
    std::string key;
    std::string value;

    size_t entryEnd = i + 1 + record[i];
    ++i;
    bool inKey = true;
    while (i < entryEnd && i < record.size()) {
        if (inKey) {
            if (record[i] == '=') {
                inKey = false;
            }
            else {
                key += static_cast<char>(record[i]);
            }
        }
        else {
            value += static_cast<char>(record[i]);
        }
        ++i;
    }
    return std::make_pair(key, value);
}

}