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');
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);
108 scr_printf(" Internet email addresses for %s\n", who);
110 scr_printf("--- --------------------------------------------------\n");
111 for (i=0; i<num_recs; ++i) {
113 scr_printf("%3d ", i+1);
115 scr_printf("%s\n", recs[i]);
119 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
122 newprompt("Enter new email address: ", buf, 50);
124 if (!IsEmptyStr(buf)) {
125 // FIXME validate the email address (format, our own domain, addr does not belong to another user)
128 recs = malloc(sizeof(char *));
131 recs = realloc(recs, (sizeof(char *)) * num_recs);
133 recs[num_recs-1] = strdup(buf);
138 i = intprompt("Delete which address", 1, 1, num_recs) - 1;
141 for (j=i; j<num_recs; ++j) {
148 for (i = 0; i < num_recs; i++)
149 r += 1 + strlen(recs[i]);
150 resp = (char *)calloc(1, r);
152 scr_printf("Can't save config - out of memory!\n");
155 if (num_recs) for (i = 0; i < num_recs; i++) {
156 strcat(resp, recs[i]);
160 //r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
161 //if (r / 100 != 4) {
162 //scr_printf("%s\n", buf);
164 //scr_printf("Wrote %d records.\n", num_recs);
167 scr_printf("<%s>\n", resp);
174 quitting = !modified || boolprompt("Quit without saving", 0);
182 for (i=0; i<num_recs; ++i) free(recs[i]);
189 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
191 void edituser(CtdlIPC *ipc, int cmd)
194 char who[USERNAME_SIZE];
195 char newname[USERNAME_SIZE];
196 struct ctdluser *user = NULL;
198 int r; /* IPC response code */
203 newprompt("User name: ", who, 29);
204 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
205 scr_printf("%s\n", buf);
207 scr_printf("Do you want to create this user? ");
209 r = CtdlIPCCreateUser(ipc, who, 0, buf);
214 scr_printf("%s\n", buf);
221 if (cmd == 25) { // user edit
223 /* val_user(ipc, user->fullname, 0); we used to display the vCard here but there's really no need */
227 while (change_name == 1) {
228 if (boolprompt("Change name", 0)) {
229 strprompt("New name", newname, USERNAME_SIZE-1);
230 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
232 scr_printf("%s\n", buf);
235 strcpy(user->fullname, newname);
245 if (newnow || boolprompt("Change password", 0)) {
246 strprompt("Password", user->password, -19);
249 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
250 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET))) {
251 user->flags |= US_INTERNET;
254 user->flags &= ~US_INTERNET;
256 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS))) {
257 user->flags &= ~US_REGIS;
260 user->flags |= US_REGIS;
262 user->timescalled = intprompt("Times called", user->timescalled, 0, INT_MAX);
263 user->posted = intprompt("Messages posted", user->posted, 0, INT_MAX);
264 user->lastcall = boolprompt("Set last login to now", 0) ? time(NULL) : user->lastcall;
265 user->USuserpurge = intprompt("Purge time (in days, 0 for system default", user->USuserpurge, 0, INT_MAX);
269 scr_printf("Do you want to delete this user? ");
274 user->axlevel = AxDeleted;
277 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
279 scr_printf("%s\n", buf);
283 if (boolprompt("Edit this user's Internet email addresses", 0)) {
284 edit_user_internet_email_addresses(ipc, who);
290 /* Display a prompt and flip a bit based on whether the user answers
291 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
292 * in which case No=1 and Yes=0.
294 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
301 scr_printf("%50s ", prompt);
304 color(BRIGHT_MAGENTA);
307 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
310 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
318 if (backwards) a = 1 - a;
320 if (backwards) a = 1 - a;
323 if (!a) temp = (temp^sbit);
328 * modes are: 0 - .EC command, 1 - .EC for new user,
329 * 2 - toggle Xpert mode 3 - toggle floor mode
331 void enter_config(CtdlIPC *ipc, int mode)
334 struct ctdluser *user = NULL;
335 int r; /* IPC response code */
337 r = CtdlIPCGetConfig(ipc, &user, buf);
339 scr_printf("%s\n", buf);
344 if (mode == 0 || mode == 1) {
346 user->flags = set_attr(ipc, user->flags,
347 "Are you an experienced Citadel user",
349 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
354 user->flags = set_attr(
357 "Print last old message on New message request",
362 user->flags = set_attr(
365 "Prompt after each message",
370 if ((user->flags & US_NOPROMPT) == 0) {
371 user->flags = set_attr(
374 "Use 'disappearing' prompts",
380 user->flags = set_attr(
383 "Pause after each screenful of text",
388 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
389 user->flags = set_attr(
392 "<N>ext and <S>top work at paginator prompt",
398 if (rc_floor_mode == RC_DEFAULT) {
399 user->flags = set_attr(
402 "View rooms by floor",
408 if (rc_ansi_color == 3) {
409 user->flags = set_attr(
412 "Enable color support",
418 if ((user->flags & US_EXPERT) == 0) {
419 formout(ipc, "unlisted");
422 user->flags = set_attr(
425 "Be unlisted in userlog",
430 if (!IsEmptyStr(editor_path)) {
431 user->flags = set_attr(
434 "Always enter messages with the full-screen editor",
443 if (user->flags & US_EXPERT) {
444 user->flags ^= US_EXPERT;
445 scr_printf("Expert mode now OFF\n");
447 user->flags |= US_EXPERT;
448 scr_printf("Expert mode now ON\n");
453 if (user->flags & US_FLOORS) {
454 user->flags ^= US_FLOORS;
455 scr_printf("Floor mode now OFF\n");
457 user->flags |= US_FLOORS;
458 scr_printf("Floor mode now ON\n");
462 r = CtdlIPCSetConfig(ipc, user, buf);
463 if (r / 100 != 2) scr_printf("%s\n", buf);
464 userflags = user->flags;
469 * getstring() - get a line of text from a file
470 * ignores lines beginning with "#"
472 int getstring(FILE *fp, char *string)
487 } while(string[0]=='#');
488 return(strlen(string));
492 /* Searches for patn in search string */
493 int pattern(char *search, char *patn) {
497 for (a=0; !IsEmptyStr(&search[a]); ++a) {
498 b=strncasecmp(&search[a],patn,len);
505 void strproc(char *string)
509 if (IsEmptyStr(string)) return;
511 /* Convert non-printable characters to blanks */
512 for (a=0; !IsEmptyStr(&string[a]); ++a) {
513 if (string[a]<32) string[a]=32;
514 if (string[a]>126) string[a]=32;
517 /* Remove leading and trailing blanks */
518 while(string[0]<33) strcpy(string,&string[1]);
519 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
521 /* Remove double blanks */
522 for (a=0; a<strlen(string); ++a) {
523 if ((string[a]==32)&&(string[a+1]==32)) {
524 strcpy(&string[a],&string[a+1]);
529 /* remove characters which would interfere with the network */
530 for (a=0; a<strlen(string); ++a) {
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]);
534 if (string[a]==',') strcpy(&string[a],&string[a+1]);
535 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
536 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
542 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
545 "**************************************************";
546 char dots_printed[51];
551 scr_printf("\r%79s\r","");
553 /* a will be range 0-50 rather than 0-100 */
554 a=(curr * 50) / cmax;
555 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
556 strncpy(dots_printed, dots, a);
558 scr_printf(fmt, dots_printed, "",
559 curr * 100 / cmax, curr, cmax);
566 * NOT the same locate_host() in locate_host.c. This one just does a
567 * 'who am i' to try to discover where the user is...
569 void locate_host(CtdlIPC* ipc, char *hbuf)
571 FILE *who = (FILE *)popen("who am i","r");
573 strcpy(hbuf, ipc->ServInfo.fqdn);
576 fgets(hbuf, SIZ, who);
578 stripallbut(hbuf, '(' , ')' );
582 * miscellaneous server commands (testing, etc.)
584 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
587 CtdlIPC_chat_send(ipc, cmd);
588 CtdlIPC_chat_recv(ipc, buf);
589 scr_printf("%s\n",buf);
591 set_keepalives(KA_HALF);
592 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
593 scr_printf("%s\n",buf);
595 set_keepalives(KA_YES);
600 newprompt("> ",buf,255);
601 CtdlIPC_chat_send(ipc, buf);
602 } while(strcmp(buf,"000"));
609 * compute the checksum of a file
611 int file_checksum(char *filename)
617 fp = fopen(filename,"r");
618 if (fp == NULL) return(0);
620 /* yes, this algorithm may allow cksum to overflow, but that's ok
621 * as long as it overflows consistently, which it will.
623 while (ch=getc(fp), ch>=0) {
624 cksum = (cksum + ch);
632 * nuke a directory and its contents
634 int nukedir(char *dirname)
640 dp = opendir(dirname);
645 while (d = readdir(dp), d != NULL) {
646 snprintf(filename, sizeof filename, "%s/%s",
652 return(rmdir(dirname));