#define _POSIX_C_SOURCE 200809L #include "form.h" #include #include static int anonymous(struct html_node *const n) { struct html_node *div, *form, *luser, *iuser, *lpass, *ipass, *submit, *signup; if (!(div = html_node_add_child(n, "div")) || !(form = html_node_add_child(div, "form")) || !(luser = html_node_add_child(form, "label")) || !(iuser = html_node_add_child(form, "input")) || !(lpass = html_node_add_child(form, "label")) || !(ipass = html_node_add_child(form, "input")) || !(submit = html_node_add_child(form, "input")) || !(signup = html_node_add_child(div, "a"))) { fprintf(stderr, "%s: html_node_add_child failed\n", __func__); return -1; } else if (html_node_add_attr(form, "action", "/login") || html_node_add_attr(form, "form", "loginform") || html_node_add_attr(form, "method", "post") || html_node_add_attr(luser, "for", "username") || html_node_add_attr(lpass, "for", "password") || html_node_add_attr(iuser, "type", "text") || html_node_add_attr(iuser, "id", "username") || html_node_add_attr(iuser, "name", "username") || html_node_add_attr(ipass, "type", "password") || html_node_add_attr(ipass, "id", "password") || html_node_add_attr(ipass, "name", "password") || html_node_add_attr(submit, "type", "submit") || html_node_add_attr(submit, "value", "Log in") || html_node_add_attr(signup, "href", "/signup")) { fprintf(stderr, "%s: html_node_add_attr failed\n", __func__); return -1; } else if (html_node_set_value(luser, "Username:") || html_node_set_value(lpass, "Password:") || html_node_set_value(signup, "Sign up")) { fprintf(stderr, "%s: html_node_set_value failed\n", __func__); return -1; } return 0; } static int loggedin(struct html_node *const n, const char *const username) { int ret = -1; struct html_node *div, *p, *form, *submit, *a; struct dynstr d; dynstr_init(&d); if (!(div = html_node_add_child(n, "div")) || !(p = html_node_add_child(div, "p")) || !(form = html_node_add_child(div, "form")) || !(submit = html_node_add_child(form, "input")) || !(a = html_node_add_child(div, "a"))) { fprintf(stderr, "%s: html_node_add_child failed\n", __func__); goto end; } else if (dynstr_append(&d, "Logged in as %s", username)) { fprintf(stderr, "%s: dynstr_append failed\n", __func__); goto end; } else if (html_node_set_value(p, d.str) || html_node_set_value(a, "User control panel")) { fprintf(stderr, "%s: html_node_set_value failed\n", __func__); goto end; } else if (html_node_add_attr(form, "action", "/logout") || html_node_add_attr(form, "form", "logoutform") || html_node_add_attr(form, "method", "post") || html_node_add_attr(submit, "type", "submit") || html_node_add_attr(submit, "value", "Logout") || html_node_add_attr(a, "href", "/ucp")) { fprintf(stderr, "%s: html_node_add_attr failed\n", __func__); goto end; } ret = 0; end: dynstr_free(&d); return ret; } int form_login(struct html_node *const n, const char *const username) { if (!username) return anonymous(n); return loggedin(n, username); }