diff options
Diffstat (limited to 'Swiften/LinkLocal')
-rw-r--r-- | Swiften/LinkLocal/LinkLocalServiceInfo.cpp | 66 | ||||
-rw-r--r-- | Swiften/LinkLocal/LinkLocalServiceInfo.h | 3 | ||||
-rw-r--r-- | Swiften/LinkLocal/UnitTest/LinkLocalServiceInfoTest.cpp | 16 |
3 files changed, 85 insertions, 0 deletions
diff --git a/Swiften/LinkLocal/LinkLocalServiceInfo.cpp b/Swiften/LinkLocal/LinkLocalServiceInfo.cpp index 99f83b8..8ee7ae0 100644 --- a/Swiften/LinkLocal/LinkLocalServiceInfo.cpp +++ b/Swiften/LinkLocal/LinkLocalServiceInfo.cpp @@ -45,4 +45,70 @@ ByteArray LinkLocalServiceInfo::getEncoded(const String& s) { return sizeByte + ByteArray(s); } +LinkLocalServiceInfo LinkLocalServiceInfo::createFromTXTRecord(const ByteArray& record) { + LinkLocalServiceInfo info; + size_t i = 0; + while (i < record.getSize()) { + std::pair<String,String> entry = readEntry(record, &i); + if (entry.first.isEmpty()) { + 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") { + info.setPort(boost::lexical_cast<int>(entry.second)); + } + else if (entry.first == "status") { + if (entry.second == "away") { + info.setStatus(Away); + } + else if (entry.second == "dnd") { + info.setStatus(DND); + } + } + } + return info; +} + +std::pair<String,String> LinkLocalServiceInfo::readEntry(const ByteArray& record, size_t* index) { + size_t& i = *index; + String key; + String value; + + size_t entryEnd = i + 1 + record[i]; + ++i; + bool inKey = true; + while (i < entryEnd && i < record.getSize()) { + if (inKey) { + if (record[i] == '=') { + inKey = false; + } + else { + key += record[i]; + } + } + else { + value += record[i]; + } + ++i; + } + return std::make_pair(key, value); +} + } diff --git a/Swiften/LinkLocal/LinkLocalServiceInfo.h b/Swiften/LinkLocal/LinkLocalServiceInfo.h index bd5286b..d78b70c 100644 --- a/Swiften/LinkLocal/LinkLocalServiceInfo.h +++ b/Swiften/LinkLocal/LinkLocalServiceInfo.h @@ -40,8 +40,11 @@ namespace Swift { ByteArray toTXTRecord() const; + static LinkLocalServiceInfo createFromTXTRecord(const ByteArray& record); + private: static ByteArray getEncoded(const String&); + static std::pair<String,String> readEntry(const ByteArray&, size_t*); private: String firstName; diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalServiceInfoTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalServiceInfoTest.cpp index 8efe4ae..b850f14 100644 --- a/Swiften/LinkLocal/UnitTest/LinkLocalServiceInfoTest.cpp +++ b/Swiften/LinkLocal/UnitTest/LinkLocalServiceInfoTest.cpp @@ -8,6 +8,8 @@ using namespace Swift; class LinkLocalServiceInfoTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(LinkLocalServiceInfoTest); CPPUNIT_TEST(testGetTXTRecord); + CPPUNIT_TEST(testCreateFromTXTRecord); + CPPUNIT_TEST(testCreateFromTXTRecord_InvalidSize); CPPUNIT_TEST_SUITE_END(); public: @@ -21,6 +23,20 @@ class LinkLocalServiceInfoTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(ByteArray("\x09txtvers=1\x09" + String("1st=Remko\x0dlast=Tron\xc3\xe7on\x0bstatus=away")), info.toTXTRecord()); } + + void testCreateFromTXTRecord() { + LinkLocalServiceInfo info = LinkLocalServiceInfo::createFromTXTRecord(ByteArray("\x09txtvers=1\x09" + String("1st=Remko\x0dlast=Tron\xc3\xe7on\x0bstatus=away"))); + + CPPUNIT_ASSERT_EQUAL(String("Remko"), info.getFirstName()); + CPPUNIT_ASSERT_EQUAL(String("Tron\xc3\xe7on"), info.getLastName()); + CPPUNIT_ASSERT_EQUAL(LinkLocalServiceInfo::Away, info.getStatus()); + } + + void testCreateFromTXTRecord_InvalidSize() { + LinkLocalServiceInfo info = LinkLocalServiceInfo::createFromTXTRecord(ByteArray("\x10last=a")); + + CPPUNIT_ASSERT_EQUAL(String("a"), info.getLastName()); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(LinkLocalServiceInfoTest); |