def view_my_mask(*, claim=None, set_mask=None):
"""
View and manage mask linking for cookies.
- GET: Shows current mask and allows claim or update.
- POST (claim/set_mask): Claim a mask and save/load cookies to/from masks.cdv.
If user claims an existing mask AND already has a mask cookie,
ALL existing cookies (except cookies_accepted) are wiped before restoring the claimed mask.
No wipe is performed when creating a new mask.
"""
cookies_ok = check_consent()
mask = get("mask", "")
# Handle claiming or setting mask via POST
if claim or set_mask:
ident = (claim or set_mask or "").strip()
norm = _normalize_mask(ident)
if not norm:
msg = "<b>mask string is invalid.</b> Please use only letters, numbers, and dashes."
else:
mask_map = _read_masks()
existing = mask_map.get(norm)
if not existing:
# New mask: Save all current cookies (except mask and cookies_accepted) to record
current = _get_current_cookies()
filtered = {k: v for k, v in current.items() if k not in ("mask", "cookies_accepted")}
mask_map[norm] = filtered
_write_masks(mask_map)
set("mask", norm)
msg = (
f"<b>mask <code>{html.escape(norm)}</code> claimed and stored!</b> "
"Your cookie data has been saved under this mask. "
"You may now restore it from any device or browser by claiming this mask again."
)
else:
# If user already has a mask, wipe all their cookies (except cookies_accepted) before restoring
if mask:
for k in list(request.cookies):
if k not in ("cookies_accepted",):
remove(k)
# Restore cookies from mask
_restore_cookies(existing)
set("mask", norm)
# Merge new cookies into record (overwriting with current, but not blanking any missing)
merged = existing.copy()
for k, v in _get_current_cookies().items():
if k not in ("mask", "cookies_accepted"):
merged[k] = v
mask_map[norm] = merged
_write_masks(mask_map)
msg = (
f"<b>mask <code>{html.escape(norm)}</code> loaded!</b> "
"All cookies for this mask have been restored and merged with your current data. "
"Future changes to your cookies will update this mask."
)
# After processing, reload view with message
return view_my_mask() + f"<div style='margin:1em 0; color:#080;'>{msg}</div>"
# GET: Show info, form, and current mask
mask_note = (
f"<div style='margin:1em 0; color:#005;'><b>Current mask:</b> <code>{html.escape(mask)}</code></div>"
if mask else
"<div style='margin:1em 0; color:#888;'>You have not claimed a mask yet.</div>"
)
claim_form = """
<form method="POST" style="margin-top:1em;">
<label for="mask" style="font-size:1em;">
Enter a mask string to claim (letters, numbers, dashes):</label>
<input type="text" id="mask" name="set_mask" required pattern="[a-zA-Z0-9\\-]+"
style="margin-left:0.5em; font-size:1.1em; width:12em; border-radius:0.3em; border:1px solid #aaa;"/>
<button type="submit" style="margin-left:1em; font-size:1em;">Claim / Load</button>
</form>
"""
return f"""
<h1>Cookie Masks</h1>
<p>
<strong>Masks</strong> allow you to copy your cookie data (such as preferences, navigation history, cart, etc)
from one device or browser to another, without needing to register an account.
Claiming a mask will save a copy of your current cookie data under the mask string you provide.<br>
<b>Warning:</b> Anyone who knows this mask string can restore your cookie data, so choose carefully.
</p>
{mask_note}
{claim_form}
<p style='margin-top:2em; color:#555; font-size:0.98em;'>
To transfer your Cookies as a Mask:<br>
1. On your main device, claim mask (e.g. "my-handle-123").<br>
2. On another device/browser, visit this page and claim the same mask to restore your data.<br>
3. Any changes you make while holding a mask will update the stored copy.
</p>
"""