]> code.citadel.org Git - citadel.git/blob - citadel/messages.c
* Full-screen curses support for Citadel text client
[citadel.git] / citadel / messages.c
1 /*
2  * $Id$
3  *
4  * Citadel/UX message support routines
5  * see copyright.txt for copyright information
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <signal.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <sys/wait.h>
20 #include <sys/stat.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <stdarg.h>
34 #include "citadel.h"
35 #include "messages.h"
36 #include "commands.h"
37 #include "rooms.h"
38 #include "tools.h"
39 #ifndef HAVE_SNPRINTF
40 #include "snprintf.h"
41 #endif
42 #include "screen.h"
43
44 #define MAXWORDBUF SIZ
45 #define MAXMSGS 512
46
47 struct cittext {
48         struct cittext *next;
49         char text[MAXWORDBUF];
50 };
51
52 void sttybbs(int cmd);
53 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp,
54           char subst);
55 int haschar(char *st, int ch);
56 int checkpagin(int lp, int pagin, int height);
57 void getline(char *string, int lim);
58 void formout(char *name);
59 int yesno(void);
60 void newprompt(char *prompt, char *str, int len);
61 int file_checksum(char *filename);
62 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
63
64 char reply_to[512];
65 long msg_arr[MAXMSGS];
66 int num_msgs;
67 char rc_alt_semantics;
68 extern char room_name[];
69 extern unsigned room_flags;
70 extern long highest_msg_read;
71 extern struct CtdlServInfo serv_info;
72 extern char temp[];
73 extern char temp2[];
74 extern int screenwidth;
75 extern int screenheight;
76 extern long maxmsgnum;
77 extern char is_mail;
78 extern char is_aide;
79 extern char is_room_aide;
80 extern char fullname[];
81 extern char axlevel;
82 extern unsigned userflags;
83 extern char sigcaught;
84 extern char editor_path[];
85 extern char printcmd[];
86 extern int rc_allow_attachments;
87 extern int rc_display_message_numbers;
88 extern int rc_force_mail_prompts;
89
90 extern int editor_pid;
91
92 void ka_sigcatch(int signum)
93 {
94         char buf[SIZ];
95         alarm(S_KEEPALIVE);
96         signal(SIGALRM, ka_sigcatch);
97         serv_puts("NOOP");
98         serv_gets(buf);
99 }
100
101
102 /*
103  * server keep-alive version of wait() (needed for external editor)
104  */
105 pid_t ka_wait(int *kstatus)
106 {
107         pid_t p;
108
109         alarm(S_KEEPALIVE);
110         signal(SIGALRM, ka_sigcatch);
111         do {
112                 errno = 0;
113                 p = wait(kstatus);
114         } while (errno == EINTR);
115         signal(SIGALRM, SIG_IGN);
116         alarm(0);
117         return (p);
118 }
119
120
121 /*
122  * version of system() that uses ka_wait()
123  */
124 int ka_system(char *shc)
125 {
126         pid_t childpid;
127         pid_t waitpid;
128         int retcode;
129
130         childpid = fork();
131         if (childpid < 0) {
132                 color(BRIGHT_RED);
133                 perror("Cannot fork");
134                 color(DIM_WHITE);
135                 return ((pid_t) childpid);
136         }
137
138         if (childpid == 0) {
139                 execlp("/bin/sh", "sh", "-c", shc, NULL);
140                 exit(127);
141         }
142
143         if (childpid > 0) {
144                 do {
145                         waitpid = ka_wait(&retcode);
146                 } while (waitpid != childpid);
147                 return (retcode);
148         }
149
150         return (-1);
151 }
152
153
154
155 /*
156  * add a newline to the buffer...
157  */
158 void add_newline(struct cittext *textlist)
159 {
160         struct cittext *ptr;
161
162         ptr = textlist;
163         while (ptr->next != NULL)
164                 ptr = ptr->next;
165
166         while (ptr->text[strlen(ptr->text) - 1] == 32)
167                 ptr->text[strlen(ptr->text) - 1] = 0;
168         /* strcat(ptr->text,"\n"); */
169
170         ptr->next = (struct cittext *)
171             malloc(sizeof(struct cittext));
172         ptr = ptr->next;
173         ptr->next = NULL;
174         strcpy(ptr->text, "");
175 }
176
177
178 /*
179  * add a word to the buffer...
180  */
181 void add_word(struct cittext *textlist, char *wordbuf)
182 {
183         struct cittext *ptr;
184
185         ptr = textlist;
186         while (ptr->next != NULL)
187                 ptr = ptr->next;
188
189         if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
190                 ptr->next = (struct cittext *)
191                     malloc(sizeof(struct cittext));
192                 ptr = ptr->next;
193                 ptr->next = NULL;
194                 strcpy(ptr->text, "");
195         }
196
197         strcat(ptr->text, wordbuf);
198         strcat(ptr->text, " ");
199 }
200
201
202 /*
203  * begin editing of an opened file pointed to by fp
204  */
205 void citedit(FILE * fp)
206 {
207         int a, prev, finished, b, last_space;
208         int appending = 0;
209         struct cittext *textlist = NULL;
210         struct cittext *ptr;
211         char wordbuf[MAXWORDBUF];
212
213         /* first, load the text into the buffer */
214         fseek(fp, 0L, 0);
215         textlist = (struct cittext *) malloc(sizeof(struct cittext));
216         textlist->next = NULL;
217         strcpy(textlist->text, "");
218
219         strcpy(wordbuf, "");
220         prev = (-1);
221         while (a = getc(fp), a >= 0) {
222                 appending = 1;
223                 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
224                         add_word(textlist, wordbuf);
225                         strcpy(wordbuf, "");
226                         if ((prev == 13) || (prev == 10)) {
227                                 add_word(textlist, "\n");
228                                 add_newline(textlist);
229                                 add_word(textlist, "");
230                         }
231                 } else {
232                         wordbuf[strlen(wordbuf) + 1] = 0;
233                         wordbuf[strlen(wordbuf)] = a;
234                 }
235                 if (strlen(wordbuf) + 3 > screenwidth) {
236                         add_word(textlist, wordbuf);
237                         strcpy(wordbuf, "");
238                 }
239                 prev = a;
240         }
241
242         /* get text */
243         finished = 0;
244         prev = (appending ? 13 : (-1));
245         strcpy(wordbuf, "");
246         async_ka_start();
247         do {
248                 a = inkey();
249                 if (a == 10)
250                         a = 13;
251                 if (a == 9)
252                         a = 32;
253                 if (a == 127)
254                         a = 8;
255
256         /******* new ***********/
257                 if ((a > 32) && (a < 127) && (prev == 13)) {
258                         add_word(textlist, "\n");
259                         scr_printf(" ");
260                 }
261         /***********************/
262
263                 if ((a == 32) && (prev == 13)) {
264                         add_word(textlist, "\n");
265                         add_newline(textlist);
266                 }
267
268                 if (a == 8) {
269                         if (strlen(wordbuf) > 0) {
270                                 wordbuf[strlen(wordbuf) - 1] = 0;
271                                 scr_putc(8);
272                                 scr_putc(32);
273                                 scr_putc(8);
274                         }
275                 } else if (a == 13) {
276                         scr_printf("\n");
277                         if (strlen(wordbuf) == 0)
278                                 finished = 1;
279                         else {
280                                 for (b = 0; b < strlen(wordbuf); ++b)
281                                         if (wordbuf[b] == 32) {
282                                                 wordbuf[b] = 0;
283                                                 add_word(textlist,
284                                                          wordbuf);
285                                                 strcpy(wordbuf,
286                                                        &wordbuf[b + 1]);
287                                                 b = 0;
288                                         }
289                                 add_word(textlist, wordbuf);
290                                 strcpy(wordbuf, "");
291                         }
292                 } else {
293                         scr_putc(a);
294                         wordbuf[strlen(wordbuf) + 1] = 0;
295                         wordbuf[strlen(wordbuf)] = a;
296                 }
297                 if ((strlen(wordbuf) + 3) > screenwidth) {
298                         last_space = (-1);
299                         for (b = 0; b < strlen(wordbuf); ++b)
300                                 if (wordbuf[b] == 32)
301                                         last_space = b;
302                         if (last_space >= 0) {
303                                 for (b = 0; b < strlen(wordbuf); ++b)
304                                         if (wordbuf[b] == 32) {
305                                                 wordbuf[b] = 0;
306                                                 add_word(textlist,
307                                                          wordbuf);
308                                                 strcpy(wordbuf,
309                                                        &wordbuf[b + 1]);
310                                                 b = 0;
311                                         }
312                                 for (b = 0; b < strlen(wordbuf); ++b) {
313                                         scr_putc(8);
314                                         scr_putc(32);
315                                         scr_putc(8);
316                                 }
317                                 scr_printf("\n%s", wordbuf);
318                         } else {
319                                 add_word(textlist, wordbuf);
320                                 strcpy(wordbuf, "");
321                                 scr_printf("\n");
322                         }
323                 }
324                 prev = a;
325         } while (finished == 0);
326         async_ka_end();
327
328         /* write the buffer back to disk */
329         fseek(fp, 0L, 0);
330         for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
331                 fprintf(fp, "%s", ptr->text);
332         }
333         putc(10, fp);
334         fflush(fp);
335         ftruncate(fileno(fp), ftell(fp));
336
337         /* and deallocate the memory we used */
338         while (textlist != NULL) {
339                 ptr = textlist->next;
340                 free(textlist);
341                 textlist = ptr;
342         }
343 }
344
345 /* Read a message from the server
346  */
347 int read_message(
348         long num,   /* message number */
349         char pagin) /* 0 = normal read, 1 = read with pagination, 2 = header */
350 {
351         char buf[SIZ];
352         char m_subject[SIZ];
353         char from[SIZ], node[SIZ], rfca[SIZ];
354         char now[SIZ];
355         int format_type = 0;
356         int fr = 0;
357         int nhdr = 0;
358
359         sigcaught = 0;
360         sttybbs(1);
361
362         sprintf(buf, "MSG0 %ld|%d", num, (pagin == READ_HEADER ? 1 : 0));
363         serv_puts(buf);
364         serv_gets(buf);
365         if (buf[0] != '1') {
366                 err_printf("*** msg #%ld: %s\n", num, buf);
367                 ++lines_printed;
368                 lines_printed =
369                     checkpagin(lines_printed, pagin, screenheight);
370                 sttybbs(0);
371                 return (0);
372         }
373
374         strcpy(m_subject, "");
375         strcpy(reply_to, "nobody ... xxxxx");
376         strcpy(from, "");
377         strcpy(node, "");
378         strcpy(rfca, "");
379
380         scr_printf("\n");
381         ++lines_printed;
382         lines_printed = checkpagin(lines_printed, pagin, screenheight);
383         scr_printf(" ");
384         if (pagin == 1) {
385                 color(BRIGHT_CYAN);
386         }
387
388         if (pagin == 2) {
389                 while (serv_gets(buf), strcmp(buf, "000")) {
390                         if (buf[4] == '=') {
391                                 scr_printf("%s\n", buf);
392                                 ++lines_printed;
393                                 lines_printed =
394                                     checkpagin(lines_printed,
395                                                pagin, screenheight);
396                         }
397                 }
398                 sttybbs(0);
399                 return (0);
400         }
401
402         while (serv_gets(buf), strncasecmp(buf, "text", 4)) {
403                 if (!strncasecmp(buf, "nhdr=yes", 8))
404                         nhdr = 1;
405                 if (!strncasecmp(buf, "from=", 5)) {
406                         strcpy(from, &buf[5]);
407                 }
408                 if (nhdr == 1)
409                         buf[0] = '_';
410                 if (!strncasecmp(buf, "type=", 5))
411                         format_type = atoi(&buf[5]);
412                 if ((!strncasecmp(buf, "msgn=", 5))
413                     && (rc_display_message_numbers)) {
414                         color(DIM_WHITE);
415                         scr_printf("[");
416                         color(BRIGHT_WHITE);
417                         scr_printf("#%s", &buf[5]);
418                         color(DIM_WHITE);
419                         scr_printf("] ");
420                 }
421                 if (!strncasecmp(buf, "from=", 5)) {
422                         color(DIM_WHITE);
423                         scr_printf("from ");
424                         color(BRIGHT_CYAN);
425                         scr_printf("%s ", &buf[5]);
426                 }
427                 if (!strncasecmp(buf, "subj=", 5))
428                         strcpy(m_subject, &buf[5]);
429
430                 if (!strncasecmp(buf, "rfca=", 5)) {
431                         safestrncpy(rfca, &buf[5], sizeof(rfca) - 5);
432                         color(DIM_WHITE);
433                         scr_printf("<");
434                         color(BRIGHT_BLUE);
435                         scr_printf("%s", &buf[5]);
436                         color(DIM_WHITE);
437                         scr_printf("> ");
438                 }
439                 if ((!strncasecmp(buf, "hnod=", 5))
440                     && (strcasecmp(&buf[5], serv_info.serv_humannode))
441                     && (strlen(rfca) == 0)) {
442                         color(DIM_WHITE);
443                         scr_printf("(");
444                         color(BRIGHT_WHITE);
445                         scr_printf("%s", &buf[5]);
446                         color(DIM_WHITE);
447                         scr_printf(") ");
448                 }
449                 if ((!strncasecmp(buf, "room=", 5))
450                     && (strcasecmp(&buf[5], room_name))
451                     && (strlen(rfca) == 0)) {
452                         color(DIM_WHITE);
453                         scr_printf("in ");
454                         color(BRIGHT_MAGENTA);
455                         scr_printf("%s> ", &buf[5]);
456                 }
457
458                 if (!strncasecmp(buf, "node=", 5)) {
459                         safestrncpy(node, &buf[5], sizeof(buf) - 5);
460                         if ((room_flags & QR_NETWORK)
461                             ||
462                             ((strcasecmp
463                               (&buf[5], serv_info.serv_nodename)
464                               &&
465                               (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
466                         {
467                                 if (strlen(rfca) == 0) {
468                                         color(DIM_WHITE);
469                                         scr_printf("@");
470                                         color(BRIGHT_YELLOW);
471                                         scr_printf("%s ", &buf[5]);
472                                 }
473                         }
474                 }
475
476                 if (!strncasecmp(buf, "rcpt=", 5)) {
477                         color(DIM_WHITE);
478                         scr_printf("to ");
479                         color(BRIGHT_CYAN);
480                         scr_printf("%s ", &buf[5]);
481                 }
482                 if (!strncasecmp(buf, "time=", 5)) {
483                         fmt_date(now, atol(&buf[5]), 0);
484                         scr_printf("%s ", now);
485                 }
486         }
487
488         if (nhdr == 1) {
489                 if (!is_room_aide) {
490                         scr_printf(" ****");
491                 } else {
492                         scr_printf(" %s", from);
493                 }
494         }
495         scr_printf("\n");
496
497         if (strlen(rfca) > 0) {
498                 strcpy(reply_to, rfca);
499         } else {
500                 snprintf(reply_to, sizeof(reply_to), "%s @ %s", from,
501                          node);
502         }
503
504         if (pagin == 1)
505                 color(BRIGHT_WHITE);
506         ++lines_printed;
507         lines_printed = checkpagin(lines_printed, pagin, screenheight);
508
509         if (strlen(m_subject) > 0) {
510                 scr_printf("Subject: %s\n", m_subject);
511                 ++lines_printed;
512                 lines_printed =
513                     checkpagin(lines_printed, pagin, screenheight);
514         }
515
516         if (format_type == 0) {
517                 fr = fmout(screenwidth, NULL,
518                            ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
519         } else {
520                 while (serv_gets(buf), strcmp(buf, "000")) {
521                         if (sigcaught == 0) {
522                                 scr_printf("%s\n", buf);
523                                 lines_printed = lines_printed + 1 +
524                                     (strlen(buf) / screenwidth);
525                                 lines_printed =
526                                     checkpagin(lines_printed, pagin,
527                                                screenheight);
528                         }
529                 }
530                 fr = sigcaught;
531         }
532         scr_printf("\n");
533         scr_flush();
534         ++lines_printed;
535         lines_printed = checkpagin(lines_printed, pagin, screenheight);
536
537         if (pagin == 1)
538                 color(DIM_WHITE);
539         sttybbs(0);
540         return (fr);
541 }
542
543 /*
544  * replace string function for the built-in editor
545  */
546 void replace_string(char *filename, long int startpos)
547 {
548         char buf[512];
549         char srch_str[128];
550         char rplc_str[128];
551         FILE *fp;
552         int a;
553         long rpos, wpos;
554         char *ptr;
555         int substitutions = 0;
556         long msglen = 0L;
557
558         scr_printf("Enter text to be replaced:\n: ");
559         getline(srch_str, 128);
560         if (strlen(srch_str) == 0)
561                 return;
562
563         scr_printf("Enter text to replace it with:\n: ");
564         getline(rplc_str, 128);
565
566         fp = fopen(filename, "r+");
567         if (fp == NULL)
568                 return;
569
570         wpos = startpos;
571         fseek(fp, startpos, 0);
572         strcpy(buf, "");
573         while (a = getc(fp), a > 0) {
574                 ++msglen;
575                 buf[strlen(buf) + 1] = 0;
576                 buf[strlen(buf)] = a;
577                 if (strlen(buf) >= strlen(srch_str)) {
578                         ptr = (&buf[strlen(buf) - strlen(srch_str)]);
579                         if (!strncasecmp(ptr, srch_str, strlen(srch_str))) {
580                                 strcpy(ptr, rplc_str);
581                                 ++substitutions;
582                         }
583                 }
584                 if (strlen(buf) > 384) {
585                         rpos = ftell(fp);
586                         fseek(fp, wpos, 0);
587                         fwrite((char *) buf, 128, 1, fp);
588                         strcpy(buf, &buf[128]);
589                         wpos = ftell(fp);
590                         fseek(fp, rpos, 0);
591                 }
592         }
593         fseek(fp, wpos, 0);
594         if (strlen(buf) > 0)
595                 fwrite((char *) buf, strlen(buf), 1, fp);
596         wpos = ftell(fp);
597         fclose(fp);
598         truncate(filename, wpos);
599         scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
600 }
601
602 /*
603  * Function to begin composing a new message
604  */
605 int client_make_message(char *filename, /* temporary file name */
606                 char *recipient,        /* NULL if it's not mail */
607                 int anon_type,          /* see MES_ types in header file */
608                 int format_type,
609                 int mode)
610 {
611         FILE *fp;
612         int a, b, e_ex_code;
613         long beg;
614         char datestr[SIZ];
615         char header[SIZ];
616         int cksum = 0;
617
618         if (mode == 2)
619                 if (strlen(editor_path) == 0) {
620                         err_printf
621                             ("*** No editor available, using built-in editor\n");
622                         mode = 0;
623                 }
624
625         fmt_date(datestr, time(NULL), 0);
626         header[0] = 0;
627
628         if (room_flags & QR_ANONONLY && !recipient) {
629                 sprintf(&header[strlen(header)], " ****");
630         }
631         else {
632                 sprintf(&header[strlen(header)],
633                         " %s from %s", datestr, fullname);
634                 if (strlen(recipient) > 0) {
635                         sprintf(&header[strlen(header)],
636                                 " to %s", recipient);
637                 }
638         }
639         scr_printf("%s\n", header);
640
641         beg = 0L;
642
643         if (mode == 1) {
644                 scr_printf("(Press ctrl-d when finished)\n");
645         }
646
647         if (mode == 0) {
648                 fp = fopen(filename, "r");
649                 if (fp != NULL) {
650                         fmout(screenwidth, fp, 0, screenheight, 0, 0);
651                         beg = ftell(fp);
652                         fclose(fp);
653                 } else {
654                         fp = fopen(filename, "w");
655                         if (fp == NULL) {
656                                 err_printf("*** Error opening temp file!\n"
657                                         "    %s: %s\n",
658                                         filename, strerror(errno));
659                         return(1);
660                         }
661                         fclose(fp);
662                 }
663         }
664
665 ME1:    switch (mode) {
666
667         case 0:
668                 fp = fopen(filename, "r+");
669                 if (fp == NULL) {
670                         err_printf("*** Error opening temp file!\n"
671                                 "    %s: %s\n",
672                                 filename, strerror(errno));
673                         return(1);
674                 }
675                 citedit(fp);
676                 fclose(fp);
677                 goto MECR;
678
679         case 1:
680                 fp = fopen(filename, "a");
681                 if (fp == NULL) {
682                         err_printf("*** Error opening temp file!\n"
683                                 "    %s: %s\n",
684                                 filename, strerror(errno));
685                         return(1);
686                 }
687                 do {
688                         a = inkey();
689                         if (a == 255)
690                                 a = 32;
691                         if (a == 13)
692                                 a = 10;
693                         if (a != 4) {
694                                 putc(a, fp);
695                                 scr_putc(a);
696                         }
697                         if (a == 10)
698                                 scr_putc(10);
699                 } while (a != 4);
700                 fclose(fp);
701                 break;
702
703         case 2:
704                 e_ex_code = 1;  /* start with a failed exit code */
705                 editor_pid = fork();
706                 cksum = file_checksum(filename);
707                 if (editor_pid == 0) {
708                         chmod(filename, 0600);
709                         screen_reset();
710                         sttybbs(SB_RESTORE);
711                         setenv("WINDOW_TITLE", header, 1);
712                         execlp(editor_path, editor_path, filename, NULL);
713                         exit(1);
714                 }
715                 if (editor_pid > 0)
716                         do {
717                                 e_ex_code = 0;
718                                 b = ka_wait(&e_ex_code);
719                         } while ((b != editor_pid) && (b >= 0));
720                 editor_pid = (-1);
721                 sttybbs(0);
722                 screen_set();
723                 break;
724         }
725
726 MECR:   if (mode == 2) {
727                 if (file_checksum(filename) == cksum) {
728                         err_printf("*** Aborted message.\n");
729                         e_ex_code = 1;
730                 }
731                 if (e_ex_code == 0)
732                         goto MEFIN;
733                 goto MEABT2;
734         }
735
736         b = keymenu("Entry command (? for options)",
737                     "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
738                     "<R>eplace string|<H>old message");
739
740         if (b == 'a')
741                 goto MEABT;
742         if (b == 'c')
743                 goto ME1;
744         if (b == 's')
745                 goto MEFIN;
746         if (b == 'p') {
747                 scr_printf(" %s from %s", datestr, fullname);
748                 if (strlen(recipient) > 0)
749                         scr_printf(" to %s", recipient);
750                 scr_printf("\n");
751                 fp = fopen(filename, "r");
752                 if (fp != NULL) {
753                         fmout(screenwidth, fp,
754                               ((userflags & US_PAGINATOR) ? 1 : 0),
755                               screenheight, 0, 0);
756                         beg = ftell(fp);
757                         fclose(fp);
758                 }
759                 goto MECR;
760         }
761         if (b == 'r') {
762                 replace_string(filename, 0L);
763                 goto MECR;
764         }
765         if (b == 'h') {
766                 return (2);
767         }
768
769 MEFIN:  return (0);
770
771 MEABT:  scr_printf("Are you sure? ");
772         if (yesno() == 0) {
773                 goto ME1;
774         }
775 MEABT2: unlink(filename);
776         return (2);
777 }
778
779 /*
780  * Transmit message text to the server.
781  * 
782  * This loop also implements a "tick" counter that displays the progress, if
783  * we're sending something that will take a long time to transmit.
784  */
785 void transmit_message(FILE *fp)
786 {
787         char buf[SIZ];
788         int ch, a;
789         long msglen;
790         time_t lasttick;
791
792         fseek(fp, 0L, SEEK_END);
793         msglen = ftell(fp);
794         rewind(fp);
795         lasttick = time(NULL);
796         strcpy(buf, "");
797         while (ch = getc(fp), (ch >= 0)) {
798                 if (ch == 10) {
799                         if (!strcmp(buf, "000"))
800                                 strcpy(buf, ">000");
801                         serv_puts(buf);
802                         strcpy(buf, "");
803                 } else {
804                         a = strlen(buf);
805                         buf[a + 1] = 0;
806                         buf[a] = ch;
807                         if ((ch == 32) && (strlen(buf) > 200)) {
808                                 buf[a] = 0;
809                                 if (!strcmp(buf, "000"))
810                                         strcpy(buf, ">000");
811                                 serv_puts(buf);
812                                 strcpy(buf, "");
813                         }
814                         if (strlen(buf) > 250) {
815                                 if (!strcmp(buf, "000"))
816                                         strcpy(buf, ">000");
817                                 serv_puts(buf);
818                                 strcpy(buf, "");
819                         }
820                 }
821
822                 if ((time(NULL) - lasttick) > 2L) {
823                         scr_printf(" %3ld%% completed\r",
824                                ((ftell(fp) * 100L) / msglen));
825                         scr_flush();
826                         lasttick = time(NULL);
827                 }
828
829         }
830         serv_puts(buf);
831         scr_printf("                \r");
832         scr_flush();
833 }
834
835
836
837 /*
838  * entmsg()  -  edit and create a message
839  *              returns 0 if message was saved
840  */
841 int entmsg(int is_reply,        /* nonzero if this was a <R>eply command */
842                 int c)          /* */
843 {
844         char buf[300];
845         char cmd[SIZ];
846         int a, b;
847         int need_recp = 0;
848         int mode;
849         long highmsg;
850         FILE *fp;
851
852         if (c > 0)
853                 mode = 1;
854         else
855                 mode = 0;
856
857         /*
858          * First, check to see if we have permission to enter a message in
859          * this room.  The server will return an error code if we can't.
860          */
861         sprintf(cmd, "ENT0 0||0|%d", mode);
862         serv_puts(cmd);
863         serv_gets(cmd);
864
865         if ((strncmp(cmd, "570", 3)) && (strncmp(cmd, "200", 3))) {
866                 scr_printf("%s\n", &cmd[4]);
867                 return (1);
868         }
869
870         /* Error code 570 is special.  It means that we CAN enter a message
871          * in this room, but a recipient needs to be specified.
872          */
873         need_recp = 0;
874         if (!strncmp(cmd, "570", 3))
875                 need_recp = 1;
876
877         /* If the user is a dumbass, tell them how to type. */
878         if ((userflags & US_EXPERT) == 0) {
879                 formout("entermsg");
880         }
881
882         /* Handle the selection of a recipient, if necessary. */
883         strcpy(buf, "");
884         if (need_recp == 1) {
885                 if (axlevel >= 2) {
886                         if (is_reply) {
887                                 strcpy(buf, reply_to);
888                         } else {
889                                 scr_printf("Enter recipient: ");
890                                 getline(buf, (SIZ-100) );
891                                 if (strlen(buf) == 0)
892                                         return (1);
893                         }
894                 } else
895                         strcpy(buf, "sysop");
896         }
897
898         b = 0;
899         if (room_flags & QR_ANONOPT) {
900                 scr_printf("Anonymous (Y/N)? ");
901                 if (yesno() == 1)
902                         b = 1;
903         }
904
905         /* If it's mail, we've got to check the validity of the recipient... */
906         if (strlen(buf) > 0) {
907                 sprintf(cmd, "ENT0 0|%s|%d|%d", buf, b, mode);
908                 serv_puts(cmd);
909                 serv_gets(cmd);
910                 if (cmd[0] != '2') {
911                         scr_printf("%s\n", &cmd[4]);
912                         return (1);
913                 }
914         }
915
916         /* Learn the number of the newest message in in the room, so we can
917          * tell upon saving whether someone else has posted too.
918          */
919         num_msgs = 0;
920         serv_puts("MSGS LAST|1");
921         serv_gets(cmd);
922         if (cmd[0] != '1') {
923                 scr_printf("%s\n", &cmd[5]);
924         } else {
925                 while (serv_gets(cmd), strcmp(cmd, "000")) {
926                         msg_arr[num_msgs++] = atol(cmd);
927                 }
928         }
929
930         /* Now compose the message... */
931         if (client_make_message(temp, buf, b, 0, c) != 0) {
932                 return (2);
933         }
934
935         /* Reopen the temp file that was created, so we can send it */
936         fp = fopen(temp, "r");
937
938         /* Yes, unlink it now, so it doesn't stick around if we crash */
939         unlink(temp);
940
941         if (fp == NULL) {
942                 err_printf("*** Internal error while trying to save message!\n"
943                         "    %s: %s\n",
944                         temp, strerror(errno));
945                 return(errno);
946         }
947
948         /* Transmit message to the server */
949         sprintf(cmd, "ENT0 1|%s|%d|%d||", buf, b, mode);
950         serv_puts(cmd);
951         serv_gets(cmd);
952         if (cmd[0] != '4') {
953                 scr_printf("%s\n", &cmd[4]);
954                 return (1);
955         }
956
957         transmit_message(fp);
958         serv_puts("000");
959
960         fclose(fp);
961
962         highmsg = msg_arr[num_msgs - 1];
963         num_msgs = 0;
964         serv_puts("MSGS NEW");
965         serv_gets(cmd);
966         if (cmd[0] != '1') {
967                 scr_printf("%s\n", &cmd[5]);
968         } else {
969                 while (serv_gets(cmd), strcmp(cmd, "000")) {
970                         msg_arr[num_msgs++] = atol(cmd);
971                 }
972         }
973
974         /* get new highest message number in room to set lrp for goto... */
975         maxmsgnum = msg_arr[num_msgs - 1];
976
977         /* now see if anyone else has posted in here */
978         b = (-1);
979         for (a = 0; a < num_msgs; ++a) {
980                 if (msg_arr[a] > highmsg) {
981                         ++b;
982                 }
983         }
984
985         /* In the Mail> room, this algorithm always counts one message
986          * higher than in public rooms, so we decrement it by one.
987          */
988         if (need_recp) {
989                 --b;
990         }
991
992         if (b == 1) {
993                 scr_printf("*** 1 additional message has been entered "
994                         "in this room by another user.\n");
995         }
996         else if (b > 1) {
997                 scr_printf("*** %d additional messages have been entered "
998                         "in this room by other users.\n", b);
999         }
1000
1001         return(0);
1002 }
1003
1004 /*
1005  * Do editing on a quoted file
1006  */
1007 void process_quote(void)
1008 {
1009         FILE *qfile, *tfile;
1010         char buf[128];
1011         int line, qstart, qend;
1012
1013         /* Unlink the second temp file as soon as it's opened, so it'll get
1014          * deleted even if the program dies
1015          */
1016         qfile = fopen(temp2, "r");
1017         unlink(temp2);
1018
1019         /* Display the quotable text with line numbers added */
1020         line = 0;
1021         fgets(buf, 128, qfile);
1022         while (fgets(buf, 128, qfile) != NULL) {
1023                 scr_printf("%2d %s", ++line, buf);
1024         }
1025         scr_printf("Begin quoting at [ 1] : ");
1026         getline(buf, 3);
1027         qstart = (buf[0] == 0) ? (1) : atoi(buf);
1028         scr_printf("  End quoting at [%d] : ", line);
1029         getline(buf, 3);
1030         qend = (buf[0] == 0) ? (line) : atoi(buf);
1031         rewind(qfile);
1032         line = 0;
1033         fgets(buf, 128, qfile);
1034         tfile = fopen(temp, "w");
1035         while (fgets(buf, 128, qfile) != NULL) {
1036                 if ((++line >= qstart) && (line <= qend))
1037                         fprintf(tfile, " >%s", buf);
1038         }
1039         fprintf(tfile, " \n");
1040         fclose(qfile);
1041         fclose(tfile);
1042         chmod(temp, 0666);
1043 }
1044
1045
1046
1047 /*
1048  * List the URL's which were embedded in the previous message
1049  */
1050 void list_urls()
1051 {
1052         int i;
1053         char cmd[SIZ];
1054
1055         if (num_urls == 0) {
1056                 scr_printf("There were no URL's in the previous message.\n\n");
1057                 return;
1058         }
1059
1060         for (i = 0; i < num_urls; ++i) {
1061                 scr_printf("%3d %s\n", i + 1, urls[i]);
1062         }
1063
1064         if ((i = num_urls) != 1)
1065                 i = intprompt("Display which one", 1, 1, num_urls);
1066
1067         sprintf(cmd, rc_url_cmd, urls[i - 1]);
1068         system(cmd);
1069         scr_printf("\n");
1070 }
1071
1072 /*
1073  * Read the messages in the current room
1074  */
1075 void readmsgs(
1076         int c,          /* 0=Read all  1=Read new  2=Read old 3=Read last q */
1077         int rdir,       /* 1=Forward (-1)=Reverse */
1078         int q           /* Number of msgs to read (if c==3) */
1079 ) {
1080         int a, b, e, f, g, start;
1081         int savedpos;
1082         int hold_sw = 0;
1083         char arcflag = 0;
1084         char quotflag = 0;
1085         int hold_color = 0;
1086         char prtfile[PATH_MAX];
1087         char pagin;
1088         char cmd[SIZ];
1089         char targ[ROOMNAMELEN];
1090         char filename[SIZ];
1091
1092         if (c < 0)
1093                 b = (MAXMSGS - 1);
1094         else
1095                 b = 0;
1096
1097         strcpy(prtfile, tmpnam(NULL));
1098
1099         num_msgs = 0;
1100         strcpy(cmd, "MSGS ");
1101         switch (c) {
1102         case 0:
1103                 strcat(cmd, "ALL");
1104                 break;
1105         case 1:
1106                 strcat(cmd, "NEW");
1107                 break;
1108         case 2:
1109                 strcat(cmd, "OLD");
1110                 break;
1111         case 3:
1112                 sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
1113                 break;
1114         }
1115         serv_puts(cmd);
1116         serv_gets(cmd);
1117         if (cmd[0] != '1') {
1118                 scr_printf("%s\n", &cmd[5]);
1119         } else {
1120                 while (serv_gets(cmd), strcmp(cmd, "000")) {
1121                         if (num_msgs == MAXMSGS) {
1122                                 memcpy(&msg_arr[0], &msg_arr[1],
1123                                        (sizeof(long) * (MAXMSGS - 1)));
1124                                 --num_msgs;
1125                         }
1126                         msg_arr[num_msgs++] = atol(cmd);
1127                 }
1128         }
1129
1130         if (num_msgs == 0) {
1131                 if (c == 3) return;
1132                 scr_printf("*** There are no ");
1133                 if (c == 1) scr_printf("new ");
1134                 if (c == 2) scr_printf("old ");
1135                 scr_printf("messages in this room.\n");
1136                 return;
1137         }
1138
1139         lines_printed = 0;
1140
1141         /* this loop cycles through each message... */
1142         start = ((rdir == 1) ? 0 : (num_msgs - 1));
1143         for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1144                 while (msg_arr[a] == 0L) {
1145                         a = a + rdir;
1146                         if ((a == MAXMSGS) || (a == (-1)))
1147                                 return;
1148                 }
1149
1150 RAGAIN:         pagin = ((arcflag == 0)
1151                          && (quotflag == 0)
1152                          && (userflags & US_PAGINATOR)) ? 1 : 0;
1153
1154                 /* If we're doing a quote, set the screenwidth to 72 */
1155                 if (quotflag) {
1156                         hold_sw = screenwidth;
1157                         screenwidth = 72;
1158                 }
1159
1160                 /* If printing or archiving, set the screenwidth to 80 */
1161                 if (arcflag) {
1162                         hold_sw = screenwidth;
1163                         screenwidth = 80;
1164                 }
1165
1166                 /* now read the message... */
1167                 e = read_message(msg_arr[a], pagin);
1168
1169                 /* ...and set the screenwidth back if we have to */
1170                 if ((quotflag) || (arcflag)) {
1171                         screenwidth = hold_sw;
1172                 }
1173 RMSGREAD:       scr_flush();
1174                 highest_msg_read = msg_arr[a];
1175                 if (quotflag) {
1176                         freopen("/dev/tty", "r+", stdout);
1177                         quotflag = 0;
1178                         enable_color = hold_color;
1179                         process_quote();
1180                 }
1181                 if (arcflag) {
1182                         freopen("/dev/tty", "r+", stdout);
1183                         arcflag = 0;
1184                         enable_color = hold_color;
1185                         f = fork();
1186                         if (f == 0) {
1187                                 freopen(prtfile, "r", stdin);
1188                                 screen_reset();
1189                                 sttybbs(SB_RESTORE);
1190                                 ka_system(printcmd);
1191                                 sttybbs(SB_NO_INTR);
1192                                 screen_set();
1193                                 unlink(prtfile);
1194                                 exit(0);
1195                         }
1196                         if (f > 0)
1197                                 do {
1198                                         g = wait(NULL);
1199                                 } while ((g != f) && (g >= 0));
1200                         scr_printf("Message printed.\n");
1201                 }
1202                 if (rc_alt_semantics && c == 1) {
1203                         char buf[SIZ];
1204
1205                         snprintf(buf, sizeof(buf), "SEEN %ld", msg_arr[a]);
1206                         serv_puts(buf);
1207                         serv_gets(buf); /* Don't need to check this? */
1208                 }
1209                 if (e == 3)
1210                         return;
1211                 if (((userflags & US_NOPROMPT) || (e == 2))
1212                     && (((room_flags & QR_MAILBOX) == 0)
1213                         || (rc_force_mail_prompts == 0))) {
1214                         e = 'n';
1215                 } else {
1216                         color(DIM_WHITE);
1217                         scr_printf("(");
1218                         color(BRIGHT_WHITE);
1219                         scr_printf("%d", num_msgs - a - 1);
1220                         color(DIM_WHITE);
1221                         scr_printf(") ");
1222
1223                         keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top m<Y> next ");
1224                         if (rc_url_cmd[0] && num_urls)
1225                                 keyopt("<U>RLview ");
1226                         keyopt("<?>help -> ");
1227
1228                         do {
1229                                 lines_printed = 2;
1230                                 e = (inkey() & 127);
1231                                 e = tolower(e);
1232 /* return key same as <N> */ if (e == 13)
1233                                         e = 'n';
1234 /* space key same as <N> */ if (e == 32)
1235                                         e = 'n';
1236 /* del/move for aides only */
1237                                     if ((!is_room_aide)
1238                                         && ((room_flags & QR_MAILBOX) ==
1239                                             0)) {
1240                                         if ((e == 'd') || (e == 'm'))
1241                                                 e = 0;
1242                                 }
1243 /* print only if available */
1244                                 if ((e == 'p') && (strlen(printcmd) == 0))
1245                                         e = 0;
1246 /* can't file if not allowed */
1247                                     if ((e == 'f')
1248                                         && (rc_allow_attachments == 0))
1249                                         e = 0;
1250 /* link only if browser avail*/
1251                                     if ((e == 'u')
1252                                         && (strlen(rc_url_cmd) == 0))
1253                                         e = 0;
1254                         } while ((e != 'a') && (e != 'n') && (e != 's')
1255                                  && (e != 'd') && (e != 'm') && (e != 'p')
1256                                  && (e != 'q') && (e != 'b') && (e != 'h')
1257                                  && (e != 'r') && (e != 'f') && (e != '?')
1258                                  && (e != 'u') && (e != 'c') && (e != 'y'));
1259                         switch (e) {
1260                         case 's':
1261                                 scr_printf("Stop");
1262                                 break;
1263                         case 'a':
1264                                 scr_printf("Again");
1265                                 break;
1266                         case 'd':
1267                                 scr_printf("Delete");
1268                                 break;
1269                         case 'm':
1270                                 scr_printf("Move");
1271                                 break;
1272                         case 'c':
1273                                 scr_printf("Copy");
1274                                 break;
1275                         case 'n':
1276                                 scr_printf("Next");
1277                                 break;
1278                         case 'p':
1279                                 scr_printf("Print");
1280                                 break;
1281                         case 'q':
1282                                 scr_printf("Quote");
1283                                 break;
1284                         case 'b':
1285                                 scr_printf("Back");
1286                                 break;
1287                         case 'h':
1288                                 scr_printf("Header");
1289                                 break;
1290                         case 'r':
1291                                 scr_printf("Reply");
1292                                 break;
1293                         case 'f':
1294                                 scr_printf("File");
1295                                 break;
1296                         case 'u':
1297                                 scr_printf("URL's");
1298                                 break;
1299                         case 'y':
1300                                 scr_printf("mY next");
1301                                 break;
1302                         case '?':
1303                                 scr_printf("? <help>");
1304                                 break;
1305                         }
1306                         if (userflags & US_DISAPPEAR)
1307                                 scr_printf("\r%79s\r", "");
1308                         else
1309                                 scr_printf("\n");
1310                         scr_flush();
1311                 }
1312                 switch (e) {
1313                 case '?':
1314                         scr_printf("Options available here:\n"
1315                                 " ?  Help (prints this message)\n"
1316                                 " S  Stop reading immediately\n"
1317                                 " A  Again (repeats last message)\n"
1318                                 " N  Next (continue with next message)\n"
1319                                 " Y  My Next (continue with next message you authored)\n"
1320                                 " B  Back (go back to previous message)\n");
1321                         if ((is_room_aide)
1322                             || (room_flags & QR_MAILBOX)) {
1323                                 scr_printf(" D  Delete this message\n"
1324                                         " M  Move message to another room\n");
1325                         }
1326                         scr_printf(" C  Copy message to another room\n");
1327                         if (strlen(printcmd) > 0)
1328                                 scr_printf(" P  Print this message\n");
1329                         scr_printf(
1330                                 " Q  Quote portions of this message for your next post\n"
1331                                 " H  Headers (display message headers only)\n");
1332                         if (is_mail)
1333                                 scr_printf(" R  Reply to this message\n");
1334                         if (rc_allow_attachments)
1335                                 scr_printf
1336                                     (" F  (save attachments to a file)\n");
1337                         if (strlen(rc_url_cmd) > 0)
1338                                 scr_printf(" U  (list URL's for display)\n");
1339                         scr_printf("\n");
1340                         goto RMSGREAD;
1341                 case 'p':
1342                         scr_flush();
1343                         freopen(prtfile, "w", stdout);
1344                         arcflag = 1;
1345                         hold_color = enable_color;
1346                         enable_color = 0;
1347                         goto RAGAIN;
1348                 case 'q':
1349                         scr_flush();
1350                         freopen(temp2, "w", stdout);
1351                         quotflag = 1;
1352                         hold_color = enable_color;
1353                         enable_color = 0;
1354                         goto RAGAIN;
1355                 case 's':
1356                         return;
1357                 case 'a':
1358                         goto RAGAIN;
1359                 case 'b':
1360                         a = a - (rdir * 2);
1361                         break;
1362                 case 'm':
1363                 case 'c':
1364                         newprompt("Enter target room: ",
1365                                   targ, ROOMNAMELEN - 1);
1366                         if (strlen(targ) > 0) {
1367                                 sprintf(cmd, "MOVE %ld|%s|%d",
1368                                         msg_arr[a], targ,
1369                                         (e == 'c' ? 1 : 0));
1370                                 serv_puts(cmd);
1371                                 serv_gets(cmd);
1372                                 scr_printf("%s\n", &cmd[4]);
1373                                 if (cmd[0] == '2')
1374                                         msg_arr[a] = 0L;
1375                         } else {
1376                                 goto RMSGREAD;
1377                         }
1378                         if (cmd[0] != '2')
1379                                 goto RMSGREAD;
1380                         break;
1381                 case 'f':
1382                         newprompt("Which section? ", filename,
1383                                   ((sizeof filename) - 1));
1384                         snprintf(cmd, sizeof cmd,
1385                                  "OPNA %ld|%s", msg_arr[a], filename);
1386                         serv_puts(cmd);
1387                         serv_gets(cmd);
1388                         if (cmd[0] == '2') {
1389                                 extract(filename, &cmd[4], 2);
1390                                 download_to_local_disk(filename,
1391                                                        extract_int(&cmd[4],
1392                                                                    0));
1393                         } else {
1394                                 scr_printf("%s\n", &cmd[4]);
1395                         }
1396                         goto RMSGREAD;
1397                 case 'd':
1398                         scr_printf("*** Delete this message? ");
1399                         if (yesno() == 1) {
1400                                 sprintf(cmd, "DELE %ld", msg_arr[a]);
1401                                 serv_puts(cmd);
1402                                 serv_gets(cmd);
1403                                 scr_printf("%s\n", &cmd[4]);
1404                                 if (cmd[0] == '2')
1405                                         msg_arr[a] = 0L;
1406                         } else {
1407                                 goto RMSGREAD;
1408                         }
1409                         break;
1410                 case 'h':
1411                         read_message(msg_arr[a], READ_HEADER);
1412                         goto RMSGREAD;
1413                 case 'r':
1414                         savedpos = num_msgs;
1415                         entmsg(1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1416                         num_msgs = savedpos;
1417                         goto RMSGREAD;
1418                 case 'u':
1419                         list_urls();
1420                         goto RMSGREAD;
1421             case 'y':
1422           { /* hack hack hack */
1423             /* find the next message by me, stay here if we find nothing */
1424             int finda;
1425             int lasta = a;
1426             for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1427               {
1428                 /* this is repetitivly dumb, but that's what computers are for.
1429                    We have to load up messages until we find one by us */
1430                 char buf[SIZ];
1431                 int founda = 0;
1432                 
1433                 sprintf(buf, "MSG0 %ld|%d", msg_arr[finda], 1); /* read the header so we can get 'from=' */
1434                 serv_puts(buf);
1435                 serv_gets(buf);
1436                 while (serv_gets(buf), strcmp(buf, "000")) 
1437                   {
1438                         if ((!strncasecmp(buf, "from=", 5)) && (finda != a)) /* Skip current message. */
1439                       { 
1440                         if (strcasecmp(buf+5, fullname) == 0)
1441                           {
1442                             a = lasta; /* meesa current */
1443                             founda = 1;
1444                           }
1445                           }
1446                   }
1447                     // we are now in synch with the server
1448                 if (founda)
1449                   break; /* for */
1450                 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1451               } /* for */
1452           } /* case 'y' */
1453       } /* switch */
1454         }                       /* end for loop */
1455 }                               /* end read routine */
1456
1457
1458
1459
1460 /*
1461  * View and edit a system message
1462  */
1463 void edit_system_message(char *which_message)
1464 {
1465         char desc[64];
1466         char read_cmd[64];
1467         char write_cmd[64];
1468
1469         sprintf(desc, "system message '%s'", which_message);
1470         sprintf(read_cmd, "MESG %s", which_message);
1471         sprintf(write_cmd, "EMSG %s", which_message);
1472         do_edit(desc, read_cmd, "NOOP", write_cmd);
1473 }
1474
1475
1476
1477
1478 /*
1479  * Verify the message base
1480  */
1481 void check_message_base(void)
1482 {
1483         char buf[SIZ];
1484
1485         scr_printf
1486             ("Please read the documentation before running this command.\n"
1487             "Having done so, do you still want to check the message base? ");
1488         if (yesno() == 0)
1489                 return;
1490
1491         serv_puts("FSCK");
1492         serv_gets(buf);
1493         if (buf[0] != '1') {
1494                 scr_printf("%s\n", &buf[4]);
1495                 return;
1496         }
1497
1498         while (serv_gets(buf), strcmp(buf, "000")) {
1499                 scr_printf("%s\n", buf);
1500         }
1501 }