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 or delete a user (cmd=25 to edit/create, 96 to delete)
78 void edituser(CtdlIPC *ipc, int cmd)
81 char who[USERNAME_SIZE];
82 char newname[USERNAME_SIZE];
83 struct ctdluser *user = NULL;
85 int r; /* IPC response code */
90 newprompt("User name: ", who, 29);
91 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
92 scr_printf("%s\n", buf);
94 scr_printf("Do you want to create this user? ");
96 r = CtdlIPCCreateUser(ipc, who, 0, buf);
101 scr_printf("%s\n", buf);
109 val_user(ipc, user->fullname, 0); /* Display registration */
113 while (change_name == 1) {
114 if (boolprompt("Change name", 0)) {
115 strprompt("New name", newname, USERNAME_SIZE-1);
116 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
118 scr_printf("%s\n", buf);
121 strcpy(user->fullname, newname);
131 if (newnow || boolprompt("Change password", 0)) {
132 strprompt("Password", user->password, -19);
135 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
136 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET)))
137 user->flags |= US_INTERNET;
139 user->flags &= ~US_INTERNET;
140 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS)))
141 user->flags &= ~US_REGIS;
143 user->flags |= US_REGIS;
144 user->timescalled = intprompt("Times called",
145 user->timescalled, 0, INT_MAX);
146 user->posted = intprompt("Messages posted",
147 user->posted, 0, INT_MAX);
148 user->lastcall = boolprompt("Set last call to now", 0) ?
149 time(NULL) : user->lastcall;
150 user->USuserpurge = intprompt("Purge time (in days, 0 for system default",
151 user->USuserpurge, 0, INT_MAX);
155 scr_printf("Do you want to delete this user? ");
160 user->axlevel = AxDeleted;
163 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
165 scr_printf("%s\n", buf);
171 /* Display a prompt and flip a bit based on whether the user answers
172 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
173 * in which case No=1 and Yes=0.
175 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
182 scr_printf("%50s ", prompt);
185 color(BRIGHT_MAGENTA);
188 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
191 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
199 if (backwards) a = 1 - a;
201 if (backwards) a = 1 - a;
204 if (!a) temp = (temp^sbit);
209 * modes are: 0 - .EC command, 1 - .EC for new user,
210 * 2 - toggle Xpert mode 3 - toggle floor mode
212 void enter_config(CtdlIPC *ipc, int mode)
215 struct ctdluser *user = NULL;
216 int r; /* IPC response code */
218 r = CtdlIPCGetConfig(ipc, &user, buf);
220 scr_printf("%s\n", buf);
225 if (mode == 0 || mode == 1) {
227 user->flags = set_attr(ipc, user->flags,
228 "Are you an experienced Citadel user",
230 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
235 user->flags = set_attr(
238 "Print last old message on New message request",
243 user->flags = set_attr(
246 "Prompt after each message",
251 if ((user->flags & US_NOPROMPT) == 0) {
252 user->flags = set_attr(
255 "Use 'disappearing' prompts",
261 user->flags = set_attr(
264 "Pause after each screenful of text",
269 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
270 user->flags = set_attr(
273 "<N>ext and <S>top work at paginator prompt",
279 if (rc_floor_mode == RC_DEFAULT) {
280 user->flags = set_attr(
283 "View rooms by floor",
289 if (rc_ansi_color == 3) {
290 user->flags = set_attr(
293 "Enable color support",
299 if ((user->flags & US_EXPERT) == 0) {
300 formout(ipc, "unlisted");
303 user->flags = set_attr(
306 "Be unlisted in userlog",
311 if (!IsEmptyStr(editor_path)) {
312 user->flags = set_attr(
315 "Always enter messages with the full-screen editor",
324 if (user->flags & US_EXPERT) {
325 user->flags ^= US_EXPERT;
326 scr_printf("Expert mode now OFF\n");
328 user->flags |= US_EXPERT;
329 scr_printf("Expert mode now ON\n");
334 if (user->flags & US_FLOORS) {
335 user->flags ^= US_FLOORS;
336 scr_printf("Floor mode now OFF\n");
338 user->flags |= US_FLOORS;
339 scr_printf("Floor mode now ON\n");
343 r = CtdlIPCSetConfig(ipc, user, buf);
344 if (r / 100 != 2) scr_printf("%s\n", buf);
345 userflags = user->flags;
350 * getstring() - get a line of text from a file
351 * ignores lines beginning with "#"
353 int getstring(FILE *fp, char *string)
368 } while(string[0]=='#');
369 return(strlen(string));
373 /* Searches for patn in search string */
374 int pattern(char *search, char *patn) {
378 for (a=0; !IsEmptyStr(&search[a]); ++a) {
379 b=strncasecmp(&search[a],patn,len);
386 void strproc(char *string)
390 if (IsEmptyStr(string)) return;
392 /* Convert non-printable characters to blanks */
393 for (a=0; !IsEmptyStr(&string[a]); ++a) {
394 if (string[a]<32) string[a]=32;
395 if (string[a]>126) string[a]=32;
398 /* Remove leading and trailing blanks */
399 while(string[0]<33) strcpy(string,&string[1]);
400 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
402 /* Remove double blanks */
403 for (a=0; a<strlen(string); ++a) {
404 if ((string[a]==32)&&(string[a+1]==32)) {
405 strcpy(&string[a],&string[a+1]);
410 /* remove characters which would interfere with the network */
411 for (a=0; a<strlen(string); ++a) {
412 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
413 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
414 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
415 if (string[a]==',') strcpy(&string[a],&string[a+1]);
416 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
417 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
423 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
426 "**************************************************";
427 char dots_printed[51];
432 scr_printf("\r%79s\r","");
434 /* a will be range 0-50 rather than 0-100 */
435 a=(curr * 50) / cmax;
436 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
437 strncpy(dots_printed, dots, a);
439 scr_printf(fmt, dots_printed, "",
440 curr * 100 / cmax, curr, cmax);
447 * NOT the same locate_host() in locate_host.c. This one just does a
448 * 'who am i' to try to discover where the user is...
450 void locate_host(CtdlIPC* ipc, char *hbuf)
452 FILE *who = (FILE *)popen("who am i","r");
454 strcpy(hbuf, ipc->ServInfo.fqdn);
457 fgets(hbuf, SIZ, who);
459 stripallbut(hbuf, '(' , ')' );
463 * miscellaneous server commands (testing, etc.)
465 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
468 CtdlIPC_chat_send(ipc, cmd);
469 CtdlIPC_chat_recv(ipc, buf);
470 scr_printf("%s\n",buf);
472 set_keepalives(KA_HALF);
473 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
474 scr_printf("%s\n",buf);
476 set_keepalives(KA_YES);
481 newprompt("> ",buf,255);
482 CtdlIPC_chat_send(ipc, buf);
483 } while(strcmp(buf,"000"));
490 * compute the checksum of a file
492 int file_checksum(char *filename)
498 fp = fopen(filename,"r");
499 if (fp == NULL) return(0);
501 /* yes, this algorithm may allow cksum to overflow, but that's ok
502 * as long as it overflows consistently, which it will.
504 while (ch=getc(fp), ch>=0) {
505 cksum = (cksum + ch);
513 * nuke a directory and its contents
515 int nukedir(char *dirname)
521 dp = opendir(dirname);
526 while (d = readdir(dp), d != NULL) {
527 snprintf(filename, sizeof filename, "%s/%s",
533 return(rmdir(dirname));