Mercurial > prosody-modules
annotate mod_candy/mod_candy.lua @ 2450:36ffe9d11132
mod_candy: Add config option for a list of rooms to join
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 20 Jan 2017 01:24:32 +0100 |
parents | c9372cfac3b7 |
children | 56bab95e57f0 |
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; |
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
|
5 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
|
6 |
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 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
|
8 |
2450
36ffe9d11132
mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents:
2449
diff
changeset
|
9 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
|
10 |
36ffe9d11132
mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents:
2449
diff
changeset
|
11 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
|
12 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
|
13 -- 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
|
14 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
|
15 end |
36ffe9d11132
mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents:
2449
diff
changeset
|
16 -- 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
|
17 return true; |
36ffe9d11132
mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents:
2449
diff
changeset
|
18 end |
36ffe9d11132
mod_candy: Add config option for a list of rooms to join
Kim Alvefur <zash@zash.se>
parents:
2449
diff
changeset
|
19 |
2449
c9372cfac3b7
mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents:
2440
diff
changeset
|
20 local function get_connect_path() |
c9372cfac3b7
mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents:
2440
diff
changeset
|
21 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
|
22 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
|
23 end |
c9372cfac3b7
mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents:
2440
diff
changeset
|
24 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
|
25 module:depends("bosh"); |
c9372cfac3b7
mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents:
2440
diff
changeset
|
26 end |
c9372cfac3b7
mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents:
2440
diff
changeset
|
27 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
|
28 end |
c9372cfac3b7
mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents:
2440
diff
changeset
|
29 |
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
|
30 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
|
31 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
|
32 ["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
|
33 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
|
34 |
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
|
35 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
|
36 .."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
|
37 :format(json_encode({ |
2449
c9372cfac3b7
mod_candy: Break out connect path into a function
Kim Alvefur <zash@zash.se>
parents:
2440
diff
changeset
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 })); |
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 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
|
45 ["GET /*"] = serve(module:get_directory().."/www_files"); |
1384
f67eacb1ac9f
mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents:
1031
diff
changeset
|
46 GET = function(event) |
f67eacb1ac9f
mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents:
1031
diff
changeset
|
47 event.response.headers.location = event.request.path.."/"; |
f67eacb1ac9f
mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents:
1031
diff
changeset
|
48 return 301; |
f67eacb1ac9f
mod_candy: Redirect from /candy -> /candy/
Kim Alvefur <zash@zash.se>
parents:
1031
diff
changeset
|
49 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
|
50 } |
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 |