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.
15 #include "textclient.h"
17 #define IFAIDE if(axlevel>=AxAideU)
18 #define IFNAIDE if (axlevel<AxAideU)
20 extern unsigned userflags;
21 extern char sigcaught;
22 extern char rc_floor_mode;
23 extern int rc_ansi_color;
24 extern int rc_prompt_control;
26 /* Destructive backspace */
30 for (a = 0; a < spaces; ++a) {
38 * Edit a user's Internet email addresses
40 void edit_user_internet_email_addresses(CtdlIPC * ipc, char *who)
53 r = CtdlIPCAideGetEmailAddresses(ipc, who, emailaddrs, buf);
55 while (!IsEmptyStr(emailaddrs)) {
56 extract_token(buf, emailaddrs, 0, '\n', sizeof buf);
57 remove_token(emailaddrs, 0, '\n');
58 if (!IsEmptyStr(buf)) {
61 recs = malloc(sizeof(char *));
63 recs = realloc(recs, (sizeof(char *)) * num_recs);
64 recs[num_recs - 1] = malloc(strlen(buf) + 1);
65 strcpy(recs[num_recs - 1], buf);
73 scr_printf(" Internet email addresses for %s\n", who);
75 scr_printf("--- --------------------------------------------------\n");
76 for (i = 0; i < num_recs; ++i) {
78 scr_printf("%3d ", i + 1);
80 scr_printf("%s\n", recs[i]);
84 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
87 newprompt("Enter new email address: ", buf, 50);
89 if (!IsEmptyStr(buf)) {
90 // FIXME validate the email address (format, our own domain, addr does not belong to another user)
93 recs = malloc(sizeof(char *));
95 recs = realloc(recs, (sizeof(char *)) * num_recs);
97 recs[num_recs - 1] = strdup(buf);
102 i = intprompt("Delete which address", 1, 1, num_recs) - 1;
105 for (j = i; j < num_recs; ++j) {
106 recs[j] = recs[j + 1];
112 for (i = 0; i < num_recs; i++)
113 r += 1 + strlen(recs[i]);
114 resp = (char *) calloc(1, r);
116 scr_printf("Can't save config - out of memory!\n");
120 for (i = 0; i < num_recs; i++) {
121 strcat(resp, recs[i]);
124 r = CtdlIPCAideSetEmailAddresses(ipc, who, resp, buf);
126 scr_printf("%s\n", buf);
128 scr_printf("Saved %d addresses.\n", num_recs);
135 quitting = !modified || boolprompt("Quit without saving", 0);
143 for (i = 0; i < num_recs; ++i)
151 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
153 void edituser(CtdlIPC * ipc, int cmd)
156 char who[USERNAME_SIZE];
157 char newname[USERNAME_SIZE];
158 struct ctdluser *user = NULL;
160 int r; /* IPC response code */
165 newprompt("User name: ", who, 29);
166 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
167 scr_printf("%s\n", buf);
169 scr_printf("Do you want to create this user? ");
171 r = CtdlIPCCreateUser(ipc, who, 0, buf);
176 scr_printf("%s\n", buf);
183 if (cmd == 25) { // user edit
185 /* val_user(ipc, user->fullname, 0); we used to display the vCard here but there's really no need */
189 while (change_name == 1) {
190 if (boolprompt("Change name", 0)) {
191 strprompt("New name", newname, USERNAME_SIZE - 1);
192 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
194 scr_printf("%s\n", buf);
196 strcpy(user->fullname, newname);
205 if (newnow || boolprompt("Change password", 0)) {
206 strprompt("Password", user->password, -19);
209 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
210 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET))) {
211 user->flags |= US_INTERNET;
213 user->flags &= ~US_INTERNET;
215 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS))) {
216 user->flags &= ~US_REGIS;
218 user->flags |= US_REGIS;
220 user->timescalled = intprompt("Times called", user->timescalled, 0, INT_MAX);
221 user->posted = intprompt("Messages posted", user->posted, 0, INT_MAX);
222 user->lastcall = boolprompt("Set last login to now", 0) ? time(NULL) : user->lastcall;
223 user->USuserpurge = intprompt("Purge time (in days, 0 for system default", user->USuserpurge, 0, INT_MAX);
227 scr_printf("Do you want to delete this user? ");
232 user->axlevel = AxDeleted;
235 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
237 scr_printf("%s\n", buf);
241 if (boolprompt("Edit this user's Internet email addresses", 0)) {
242 edit_user_internet_email_addresses(ipc, who);
248 /* Display a prompt and flip a bit based on whether the user answers
249 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
250 * in which case No=1 and Yes=0.
252 int set_attr(CtdlIPC * ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
259 scr_printf("%50s ", prompt);
262 color(BRIGHT_MAGENTA);
265 scr_printf("%3s", ((temp & sbit) ? "No" : "Yes"));
267 scr_printf("%3s", ((temp & sbit) ? "Yes" : "No"));
282 temp = (temp | sbit);
284 temp = (temp ^ sbit);
289 * modes are: 0 - .EC command, 1 - .EC for new user,
290 * 2 - toggle Xpert mode 3 - toggle floor mode
292 void enter_config(CtdlIPC * ipc, int mode)
295 struct ctdluser *user = NULL;
296 int r; /* IPC response code */
298 r = CtdlIPCGetConfig(ipc, &user, buf);
300 scr_printf("%s\n", buf);
305 if (mode == 0 || mode == 1) {
307 user->flags = set_attr(ipc, user->flags, "Are you an experienced Citadel user", US_EXPERT, 0);
308 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
313 user->flags = set_attr(ipc, user->flags, "Print last old message on New message request", US_LASTOLD, 0);
315 user->flags = set_attr(ipc, user->flags, "Prompt after each message", US_NOPROMPT, 1);
317 if ((user->flags & US_NOPROMPT) == 0) {
318 user->flags = set_attr(ipc, user->flags, "Use 'disappearing' prompts", US_DISAPPEAR, 0);
321 user->flags = set_attr(ipc, user->flags, "Pause after each screenful of text", US_PAGINATOR, 0);
323 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
324 user->flags = set_attr(ipc, user->flags, "<N>ext and <S>top work at paginator prompt", US_PROMPTCTL, 0);
327 if (rc_floor_mode == RC_DEFAULT) {
328 user->flags = set_attr(ipc, user->flags, "View rooms by floor", US_FLOORS, 0);
331 if (rc_ansi_color == 3) {
332 user->flags = set_attr(ipc, user->flags, "Enable color support", US_COLOR, 0);
335 if ((user->flags & US_EXPERT) == 0) {
336 formout(ipc, "unlisted");
339 user->flags = set_attr(ipc, user->flags, "Be unlisted in userlog", US_UNLISTED, 0);
341 if (!IsEmptyStr(editor_path)) {
342 user->flags = set_attr(ipc,
343 user->flags, "Always enter messages with the full-screen editor", US_EXTEDIT, 0);
349 if (user->flags & US_EXPERT) {
350 user->flags ^= US_EXPERT;
351 scr_printf("Expert mode now OFF\n");
353 user->flags |= US_EXPERT;
354 scr_printf("Expert mode now ON\n");
359 if (user->flags & US_FLOORS) {
360 user->flags ^= US_FLOORS;
361 scr_printf("Floor mode now OFF\n");
363 user->flags |= US_FLOORS;
364 scr_printf("Floor mode now ON\n");
368 r = CtdlIPCSetConfig(ipc, user, buf);
370 scr_printf("%s\n", buf);
371 userflags = user->flags;
376 * getstring() - get a line of text from a file
377 * ignores lines beginning with "#"
379 int getstring(FILE * fp, char *string)
394 } while (string[0] == '#');
395 return (strlen(string));
399 /* Searches for patn in search string */
400 int pattern(char *search, char *patn)
405 for (a = 0; !IsEmptyStr(&search[a]); ++a) {
406 b = strncasecmp(&search[a], patn, len);
414 void strproc(char *string)
418 if (IsEmptyStr(string))
421 /* Convert non-printable characters to blanks */
422 for (a = 0; !IsEmptyStr(&string[a]); ++a) {
429 /* Remove leading and trailing blanks */
430 while (string[0] < 33)
431 strcpy(string, &string[1]);
432 while (string[strlen(string) - 1] < 33)
433 string[strlen(string) - 1] = 0;
435 /* Remove double blanks */
436 for (a = 0; a < strlen(string); ++a) {
437 if ((string[a] == 32) && (string[a + 1] == 32)) {
438 strcpy(&string[a], &string[a + 1]);
443 /* remove characters which would interfere with the network */
444 for (a = 0; a < strlen(string); ++a) {
445 if (string[a] == '!')
446 strcpy(&string[a], &string[a + 1]);
447 if (string[a] == '@')
448 strcpy(&string[a], &string[a + 1]);
449 if (string[a] == '_')
450 strcpy(&string[a], &string[a + 1]);
451 if (string[a] == ',')
452 strcpy(&string[a], &string[a + 1]);
453 if (string[a] == '%')
454 strcpy(&string[a], &string[a + 1]);
455 if (string[a] == '|')
456 strcpy(&string[a], &string[a + 1]);
462 void progress(CtdlIPC * ipc, unsigned long curr, unsigned long cmax)
464 static char dots[] = "**************************************************";
465 char dots_printed[51];
470 scr_printf("\r%79s\r", "");
472 /* a will be range 0-50 rather than 0-100 */
473 a = (curr * 50) / cmax;
474 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
475 strncpy(dots_printed, dots, a);
477 scr_printf(fmt, dots_printed, "", curr * 100 / cmax, curr, cmax);
484 * NOT the same locate_host() in locate_host.c. This one just does a
485 * 'who am i' to try to discover where the user is...
487 void locate_host(CtdlIPC * ipc, char *hbuf)
489 FILE *who = (FILE *) popen("who am i", "r");
491 strcpy(hbuf, ipc->ServInfo.fqdn);
494 fgets(hbuf, SIZ, who);
496 stripallbut(hbuf, '(', ')');
500 * miscellaneous server commands (testing, etc.)
502 void misc_server_cmd(CtdlIPC * ipc, char *cmd)
506 CtdlIPC_chat_send(ipc, cmd);
507 CtdlIPC_chat_recv(ipc, buf);
508 scr_printf("%s\n", buf);
510 set_keepalives(KA_HALF);
511 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
512 scr_printf("%s\n", buf);
514 set_keepalives(KA_YES);
519 newprompt("> ", buf, 255);
520 CtdlIPC_chat_send(ipc, buf);
521 } while (strcmp(buf, "000"));
528 * compute the checksum of a file
530 int file_checksum(char *filename)
536 fp = fopen(filename, "r");
540 /* yes, this algorithm may allow cksum to overflow, but that's ok
541 * as long as it overflows consistently, which it will.
543 while (ch = getc(fp), ch >= 0) {
544 cksum = (cksum + ch);
552 * nuke a directory and its contents
554 int nukedir(char *dirname)
560 dp = opendir(dirname);
565 while (d = readdir(dp), d != NULL) {
566 snprintf(filename, sizeof filename, "%s/%s", dirname, d->d_name);
571 return (rmdir(dirname));