Annotate

mod_candy/mod_candy.lua @ 2453:f36a6dcc05ef

mod_candy: Add note about redirect from /candy to /candy/ not being necessary anymore
author Kim Alvefur <zash@zash.se>
date Fri, 20 Jan 2017 01:21:21 +0100
parent 2451:56bab95e57f0
child 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");
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
11
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
12 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
13 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
14 -- 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
15 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
16 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
17 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
18 -- 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
19 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
20 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
21 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
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 -- 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
24 -- 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
25 return true;
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
26 end
36ffe9d11132 mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents: 2449
diff changeset
27
2449
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
28 local function get_connect_path()
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
29 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
30 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
31 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
32 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
33 module:depends("bosh");
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
34 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
35 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
36 end
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
37
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
38 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
39 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
40 ["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
41 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
42
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
43 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
44 .."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
45 :format(json_encode({
2449
c9372cfac3b7 mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents: 2440
diff changeset
46 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
47 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
48 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
49 host = module:get_host();
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 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
51 }));
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 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
53 ["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
54
f36a6dcc05ef mod_candy: Add note about redirect from /candy to /candy/ not being necessary anymore
Kim Alvefur <zash@zash.se>
parents: 2451
diff changeset
55 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
56 event.response.headers.location = event.request.path.."/";
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
57 return 301;
f67eacb1ac9f mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents: 1031
diff changeset
58 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
59 }
5a975ba6a845 mod_candy: Example of how easy it is to serve files from a prosody module
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 });
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