// A contact form with a picture captcha; the answer stays in the session.
//
//   quillo run captcha.qwe      then open http://localhost:8080/contact

use "captcha.qwer"

route GET /contact {
    let c = captcha_new(5, false)?
    set_session({ captcha: c.answer }, { max_age: "10m" })
    <form method="post" action="/contact">
        <img src={c.image} alt="The letters to type"/>
        <input name="answer" autocomplete="off" placeholder="The letters above"/>
        <textarea name="message"></textarea>
        <button>Send</button>
    </form>
}

route POST /contact { form: { answer: str, message: str } } {
    if !captcha_check(session().captcha ?? "", answer) { reply 400 "The letters did not match: try a new picture." }
    set_session({})
    { sent: message }
}

route GET /check {
    let c = captcha_new(6, true)?
    { length: len(c.answer), png: starts_with(c.image, "data:image/png;base64,"), right: captcha_check(c.answer, upper(c.answer)), wrong: captcha_check(c.answer, "nope") }
}
