Fix timezone handling in procheader_date_parse() on Windows.
[claws.git] / src / procheader.c
index 2aa4e6f66e8f64f292b225cd726fb409be03f2d5..327774009370f6d0200eb76cde399b4049eb7fc5 100644 (file)
@@ -135,15 +135,11 @@ static gint generic_get_one_field(gchar **bufptr, void *data,
        size_t len;
        gchar *buf;
 
-/*     cm_return_val_if_fail(bufptr != NULL, -1); TODO */
+       cm_return_val_if_fail(bufptr != NULL, -1);
 
        len = BUFFSIZE;
        buf = g_malloc(len);
-       if (buf == NULL) {
-               debug_print("generic_get_one_field: primary allocation error\n");
-               *bufptr = NULL;
-               return -1;
-       }
+
        if (hentry != NULL) {
                /* skip non-required headers */
                /* and get hentry header line */
@@ -213,12 +209,7 @@ static gint generic_get_one_field(gchar **bufptr, void *data,
                        
                        /* read next line */
                        tmpbuf = g_malloc(BUFFSIZE);
-                       if (tmpbuf == NULL) {
-                               debug_print("generic_get_one_field: secondary allocation error\n");
-                               g_free(buf);
-                               *bufptr = NULL;
-                               return -1;
-                       }
+
                        if (getline(tmpbuf, BUFFSIZE, data) == NULL) {
                                g_free(tmpbuf);
                                break;
@@ -269,7 +260,7 @@ GPtrArray *procheader_get_header_array_asis(FILE *fp)
 
        headers = g_ptr_array_new();
 
-       while (procheader_get_one_field_asis(&buf, fp) != -1 && buf != NULL) {
+       while (procheader_get_one_field_asis(&buf, fp) != -1) {
                if ((header = procheader_parse_header(buf)) != NULL)
                        g_ptr_array_add(headers, header);
                g_free(buf);
@@ -390,7 +381,7 @@ void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
 
        if (hentry == NULL) return;
 
-       while ((hnum = procheader_get_one_field(&buf, fp, hentry)) != -1 && buf != NULL) {
+       while ((hnum = procheader_get_one_field(&buf, fp, hentry)) != -1) {
                hp = hentry + hnum;
 
                p = buf + strlen(hp->name);
@@ -579,7 +570,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
        hentry = procheader_get_headernames(full);
 
        if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
-               while (get_one_field(&buf, data, NULL) != -1 && buf != NULL) {
+               while (get_one_field(&buf, data, NULL) != -1) {
                        if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
                                strlen("X-Claws-End-Special-Headers:"))) ||
                            (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
@@ -619,7 +610,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                avatar_hook_id = 0;
        }
 
-       while ((hnum = get_one_field(&buf, data, hentry)) != -1 && buf != NULL) {
+       while ((hnum = get_one_field(&buf, data, hentry)) != -1) {
                hp = buf + strlen(hentry[hnum].name);
                while (*hp == ' ' || *hp == '\t') hp++;
 
@@ -956,8 +947,8 @@ static gint procheader_scan_date_string(const gchar *str,
 
        *zone = '\0';
 
-       /* RFC3339 subset */
-       /* This particular "subset" is invalid, RFC requires the time offset */
+       /* RFC3339 subset, no fraction of second, and no timezone offset */
+       /* This particular "subset" is invalid, RFC requires the offset */
        result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
                        year, &month_n, day, hh, mm, ss);
        if (result == 6) {
@@ -967,6 +958,17 @@ static gint procheader_scan_date_string(const gchar *str,
                }
        }
 
+       /* ISO8601 format with just date (YYYY-MM-DD) */
+       result = sscanf(str, "%4d-%2d-%2d",
+                       year, &month_n, day);
+       if (result == 3) {
+               *hh = *mm = *ss = 0;
+               if (1 <= month_n && month_n <= 12) {
+                       strncpy2(month, monthstr+((month_n-1)*3), 4);
+                       return 0;
+               }
+       }
+
        return -1;
 }
 
@@ -1055,15 +1057,16 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
 
 #ifdef G_OS_WIN32
        GTimeZone *tz;
-       GDateTime *dt;
-
-       tz = g_time_zone_new(zone);
-       dt = g_date_time_new(tz, year, dmonth, day, hh, mm, ss);
+       GDateTime *dt, *dt2;
 
-       timer = g_date_time_to_unix(dt);
-
-       g_date_time_unref(dt);
+       tz = g_time_zone_new(zone); // can't return NULL no need to check for it
+       dt = g_date_time_new(tz, 1, 1, 1, 0, 0, 0);
        g_time_zone_unref(tz);
+       dt2 = g_date_time_add_full(dt, year-1, dmonth-1, day-1, hh, mm, ss);
+       g_date_time_unref(dt);
+
+       timer = g_date_time_to_unix(dt2);
+       g_date_time_unref(dt2);
 
 #else
        struct tm t;
@@ -1155,13 +1158,7 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *he
                return -1;
        }
        val = procheader_get_one_field(buf, fp, hentry);
-       if (buf == NULL) {
-               claws_unlink(file);
-               g_free(file);
-               g_free(*buf);
-               *buf = NULL;
-               return -1;
-       }
+
        if (fclose(fp) == EOF) {
                FILE_OP_ERROR(file, "fclose");
                claws_unlink(file);