changeset 241:8584f919d9b9

[/trunk] Changeset 254 by mikael * Use ISO8601 for time format in log file * Update TODO
author mikael
date Mon, 13 Jun 2005 19:29:09 +0000
parents 723433a677f0
children cb2f18e20e6a
files mcabber/src/TODO mcabber/src/histolog.c mcabber/src/jabglue.c mcabber/src/utils.c mcabber/src/utils.h
diffstat 5 files changed, 152 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/TODO	Sat Jun 11 19:27:40 2005 +0000
+++ b/mcabber/src/TODO	Mon Jun 13 19:29:09 2005 +0000
@@ -9,7 +9,6 @@
 TODO:
 
 * Presence notification is always accepted.  We should ask...
-* Multi-lines messages
 * Resource priority
 * UTF-8 support
 * Display status / chat mode
--- a/mcabber/src/histolog.c	Sat Jun 11 19:27:40 2005 +0000
+++ b/mcabber/src/histolog.c	Mon Jun 13 19:29:09 2005 +0000
@@ -31,6 +31,7 @@
 #include "hbuf.h"
 #include "jabglue.h"
 #include "screen.h"
+#include "utils.h"
 
 static guint UseFileLogging;
 static guint FileLoadLogs;
@@ -67,6 +68,7 @@
   time_t ts;
   const char *p;
   char *filename;
+  char str_ts[20];
 
   if (!UseFileLogging) return;
 
@@ -85,8 +87,9 @@
   for (p=data ; *p ; p++)
     if (*p == '\n') len++;
 
-  /* Line format: "TI DDDDDDDDDD LLL [data]"
-   * T=Type, I=Info, DDDDDDDDDD=date, LLL=0-padded-len
+  /* Line format: "TI yyyymmddThh:mm:ssZ [data]"
+   * (Old format: "TI DDDDDDDDDD LLL [data])"
+   * T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len
    *
    * Types:
    * - M message    Info: S (send) R (receive)
@@ -98,7 +101,8 @@
   g_free(filename);
   if (!fp) return;
 
-  fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data);
+  to_iso8601(str_ts, ts);
+  fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data);
   fclose(fp);
 }
 
@@ -115,6 +119,7 @@
   FILE *fp;
   struct stat bufstat;
   guint err = 0;
+  guint oldformat;
 
   if (!FileLoadLogs) return;
 
@@ -139,14 +144,23 @@
 
   /* See write_histo_line() for line format... */
   while (!feof(fp)) {
+    int format_off =0;
     if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break;
 
     for (tail = data; *tail; tail++) ;
 
     type = data[0];
     info = data[1];
+
+    // XXX Log format detection.  We can read both old and new log formats
+    // To be removed in a future version
+    oldformat = (data[11] != 'T' || data[20] != 'Z');
+    if (!oldformat)
+      format_off = 8;
+
     if ((type != 'M' && type != 'S') || 
-        (data[13] != ' ') || (data[17] != ' ')) {
+        (oldformat && ((data[13] != ' ') || (data[17] != ' '))) ||
+        ((!oldformat) && ((data[21] != ' ') || (data[25] != ' ')))) {
       if (!err) {
         scr_LogPrint("Error in history file format (%s)", jid);
         err = 1;
@@ -154,9 +168,12 @@
       //break;
       continue;
     }
-    data[13] = data[17] = 0;
-    timestamp = (unsigned long) atol(&data[3]);
-    len = (unsigned long) atol(&data[14]);
+    data[13+format_off] = data[17+format_off] = 0;
+    if (oldformat)
+      timestamp = (unsigned long) atol(&data[3]);
+    else
+      timestamp = from_iso8601(&data[3], 1);
+    len = (unsigned long) atol(&data[14+format_off]);
     
     // Some checks
     if (((type == 'M') && (info != 'S' && info != 'R')) ||
@@ -176,7 +193,7 @@
       while (*tail) tail++;
     }
     // Remove last CR
-    if ((tail > data+18) && (*(tail-1) == '\n'))
+    if ((tail > data+18+format_off) && (*(tail-1) == '\n'))
       *(tail-1) = 0;
 
     if (type == 'M') {
@@ -184,7 +201,7 @@
         prefix_flags = HBB_PREFIX_OUT;
       else
         prefix_flags = HBB_PREFIX_IN;
-      hbuf_add_line(p_buddyhbuf, &data[18], timestamp, prefix_flags, width);
+      hbuf_add_line(p_buddyhbuf, &data[18+format_off], timestamp, prefix_flags, width);
       err = 0;
     }
   }
--- a/mcabber/src/jabglue.c	Sat Jun 11 19:27:40 2005 +0000
+++ b/mcabber/src/jabglue.c	Mon Jun 13 19:29:09 2005 +0000
@@ -627,12 +627,8 @@
 
           // Timestamp?
           if ((x = xmlnode_get_tag(packet->x, "x")) != NULL) {
-            if ((p = xmlnode_get_attrib(x, "stamp")) != NULL) {
-              struct tm tstamp;
-              memset(&tstamp, 0, sizeof(tstamp));
-              if (strptime(p, "%Y%m%dT%H:%M:%S", &tstamp))
-                timestamp = mktime(&tstamp) - timezone;
-            }
+            if ((p = xmlnode_get_attrib(x, "stamp")) != NULL)
+              timestamp = from_iso8601(p, 1);
           }
 
           if (from && body)
--- a/mcabber/src/utils.c	Sat Jun 11 19:27:40 2005 +0000
+++ b/mcabber/src/utils.c	Mon Jun 13 19:29:09 2005 +0000
@@ -1,9 +1,34 @@
+/*
+ * utils.c      -- Various utility functions
+ * 
+ * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
+ * ut_* functions are derived from Cabber debug/log code.
+ * from_iso8601() comes from the Gaim project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
 #include <time.h>
 
+#include <config.h>
+
 static int DebugEnabled;
 static char *FName;
 
@@ -65,3 +90,99 @@
   }
 }
 
+//  to_iso8601(dststr, timestamp)
+// Convert timestamp to iso8601 format, and store it in dststr.
+// NOTE: dststr should be at last 19 chars long.
+// Return should be 0
+int to_iso8601(char *dststr, time_t timestamp)
+{
+  struct tm *tm_time;
+  int ret;
+
+  tm_time = gmtime(&timestamp);
+
+  ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ",
+        1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday,
+        tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec);
+
+  return ((ret == -1) ? -1 : 0);
+}
+
+//  from_iso8601(timestamp, utc)
+// This function comes from the Gaim project, gaim_str_to_time().
+// (Actually date may not be pure iso-8601)
+// Thanks, guys!
+time_t from_iso8601(const char *timestamp, int utc)
+{
+  struct tm t;
+  time_t retval = 0;
+  char buf[32];
+  char *c;
+  int tzoff = 0;
+
+  time(&retval);
+  localtime_r(&retval, &t);
+
+  snprintf(buf, sizeof(buf), "%s", timestamp);
+  c = buf;
+
+  /* 4 digit year */
+  if (!sscanf(c, "%04d", &t.tm_year)) return 0;
+  c+=4;
+  if (*c == '-')
+    c++;
+
+  t.tm_year -= 1900;
+
+  /* 2 digit month */
+  if (!sscanf(c, "%02d", &t.tm_mon)) return 0;
+  c+=2;
+  if (*c == '-')
+    c++;
+
+  t.tm_mon -= 1;
+
+  /* 2 digit day */
+  if (!sscanf(c, "%02d", &t.tm_mday)) return 0;
+  c+=2;
+  if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */
+    c++; /* skip the "T" */
+
+    /* 2 digit hour */
+    if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 ||
+        sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) {
+      int tzhrs, tzmins;
+      c+=8;
+      if (*c == '.') /* dealing with precision we don't care about */
+        c += 4;
+
+      if ((*c == '+' || *c == '-') &&
+          sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) {
+        tzoff = tzhrs*60*60 + tzmins*60;
+        if (*c == '+')
+          tzoff *= -1;
+      }
+
+      if (tzoff || utc) {
+
+//#ifdef HAVE_TM_GMTOFF
+        tzoff += t.tm_gmtoff;
+//#else
+//#   ifdef HAVE_TIMEZONE
+//        tzset();    /* making sure */
+//        tzoff -= timezone;
+//#   endif
+//#endif
+      }
+    }
+  }
+
+  t.tm_isdst = -1;
+
+  retval = mktime(&t);
+
+  retval += tzoff;
+
+  return retval;
+}
+
--- a/mcabber/src/utils.h	Sat Jun 11 19:27:40 2005 +0000
+++ b/mcabber/src/utils.h	Mon Jun 13 19:29:09 2005 +0000
@@ -4,4 +4,7 @@
 void ut_InitDebug(unsigned int level, char *file);
 void ut_WriteLog(const char *fmt, ...);
 
+int    to_iso8601(char *dststr, time_t timestamp);
+time_t from_iso8601(const char *timestamp, int utc);
+
 #endif