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 */
28 for (a = 0; a < spaces; ++a) {
36 * Edit a user's Internet email addresses
38 void edit_user_internet_email_addresses(CtdlIPC * ipc, char *who)
51 r = CtdlIPCAideGetEmailAddresses(ipc, who, emailaddrs, buf);
53 while (!IsEmptyStr(emailaddrs)) {
54 extract_token(buf, emailaddrs, 0, '\n', sizeof buf);
55 remove_token(emailaddrs, 0, '\n');
56 if (!IsEmptyStr(buf)) {
59 recs = malloc(sizeof(char *));
61 recs = realloc(recs, (sizeof(char *)) * num_recs);
62 recs[num_recs - 1] = malloc(strlen(buf) + 1);
63 strcpy(recs[num_recs - 1], buf);
71 scr_printf(" Internet email addresses for %s\n", who);
73 scr_printf("--- --------------------------------------------------\n");
74 for (i = 0; i < num_recs; ++i) {
76 scr_printf("%3d ", i + 1);
78 scr_printf("%s\n", recs[i]);
82 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
85 newprompt("Enter new email address: ", buf, 50);
87 if (!IsEmptyStr(buf)) {
88 // FIXME validate the email address (format, our own domain, addr does not belong to another user)
91 recs = malloc(sizeof(char *));
93 recs = realloc(recs, (sizeof(char *)) * num_recs);
95 recs[num_recs - 1] = strdup(buf);
100 i = intprompt("Delete which address", 1, 1, num_recs) - 1;
103 for (j = i; j < num_recs; ++j) {
104 recs[j] = recs[j + 1];
110 for (i = 0; i < num_recs; i++)
111 r += 1 + strlen(recs[i]);
112 resp = (char *) calloc(1, r);
114 scr_printf("Can't save config - out of memory!\n");
118 for (i = 0; i < num_recs; i++) {
119 strcat(resp, recs[i]);
122 r = CtdlIPCAideSetEmailAddresses(ipc, who, resp, buf);
124 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)
154 char who[USERNAME_SIZE];
155 char newname[USERNAME_SIZE];
156 struct ctdluser *user = NULL;
158 int r; /* IPC response code */
163 newprompt("User name: ", who, 29);
164 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
165 scr_printf("%s\n", buf);
167 scr_printf("Do you want to create this user? ");
169 r = CtdlIPCCreateUser(ipc, who, 0, buf);
174 scr_printf("%s\n", buf);
181 if (cmd == 25) { // user edit
183 /* val_user(ipc, user->fullname, 0); we used to display the vCard here but there's really no need */
187 while (change_name == 1) {
188 if (boolprompt("Change name", 0)) {
189 strprompt("New name", newname, USERNAME_SIZE - 1);
190 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
192 scr_printf("%s\n", buf);
194 strcpy(user->fullname, newname);
203 if (newnow || boolprompt("Change password", 0)) {
204 strprompt("Password", user->password, -19);
207 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
208 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET))) {
209 user->flags |= US_INTERNET;
211 user->flags &= ~US_INTERNET;
213 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS))) {
214 user->flags &= ~US_REGIS;
216 user->flags |= US_REGIS;
218 user->timescalled = intprompt("Times called", user->timescalled, 0, INT_MAX);
219 user->posted = intprompt("Messages posted", user->posted, 0, INT_MAX);
220 user->lastcall = boolprompt("Set last login to now", 0) ? time(NULL) : user->lastcall;
221 user->USuserpurge = intprompt("Purge time (in days, 0 for system default", user->USuserpurge, 0, INT_MAX);
225 scr_printf("Do you want to delete this user? ");
230 user->axlevel = AxDeleted;
233 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
235 scr_printf("%s\n", buf);
239 if (boolprompt("Edit this user's Internet email addresses", 0)) {
240 edit_user_internet_email_addresses(ipc, who);
246 /* Display a prompt and flip a bit based on whether the user answers
247 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
248 * in which case No=1 and Yes=0.
250 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"));
265 scr_printf("%3s", ((temp & sbit) ? "Yes" : "No"));
280 temp = (temp | sbit);
282 temp = (temp ^ sbit);
287 * modes are: 0 - .EC command, 1 - .EC for new user,
288 * 2 - toggle Xpert mode 3 - toggle floor mode
290 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");
351 user->flags |= US_EXPERT;
352 scr_printf("Expert mode now ON\n");
357 if (user->flags & US_FLOORS) {
358 user->flags ^= US_FLOORS;
359 scr_printf("Floor mode now OFF\n");
361 user->flags |= US_FLOORS;
362 scr_printf("Floor mode now ON\n");
366 r = CtdlIPCSetConfig(ipc, user, buf);
368 scr_printf("%s\n", buf);
369 userflags = user->flags;
374 * getstring() - get a line of text from a file
375 * ignores lines beginning with "#"
377 int getstring(FILE * fp, char *string)
392 } while (string[0] == '#');
393 return (strlen(string));
397 /* Searches for patn in search string */
398 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)
416 if (IsEmptyStr(string))
419 /* Convert non-printable characters to blanks */
420 for (a = 0; !IsEmptyStr(&string[a]); ++a) {
427 /* Remove leading and trailing blanks */
428 while (string[0] < 33)
429 strcpy(string, &string[1]);
430 while (string[strlen(string) - 1] < 33)
431 string[strlen(string) - 1] = 0;
433 /* Remove double blanks */
434 for (a = 0; a < strlen(string); ++a) {
435 if ((string[a] == 32) && (string[a + 1] == 32)) {
436 strcpy(&string[a], &string[a + 1]);
441 /* remove characters which would interfere with the network */
442 for (a = 0; a < strlen(string); ++a) {
443 if (string[a] == '!')
444 strcpy(&string[a], &string[a + 1]);
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]);
460 void progress(CtdlIPC * ipc, unsigned long curr, unsigned long cmax)
462 static char dots[] = "**************************************************";
463 char dots_printed[51];
468 scr_printf("\r%79s\r", "");
470 /* a will be range 0-50 rather than 0-100 */
471 a = (curr * 50) / cmax;
472 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
473 strncpy(dots_printed, dots, a);
475 scr_printf(fmt, dots_printed, "", curr * 100 / cmax, curr, cmax);
482 * NOT the same locate_host() in locate_host.c. This one just does a
483 * 'who am i' to try to discover where the user is...
485 void locate_host(CtdlIPC * ipc, char *hbuf)
487 FILE *who = (FILE *) popen("who am i", "r");
489 strcpy(hbuf, ipc->ServInfo.fqdn);
492 fgets(hbuf, SIZ, who);
494 stripallbut(hbuf, '(', ')');
498 * miscellaneous server commands (testing, etc.)
500 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)
534 fp = fopen(filename, "r");
538 /* yes, this algorithm may allow cksum to overflow, but that's ok
539 * as long as it overflows consistently, which it will.
541 while (ch = getc(fp), ch >= 0) {
542 cksum = (cksum + ch);
550 * nuke a directory and its contents
552 int nukedir(char *dirname)
558 dp = opendir(dirname);
563 while (d = readdir(dp), d != NULL) {
564 snprintf(filename, sizeof filename, "%s/%s", dirname, d->d_name);
569 return (rmdir(dirname));