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