./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 200 #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 }.
41
42 -spec process_iq(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq(), any()) ->
43 {mongoose_acc:t(), jlib:iq()}.
44 process_iq(Acc, _From, _To, #iq{type = set, sub_el = SubEl} = IQ, _Extra) ->
45
:-(
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:not_allowed()]}};
46 process_iq(Acc, _From, _To, #iq{type = get} = IQ, _Extra) ->
47 2 HostType = mongoose_acc:host_type(Acc),
48 2 {Name, Version} = mongoose_info(),
49 2 {Acc, IQ#iq{type = result,
50 sub_el =
51 [#xmlel{name = <<"query">>,
52 attrs = [{<<"xmlns">>, ?NS_VERSION}],
53 children =
54 [#xmlel{name = <<"name">>, attrs = [],
55 children =[#xmlcdata{content = Name}]},
56 #xmlel{name = <<"version">>, attrs = [],
57 children =[#xmlcdata{content = Version}]}
58 ] ++ add_os_info(HostType)}]}}.
59
60 -spec add_os_info(mongooseim:host_type()) -> [exml:element()] | [].
61 add_os_info(HostType) ->
62 2 case gen_mod:get_module_opt(HostType, ?MODULE, os_info) of
63 true ->
64 1 [#xmlel{name = <<"os">>, attrs = [],
65 children = [#xmlcdata{content = os_info()}]}];
66 _ ->
67 1 []
68 end.
69
70 -spec mongoose_info() -> {binary(), binary()}.
71 mongoose_info() ->
72 2 {ok, Version} = application:get_key(mongooseim, vsn),
73 2 {<<"MongooseIM">>, list_to_binary(Version)}.
74
75 -spec os_info() -> binary().
76 os_info() ->
77 1 {Family, Name} = os:type(),
78 1 {Major, Minor, Release} = os:version(),
79 1 list_to_binary(
80 atom_to_list(Family) ++ " " ++
81 atom_to_list(Name) ++ " " ++
82 integer_to_list(Major) ++ "." ++
83 integer_to_list(Minor) ++ "." ++
84 integer_to_list(Release)
85 ).
Line Hits Source