WIP add js to parse and add matches

This commit is contained in:
Balakrishnan Balasubramanian 2023-07-02 23:48:04 -04:00
parent 6d0040415b
commit 117c93deaf
6 changed files with 274 additions and 10 deletions

1
jsdev/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tmp

44
jsdev/air.toml Normal file
View File

@ -0,0 +1,44 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "cp ./restart.sh ./tmp/main"
delay = 0
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[screen]
clear_on_rebuild = false
keep_scroll = true

130
jsdev/index.html Normal file
View File

@ -0,0 +1,130 @@
<!doctype html>
<html>
<head>
<title>Mail4one Web config</title>
<script type="application/json" id="m41config">
{
"matches": [
{
"name": "mydomain",
"addr_rexs": [
".*@mydomain.com",
".*@m.mydomain.com"
]
},
{
"name": "personal",
"addrs": [
"first.last@mydomain.com",
"secret.name@mydomain.com"
]
}
],
"boxes": [
{
"name": "spam",
"rules": [
{
"match_name": "mydomain",
"negate": true,
"stop_check": true
}
]
},
{
"name": "important",
"rules": [
{
"match_name": "personal"
}
]
},
{
"name": "all",
"rules": [
{
"match_name": "default_match_all"
}
]
}
],
"users": [
{
"username": "mymobile",
"password_hash": "AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWDZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQDAQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF",
"mbox": "important"
},
{
"username": "mydesk",
"password_hash": "AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWDZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQDAQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF",
"mbox": "all"
}
]
}
</script>
<script type="application/javascript">
function main() {
console.log("hello world !!!")
var config = JSON.parse(document.getElementById('m41config').text);
matches_table = document.getElementById("web-cfg-matches")
console.log(matches_table)
for (match of config["matches"]) {
vals = []
matchType = ""
if ("addr_rexs" in match) {
vals = match["addr_rexs"]
matchType = "addr_rexs"
}else if ("addrs" in match) {
vals = match["addrs"]
matchType = "addrs"
}
row = matches_table.insertRow(-1)
name_cell = row.insertCell(-1)
name_cell.innerText = match["name"]
type_cell = row.insertCell(-1)
type_cell.innerText = matchType
value_cell = row.insertCell(-1)
value_cell.contentEditable = true
for (val of vals) {
const node = document.createElement("p");
const textnode = document.createTextNode(val);
node.appendChild(textnode);
value_cell.appendChild(node);
}
vt = value_cell.innerText
console.log(vt.split("\n"))
}
}
function addMatchRow() {
matches_table = document.getElementById("web-cfg-matches")
row_tmpl = document.getElementById("web-cfg-matches-row")
row_copy = row_tmpl.content.cloneNode(true)
tb = matches_table.tBodies[0]
tb.appendChild(row_copy)
}
</script>
</head>
<body onload="main()">
<template id="web-cfg-matches-row">
<tr>
<td>Name</td>
<td>Type</td>
<td>Values</td>
</tr>
</template>
<h1>Mail4one Web config</h1>
<table id="web-cfg-matches">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Values</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<button onClick="addMatchRow()">Add Match</button>
</body>
</html>

11
jsdev/restart.sh Executable file
View File

@ -0,0 +1,11 @@
#!/bin/sh
# kill "$(cat current.pid)"
# echo "starting python server"
python3 -m http.server
# echo $! > current.pid
# echo "started python server"

View File

@ -1,5 +1,88 @@
<!doctype html> <!doctype html>
<html> <html>
<head><title>Mail4one Web config</title></head> <head>
<body><h1>Hello World</h1></body> <title>Mail4one Web config</title>
<script type="application/json" id="m41config">
{
"matches": [
{
"name": "mydomain",
"addr_rexs": [
".*@mydomain.com",
".*@m.mydomain.com"
]
},
{
"name": "personal",
"addrs": [
"first.last@mydomain.com",
"secret.name@mydomain.com"
]
}
],
"boxes": [
{
"name": "spam",
"rules": [
{
"match_name": "mydomain",
"negate": true,
"stop_check": true
}
]
},
{
"name": "important",
"rules": [
{
"match_name": "personal"
}
]
},
{
"name": "all",
"rules": [
{
"match_name": "default_match_all"
}
]
}
],
"users": [
{
"username": "mymobile",
"password_hash": "AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWDZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQDAQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF",
"mbox": "important"
},
{
"username": "mydesk",
"password_hash": "AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWDZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQDAQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF",
"mbox": "all"
}
]
}
</script>
<script type="application/javascript">
function main() {
document.write("hello world")
}
</script>
</head>
<body onload="main()">
<template id="web-cfg-matches-row">
<tr>
<td>Name</td>
<td>Type</td>
<td>Values</td>
</tr>
</template>
<h1>Mail4one Web config</h1>
<table id="web-cfg-matches">
<tr>
<th>Name</th>
<th>Type</th>
<th>Values</th>
</tr>
</table>
</body>
</html> </html>

View File

@ -45,17 +45,12 @@ class WebonfigHandler(RequestHandler):
userpassb64 = auth_header[len("Basic ") :] userpassb64 = auth_header[len("Basic ") :]
try: try:
userpass = b64decode(userpassb64) userpass = b64decode(userpassb64)
username, password = userpass.split(b":")
except: except:
logging.exception("bad request") logging.exception("bad request")
return False, Response.no_body_response(http.HTTPStatus.BAD_REQUEST) return False, Response.no_body_response(http.HTTPStatus.BAD_REQUEST)
try: if username == self.username and check_pass(password.decode(), self.pwinfo):
user, passwd = userpass.split(b":")
except:
logging.exception("bad request")
return False, Response.no_body_response(http.HTTPStatus.BAD_REQUEST)
if user == self.username and check_pass(passwd.decode(), self.pwinfo):
return True, None return True, None
return False, resp_unauthorized() return False, resp_unauthorized()
@ -67,7 +62,7 @@ class WebonfigHandler(RequestHandler):
if not ok: if not ok:
if resp: if resp:
return resp return resp
else: else: # To silence mypy
raise Exception("Something went wrong!") raise Exception("Something went wrong!")
return Response.create_ok_response(get_template()) return Response.create_ok_response(get_template())