ejabberdejabberd-api

ejabberd add_channel API for MIX


Currently, there is no api for creating a MIX channel. I'm written a custom module for the same.

So far, I have written the following code. But I'm not how to proceed further. I would really appreciate someone's guidance here. Thanks in advance.

-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(_) -> [].

Solution

  • Try something like this:

    -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(_) -> [].