]> code.citadel.org Git - citadel.git/blobdiff - citadel/messages.c
* Replaced all "Citadel/UX" references with "Citadel"
[citadel.git] / citadel / messages.c
index 4e496aa397bc4d902052b0436026af70a750b158..f29dddfbe6554acea109c2deffd99311a8122ea2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $Id$
  *
- * Citadel/UX message support routines
+ * Citadel message support routines
  * see copyright.txt for copyright information
  */
 
 # endif
 #endif
 
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+
 #include <stdarg.h>
 #include "citadel.h"
 #include "citadel_ipc.h"
@@ -59,16 +63,16 @@ void sttybbs(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;
 int num_msgs;
 char rc_alt_semantics;
+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;
@@ -87,6 +91,13 @@ extern int rc_display_message_numbers;
 extern int rc_force_mail_prompts;
 extern int editor_pid;
 extern CtdlIPC *ipc_for_signal_handlers;       /* KLUDGE cover your eyes */
+int num_urls = 0;
+char urls[MAXURLS][SIZ];
+char imagecmd[SIZ];
+int has_images = 0;                            /* Current msg has images */
+struct parts *last_message_parts = NULL;       /* Parts from last msg */
+
+
 
 void ka_sigcatch(int signum)
 {
@@ -346,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 */
@@ -363,9 +393,14 @@ int read_message(CtdlIPC *ipc,
        char *converted_text = NULL;
        char *lineptr;
        char *nextline;
+       char *searchptr;
+       int i;
+       char ch;
        int linelen;
        int final_line_is_blank = 0;
 
+       has_images = 0;
+
        sigcaught = 0;
        sttybbs(1);
 
@@ -474,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);
@@ -488,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);
@@ -590,6 +625,26 @@ int read_message(CtdlIPC *ipc,
                format_type = 1;
        }
 
+       /* Extract URL's */
+       num_urls = 0;   /* Start with a clean slate */
+       searchptr = message->text;
+       while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
+               searchptr = strstr(searchptr, "http://");
+               if (searchptr != NULL) {
+                       safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
+                       for (i = 0; i < strlen(urls[num_urls]); i++) {
+                               ch = urls[num_urls][i];
+                               if (ch == '>' || ch == '\"' || ch == ')' ||
+                                   ch == ' ' || ch == '\n') {
+                                       urls[num_urls][i] = 0;
+                                       break;
+                               }
+                       }
+                       num_urls++;
+                       ++searchptr;
+               }
+       }
+
        /*
         * Here we go
         */
@@ -650,19 +705,24 @@ 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++;
                        }
                }
        }
 
+       /* Save the attachments info for later */
+       last_message_parts = message->attachments;
+
        /* Now we're done */
        free(message->text);
        free(message);
@@ -852,10 +912,10 @@ ME1:      switch (mode) {
        case 2:
        default:        /* allow 2+ modes */
                e_ex_code = 1;  /* start with a failed exit code */
-               editor_pid = fork();
-               cksum = file_checksum(filename);
                screen_reset();
                sttybbs(SB_RESTORE);
+               editor_pid = fork();
+               cksum = file_checksum(filename);
                if (editor_pid == 0) {
                        char tmp[SIZ];
 
@@ -1134,13 +1194,11 @@ int entmsg(CtdlIPC *ipc,
        /* 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);
        }
 
@@ -1153,6 +1211,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);
@@ -1265,6 +1326,131 @@ void list_urls(CtdlIPC *ipc)
        scr_printf("\n");
 }
 
+
+/*
+ * Run image viewer in background
+ */
+int do_image_view(const char *filename)
+{
+       char cmd[SIZ];
+       pid_t childpid;
+
+       snprintf(cmd, sizeof cmd, imagecmd, filename);
+       childpid = fork();
+       if (childpid < 0) {
+               unlink(filename);
+               return childpid;
+       }
+
+       if (childpid == 0) {
+               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);
+               return retcode;
+       }
+       
+       return -1;
+}
+
+
+/*
+ * View an image attached to a message
+ */
+void image_view(CtdlIPC *ipc, unsigned long msg)
+{
+       struct parts *ptr = last_message_parts;
+       char part[SIZ];
+       int found = 0;
+
+       /* 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)) {
+                       found++;
+                       if (found == 1) {
+                               strcpy(part, ptr->number);
+                       }
+               }
+       }
+
+       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)
+                          && !strcasecmp(ptr->number, part)) {
+                               char tmp[PATH_MAX];
+                               char buf[SIZ];
+                               void *file = NULL; /* The downloaded file */
+                               int r;
+       
+                               // 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(ipc, len, len);
+                                       scr_flush();
+                                       snprintf(tmp, sizeof tmp, "%s.%s",
+                                               tmpnam(NULL),
+                                               ptr->filename);
+                                       save_buffer(file, len, tmp);
+                                       free(file);
+                                       do_image_view(tmp);
+                               }
+                               break;
+                       }
+               }
+               if (found == 1)
+                       break;
+       }
+}
+
 /*
  * Read the messages in the current room
  */
@@ -1344,6 +1530,10 @@ RAGAIN:          pagin = ((arcflag == 0)
                        screenwidth = 80;
                }
 
+               /* clear parts list */
+               free_parts(last_message_parts);
+               last_message_parts = NULL;
+
                /* now read the message... */
                e = read_message(ipc, msg_arr[a], pagin, dest);
 
@@ -1401,9 +1591,11 @@ 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)
+                               keyopt("<I>mages ");
                        keyopt("<?>help -> ");
 
                        do {
@@ -1432,11 +1624,15 @@ RMSGREAD:       scr_flush();
                                    if ((e == 'u')
                                        && (strlen(rc_url_cmd) == 0))
                                        e = 0;
+                               if ((e == 'i')
+                                       && (!strlen(imagecmd) || !has_images))
+                                       e = 0;
                        } while ((e != 'a') && (e != 'n') && (e != 's')
                                 && (e != 'd') && (e != 'm') && (e != 'p')
                                 && (e != 'q') && (e != 'b') && (e != 'h')
                                 && (e != 'r') && (e != 'f') && (e != '?')
-                                && (e != 'u') && (e != 'c') && (e != 'y'));
+                                && (e != 'u') && (e != 'c') && (e != 'y')
+                                && (e != 'i'));
                        switch (e) {
                        case 's':
                                scr_printf("Stop");
@@ -1480,11 +1676,13 @@ RMSGREAD:       scr_flush();
                        case 'y':
                                scr_printf("mY next");
                                break;
+                       case 'i':
+                               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");
@@ -1517,6 +1715,8 @@ RMSGREAD: scr_flush();
                                    (" F  (save attachments to a file)\n");
                        if (strlen(rc_url_cmd) > 0)
                                scr_printf(" U  (list URL's for display)\n");
+                       if (strlen(imagecmd) > 0 && has_images > 0)
+                               scr_printf(" I  Image viewer\n");
                        scr_printf("\n");
                        goto RMSGREAD;
                case 'p':
@@ -1565,12 +1765,21 @@ RMSGREAD:       scr_flush();
                                scr_printf("%s\n", cmd);
                        } else {
                                extract(filename, cmd, 2);
+                               /*
+                                * Part 1 won't have a filename; use the
+                                * subject of the message instead. IO
+                                */
+                               if (!strlen(filename))
+                                       strcpy(filename, reply_subject);
                                destination_directory(save_to, filename);
                                save_buffer(attachment,
                                                extract_unsigned_long(cmd, 0),
                                                save_to);
                        }
-                       if (attachment) free(attachment);
+                       if (attachment) {
+                               free(attachment);
+                               attachment = NULL;
+                       }
                        goto RMSGREAD;
                case 'd':
                        scr_printf("*** Delete this message? ");
@@ -1588,12 +1797,15 @@ RMSGREAD:       scr_flush();
                        goto RMSGREAD;
                case 'r':
                        savedpos = num_msgs;
-                       entmsg(ipc, 1, (DEFAULT_ENTRY == 46 ? 2 : 0));
+                       entmsg(ipc, 1, (rc_reply_extedit ? 2 : 0));
                        num_msgs = savedpos;
                        goto RMSGREAD;
                case 'u':
                        list_urls(ipc);
                        goto RMSGREAD;
+               case 'i':
+                       image_view(ipc, msg_arr[a]);
+                       goto RMSGREAD;
            case 'y':
           { /* hack hack hack */
             /* find the next message by me, stay here if we find nothing */
@@ -1601,29 +1813,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;
                 
-                       snprintf(buf, sizeof buf, "MSG0 %ld|1", msg_arr[finda]); /* read the header so we can get 'from=' */
-               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 */
+               /* read the header so we can get 'from=' */
+               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 */