changeset 1485:0121b6f3047c

New command: /buffer save
author Mikael Berthe <mikael@lilotux.net>
date Sun, 20 Apr 2008 16:06:37 +0200
parents 7b36b91a4388
children f83a51eaa5ed
files mcabber/src/commands.c mcabber/src/hbuf.c mcabber/src/hbuf.h mcabber/src/screen.c mcabber/src/screen.h
diffstat 5 files changed, 85 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/commands.c	Sun Apr 20 14:30:25 2008 +0200
+++ b/mcabber/src/commands.c	Sun Apr 20 16:06:37 2008 +0200
@@ -213,6 +213,7 @@
   compl_add_category_word(COMPL_BUFFER, "scroll_unlock");
   compl_add_category_word(COMPL_BUFFER, "scroll_toggle");
   compl_add_category_word(COMPL_BUFFER, "list");
+  compl_add_category_word(COMPL_BUFFER, "save");
 
   // Group category
   compl_add_category_word(COMPL_GROUP, "fold");
@@ -1630,6 +1631,8 @@
     buffer_date(arg);
   } else if (*subcmd == '%') {
     buffer_percent(subcmd+1, arg);
+  } else if (!strcasecmp(subcmd, "save")) {
+    scr_BufferDump(arg);
   } else if (!strcasecmp(subcmd, "list")) {
     scr_BufferList();
   } else {
--- a/mcabber/src/hbuf.c	Sun Apr 20 14:30:25 2008 +0200
+++ b/mcabber/src/hbuf.c	Sun Apr 20 16:06:37 2008 +0200
@@ -20,10 +20,14 @@
  */
 
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include "hbuf.h"
 #include "utils.h"
 #include "utf8.h"
+#include "screen.h"
 
 
 /* This is a private structure type */
@@ -329,6 +333,7 @@
   for (i = 0 ; i < n ; i++) {
     if (hbuf) {
       int maxlen;
+
       blk = (hbuf_block*)(hbuf->data);
       maxlen = blk->ptr_end - blk->ptr;
       *array_elt = (hbb_line*)g_new(hbb_line, 1);
@@ -344,9 +349,9 @@
         (*array_elt)->flags |= last_persist_prefixflags &
                                (HBB_PREFIX_HLIGHT_OUT | HBB_PREFIX_HLIGHT |
                                 HBB_PREFIX_INFO | HBB_PREFIX_IN);
-        //Continuation of a message - omit the prefix
+        // Continuation of a message - omit the prefix
         (*array_elt)->flags |= HBB_PREFIX_CONT;
-        (*array_elt)->mucnicklen = 0;//The nick is in the first one
+        (*array_elt)->mucnicklen = 0; // The nick is in the first one
       }
 
       hbuf = g_list_next(hbuf);
@@ -412,6 +417,59 @@
   return g_list_nth(hbuf, pc*hlen/100);
 }
 
+//  hbuf_dump_to_file(hbuf, filename)
+// Save the buffer to a file.
+void hbuf_dump_to_file(GList *hbuf, const char *filename)
+{
+  hbuf_block *blk;
+  hbb_line line;
+  guint last_persist_prefixflags;
+  char pref[96];
+  FILE *fp;
+  struct stat statbuf;
+
+  if (!stat(filename, &statbuf)) {
+    scr_LogPrint(LPRINT_NORMAL, "The file already exists.");
+    return;
+  }
+  fp = fopen(filename, "w");
+  if (!fp) {
+    scr_LogPrint(LPRINT_NORMAL, "Unable to open the file.");
+    return;
+  }
+
+  for (hbuf = g_list_first(hbuf); hbuf; hbuf = g_list_next(hbuf)) {
+    int maxlen;
+
+    blk = (hbuf_block*)(hbuf->data);
+    maxlen = blk->ptr_end - blk->ptr;
+
+    memset(&line, 0, sizeof(line));
+    line.timestamp  = blk->prefix.timestamp;
+    line.flags      = blk->prefix.flags;
+    line.mucnicklen = blk->prefix.mucnicklen;
+    line.text       = g_strndup(blk->ptr, maxlen);
+
+    if ((blk->flags & HBB_FLAG_PERSISTENT) && blk->prefix.flags) {
+      last_persist_prefixflags = blk->prefix.flags;
+    } else {
+      // Propagate highlighting flags
+      line.flags |= last_persist_prefixflags &
+                    (HBB_PREFIX_HLIGHT_OUT | HBB_PREFIX_HLIGHT |
+                     HBB_PREFIX_INFO | HBB_PREFIX_IN);
+      // Continuation of a message - omit the prefix
+      line.flags |= HBB_PREFIX_CONT;
+      line.mucnicklen = 0; // The nick is in the first one
+    }
+
+    scr_line_prefix(&line, pref, sizeof pref);
+    fprintf(fp, "%s%s\n", pref, line.text);
+  }
+
+  fclose(fp);
+  return;
+}
+
 //  hbuf_get_blocks_number()
 // Returns the number of allocated hbuf_block's.
 guint hbuf_get_blocks_number(GList *hbuf)
--- a/mcabber/src/hbuf.h	Sun Apr 20 14:30:25 2008 +0200
+++ b/mcabber/src/hbuf.h	Sun Apr 20 16:06:37 2008 +0200
@@ -48,6 +48,8 @@
 GList *hbuf_jump_date(GList *hbuf, time_t t);
 GList *hbuf_jump_percent(GList *hbuf, int pc);
 
+void hbuf_dump_to_file(GList *hbuf, const char *filename);
+
 guint hbuf_get_blocks_number(GList *p_hbuf);
 
 #endif /* __HBUF_H__ */
--- a/mcabber/src/screen.c	Sun Apr 20 14:30:25 2008 +0200
+++ b/mcabber/src/screen.c	Sun Apr 20 16:06:37 2008 +0200
@@ -2703,6 +2703,25 @@
   update_panels();
 }
 
+void scr_BufferDump(const char *file)
+{
+  char *extfname;
+
+  if (!currentWindow) {
+    scr_LogPrint(LPRINT_NORMAL, "No current buffer!");
+    return;
+  }
+
+  if (!file || !*file) {
+    scr_LogPrint(LPRINT_NORMAL, "Missing parameter (file name)!");
+    return;
+  }
+
+  extfname = expand_filename(file);
+  hbuf_dump_to_file(currentWindow->bd->hbuf, extfname);
+  g_free(extfname);
+}
+
 //  buffer_list()
 // key: winId/jid
 // value: winbuf structure
--- a/mcabber/src/screen.h	Sun Apr 20 14:30:25 2008 +0200
+++ b/mcabber/src/screen.h	Sun Apr 20 16:06:37 2008 +0200
@@ -162,6 +162,7 @@
 void scr_BufferSearch(int direction, const char *text);
 void scr_BufferPercent(int pc);
 void scr_BufferDate(time_t t);
+void scr_BufferDump(const char *file);
 void scr_RosterUnreadMessage(int);
 void scr_RosterJumpAlternate(void);
 void scr_BufferScrollUpDown(int updown, unsigned int nblines);