Ejabberd - 在offline_message_hook中将消息路由到用户

时间:2017-05-26 08:09:53

标签: erlang ejabberd ejabberd-module ejabberd-hooks

我想构建一个客户支持聊天应用。有usersadmin。管理员下方有multiple sub-admins。最初聊天是仅使用管理员启动的,但如果管理员处于离线状态,我需要将消息路由到子管理员。

offline_message_hook hook用于此目的。我将检查To是否为admin,然后我需要将Packet路由到其中一个子管理员。如何在offline_message_hook内将数据包路由/发送给其他用户。简而言之,如何更改数据包中的To以便将数据包重定向到新的子管理员?

这是我尝试过的: -

offline_message_hook({_Action, #message{from = Peer, to = To} = Pkt} = Acc) ->
    ?INFO_MSG("Inside offline", []),
    ejabberd_router:route(From, To, Packet),
    ok.

我正在使用ejabberd 17.04.105

更新

按照user2610053的建议,我这样做了: -

-spec offline_message_hook({any(), message()}) -> {any(), message()}.
offline_message_hook({_Action, Msg} = Acc) ->
    ejabberd_router:route(xmpp:set_to(Msg, 'praful@localhost')),
    {routed, Msg}.

以下是错误: -

15:13:12.291 [error] failed to route packet:
#message{id = <<"purple187f6502">>,type = chat,lang = <<"en">>,
         from = {jid,<<"praful2">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>,
                     <<"praful2">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},
         to = praful@localhost,subject = [],
         body = [#text{lang = <<>>,data = <<"co=umon">>}],
         thread = undefined,
         sub_els = [{xmlel,<<"active">>,
                           [{<<"xmlns">>,
                             <<"http://jabber.org/protocol/chatstates">>}],
                           []}],
         meta = #{ip => {0,0,0,0,0,0,0,1}}}
Reason = {error,{{badrecord,jid},[{ejabberd_router,do_route,1,[{file,"src/ejabberd_router.erl"},{line,343}]},{ejabberd_router,route,1,[{file,"src/ejabberd_router.erl"},{line,87}]},{mod_sunshine,offline_message_hook,1,[{file,"src/mod_sunshine.erl"},{line,24}]},{ejabberd_hooks,safe_apply,4,[{file,"src/ejabberd_hooks.erl"},{line,380}]},{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,364}]},{ejabberd_sm,route,1,[{file,"src/ejabberd_sm.erl"},{line,138}]},{ejabberd_local,route,1,[{file,"src/ejabberd_local.erl"},{line,116}]},{ejabberd_router,do_route,1,[{file,"src/ejabberd_router.erl"},{line,348}]}]}}

用户praful@localhost存在。请指出究竟是什么问题?

Update2 - `UserReceivePacket Hook

user_receive_packet数据包挂钩中,使用相同的函数ejabberd_router:route(xmpp:set_to(Packet, jid:decode("praful@localhost"))),时会抛出错误说: -

Hook user_receive_packet crashed when running mod_sunshine:user_receive_packet/1:
** Reason = {error,function_clause,[{jid,decode,[{file,"src/jid.erl"},{line,132}],["praful@localhost"]},{mod_sunshine,user_receive_packet,[{file,"src/mod_sunshine.erl"},{line,29}],1},{ejabberd_hooks,safe_apply,[{file,"src/ejabberd_hooks.erl"},{line,380}],4},{ejabberd_hooks,run_fold1,[{file,"src/ejabberd_hooks.erl"},{line,364}],4},{ejabberd_c2s,process_info,[{file,"src/ejabberd_c2s.erl"},{line,231}],2},{ejabberd_hooks,safe_apply,[{file,"src/ejabberd_hooks.erl"},{line,380}],4},{ejabberd_hooks,run_fold1,[{file,"src/ejabberd_hooks.erl"},{line,364}],4},{xmpp_stream_in,handle_info,[{file,"src/xmpp_stream_in.erl"},{line,373}],2}]}

所以,我读到了关于function_clause的内容,但却无法理解。这到底出了什么问题?

1 个答案:

答案 0 :(得分:1)

我认为你在询问xmpp:set_to/2。这是一个例子:

offline_message_hook({_Action, Msg} = Acc) ->
  SubAdmins = get_sub_admins(Msg#message.to),
  lists:foreach(
    fun(Admin) ->
      ejabberd_router:route(xmpp:set_to(Msg, Admin))
    end, Admins),
  {routed, Msg}.