Software /
code /
prosody-modules
Annotate
mod_persisthosts/mod_persisthosts.lua @ 4935:a85efae90e21
mod_rest: Expand mapping of XEP-0045 join stanza
The previous 'join' mapping was apparently lost in translation when
swithing to datamapper, so might as well map some properties allowing
history control. Usually you probably want either zero history or
history since the last known time of being joined. Maybe that the former
should be the default?
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 30 Apr 2022 01:00:01 +0200 |
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 |