+
+/*
+ * 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();
+ CtdlMakeTempFileName(tmp, sizeof tmp);
+ strcat(tmp, ptr->filename);
+ save_buffer(file, len, tmp);
+ free(file);
+ do_image_view(tmp);
+ }
+ break;
+ }
+ }
+ if (found == 1)
+ break;
+ }
+}
+
+