【问题标题】:ejabberd add_channel API for MIX用于 MIX 的 ejabberd add_channel API
【发布时间】:2021-02-05 05:58:52
【问题描述】:

目前还没有创建 MIX 频道的 api。 我为此编写了一个自定义模块。

到目前为止,我已经编写了以下代码。但我不知道如何进一步进行。 我真的很感谢有人在这里指导。提前致谢。

-module(mod_custom).
-behaviour(gen_mod).
-include("logger.hrl").
-export([start/2, stop/1, reload/3, mod_options/1,
     get_commands_spec/0, depends/2]).
-export([
     % Create channel
     add_channel/4
    ]).

-include("ejabberd_commands.hrl").
-include("ejabberd_sm.hrl").
-include("xmpp.hrl").

start(_Host, _Opts) ->
    ejabberd_commands:register_commands(get_commands_spec()).
stop(Host) ->
    case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
    false ->
        ejabberd_commands:unregister_commands(get_commands_spec());
    true ->
        ok
    end.
reload(_Host, _NewOpts, _OldOpts) ->
    ok.
depends(_Host, _Opts) ->
    [].

get_commands_spec() ->
    [
        #ejabberd_commands{name = add_channel, tags = [group],
            desc = "Create a WhatsApp like group",
            module = ?MODULE, function = add_channel,
            args = [{jid, binary}, {channel, binary}, {id, binary}],
            args_example = [<<"admin@localhost">>, <<"testgroup123@localhost">>, <<"abc123456">>],
            args_desc = ["Admin JID", "Channel JID", "Unique ID"],
            result = {res, rescode}}
    ].

add_channel(JID, Channel, ID) ->
    %%% Create channel code goes here...
ok.

mod_options(_) -> [].

【问题讨论】:

    标签: ejabberd ejabberd-api


    【解决方案1】:

    试试这样的:

    -module(mod_custom).
    -behaviour(gen_mod).
    
    -export([start/2, stop/1, reload/3, mod_options/1,
             get_commands_spec/0, depends/2]).
    -export([create_channel/3]).
    
    -include("logger.hrl").
    -include("ejabberd_commands.hrl").
    -include("ejabberd_sm.hrl").
    -include_lib("xmpp/include/xmpp.hrl").
    
    start(_Host, _Opts) ->
        ejabberd_commands:register_commands(get_commands_spec()).
    stop(Host) ->
        case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
            false ->
                ejabberd_commands:unregister_commands(get_commands_spec());
            true ->
                ok
        end.
    reload(_Host, _NewOpts, _OldOpts) ->
        ok.
    depends(_Host, _Opts) ->
        [].
    
    get_commands_spec() ->
        [#ejabberd_commands{name = create_channel, tags = [group],
                            desc = "Create a WhatsApp like group",
                            module = ?MODULE, function = create_channel,
                            args = [{from, binary},
                                    {channel, binary},
                                    {service, binary}],
                            args_example = [<<"admin@localhost">>,
                                            <<"testgroup123">>,
                                            <<"mix.localhost">>],
                            args_desc = ["From JID", "Channel Name", "MIX Service"],
                            result = {res, rescode}}
        ].
    
    create_channel(From, ChannelName, Service) ->
        try xmpp:decode(
              #xmlel{name = <<"iq">>,
                     attrs = [{<<"to">>, Service},
                              {<<"from">>, From},
                              {<<"type">>, <<"set">>},
                              {<<"id">>, p1_rand:get_string()}],
                     children =
                         [#xmlel{name = <<"create">>,
                                 attrs = [{<<"channel">>, ChannelName},
                                          {<<"xmlns">>, ?NS_MIX_CORE_0}]}
                         ]},
              ?NS_CLIENT, []) of
            #iq{type = set} = Iq ->
                case mod_mix:process_mix_core(Iq) of
                    #iq{type = result} ->
                        ok;
                    _ ->
                        {error, unexpected_response}
                end
        catch _:{xmpp_codec, Why} ->
                {error, xmpp:format_error(Why)}
        end.
    
    mod_options(_) -> [].
    

    【讨论】:

    • 感谢您的回答。它使我免于走错方向。我只调用了 mod_mix_pam:process_join 函数,没有创建通道。
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2017-10-16
    • 2017-10-02
    • 1970-01-01
    • 2015-09-10
    • 2020-01-31
    • 2016-09-08
    • 2013-09-22
    相关资源
    最近更新 更多