Ejabberd-为自定义命令运行SQL查询 [英] Ejabberd - Run SQL Query for Custom Command

查看:123
本文介绍了Ejabberd-为自定义命令运行SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在mod_admin_extra.erl中进行自定义命令.来获取2个JID之间的消息.

I'm trying to make a custom command in mod_admin_extra.erl. to fetch messages between 2 JIDs.

我的命令将如下所示:-

My command will look like this:-

ejabberdctl get_messages HOST FROM TO START_TIME END_TIME

SQL查询将类似于:-

The SQL query will be like:-

select * from archive where (username = FROM and bare_peer = TO) OR (username=TO and bare_peer = FROM) where created_at BETWEEN START_TIME AND END_TIME;

我经历了

I went through this thread to understand how IQ query works and want to build a similar sort of a thing via the command and API.

如何在上述功能中触发查询,以便在2个JID的会话之间获取消息?

我的回答将是一个字典列表:-

My response would be a list of dictionaries:-

[{from: jid1, to: jid2, body: Hello, created_at: T1}]

我将依次使用相同的POST API命令来获取消息.

I would be in turn using the same command for the POST API to fetch messages.

更新

根据@Badlop提供的建议,我用

As per the suggestion provided by @Badlop, I updated my function with

 % ----------------- Custom Command Get Message ----------------------
 #ejabberd_commands{name = get_message, tags = [stanza],
        desc = "Get messages from a local or remote bare of full JID",
        longdesc = "Get messages of a specific JID sent to a JID",
        module = ?MODULE, function = get_message,
        args = [{host, binary}, {from, binary}, {to, binary},
            {start_time, binary}, {end_time, binary}],
        args_example = [<<"localhost">>, <<"admin">>, <<"user1">>,
            <<"2015-07-00T00:00:00Z">>, <<"2015-07-029T13:23:54Z">>],
        args_desc = ["Host", "From JID", "Receiver JID", "Start Time", "End Time"],
        result = {result, {
                    tuple, [{messages, list, {message, {tuple,
                    [
                       {timestamp, string},
                       {xml, string},
                       {txt, string},
                       {peer, integer},
                       {kind, integer},
                       {nick, string}
                    ]}}},
                  {status, string},
                  {count, integer}]}}
        },
 % ----------------- Custom Command Ends -----------------------------

这是我收到命令时调用的函数.

This is my function that gets called when the command is received.

% ----------------- Custom Function Get Message ----------------------
get_message(Host, From, To, StartTime, EndTime) ->
    mod_mam:select(
        Host,
        jid:make(From, Host),
        jid:make(From, Host),
        [{start, xmpp_util:decode_timestamp(StartTime)},
         {'end', xmpp_util:decode_timestamp(EndTime)},
         {with, jid:make(To, Host)}],
        #rsm_set{},
        chat,
        all
    ).
% ----------------- Custom Function Get Message ----------------------

但是,它返回错误响应:-

However, it returns an error response:-

Unhandled exception occurred executing the command:
** exception error: no function clause matching
                  ejabberd_ctl:format_result([],
                                             {messages,list,
                                              {message,
                                               {tuple,
                                                [{timestamp,string},
                                                 {xml,string},
                                                 {peer,integer},
                                                 {kind,integer},
                                                 {nick,string}]}}}) (src/ejabberd_ctl.erl, line 405)
   in function  ejabberd_ctl:format_result/2 (src/ejabberd_ctl.erl, line 461)
   in call from ejabberd_ctl:try_call_command/4 (src/ejabberd_ctl.erl, line 321)
   in call from ejabberd_ctl:process2/4 (src/ejabberd_ctl.erl, line 274)
   in call from ejabberd_ctl:process/2 (src/ejabberd_ctl.erl, line 252)
   in call from rpc:'-handle_call_call/6-fun-0-'/5 (rpc.erl, line 197)

在日志中打印的查询如下:-

The query printed in the logs in as follow:-

2020-04-24 21:57:13.717746+05:30 [debug] SQL: "SELECT  timestamp, xml, peer, kind, nick FROM archive WHERE username=E'admin' and server_host=E'localhost' and bare_peer=E'test@localhost' and timestamp >= 1587692943312536 and timestamp <= 1587779343312536 ORDER BY timestamp ASC ;"
2020-04-24 21:57:13.726745+05:30 [debug] SQL: "SELECT COUNT(*) FROM archive WHERE  username=E'admin' and server_host=E'localhost' and bare_peer=E'test@localhost' and timestamp >= 1587692943312536 and timestamp <= 1587779343312536;"

推荐答案

嗯,您还很远,命令结果错误,必须处理调用结果.那怎么办?

Umm, you were still far away, the command result was wrong, and the call result must be processed. what about this?

$ ejabberdctl get_mam_messages user1@localhost user2@localhost 2020-04-27T00:00:00Z 2020-04-27T23:59:59Z

所需补丁:

diff --git a/src/mod_mam.erl b/src/mod_mam.erl
index 08a4059b4..d2d74913c 100644
--- a/src/mod_mam.erl
+++ b/src/mod_mam.erl
@@ -42,6 +42,7 @@
     get_room_config/4, set_room_option/3, offline_message/1, export/1,
     mod_options/1, remove_mam_for_user_with_peer/3, remove_mam_for_user/2,
     is_empty_for_user/2, is_empty_for_room/3, check_create_room/4,
+    get_messages_command/4,
     process_iq/3, store_mam_message/7, make_id/0, wrap_as_mucsub/2, select/7]).

 -include("xmpp.hrl").
@@ -1355,8 +1356,29 @@ get_jids(undefined) ->
 get_jids(Js) ->
     [jid:tolower(jid:remove_resource(J)) || J <- Js].

+get_messages_command(From, To, StartTime, EndTime) ->
+    FromJid = jid:decode(From),
+    {Stanzas, _, _} =
+   mod_mam:select(
+     FromJid#jid.lserver, FromJid, FromJid,
+     [{start, xmpp_util:decode_timestamp(StartTime)},
+      {'end', xmpp_util:decode_timestamp(EndTime)},
+      {with, jid:decode(To)}],
+     #rsm_set{}, chat, all),
+    [fxml:element_to_binary(xmpp:encode(Subels))
+     || {_, _, #forwarded{sub_els = [Subels]}} <- Stanzas].
+
 get_commands_spec() ->
-    [#ejabberd_commands{name = delete_old_mam_messages, tags = [purge],
+    [#ejabberd_commands{
+   name = get_mam_messages, tags = [mam],
+   desc = "Get archived messages of an account with another contact",
+   module = ?MODULE, function = get_messages_command,
+   args = [{from, binary}, {to, binary}, {start, binary}, {'end', binary}],
+   args_example = [<<"user1@localhost">>, <<"user2@example.org">>,
+           <<"2020-04-27T00:00:00Z">>, <<"2020-04-27T23:59:59Z">>],
+   args_desc = ["Local JID", "Contact JID", "Start Time", "End Time"],
+   result = {messages, {list, {message, string}}}},
+     #ejabberd_commands{name = delete_old_mam_messages, tags = [purge],
            desc = "Delete MAM messages older than DAYS",
            longdesc = "Valid message TYPEs: "
                   "\"chat\", \"groupchat\", \"all\".",

这篇关于Ejabberd-为自定义命令运行SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆