AmendHub

Download

jcs

/

subtext

/

signup.c

 

(View History)

jcs   session: Fix 'new'/'signup' login going straight to signup Latest amendment: 574 on 2023-12-05

1 /*
2 * Copyright (c) 2022 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "subtext.h"
23 #include "session.h"
24 #include "signup.h"
25 #include "user.h"
26 #include "util.h"
27
28 struct user *
29 signup(struct session *s)
30 {
31 struct user *user = NULL;
32 char *username = NULL, *password = NULL, *password_confirm = NULL,
33 *error = NULL;
34
35 session_logf(s, "Signing up for an account");
36
37 session_printf(s, "{{B}}Create Account{{/B}} "
38 "(^C to cancel)\r\n"
39 "{{B}}----------------------------{{/B}}\r\n");
40 session_output_view_or_printf(s, DB_VIEW_SIGNUP,
41 "[ Signup instructions missing ]\r\n\r\n");
42 session_flush(s);
43
44 for (;;) {
45 session_printf(s, "{{B}}Username{{/B}} (Up to %d letters/numbers): ",
46 DB_USERNAME_LENGTH);
47 session_flush(s);
48
49 username = session_field_input(s, DB_USERNAME_LENGTH + 1,
50 DB_USERNAME_LENGTH, NULL, false, 0);
51 session_output(s, "\r\n", 2);
52 session_flush(s);
53
54 if (username == NULL || username[0] == '\0' || s->ending)
55 goto signup_done;
56
57 if (!user_valid_username(NULL, username, &error)) {
58 session_printf(s, "{{B}}Error:{{/B}} %s\r\n", error);
59 xfree(&error);
60 continue;
61 }
62
63 break;
64 }
65
66 for (;;) {
67 session_printf(s, "{{B}}Password:{{/B}} ");
68 session_flush(s);
69 password = session_field_input(s, 64, 64, NULL, false, '*');
70 session_output(s, "\r\n", 2);
71 session_flush(s);
72
73 if (password == NULL || s->ending)
74 goto signup_done;
75
76 if (password[0] == '\0') {
77 session_printf(s, "{{B}}Error:{{/B}} "
78 "Password cannot be blank\r\n");
79 xfree(&password);
80 continue;
81 }
82
83 session_printf(s, "{{B}}Password (again):{{/B}} ");
84 session_flush(s);
85 password_confirm = session_field_input(s, 64, 64, NULL, false,
86 '*');
87 session_output(s, "\r\n", 2);
88 session_flush(s);
89
90 if (password_confirm == NULL || s->ending)
91 goto signup_done;
92
93 if (strcmp(password_confirm, password) != 0) {
94 session_printf(s, "{{B}}Error:{{/B}} "
95 "Passwords do not match\r\n");
96 xfree(&password);
97 xfree(&password_confirm);
98 continue;
99 }
100
101 break;
102 }
103
104 user = xmalloczero(sizeof(struct user));
105 if (user == NULL)
106 goto signup_done;
107 strncpy(user->username, username, sizeof(user->username));
108 user->created_at = Time;
109 user->is_enabled = 1;
110 user_set_password(user, password);
111 if (!user_save(user)) {
112 session_logf(s, "Failed creating new user account for %s",
113 user->username);
114 xfree(&user);
115 goto signup_done;
116 }
117 user_cache_usernames();
118
119 session_logf(s, "New user account created for %s", user->username);
120
121 session_printf(s, "\r\nWelcome, {{B}}%s{{/B}}!\r\n", user->username);
122 session_flush(s);
123
124 signup_done:
125 if (username != NULL)
126 xfree(&username);
127 if (password != NULL)
128 xfree(&password);
129 if (password_confirm != NULL)
130 xfree(&password_confirm);
131
132 return user;
133 }