Fix more warnings; output errors to the users.
[citadel.git] / citadel / textclient / messages.c
index 0f0636d673ee3d1f6fd674678009da31415b02ca..3935355cc042e7a73e80bf58ae9a8e89f41d7aa7 100644 (file)
@@ -363,7 +363,10 @@ void citedit(FILE *fp)
        putc(10, fp);
        fflush(fp);
        rv = ftruncate(fileno(fp), ftell(fp));
+       if (rv < 0)
+               scr_printf("failed to set message buffer: %s\n", strerror(errno));
 
+       
        /* and deallocate the memory we used */
        while (textlist != NULL) {
                ptr = textlist->next;
@@ -427,8 +430,6 @@ int read_message(CtdlIPC *ipc,
        r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
        if (r / 100 != 1) {
                scr_printf("*** msg #%ld: %d %s\n", num, r, buf);
-               ++lines_printed;
-               lines_printed = checkpagin(lines_printed, pagin, screenheight);
                stty_ctdl(0);
                free(message->text);
                free_parts(message->attachments);
@@ -440,8 +441,6 @@ int read_message(CtdlIPC *ipc,
                fprintf(dest, "\n ");
        } else {
                scr_printf("\n");
-               ++lines_printed;
-               lines_printed = checkpagin(lines_printed, pagin, screenheight);
                if (pagin != 2)
                        scr_printf(" ");
        }
@@ -451,34 +450,34 @@ int read_message(CtdlIPC *ipc,
 
        /* View headers only */
        if (pagin == 2) {
-               pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
+               scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
                                message->nhdr ? "yes" : "no",
                                message->author, message->type,
                                message->msgid);
                if (!IsEmptyStr(message->subject)) {
-                       pprintf("subj=%s\n", message->subject);
+                       scr_printf("subj=%s\n", message->subject);
                }
                if (!IsEmptyStr(message->email)) {
-                       pprintf("rfca=%s\n", message->email);
+                       scr_printf("rfca=%s\n", message->email);
                }
-               pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
+               scr_printf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
                                message->hnod, message->room,
                                message->node, 
                                asctime(localtime(&message->time)));
                if (!IsEmptyStr(message->recipient)) {
-                       pprintf("rcpt=%s\n", message->recipient);
+                       scr_printf("rcpt=%s\n", message->recipient);
                }
                if (message->attachments) {
                        struct parts *ptr;
 
                        for (ptr = message->attachments; ptr; ptr = ptr->next) {
-                               pprintf("part=%s|%s|%s|%s|%s|%ld\n",
+                               scr_printf("part=%s|%s|%s|%s|%s|%ld\n",
                                        ptr->name, ptr->filename, ptr->number,
                                        ptr->disposition, ptr->mimetype,
                                        ptr->length);
                        }
                }
-               pprintf("\n");
+               scr_printf("\n");
                stty_ctdl(0);
                free(message->text);
                free_parts(message->attachments);
@@ -602,12 +601,6 @@ int read_message(CtdlIPC *ipc,
                         message->author, message->node);
        }
 
-       if (!dest) {
-               ++lines_printed;
-               lines_printed = checkpagin(lines_printed, pagin, screenheight);
-       }
-
-
        if (message->msgid != NULL) {
                safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
        }
@@ -627,9 +620,6 @@ int read_message(CtdlIPC *ipc,
                                scr_printf("Subject: ");
                                color(BRIGHT_CYAN);
                                scr_printf("%s\n", message->subject);
-                               ++lines_printed;
-                               lines_printed = checkpagin(lines_printed,
-                                               pagin, screenheight);
                        }
                }
        }
@@ -682,8 +672,7 @@ int read_message(CtdlIPC *ipc,
         * Here we go
         */
        if (format_type == 0) {
-               fr = fmout(screenwidth, NULL, message->text, dest,
-                          ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
+               fr = fmout(screenwidth, NULL, message->text, dest, 1);
        } else {
                /* renderer for text/plain */
 
@@ -706,11 +695,6 @@ int read_message(CtdlIPC *ipc,
                                        fprintf(dest, "%s\n", lineptr);
                                } else {
                                        scr_printf("%s\n", lineptr);
-                                       lines_printed = lines_printed + 1 +
-                                           (linelen / screenwidth);
-                                       lines_printed =
-                                           checkpagin(lines_printed, pagin,
-                                                      screenheight);
                                }
                        }
                        if (lineptr[0] == 0) final_line_is_blank = 1;
@@ -725,8 +709,6 @@ int read_message(CtdlIPC *ipc,
                }
                else {
                        scr_printf("\n");
-                       ++lines_printed;
-                       lines_printed = checkpagin(lines_printed, pagin, screenheight);
                        fr = sigcaught;         
                }
        }
@@ -744,15 +726,15 @@ int read_message(CtdlIPC *ipc,
                                   && (!IsEmptyStr(ptr->mimetype))
                                ) {
                                        color(DIM_WHITE);
-                                       pprintf("Part ");
+                                       scr_printf("Part ");
                                        color(BRIGHT_MAGENTA);
-                                       pprintf("%s", ptr->number);
+                                       scr_printf("%s", ptr->number);
                                        color(DIM_WHITE);
-                                       pprintf(": ");
+                                       scr_printf(": ");
                                        color(BRIGHT_CYAN);
-                                       pprintf("%s", ptr->filename);
+                                       scr_printf("%s", ptr->filename);
                                        color(DIM_WHITE);
-                                       pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
+                                       scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
                                        if (!strncmp(ptr->mimetype, "image/", 6)) {
                                                has_images++;
                                        }
@@ -822,6 +804,10 @@ void replace_string(char *filename, long int startpos)
                        rpos = ftell(fp);
                        fseek(fp, wpos, 0);
                        rv = fwrite((char *) buf, 128, 1, fp);
+                       if (rv < 0) {
+                               scr_printf("failed to replace string: %s\n", strerror(errno));
+                               break; /*whoopsi! */
+                       }
                        strcpy(buf, &buf[128]);
                        wpos = ftell(fp);
                        fseek(fp, rpos, 0);
@@ -898,8 +884,6 @@ int client_make_message(CtdlIPC *ipc,
                newprompt("Subject: ", subject, 70);
        }
 
-       beg = 0L;
-
        if (mode == 1) {
                scr_printf("(Press ctrl-d when finished)\n");
        }
@@ -907,9 +891,11 @@ int client_make_message(CtdlIPC *ipc,
        if (mode == 0) {
                fp = fopen(filename, "r");
                if (fp != NULL) {
-                       fmout(screenwidth, fp, NULL, NULL, 0,
-                               screenheight, 0, 0);
+                       fmout(screenwidth, fp, NULL, NULL, 0);
                        beg = ftell(fp);
+                       if (beg < 0)
+                               scr_printf("failed to get stream position %s\n", 
+                                          strerror(errno));
                        fclose(fp);
                } else {
                        fp = fopen(filename, "w");
@@ -964,7 +950,6 @@ ME1:        switch (mode) {
        case 2:
        default:        /* allow 2+ modes */
                e_ex_code = 1;  /* start with a failed exit code */
-               screen_reset();
                stty_ctdl(SB_RESTORE);
                editor_pid = fork();
                cksum = file_checksum(filename);
@@ -984,7 +969,6 @@ ME1:        switch (mode) {
                        } while ((b != editor_pid) && (b >= 0));
                editor_pid = (-1);
                stty_ctdl(0);
-               screen_set();
                break;
        }
 
@@ -1023,10 +1007,12 @@ MECR:   if (mode >= 2) {
                }
                fp = fopen(filename, "r");
                if (fp != NULL) {
-                       fmout(screenwidth, fp, NULL, NULL,
-                             ((userflags & US_PAGINATOR) ? 1 : 0),
-                             screenheight, 0, 0);
+                       fmout(screenwidth, fp, NULL, NULL, 0);
+                       /* TODO: why ftell if we ignore the result? */
                        beg = ftell(fp);
+                       if (beg < 0)
+                               scr_printf("failed to get stream position %s\n", 
+                                          strerror(errno));
                        fclose(fp);
                }
                goto MECR;
@@ -1143,6 +1129,37 @@ int entmsg(CtdlIPC *ipc,
                newprompt("Display name for this message: ", message.author, 40);
        }
 
+       if (is_reply) {
+
+               if (!IsEmptyStr(reply_subject)) {
+                       if (!strncasecmp(reply_subject,
+                          "Re: ", 3)) {
+                               strcpy(message.subject, reply_subject);
+                       }
+                       else {
+                               snprintf(message.subject,
+                                       sizeof message.subject,
+                                       "Re: %s",
+                                       reply_subject);
+                       }
+               }
+
+               /* Trim down excessively long lists of thread references.  We eliminate the
+                * second one in the list so that the thread root remains intact.
+                */
+               int rrtok = num_tokens(reply_references, '|');
+               int rrlen = strlen(reply_references);
+               if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
+                       remove_token(reply_references, 1, '|');
+               }
+
+               snprintf(message.references, sizeof message.references, "%s%s%s",
+                       reply_references,
+                       (IsEmptyStr(reply_references) ? "" : "|"),
+                       reply_inreplyto
+               );
+       }
+
        r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
 
        if (r / 100 != 2 && r / 10 != 57) {
@@ -1180,37 +1197,6 @@ int entmsg(CtdlIPC *ipc,
        }
        strcpy(message.recipient, buf);
 
-       if (is_reply) {
-
-               if (!IsEmptyStr(reply_subject)) {
-                       if (!strncasecmp(reply_subject,
-                          "Re: ", 3)) {
-                               strcpy(message.subject, reply_subject);
-                       }
-                       else {
-                               snprintf(message.subject,
-                                       sizeof message.subject,
-                                       "Re: %s",
-                                       reply_subject);
-                       }
-               }
-
-               /* Trim down excessively long lists of thread references.  We eliminate the
-                * second one in the list so that the thread root remains intact.
-                */
-               int rrtok = num_tokens(reply_references, '|');
-               int rrlen = strlen(reply_references);
-               if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
-                       remove_token(reply_references, 1, '|');
-               }
-
-               snprintf(message.references, sizeof message.references, "%s%s%s",
-                       reply_references,
-                       (IsEmptyStr(reply_references) ? "" : "|"),
-                       reply_inreplyto
-               );
-       }
-
        if (room_flags & QR_ANONOPT) {
                scr_printf("Anonymous (Y/N)? ");
                if (yesno() == 1)
@@ -1388,6 +1374,8 @@ void list_urls(CtdlIPC *ipc)
 
        snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
        rv = system(cmd);
+       if (rv != 0) 
+               scr_printf("failed to '%s' by %d\n", cmd, rv);
        scr_printf("\n");
 }
 
@@ -1523,7 +1511,7 @@ void readmsgs(CtdlIPC *ipc,
        enum MessageDirection rdir,     /* 1=Forward (-1)=Reverse */
        int q           /* Number of msgs to read (if c==3) */
 ) {
-       int a, b, e, f, g, start;
+       int a, e, f, g, start;
        int savedpos;
        int hold_sw = 0;
        char arcflag = 0;
@@ -1541,11 +1529,6 @@ void readmsgs(CtdlIPC *ipc,
        static int att_seq = 0;         /* Attachment download sequence number */
        int rv = 0;                     /* silence the stupid warn_unused_result warnings */
 
-       if (c < 0)
-               b = (num_msgs - 1);
-       else
-               b = 0;
-
        CtdlMakeTempFileName(prtfile, sizeof prtfile);
 
        if (msg_arr) {
@@ -1569,8 +1552,6 @@ void readmsgs(CtdlIPC *ipc,
                return;
        }
 
-       lines_printed = 0;
-
        /* this loop cycles through each message... */
        start = ((rdir == 1) ? 0 : (num_msgs - 1));
        for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
@@ -1607,7 +1588,7 @@ RAGAIN:           pagin = ((arcflag == 0)
                if ((quotflag) || (arcflag)) {
                        screenwidth = hold_sw;
                }
-RMSGREAD:      scr_flush();
+RMSGREAD:
                highest_msg_read = msg_arr[a];
                if (quotflag) {
                        fclose(dest);
@@ -1628,11 +1609,9 @@ RMSGREAD:        scr_flush();
                                if (freopen(prtfile, "r", stdin) == NULL) {
                                        /* we probably should handle the error condition here */
                                }
-                               screen_reset();
                                stty_ctdl(SB_RESTORE);
                                ka_system(printcmd);
                                stty_ctdl(SB_NO_INTR);
-                               screen_set();
                                unlink(prtfile);
                                exit(0);
                        }
@@ -1669,7 +1648,6 @@ RMSGREAD: scr_flush();
                        keyopt("<?>help -> ");
 
                        do {
-                               lines_printed = 2;
                                e = (inkey() & 127);
                                e = tolower(e);
 /* return key same as <N> */ if (e == 10)
@@ -1760,7 +1738,6 @@ RMSGREAD: scr_flush();
                                scr_printf("\r%79s\r", "");
                        else
                                scr_printf("\n");
-                       scr_flush();
                }
 DONE_QUOTING:  switch (e) {
                case '?':
@@ -1858,6 +1835,8 @@ DONE_QUOTING:     switch (e) {
                                        save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
                                        snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
                                        rv = system(cmd);
+                                       if (rv != 0)
+                                               scr_printf("failed to save %s Reason %d\n", cmd, rv);
                                }
                                else {                  /* save attachment to disk */
                                        destination_directory(save_to, filename);
@@ -1968,10 +1947,9 @@ void check_message_base(CtdlIPC *ipc)
        }
 
        while (transcript && !IsEmptyStr(transcript)) {
-               lines_printed = 1;
                extract_token(buf, transcript, 0, '\n', sizeof buf);
                remove_token(transcript, 0, '\n');
-               pprintf("%s\n", buf);
+               scr_printf("%s\n", buf);
        }
        if (transcript) free(transcript);
        return;