From 8d93e8c8cd8169ef0071301d1dd8888199bc6e87 Mon Sep 17 00:00:00 2001 From: Andrej Kacian Date: Sat, 1 Oct 2016 16:22:44 +0200 Subject: [PATCH] Make Message-ID string generation less confusing. Instead of having to prepare one part, then second part in advance and pass them to generate_msgid() which does its own voodoo with them, we now just pass a pointer to PrefsAccount to prefs_account_generate_msgid(), and it does all of it on its own, returning pointer to a newly allocated string. --- src/common/utils.c | 33 ----------------- src/common/utils.h | 1 - src/compose.c | 42 ++++------------------ src/messageview.c | 23 +++--------- src/plugins/vcalendar/vcal_manager.c | 16 ++------- src/plugins/vcalendar/vcal_meeting_gtk.c | 14 +------- src/plugins/vcalendar/vcalendar.c | 15 ++------ src/prefs_account.c | 45 ++++++++++++++++++++++++ src/prefs_account.h | 1 + 9 files changed, 64 insertions(+), 126 deletions(-) diff --git a/src/common/utils.c b/src/common/utils.c index ad67d3cac..d7d1b4cb9 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -3731,39 +3731,6 @@ gint g_int_compare(gconstpointer a, gconstpointer b) return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b); } -gchar *generate_msgid(gchar *buf, gint len, gchar *user_addr) -{ - struct tm *lt; - time_t t; - gchar *addr; - struct tm buft; - - t = time(NULL); - lt = localtime_r(&t, &buft); - - if (user_addr != NULL) - addr = g_strdup_printf(".%s", user_addr); - else if (strlen(buf) != 0) - addr = g_strdup_printf("@%s", buf); - else - addr = g_strdup_printf("@%s", get_domain_name()); - - /* Replace all @ but the last one in addr, with underscores. - * RFC 2822 States that msg-id syntax only allows one @. - */ - while (strchr(addr, '@') != NULL && strchr(addr, '@') != strrchr(addr, '@')) - *(strchr(addr, '@')) = '_'; - - g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x%s", - lt->tm_year + 1900, lt->tm_mon + 1, - lt->tm_mday, lt->tm_hour, - lt->tm_min, lt->tm_sec, - (guint) rand(), addr); - - g_free(addr); - return buf; -} - /* quote_cmd_argument() diff --git a/src/common/utils.h b/src/common/utils.h index 90f255a6e..c90487432 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -543,7 +543,6 @@ const gchar * line_has_quote_char (const gchar *str, gint g_int_compare (gconstpointer a, gconstpointer b); -gchar *generate_msgid (gchar *buf, gint len, gchar *user_addr); gchar *generate_mime_boundary (const gchar *prefix); gint quote_cmd_argument(gchar * result, guint size, diff --git a/src/compose.c b/src/compose.c index b05006b01..c9cd93c8b 100644 --- a/src/compose.c +++ b/src/compose.c @@ -5411,27 +5411,12 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp) } /* Resent-Message-ID */ - if (compose->account->set_domain && compose->account->domain) { - g_snprintf(buf, sizeof(buf), "%s", compose->account->domain); - } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) { - g_snprintf(buf, sizeof(buf), "%s", - strchr(compose->account->address, '@') ? - strchr(compose->account->address, '@')+1 : - compose->account->address); - } else { - g_snprintf(buf, sizeof(buf), "%s", ""); - } - if (compose->account->gen_msgid) { - gchar *addr = NULL; - if (compose->account->msgid_with_addr) { - addr = compose->account->address; - } - generate_msgid(buf, sizeof(buf), addr); - err |= (fprintf(fp, "Resent-Message-ID: <%s>\n", buf) < 0); + gchar *addr = prefs_account_generate_msgid(compose->account); + err |= (fprintf(fp, "Resent-Message-ID: <%s>\n", addr) < 0); if (compose->msgid) g_free(compose->msgid); - compose->msgid = g_strdup(buf); + compose->msgid = addr; } else { compose->msgid = NULL; } @@ -6561,27 +6546,12 @@ static gchar *compose_get_header(Compose *compose) g_free(str); /* Message-ID */ - if (compose->account->set_domain && compose->account->domain) { - g_snprintf(buf, sizeof(buf), "%s", compose->account->domain); - } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) { - g_snprintf(buf, sizeof(buf), "%s", - strchr(compose->account->address, '@') ? - strchr(compose->account->address, '@')+1 : - compose->account->address); - } else { - g_snprintf(buf, sizeof(buf), "%s", ""); - } - if (compose->account->gen_msgid) { - gchar *addr = NULL; - if (compose->account->msgid_with_addr) { - addr = compose->account->address; - } - generate_msgid(buf, sizeof(buf), addr); - g_string_append_printf(header, "Message-ID: <%s>\n", buf); + gchar *addr = prefs_account_generate_msgid(compose->account); + g_string_append_printf(header, "Message-ID: <%s>\n", addr); if (compose->msgid) g_free(compose->msgid); - compose->msgid = g_strdup(buf); + compose->msgid = addr; } else { compose->msgid = NULL; } diff --git a/src/messageview.c b/src/messageview.c index e0c0d3020..d2373161c 100644 --- a/src/messageview.c +++ b/src/messageview.c @@ -976,26 +976,13 @@ static gint disposition_notification_send(MsgInfo *msginfo) goto FILE_ERROR; /* Message ID */ - if (account->set_domain && account->domain) { - g_snprintf(buf, sizeof(buf), "%s", account->domain); - } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) { - g_snprintf(buf, sizeof(buf), "%s", - strchr(account->address, '@') ? - strchr(account->address, '@')+1 : - account->address); - } else { - g_snprintf(buf, sizeof(buf), "%s", ""); - } - if (account->gen_msgid) { - gchar *addr = NULL; - if (account->msgid_with_addr) { - addr = account->address; - } - generate_msgid(buf, sizeof(buf), addr); - - if (fprintf(fp, "Message-ID: <%s>\n", buf) < 0) + gchar *addr = prefs_account_generate_msgid(account); + if (fprintf(fp, "Message-ID: <%s>\n", addr) < 0) { + g_free(addr); goto FILE_ERROR; + } + g_free(addr); } boundary = generate_mime_boundary("DN"); diff --git a/src/plugins/vcalendar/vcal_manager.c b/src/plugins/vcalendar/vcal_manager.c index 8519c1baa..f800eeb79 100644 --- a/src/plugins/vcalendar/vcal_manager.c +++ b/src/plugins/vcalendar/vcal_manager.c @@ -1191,7 +1191,7 @@ static gchar *write_headers(PrefsAccount *account, enum icalparameter_partstat status; gchar *prefix = NULL; gchar enc_subject[512], enc_from[512], *from = NULL; - gchar msgid[128]; + gchar *msgid; gchar *calmsgid = NULL; cm_return_val_if_fail(account != NULL, NULL); @@ -1282,18 +1282,7 @@ static gchar *write_headers(PrefsAccount *account, calmsgid = g_strdup(""); } - if (account && account->set_domain && account->domain) { - g_snprintf(msgid, sizeof(msgid), "%s", account->domain); - } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) { - g_snprintf(msgid, sizeof(msgid), "%s", - strchr(account->address, '@') ? - strchr(account->address, '@')+1 : - account->address); - } else { - g_snprintf(msgid, sizeof(msgid), "%s", ""); - } - - generate_msgid(msgid, sizeof(msgid), account->address); + msgid = prefs_account_generate_msgid(account); result = g_strdup_printf("%s" "From: %s <%s>\n" @@ -1323,6 +1312,7 @@ static gchar *write_headers(PrefsAccount *account, g_free(save_folder); g_free(queue_headers); g_free(attendees); + g_free(msgid); return result; diff --git a/src/plugins/vcalendar/vcal_meeting_gtk.c b/src/plugins/vcalendar/vcal_meeting_gtk.c index da07beadd..941482ba3 100644 --- a/src/plugins/vcalendar/vcal_meeting_gtk.c +++ b/src/plugins/vcalendar/vcal_meeting_gtk.c @@ -1257,22 +1257,10 @@ static gboolean send_meeting_cb(GtkButton *widget, gpointer data) organizer_name = get_organizer_name(meet); - if (account->set_domain && account->domain) { - g_snprintf(buf, sizeof(buf), "%s", account->domain); - } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) { - g_snprintf(buf, sizeof(buf), "%s", - strchr(account->address, '@') ? - strchr(account->address, '@')+1 : - account->address); - } else { - g_snprintf(buf, sizeof(buf), "%s", ""); - } - generate_msgid(buf, 255, account->address); - if (meet->uid) { uid = g_strdup(meet->uid); } else { - uid = g_strdup(buf); + uid = prefs_account_generate_msgid(account); } dtstart = get_date(meet, TRUE); diff --git a/src/plugins/vcalendar/vcalendar.c b/src/plugins/vcalendar/vcalendar.c index a47f86222..576da832c 100644 --- a/src/plugins/vcalendar/vcalendar.c +++ b/src/plugins/vcalendar/vcalendar.c @@ -150,7 +150,7 @@ static void create_meeting_from_message_cb_ui(GtkAction *action, gpointer data) } if (fp) { - gchar uid[256]; + gchar *uid; time_t t = time(NULL); time_t t2 = t+3600; gchar *org = NULL; @@ -179,22 +179,13 @@ static void create_meeting_from_message_cb_ui(GtkAction *action, gpointer data) org = g_strdup(account->address); - if (account->set_domain && account->domain) { - g_snprintf(uid, sizeof(uid), "%s", account->domain); - } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) { - g_snprintf(uid, sizeof(uid), "%s", - strchr(account->address, '@') ? - strchr(account->address, '@')+1 : - account->address); - } else { - g_snprintf(uid, sizeof(uid), "%s", ""); - } - generate_msgid(uid, 255, account->address); + uid = prefs_account_generate_msgid(account); event = vcal_manager_new_event(uid, org, NULL, NULL/*location*/, summary, description, dtstart, dtend, recur, tzid, url, method, sequence, ICAL_VTODO_COMPONENT); + g_free(uid); /* hack to get default hours */ g_free(event->dtstart); diff --git a/src/prefs_account.c b/src/prefs_account.c index 250a17348..5bd2afd55 100644 --- a/src/prefs_account.c +++ b/src/prefs_account.c @@ -4907,6 +4907,51 @@ static void prefs_account_compose_default_dictionary_set_optmenu_from_string } #endif +gchar *prefs_account_generate_msgid(PrefsAccount *account) +{ + gchar *addr, *tmbuf, *buf = NULL; + GDateTime *now; + gchar *user_addr = account->msgid_with_addr ? account->address : NULL; + + if (account->set_domain && account->domain) { + buf = g_strdup(account->domain); + } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) { + buf = g_strdup( + strchr(account->address, '@') ? + strchr(account->address, '@')+1 : + account->address); + } + + if (user_addr != NULL) { + addr = g_strdup_printf(".%s", user_addr); + } else { + addr = g_strdup_printf("@%s", + buf != NULL && strlen(buf) > 0 ? + buf : get_domain_name()); + } + + if (buf != NULL) + g_free(buf); + if (user_addr != NULL) + g_free(user_addr); + + /* Replace all @ but the last one in addr, with underscores. + * RFC 2822 States that msg-id syntax only allows one @. + */ + while (strchr(addr, '@') != NULL && strchr(addr, '@') != strrchr(addr, '@')) + *(strchr(addr, '@')) = '_'; + + now = g_date_time_new_now_local(); + tmbuf = g_date_time_format(now, "%Y%m%d%H%M%S"); + buf = g_strdup_printf("%s.%08x%s", + tmbuf, (guint)rand(), addr); + g_free(tmbuf); + g_free(addr); + + debug_print("Generated Message-ID string '%s'\n", buf); + return buf; +} + void prefs_account_register_page(PrefsPage *page) { prefs_pages = g_slist_append(prefs_pages, page); diff --git a/src/prefs_account.h b/src/prefs_account.h index 4e4217d69..f7feb137f 100644 --- a/src/prefs_account.h +++ b/src/prefs_account.h @@ -223,6 +223,7 @@ PrefsAccount *prefs_account_open (PrefsAccount *ac_prefs, gboolean *dirty); const gchar *prefs_account_get_privacy_prefs(PrefsAccount *account, gchar *id); void prefs_account_set_privacy_prefs(PrefsAccount *account, gchar *id, gchar *new_value); +gchar *prefs_account_generate_msgid(PrefsAccount *account); void prefs_account_register_page (PrefsPage *page); void prefs_account_unregister_page (PrefsPage *page); -- 2.25.1