Annotate

mod_candy/mod_candy.lua @ 3211:2969ed764fe8

mod_adhoc_test: A module to test dataforms generation and rendering
author Kim Alvefur <zash@zash.se>
date Fri, 03 Aug 2018 19:30:15 +0200
parent 2454:51cf82d36a8a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_candy.lua
2440
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
2 -- Copyright (C) 2013-2017 Kim Alvefur
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local json_encode = require"util.json".encode;
2451
56bab95e57f0 mod_candy: If no room list is set, try to find a MUC domain and guess a room name of 'candy'
Kim Alvefur <zash@zash.se>
parents: 2450
diff changeset
5 local get_host_children = require "core.hostmanager".get_children;
2440
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
6 local is_module_loaded = require "core.modulemanager".is_loaded;
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local serve = module:depends"http_files".serve;
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
2450
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
10 local candy_rooms = module:get_option_array("candy_rooms");
2454
51cf82d36a8a mod_candy: Add support for enabling Candy debug mode from Prosodys config
Kim Alvefur <zash@zash.se>
parents: 2453
diff changeset
11 local candy_debug = module:get_option_boolean("candy_debug", false);
2450
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
12
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
13 local function get_autojoin()
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
14 if candy_rooms then
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
15 -- Configured room list, if any
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
16 return candy_rooms;
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
17 end
2451
56bab95e57f0 mod_candy: If no room list is set, try to find a MUC domain and guess a room name of 'candy'
Kim Alvefur <zash@zash.se>
parents: 2450
diff changeset
18 for subdomain in pairs(get_host_children(module.host)) do
56bab95e57f0 mod_candy: If no room list is set, try to find a MUC domain and guess a room name of 'candy'
Kim Alvefur <zash@zash.se>
parents: 2450
diff changeset
19 -- Attempt autodetect a MUC host
56bab95e57f0 mod_candy: If no room list is set, try to find a MUC domain and guess a room name of 'candy'
Kim Alvefur <zash@zash.se>
parents: 2450
diff changeset
20 if is_module_loaded(subdomain, "muc") then
56bab95e57f0 mod_candy: If no room list is set, try to find a MUC domain and guess a room name of 'candy'
Kim Alvefur <zash@zash.se>
parents: 2450
diff changeset
21 return { "candy@" .. subdomain }
56bab95e57f0 mod_candy: If no room list is set, try to find a MUC domain and guess a room name of 'candy'
Kim Alvefur <zash@zash.se>
parents: 2450
diff changeset
22 end
56bab95e57f0 mod_candy: If no room list is set, try to find a MUC domain and guess a room name of 'candy'
Kim Alvefur <zash@zash.se>
parents: 2450
diff changeset
23 end
56bab95e57f0 mod_candy: If no room list is set, try to find a MUC domain and guess a room name of 'candy'
Kim Alvefur <zash@zash.se>
parents: 2450
diff changeset
24 -- Autojoin bookmarks then?
2450
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
25 -- Check out mod_default_bookmarks
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
26 return true;
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
27 end
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
28
2449
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
29 local function get_connect_path()
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
30 if is_module_loaded(module.host, "websocket") then
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
31 return module:http_url("websocket", "xmpp-websocket"):gsub("^http", "ws");
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
32 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
33 if not is_module_loaded(module.host, "bosh") then
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
34 module:depends("bosh");
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
35 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
36 return module:http_url("bosh", "/http-bind");
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
37 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
38
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 module:provides("http", {
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 route = {
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 ["GET /prosody.js"] = function(event)
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 event.response.headers.content_type = "text/javascript";
2440
7814a5c7fee8 mod_candy: Provide websocket URI if mod_websocket is loaded, fall back to BOSH
Kim Alvefur <zash@zash.se>
parents: 1864
diff changeset
43
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return ("// Generated by Prosody\n"
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 .."var Prosody = %s;\n")
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 :format(json_encode({
2449
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
47 connect_path = get_connect_path();
2450
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
48 autojoin = get_autojoin();
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 version = prosody.version;
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 host = module:get_host();
2454
51cf82d36a8a mod_candy: Add support for enabling Candy debug mode from Prosodys config
Kim Alvefur <zash@zash.se>
parents: 2453
diff changeset
51 debug = candy_debug;
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 anonymous = module:get_option_string("authentication") == "anonymous";
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 }));
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end;
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 ["GET /*"] = serve(module:get_directory().."/www_files");
2453
f36a6dcc05ef mod_candy: Add note about redirect from /candy to /candy/ not being necessary anymore
Kim Alvefur <zash@zash.se>
parents: 2451
diff changeset
56
f36a6dcc05ef mod_candy: Add note about redirect from /candy to /candy/ not being necessary anymore
Kim Alvefur <zash@zash.se>
parents: 2451
diff changeset
57 GET = function(event) -- TODO Remove this, it's done by mod_http in 0.10+
1384
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
58 event.response.headers.location = event.request.path.."/";
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
59 return 301;
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
60 end;
933
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 }
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 });
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63