@@ -109,24 +109,98 @@ bool client_find(struct client** pc, int id)
109
109
return false;
110
110
}
111
111
112
+ bool client_validate_username (struct client * c , struct client_name_request * req )
113
+ {
114
+ assert (c != NULL );
115
+ assert (req != NULL );
116
+
117
+ // TODO: change from bool to int
118
+ // Create error codes for all the possible errors
119
+ // To give the user meaningful error messages
120
+ char * name = NULL ;
121
+ size_t size ;
122
+ req -> valid = false;
123
+ size = queue_pop_no_copy (c -> recv_qu , & name );
124
+ size -- ; // Loose the /r
125
+ if (size < 1 ) {
126
+ strcpy (req -> invalid_name_response , "Empty username provided" );
127
+ return false;
128
+ }
129
+ if (size >= CLIENT_MAX_NAME ) {
130
+ sprintf (req -> invalid_name_response ,
131
+ "Name provided exceeds limit for number of characters: %d" ,
132
+ CLIENT_MAX_NAME );
133
+ return false;
134
+ }
135
+
136
+ for (size_t k = 0 ; k < size ; k ++ ) {
137
+ if (!isalnum (name [k ])) {
138
+ strcpy (req -> invalid_name_response , "Username must only contain alphanumeric characters" );
139
+ return false;
140
+ }
141
+ }
142
+
143
+ req -> name = name ;
144
+ req -> size = size ;
145
+ if (!client_name_exists (c , req )) {
146
+ strcpy (req -> invalid_name_response , "Username is already taken" );
147
+ return false;
148
+ }
149
+
150
+ memcpy (c -> name , name , size );
151
+ c -> name [size + 1 ] = 0 ;
152
+ return true;
153
+ }
154
+
155
+ int client_handle_newclient (struct client * c )
156
+ {
157
+ assert (c != NULL );
158
+
159
+ struct client_name_request req ;
160
+ if (!client_validate_username (c , & req )) {
161
+ // sendall(req.invalid_name_response)
162
+ return -1 ;
163
+ }
164
+
165
+ // Collect list of all names in chat
166
+ // client broadcast message from
167
+ // client_send(c, WELCOME_MESSAGE)
168
+ return 1 ;
169
+ }
170
+
112
171
int client_handle_request (struct client * c )
113
172
{
114
173
// tokenize the messages
115
174
// TODO: what to do with more than one message
116
175
if (c -> name [0 ] == 0 ) {
117
- if (!client_set_name (c ))
118
- return -1 ;
119
-
120
- // client broadcast message from
121
- // client_send(c, WELCOME_MESSAGE)
176
+ return client_handle_newclient (c );
122
177
}
123
178
124
- char * msg ;
179
+ char * msg ;
125
180
size_t size ;
126
181
size = queue_pop_no_copy (c -> recv_qu , & msg );
127
182
}
128
183
129
- void client_collect_list_of_names_other_names (struct client * c ) {}
184
+ /*void client_on_valid_username(struct client* c)*/
185
+ /*{*/
186
+ /* // To *c*/
187
+ /* // - The room contains: etc..*/
188
+ /* // To everybody else:*/
189
+ /* // - *c has entered the room*/
190
+ /*}*/
191
+
192
+ void client_on_exit (struct client * c )
193
+ {
194
+ // To everybody else:
195
+ // - *c has entered the room
196
+ }
197
+
198
+ void client_collect_list_of_names_other_names (struct client * c )
199
+ {
200
+ assert (* c != NULL );
201
+
202
+ //
203
+ }
130
204
131
205
void client_broadcast_message_to_all (struct client * c , char * msg , size_t size )
132
206
{
@@ -140,8 +214,32 @@ void client_broadcast_message_from(struct client* c, char* msg, size_t size)
140
214
// sendall(msg, size);
141
215
}
142
216
143
- void client_name_exists (struct client * c , struct client_name_request * name_req )
217
+ bool client_name_exists (struct client * c , struct client_name_request * name_req )
144
218
{
219
+ assert (c != NULL );
220
+ assert (name_req != NULL );
221
+
222
+ // Does this username exists?
223
+ // for each client
224
+ // if (client->id == c->id) continue;
225
+ // if (strcmp(client->name, c->name) == 0) return false;
226
+ struct client * me = c ;
227
+ client_first (& c );
228
+ struct client * next = c -> next ;
229
+ struct client * last = c ;
230
+ do {
231
+ if ((last -> id != me -> id )
232
+ && (strncmp (last -> name , name_req -> name , name_req -> size ) == 0 ))
233
+ {
234
+ name_req -> valid = false;
235
+ return false;
236
+ }
237
+
238
+ last = next ;
239
+ next = next -> next ;
240
+ } while (next != NULL );
241
+
242
+ return true;
145
243
}
146
244
147
245
void client_send_welcome_prompt (struct client * c )
@@ -151,22 +249,3 @@ void client_send_welcome_prompt(struct client* c)
151
249
assert (res == 0 );
152
250
assert (l == CLIENT_WELCOME_PROMPT_SIZE );
153
251
}
154
-
155
- bool client_set_name (struct client * c )
156
- {
157
- char * name ;
158
- size_t size ;
159
- size = queue_pop_no_copy (c -> recv_qu , & name );
160
- if (size < 1 )
161
- return false;
162
- if (size > CLIENT_MAX_NAME )
163
- return false;
164
-
165
- for (size_t k = 0 ; k < size ; k ++ ) {
166
- if (!isalnum (name [k ])
167
- return false;
168
- }
169
- memcpy (c -> name , name , size );
170
- c -> name [size + 1 ] = 0 ;
171
- return true;
172
- }
0 commit comments