changeset 178:cfefae4b6de9

[/trunk] Changeset 190 by mikael * Read history files when creating a buddy panel.
author mikael
date Thu, 05 May 2005 11:54:57 +0000
parents a51ce78a0e2a
children d416d5cb8ddb
files mcabber/src/TODO mcabber/src/histolog.c mcabber/src/histolog.h mcabber/src/hooks.c mcabber/src/screen.c
diffstat 5 files changed, 86 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/TODO	Thu May 05 08:53:49 2005 +0000
+++ b/mcabber/src/TODO	Thu May 05 11:54:57 2005 +0000
@@ -18,6 +18,7 @@
   top variable on a resize).
 * Show number of online contacts in folded groups
 * Add a cfg_read_int() function
+* Better prefix usage in the hbuf buffers (use flags for IN/OUT etc.)
 
 * Commands! :-)
   - /roster <hide_offline|show_offline|top|bottom>
--- a/mcabber/src/histolog.c	Thu May 05 08:53:49 2005 +0000
+++ b/mcabber/src/histolog.c	Thu May 05 11:54:57 2005 +0000
@@ -28,6 +28,7 @@
 #include <fcntl.h>
 
 #include "histolog.h"
+#include "hbuf.h"
 #include "jabglue.h"
 #include "screen.h"
 
@@ -42,8 +43,7 @@
 static char *user_histo_file(const char *jid)
 {
   char *filename;
-  if (!UseFileLogging)
-    return NULL;
+  if (!UseFileLogging && !FileLoadLogs) return NULL;
 
   filename = g_new(char, strlen(RootDir) + strlen(jid) + 1);
   strcpy(filename, RootDir);
@@ -51,7 +51,7 @@
   return filename;
 }
 
-//  write()
+//  write_histo_line()
 // Adds a history (multi-)line to the jid's history logfile
 static void write_histo_line(const char *jid,
         time_t timestamp, guchar type, guchar info, const char *data)
@@ -60,10 +60,11 @@
   FILE *fp;
   time_t ts;
   const char *p;
-  char *filename = user_histo_file(jid);
+  char *filename;
 
-  if (!filename)
-    return;
+  if (!UseFileLogging) return;
+
+  filename = user_histo_file(jid);
 
   // If timestamp is null, get current date
   if (timestamp)
@@ -89,12 +90,83 @@
 
   fp = fopen(filename, "a");
   g_free(filename);
-  if (!fp)
-    return;
+  if (!fp) return;
+
   fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data);
   fclose(fp);
 }
 
+//  hlog_read_history()
+// Reads the jid's history logfile
+void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width)
+{
+  char *filename;
+  time_t timestamp;
+  guchar type, info;
+  char *data, *tail;
+  guint len;
+  FILE *fp;
+  char prefix[32];
+
+  if (!FileLoadLogs) return;
+
+  data = g_new(char, HBB_BLOCKSIZE+32);
+  if (!data) {
+    scr_LogPrint("Not enough memory to read history file");
+    return;
+  }
+
+  filename = user_histo_file(jid);
+
+  fp = fopen(filename, "r");
+  g_free(filename);
+  if (!fp) { g_free(data); return; }
+
+  /* See write_histo_line() for line format... */
+  while (!feof(fp)) {
+    if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break;
+
+    for (tail = data; *tail; tail++) ;
+
+    type = data[0];
+    info = data[1];
+    if ((type != 'M' && type != 'S') || 
+        (data[13] != ' ') || (data[17] != ' ')) {
+      scr_LogPrint("Error in history file format");
+      break;
+    }
+    data[13] = data[17] = 0;
+    timestamp = (unsigned long) atol(&data[3]);
+    len = (unsigned long) atol(&data[14]);
+    
+    // Some checks
+    if (((type == 'M') && (info != 'S' && info != 'R')) ||
+        ((type == 'I') && (!strchr("oaifdcn", info)))) {
+      scr_LogPrint("Error in history file format");
+      break;
+    }
+
+    while (len--) {
+      if (fgets(tail, HBB_BLOCKSIZE+24 - (tail-data), fp) == NULL) break;
+
+      while (*tail) tail++;
+    }
+    if ((tail > data+18) && (*(tail-1) == '\n'))
+      *(tail-1) = 0;
+
+    if (type == 'M') {
+      strftime(prefix, 12, "[%H:%M] ", localtime(&timestamp));
+      if (info == 'S')
+        strcat(prefix, "--> ");
+      else
+        strcat(prefix, "<== ");
+      hbuf_add_line(p_buddyhbuf, &data[18], prefix, width);
+    }
+  }
+  fclose(fp);
+  g_free(data);
+}
+
 //  hlog_enable()
 // Enable logging to files.  If root_dir is NULL, then $HOME/.mcabber is used.
 // If loadfiles is TRUE, we will try to load buddies history logs from file.
--- a/mcabber/src/histolog.h	Thu May 05 08:53:49 2005 +0000
+++ b/mcabber/src/histolog.h	Thu May 05 11:54:57 2005 +0000
@@ -6,6 +6,7 @@
 #include "jabglue.h"
 
 void hlog_enable(guint enable, char *root_dir, guint loadfile);
+void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width);
 inline void hlog_write_message(const char *jid, time_t timestamp, int sent,
         const char *msg);
 inline void hlog_write_status(const char *jid, time_t timestamp,
--- a/mcabber/src/hooks.c	Thu May 05 08:53:49 2005 +0000
+++ b/mcabber/src/hooks.c	Thu May 05 11:54:57 2005 +0000
@@ -41,6 +41,9 @@
     new_guy = TRUE;
   }
 
+  // Note: the hlog_write should not be called first, because in some
+  // cases scr_WriteIncomingMessage() will load the history and we'd
+  // have the message twice...
   scr_WriteIncomingMessage(jid, buffer);
   hlog_write_message(jid, timestamp, FALSE, buffer);
   hk_ext_cmd(jid, 'M', 'R', NULL);
--- a/mcabber/src/screen.c	Thu May 05 08:53:49 2005 +0000
+++ b/mcabber/src/screen.c	Thu May 05 11:54:57 2005 +0000
@@ -219,6 +219,7 @@
   }
   update_panels();
 
+  hlog_read_history(title, &tmp->hbuf, maxX - scr_WindowWidth(rosterWnd) - 14);
   list_add_tail(&tmp->list, &window_list);
 
   return tmp;