~samwhited/xmpp

16bd723b15ac977489574768a3994cbacf3cc9ed — Sam Whited 2 months ago 54b1b91
internal/integration/prosody: add muc room config

Allowing us to pre-configure MUC rooms will reduce boilerplate (and
round trips) for integration tests in the muc package.

Also ship the license file like we do for Go to make sure we're properly
attributing the authors and following the license to the best of our
ability.

Signed-off-by: Sam Whited <sam@samwhited.com>
A internal/integration/prosody/LICENSE.modules => internal/integration/prosody/LICENSE.modules +20 -0
@@ 0,0 1,20 @@
The MIT License (MIT)

Copyright (c) 2009-2015 Various Contributors (see individual files and source control)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

M internal/integration/prosody/config.go => internal/integration/prosody/config.go +56 -5
@@ 23,12 23,34 @@ type Config struct {
	VHosts    []string
	Options   map[string]interface{}
	Component map[string]struct {
		Name    string
		Secret  string
		Modules []string
		Name        string
		Secret      string
		Modules     []string
		MUCDefaults []ChannelConfig
	}
}

// ChannelConfig configures a Multi-User Chat channel.
type ChannelConfig struct {
	Localpart          string
	Admins             []string
	Owners             []string
	Visitors           []string
	Name               string
	Desc               string
	AllowMemberInvites bool
	ChangeSubject      bool
	HistoryLen         int
	Lang               string
	Pass               string
	Logging            bool
	MembersOnly        bool
	Moderated          bool
	Persistent         bool
	Public             bool
	PublicJIDs         bool
}

const cfgBase = `daemonize = false
pidfile = "{{ filepathJoin .ConfigDir "prosody.pid" }}"
admins = { {{ joinQuote .Admins }} }


@@ 110,8 132,37 @@ VirtualHost "{{ . }}"

{{ range $domain, $cfg := .Component }}
Component "{{$domain}}" {{if $cfg.Name}}"{{$cfg.Name}}"{{end}}
				 {{if $cfg.Modules}}modules_enabled = { {{ luaList $cfg.Modules }} }{{end}}
				 {{if $cfg.Secret}}component_secret = "{{$cfg.Secret}}"{{end}}
	{{if $cfg.Modules}}modules_enabled = { {{ luaList $cfg.Modules }} }{{end}}
	{{if $cfg.Secret}}component_secret = "{{$cfg.Secret}}"{{end}}
	{{if $cfg.MUCDefaults }}
	default_mucs = {
	{{range $cfg.MUCDefaults}}
			{
				 jid_node = "{{.Localpart}}",
				 affiliations = {
									admin = { {{ joinQuote .Admins }} },
									owner = { {{ joinQuote .Owners }} },
									visitors = { {{ joinQuote .Visitors }} }
				 },
				 config = {
									name = "{{.Name}}",
									description = "{{.Desc}}",
									allow_member_invites = {{.AllowMemberInvites}},
									change_subject = {{.ChangeSubject}},
									history_length = {{.HistoryLen}},
									lang = "{{.Lang}}",
									logging = {{.Logging}},
									members_only = {{.MembersOnly}},
									moderated = {{.Moderated}},
									persistent = {{.Persistent}},
									public = {{.Public}},
									public_jids = {{.PublicJIDs}}
									{{ if .Pass}}, pass = {{quoteOrPrint .Pass}}{{end}}
				 }
			}
	{{end}}
	}
	{{end}}
{{ end }}`

var cfgTmpl = template.Must(template.New("cfg").Funcs(template.FuncMap{

M internal/integration/prosody/prosody.go => internal/integration/prosody/prosody.go +119 -3
@@ 1,6 1,10 @@
// Copyright 2020 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
//
// Some Lua embedded in this file is taken from the Prosody Community Modules
// and is licensed under the terms of the MIT license, a copy of which can be
// found in the file "LICENSE.modules".

// Package prosody facilitates integration testing against Prosody.
package prosody // import "mellium.im/xmpp/internal/integration/prosody"


@@ 169,6 173,117 @@ func MUC(domain string, modules ...string) integration.Option {
	return Component(domain, "", "muc", modules...)
}

// Channel configures the MUC component (if loaded) with a default channel or
// channels.
func Channel(domain string, c ...ChannelConfig) integration.Option {
	const modName = "muc_defaults"
	return func(cmd *integration.Cmd) error {
		cfg := getConfig(cmd)
		comp := cfg.Component[domain]
		comp.MUCDefaults = append(comp.MUCDefaults, c...)
		comp.Modules = append(comp.Modules, modName)
		cfg.Component[domain] = comp
		cmd.Config = cfg
		return integration.TempFile("mod_"+modName+".lua", func(_ *integration.Cmd, w io.Writer) error {
			_, err := io.WriteString(w, `local log = module._log;
local params = module:get_option("default_mucs", {});
local jid_bare = require "util.jid".bare;


local function set_affiliations(room, affiliations)
	for affiliation, jids in pairs(affiliations) do
		for i, jid in pairs(jids) do
			module:log("debug", "Setting affiliation %s for jid %s", affiliation, jid);
			room:set_affiliation(true, jid_bare(jid), affiliation);
		end
	end
end


local function configure_room(room, config)
	local should_save = false;
	if config.name ~= nil then
		should_save = room:set_name(config.name) or should_save;
	end
	if config.description ~= nil then
		should_save = room:set_description(config.description) or should_save;
	end
	if config.allow_member_invites ~= nil then
		should_save =
			room:set_allow_member_invites(config.allow_member_invites)
			or should_save;
	end
	if config.change_subject ~= nil then
		should_save =
			room:set_changesubject(config.change_subject)
			or should_save;
	end
	if config.history_length ~= nil then
		should_save =
			room:set_historylength(config.history_length)
			or should_save;
	end
	if config.lang ~= nil then
		should_save = room:set_language(config.lang) or should_save;
	end
	if config.pass ~= nil then
		should_save = room:set_password(config.pass) or should_save;
	end
	if config.members_only ~= nil then
		should_save =
			room:set_members_only(config.members_only)
			or should_save;
	end
	if config.moderated ~= nil then
		should_save = room:set_moderated(config.moderated) or should_save;
	end
	if config.persistent ~= nil then
		should_save = room:set_persistent(config.persistent) or should_save;
	end
	if config.presence_broadcast ~= nil then
		should_save = room:set_presence_broadcast(config.presence_broadcast) or should_save;
	end
	if config.public ~= nil then
		should_save = room:set_hidden(not config.public) or should_save;
	end
	if config.public_jids ~= nil then
		should_save =
			room:set_whois(config.public_jids and "anyone" or "moderators")
			or should_save;
	end
	if config.logging ~= room._data.logging then
		room._data.logging = config.logging;
		should_save = true;
	end
	if should_save then
		room:save(true);
	end
end


local i, room_data;
for i, room_data in pairs(params) do
	local host = module.host;
	local room_jid = room_data.jid_node.."@"..host;
	local mod_muc = prosody.hosts[host].modules.muc;
	local room = mod_muc.get_room_from_jid(room_jid);
	if not room then
		module:log("debug", "Creating new room %s", room_jid);
		-- We don't pass in the config, so that the default config is set first.
		room = mod_muc.create_room(room_jid);
	else
		module:log("debug", "Configuring already existing room %s", room_jid);
	end
	configure_room(room, room_data.config);
	if room_data.affiliations then
		set_affiliations(room, room_data.affiliations);
	end
end`)
			return err
		})(cmd)
	}
}

// Component adds an component with the given domain and secret to the config
// file.
// If a name is provided the component must be a builtin.


@@ 197,9 312,10 @@ func Component(domain, secret, name string, modules ...string) integration.Optio
		}
		if cfg.Component == nil {
			cfg.Component = make(map[string]struct {
				Name    string
				Secret  string
				Modules []string
				Name        string
				Secret      string
				Modules     []string
				MUCDefaults []ChannelConfig
			})
		}
		comp := cfg.Component[domain]