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__);
		}
Exemplo n.º 2
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());
	}
}