2 * Client-side support functions.
4 * Copyright (c) 1987-2016 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
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.
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
44 #include <libcitadel.h>
45 #include "citadel_ipc.h"
52 #include "citadel_decls.h"
53 #include "routines2.h"
54 #include "tuiconfig.h"
56 #define IFAIDE if(axlevel>=AxAideU)
57 #define IFNAIDE if (axlevel<AxAideU)
59 extern unsigned userflags;
60 extern char sigcaught;
61 extern char rc_floor_mode;
62 extern int rc_ansi_color;
63 extern int rc_prompt_control;
65 /* Destructive backspace */
66 void back(int spaces) {
68 for (a=0; a<spaces; ++a) {
76 * Edit a user's Internet email addresses
78 void edit_user_internet_email_addresses(CtdlIPC *ipc, char *who)
91 r = CtdlIPCAideGetEmailAddresses(ipc, who, emailaddrs, buf);
93 while (!IsEmptyStr(emailaddrs)) {
94 extract_token(buf, emailaddrs, 0, '\n', sizeof buf);
95 remove_token(emailaddrs, 0, '\n');
96 if (!IsEmptyStr(buf)) {
98 if (num_recs == 1) recs = malloc(sizeof(char *));
99 else recs = realloc(recs, (sizeof(char *)) * num_recs);
100 recs[num_recs-1] = malloc(strlen(buf) + 1);
101 strcpy(recs[num_recs-1], buf);
109 scr_printf(" Internet email addresses for %s\n", who);
111 scr_printf("--- --------------------------------------------------\n");
112 for (i=0; i<num_recs; ++i) {
114 scr_printf("%3d ", i+1);
116 scr_printf("%s\n", recs[i]);
120 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
123 newprompt("Enter new email address: ", buf, 50);
125 if (!IsEmptyStr(buf)) {
126 // FIXME validate the email address (format, our own domain, addr does not belong to another user)
129 recs = malloc(sizeof(char *));
132 recs = realloc(recs, (sizeof(char *)) * num_recs);
134 recs[num_recs-1] = strdup(buf);
139 i = intprompt("Delete which address", 1, 1, num_recs) - 1;
142 for (j=i; j<num_recs; ++j) {
149 for (i = 0; i < num_recs; i++)
150 r += 1 + strlen(recs[i]);
151 resp = (char *)calloc(1, r);
153 scr_printf("Can't save config - out of memory!\n");
156 if (num_recs) for (i = 0; i < num_recs; i++) {
157 strcat(resp, recs[i]);
160 r = CtdlIPCAideSetEmailAddresses(ipc, who, resp, buf);
162 scr_printf("%s\n", buf);
164 scr_printf("Saved %d addresses.\n", num_recs);
171 quitting = !modified || boolprompt("Quit without saving", 0);
179 for (i=0; i<num_recs; ++i) free(recs[i]);
186 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
188 void edituser(CtdlIPC *ipc, int cmd)
191 char who[USERNAME_SIZE];
192 char newname[USERNAME_SIZE];
193 struct ctdluser *user = NULL;
195 int r; /* IPC response code */
200 newprompt("User name: ", who, 29);
201 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
202 scr_printf("%s\n", buf);
204 scr_printf("Do you want to create this user? ");
206 r = CtdlIPCCreateUser(ipc, who, 0, buf);
211 scr_printf("%s\n", buf);
218 if (cmd == 25) { // user edit
220 /* val_user(ipc, user->fullname, 0); we used to display the vCard here but there's really no need */
224 while (change_name == 1) {
225 if (boolprompt("Change name", 0)) {
226 strprompt("New name", newname, USERNAME_SIZE-1);
227 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
229 scr_printf("%s\n", buf);
232 strcpy(user->fullname, newname);
242 if (newnow || boolprompt("Change password", 0)) {
243 strprompt("Password", user->password, -19);
246 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
247 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET))) {
248 user->flags |= US_INTERNET;
251 user->flags &= ~US_INTERNET;
253 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS))) {
254 user->flags &= ~US_REGIS;
257 user->flags |= US_REGIS;
259 user->timescalled = intprompt("Times called", user->timescalled, 0, INT_MAX);
260 user->posted = intprompt("Messages posted", user->posted, 0, INT_MAX);
261 user->lastcall = boolprompt("Set last login to now", 0) ? time(NULL) : user->lastcall;
262 user->USuserpurge = intprompt("Purge time (in days, 0 for system default", user->USuserpurge, 0, INT_MAX);
266 scr_printf("Do you want to delete this user? ");
271 user->axlevel = AxDeleted;
274 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
276 scr_printf("%s\n", buf);
280 if (boolprompt("Edit this user's Internet email addresses", 0)) {
281 edit_user_internet_email_addresses(ipc, who);
287 /* Display a prompt and flip a bit based on whether the user answers
288 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
289 * in which case No=1 and Yes=0.
291 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
298 scr_printf("%50s ", prompt);
301 color(BRIGHT_MAGENTA);
304 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
307 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
315 if (backwards) a = 1 - a;
317 if (backwards) a = 1 - a;
320 if (!a) temp = (temp^sbit);
325 * modes are: 0 - .EC command, 1 - .EC for new user,
326 * 2 - toggle Xpert mode 3 - toggle floor mode
328 void enter_config(CtdlIPC *ipc, int mode)
331 struct ctdluser *user = NULL;
332 int r; /* IPC response code */
334 r = CtdlIPCGetConfig(ipc, &user, buf);
336 scr_printf("%s\n", buf);
341 if (mode == 0 || mode == 1) {
343 user->flags = set_attr(ipc, user->flags,
344 "Are you an experienced Citadel user",
346 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
351 user->flags = set_attr(
354 "Print last old message on New message request",
359 user->flags = set_attr(
362 "Prompt after each message",
367 if ((user->flags & US_NOPROMPT) == 0) {
368 user->flags = set_attr(
371 "Use 'disappearing' prompts",
377 user->flags = set_attr(
380 "Pause after each screenful of text",
385 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
386 user->flags = set_attr(
389 "<N>ext and <S>top work at paginator prompt",
395 if (rc_floor_mode == RC_DEFAULT) {
396 user->flags = set_attr(
399 "View rooms by floor",
405 if (rc_ansi_color == 3) {
406 user->flags = set_attr(
409 "Enable color support",
415 if ((user->flags & US_EXPERT) == 0) {
416 formout(ipc, "unlisted");
419 user->flags = set_attr(
422 "Be unlisted in userlog",
427 if (!IsEmptyStr(editor_path)) {
428 user->flags = set_attr(
431 "Always enter messages with the full-screen editor",
440 if (user->flags & US_EXPERT) {
441 user->flags ^= US_EXPERT;
442 scr_printf("Expert mode now OFF\n");
444 user->flags |= US_EXPERT;
445 scr_printf("Expert mode now ON\n");
450 if (user->flags & US_FLOORS) {
451 user->flags ^= US_FLOORS;
452 scr_printf("Floor mode now OFF\n");
454 user->flags |= US_FLOORS;
455 scr_printf("Floor mode now ON\n");
459 r = CtdlIPCSetConfig(ipc, user, buf);
460 if (r / 100 != 2) scr_printf("%s\n", buf);
461 userflags = user->flags;
466 * getstring() - get a line of text from a file
467 * ignores lines beginning with "#"
469 int getstring(FILE *fp, char *string)
484 } while(string[0]=='#');
485 return(strlen(string));
489 /* Searches for patn in search string */
490 int pattern(char *search, char *patn) {
494 for (a=0; !IsEmptyStr(&search[a]); ++a) {
495 b=strncasecmp(&search[a],patn,len);
502 void strproc(char *string)
506 if (IsEmptyStr(string)) return;
508 /* Convert non-printable characters to blanks */
509 for (a=0; !IsEmptyStr(&string[a]); ++a) {
510 if (string[a]<32) string[a]=32;
511 if (string[a]>126) string[a]=32;
514 /* Remove leading and trailing blanks */
515 while(string[0]<33) strcpy(string,&string[1]);
516 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
518 /* Remove double blanks */
519 for (a=0; a<strlen(string); ++a) {
520 if ((string[a]==32)&&(string[a+1]==32)) {
521 strcpy(&string[a],&string[a+1]);
526 /* remove characters which would interfere with the network */
527 for (a=0; a<strlen(string); ++a) {
528 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
529 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
530 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
531 if (string[a]==',') strcpy(&string[a],&string[a+1]);
532 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
533 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
539 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
542 "**************************************************";
543 char dots_printed[51];
548 scr_printf("\r%79s\r","");
550 /* a will be range 0-50 rather than 0-100 */
551 a=(curr * 50) / cmax;
552 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
553 strncpy(dots_printed, dots, a);
555 scr_printf(fmt, dots_printed, "",
556 curr * 100 / cmax, curr, cmax);
563 * NOT the same locate_host() in locate_host.c. This one just does a
564 * 'who am i' to try to discover where the user is...
566 void locate_host(CtdlIPC* ipc, char *hbuf)
568 FILE *who = (FILE *)popen("who am i","r");
570 strcpy(hbuf, ipc->ServInfo.fqdn);
573 fgets(hbuf, SIZ, who);
575 stripallbut(hbuf, '(' , ')' );
579 * miscellaneous server commands (testing, etc.)
581 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
584 CtdlIPC_chat_send(ipc, cmd);
585 CtdlIPC_chat_recv(ipc, buf);
586 scr_printf("%s\n",buf);
588 set_keepalives(KA_HALF);
589 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
590 scr_printf("%s\n",buf);
592 set_keepalives(KA_YES);
597 newprompt("> ",buf,255);
598 CtdlIPC_chat_send(ipc, buf);
599 } while(strcmp(buf,"000"));
606 * compute the checksum of a file
608 int file_checksum(char *filename)
614 fp = fopen(filename,"r");
615 if (fp == NULL) return(0);
617 /* yes, this algorithm may allow cksum to overflow, but that's ok
618 * as long as it overflows consistently, which it will.
620 while (ch=getc(fp), ch>=0) {
621 cksum = (cksum + ch);
629 * nuke a directory and its contents
631 int nukedir(char *dirname)
637 dp = opendir(dirname);
642 while (d = readdir(dp), d != NULL) {
643 snprintf(filename, sizeof filename, "%s/%s",
649 return(rmdir(dirname));