2018-05-07 16:04:23 +02:00
|
|
|
|
2022-06-01 17:47:18 +02:00
|
|
|
// $Id: imapsync_form.js,v 1.27 2022/01/30 20:57:46 gilles Exp gilles $
|
2018-05-07 16:04:23 +02:00
|
|
|
|
|
|
|
/*jslint browser: true*/ /*global $*/
|
2019-07-03 01:25:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
$(document).ready(
|
2020-04-11 01:15:57 +02:00
|
|
|
function ()
|
|
|
|
{
|
|
|
|
"use strict";
|
|
|
|
// Bootstrap popover and tooltip
|
|
|
|
$("[data-toggle='tooltip']").tooltip();
|
|
|
|
|
|
|
|
var readyStateStr = {
|
|
|
|
"0": "Request not initialized",
|
|
|
|
"1": "Server connection established",
|
|
|
|
"2": "Response headers received",
|
|
|
|
"3": "Processing request",
|
|
|
|
"4": "Finished and response is ready"
|
|
|
|
} ;
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var refresh_interval_ms = 6000 ;
|
2020-04-11 01:15:57 +02:00
|
|
|
var refresh_interval_s = refresh_interval_ms / 1000 ;
|
|
|
|
var test = {
|
|
|
|
counter_all : 0 ,
|
|
|
|
counter_ok : 0 ,
|
|
|
|
counter_nok : 0 ,
|
|
|
|
failed_tests : ""
|
|
|
|
} ;
|
|
|
|
|
|
|
|
var is = function is( expected, given, comment )
|
|
|
|
{
|
|
|
|
test.counter_all += 1 ;
|
|
|
|
var message = test.counter_all + " - ["
|
|
|
|
+ expected
|
|
|
|
+ "] === ["
|
|
|
|
+ given
|
|
|
|
+ "] "
|
|
|
|
+ comment
|
|
|
|
+ "\n" ;
|
|
|
|
if ( expected === given )
|
|
|
|
{
|
|
|
|
test.counter_ok += 1 ;
|
|
|
|
message = "ok " + message ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
else
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2020-04-11 01:15:57 +02:00
|
|
|
test.counter_nok += 1 ;
|
|
|
|
test.failed_tests += "nb " + message + "\n" ;
|
|
|
|
message = "not ok " + message ;
|
|
|
|
}
|
|
|
|
$("#tests").append( message ) ;
|
|
|
|
} ;
|
2021-08-04 21:14:36 +02:00
|
|
|
|
|
|
|
var note = function note( message )
|
|
|
|
{
|
|
|
|
$("#tests").append( message ) ;
|
2022-02-15 19:29:45 +01:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
var tests_last_x_lines = function tests_last_x_lines()
|
|
|
|
{
|
|
|
|
is( "", last_x_lines(), "last_x_lines: no args => empty string" ) ;
|
|
|
|
is( "", last_x_lines(""), "last_x_lines: empty string => empty string" ) ;
|
|
|
|
is( "abc", last_x_lines("abc"), "last_x_lines: abc => abc" ) ;
|
|
|
|
is( "abc\ndef", last_x_lines("abc\ndef"), "last_x_lines: abc\ndef => abc\ndef" ) ;
|
|
|
|
is( "def", last_x_lines("abc\ndef", -1), "last_x_lines: abc\ndef -1 => def\n" ) ;
|
|
|
|
is( "", last_x_lines("abc\ndef", 0), "last_x_lines: abc\ndef 0 => empty string" ) ;
|
|
|
|
is( "abc\ndef", last_x_lines("abc\ndef", -10), "last_x_lines: last 10 of 2 lines => 2 lines" ) ;
|
|
|
|
is( "4\n5\n", last_x_lines("1\n2\n3\n4\n5\n", -3), "last_x_lines: last 3 lines of 5 lines" ) ;
|
|
|
|
is( "3\n4\n5", last_x_lines("1\n2\n3\n4\n5", -3), "last_x_lines: last 3 lines of 5 lines" ) ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
var last_x_lines = function last_x_lines( string, num )
|
|
|
|
{
|
|
|
|
if ( undefined === string || 0 === num )
|
|
|
|
{
|
|
|
|
return "" ;
|
|
|
|
}
|
|
|
|
return string.split(/\r?\n/).slice(num).join("\n") ;
|
|
|
|
} ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var last_eta = function last_eta( string )
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
// return the last occurrence of the substring "ETA: ...\n"
|
|
|
|
// or "ETA: unknown" or ""
|
|
|
|
var eta ;
|
|
|
|
var last_found ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
if ( undefined === string )
|
|
|
|
{
|
|
|
|
return "" ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
var eta_re = /ETA:.*\n/g ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
eta = string.match( eta_re ) ;
|
|
|
|
if ( eta )
|
|
|
|
{
|
|
|
|
last_found = eta[eta.length -1 ] ;
|
|
|
|
return last_found ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
else
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2020-04-11 01:15:57 +02:00
|
|
|
return "ETA: unknown" ;
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 19:29:45 +01:00
|
|
|
|
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var tests_last_eta = function tests_last_eta()
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
is( "", last_eta( ), "last_eta: no args => empty string" ) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"ETA: unknown",
|
|
|
|
last_eta( "" ),
|
|
|
|
"last_eta: empty => empty string" ) ;
|
|
|
|
|
|
|
|
is( "ETA: unknown",
|
|
|
|
last_eta( "ETA" ),
|
|
|
|
"last_eta: ETA => empty string" ) ;
|
|
|
|
|
|
|
|
is( "ETA: unknown", last_eta( "ETA: but no CR" ),
|
|
|
|
"last_eta: ETA: but no CR => empty string" ) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"ETA: with CR\n",
|
|
|
|
last_eta( "Blabla ETA: with CR\n" ),
|
|
|
|
"last_eta: ETA: with CR => ETA: with CR"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"ETA: 2 with CR\n",
|
|
|
|
last_eta( "Blabla ETA: 1 with CR\nBlabla ETA: 2 with CR\n" ),
|
|
|
|
"last_eta: several ETA: with CR => ETA: 2 with CR"
|
|
|
|
) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests_decompose_eta_line = function tests_decompose_eta_line()
|
|
|
|
{
|
|
|
|
var eta_obj ;
|
|
|
|
var eta_str = "ETA: Wed Jul 3 14:55:27 2019 1234 s 123/4567 msgs left\n" ;
|
|
|
|
|
|
|
|
eta_obj = decompose_eta_line( "" ) ;
|
|
|
|
is(
|
|
|
|
"",
|
|
|
|
eta_obj.str,
|
|
|
|
"decompose_eta_line: no match => undefined"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
|
|
|
|
eta_obj = decompose_eta_line( eta_str ) ;
|
|
|
|
is(
|
|
|
|
eta_str,
|
|
|
|
eta_str,
|
|
|
|
"decompose_eta_line: str is str"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
eta_str,
|
|
|
|
eta_obj.str,
|
|
|
|
"decompose_eta_line: str back"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"Wed Jul 3 14:55:27 2019",
|
|
|
|
eta_obj.date,
|
|
|
|
"decompose_eta_line: date"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"1234",
|
|
|
|
eta_obj.seconds_left,
|
|
|
|
"decompose_eta_line: seconds_left"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"123",
|
|
|
|
eta_obj.msgs_left,
|
|
|
|
"decompose_eta_line: msgs_left"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"4567",
|
|
|
|
eta_obj.msgs_total,
|
|
|
|
"decompose_eta_line: msgs_total"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"4444",
|
|
|
|
eta_obj.msgs_done(),
|
|
|
|
"decompose_eta_line: msgs_done"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"97.31",
|
|
|
|
eta_obj.percent_done(),
|
|
|
|
"decompose_eta_line: percent_done"
|
|
|
|
) ;
|
|
|
|
|
|
|
|
is(
|
|
|
|
"2.69",
|
|
|
|
eta_obj.percent_left(),
|
|
|
|
"decompose_eta_line: percent_left"
|
|
|
|
) ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
var decompose_eta_line = function decompose_eta_line( eta_str )
|
|
|
|
{
|
|
|
|
var eta_obj ;
|
|
|
|
var eta_array ;
|
|
|
|
|
|
|
|
var regex_eta = /^ETA:\s+(.*?)\s+([0-9]+)\s+s\s+([0-9]+)\/([0-9]+)\s+msgs\s+left\n?$/ ;
|
|
|
|
eta_array = regex_eta.exec( eta_str ) ;
|
|
|
|
|
|
|
|
if ( null !== eta_array )
|
|
|
|
{
|
|
|
|
eta_obj = {
|
|
|
|
str : eta_str,
|
|
|
|
date : eta_array[1],
|
|
|
|
seconds_left : eta_array[2],
|
|
|
|
msgs_left : eta_array[3],
|
|
|
|
msgs_total : eta_array[4],
|
|
|
|
msgs_done : function() {
|
|
|
|
var diff = eta_obj.msgs_total - eta_obj.msgs_left ;
|
|
|
|
return( diff.toString() ) ;
|
|
|
|
},
|
|
|
|
percent_done : function() {
|
|
|
|
var percent ;
|
|
|
|
if ( 0 === eta_obj.msgs_total )
|
|
|
|
{
|
|
|
|
return "0" ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
percent = ( eta_obj.msgs_total - eta_obj.msgs_left ) / eta_obj.msgs_total * 100 ;
|
|
|
|
return( percent.toFixed(2) ) ;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
percent_left : function() {
|
|
|
|
var percent ;
|
|
|
|
if ( 0 === eta_obj.msgs_total )
|
|
|
|
{
|
|
|
|
return "0" ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
percent = ( eta_obj.msgs_left / eta_obj.msgs_total * 100 ) ;
|
|
|
|
return( percent.toFixed(2) ) ;
|
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
} ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eta_obj = {
|
|
|
|
str : "",
|
|
|
|
date : "?",
|
|
|
|
seconds_left : "?",
|
|
|
|
msgs_left : "?",
|
|
|
|
msgs_total : "?",
|
|
|
|
msgs_done : "?",
|
|
|
|
percent_done : function() { return "" ; },
|
|
|
|
percent_left : function() { return "" ; }
|
|
|
|
} ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
return eta_obj ;
|
|
|
|
} ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
var extract_eta = function extract_eta( xhr )
|
|
|
|
{
|
|
|
|
var eta_obj ;
|
|
|
|
var slice_length ;
|
|
|
|
var slice_log ;
|
|
|
|
var eta_str ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
if ( xhr.readyState === 4 )
|
|
|
|
{
|
|
|
|
slice_length = -24000 ;
|
|
|
|
}
|
|
|
|
else
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2022-02-15 19:29:45 +01:00
|
|
|
slice_length = -2400 ;
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
|
|
|
slice_log = xhr.responseText.slice( slice_length ) ;
|
|
|
|
eta_str = last_eta( slice_log ) ;
|
|
|
|
// $("#tests").append( "extract_eta eta_str: " + eta_str + "\n" ) ;
|
|
|
|
eta_obj = decompose_eta_line( eta_str ) ;
|
|
|
|
return eta_obj ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
var progress_bar_update = function progress_bar_update( eta_obj )
|
|
|
|
{
|
|
|
|
if ( eta_obj.str.length )
|
|
|
|
{
|
|
|
|
$("#progress-bar-done").css( "width", eta_obj.percent_done() + "%" ).attr( "aria-valuenow", eta_obj.percent_done() ) ;
|
|
|
|
$("#progress-bar-left").css( "width", eta_obj.percent_left() + "%" ).attr( "aria-valuenow", eta_obj.percent_left() ) ;
|
|
|
|
$("#progress-bar-done").text( eta_obj.percent_done() + "% " + "done" ) ;
|
|
|
|
$("#progress-bar-left").text( eta_obj.percent_left() + "% " + "left" ) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$("#progress-bar-done").text( "unknown % " + "done" ) ;
|
|
|
|
$("#progress-bar-left").text( "unknown % " + "left" ) ;
|
|
|
|
}
|
|
|
|
return ;
|
|
|
|
} ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var refreshLog = function refreshLog( xhr )
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
var eta_obj ;
|
|
|
|
var eta_str ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
$( "#imapsync_current" ).load( "imapsync_current.txt" ) ;
|
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
eta_obj = extract_eta( xhr ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
progress_bar_update( eta_obj ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
if ( xhr.readyState === 4 )
|
|
|
|
{
|
|
|
|
// end of sync
|
2022-02-15 19:29:45 +01:00
|
|
|
$("#progress-txt").text(
|
2020-04-11 01:15:57 +02:00
|
|
|
"Ended. It remains "
|
|
|
|
+ eta_obj.msgs_left + " messages to be synced" ) ;
|
2022-02-15 19:29:45 +01:00
|
|
|
|
|
|
|
$( "#output" ).text( xhr.responseText ) ;
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-15 19:29:45 +01:00
|
|
|
eta_str = eta_obj.str + " (refresh every " + refresh_interval_s + " s)" ;
|
|
|
|
eta_str = eta_str.replace(/(\r\n|\n|\r)/gm, "") ; // trim newlines
|
|
|
|
//$("#tests").append( "refreshLog eta_str: " + eta_str + "\n" ) ;
|
|
|
|
$( "#progress-txt" ).text( eta_str ) ;
|
|
|
|
var last_lines = last_x_lines( xhr.responseText.slice(-2000), -10)
|
|
|
|
$( "#output" ).text( last_lines ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var handleRun = function handleRun(xhr, timerRefreshLog)
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$("#console").text(
|
|
|
|
"Status: " + xhr.status + " " + xhr.statusText + "\n"
|
|
|
|
+ "State: " + readyStateStr[xhr.readyState] + "\n" ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
if ( xhr.readyState === 4 ) {
|
|
|
|
// var headers = xhr.getAllResponseHeaders();
|
|
|
|
// $("#console").append(headers);
|
|
|
|
// $("#console").append("See the completed log\n");
|
|
|
|
clearInterval( timerRefreshLog ) ;
|
|
|
|
refreshLog( xhr ) ; // a last time
|
|
|
|
// back to enable state for next run
|
|
|
|
$("#bt-sync").prop("disabled", false) ;
|
2021-08-04 21:14:36 +02:00
|
|
|
|
|
|
|
$( "#imapsync_current" ).load( "imapsync_current.txt" ) ;
|
|
|
|
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var imapsync = function imapsync()
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
var querystring = $("#form").serialize() ;
|
|
|
|
$("#abort").text("\n\n") ; // clean abort console
|
|
|
|
$("#output").text("Here comes the log!\n\n") ;
|
|
|
|
|
|
|
|
if ( "imap.gmail.com" === $("#host1").val() )
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2020-04-11 01:15:57 +02:00
|
|
|
querystring = querystring + "&gmail1=on" ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
if ( "imap.gmail.com" === $("#host2").val() )
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2020-04-11 01:15:57 +02:00
|
|
|
querystring = querystring + "&gmail2=on" ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
// Same for "outlook.office365.com"
|
|
|
|
if ( "outlook.office365.com" === $("#host1").val() )
|
|
|
|
{
|
|
|
|
querystring = querystring + "&office1=on" ;
|
|
|
|
}
|
|
|
|
if ( "outlook.office365.com" === $("#host2").val() )
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2020-04-11 01:15:57 +02:00
|
|
|
querystring = querystring + "&office2=on" ;
|
|
|
|
}
|
2021-08-04 21:14:36 +02:00
|
|
|
|
|
|
|
// querystring = querystring + "&tmphash=" + tmphash( ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
var xhr ;
|
|
|
|
xhr = new XMLHttpRequest() ;
|
|
|
|
var timerRefreshLog = setInterval(
|
|
|
|
function ()
|
|
|
|
{
|
|
|
|
refreshLog( xhr ) ;
|
|
|
|
}, refresh_interval_ms ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
xhr.onreadystatechange = function ()
|
|
|
|
{
|
|
|
|
handleRun( xhr, timerRefreshLog ) ;
|
|
|
|
} ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
xhr.open( "POST", "/cgi-bin/imapsync", true ) ;
|
|
|
|
xhr.setRequestHeader( "Content-type",
|
|
|
|
"application/x-www-form-urlencoded" ) ;
|
|
|
|
xhr.send( querystring ) ;
|
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var handleAbort = function handleAbort( xhr )
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
$( "#abort" ).text(
|
|
|
|
"Status: " + xhr.status + " " + xhr.statusText + "\n"
|
|
|
|
+ "State: " + readyStateStr[xhr.readyState] + "\n\n" ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
if ( xhr.readyState === 4 )
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2020-04-11 01:15:57 +02:00
|
|
|
$("#abort").append(xhr.responseText);
|
|
|
|
$("#bt-sync").prop("disabled", false);
|
|
|
|
$("#bt-abort").prop("disabled", false); // back for next abort
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var abort = function abort()
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
var querystring = $("#form").serialize() + "&abort=on";
|
|
|
|
var xhr;
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = function ()
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2020-04-11 01:15:57 +02:00
|
|
|
handleAbort(xhr);
|
|
|
|
};
|
|
|
|
xhr.open("POST", "/cgi-bin/imapsync", true);
|
|
|
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
|
|
|
xhr.send(querystring);
|
|
|
|
}
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var store = function store( id )
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
var stored ;
|
|
|
|
//$( "#tests" ).append( "Eco: " + id + " type is " + $( id ).attr( "type" ) + "\n" ) ;
|
|
|
|
if ( "text" === $( id ).attr( "type" ) || "password" === $( id ).attr( "type" ) )
|
|
|
|
{
|
|
|
|
localStorage.setItem( id, $(id).val() ) ;
|
|
|
|
stored = $( id ).val() ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
else if ( "checkbox" === $( id ).attr( "type" ) )
|
|
|
|
{
|
|
|
|
//$( "#tests" ).append( "Eco: " + id + " checked is " + $( id )[0].checked + "\n" ) ;
|
|
|
|
localStorage.setItem( id, $( id )[0].checked ) ;
|
|
|
|
stored = $( id )[0].checked ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
return stored ;
|
|
|
|
}
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var retrieve = function retrieve( id )
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
var retrieved ;
|
|
|
|
//$( "#tests" ).append( "Eco: " + id + " type is " + $( id ).attr( "type" ) + " length is " + $( id ).length + "\n" ) ;
|
|
|
|
if ( "text" === $( id ).attr( "type" ) || "password" === $( id ).attr( "type" ) )
|
2019-07-03 01:25:47 +02:00
|
|
|
{
|
2020-04-11 01:15:57 +02:00
|
|
|
$( id ).val( localStorage.getItem( id ) ) ;
|
|
|
|
retrieved = $( id ).val() ;
|
2019-07-03 01:25:47 +02:00
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
else if ( "checkbox" === $( id ).attr( "type" ) )
|
|
|
|
{
|
|
|
|
//$( "#tests" ).append( "Eco: " + id + " getItem is " + localStorage.getItem( id ) + "\n" ) ;
|
|
|
|
$( id )[0].checked = JSON.parse( localStorage.getItem( id ) ) ;
|
|
|
|
retrieved = $( id )[0].checked ;
|
|
|
|
}
|
|
|
|
return retrieved ;
|
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var tests_store_retrieve = function tests_store_retrieve()
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
if ( $("#tests").length !== 0 )
|
|
|
|
{
|
|
|
|
is( 1, 1, "one equals one" ) ;
|
|
|
|
// isnot( 0, 1, "zero differs one" ) ;
|
|
|
|
|
|
|
|
// no exist
|
|
|
|
is( undefined, store( "#test_noexists" ),
|
|
|
|
"store: #test_noexists" ) ;
|
|
|
|
is( undefined, retrieve( "#test_noexists" ),
|
|
|
|
"retrieve: #test_noexists" ) ;
|
|
|
|
is( undefined, retrieve( "#test_noexists2" ),
|
|
|
|
"retrieve: #test_noexists2" ) ;
|
|
|
|
|
|
|
|
// input text
|
|
|
|
$("#test_text" ).val( "foo" ) ;
|
|
|
|
is( "foo", $("#test_text" ).val( ), "#test_text val = foo" ) ;
|
|
|
|
is( "foo", store( "#test_text" ), "store: #test_text" ) ;
|
|
|
|
$("#test_text" ).val( "bar" ) ;
|
|
|
|
is( "bar", $("#test_text" ).val( ), "#test_text val = bar" ) ;
|
|
|
|
is( "foo", retrieve( "#test_text" ), "retrieve: #test_text = foo" ) ;
|
|
|
|
is( "foo", $("#test_text" ).val( ), "#test_text val = foo" ) ;
|
|
|
|
|
|
|
|
|
|
|
|
// input check button
|
|
|
|
$( "#test_checkbox" ).prop( "checked", true );
|
|
|
|
is( true, store( "#test_checkbox" ), "store: #test_checkbox checked" ) ;
|
|
|
|
|
|
|
|
$( "#test_checkbox" ).prop( "checked", false );
|
|
|
|
is( true, retrieve( "#test_checkbox" ), "retrieve: #test_checkbox = true" ) ;
|
|
|
|
|
|
|
|
$( "#test_checkbox" ).prop( "checked", false );
|
|
|
|
is( false, store( "#test_checkbox" ), "store: #test_checkbox not checked" ) ;
|
|
|
|
$( "#test_checkbox" ).prop( "checked", true );
|
|
|
|
is( false, retrieve( "#test_checkbox" ), "retrieve: #test_checkbox = false" ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var store_form = function store_form()
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
if ( Storage !== "undefined")
|
|
|
|
{
|
|
|
|
// Code for localStorage.
|
|
|
|
store("#user1") ;
|
|
|
|
store("#password1") ;
|
|
|
|
store("#host1") ;
|
|
|
|
store("#subfolder1") ;
|
|
|
|
store("#showpassword1") ;
|
|
|
|
|
|
|
|
store("#user2") ;
|
|
|
|
store("#password2") ;
|
|
|
|
store("#host2") ;
|
|
|
|
store("#subfolder2") ;
|
|
|
|
store("#showpassword2") ;
|
|
|
|
|
|
|
|
store("#dry") ;
|
|
|
|
store("#justlogin") ;
|
|
|
|
store("#justfolders") ;
|
|
|
|
store("#justfoldersizes") ;
|
|
|
|
|
|
|
|
localStorage.account1_background_color = $("#account1").css("background-color") ;
|
|
|
|
localStorage.account2_background_color = $("#account2").css("background-color") ;
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var show_extra_if_needed = function show_extra_if_needed()
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
if ( $("#subfolder1").length && $("#subfolder1").val().length > 0 )
|
|
|
|
{
|
|
|
|
$(".extra_param").show() ;
|
|
|
|
}
|
|
|
|
if ( $("#subfolder2").length && $("#subfolder2").val().length > 0 )
|
|
|
|
{
|
|
|
|
$(".extra_param").show() ;
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var retrieve_form = function retrieve_form()
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
if ( Storage !== "undefined" )
|
|
|
|
{
|
|
|
|
// Code for localStorage.
|
|
|
|
retrieve( "#user1" ) ;
|
|
|
|
retrieve( "#password1" ) ;
|
|
|
|
// retrieve("#showpassword1") ;
|
|
|
|
retrieve( "#host1" ) ;
|
|
|
|
retrieve( "#subfolder1" ) ;
|
|
|
|
|
|
|
|
retrieve( "#user2" ) ;
|
|
|
|
retrieve( "#password2" ) ;
|
|
|
|
// retrieve("#showpassword2") ;
|
|
|
|
retrieve( "#host2" ) ;
|
|
|
|
retrieve( "#subfolder2" ) ;
|
|
|
|
|
|
|
|
retrieve( "#dry" ) ;
|
|
|
|
retrieve( "#justlogin" ) ;
|
|
|
|
retrieve( "#justfolders" ) ;
|
|
|
|
retrieve( "#justfoldersizes" ) ;
|
|
|
|
|
|
|
|
// In case, how to restore the original color from css file.
|
|
|
|
// localStorage.removeItem( "account1_background_color" ) ;
|
|
|
|
// localStorage.removeItem( "account2_background_color" ) ;
|
|
|
|
|
|
|
|
if ( localStorage.account1_background_color )
|
|
|
|
{
|
|
|
|
$("#account1").css("background-color",
|
|
|
|
localStorage.account1_background_color ) ;
|
|
|
|
}
|
|
|
|
if ( localStorage.account2_background_color )
|
|
|
|
{
|
|
|
|
$("#account2").css("background-color",
|
|
|
|
localStorage.account2_background_color ) ;
|
|
|
|
}
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
// Show the extra parameters if they are not empty because it would
|
|
|
|
// be dangerous to retrieve them without showing them
|
2020-04-11 01:15:57 +02:00
|
|
|
show_extra_if_needed() ;
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var showpassword = function showpassword( id, button )
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
var x = document.getElementById( id );
|
|
|
|
if ( button.checked )
|
|
|
|
{
|
|
|
|
x.type = "text";
|
|
|
|
} else {
|
|
|
|
x.type = "password";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
var tests_cryptojs = function tests_cryptojs()
|
|
|
|
{
|
|
|
|
if ( $("#tests").length !== 0 )
|
|
|
|
{
|
|
|
|
if (typeof CryptoJS === 'undefined')
|
|
|
|
{
|
|
|
|
is( true, typeof CryptoJS !== 'undefined', "CryptoJS is available" ) ;
|
|
|
|
note( "CryptoJS is not available on this site. Ask the admin to fix this.\n" ) ;
|
|
|
|
}
|
|
|
|
else if (typeof CryptoJS.SHA256 !== "function")
|
|
|
|
{
|
|
|
|
is( "function", typeof CryptoJS.SHA256, "CryptoJS.SHA256 is a function" ) ;
|
|
|
|
note( "CryptoJS.SHA256 function is not available on this site. Ask the admin to fix this.\n" ) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// safe to use the function
|
|
|
|
is( "function", typeof CryptoJS.SHA256, "CryptoJS.SHA256 is a function" ) ;
|
|
|
|
is( "2f77668a9dfbf8d5848b9eeb4a7145ca94c6ed9236e4a773f6dcafa5132b2f91", sha256("Message"), "sha256 Message" ) ;
|
|
|
|
is( "26429a356b1d25b7d57c0f9a6d5fed8a290cb42374185887dcd2874548df0779", sha256("caca"), "sha256 caca" ) ;
|
|
|
|
is( "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", sha256(""), "sha256 ''" ) ;
|
|
|
|
is( tmphash(), tmphash(), "tmphash" ) ;
|
|
|
|
|
|
|
|
$("#user1").val("test1") ;
|
|
|
|
$("#password1").val("secret1") ;
|
|
|
|
$("#host1").val("test1.lamiral.info") ;
|
|
|
|
$("#user2").val("test2") ;
|
|
|
|
$("#password2").val("secret2") ;
|
|
|
|
$("#host2").val("test2.lamiral.info") ;
|
|
|
|
is( "20d2b4917cf69114876b4c8779af543e89c5871c6ada68107619722e55af1101", tmphash(), "tmphash like testslive" ) ;
|
|
|
|
$("#user1").val("") ;
|
|
|
|
$("#password1").val("") ;
|
|
|
|
$("#host1").val("") ;
|
|
|
|
$("#user2").val("") ;
|
|
|
|
$("#password2").val("") ;
|
|
|
|
$("#host2").val("") ;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sha256 = function sha256( string )
|
|
|
|
{
|
|
|
|
var hash = CryptoJS.SHA256( string ) ;
|
|
|
|
var hash_hex = hash.toString( CryptoJS.enc.Hex ) ;
|
|
|
|
return( hash_hex ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmphash = function tmphash()
|
|
|
|
{
|
|
|
|
var string = "" ;
|
|
|
|
string = string.concat(
|
|
|
|
$("#user1").val(), $("#password1").val(), $("#host1").val(),
|
|
|
|
$("#user2").val(), $("#password2").val(), $("#host2").val(),
|
|
|
|
)
|
|
|
|
return( sha256( string ) ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
var init = function init()
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
// in case of a manual refresh, start with
|
|
|
|
$("#bt-sync").prop("disabled", false);
|
|
|
|
$("#bt-abort").prop("disabled", false);
|
|
|
|
$("#progress-bar-left").css( "width", 100 + "%" ).attr( "aria-valuenow", 100 ) ;
|
|
|
|
|
|
|
|
$("#showpassword1").click(
|
|
|
|
function ( event )
|
|
|
|
{
|
|
|
|
var button = event.target ;
|
|
|
|
showpassword( "password1", button ) ;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$("#showpassword2").click(
|
|
|
|
function ( event )
|
|
|
|
{
|
|
|
|
//$("#tests").append( "\nthat1=" + JSON.stringify( event.target, undefined, 4 ) ) ;
|
|
|
|
var button = event.target ;
|
|
|
|
showpassword( "password2", button ) ;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$("#bt-sync").click(
|
|
|
|
function ()
|
|
|
|
{
|
2021-08-04 21:14:36 +02:00
|
|
|
$( "#imapsync_current" ).load( "imapsync_current.txt" ) ;
|
2020-04-11 01:15:57 +02:00
|
|
|
$("#bt-sync").prop("disabled", true) ;
|
|
|
|
$("#bt-abort").prop("disabled", false) ;
|
|
|
|
$("#progress-txt").text( "ETA: coming soon" ) ;
|
|
|
|
store_form() ;
|
|
|
|
imapsync() ;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$("#bt-abort").click(
|
|
|
|
function ()
|
|
|
|
{
|
2021-08-04 21:14:36 +02:00
|
|
|
$( "#imapsync_current" ).load( "imapsync_current.txt" ) ;
|
2020-04-11 01:15:57 +02:00
|
|
|
$("#bt-sync").prop("disabled", true);
|
|
|
|
$("#bt-abort").prop("disabled", true);
|
|
|
|
abort();
|
2021-08-04 21:14:36 +02:00
|
|
|
$( "#imapsync_current" ).load( "imapsync_current.txt" ) ;
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
var swap = function swap( p1, p2 )
|
|
|
|
{
|
|
|
|
var temp = $( p2 ).val( ) ;
|
|
|
|
$( p2 ).val( $( p1 ).val( ) ) ;
|
|
|
|
$( p1 ).val( temp ) ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
$("#swap").click(
|
|
|
|
function()
|
|
|
|
{
|
|
|
|
// swaping colors can't use swap()
|
|
|
|
var temp1 = $("#account1").css("background-color") ;
|
|
|
|
var temp2 = $("#account2").css("background-color") ;
|
|
|
|
$("#account1").css("background-color", temp2 );
|
|
|
|
$("#account2").css("background-color", temp1 );
|
|
|
|
|
|
|
|
swap( $("#user1"), $("#user2") ) ;
|
|
|
|
swap( $("#password1"), $("#password2") ) ;
|
|
|
|
swap( $("#host1"), $("#host2") ) ;
|
|
|
|
swap( $("#subfolder1"), $("#subfolder2") ) ;
|
|
|
|
|
|
|
|
var temp = $("#showpassword1")[0].checked ;
|
|
|
|
$("#showpassword1")[0].checked = $("#showpassword2")[0].checked ;
|
|
|
|
$("#showpassword2")[0].checked = temp ;
|
|
|
|
showpassword( "password1", $("#showpassword1")[0] ) ;
|
|
|
|
showpassword( "password2", $("#showpassword2")[0] ) ;
|
|
|
|
}
|
|
|
|
) ;
|
2021-08-04 21:14:36 +02:00
|
|
|
|
|
|
|
if ( "imapsync.lamiral.info" === location.hostname )
|
|
|
|
{
|
2022-06-01 17:47:18 +02:00
|
|
|
// $( "#status_24h" ).attr('src', 'https://lstu.fr/imapsync_online_status_24h_1200x70') ;
|
|
|
|
// $( "#status_7d" ).attr('src', 'https://lstu.fr/imapsync_online_status_7d') ;
|
|
|
|
// $( "#status_2m" ).attr('src', 'https://lstu.fr/imapsync_online_status_2months_1200x70') ;
|
2021-08-04 21:14:36 +02:00
|
|
|
$( "#local_bandwidth" ).collapse( "show" ) ;
|
|
|
|
$( "#local_status_dbmon" ).collapse( "show" ) ;
|
|
|
|
$( "#local_status_hetrix" ).collapse( "show" ) ;
|
|
|
|
$( "#imapsync_advice_hours" ).collapse( "show" ) ;
|
|
|
|
$( "#imapsync_current" ).load( "imapsync_current.txt" ) ;
|
|
|
|
}
|
|
|
|
else if ( "lamiral.info" === location.hostname )
|
|
|
|
{
|
2022-02-15 19:29:45 +01:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
$( "#local_bandwidth" ).collapse( "show" ) ;
|
|
|
|
$( "#local_status_dbmon" ).collapse( "show" ) ;
|
|
|
|
$( "#local_status_hetrix" ).collapse( "show" ) ;
|
|
|
|
$( "#imapsync_advice_hours" ).collapse( "show" ) ;
|
|
|
|
$( "#imapsync_current" ).load( "imapsync_current.txt" ) ;
|
|
|
|
}
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var tests_bilan = function tests_bilan( nb_attended_test )
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
2021-08-04 21:14:36 +02:00
|
|
|
// attended number of tests: nb_attended_test
|
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
$("#tests").append( "1.." + test.counter_all + "\n" ) ;
|
|
|
|
if ( test.counter_nok > 0 )
|
|
|
|
{
|
|
|
|
$("#tests").append(
|
|
|
|
"\nFAILED tests \n"
|
|
|
|
+ test.failed_tests
|
|
|
|
) ;
|
|
|
|
$("#tests").collapse("show") ;
|
|
|
|
}
|
|
|
|
// Summary of tests: failed 0 tests, run xx tests,
|
|
|
|
// expected to run yy tests.
|
|
|
|
if ( test.counter_all !== nb_attended_test )
|
|
|
|
{
|
|
|
|
$("#tests").append( "# Looks like you planned "
|
|
|
|
+ nb_attended_test
|
|
|
|
+ " tests but ran "
|
|
|
|
+ test.counter_all + ".\n"
|
|
|
|
) ;
|
|
|
|
$("#tests").collapse("show") ;
|
|
|
|
}
|
|
|
|
} ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
var tests = function tests( nb_attended_test )
|
2020-04-11 01:15:57 +02:00
|
|
|
{
|
|
|
|
if ( $("#tests").length !== 0 )
|
|
|
|
{
|
|
|
|
tests_store_retrieve( ) ;
|
|
|
|
tests_last_eta( ) ;
|
|
|
|
tests_decompose_eta_line( ) ;
|
2022-02-15 19:29:45 +01:00
|
|
|
tests_last_x_lines( ) ;
|
2021-08-04 21:14:36 +02:00
|
|
|
// tests_cryptojs( ) ;
|
|
|
|
|
|
|
|
// The following test can be used to check that if a test fails
|
|
|
|
// then all the tests are shown to the user.
|
2020-04-11 01:15:57 +02:00
|
|
|
//is( 0, 1, "this test always fails" ) ;
|
2021-08-04 21:14:36 +02:00
|
|
|
|
|
|
|
tests_bilan( nb_attended_test ) ;
|
2022-02-15 19:29:45 +01:00
|
|
|
|
2021-08-04 21:14:36 +02:00
|
|
|
// If you want to always see the tests, uncomment the following
|
|
|
|
// line
|
2022-02-15 19:29:45 +01:00
|
|
|
//$("#tests").collapse("show") ;
|
2020-04-11 01:15:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
init( ) ;
|
2022-02-15 19:29:45 +01:00
|
|
|
tests( 38 ) ;
|
2021-08-04 21:14:36 +02:00
|
|
|
retrieve_form( ) ;
|
2019-07-03 01:25:47 +02:00
|
|
|
|
2020-04-11 01:15:57 +02:00
|
|
|
}
|
2019-07-03 01:25:47 +02:00
|
|
|
|
|
|
|
);
|
2018-05-07 16:04:23 +02:00
|
|
|
|