问题
I want to know when a user is logged in and logged out from an ejabberd session in a custom module, without changing the ejabberd code.
I need that because I have to execute some actions when a user logs in and clean up the actions I did when the user logs out. Also, I need to be able to logoff a user given some circumstances.
So, is there a way to extend some module to get those feature? I'm still looking for some documentation that could help me with that.
回答1:
You can write your own code and build it has a plugin with the behaviour gen_mod that ejabberd gives you. A nice place to begin with is this blog/tutorial and follow to next part. This should be enough but you will find more on the same blog.
After you get a little more comfortable with building your own module I suggest you take a look at the hooks set_presence_hook and unset_presence_hook
Just notice that set_presence_hook is activated every time a presence is set, not only on log in, you just have to work that around, if you can.
Long story short you will end up with something like the following
-module(mod_your_mod).
-behavior(gen_mod).
-include("ejabberd.hrl").
-export([start/2, stop/1, on_set/4, on_unset/4]).
start(Host, _Opts) ->
ejabberd_hooks:add(set_presence_hook, Host, ?MODULE, on_set, 50),
ejabberd_hooks:add(unset_presence_hook, Host, ?MODULE, on_unset, 50),
ok.
stop(Host) ->
ejabberd_hooks:delete(set_presence_hook, Host, ?MODULE, on_set, 50),
ejabberd_hooks:delete(unset_presence_hook, Host, ?MODULE, on_unset, 50),
ok.
on_set(User, Server, _Resource, _Packet) ->
<presence code>
on_unset(User, Server, _Resource, _Packet) ->
<offline code>
Hope this help
来源:https://stackoverflow.com/questions/8392326/intercept-login-logout-ejabberd