-
Notifications
You must be signed in to change notification settings - Fork 32
/
ejabberdmod.erl
90 lines (74 loc) · 2.26 KB
/
ejabberdmod.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
%%%' HEADER
%% @author {{author_name}} <{{author_email}}>
%% @copyright {{copyright_year}} {{author_name}}
%% @doc ejabberd module that ... listens to packets sent & received by users.
%% @end
-module(mod_{{name}}).
-author('{{author_name}} <{{author_email}}>').
-behaviour(gen_mod).
-export([start/2, init/2, stop/1,
send_packet/3, receive_packet/4]).
-include_lib("ejabberd/include/ejabberd.hrl").
-include("ejabberd/include/jlib.hrl").
-define(PROCNAME, ejabberd_{{name}}).
-ifdef(TEST).
-compile(export_all).
-endif.
%%%.
%%%' CALLBACKS
start(Host, Opts) ->
Opt1 = gen_mod:get_opt(opt1, Opts, "default value"),
% capture packets sent by user
ejabberd_hooks:add(user_send_packet, Host, ?MODULE, send_packet, 90),
% capture packets received by user
ejabberd_hooks:add(user_receive_packet, Host, ?MODULE, receive_packet, 90),
% register the module process for Host and spawn.
register(gen_mod:get_module_proc(Host, ?PROCNAME),
spawn(?MODULE, init, [Host, Opt1])).
stop(Host) ->
% remove hook for packets sent by user
ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, send_packet, 90),
% remove hook for packets received by user
ejabberd_hooks:delete(user_receive_packet, Host, ?MODULE, receive_packet, 90),
% send stop message to process
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
Proc ! stop,
{wait, Proc}.
init(Host, Opt1) ->
% do something here instead of nothing
loop(Host, Opt1).
loop(Host, Opt1) ->
receive
{persist, E} ->
persist(E),
loop(Host, Opt1);
stop ->
ok;
_ ->
loop(Host, Opt1)
end.
%%%.
%%%' PUBLIC API
%% @doc
%% @end
send_packet(FromJID, ToJID, P) ->
Host = FromJID#jid.lserver,
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
Proc ! {persist, {send, FromJID, ToJID, P}}.
%% @doc
%% @end
receive_packet(_JID, From, To, P) ->
Host = To#jid.lserver,
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
Proc ! {persist, {recv, From, To, P}}.
%%%.
%%%' PRIVATE FUNCTIONS
%% @private
persist({recv, From, To, P}) ->
% do something with this data...like persist it somehow
ok;
persist({send, FromJID, ToJID, P}) ->
% do something with this data...like persist it somehow
ok.
%%%.
%%% vim: set filetype=erlang tabstop=2 foldmarker=%%%',%%%. foldmethod=marker: