Search code examples
erlangmongoose-im

Implementing mod_blocking on MongooseIM


I'm trying to implement mod_blocking that will work only with mod_privacy in odbc module.

I keep getting the response:

<error code='500' type='wait'><internal-server-error0 xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>

Here is my code:

process_blocklist_block(LUser, LServer, JIDs) ->
Filter = fun (List) ->
         AlreadyBlocked = list_to_blocklist_jids(List, []),
         lists:foldr(fun (JID, List1) ->
                 case lists:member(JID, AlreadyBlocked)
                     of
                   true -> List1;
                   false ->
                       [#listitem{type = jid,
                          value = JID,
                          action = deny,
                          order = 0,
                          match_all = true}
                    | List1]
                 end
             end,
             List, JIDs)
     end,
case process_blocklist_block(LUser, LServer, Filter,odbc)
of
  {atomic, {ok, Default, List}} ->
  UserList = make_userlist(Default, List),
  broadcast_list_update(LUser, LServer, Default,
            UserList),
  broadcast_blocklist_event(LUser, LServer,
                {block, JIDs}),
  {result, [], UserList};
  _ -> {error, ?ERR_INTERNAL_SERVER_ERROR}
end.

process_blocklist_block(LUser, LServer, Filter, odbc) ->
F = fun () ->
    Default = case mod_privacy_odbc:sql_get_default_privacy_list_t(LUser)
        of
            {selected, [<<"name">>], []} ->
                Name = <<"Blocked contacts">>,
                mod_privacy_odbc:sql_add_privacy_list(LUser, Name),
                mod_privacy_odbc:sql_set_default_privacy_list(LUser, Name),
                Name;
            {selected, [<<"name">>], [[Name]]} -> Name
        end,
    {selected, [<<"id">>], [[ID]]} = mod_privacy_odbc:sql_get_privacy_list_id_t(LUser, Default),
    case mod_privacy_odbc:sql_get_privacy_list_data_by_id_t(ID)
        of
            {selected,
                [<<"t">>, <<"value">>, <<"action">>, <<"ord">>,
                <<"match_all">>, <<"match_iq">>, <<"match_message">>,
                <<"match_presence_in">>, <<"match_presence_out">>],
                RItems = [_ | _]} ->
                List = lists:map(fun mod_privacy_odbc:raw_to_item/1, RItems);
            _ -> List = []
    end,
    NewList = Filter(List),
    NewRItems = lists:map(fun mod_privacy_odbc:item_to_raw/1, NewList),
    mod_privacy_odbc:sql_set_privacy_list(ID, NewRItems),
    {ok, Default, NewList}
end,
ejabberd_odbc:sql_transaction(LServer, F).

I've checked all the queries. They work fine. I struggling with understanding the logic of this piece of code. What is the best way to debug this code, so i can understand when my logic is failing? Could anyone point me in the right direction?

UserList = make_userlist(Default, List),
  broadcast_list_update(LUser, LServer, Default,
            UserList),
  broadcast_blocklist_event(LUser, LServer,
                {block, JIDs}),
  {result, [], UserList};
  _ -> {error, ?ERR_INTERNAL_SERVER_ERROR}

Solution

  • if you expect

      _ -> {error, ?ERR_INTERNAL_SERVER_ERROR}
    

    is the point where server returns 'internal server error', you can just change this place to

      _Otherwise ->
           ?WARNING_MSG("Got some unexpected result in mod_priv ~100000p",[_Otherwise]),
           {error, ?ERR_INTERNAL_SERVER_ERROR)
    

    and then look at log/ejabberd.log

    Of course you need to start mongooseim with loglevel at least 3 in the ejabberd.cfg