1 // Client-side support functions.
3 // Copyright (c) 1987-2016 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License version 3.
7 #include "textclient.h"
9 #define IFAIDE if(axlevel>=AxAideU)
10 #define IFNAIDE if (axlevel<AxAideU)
12 extern unsigned userflags;
13 extern char sigcaught;
14 extern char rc_floor_mode;
15 extern int rc_ansi_color;
16 extern int rc_prompt_control;
18 /* Destructive backspace */
19 void back(int spaces) {
21 for (a = 0; a < spaces; ++a) {
29 * Edit a user's Internet email addresses
31 void edit_user_internet_email_addresses(CtdlIPC * ipc, char *who) {
43 r = CtdlIPCAideGetEmailAddresses(ipc, who, emailaddrs, buf);
45 while (!IsEmptyStr(emailaddrs)) {
46 extract_token(buf, emailaddrs, 0, '\n', sizeof buf);
47 remove_token(emailaddrs, 0, '\n');
48 if (!IsEmptyStr(buf)) {
51 recs = malloc(sizeof(char *));
53 recs = realloc(recs, (sizeof(char *)) * num_recs);
54 recs[num_recs - 1] = malloc(strlen(buf) + 1);
55 strcpy(recs[num_recs - 1], buf);
63 scr_printf(" Internet email addresses for %s\n", who);
65 scr_printf("--- --------------------------------------------------\n");
66 for (i = 0; i < num_recs; ++i) {
68 scr_printf("%3d ", i + 1);
70 scr_printf("%s\n", recs[i]);
74 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
77 newprompt("Enter new email address: ", buf, 50);
79 if (!IsEmptyStr(buf)) {
80 // FIXME validate the email address (format, our own domain, addr does not belong to another user)
83 recs = malloc(sizeof(char *));
86 recs = realloc(recs, (sizeof(char *)) * num_recs);
88 recs[num_recs - 1] = strdup(buf);
93 i = intprompt("Delete which address", 1, 1, num_recs) - 1;
96 for (j = i; j < num_recs; ++j) {
97 recs[j] = recs[j + 1];
103 for (i = 0; i < num_recs; i++)
104 r += 1 + strlen(recs[i]);
105 resp = (char *) calloc(1, r);
107 scr_printf("Can't save config - out of memory!\n");
111 for (i = 0; i < num_recs; i++) {
112 strcat(resp, recs[i]);
115 r = CtdlIPCAideSetEmailAddresses(ipc, who, resp, buf);
117 scr_printf("%s\n", buf);
120 scr_printf("Saved %d addresses.\n", num_recs);
127 quitting = !modified || boolprompt("Quit without saving", 0);
135 for (i = 0; i < num_recs; ++i)
143 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
145 void edituser(CtdlIPC * ipc, int cmd) {
147 char who[USERNAME_SIZE];
148 char newname[USERNAME_SIZE];
149 struct ctdluser *user = NULL;
151 int r; /* IPC response code */
156 newprompt("User name: ", who, 29);
157 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
158 scr_printf("%s\n", buf);
160 scr_printf("Do you want to create this user? ");
162 r = CtdlIPCCreateUser(ipc, who, 0, buf);
167 scr_printf("%s\n", buf);
174 if (cmd == 25) { // user edit
176 /* val_user(ipc, user->fullname, 0); we used to display the vCard here but there's really no need */
180 while (change_name == 1) {
181 if (boolprompt("Change name", 0)) {
182 strprompt("New name", newname, USERNAME_SIZE - 1);
183 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
185 scr_printf("%s\n", buf);
188 strcpy(user->fullname, newname);
198 if (newnow || boolprompt("Change password", 0)) {
199 strprompt("Password", user->password, -19);
202 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
203 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET))) {
204 user->flags |= US_INTERNET;
207 user->flags &= ~US_INTERNET;
209 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS))) {
210 user->flags &= ~US_REGIS;
213 user->flags |= US_REGIS;
215 user->lastcall = boolprompt("Set last login to now", 0) ? time(NULL) : user->lastcall;
216 user->USuserpurge = intprompt("Purge time (in days, 0 for system default", user->USuserpurge, 0, INT_MAX);
220 scr_printf("Do you want to delete this user? ");
225 user->axlevel = AxDeleted;
228 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
230 scr_printf("%s\n", buf);
234 if (cmd == 25) { // user edit
235 if (boolprompt("Edit this user's Internet email addresses", 0)) {
236 edit_user_internet_email_addresses(ipc, who);
243 /* Display a prompt and flip a bit based on whether the user answers
244 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
245 * in which case No=1 and Yes=0.
247 int set_attr(CtdlIPC * ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards) {
253 scr_printf("%50s ", prompt);
256 color(BRIGHT_MAGENTA);
259 scr_printf("%3s", ((temp & sbit) ? "No" : "Yes"));
262 scr_printf("%3s", ((temp & sbit) ? "Yes" : "No"));
277 temp = (temp | sbit);
279 temp = (temp ^ sbit);
284 * modes are: 0 - .EC command, 1 - .EC for new user,
285 * 2 - toggle Xpert mode 3 - toggle floor mode
287 void enter_config(CtdlIPC * ipc, int mode) {
289 struct ctdluser *user = NULL;
290 int r; /* IPC response code */
292 r = CtdlIPCGetConfig(ipc, &user, buf);
294 scr_printf("%s\n", buf);
299 if (mode == 0 || mode == 1) {
301 user->flags = set_attr(ipc, user->flags, "Are you an experienced Citadel user", US_EXPERT, 0);
302 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
307 user->flags = set_attr(ipc, user->flags, "Print last old message on New message request", US_LASTOLD, 0);
309 user->flags = set_attr(ipc, user->flags, "Prompt after each message", US_NOPROMPT, 1);
311 if ((user->flags & US_NOPROMPT) == 0) {
312 user->flags = set_attr(ipc, user->flags, "Use 'disappearing' prompts", US_DISAPPEAR, 0);
315 user->flags = set_attr(ipc, user->flags, "Pause after each screenful of text", US_PAGINATOR, 0);
317 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
318 user->flags = set_attr(ipc, user->flags, "<N>ext and <S>top work at paginator prompt", US_PROMPTCTL, 0);
321 if (rc_floor_mode == RC_DEFAULT) {
322 user->flags = set_attr(ipc, user->flags, "View rooms by floor", US_FLOORS, 0);
325 if (rc_ansi_color == 3) {
326 user->flags = set_attr(ipc, user->flags, "Enable color support", US_COLOR, 0);
329 if ((user->flags & US_EXPERT) == 0) {
330 formout(ipc, "unlisted");
333 user->flags = set_attr(ipc, user->flags, "Be unlisted in userlog", US_UNLISTED, 0);
335 if (!IsEmptyStr(editor_path)) {
336 user->flags = set_attr(ipc,
337 user->flags, "Always enter messages with the full-screen editor", US_EXTEDIT, 0);
343 if (user->flags & US_EXPERT) {
344 user->flags ^= US_EXPERT;
345 scr_printf("Expert mode now OFF\n");
348 user->flags |= US_EXPERT;
349 scr_printf("Expert mode now ON\n");
354 if (user->flags & US_FLOORS) {
355 user->flags ^= US_FLOORS;
356 scr_printf("Floor mode now OFF\n");
359 user->flags |= US_FLOORS;
360 scr_printf("Floor mode now ON\n");
364 r = CtdlIPCSetConfig(ipc, user, buf);
366 scr_printf("%s\n", buf);
367 userflags = user->flags;
372 * getstring() - get a line of text from a file
373 * ignores lines beginning with "#"
375 int getstring(FILE * fp, char *string) {
389 } while (string[0] == '#');
390 return (strlen(string));
394 /* Searches for patn in search string */
395 int pattern(char *search, char *patn) {
399 for (a = 0; !IsEmptyStr(&search[a]); ++a) {
400 b = strncasecmp(&search[a], patn, len);
408 void strproc(char *string) {
411 if (IsEmptyStr(string))
414 /* Convert non-printable characters to blanks */
415 for (a = 0; !IsEmptyStr(&string[a]); ++a) {
422 /* Remove leading and trailing blanks */
423 while (string[0] < 33)
424 strcpy(string, &string[1]);
425 while (string[strlen(string) - 1] < 33)
426 string[strlen(string) - 1] = 0;
428 /* Remove double blanks */
429 for (a = 0; a < strlen(string); ++a) {
430 if ((string[a] == 32) && (string[a + 1] == 32)) {
431 strcpy(&string[a], &string[a + 1]);
436 /* remove characters which would interfere with the network */
437 for (a = 0; a < strlen(string); ++a) {
438 if (string[a] == '!')
439 strcpy(&string[a], &string[a + 1]);
440 if (string[a] == '@')
441 strcpy(&string[a], &string[a + 1]);
442 if (string[a] == '_')
443 strcpy(&string[a], &string[a + 1]);
444 if (string[a] == ',')
445 strcpy(&string[a], &string[a + 1]);
446 if (string[a] == '%')
447 strcpy(&string[a], &string[a + 1]);
448 if (string[a] == '|')
449 strcpy(&string[a], &string[a + 1]);
455 void progress(CtdlIPC * ipc, unsigned long curr, unsigned long cmax) {
456 static char dots[] = "**************************************************";
457 char dots_printed[51];
462 scr_printf("\r%79s\r", "");
465 /* a will be range 0-50 rather than 0-100 */
466 a = (curr * 50) / cmax;
467 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
468 strncpy(dots_printed, dots, a);
470 scr_printf(fmt, dots_printed, "", curr * 100 / cmax, curr, cmax);
477 * NOT the same locate_host() in locate_host.c. This one just does a
478 * 'who am i' to try to discover where the user is...
480 void locate_host(CtdlIPC * ipc, char *hbuf) {
481 FILE *who = (FILE *) popen("who am i", "r");
483 strcpy(hbuf, ipc->ServInfo.fqdn);
486 fgets(hbuf, SIZ, who);
487 if (hbuf[strlen(hbuf) - 1] == '\n') {
488 hbuf[strlen(hbuf) - 1] = 0;
491 stripallbut(hbuf, '(', ')');
495 * miscellaneous server commands (testing, etc.)
497 void misc_server_cmd(CtdlIPC * ipc, char *cmd) {
500 CtdlIPC_chat_send(ipc, cmd);
501 CtdlIPC_chat_recv(ipc, buf);
502 scr_printf("%s\n", buf);
504 set_keepalives(KA_HALF);
505 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
506 scr_printf("%s\n", buf);
508 set_keepalives(KA_YES);
513 newprompt("> ", buf, 255);
514 CtdlIPC_chat_send(ipc, buf);
515 } while (strcmp(buf, "000"));
522 * compute the checksum of a file
524 int file_checksum(char *filename) {
529 fp = fopen(filename, "r");
533 /* yes, this algorithm may allow cksum to overflow, but that's ok
534 * as long as it overflows consistently, which it will.
536 while (ch = getc(fp), ch >= 0) {
537 cksum = (cksum + ch);
545 * nuke a directory and its contents
547 int nukedir(char *dirname) {
552 dp = opendir(dirname);
557 while (d = readdir(dp), d != NULL) {
558 snprintf(filename, sizeof filename, "%s/%s", dirname, d->d_name);
563 return (rmdir(dirname));