1 // Client-side support functions.
3 // Copyright (c) 1987-2016 by the citadel.org team
5 // This program is open source software. Use, duplication, and/or
6 // disclosure are subject to the GNU General Purpose License version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 #include "textclient.h"
15 #define IFAIDE if(axlevel>=AxAideU)
16 #define IFNAIDE if (axlevel<AxAideU)
18 extern unsigned userflags;
19 extern char sigcaught;
20 extern char rc_floor_mode;
21 extern int rc_ansi_color;
22 extern int rc_prompt_control;
24 /* Destructive backspace */
25 void back(int spaces) {
27 for (a = 0; a < spaces; ++a) {
35 * Edit a user's Internet email addresses
37 void edit_user_internet_email_addresses(CtdlIPC * ipc, char *who) {
49 r = CtdlIPCAideGetEmailAddresses(ipc, who, emailaddrs, buf);
51 while (!IsEmptyStr(emailaddrs)) {
52 extract_token(buf, emailaddrs, 0, '\n', sizeof buf);
53 remove_token(emailaddrs, 0, '\n');
54 if (!IsEmptyStr(buf)) {
57 recs = malloc(sizeof(char *));
59 recs = realloc(recs, (sizeof(char *)) * num_recs);
60 recs[num_recs - 1] = malloc(strlen(buf) + 1);
61 strcpy(recs[num_recs - 1], buf);
69 scr_printf(" Internet email addresses for %s\n", who);
71 scr_printf("--- --------------------------------------------------\n");
72 for (i = 0; i < num_recs; ++i) {
74 scr_printf("%3d ", i + 1);
76 scr_printf("%s\n", recs[i]);
80 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
83 newprompt("Enter new email address: ", buf, 50);
85 if (!IsEmptyStr(buf)) {
86 // FIXME validate the email address (format, our own domain, addr does not belong to another user)
89 recs = malloc(sizeof(char *));
92 recs = realloc(recs, (sizeof(char *)) * num_recs);
94 recs[num_recs - 1] = strdup(buf);
99 i = intprompt("Delete which address", 1, 1, num_recs) - 1;
102 for (j = i; j < num_recs; ++j) {
103 recs[j] = recs[j + 1];
109 for (i = 0; i < num_recs; i++)
110 r += 1 + strlen(recs[i]);
111 resp = (char *) calloc(1, r);
113 scr_printf("Can't save config - out of memory!\n");
117 for (i = 0; i < num_recs; i++) {
118 strcat(resp, recs[i]);
121 r = CtdlIPCAideSetEmailAddresses(ipc, who, resp, buf);
123 scr_printf("%s\n", buf);
126 scr_printf("Saved %d addresses.\n", num_recs);
133 quitting = !modified || boolprompt("Quit without saving", 0);
141 for (i = 0; i < num_recs; ++i)
149 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
151 void edituser(CtdlIPC * ipc, int cmd) {
153 char who[USERNAME_SIZE];
154 char newname[USERNAME_SIZE];
155 struct ctdluser *user = NULL;
157 int r; /* IPC response code */
162 newprompt("User name: ", who, 29);
163 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
164 scr_printf("%s\n", buf);
166 scr_printf("Do you want to create this user? ");
168 r = CtdlIPCCreateUser(ipc, who, 0, buf);
173 scr_printf("%s\n", buf);
180 if (cmd == 25) { // user edit
182 /* val_user(ipc, user->fullname, 0); we used to display the vCard here but there's really no need */
186 while (change_name == 1) {
187 if (boolprompt("Change name", 0)) {
188 strprompt("New name", newname, USERNAME_SIZE - 1);
189 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
191 scr_printf("%s\n", buf);
194 strcpy(user->fullname, newname);
204 if (newnow || boolprompt("Change password", 0)) {
205 strprompt("Password", user->password, -19);
208 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
209 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET))) {
210 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;
219 user->flags |= US_REGIS;
221 user->lastcall = boolprompt("Set last login to now", 0) ? time(NULL) : user->lastcall;
222 user->USuserpurge = intprompt("Purge time (in days, 0 for system default", user->USuserpurge, 0, INT_MAX);
226 scr_printf("Do you want to delete this user? ");
231 user->axlevel = AxDeleted;
234 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
236 scr_printf("%s\n", buf);
240 if (boolprompt("Edit this user's Internet email addresses", 0)) {
241 edit_user_internet_email_addresses(ipc, who);
247 /* Display a prompt and flip a bit based on whether the user answers
248 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
249 * in which case No=1 and Yes=0.
251 int set_attr(CtdlIPC * ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards) {
257 scr_printf("%50s ", prompt);
260 color(BRIGHT_MAGENTA);
263 scr_printf("%3s", ((temp & sbit) ? "No" : "Yes"));
266 scr_printf("%3s", ((temp & sbit) ? "Yes" : "No"));
281 temp = (temp | sbit);
283 temp = (temp ^ sbit);
288 * modes are: 0 - .EC command, 1 - .EC for new user,
289 * 2 - toggle Xpert mode 3 - toggle floor mode
291 void enter_config(CtdlIPC * ipc, int mode) {
293 struct ctdluser *user = NULL;
294 int r; /* IPC response code */
296 r = CtdlIPCGetConfig(ipc, &user, buf);
298 scr_printf("%s\n", buf);
303 if (mode == 0 || mode == 1) {
305 user->flags = set_attr(ipc, user->flags, "Are you an experienced Citadel user", US_EXPERT, 0);
306 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
311 user->flags = set_attr(ipc, user->flags, "Print last old message on New message request", US_LASTOLD, 0);
313 user->flags = set_attr(ipc, user->flags, "Prompt after each message", US_NOPROMPT, 1);
315 if ((user->flags & US_NOPROMPT) == 0) {
316 user->flags = set_attr(ipc, user->flags, "Use 'disappearing' prompts", US_DISAPPEAR, 0);
319 user->flags = set_attr(ipc, user->flags, "Pause after each screenful of text", US_PAGINATOR, 0);
321 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
322 user->flags = set_attr(ipc, user->flags, "<N>ext and <S>top work at paginator prompt", US_PROMPTCTL, 0);
325 if (rc_floor_mode == RC_DEFAULT) {
326 user->flags = set_attr(ipc, user->flags, "View rooms by floor", US_FLOORS, 0);
329 if (rc_ansi_color == 3) {
330 user->flags = set_attr(ipc, user->flags, "Enable color support", US_COLOR, 0);
333 if ((user->flags & US_EXPERT) == 0) {
334 formout(ipc, "unlisted");
337 user->flags = set_attr(ipc, user->flags, "Be unlisted in userlog", US_UNLISTED, 0);
339 if (!IsEmptyStr(editor_path)) {
340 user->flags = set_attr(ipc,
341 user->flags, "Always enter messages with the full-screen editor", US_EXTEDIT, 0);
347 if (user->flags & US_EXPERT) {
348 user->flags ^= US_EXPERT;
349 scr_printf("Expert mode now OFF\n");
352 user->flags |= US_EXPERT;
353 scr_printf("Expert mode now ON\n");
358 if (user->flags & US_FLOORS) {
359 user->flags ^= US_FLOORS;
360 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) {
393 } while (string[0] == '#');
394 return (strlen(string));
398 /* Searches for patn in search string */
399 int pattern(char *search, char *patn) {
403 for (a = 0; !IsEmptyStr(&search[a]); ++a) {
404 b = strncasecmp(&search[a], patn, len);
412 void strproc(char *string) {
415 if (IsEmptyStr(string))
418 /* Convert non-printable characters to blanks */
419 for (a = 0; !IsEmptyStr(&string[a]); ++a) {
426 /* Remove leading and trailing blanks */
427 while (string[0] < 33)
428 strcpy(string, &string[1]);
429 while (string[strlen(string) - 1] < 33)
430 string[strlen(string) - 1] = 0;
432 /* Remove double blanks */
433 for (a = 0; a < strlen(string); ++a) {
434 if ((string[a] == 32) && (string[a + 1] == 32)) {
435 strcpy(&string[a], &string[a + 1]);
440 /* remove characters which would interfere with the network */
441 for (a = 0; a < strlen(string); ++a) {
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]);
450 if (string[a] == '%')
451 strcpy(&string[a], &string[a + 1]);
452 if (string[a] == '|')
453 strcpy(&string[a], &string[a + 1]);
459 void progress(CtdlIPC * ipc, unsigned long curr, unsigned long cmax) {
460 static char dots[] = "**************************************************";
461 char dots_printed[51];
466 scr_printf("\r%79s\r", "");
469 /* a will be range 0-50 rather than 0-100 */
470 a = (curr * 50) / cmax;
471 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
472 strncpy(dots_printed, dots, a);
474 scr_printf(fmt, dots_printed, "", curr * 100 / cmax, curr, cmax);
481 * NOT the same locate_host() in locate_host.c. This one just does a
482 * 'who am i' to try to discover where the user is...
484 void locate_host(CtdlIPC * ipc, char *hbuf) {
485 FILE *who = (FILE *) popen("who am i", "r");
487 strcpy(hbuf, ipc->ServInfo.fqdn);
490 fgets(hbuf, SIZ, who);
491 if (hbuf[strlen(hbuf) - 1] == '\n') {
492 hbuf[strlen(hbuf) - 1] = 0;
495 stripallbut(hbuf, '(', ')');
499 * miscellaneous server commands (testing, etc.)
501 void misc_server_cmd(CtdlIPC * ipc, char *cmd) {
504 CtdlIPC_chat_send(ipc, cmd);
505 CtdlIPC_chat_recv(ipc, buf);
506 scr_printf("%s\n", buf);
508 set_keepalives(KA_HALF);
509 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
510 scr_printf("%s\n", buf);
512 set_keepalives(KA_YES);
517 newprompt("> ", buf, 255);
518 CtdlIPC_chat_send(ipc, buf);
519 } while (strcmp(buf, "000"));
526 * compute the checksum of a file
528 int file_checksum(char *filename) {
533 fp = fopen(filename, "r");
537 /* yes, this algorithm may allow cksum to overflow, but that's ok
538 * as long as it overflows consistently, which it will.
540 while (ch = getc(fp), ch >= 0) {
541 cksum = (cksum + ch);
549 * nuke a directory and its contents
551 int nukedir(char *dirname) {
556 dp = opendir(dirname);
561 while (d = readdir(dp), d != NULL) {
562 snprintf(filename, sizeof filename, "%s/%s", dirname, d->d_name);
567 return (rmdir(dirname));