ejabberd - Comments for "Update user presence using API" https://www.ejabberd.im/forum/28880/update-user-presence-using-api en Ok, I've comitted the patch https://www.ejabberd.im/forum/28880/update-user-presence-using-api#comment-67562 <p>Ok, I've comitted the patch to git: <noindex><a href="https://github.com/processone/ejabberd/commit/aaef1a14b4f79c42f5943fd9149ecee3ca48cdb1" title="https://github.com/processone/ejabberd/commit/aaef1a14b4f79c42f5943fd9149ecee3ca48cdb1" rel="nofollow" >https://github.com/processone/ejabberd/commit/aaef1a14b4f79c42f5943fd914...</a></noindex></p> Fri, 07 Jul 2017 08:56:10 +0000 badlop comment 67562 at https://www.ejabberd.im Hi, I applied the patch at https://www.ejabberd.im/forum/28880/update-user-presence-using-api#comment-67561 <p>Hi,</p> <p>I applied the patch at version 17.06. It worked! </p> <p>Thanks! :)</p> Thu, 06 Jul 2017 17:25:51 +0000 nivaldomjunior comment 67561 at https://www.ejabberd.im Ah, you are right, that https://www.ejabberd.im/forum/28880/update-user-presence-using-api#comment-67557 <p>Ah, you are right, that doesn't work correctly in recent ejabberd. I've written and tested this patch. Can you test it?</p> <pre> From 0a279e834968666906e04a5d225aed826d6282ca Mon Sep 17 00:00:00 2001 From: Badlop Date: Wed, 5 Jul 2017 18:27:02 +0200 Subject: [PATCH] Fix set_presence command to work in recent ejabberd --- src/ejabberd_c2s.erl | 9 ++++++++- src/mod_admin_extra.erl | 24 ++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 7cbc16f..9738b40 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -46,7 +46,7 @@ reject_unauthenticated_packet/2, process_closed/2, process_terminated/2, process_info/2]). %% API --export([get_presence/1, resend_presence/1, resend_presence/2, +-export([set_presence/2, get_presence/1, resend_presence/1, resend_presence/2, open_session/1, call/3, send/2, close/1, close/2, stop/1, reply/2, copy_state/2, set_timeout/2, route/2, host_up/1, host_down/1]). @@ -93,6 +93,10 @@ call(Ref, Msg, Timeout) -&gt; reply(Ref, Reply) -&gt; xmpp_stream_in:reply(Ref, Reply). +-spec set_presence(pid(), any()) -&gt; presence(). +set_presence(Ref, Pres) -&gt; + call(Ref, {set_presence, Pres}, 1000). + -spec get_presence(pid()) -&gt; presence(). get_presence(Ref) -&gt; call(Ref, get_presence, 1000). @@ -516,6 +520,9 @@ init([State, Opts]) -&gt; shaper =&gt; Shaper}, ejabberd_hooks:run_fold(c2s_init, {ok, State1}, [Opts]). +handle_call({set_presence, Pres}, From, State) -&gt; + reply(From, ok), + process_self_presence(State, Pres); handle_call(get_presence, From, #{jid := JID} = State) -&gt; Pres = case maps:get(pres_last, State, error) of error -&gt; diff --git a/src/mod_admin_extra.erl b/src/mod_admin_extra.erl index e5d2892..f2cb692 100644 --- a/src/mod_admin_extra.erl +++ b/src/mod_admin_extra.erl @@ -1002,20 +1002,16 @@ set_presence(User, Host, Resource, Type, Show, Status, Priority0) -&gt; Priority = if is_integer(Priority0) -&gt; Priority0; true -&gt; binary_to_integer(Priority0) end, - case ejabberd_sm:get_session_pid(User, Host, Resource) of - none -&gt; - error; - Pid -&gt; - From = jid:make(User, Host, Resource), - To = jid:make(User, Host), - Presence = #presence{from = From, to = To, - type = misc:binary_to_atom(Type), - show = misc:binary_to_atom(Show), - status = xmpp:mk_text(Status), - priority = Priority}, - Pid ! {route, Presence}, - ok - end. + Pres = #presence{ + from = jid:make(User, Host, Resource), + to = jid:make(User, Host), + type = misc:binary_to_atom(Type), + status = xmpp:mk_text(Status), + show = misc:binary_to_atom(Show), + priority = Priority, + sub_els = []}, + Ref = ejabberd_sm:get_session_pid(User, Host, Resource), + ejabberd_c2s:set_presence(Ref, Pres). user_sessions_info(User, Host) -&gt; CurrentSec = calendar:datetime_to_gregorian_seconds({date(), time()}), -- 2.7.4 </pre> Wed, 05 Jul 2017 16:28:33 +0000 badlop comment 67557 at https://www.ejabberd.im Hi, Did your clients received https://www.ejabberd.im/forum/28880/update-user-presence-using-api#comment-67555 <p>Hi,</p> <p>Did your clients received the presence and updated the user presence?<br /> I have this at the logs too, but the presence is not updated on the user clients, neither at the server side.</p> <p>[root@unity bin]# ./ejabberdctl get_presence junior unity.local<br /> junior@unity.local/MacBook-Pro-de-Nivaldo available<br /> [root@unity bin]# ./ejabberdctl set_presence junior unity.local MacBook-Pro-de-Nivaldo available away Saiu 0<br /> [root@unity bin]# ./ejabberdctl get_presence junior unity.local<br /> junior@unity.local/MacBook-Pro-de-Nivaldo available</p> <p>At the log i receive the same messages, the only diference is the last line 19:00:58.742 [debug] user1@localhost/tka1 acknowledged 16 of 16 stanzas:</p> <p>2017-06-24 11:42:20.853 [debug] &lt;0.307.0&gt;@ejabberd_access_permissions:handle_call:139 Command 'set_presence' execution allowed by rule 'API used from localhost allows all calls' (CallerInfo=#{caller_module =&gt; ejabberd_ctl})<br /> 2017-06-24 11:42:20.853 [debug] &lt;0.686.0&gt;@ejabberd_commands:do_execute_command:633 Executing command mod_admin_extra:set_presence with Args=[&lt;&lt;"junior"&gt;&gt;,&lt;&lt;"unity.local"&gt;&gt;,&lt;&lt;"MacBook-Pro-de-Nivaldo"&gt;&gt;,&lt;&lt;"available"&gt;&gt;,&lt;&lt;"away"&gt;&gt;,&lt;&lt;"Saiu"&gt;&gt;,&lt;&lt;"0"&gt;&gt;]<br /> 2017-06-24 11:42:20.853 [debug] &lt;0.646.0&gt;@mod_client_state:filter_other:291 Won't add stanza for junior@unity.local/MacBook-Pro-de-Nivaldo to CSI queue<br /> 2017-06-24 11:42:20.853 [debug] &lt;0.646.0&gt;@mod_client_state:dequeue_sender:330 Flushing packets of junior@unity.local from CSI queue of junior@unity.local/MacBook-Pro-de-Nivaldo<br /> 2017-06-24 11:42:20.853 [debug] &lt;0.646.0&gt;@ejabberd_socket:send:216 (tls|&lt;0.645.0&gt;) Send XML on stream = &lt;&lt;"0awaySaiu"&gt;&gt;</p> <p>Do i need to change any configuration at the server side?</p> <p>Thanks!</p> Tue, 04 Jul 2017 11:40:35 +0000 nivaldomjunior comment 67555 at https://www.ejabberd.im I execute a command similar https://www.ejabberd.im/forum/28880/update-user-presence-using-api#comment-67554 <p>I execute a command similar to yours, indicating an existing XMPP session (a user is already connected there):</p> <pre> $ ejabberdctl set_presence user1 localhost tka1 available away Saiu 0 $ </pre><p>Then the connected client receives:</p> <pre> &lt;presence to='user1@localhost/tka1' from='user1@localhost/tka1'&gt; &lt;priority&gt;0&lt;/priority&gt; &lt;show&gt;away&lt;/show&gt; &lt;status xml:lang='en'&gt;Saiu&lt;/status&gt; &lt;/presence&gt; </pre><p> If I configure loglevel to 5, then I see in the logs:</p> <pre> 19:00:58.716 [debug] Command 'set_presence' execution allowed by rule 'console commands' (CallerInfo=#{caller_module =&gt; ejabberd_ctl}) 19:00:58.716 [debug] Executing command mod_admin_extra:set_presence with Args=["user1",... 19:00:58.718 [debug] Won't add stanza for user1@localhost/tka1 to CSI queue 19:00:58.718 [debug] Flushing packets of user1@localhost from CSI queue of user1@localhost/tka1 19:00:58.718 [debug] (tcp|&lt;0.544.0&gt;) Send XML on stream = "&lt;presence to='user1@localhost/tka1' from='user1@localhost/tka1'&gt;... 19:00:58.742 [debug] user1@localhost/tka1 acknowledged 16 of 16 stanzas </pre> Mon, 03 Jul 2017 17:02:00 +0000 badlop comment 67554 at https://www.ejabberd.im