# Experimental. If it jams, hit it with a hammer. If it breaks, it needed
# replacing anyways.

use strict;
use vars qw($VERSION %IRSSI);

use Irssi 20020120; # 21/01/2002, 18:00 cvs commit
$VERSION = "2.05";
%IRSSI = (
    authors     => 'Larry "Vizzie" Daffner',
    contact     => 'vizzie@flamingpackets.net',
    name        => 'Buffer scrollback logger',
    description => 'Saves the buffer to a log',
    license     => "Public Domain",
    url         => "http://www.flamingpackets.net/~vizzie/irssi/",
    changed     => 'Wed Mar 13 00:18:07 CST 2002',
    changes     => 'First edition'
);

use Irssi::TextUI;

# Pretty simple. Get the current window object. Read the scrollback
# buffer, and save it to the file given.

sub cmd_logscroll {
  my($file) = @_;
  my($window, $view, $lines, $line, $buf);

  if (!$file) {
    Irssi::print("usage: /logscroll <file>");
    return 1;
  }

  $window = Irssi::active_win();
  
  return unless defined $window;

  $view = $window->view();
  my $line = $view->get_lines();
  open (BUF, sprintf('>>%s/%s', Irssi::get_irssi_dir(), $file));

  my $lines  = 0;
  my $buf = "---- log begins here ----\n";

  if (defined $line){
    {
      $buf .= $line->get_text(0) . "\n";
      $line = $line->next();
      $lines++;
      redo if defined $line;
    }
  }
  $buf .= "---- log ends here ----\n";
  printf BUF ("%s", $buf);
  close BUF;

  Irssi::print("Logged scrollbuffer to $file");
}

Irssi::command_bind('logscroll', 'cmd_logscroll');
