# signature { license=0, seed=277031123, key="4c02a8e4256fa014cfc9ae0c779399b3" }
cgi.print([[]],cgi.head,[[
<title>InOAuth ]],i18n("Configuration"),[[</title>
]]);



local state = cfg.State or 0
local image = ( cfg.Authenticator or '../cancel' ) .. '.png'
 cgi.print([[
<script>
function dom( x ) { return document.getElementById( x ) }

function askRestore(filename)
{
	var txt = "]],i18n("You are about to upload the file {filename} as new InOAuth configuration\\nContinue ?"),[[";
	txt = txt.replace("{filename} ", '\n"'+ filename +'"\n');
	if (window.confirm(txt) )
		document.putcfg.submit();
	return false;
}

var cfg =]],cfg:JSON(),[[;

function GET( url )
{
	objHTTP = new XMLHttpRequest();
	objHTTP.open( "GET", url, false, "]],cfg.XmlProUserID,[[", "]],cfg:password('XmlPro'),[[" );
	objHTTP.send();
	return ""+ objHTTP.responseText;
}

/*
	finds the index of an option whose value matches the given one
*/
function findOption( e, value )
{
	for (var i=0; i<e.length; i++)
	{
		if (value == e.options[i].value)
			return i;
	}
	return -1;
}

function forConfig( f )
{
	for (var name in cfg)
	{
		var item = cfg[ name ];
		var e = document.getElementsByName( name );
		if (e && e[0]) f( e[0], item, name );
	}
}

/*
	fills the values in the <input>s and <select>s with the data from
	configuration
*/
function init()
{
	function setHtml( e, item )
	{
		switch (e.tagName)
		{
			case "INPUT": e.value = item; break;
			case "SELECT":e.selectedIndex = findOption( e, item )
		}
	}

	forConfig( setHtml );
}

function send()
{
	var s = '';

	function changeNote( e, item, name)
	{
		var v;
		switch (e.tagName)
		{
			case 'INPUT': v = e.value; break;
			case 'SELECT':v = e.selectedIndex;
				if (v<0) return;
				v = e.options[ v].value;
		};
		switch (typeof item)
		{
			case 'number':	s += name +'='+ (v-0) +";\n"; break;
			case 'string':	s += name +'="'+ v.replace(/\\/g,"\\\\").replace(/"/g,"\\\"") +'";\n';  break;
			case 'boolean':	s += name + (v ? '=true;\n' : '=false;\n'); break;
		}
	}

	forConfig( changeNote );

	document.xfer.change.value = s;
	document.xfer.submit();
}

// throws the selector switch and commands oauth.cgi to preset the settings
//
function preset()
{
	var e = document.forms.conf.Authenticator;
	var v = e.selectedIndex;
	if (v<0) return;
	v = e.options[ v ].value;
	window.location.href = 'oauth.cgi?cmd=' + v;
}

var MsgBox="]],MsgBox or '',[[";
function start()
{
	init()
	if (MsgBox && MsgBox > '') alert( MsgBox ) ;
}

</script>
</head>
<body onload="start()">]],cgi.h1,[[
<div class="body">
	<h2>]],i18n("Configuration"),[[
		]],cgi.button{ title=i18n("save changes in configuration"), href="javascript:send()", img="../ok.png", i18n("Ok")},[[
		]],cgi.button
		{
			title = i18n("Authorise InOAuth for the email account") ,
			href = cfg.AuthServerURL ..
			"?redirect_uri=" .. urlencode( cfg.RedirectURI ) ..
			"&prompt=consent&response_type=code&client_id=" .. urlencode( cfg.ClientId ) ..
			"&scope=" .. urlencode( cfg.Scopes ) ..
			"&state=" .. tostring( state ) ..
			"&access_type=offline" ,
			img = image ,
			target = "_blank" ,
			i18n("Authorise")
		},[[
	</h2>
	<h3>]],i18n("Files"),[[</h3>
	<div class="icons" >
		]],cgi.icon{ title=i18n("download a copy of the configuration file"), href="oauth.cgi?cmd=getcfg", img="backup.png", i18n("Backup") },[[

		<form name="putcfg" action="oauth.cgi?cmd=putcfg" method="post" enctype="multipart/form-data">
			<a class="icon"><img alt="" src="restore.png" />]],i18n("Restore"),[[
				<div class="upload">
					<input type="file" id="cfgfile" name="cfgfile" onchange="askRestore(this.value)"
						title="]],i18n("upload a (previously backed up) configuration file"),[[" />
				</div>
			</a>
		</form>

		]],cgi.icon{ title=i18n("read the InOAuth log file"), href="oauth.cgi?cmd=viewlog", img="viewlog.png", i18n("Logfile") },[[

		]],cgi.icon{ title=i18n("download a text file for localisation"), href="oauth.cgi?cmd=geti18n", img="langdn.png", i18n("Get Texts") },[[

		<form name="puti18n" action="oauth.cgi?cmd=puti18n" method="post" enctype="multipart/form-data">
			<a class="icon"><img alt="" src="langup.png" />]],i18n("Localise"),[[
				<div class="upload">
				<input type="file" id="i18nfile" name="i18nfile" onchange="setTimeout('document.forms.puti18n.submit()',10)"
					title="]],i18n("upload a localised text file"),[[" />
				</div>
			</a>
		</form>
	</div>
	<form id="conf">
	<h3>]],i18n("Authentication Server Settings"),[[</h3>
	<ul class="cfg">
		<li><select id="Authenticator" name="Authenticator" onchange="preset()">
				<option value="gmail">]],i18n("Google Gmail"),[[</option>
				<option value="o365" >]],i18n("Microsoft Exchange Online / Outlook.com"),[[</option>
			</select>
			<label for="Authenticator">]],i18n("Authentication Provider"),[[</label>
		</li>
		<li><input name="ActivationCode" type="text"     size="30" maxlength="2000" value="" /> ]],i18n("URL Address from Browser"),[[</li>
		<li><input name="TokenServerURL" type="text"     size="30" maxlength="100"  value="" /> ]],i18n("Token Server URL"),[[</li>
		<li><input name="AuthServerURL"  type="text"     size="30" maxlength="100"  value="" /> ]],i18n("Authentication Server URL"),[[</li>
		<li><input name="Pop3Scope"      type="text"     size="30" maxlength="80"   value="" /> ]],i18n("OAuth Scope for POP3 Access"),[[</li>
		<li><input name="SmtpScope"      type="text"     size="30" maxlength="80"   value="" /> ]],i18n("OAuth Scope for SMTP Access"),[[</li>
		<li><input name="RedirectURI"    type="text"     size="30" maxlength="80"   value="" /> ]],i18n("Target URL for OAuth Redirection"),[[</li>
		<li><input name="ClientId"       type="text"     size="30" maxlength="80"   value="" /> ]],i18n("Client ID"),[[</li>
		<li><input name="ClientPassword" type="text"     size="30" maxlength="80"   value="" /> ]],i18n("Client Secret"),[[</li>
	</ul>

	<h3>]],i18n("Email Sending (SMTP) Settings"),[[</h3>
	<ul class="cfg">
		<li><input name="SmtpClientPort" type="text"     size="30" maxlength="5" value="" /> ]],i18n("Local TCP Port for SMTP"),[[</li>
		<li><input name="SmtpServer"     type="text"     size="30" maxlength="80" value="" /> ]],i18n("Name or IP address of the SMTP Server"),[[</li>
		<li><input name="SmtpServerPort" type="text"     size="30" maxlength="5" value="" /> ]],i18n("Remote TCP Port for SMTP"),[[</li>
		<li><select name="SmtpSecurity" size="1" maxlength="50">
				<option value="none">]],i18n("none"),[[</option>
				<option value="starttls">]],i18n("StartTLS"),[[</option>
				<option value="tls">]],i18n("SSL/TLS"),[[</option>
			</select>]],i18n("transport encryption for SMTP"),[[
		</li>
	</ul>

	<h3>]],i18n("Email Receiving (POP3) Settings"),[[</h3>
	<ul class="cfg">
		<li><input name="Pop3ClientPort" type="text"     size="30" maxlength="5" value="" /> ]],i18n("Local TCP Port for POP3"),[[</li>
		<li><input name="Pop3Server"     type="text"     size="30" maxlength="80" value="" /> ]],i18n("Name or IP address of the POP3 Server"),[[</li>
		<li><input name="Pop3ServerPort" type="text"     size="30" maxlength="5" value="" /> ]],i18n("Remote TCP Port for POP3"),[[</li>

	</ul>
	</form>
</div>
<form name="xfer" action="oauth.cgi?cmd=cfghome&state=]],state,[[" method="post">
	<input type="hidden" name="change" value="" />
</form>
</body>
</html>
]])
