gloox配置聊天室


(金庆的专栏)


gloox是XMPP协议的C++客户端库。


以下代码测试创建多人聊天室(MUC), 并进行配置。


参照gloox中的muc示例代码。


gloox代码示例中没有聊天室的配置。


配置聊天室需要获取配置表单(DataForm), 填好表单然后调用 setRoomConfig().


配置表单读取服务器发来的默认配置,仅更改了其中一项。


测试服务器使用了ejabberd.


const char SERVER[] = "xmpp.jinqing.net";
const char TESTER[] = "tester";
const char PASSWORD[] = "password";

using namespace gloox;

static std::string GetTesterJid()
{
    return std::string(TESTER) + "@" + SERVER;
}

static DataForm* CreateMUCConfigForm(const DataForm& form)
{
    DataForm* pNewFm = new DataForm(TypeSubmit);
    const DataForm::FieldList& fl = form.fields();
    for (const DataFormField* pFld : fl)
    {
        DataFormField* pNewFld = pNewFm->addField(
            pFld->type(), pFld->name(), pFld->value());
        if (pFld->name() == "muc#roomconfig_roomdesc")
            pNewFld->setValue("RoomDesc_JinqTest");
    }
    return pNewFm;
}

class MUCRoomConfigHandlerTest : public MUCRoomConfigHandler
{

public:

void handleMUCConfigList(MUCRoom* room, const MUCListItemList& items,
        MUCOperation operation) override {}

    void handleMUCConfigForm(MUCRoom* room, const DataForm& form) override
    {
        DataForm* pNewForm = CreateMUCConfigForm(form);  // deleted in setRoomConfig()
        room->setRoomConfig(pNewForm);
    }

    void handleMUCConfigResult(MUCRoom* room, bool success,
        MUCOperation operation) override {}
    void handleMUCRequest(MUCRoom* room, const DataForm& form) override {}
};  // class MUCRoomConfigHandlerTest

class CreateRoomTest : public ConnectionListener, MUCRoomHandler
{
void handleMUCConfigList(MUCRoom* room, const MUCListItemList& items,
        MUCOperation operation) override {}

    void handleMUCConfigForm(MUCRoom* room, const DataForm& form) override
    {
        DataForm* pNewForm = CreateMUCConfigForm(form);  // deleted in setRoomConfig()
        room->setRoomConfig(pNewForm);
    }

    void handleMUCConfigResult(MUCRoom* room, bool success,
        MUCOperation operation) override {}
    void handleMUCRequest(MUCRoom* room, const DataForm& form) override {}
};  // class MUCRoomConfigHandlerTest

class CreateRoomTest : public ConnectionListener, MUCRoomHandler
{

public:

CreateRoomTest() : m_client(JID(GetTesterJid()), PASSWORD)
    {
        m_client.registerConnectionListener(this);
    }

public:

void TestCreate()
    {
        m_client.setPresence(Presence::Available, -1);
        JID nick(std::string("gloox@conference.") + SERVER + "/gloox");
        m_pRoom.reset(new MUCRoom(&m_client, nick, this, &m_cfgHdlr));
        m_client.connect();
    }

    void onConnect() override
    {
        m_pRoom->join();
        m_pRoom->requestRoomConfig();
    }

    void onDisconnect(ConnectionError /*e*/) override {}

    bool onTLSConnect(const CertInfo& info) override
    {
        return true;
    }

    void handleMUCParticipantPresence(MUCRoom * /*room*/,
        const MUCRoomParticipant participant,
        const Presence& presence) override {}

    void handleMUCMessage(MUCRoom* /*room*/,
        const Message& msg, bool priv) override {}

    void handleMUCSubject(MUCRoom * /*room*/, const std::string& nick,
        const std::string& subject) override {}

    void handleMUCError(MUCRoom * /*room*/, StanzaError error) override {}

    void handleMUCInfo(MUCRoom * /*room*/, int features,
        const std::string& name, const DataForm* infoForm) override {}

    void handleMUCItems(MUCRoom * /*room*/,
        const Disco::ItemList& items) override {}

    void handleMUCInviteDecline(MUCRoom * /*room*/, const JID& invitee,
        const std::string& reason) override {}

    bool handleMUCRoomCreation(MUCRoom *room) override
    {
        return true;
    }


private:

gloox::Client m_client;
    MUCRoomConfigHandlerTest m_cfgHdlr;
    std::unique_ptr<MUCRoom> m_pRoom;
};  // class CreateRoomTest

int main()
{
    CreateRoomTest().TestCreate();
    return 0;
}