From 88c9fbf7dbee5f4dfab9d991a721ad1255e2a5bd Mon Sep 17 00:00:00 2001 From: Markus Koch Date: Wed, 15 Feb 2017 17:50:23 +0100 Subject: [PATCH] Added support for gpg encrypted files --- src/mdiary.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 10 deletions(-) diff --git a/src/mdiary.c b/src/mdiary.c index 1256870..bbfef47 100644 --- a/src/mdiary.c +++ b/src/mdiary.c @@ -217,13 +217,48 @@ static GList *mdiary_add_tags_from_string(gchar *string) return ret; } -static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListStore, GtkListStore *autoCompletion) +/** + * @brief mdiary_get_line_from_string WARNING: This does modify str and the content of str! + * @param str The pointer to the string where to search the linebreak + * @return The current line (null terminated). Do not free, this is a direct pointer into the original string. + */ +static gchar *mdiary_get_line_from_string(gchar **str_ptr) +{ + gchar *start; + gchar *str; + + if (!str_ptr || !*str_ptr | !**str_ptr) + return NULL; + + str = *str_ptr; + start = str; + + while (*str != '\n' && *str != '\0') { + str++; + } + + *str = '\0'; + *str_ptr = ++str; + + return start; +} + +/** + * @brief mdiary_add_file_to_store + * @param filename + * @param content NULL when the file shall be read by the function. Otherwise the file content. + * @param entryListStore + * @param autoCompletion + */ +static void mdiary_add_file_to_store(gchar *filename, gchar *content, + GtkListStore *entryListStore, GtkListStore *autoCompletion) { GFile *file; GError *err = NULL; GFileInputStream *stream; GDataInputStream *dstream; gchar *line; + gchar *content_ptr; GDateTime *datetime = NULL; gchar *title = NULL; @@ -234,16 +269,21 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto g_message("Add file: %s\n", filename); - file = g_file_new_for_path(filename); - stream = g_file_read(file, NULL, &err); - if (err != NULL) { - g_print("PARSER: %s\n", filename, err->message); - g_error_free(err); - return; + if (!content) { + file = g_file_new_for_path(filename); + stream = g_file_read(file, NULL, &err); + if (err != NULL) { + g_print("PARSER: %s\n", filename, err->message); + g_error_free(err); + return; + } + dstream = g_data_input_stream_new(G_INPUT_STREAM(stream)); } - dstream = g_data_input_stream_new(G_INPUT_STREAM(stream)); - while (line = g_data_input_stream_read_line(G_DATA_INPUT_STREAM(dstream), NULL, NULL, NULL)) { + content_ptr = content; + + while (line = (content ? mdiary_get_line_from_string(&content_ptr) : + g_data_input_stream_read_line(G_DATA_INPUT_STREAM(dstream), NULL, NULL, NULL))) { if (header_state == 0) { if (!datetime) { datetime = mdiary_get_date_from_string_ext(line, "Date: .*", ""); @@ -323,6 +363,31 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto g_object_unref(file); } +static void mdiary_add_gpg_to_store(gchar *filename, GtkListStore *entryListStore, GtkListStore *autoCompletion) +{ + gchar *cmd = NULL; + gchar *cmd_stdout = NULL; + gchar *cmd_stderr = NULL; + gint exit_status = 0; + + cmd = g_strdup_printf("gpg --decrypt %s", filename); + + if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) && exit_status == 0) { + g_print("GPG: Decryption OK!\n"); + mdiary_add_file_to_store(filename, cmd_stdout, entryListStore, autoCompletion); + } else { + g_print("GPG: Decryption of message failed!\n" + "---------- SNIP ----------\n" + "%s" + "---------- SNIP ----------\n", + cmd_stderr ? cmd_stderr : "Unknown error."); + } + + g_free(cmd); + g_free(cmd_stdout); + g_free(cmd_stderr); +} + static void mdiary_recurse_and_collect(gchar *base_dir, GtkListStore *entryListStore, GtkListStore *autoCompletion, @@ -343,7 +408,13 @@ static void mdiary_recurse_and_collect(gchar *base_dir, regex = g_regex_new("\\.md$", G_REGEX_CASELESS, 0, NULL); if (g_regex_match(regex, fullPath, 0, &match_info) && g_match_info_matches(match_info)) - mdiary_add_file_to_store(fullPath, entryListStore, autoCompletion); + mdiary_add_file_to_store(fullPath, NULL, entryListStore, autoCompletion); + g_regex_unref(regex); + + regex = g_regex_new("\\.md.gpg$", G_REGEX_CASELESS, 0, NULL); + if (g_regex_match(regex, fullPath, 0, &match_info) && + g_match_info_matches(match_info)) + mdiary_add_gpg_to_store(fullPath, entryListStore, autoCompletion); g_regex_unref(regex); } else if (g_file_test(fullPath, G_FILE_TEST_IS_DIR) && max_level) { mdiary_recurse_and_collect(fullPath, entryListStore, autoCompletion, max_level--);