aboutsummaryrefslogtreecommitdiff
path: root/src/mod_conversejs.erl
blob: 0b5d57a15680cf859f55ec8e293f3940bc8f9e98 (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
%%%----------------------------------------------------------------------
%%% File    : mod_conversejs.erl
%%% Author  : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Serve simple page for Converse.js XMPP web browser client
%%% Created :  8 Nov 2021 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2021   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.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------

-module(mod_conversejs).

-author('alexey@process-one.net').

-behaviour(gen_mod).

-export([start/2, stop/1, reload/3, process/2, depends/2,
         mod_opt_type/1, mod_options/1, mod_doc/0]).

-include_lib("xmpp/include/xmpp.hrl").
-include("logger.hrl").
-include("ejabberd_http.hrl").
-include("translate.hrl").
-include("ejabberd_web_admin.hrl").

start(_Host, _Opts) ->
    ok.

stop(_Host) ->
    ok.

reload(_Host, _NewOpts, _OldOpts) ->
    ok.

depends(_Host, _Opts) ->
    [].

process([], #request{method = 'GET'}) ->
    Host = ejabberd_config:get_myname(),
    Domain = gen_mod:get_module_opt(Host, ?MODULE, default_domain),
    Script = gen_mod:get_module_opt(Host, ?MODULE, conversejs_script),
    CSS = gen_mod:get_module_opt(Host, ?MODULE, conversejs_css),
    Init = [{<<"discover_connection_methods">>, false},
            {<<"jid">>, Domain},
            {<<"default_domain">>, Domain},
            {<<"domain_placeholder">>, Domain},
            {<<"view_mode">>, <<"fullscreen">>}],
    Init2 =
        case gen_mod:get_module_opt(Host, ?MODULE, websocket_url) of
            undefined -> Init;
            WSURL -> [{<<"websocket_url">>, WSURL} | Init]
        end,
    Init3 =
        case gen_mod:get_module_opt(Host, ?MODULE, bosh_service_url) of
            undefined -> Init2;
            BoshURL -> [{<<"bosh_service_url">>, BoshURL} | Init2]
        end,
    {200, [html],
     [<<"<!DOCTYPE html>">>,
      <<"<html>">>,
      <<"<head>">>,
      <<"<meta charset='utf-8'>">>,
      <<"<link rel='stylesheet' type='text/css' media='screen' href='">>,
      fxml:crypt(CSS), <<"'>">>,
      <<"<script src='">>, fxml:crypt(Script), <<"' charset='utf-8'></script>">>,
      <<"</head>">>,
      <<"<body>">>,
      <<"<script>">>,
      <<"converse.initialize(">>, jiffy:encode({Init3}), <<");">>,
      <<"</script>">>,
      <<"</body>">>,
      <<"</html>">>]};
process(_, _) ->
    ejabberd_web:error(not_found).

mod_opt_type(bosh_service_url) ->
    econf:either(undefined, econf:binary());
mod_opt_type(websocket_url) ->
    econf:either(undefined, econf:binary());
mod_opt_type(conversejs_script) ->
    econf:binary();
mod_opt_type(conversejs_css) ->
    econf:binary();
mod_opt_type(default_domain) ->
    econf:binary().

mod_options(_) ->
    [{bosh_service_url, undefined},
     {websocket_url, undefined},
     {default_domain, ejabberd_config:get_myname()},
     {conversejs_script, <<"https://cdn.conversejs.org/dist/converse.min.js">>},
     {conversejs_css, <<"https://cdn.conversejs.org/dist/converse.min.css">>}].

mod_doc() ->
    #{desc =>
          [?T("This module serves a simple page for the "
              "https://conversejs.org/[Converse] XMPP web browser client."), "",
           ?T("To use this module, in addition to adding it to the 'modules' "
              "section, you must also enable it in 'listen' -> 'ejabberd_http' -> "
              "http://../listen-options/#request-handlers[request_handlers]."), "",
           ?T("You must also setup either the option 'websocket_url' or 'bosh_service_url'."), "",
           ?T("By default, the options 'conversejs_css' and 'conversejs_script'"
              " point to the public Converse.js client. Alternatively, you can"
              " host the client locally using _`mod_http_fileserver`_.")
          ],
     example =>
         ["listen:",
          "  -",
          "    port: 5280",
          "    module: ejabberd_http",
          "    request_handlers:",
          "      /websocket: ejabberd_http_ws",
          "      /conversejs: mod_conversejs",
          "",
          "modules:",
          "  mod_conversejs:",
          "    websocket_url: \"ws://example.org:5280/websocket\""],
      opts =>
          [{websocket_url,
            #{value => ?T("WebsocketURL"),
              desc =>
                  ?T("A websocket URL to which Converse.js can connect to.")}},
           {bosh_service_url,
            #{value => ?T("BoshURL"),
              desc =>
                  ?T("BOSH service URL to which Converse.js can connect to.")}},
           {default_domain,
            #{value => ?T("Domain"),
              desc =>
                  ?T("Specify a domain to act as the default for user JIDs. "
                     "The default value is the first domain defined in the "
                     "ejabberd configuration file.")}},
           {conversejs_script,
            #{value => ?T("URL"),
              desc =>
                  ?T("Converse.js main script URL.")}},
           {conversejs_css,
            #{value => ?T("URL"),
              desc =>
                  ?T("Converse.js CSS URL.")}}]
     }.