aboutsummaryrefslogtreecommitdiff
path: root/src/adhoc.erl
blob: 6af65e5d16c129b75a0bf60da2fa1c0668ed2874 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
%%%----------------------------------------------------------------------
%%% File    : adhoc.erl
%%% Author  : Magnus Henoch <henoch@dtek.chalmers.se>
%%% Purpose : Provide helper functions for ad-hoc commands (XEP-0050)
%%% Created : 31 Oct 2005 by Magnus Henoch <henoch@dtek.chalmers.se>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2013   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with this program; if not, write to the Free Software
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
%%% 02111-1307 USA
%%%
%%%----------------------------------------------------------------------

-module(adhoc).

-author('henoch@dtek.chalmers.se').

-export([
    parse_request/1,
    produce_response/2,
    produce_response/1
]).

-include("ejabberd.hrl").
-include("jlib.hrl").
-include("adhoc.hrl").

%% Parse an ad-hoc request.  Return either an adhoc_request record or
%% an {error, ErrorType} tuple.
%%
-spec(parse_request/1 ::
(
  IQ :: iq_request())
    -> adhoc_response()
    %%
     | {error, _}
).

parse_request(#iq{type = set, lang = Lang, sub_el = SubEl, xmlns = ?NS_COMMANDS}) ->
    ?DEBUG("entering parse_request...", []),
    Node = xml:get_tag_attr_s(<<"node">>, SubEl),
    SessionID = xml:get_tag_attr_s(<<"sessionid">>, SubEl),
    Action = xml:get_tag_attr_s(<<"action">>, SubEl),
    XData = find_xdata_el(SubEl),
    #xmlel{children = AllEls} = SubEl,
    Others = case XData of
        false -> AllEls;
        _     -> lists:delete(XData, AllEls)
    end,
    #adhoc_request{
        lang      = Lang,
        node      = Node,
        sessionid = SessionID,
        action    = Action,
        xdata     = XData,
        others    = Others
    };
parse_request(_) -> {error, ?ERR_BAD_REQUEST}.

%% Borrowed from mod_vcard.erl
find_xdata_el(#xmlel{children = SubEls}) ->
    find_xdata_el1(SubEls).

find_xdata_el1([]) -> false;
find_xdata_el1([El | Els]) when is_record(El, xmlel) ->
    case xml:get_tag_attr_s(<<"xmlns">>, El) of
        ?NS_XDATA -> El;
        _         -> find_xdata_el1(Els)
    end;
find_xdata_el1([_ | Els]) -> find_xdata_el1(Els).

%% Produce a <command/> node to use as response from an adhoc_response
%% record, filling in values for language, node and session id from
%% the request.
%%
-spec(produce_response/2 ::
(
  Adhoc_Request  :: adhoc_request(),
  Adhoc_Response :: adhoc_response())
    -> Xmlel::xmlel()
).

%% Produce a <command/> node to use as response from an adhoc_response
%% record.
produce_response(#adhoc_request{lang = Lang, node = Node, sessionid = SessionID},
  Adhoc_Response) ->
    produce_response(Adhoc_Response#adhoc_response{
        lang = Lang, node = Node, sessionid = SessionID
    }).

%%
-spec(produce_response/1 ::
(
  Adhoc_Response::adhoc_response())
    -> Xmlel::xmlel()
).

produce_response(
  #adhoc_response{
   %lang          = _Lang,
    node          = Node,
    sessionid     = ProvidedSessionID,
    status        = Status,
    defaultaction = DefaultAction,
    actions       = Actions,
    notes         = Notes,
    elements      = Elements
  }) ->
    SessionID = if is_binary(ProvidedSessionID),
        ProvidedSessionID /= <<"">> -> ProvidedSessionID;
        true                        -> jlib:now_to_utc_string(now())
    end,
    case Actions of
        [] ->
            ActionsEls = [];
        _ ->
            case DefaultAction of
                <<"">> -> ActionsElAttrs = [];
                _      -> ActionsElAttrs = [{<<"execute">>, DefaultAction}]
            end,
            ActionsEls = [
                #xmlel{
                    name = <<"actions">>,
                    attrs = ActionsElAttrs,
                    children = [
                        #xmlel{name = Action, attrs = [], children = []}
                            || Action <- Actions]
                }
            ]
    end,
    NotesEls = lists:map(fun({Type, Text}) ->
        #xmlel{
            name     = <<"note">>,
            attrs    = [{<<"type">>, Type}],
            children = [{xmlcdata, Text}]
        }
    end, Notes),
    #xmlel{
        name     = <<"command">>,
        attrs    = [
            {<<"xmlns">>,     ?NS_COMMANDS},
            {<<"sessionid">>, SessionID},
            {<<"node">>,      Node},
            {<<"status">>,    iolist_to_binary(atom_to_list(Status))}
        ],
        children = ActionsEls ++ NotesEls ++ Elements
    }.