]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-show.c
python: move the notmuch module section to notes.rst
[notmuch] / notmuch-show.c
index 6a890b2466b841a95014563719b330b29a4b5bf9..d930f94243c0f668db32bca297d6a7943ee352eb 100644 (file)
@@ -727,67 +727,48 @@ format_part_text (const void *ctx, mime_node_t *node,
     GMimeObject *meta = node->envelope_part ?
        GMIME_OBJECT (node->envelope_part) : node->part;
     GMimeContentType *content_type = g_mime_object_get_content_type (meta);
+    const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
+    const char *part_type;
     int i;
 
     if (node->envelope_file) {
        notmuch_message_t *message = node->envelope_file;
-       const char *headers[] = {
-           "Subject", "From", "To", "Cc", "Bcc", "Date"
-       };
-       const char *name, *value;
-       unsigned int i;
-
-       printf ("\fmessage{ ");
-       printf ("id:%s depth:%d match:%d filename:%s\n",
+
+       part_type = "message";
+       printf ("\f%s{ id:%s depth:%d match:%d filename:%s\n",
+               part_type,
                notmuch_message_get_message_id (message),
                indent,
                notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
                notmuch_message_get_filename (message));
-
-       printf ("\fheader{\n");
-
-       printf ("%s\n", _get_one_line_summary (ctx, message));
-
-       for (i = 0; i < ARRAY_SIZE (headers); i++) {
-           name = headers[i];
-           value = notmuch_message_get_header (message, name);
-           if (value && strlen (value))
-               printf ("%s: %s\n", name, value);
-       }
-       printf ("\fheader}\n");
     } else {
        GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
        const char *cid = g_mime_object_get_content_id (meta);
+       const char *filename = leaf ?
+           g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
 
        if (disposition &&
            strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
-       {
-           printf ("\fattachment{ ID: %d", node->part_num);
-
-       } else {
-
-           printf ("\fpart{ ID: %d", node->part_num);
-       }
-
-       if (GMIME_IS_PART (node->part))
-       {
-           const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
-           if (filename)
-               printf (", Filename: %s", filename);
-       }
+           part_type = "attachment";
+       else
+           part_type = "part";
 
+       printf ("\f%s{ ID: %d", part_type, node->part_num);
+       if (filename)
+           printf (", Filename: %s", filename);
        if (cid)
            printf (", Content-id: %s", cid);
-
        printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
     }
 
-    if (node->envelope_part) {
+    if (GMIME_IS_MESSAGE (node->part)) {
        GMimeMessage *message = GMIME_MESSAGE (node->part);
        InternetAddressList *recipients;
        const char *recipients_string;
 
        printf ("\fheader{\n");
+       if (node->envelope_file)
+           printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
        printf ("Subject: %s\n", g_mime_message_get_subject (message));
        printf ("From: %s\n", g_mime_message_get_sender (message));
        recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
@@ -800,9 +781,11 @@ format_part_text (const void *ctx, mime_node_t *node,
            printf ("Cc: %s\n", recipients_string);
        printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
        printf ("\fheader}\n");
+
+       printf ("\fbody{\n");
     }
 
-    if (!node->envelope_file) {
+    if (leaf) {
        if (g_mime_content_type_is_type (content_type, "text", "*") &&
            !g_mime_content_type_is_type (content_type, "text", "html"))
        {
@@ -810,45 +793,19 @@ format_part_text (const void *ctx, mime_node_t *node,
            g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
            show_text_part_content (node->part, stream_stdout);
            g_object_unref(stream_stdout);
-       }
-       else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
-                g_mime_content_type_is_type (content_type, "message", "rfc822"))
-       {
-           /* Do nothing for multipart since its content will be printed
-            * when recursing. */
-       }
-       else
-       {
+       } else {
            printf ("Non-text part: %s\n",
                    g_mime_content_type_to_string (content_type));
        }
     }
 
-    if (GMIME_IS_MESSAGE (node->part))
-       printf ("\fbody{\n");
-
     for (i = 0; i < node->nchildren; i++)
        format_part_text (ctx, mime_node_child (node, i), indent, params);
 
     if (GMIME_IS_MESSAGE (node->part))
        printf ("\fbody}\n");
 
-    if (node->envelope_file) {
-       printf ("\fmessage}\n");
-    } else {
-       GMimeContentDisposition *disposition;
-
-       disposition = g_mime_object_get_content_disposition (meta);
-       if (disposition &&
-           strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
-       {
-           printf ("\fattachment}\n");
-       }
-       else
-       {
-           printf ("\fpart}\n");
-       }
-    }
+    printf ("\f%s}\n", part_type);
 }
 
 static void
@@ -1057,6 +1014,14 @@ do_show (void *ctx,
     return 0;
 }
 
+enum {
+    NOTMUCH_FORMAT_NOT_SPECIFIED,
+    NOTMUCH_FORMAT_JSON,
+    NOTMUCH_FORMAT_TEXT,
+    NOTMUCH_FORMAT_MBOX,
+    NOTMUCH_FORMAT_RAW
+};
+
 int
 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
 {
@@ -1064,92 +1029,94 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     notmuch_database_t *notmuch;
     notmuch_query_t *query;
     char *query_string;
-    char *opt;
+    int opt_index, ret;
     const notmuch_show_format_t *format = &format_text;
-    notmuch_show_params_t params;
-    int mbox = 0;
-    int format_specified = 0;
-    int i;
+    notmuch_show_params_t params = { .part = -1 };
+    int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
+    notmuch_bool_t verify = FALSE;
+
+    notmuch_opt_desc_t options[] = {
+       { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
+         (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
+                                 { "text", NOTMUCH_FORMAT_TEXT },
+                                 { "mbox", NOTMUCH_FORMAT_MBOX },
+                                 { "raw", NOTMUCH_FORMAT_RAW },
+                                 { 0, 0 } } },
+       { NOTMUCH_OPT_INT, &params.part, "part", 'p', 0 },
+       { NOTMUCH_OPT_BOOLEAN, &params.entire_thread, "entire-thread", 't', 0 },
+       { NOTMUCH_OPT_BOOLEAN, &params.decrypt, "decrypt", 'd', 0 },
+       { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },
+       { 0, 0, 0, 0, 0 }
+    };
 
-    params.entire_thread = 0;
-    params.raw = 0;
-    params.part = -1;
-    params.cryptoctx = NULL;
-    params.decrypt = 0;
+    opt_index = parse_arguments (argc, argv, options, 1);
+    if (opt_index < 0) {
+       /* diagnostics already printed */
+       return 1;
+    }
 
-    argc--; argv++; /* skip subcommand argument */
+    if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
+       /* if part was requested and format was not specified, use format=raw */
+       if (params.part >= 0)
+           format_sel = NOTMUCH_FORMAT_RAW;
+       else
+           format_sel = NOTMUCH_FORMAT_TEXT;
+    }
 
-    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
-       if (strcmp (argv[i], "--") == 0) {
-           i++;
-           break;
+    switch (format_sel) {
+    case NOTMUCH_FORMAT_JSON:
+       format = &format_json;
+       params.entire_thread = TRUE;
+       break;
+    case NOTMUCH_FORMAT_TEXT:
+       format = &format_text;
+       break;
+    case NOTMUCH_FORMAT_MBOX:
+       if (params.part > 0) {
+           fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
+           return 1;
        }
-       if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
-           opt = argv[i] + sizeof ("--format=") - 1;
-           if (strcmp (opt, "text") == 0) {
-               format = &format_text;
-           } else if (strcmp (opt, "json") == 0) {
-               format = &format_json;
-               params.entire_thread = 1;
-           } else if (strcmp (opt, "mbox") == 0) {
-               format = &format_mbox;
-               mbox = 1;
-           } else if (strcmp (opt, "raw") == 0) {
-               format = &format_raw;
-               params.raw = 1;
-           } else {
-               fprintf (stderr, "Invalid value for --format: %s\n", opt);
-               return 1;
-           }
-           format_specified = 1;
-       } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
-           params.part = atoi(argv[i] + sizeof ("--part=") - 1);
-       } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
-           params.entire_thread = 1;
-       } else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||
-                  (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
-           if (params.cryptoctx == NULL) {
+       format = &format_mbox;
+       break;
+    case NOTMUCH_FORMAT_RAW:
+       format = &format_raw;
+       /* If --format=raw specified without specifying part, we can only
+        * output single message, so set part=0 */
+       if (params.part < 0)
+           params.part = 0;
+       params.raw = TRUE;
+       break;
+    }
+
+    if (params.decrypt || verify) {
 #ifdef GMIME_ATLEAST_26
-               /* TODO: GMimePasswordRequestFunc */
-               if (NULL == (params.cryptoctx = g_mime_gpg_context_new(NULL, "gpg")))
+       /* TODO: GMimePasswordRequestFunc */
+       params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
 #else
-               GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
-               if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
-#endif
-                   fprintf (stderr, "Failed to construct gpg context.\n");
-               else
-                   g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
-#ifndef GMIME_ATLEAST_26
-               g_object_unref (session);
-               session = NULL;
+       GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
+       params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
 #endif
-           }
-           if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)
-               params.decrypt = 1;
+       if (params.cryptoctx) {
+           g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
        } else {
-           fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
-           return 1;
+           params.decrypt = FALSE;
+           fprintf (stderr, "Failed to construct gpg context.\n");
        }
+#ifndef GMIME_ATLEAST_26
+       g_object_unref (session);
+#endif
     }
 
-    argc -= i;
-    argv += i;
-
     config = notmuch_config_open (ctx, NULL, NULL);
     if (config == NULL)
        return 1;
 
-    query_string = query_string_from_args (ctx, argc, argv);
+    query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
     if (query_string == NULL) {
        fprintf (stderr, "Out of memory\n");
        return 1;
     }
 
-    if (mbox && params.part > 0) {
-       fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
-       return 1;
-    }
-
     if (*query_string == '\0') {
        fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
        return 1;
@@ -1166,19 +1133,10 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
        return 1;
     }
 
-    /* if part was requested and format was not specified, use format=raw */
-    if (params.part >= 0 && !format_specified)
-       format = &format_raw;
-
-    /* If --format=raw specified without specifying part, we can only
-     * output single message, so set part=0 */
-    if (params.raw && params.part < 0)
-       params.part = 0;
-
     if (params.part >= 0)
-       return do_show_single (ctx, query, format, &params);
+       ret = do_show_single (ctx, query, format, &params);
     else
-       return do_show (ctx, query, format, &params);
+       ret = do_show (ctx, query, format, &params);
 
     notmuch_query_destroy (query);
     notmuch_database_close (notmuch);
@@ -1186,5 +1144,5 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     if (params.cryptoctx)
        g_object_unref(params.cryptoctx);
 
-    return 0;
+    return ret;
 }