/* * Copyright (c) 2022 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include "subtext.h" #include "session.h" #include "signup.h" #include "user.h" #include "util.h" struct user * signup(struct session *s) { struct user *user = NULL; char *username = NULL, *password = NULL, *password_confirm = NULL, *error = NULL; session_logf(s, "Signing up for an account"); session_printf(s, "{{B}}Create Account{{/B}} " "(^C to cancel)\r\n" "{{B}}----------------------------{{/B}}\r\n"); session_output_view_or_printf(s, DB_VIEW_SIGNUP, "[ Signup instructions missing ]\r\n\r\n"); session_flush(s); for (;;) { session_printf(s, "{{B}}Username{{/B}} (Up to %d letters/numbers): ", DB_USERNAME_LENGTH); session_flush(s); username = session_field_input(s, DB_USERNAME_LENGTH + 1, DB_USERNAME_LENGTH, NULL, false, 0); session_output(s, "\r\n", 2); session_flush(s); if (username == NULL || username[0] == '\0' || s->ending) goto signup_done; if (!user_valid_username(NULL, username, &error)) { session_printf(s, "{{B}}Error:{{/B}} %s\r\n", error); xfree(&error); continue; } break; } for (;;) { session_printf(s, "{{B}}Password:{{/B}} "); session_flush(s); password = session_field_input(s, 64, 64, NULL, false, '*'); session_output(s, "\r\n", 2); session_flush(s); if (password == NULL || s->ending) goto signup_done; if (password[0] == '\0') { session_printf(s, "{{B}}Error:{{/B}} " "Password cannot be blank\r\n"); xfree(&password); continue; } session_printf(s, "{{B}}Password (again):{{/B}} "); session_flush(s); password_confirm = session_field_input(s, 64, 64, NULL, false, '*'); session_output(s, "\r\n", 2); session_flush(s); if (password_confirm == NULL || s->ending) goto signup_done; if (strcmp(password_confirm, password) != 0) { session_printf(s, "{{B}}Error:{{/B}} " "Passwords do not match\r\n"); xfree(&password); xfree(&password_confirm); continue; } break; } user = xmalloczero(sizeof(struct user)); if (user == NULL) goto signup_done; strncpy(user->username, username, sizeof(user->username)); user->created_at = Time; user->is_enabled = 1; user_set_password(user, password); if (!user_save(user)) { session_logf(s, "Failed creating new user account for %s", user->username); xfree(&user); goto signup_done; } user_cache_usernames(); session_logf(s, "New user account created for %s", user->username); session_printf(s, "\r\nWelcome, {{B}}%s{{/B}}!\r\n", user->username); session_flush(s); signup_done: if (username != NULL) xfree(&username); if (password != NULL) xfree(&password); if (password_confirm != NULL) xfree(&password_confirm); return user; }