Make Message-ID string generation less confusing.
authorAndrej Kacian <ticho@claws-mail.org>
Sat, 1 Oct 2016 14:22:44 +0000 (16:22 +0200)
committerAndrej Kacian <ticho@claws-mail.org>
Sat, 1 Oct 2016 14:22:44 +0000 (16:22 +0200)
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
src/common/utils.h
src/compose.c
src/messageview.c
src/plugins/vcalendar/vcal_manager.c
src/plugins/vcalendar/vcal_meeting_gtk.c
src/plugins/vcalendar/vcalendar.c
src/prefs_account.c
src/prefs_account.h

index ad67d3c..d7d1b4c 100644 (file)
@@ -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()
 
index 90f255a..c904874 100644 (file)
@@ -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,
index b05006b..c9cd93c 100644 (file)
@@ -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;
        }
index e0c0d30..d237316 100644 (file)
@@ -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");
index 8519c1b..f800eeb 100644 (file)
@@ -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;                  
                                                                                
 
index da07bea..941482b 100644 (file)
@@ -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);
index a47f862..576da83 100644 (file)
@@ -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);
index 250a173..5bd2afd 100644 (file)
@@ -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);
index 4e4217d..f7feb13 100644 (file)
@@ -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);