cdb_fetch() and cdb_next_item() now return a struct cdbdata instead of a pointer...
[citadel.git] / citadel / server / modules / ctdlproto / serv_user.c
1 // Server functions which perform operations on user objects.
2 //
3 // Copyright (c) 1987-2023 by the citadel.org team
4 //
5 // This program is open source software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12
13
14 #include "../../support.h"
15 #include "../../control.h"
16 #include "../../ctdl_module.h"
17 #include "../../citserver.h"
18 #include "../../config.h"
19 #include "../../user_ops.h"
20 #include "../../internet_addressing.h"
21
22
23 // USER command -- attempt to log in as an existing user
24 void cmd_user(char *cmdbuf) {
25         char username[256];
26         int a;
27
28         extract_token(username, cmdbuf, 0, '|', sizeof username);
29         string_trim(username);
30         syslog(LOG_DEBUG, "user_ops: cmd_user(%s)", username);
31
32         a = CtdlLoginExistingUser(username);
33         switch (a) {
34         case login_already_logged_in:
35                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
36                 return;
37         case login_too_many_users:
38                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
39                         ERROR + MAX_SESSIONS_EXCEEDED,
40                         CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions")
41                 );
42                 return;
43         case login_ok:
44                 cprintf("%d Password required for %s\n", MORE_DATA, CC->curr_user);
45                 return;
46         case login_not_found:
47                 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
48                 return;
49         default:
50                 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
51         }
52 }
53
54
55 // PASS command -- complete logging in as an existing user (used after USER returns MORE_DATA)
56 void cmd_pass(char *buf) {
57         char password[SIZ];
58         int a;
59         long len;
60
61         memset(password, 0, sizeof(password));
62         len = extract_token(password, buf, 0, '|', sizeof password);
63         a = CtdlTryPassword(password, len);
64
65         switch (a) {
66         case pass_already_logged_in:
67                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
68                 return;
69         case pass_no_user:
70                 cprintf("%d You must send a name with USER first.\n", ERROR + USERNAME_REQUIRED);
71                 return;
72         case pass_wrong_password:
73                 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
74                 return;
75         case pass_ok:
76                 logged_in_response();
77                 return;
78         }
79 }
80
81
82 // cmd_newu()  -  create a new user account and log in as that user
83 void cmd_newu(char *cmdbuf) {
84         int a;
85         char username[SIZ];
86
87         if (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) {
88                 cprintf("%d This system does not use native mode authentication.\n",
89                         ERROR + NOT_HERE);
90                 return;
91         }
92
93         if (CtdlGetConfigInt("c_disable_newu")) {
94                 cprintf("%d Self-service user account creation is disabled on this system.\n", ERROR + NOT_HERE);
95                 return;
96         }
97
98         if (CC->logged_in) {
99                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
100                 return;
101         }
102         if (CC->nologin) {
103                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
104                         ERROR + MAX_SESSIONS_EXCEEDED,
105                         CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions"));
106                 return;
107         }
108         extract_token(username, cmdbuf, 0, '|', sizeof username);
109         strproc(username);
110
111         if (IsEmptyStr(username)) {
112                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
113                 return;
114         }
115
116         if ((!strcasecmp(username, "bbs")) ||
117             (!strcasecmp(username, "new")) ||
118             (!strcasecmp(username, "."))
119         ) {
120                 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
121                 return;
122         }
123
124         a = create_user(username, CREATE_USER_BECOME_USER, NATIVE_AUTH_UID);
125
126         if (a == 0) {
127                 logged_in_response();
128         }
129         else if (a == ERROR + ALREADY_EXISTS) {
130                 cprintf("%d '%s' already exists.\n",
131                         ERROR + ALREADY_EXISTS, username);
132                 return;
133         }
134         else if (a == ERROR + INTERNAL_ERROR) {
135                 cprintf("%d Internal error - user record disappeared?\n",
136                         ERROR + INTERNAL_ERROR);
137                 return;
138         }
139         else {
140                 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
141         }
142 }
143
144
145 // set password - citadel protocol implementation
146 void cmd_setp(char *new_pw) {
147         if (CtdlAccessCheck(ac_logged_in)) {
148                 return;
149         }
150         if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
151                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
152                 return;
153         }
154
155         if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
156                 char random_password[17];
157                 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
158                 CtdlSetPassword(random_password);
159                 cprintf("%d %s\n", CIT_OK, random_password);
160         }
161         else {
162                 strproc(new_pw);
163                 if (IsEmptyStr(new_pw)) {
164                         cprintf("%d Password unchanged.\n", CIT_OK);
165                         return;
166                 }
167                 CtdlSetPassword(new_pw);
168                 cprintf("%d Password changed.\n", CIT_OK);
169         }
170 }
171
172
173 // cmd_creu() - administratively create a new user account (do not log in to it)
174 void cmd_creu(char *cmdbuf) {
175         int a;
176         char username[SIZ];
177         char password[SIZ];
178         struct ctdluser tmp;
179
180         if (CtdlAccessCheck(ac_aide)) {
181                 return;
182         }
183
184         extract_token(username, cmdbuf, 0, '|', sizeof username);
185         strproc(username);
186         strproc(password);
187         if (IsEmptyStr(username)) {
188                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
189                 return;
190         }
191
192         extract_token(password, cmdbuf, 1, '|', sizeof password);
193
194         a = create_user(username, CREATE_USER_DO_NOT_BECOME_USER, NATIVE_AUTH_UID);
195
196         if (a == 0) {
197                 if (!IsEmptyStr(password)) {
198                         CtdlGetUserLock(&tmp, username);
199                         safestrncpy(tmp.password, password, sizeof(tmp.password));
200                         CtdlPutUserLock(&tmp);
201                 }
202                 cprintf("%d User '%s' created %s.\n", CIT_OK, username, (!IsEmptyStr(password)) ? "and password set" : "with no password");
203                 return;
204         }
205         else if (a == ERROR + ALREADY_EXISTS) {
206                 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
207                 return;
208         }
209         else if ( (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
210                 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n", ERROR + NO_SUCH_USER);
211                 return;
212         }
213         else {
214                 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
215         }
216 }
217
218
219 // get user parameters
220 void cmd_getu(char *cmdbuf) {
221         if (CtdlAccessCheck(ac_logged_in)) {
222                 return;
223         }
224
225         CtdlGetUser(&CC->user, CC->curr_user);
226         cprintf("%d 80|24|%d|\n", CIT_OK, (CC->user.flags & US_USER_SET));
227 }
228
229
230 // set user parameters
231 void cmd_setu(char *new_parms) {
232         if (CtdlAccessCheck(ac_logged_in)) {
233                 return;
234         }
235
236         if (num_parms(new_parms) < 3) {
237                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
238                 return;
239         }
240         CtdlLockGetCurrentUser();
241         CC->user.flags = CC->user.flags & (~US_USER_SET);
242         CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
243         CtdlPutCurrentUserLock();
244         cprintf("%d Ok\n", CIT_OK);
245 }
246
247
248 // set last read pointer (marks all messages in the current room as read, up to the specified point)
249 void cmd_slrp(char *new_ptr) {
250         long newlr;
251         struct visit vbuf;
252         struct visit original_vbuf;
253
254         if (CtdlAccessCheck(ac_logged_in)) {
255                 return;
256         }
257
258         if (!strncasecmp(new_ptr, "highest", 7)) {
259                 newlr = CC->room.QRhighest;
260         }
261         else {
262                 newlr = atol(new_ptr);
263         }
264
265         CtdlLockGetCurrentUser();
266
267         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
268         memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
269         vbuf.v_lastseen = newlr;
270         snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
271
272         // Only rewrite the record if it changed
273         if (    (vbuf.v_lastseen != original_vbuf.v_lastseen)
274                 || (strcmp(vbuf.v_seen, original_vbuf.v_seen))
275         ) {
276                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
277         }
278
279         CtdlPutCurrentUserLock();
280         cprintf("%d %ld\n", CIT_OK, newlr);
281 }
282
283
284 void cmd_seen(char *argbuf) {
285         long target_msgnum = 0L;
286         int target_setting = 0;
287
288         if (CtdlAccessCheck(ac_logged_in)) {
289                 return;
290         }
291
292         if (num_parms(argbuf) != 2) {
293                 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
294                 return;
295         }
296
297         target_msgnum = extract_long(argbuf, 0);
298         target_setting = extract_int(argbuf, 1);
299
300         CtdlSetSeen(&target_msgnum, 1, target_setting, ctdlsetseen_seen, NULL, NULL);
301         cprintf("%d OK\n", CIT_OK);
302 }
303
304
305 void cmd_gtsn(char *argbuf) {
306         struct visit vbuf;
307
308         if (CtdlAccessCheck(ac_logged_in)) {
309                 return;
310         }
311
312         // Learn about the user and room in question
313         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
314
315         cprintf("%d ", CIT_OK);
316         client_write(vbuf.v_seen, strlen(vbuf.v_seen));
317         client_write(HKEY("\n"));
318 }
319
320
321 // INVT and KICK commands (grant/revoke access to an invitation-only room)
322 void cmd_invt_kick(char *iuser, int op) {
323
324         // These commands are only allowed by admins, room admins,
325         // and room namespace owners
326         if (is_room_aide()) {
327                 // access granted
328         }
329         else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
330                 // access granted
331         }
332         else {
333                 // access denied
334                 cprintf("%d Higher access or room ownership required.\n", ERROR + HIGHER_ACCESS_REQUIRED);
335                 return;
336         }
337
338         if (!strncasecmp(CC->room.QRname, CtdlGetConfigStr("c_baseroom"), ROOMNAMELEN)) {
339                 cprintf("%d Can't add/remove users from this room.\n", ERROR + NOT_HERE);
340                 return;
341         }
342
343         if (CtdlInvtKick(iuser, op) != 0) {
344                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
345                 return;
346         }
347
348         cprintf("%d %s %s %s.\n",
349                 CIT_OK, iuser,
350                 ((op == 1) ? "invited to" : "kicked out of"),
351                 CC->room.QRname);
352         return;
353 }
354
355
356 void cmd_invt(char *iuser) {
357         cmd_invt_kick(iuser, 1);
358 }
359
360
361 void cmd_kick(char *iuser) {
362         cmd_invt_kick(iuser, 0);
363 }
364
365
366 // forget (Zap) the current room
367 void cmd_forg(char *argbuf) {
368
369         if (CtdlAccessCheck(ac_logged_in)) {
370                 return;
371         }
372
373         if (CtdlForgetThisRoom() == 0) {
374                 cprintf("%d Ok\n", CIT_OK);
375         }
376         else {
377                 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
378         }
379 }
380
381
382 // Get Next Unregistered User
383 void cmd_gnur(char *argbuf) {
384         struct cdbdata cdbus;
385         struct ctdluser usbuf;
386
387         if (CtdlAccessCheck(ac_aide)) {
388                 return;
389         }
390
391         if ((CtdlGetConfigInt("MMflags") & MM_VALID) == 0) {
392                 cprintf("%d There are no unvalidated users.\n", CIT_OK);
393                 return;
394         }
395
396         // There are unvalidated users.  Traverse the user database, and return the first user we find that needs validation.
397         cdb_rewind(CDB_USERS);
398         while (cdbus = cdb_next_item(CDB_USERS), cdbus.len>0) {
399                 memset(&usbuf, 0, sizeof(struct ctdluser));
400                 memcpy(&usbuf, cdbus.ptr, ((cdbus.len > sizeof(struct ctdluser)) ?  sizeof(struct ctdluser) : cdbus.len));
401                 if ((usbuf.flags & US_NEEDVALID) && (usbuf.axlevel > AxDeleted)) {
402                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
403                         cdb_close_cursor(CDB_USERS);
404                         return;
405                 }
406         }
407
408         // If we get to this point, there are no more unvalidated users.  Therefore we clear the "users need validation" flag.
409         begin_critical_section(S_CONTROL);
410         int flags;
411         flags = CtdlGetConfigInt("MMflags");
412         flags = flags & (~MM_VALID);
413         CtdlSetConfigInt("MMflags", flags);
414         end_critical_section(S_CONTROL);
415         cprintf("%d *** End of registration.\n", CIT_OK);
416 }
417
418
419 // validate a user
420 void cmd_vali(char *v_args) {
421         char user[128];
422         int newax;
423         struct ctdluser userbuf;
424
425         extract_token(user, v_args, 0, '|', sizeof user);
426         newax = extract_int(v_args, 1);
427
428         if (CtdlAccessCheck(ac_aide) || (newax > AxAideU) || (newax < AxDeleted)) {
429                 return;
430         }
431
432         if (CtdlGetUserLock(&userbuf, user) != 0) {
433                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
434                 return;
435         }
436
437         userbuf.axlevel = newax;
438         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
439
440         CtdlPutUserLock(&userbuf);
441
442         // If the access level was set to zero, delete the user
443         if (newax == 0) {
444                 if (purge_user(user) == 0) {
445                         cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
446                         return;
447                 }
448         }
449         cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
450 }
451
452
453 // List one user (this works with cmd_list)
454 void ListThisUser(char *username, void *data) {
455         char *searchstring;
456         struct ctdluser usbuf;
457
458         if (CtdlGetUser(&usbuf, username) != 0) {
459                 return;
460         }
461
462         searchstring = (char *)data;
463         if (bmstrcasestr(usbuf.fullname, searchstring) == NULL) {
464                 return;
465         }
466
467         if (usbuf.axlevel > AxDeleted) {
468                 if ((CC->user.axlevel >= AxAideU)
469                     || ((usbuf.flags & US_UNLISTED) == 0)
470                     || ((CC->internal_pgm))) {
471                         cprintf("%s|%d|%ld|%ld|0|0||\n",
472                                 usbuf.fullname,
473                                 usbuf.axlevel,
474                                 usbuf.usernum,
475                                 (long)usbuf.lastcall);
476                 }
477         }
478 }
479
480
481 //  List users (searchstring may be empty to list all users)
482 void cmd_list(char *cmdbuf) {
483         char searchstring[256];
484         extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
485         string_trim(searchstring);
486         cprintf("%d \n", LISTING_FOLLOWS);
487         ForEachUser(ListThisUser, (void *)searchstring );
488         cprintf("000\n");
489 }
490
491
492 // assorted info we need to check at login
493 void cmd_chek(char *argbuf) {
494         int mail = 0;
495         int regis = 0;
496         int vali = 0;
497
498         if (CtdlAccessCheck(ac_logged_in)) {
499                 return;
500         }
501
502         CtdlGetUser(&CC->user, CC->curr_user);  // no lock is needed here 
503         if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0)) {
504                 regis = 1;
505         }
506
507         if (CC->user.axlevel >= AxAideU) {
508                 if (CtdlGetConfigInt("MMflags") & MM_VALID) {
509                         vali = 1;
510                 }
511         }
512
513         mail = InitialMailCheck();              // check for mail
514         cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
515 }
516
517
518 // check to see if a user exists
519 void cmd_qusr(char *who) {
520         struct ctdluser usbuf;
521
522         if (CtdlGetUser(&usbuf, who) == 0) {
523                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
524         }
525         else {
526                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
527         }
528 }
529
530
531 // Administrative Get User Parameters
532 void cmd_agup(char *cmdbuf) {
533         struct ctdluser usbuf;
534         char requested_user[128];
535
536         if (CtdlAccessCheck(ac_aide)) {
537                 return;
538         }
539
540         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
541         if (CtdlGetUser(&usbuf, requested_user) != 0) {
542                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
543                 return;
544         }
545         cprintf("%d %s|%s|%u|0|0|%d|%ld|%ld|%d\n",
546                 CIT_OK,
547                 usbuf.fullname,
548                 usbuf.password,
549                 usbuf.flags,
550                 (int) usbuf.axlevel,
551                 usbuf.usernum,
552                 (long)usbuf.lastcall,
553                 usbuf.USuserpurge);
554 }
555
556
557 // Administrative Set User Parameters
558 void cmd_asup(char *cmdbuf) {
559         struct ctdluser usbuf;
560         char requested_user[128];
561         char notify[SIZ];
562         int np;
563         int newax;
564         int deleted = 0;
565
566         if (CtdlAccessCheck(ac_aide))
567                 return;
568
569         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
570         if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
571                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
572                 return;
573         }
574         np = num_parms(cmdbuf);
575         if (np > 1)
576                 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
577         if (np > 2)
578                 usbuf.flags = extract_int(cmdbuf, 2);
579         if (np > 5) {
580                 newax = extract_int(cmdbuf, 5);
581                 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
582                         usbuf.axlevel = newax;
583                 }
584         }
585         if (np > 7) {
586                 usbuf.lastcall = extract_long(cmdbuf, 7);
587         }
588         if (np > 8) {
589                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
590         }
591         CtdlPutUserLock(&usbuf);
592         if (usbuf.axlevel == AxDeleted) {
593                 if (purge_user(requested_user) == 0) {
594                         deleted = 1;
595                 }
596         }
597
598         if (deleted) {
599                 snprintf(notify, SIZ, 
600                         "User \"%s\" has been deleted by %s.\n",
601                         usbuf.fullname, (CC->logged_in ? CC->user.fullname : "an administrator")
602                 );
603                 CtdlAideMessage(notify, "User Deletion Message");
604         }
605
606         cprintf("%d Ok", CIT_OK);
607         if (deleted) {
608                 cprintf(" (%s deleted)", requested_user);
609         }
610         cprintf("\n");
611 }
612
613
614 // Citadel protocol command to do the same
615 void cmd_isme(char *argbuf) {
616         char addr[256];
617
618         if (CtdlAccessCheck(ac_logged_in)) return;
619         extract_token(addr, argbuf, 0, '|', sizeof addr);
620
621         if (CtdlIsMe(addr, sizeof addr)) {
622                 cprintf("%d %s\n", CIT_OK, addr);
623         }
624         else {
625                 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);
626         }
627
628 }
629
630
631 // Retrieve all Internet email addresses/aliases for the specified user
632 void cmd_agea(char *cmdbuf) {
633         struct ctdluser usbuf;
634         char requested_user[128];
635         int i, num_e;
636         char e[512];
637
638         if (CtdlAccessCheck(ac_aide)) {
639                 return;
640         }
641
642         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
643         if (CtdlGetUser(&usbuf, requested_user) != 0) {
644                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
645                 return;
646         }
647         cprintf("%d internet email addresses for %s\n", LISTING_FOLLOWS, usbuf.fullname);
648         num_e = num_tokens(usbuf.emailaddrs, '|');
649         for (i=0; i<num_e; ++i) {
650                 extract_token(e, usbuf.emailaddrs, i, '|', sizeof e);
651                 cprintf("%s\n", e);
652         }
653         cprintf("000\n");
654 }
655
656
657 // Set the Internet email addresses/aliases for the specified user
658 void cmd_asea(char *cmdbuf) {
659         struct ctdluser usbuf;
660         char requested_user[128];
661         char buf[SIZ];
662         char whodat[64];
663         char new_emailaddrs[512] = { 0 } ;
664
665         if (CtdlAccessCheck(ac_aide)) return;
666
667         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
668         if (CtdlGetUser(&usbuf, requested_user) != 0) {
669                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
670                 return;
671         }
672
673         cprintf("%d Ok\n", SEND_LISTING);
674         while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
675                 if (IsEmptyStr(buf)) {
676                         syslog(LOG_ERR, "user_ops: address <%s> is empty - not using", buf);
677                 }
678                 else if ((strlen(new_emailaddrs) + strlen(buf) + 2) > sizeof(new_emailaddrs)) {
679                         syslog(LOG_ERR, "user_ops: address <%s> does not fit in buffer - not using", buf);
680                 }
681                 else if (!IsDirectory(buf, 0)) {
682                         syslog(LOG_ERR, "user_ops: address <%s> is not in one of our domains - not using", buf);
683                 }
684                 else if ( (CtdlDirectoryLookup(whodat, buf, sizeof whodat) == 0) && (CtdlUserCmp(whodat, requested_user)) ) {
685                         syslog(LOG_ERR, "user_ops: address <%s> already belongs to <%s> - not using", buf, whodat);
686                 }
687                 else {
688                         syslog(LOG_DEBUG, "user_ops: address <%s> validated", buf);
689                         if (!IsEmptyStr(new_emailaddrs)) {
690                                 strcat(new_emailaddrs, "|");
691                         }
692                         strcat(new_emailaddrs, buf);
693                 }
694         }
695
696         CtdlSetEmailAddressesForUser(requested_user, new_emailaddrs);
697 }
698
699
700 // Set the preferred view for the current user/room combination
701 void cmd_view(char *cmdbuf) {
702         int requested_view;
703         struct visit vbuf;
704
705         if (CtdlAccessCheck(ac_logged_in)) {
706                 return;
707         }
708
709         requested_view = extract_int(cmdbuf, 0);
710
711         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
712         vbuf.v_view = requested_view;
713         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
714         
715         cprintf("%d ok\n", CIT_OK);
716 }
717
718
719 // Rename a user
720 void cmd_renu(char *cmdbuf) {
721         int retcode;
722         char oldname[USERNAME_SIZE];
723         char newname[USERNAME_SIZE];
724
725         if (CtdlAccessCheck(ac_aide)) {
726                 return;
727         }
728
729         extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
730         extract_token(newname, cmdbuf, 1, '|', sizeof newname);
731
732         retcode = rename_user(oldname, newname);
733         switch(retcode) {
734                 case RENAMEUSER_OK:
735                         cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
736                         return;
737                 case RENAMEUSER_LOGGED_IN:
738                         cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
739                                 ERROR + ALREADY_LOGGED_IN , oldname
740                         );
741                         return;
742                 case RENAMEUSER_NOT_FOUND:
743                         cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
744                         return;
745                 case RENAMEUSER_ALREADY_EXISTS:
746                         cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
747                         return;
748         }
749
750         cprintf("%d An unknown error occurred.\n", ERROR);
751 }
752
753
754 void cmd_quit(char *argbuf) {
755         cprintf("%d Goodbye.\n", CIT_OK);
756         CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
757 }
758
759
760 void cmd_lout(char *argbuf) {
761         if (CC->logged_in) 
762                 CtdlUserLogout();
763         cprintf("%d logged out.\n", CIT_OK);
764 }
765
766
767 void cmd_biff(char *argbuf) {                           // has new mail arrived?
768         cprintf("%d %d\n", CIT_OK, NewMailCount());
769 }
770
771
772 // Initialization function, called from modules_init.c
773 char *ctdl_module_init_serv_user(void) {
774         if (!threading) {
775                 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
776                 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
777                 CtdlRegisterProtoHook(cmd_quit, "QUIT", "log out and disconnect from server");
778                 CtdlRegisterProtoHook(cmd_lout, "LOUT", "log out but do not disconnect from server");
779                 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
780                 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
781                 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
782                 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
783                 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
784                 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
785                 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
786                 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
787                 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
788                 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
789                 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
790                 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
791                 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
792                 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
793                 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
794                 CtdlRegisterProtoHook(cmd_agea, "AGEA", "Administratively Get Email Addresses");
795                 CtdlRegisterProtoHook(cmd_asea, "ASEA", "Administratively Set Email Addresses");
796                 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
797                 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
798                 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
799                 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
800                 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
801                 CtdlRegisterProtoHook(cmd_isme, "ISME", "Determine whether an email address belongs to a user");
802                 CtdlRegisterProtoHook(cmd_biff, "BIFF", "Count new messages that have arrived in the inbox");
803         }
804         /* return our Subversion id for the Log */
805         return "user";
806 }