]> code.citadel.org Git - citadel.git/blobdiff - citadel/messages.c
* Variable names, comments, documentation, etc... removed the acronym 'BBS'
[citadel.git] / citadel / messages.c
index 971afbf1bb67b786850b3cc56140d8feabafac8d..701ad9d2a0a288a992ea2ebad4fe515984d282c4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $Id$
  *
- * Citadel/UX message support routines
+ * Citadel message support routines
  * see copyright.txt for copyright information
  */
 
@@ -59,11 +59,11 @@ struct cittext {
        char text[MAXWORDBUF];
 };
 
-void sttybbs(int cmd);
+void stty_ctdl(int cmd);
 int haschar(const char *st, int ch);
 void getline(char *string, int lim);
 int file_checksum(char *filename);
-void progress(unsigned long curr, unsigned long cmax);
+void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
 
 unsigned long *msg_arr = NULL;
 int msg_arr_size = 0;
@@ -73,7 +73,6 @@ char rc_reply_extedit;
 extern char room_name[];
 extern unsigned room_flags;
 extern long highest_msg_read;
-extern struct CtdlServInfo serv_info;
 extern char temp[];
 extern char temp2[];
 extern int screenwidth;
@@ -358,7 +357,26 @@ void citedit(CtdlIPC *ipc, FILE * fp)
        }
 }
 
-/* Read a message from the server
+
+/*
+ * Free the struct parts
+ */
+void free_parts(struct parts *p)
+{
+       struct parts *a_part = p;
+
+       while (a_part) {
+               struct parts *q;
+
+               q = a_part;
+               a_part = a_part->next;
+               free(q);
+       }
+}
+
+
+/*
+ * Read a message from the server
  */
 int read_message(CtdlIPC *ipc,
        long num,   /* message number */
@@ -384,7 +402,7 @@ int read_message(CtdlIPC *ipc,
        has_images = 0;
 
        sigcaught = 0;
-       sttybbs(1);
+       stty_ctdl(1);
 
        strcpy(reply_to, NO_REPLY_TO);
        strcpy(reply_subject, "");
@@ -397,7 +415,7 @@ int read_message(CtdlIPC *ipc,
                ++lines_printed;
                lines_printed =
                    checkpagin(lines_printed, pagin, screenheight);
-               sttybbs(0);
+               stty_ctdl(0);
                return (0);
        }
 
@@ -444,7 +462,7 @@ int read_message(CtdlIPC *ipc,
                        }
                }
                pprintf("\n");
-               sttybbs(0);
+               stty_ctdl(0);
                return (0);
        }
 
@@ -491,8 +509,8 @@ int read_message(CtdlIPC *ipc,
                }
                if (strlen(message->node)) {
                        if ((room_flags & QR_NETWORK)
-                           || ((strcasecmp(message->node, serv_info.serv_nodename)
-                            && (strcasecmp(message->node, serv_info.serv_fqdn))))) {
+                           || ((strcasecmp(message->node, ipc->ServInfo.nodename)
+                            && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
                                if (strlen(message->email) == 0) {
                                        if (dest) {
                                                fprintf(dest, "@%s ", message->node);
@@ -505,7 +523,7 @@ int read_message(CtdlIPC *ipc,
                                }
                        }
                }
-               if (strcasecmp(message->hnod, serv_info.serv_humannode)
+               if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
                    && (strlen(message->hnod)) && (!strlen(message->email))) {
                        if (dest) {
                                fprintf(dest, "(%s) ", message->hnod);
@@ -687,15 +705,15 @@ int read_message(CtdlIPC *ipc,
                        if ( (!strcasecmp(ptr->disposition, "attachment"))
                           || (!strcasecmp(ptr->disposition, "inline"))) {
                                color(DIM_WHITE);
-                               scr_printf("Part ");
+                               pprintf("Part ");
                                color(BRIGHT_MAGENTA);
-                               scr_printf("%s", ptr->number);
+                               pprintf("%s", ptr->number);
                                color(DIM_WHITE);
-                               scr_printf(": ");
+                               pprintf(": ");
                                color(BRIGHT_CYAN);
-                               scr_printf("%s", ptr->filename);
+                               pprintf("%s", ptr->filename);
                                color(DIM_WHITE);
-                               scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
+                               pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
                                if (!strncmp(ptr->mimetype, "image/", 6))
                                        has_images++;
                        }
@@ -711,7 +729,7 @@ int read_message(CtdlIPC *ipc,
 
        if (pagin == 1 && !dest)
                color(DIM_WHITE);
-       sttybbs(0);
+       stty_ctdl(0);
        return (fr);
 }
 
@@ -800,8 +818,8 @@ int client_make_message(CtdlIPC *ipc,
                } else if (strlen(editor_paths[0]) > 0) {
                        editor_path = editor_paths[0];
                } else {
-                       err_printf
-                           ("*** No editor available, using built-in editor\n");
+                       err_printf("*** No editor available, "
+                               "using built-in editor\n");
                        mode = 0;
                }
        }
@@ -838,7 +856,8 @@ 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,
+                               screenheight, 0, 0);
                        beg = ftell(fp);
                        fclose(fp);
                } else {
@@ -894,10 +913,10 @@ 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);
-               screen_reset();
-               sttybbs(SB_RESTORE);
                if (editor_pid == 0) {
                        char tmp[SIZ];
 
@@ -913,7 +932,7 @@ ME1:        switch (mode) {
                                b = ka_wait(&e_ex_code);
                        } while ((b != editor_pid) && (b >= 0));
                editor_pid = (-1);
-               sttybbs(0);
+               stty_ctdl(0);
                screen_set();
                break;
        }
@@ -1169,20 +1188,19 @@ int entmsg(CtdlIPC *ipc,
        }
 
        /* Now compose the message... */
-       if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject) != 0) {
+       if (client_make_message(ipc, temp, message.recipient,
+          message.anonymous, 0, c, message.subject) != 0) {
                return (2);
        }
 
        /* Reopen the temp file that was created, so we can send it */
        fp = fopen(temp, "r");
 
-       /* Yes, unlink it now, so it doesn't stick around if we crash */
-       unlink(temp);
-
        if (!fp || !(message.text = load_message_from_file(fp))) {
                err_printf("*** Internal error while trying to save message!\n"
                        "%s: %s\n",
                        temp, strerror(errno));
+               unlink(temp);
                return(errno);
        }
 
@@ -1195,6 +1213,9 @@ int entmsg(CtdlIPC *ipc,
                return (1);
        }
 
+       /* Yes, unlink it now, so it doesn't stick around if we crash */
+       unlink(temp);
+
        if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
 
        if (msgarr) free(msgarr);
@@ -1309,36 +1330,64 @@ void list_urls(CtdlIPC *ipc)
 
 
 /*
- * Image viewer thread (for background image viewing)
+ * Run image viewer in background
  */
-void *image_view_thread(void *filename)
+int do_image_view(const char *filename)
 {
        char cmd[SIZ];
        pid_t childpid;
-       int retcode;
 
-       snprintf(cmd, sizeof cmd, imagecmd, (char *)filename);
+       snprintf(cmd, sizeof cmd, imagecmd, filename);
        childpid = fork();
        if (childpid < 0) {
-               color(BRIGHT_RED);
-               perror("Cannot fork");
-               color(DIM_WHITE);
-               unlink((char *)filename);
-               return ((void *) childpid);
+               unlink(filename);
+               return childpid;
        }
 
        if (childpid == 0) {
-               execlp("/bin/sh", "sh", "-c", cmd, NULL);
-               exit(127);
+               int retcode;
+               pid_t grandchildpid;
+
+               grandchildpid = fork();
+               if (grandchildpid < 0) {
+                       return grandchildpid;
+               }
+
+               if (grandchildpid == 0) {
+                       int nullfd;
+                       int outfd = -1;
+                       int errfd = -1;
+
+                       nullfd = open("/dev/null", O_WRONLY);
+                       if (nullfd > -1) {
+                               dup2(1, outfd);
+                               dup2(2, errfd);
+                               dup2(nullfd, 1);
+                               dup2(nullfd, 2);
+                       }
+                       retcode = system(cmd);
+                       if (nullfd > -1) {
+                               dup2(outfd, 1);
+                               dup2(errfd, 2);
+                               close(nullfd);
+                       }
+                       unlink(filename);
+                       exit(retcode);
+               }
+
+               if (grandchildpid > 0) {
+                       exit(0);
+               }
        }
 
        if (childpid > 0) {
+               int retcode;
+
                waitpid(childpid, &retcode, 0);
-               unlink((char *)filename);
-               return ((void *)retcode);
+               return retcode;
        }
-
-       return ((void *)-1);
+       
+       return -1;
 }
 
 
@@ -1347,73 +1396,59 @@ void *image_view_thread(void *filename)
  */
 void image_view(CtdlIPC *ipc, unsigned long msg)
 {
-       char selected_part[SIZ];
        struct parts *ptr = last_message_parts;
+       char part[SIZ];
        int found = 0;
 
-       scr_printf("\n");
-       /* List available parts */
+       /* Run through available parts */
        for (ptr = last_message_parts; ptr; ptr = ptr->next) {
                if ((!strcasecmp(ptr->disposition, "attachment")
                   || !strcasecmp(ptr->disposition, "inline"))
                   && !strncmp(ptr->mimetype, "image/", 6)) {
-                       color(DIM_WHITE);
-                       scr_printf("Part ");
-                       color(BRIGHT_MAGENTA);
-                       scr_printf("%s", ptr->number);
-                       if (!found) {
-                               found = 1;
-                               strncpy(selected_part, ptr->number, SIZ-1);
+                       found++;
+                       if (found == 1) {
+                               strcpy(part, ptr->number);
                        }
-                       color(DIM_WHITE);
-                       scr_printf(": ");
-                       color(BRIGHT_CYAN);
-                       scr_printf("%s", ptr->filename);
-                       color(DIM_WHITE);
-                       scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
                }
        }
 
-       while (found) {
-               found = 0;
-               strprompt("View which part (0 when done)", selected_part, SIZ-1);
+       while (found > 0) {
+               if (found > 1)
+                       strprompt("View which part (0 when done)", part, SIZ-1);
+               found = -found;
                for (ptr = last_message_parts; ptr; ptr = ptr->next) {
                        if ((!strcasecmp(ptr->disposition, "attachment")
                           || !strcasecmp(ptr->disposition, "inline"))
                           && !strncmp(ptr->mimetype, "image/", 6)
-                          && !strcmp(ptr->number, selected_part)) {
+                          && !strcasecmp(ptr->number, part)) {
                                char tmp[PATH_MAX];
                                char buf[SIZ];
                                void *file = NULL; /* The downloaded file */
-#ifdef THREADED_CLIENT
-                               pthread_t *ivthread = NULL;
-#endif
                                int r;
-
-                               // view image
-                               found = 1;
-                               r = CtdlIPCAttachmentDownload(ipc, msg, selected_part, &file, progress, buf);
+       
+                               /* view image */
+                               found = -found;
+                               r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
                                if (r / 100 != 2) {
                                        scr_printf("%s\n", buf);
                                } else {
                                        size_t len;
-
+       
                                        len = (size_t)extract_long(buf, 0);
-                                       progress(len, len);
+                                       progress(ipc, len, len);
                                        scr_flush();
-                                       strcpy(tmp, tmpnam(NULL));
+                                       snprintf(tmp, sizeof tmp, "%s.%s",
+                                               tmpnam(NULL),
+                                               ptr->filename);
                                        save_buffer(file, len, tmp);
                                        free(file);
-                                       #if 0
-                                       pthread_create(ivthread, NULL, image_view_thread, tmp);
-                                       #endif
-                                       snprintf(buf, sizeof buf, imagecmd, tmp);
-                                       system(buf);
-                                       unlink(tmp);
+                                       do_image_view(tmp);
                                }
                                break;
                        }
                }
+               if (found == 1)
+                       break;
        }
 }
  
@@ -1498,7 +1533,7 @@ RAGAIN:           pagin = ((arcflag == 0)
                }
 
                /* clear parts list */
-               /* FIXME free() the old parts list */
+               free_parts(last_message_parts);
                last_message_parts = NULL;
 
                /* now read the message... */
@@ -1526,9 +1561,9 @@ RMSGREAD: scr_flush();
                        if (f == 0) {
                                freopen(prtfile, "r", stdin);
                                screen_reset();
-                               sttybbs(SB_RESTORE);
+                               stty_ctdl(SB_RESTORE);
                                ka_system(printcmd);
-                               sttybbs(SB_NO_INTR);
+                               stty_ctdl(SB_NO_INTR);
                                screen_set();
                                unlink(prtfile);
                                exit(0);
@@ -1558,7 +1593,7 @@ RMSGREAD: scr_flush();
                        color(DIM_WHITE);
                        scr_printf(") ");
 
-                       keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top m<Y> next ");
+                       keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
                        if (rc_url_cmd[0] && num_urls)
                                keyopt("<U>RLview ");
                        if (has_images > 0 && strlen(imagecmd) > 0)
@@ -1644,13 +1679,12 @@ RMSGREAD:       scr_flush();
                                scr_printf("mY next");
                                break;
                        case 'i':
-                               scr_printf("Images");
                                break;
                        case '?':
                                scr_printf("? <help>");
                                break;
                        }
-                       if (userflags & US_DISAPPEAR)
+                       if (userflags & US_DISAPPEAR || e == 'i')
                                scr_printf("\r%79s\r", "");
                        else
                                scr_printf("\n");
@@ -1732,7 +1766,7 @@ RMSGREAD: scr_flush();
                        if (r / 100 != 2) {
                                scr_printf("%s\n", cmd);
                        } else {
-                               extract(filename, cmd, 2);
+                               extract_token(filename, cmd, 2, '|', sizeof filename);
                                /*
                                 * Part 1 won't have a filename; use the
                                 * subject of the message instead. IO
@@ -1781,30 +1815,24 @@ RMSGREAD:       scr_flush();
             int lasta = a;
             for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
               {
-                /* This is repetitively dumb, but that's what computers are for.
-                   We have to load up messages until we find one by us */
-                char buf[SIZ];
-                int founda = 0;
+               /* This is repetitively dumb, but that's what computers are for.
+                  We have to load up messages until we find one by us */
+               char buf[SIZ];
+               int founda = 0;
+               struct ctdlipcmessage *msg = NULL;
                 
                /* read the header so we can get 'from=' */
-                       snprintf(buf, sizeof buf, "MSG0 %ld|1", msg_arr[finda]);
-               CtdlIPC_putline(ipc, buf);
-               CtdlIPC_getline(ipc, buf);
-               while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) 
-                  {
-                       if ((!strncasecmp(buf, "from=", 5)) && (finda != a)) /* Skip current message. */
-                     { 
-                        if (strcasecmp(buf+5, fullname) == 0)
-                          {
-                            a = lasta; /* meesa current */
-                            founda = 1;
-                          }
-                         }
-                 }
-                   /* we are now in synch with the server */
-                if (founda)
-                  break; /* for */
-                lasta = finda; /* keep one behind or we skip on the reentrance to the for */
+               r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
+               if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
+                       a = lasta; /* meesa current */
+                       founda = 1;
+               }
+
+               free(msg);
+
+               if (founda)
+                       break; /* for */
+               lasta = finda; /* keep one behind or we skip on the reentrance to the for */
               } /* for */
           } /* case 'y' */
       } /* switch */
@@ -1855,7 +1883,7 @@ void check_message_base(CtdlIPC *ipc)
 
        while (transcript && strlen(transcript)) {
                lines_printed = 1;
-               extract_token(buf, transcript, 0, '\n');
+               extract_token(buf, transcript, 0, '\n', sizeof buf);
                remove_token(transcript, 0, '\n');
                pprintf("%s\n", buf);
        }