#!/usr/bin/env lua5.3
-- * Metronome IM *
--
-- This file is part of the Metronome XMPP server and is released under the
-- ISC License, please see the LICENSE file in this source package for more
-- information about copyright and licensing.

-- This script sends mail from the Register API
	
request_type = arg[1];
mail_from = arg[2];
mail_to = arg[3];
reply_to = arg[4];
new_jid = arg[5];
url = arg[6];
token = arg[7];
secure = arg[8];

if secure == "" then secure = nil; end

CFG_SOURCEDIR='/usr/lib/metronome';
CFG_CONFIGDIR='/etc/metronome';

metronome = { events = {}, platform = "posix", serialization = "internal" };
package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
config = require "core.configmanager";
html_escape = require "util.auxiliary".html_escape;
section = require "util.jid".section;
send = require "util.mail".send;

function read_file(filepath)
	local f, err = io.open(filepath, "r");
	if not f then return f, err; end
	local t = f:read("*all");
	f:close()
	return t;
end

config.load(CFG_CONFIGDIR .. "/metronome.cfg.lua"); -- FIXME should be format/file agnostical

local template = read_file(CFG_CONFIGDIR .. "/templates/" .. request_type .. ".template.txt");
if template then
	if request_type == "associate" then template = template:gsub("%%JID%%", new_jid); end

	template = template:gsub("%%URL%%", url):gsub("%%TOKEN%%", token);
	template = html_escape(template);

	local subject =
		(request_type == "register" and "Account registration verification for: ") or
		(request_type == "associate" and "Account mail change verification for: ") or
		"Account password reset for: ";

	local user, pass, host, port =
		config.get("*", "mail_user"), config.get("*", "mail_pass"),
		config.get("*", "mail_host"), config.get("*", "mail_port");

	send(mail_from, mail_to, reply_to, subject..new_jid, template,
		{ user = user, password = pass, host = host, port = port, helo = section(new_jid, "host") }, secure and true);
end
	
os.exit(1);
