Software /
code /
prosody-modules
Annotate
mod_persisthosts/mod_persisthosts.lua @ 4876:0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
In particular this transitions PEP nodes and data to be imported via mod_pep's
APIs, fixing issues with importing at runtime while PEP data may already be
live in RAM.
Next obvious candidate for this approach is rosters, so clients get immediate
roster pushes and other special handling (such as emitting subscribes to reach
the desired subscription state).
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 18 Jan 2022 17:01:18 +0000 |
parent | 2447:366fadb5c6e5 |
rev | line source |
---|---|
2446
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- mod_persisthosts |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 module:set_global(); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local set = require"util.set"; |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local stat = require"lfs".attributes; |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local resolve_relative_path = require"core.configmanager".resolve_relative_path; |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local vhost_path = module:get_option_string("persisthosts_path", "conf.d"); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local path_pattern = resolve_relative_path(prosody.paths.config, vhost_path) .. "/%s.cfg.lua"; |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local original = set.new(); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 original:include(prosody.hosts); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 module:hook("host-activated", function(host) |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 if not original:contains(host) then |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local path = path_pattern:format(host); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 if not stat(path) then |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local fh, err = io.open(path, "w"); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 if fh then |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 fh:write(("VirtualHost%q\n"):format(host)); |
2447
366fadb5c6e5
mod_persisthosts: Fire an event to allow other modules a chance to write to the new config file
Kim Alvefur <zash@zash.se>
parents:
2446
diff
changeset
|
21 module:fire_event("host-persisted", { host = host, file = fh }); |
2446
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 fh:close(); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 module:log("info", "Config file for host '%s' created", host); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 else |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 module:log("error", "Could not open '%s' for writing: %s", path, err or "duno"); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 end |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 else |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 module:log("debug", "File '%s' existed already", path); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 end |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 else |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 module:log("debug", "VirtualHost '%s' existed already", host); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 end |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 end); |
c563f4d64302
mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |