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