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