示例#1
0
        void testSerialize_ItemWithUnknownContent() {
            RosterSerializer testling;
            std::shared_ptr<RosterPayload> roster(new RosterPayload());

            RosterItemPayload item;
            item.setJID(JID("*****@*****.**"));
            item.setName("Baz");
            item.addGroup("Group 1");
            item.addGroup("Group 2");
            item.addUnknownContent(std::string(
                "<foo xmlns=\"http://example.com\"><bar xmlns=\"http://example.com\">Baz</bar></foo>"
                "<baz xmlns=\"jabber:iq:roster\"><fum xmlns=\"jabber:iq:roster\">foo</fum></baz>"));
            roster->addItem(item);

            std::string expectedResult =
                "<query xmlns=\"jabber:iq:roster\">"
                    "<item jid=\"[email protected]\" name=\"Baz\" subscription=\"none\">"
                        "<group>Group 1</group>"
                        "<group>Group 2</group>"
                        "<foo xmlns=\"http://example.com\"><bar xmlns=\"http://example.com\">Baz</bar></foo>"
                        "<baz xmlns=\"jabber:iq:roster\"><fum xmlns=\"jabber:iq:roster\">foo</fum></baz>"
                    "</item>"
                "</query>";

            CPPUNIT_ASSERT_EQUAL(expectedResult, testling.serialize(roster));
        }
		void testSendRename() {
			JID jid("*****@*****.**");
			std::vector<std::string> groups;
			groups.push_back("Friends");
			groups.push_back("Enemies");
			xmppRoster_->addContact(jid, "Bob", groups, RosterItemPayload::From);
			CPPUNIT_ASSERT_EQUAL(groups.size(), xmppRoster_->getGroupsForJID(jid).size());
			uiEventStream_->send(boost::shared_ptr<UIEvent>(new RenameRosterItemUIEvent(jid, "Robert")));
			CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), channel_->iqs_.size());
			CPPUNIT_ASSERT_EQUAL(IQ::Set, channel_->iqs_[0]->getType());
			boost::shared_ptr<RosterPayload> payload = channel_->iqs_[0]->getPayload<RosterPayload>();
			CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), payload->getItems().size());
			RosterItemPayload item = payload->getItems()[0];
			CPPUNIT_ASSERT_EQUAL(jid, item.getJID());
			CPPUNIT_ASSERT_EQUAL(std::string("Robert"), item.getName());

			CPPUNIT_ASSERT_EQUAL(groups.size(), item.getGroups().size());
			assertVectorsEqual(groups, item.getGroups(), __LINE__);
		}
示例#3
0
static int sluift_client_add_contact(lua_State* L) {
	try {
		eventLoop.runOnce();
		SluiftClient* client = getClient(L);
		RosterItemPayload item;
		if (lua_type(L, 2) == LUA_TTABLE) {
			lua_getfield(L, 2, "jid");
			const char* rawJID = lua_tostring(L, -1);
			if (rawJID) {
				item.setJID(std::string(rawJID));
			}
			lua_getfield(L, 2, "name");
			const char* rawName = lua_tostring(L, -1);
			if (rawName) {
				item.setName(rawName);
			}
			lua_getfield(L, 2, "groups");
			if (!lua_isnil(L, -1)) {
				if (lua_type(L, -1) == LUA_TTABLE) {
					for (size_t i = 1; i <= lua_objlen(L, -1); ++i) {
						lua_rawgeti(L, -1, i);
						const char* rawGroup = lua_tostring(L, -1);
						if (rawGroup) {
							item.addGroup(rawGroup);
						}
						lua_pop(L, 1);
					}
				}
				else {
					return luaL_error(L, "Groups should be a table");
				}
			}
		}
		else {
			item.setJID(luaL_checkstring(L, 2));
		}

		client->getRoster();
		if (!client->getClient()->getRoster()->containsJID(item.getJID())) {
			RosterPayload::ref roster = boost::make_shared<RosterPayload>();
			roster->addItem(item);

			ResponseSink<RosterPayload> sink;
			SetRosterRequest::ref request = SetRosterRequest::create(roster, client->getClient()->getIQRouter());
			boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink));
			request->send();
			while (!sink.hasResponse()) {
				eventLoop.runUntilEvents();
			}
			if (sink.getResponseError()) {
				lua_pushboolean(L, false);
				return 1;
			}
		}
		client->getClient()->getSubscriptionManager()->requestSubscription(item.getJID());
		lua_pushboolean(L, true);
		return 1;
	}
	catch (const SluiftException& e) {
		return luaL_error(L, e.getReason().c_str());
	}
}