./ct_report/coverage/mod_version.COVER.html

1 -module(mod_version).
2
3 -behaviour(gen_mod).
4 -behaviour(mongoose_module_metrics).
5
6 -include("jlib.hrl").
7 -include("mongoose_config_spec.hrl").
8
9 -export([start/2]).
10 -export([stop/1]).
11 -export([supported_features/0]).
12 -export([config_spec/0]).
13 -export([process_iq/5]).
14
15 -ignore_xref([process_iq/5]).
16
17 -xep([{xep, 92}, {version, "1.1"}]).
18
19 -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> any().
20 start(HostType, Opts) ->
21 2 IQDisc = gen_mod:get_opt(iqdisc, Opts, no_queue),
22 2 gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_VERSION, ejabberd_local,
23 fun ?MODULE:process_iq/5, #{}, IQDisc).
24
25 -spec stop(mongooseim:host_type()) -> any().
26 stop(HostType) ->
27 2 gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_VERSION, ejabberd_local).
28
29 -spec supported_features() -> [atom()].
30 supported_features() ->
31 2 [dynamic_domains].
32
33 -spec config_spec() -> mongoose_config_spec:config_section().
34 config_spec() ->
35 146 #section{
36 items = #{<<"iqdisc">> => mongoose_config_spec:iqdisc(),
37 <<"os_info">> => #option{type = boolean}
38 }
39 }.
40
41 -spec process_iq(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq(), any()) ->
42 {mongoose_acc:t(), jlib:iq()}.
43 process_iq(Acc, _From, _To, #iq{type = set, sub_el = SubEl} = IQ, _Extra) ->
44
:-(
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:not_allowed()]}};
45 process_iq(Acc, _From, _To, #iq{type = get} = IQ, _Extra) ->
46 2 HostType = mongoose_acc:host_type(Acc),
47 2 {Name, Version} = mongoose_info(),
48 2 {Acc, IQ#iq{type = result,
49 sub_el =
50 [#xmlel{name = <<"query">>,
51 attrs = [{<<"xmlns">>, ?NS_VERSION}],
52 children =
53 [#xmlel{name = <<"name">>, attrs = [],
54 children =[#xmlcdata{content = Name}]},
55 #xmlel{name = <<"version">>, attrs = [],
56 children =[#xmlcdata{content = Version}]}
57 ] ++ add_os_info(HostType)}]}}.
58
59 -spec add_os_info(mongooseim:host_type()) -> [exml:element()] | [].
60 add_os_info(HostType) ->
61 2 case gen_mod:get_module_opt(HostType, ?MODULE, os_info, false) of
62 true ->
63 1 [#xmlel{name = <<"os">>, attrs = [],
64 children = [#xmlcdata{content = os_info()}]}];
65 _ ->
66 1 []
67 end.
68
69 -spec mongoose_info() -> {binary(), binary()}.
70 mongoose_info() ->
71 2 {ok, Version} = application:get_key(mongooseim, vsn),
72 2 {<<"MongooseIM">>, list_to_binary(Version)}.
73
74 -spec os_info() -> binary().
75 os_info() ->
76 1 {Family, Name} = os:type(),
77 1 {Major, Minor, Release} = os:version(),
78 1 list_to_binary(
79 atom_to_list(Family) ++ " " ++
80 atom_to_list(Name) ++ " " ++
81 integer_to_list(Major) ++ "." ++
82 integer_to_list(Minor) ++ "." ++
83 integer_to_list(Release)
84 ).
Line Hits Source