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