#! /usr/bin/perl
# $Id$

# Copyright (C) 2001-2004 MandrakeSoft
# Yves Duret <yduret at mandrakesoft.com>
# some code is Copyright: (C) 1999, Michael T. Babcock <mikebabcock@pobox.com>
#
# 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, 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.
#
# TODO: consider switching from TreeView to gtkhtml

use strict;
use lib qw(/usr/lib/libDrakX);
use standalone;     #- warning, standalone must be loaded very first, for 'explanations'
use common;
use interactive;
BEGIN {
    eval {
	require ugtk2;
	ugtk2->import(qw(:create :dialogs :helpers :wrappers));
    };
    if ($@) {
	print "This program cannot be run in console mode.\n";
	c::_exit(0);  #- skip ugtk2::END
    }
}
use vars qw(*F);

my $in = 'interactive'->vnew('su');

my ($isExplain, $Explain, $isFile, $File, $isWord, $Word);

#- parse arguments list.
foreach (@ARGV) {
    /^--explain=(.*)$/ and do { $isExplain = ($Explain) = $1; $isFile = 1; $File = "/var/log/explanations"; next };
    /^--file=(.*)$/ and do { $isFile = ($File) = $1; next };
    /^--word=(.*)$/ and do { $isWord = ($Word) = $1; next };
    /^--alert$/ and do { alert_config(); quit() };
}

my $isTail = 1 if $isFile;
$| = 1 if $isTail;
my $h = chomp_(`hostname -s`);

$ugtk2::wm_icon = "logdrake";
my $explain_title = N("Mandrake Tools Logs");
my $my_win = ugtk2->new($isExplain ? $explain_title : N("Logdrake"));

unless ($::isEmbedded) {
    $my_win->{window}->set_border_width(5);
    #$my_win->{window}->set_default_size(540,460);
}
$my_win->{window}->signal_connect(delete_event => \&quit);

my $cal = gtkset_sensitive(new Gtk2::Calendar(), 0);
my $mday = (localtime(time()))[3];
$cal->select_day($mday);
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my $cal_mode = 0;
my $cal_butt = gtksignal_connect(new Gtk2::CheckButton(N("Show only for the selected day")), clicked => sub { $cal_mode = !$cal_mode; gtkset_sensitive($cal,$cal_mode) });

### menus definition
# the menus are not shown
# but they provides shiny shortcut like C-q
my @menu_items = (
		  { path => N("/_File"), item_type => '<Branch>' },
		  { path => N("/File/_New"), accelerator => N("<control>N") },
		  { path => N("/File/_Open"), accelerator => N("<control>O") },
		  { path => N("/File/_Save"), accelerator => N("<control>S"), callback => \&save },
		  { path => N("/File/Save _As") },
		  { path => N("/File/-"), item_type => '<Separator>' },
		  { path => N("/File/_Quit"), accelerator => N("<control>Q"), callback => \&quit },
		  { path => N("/_Options"), item_type => '<Branch>' },
		  { path => N("/Options/Test") },
		  { path => N("/_Help"), item_type => '<LastBranch>' },
		  { path => N("/Help/_About...") } 
		 );
my $_menubar = create_factory_menu($my_win->{rwindow}, @menu_items) unless $::isEmbedded;
######### menus end


########## font and colors

    
# Define global terms:
# Define good notables:
my @word_good = ("starting\n", "Freeing", "Detected", "starting.", "accepted.\n", "authenticated.\n", "Ready", "active", "reloading", "saved;", "restarting", "ONLINE\n");
my @word_warn = ("dangling", "closed.\n", "Assuming", "root", "root\n", "exiting\n", "missing", "Ignored", "adminalert:", "deleting", "OFFLINE\n");
my @word_bad = "bad";
my @word_note = ("LOGIN", "DHCP_OFFER", "optimized", "reset:", "unloaded", "disconnected", "connect", "Successful", "registered\n");
#my @line_good = ("up", "DHCP_ACK", "Cleaned", "Initializing", "Starting", "success", "successfully", "alive", "found", "ONLINE\n");
#my @line_warn = ("warning:", "WARNING:", "invalid", "obsolete", "bad", "Password", "detected", "timeout", "timeout:", "attackalert:", "wrong", "Lame", "FAILED", "failing", "unknown", "obsolete", "stopped.\n", "terminating.", "disabled\n", "disabled", "Lost");
#my @line_bad = ("DENY", "lost", "shutting", "dead", "DHCP_NAK", "failure;", "Unable", "inactive", "terminating", "refused", "rejected", "down", "OFFLINE\n", "error\n", "ERROR\n", "ERROR:", "error", "ERROR", "error:", "failed:");

# Now define what we want to use when:
my $col_good = 'green4';
my $col_warn = 'yellow4';
my $col_bad = 'red';
my $col_note = 'purple';
my $col = 'darkcyan';

######### font and colors end

my %files = (
	     "auth" => { file => "/var/log/auth.log", desc => N("_:this is the auth.log log file\nAuthentication") },
	     "user" => { file => "/var/log/user.log", desc => N("_:this is the user.log log file\nUser") },
	     "messages" => { file => "/var/log/messages", desc => N("_:this is the /var/log/messages log file\nMessages") },
	     "syslog" => { file => "/var/log/syslog", desc => N("_:this is the /var/log/syslog log file\nSyslog") },
	     "explanations" => { file => "/var/log/explanations", desc => $explain_title }
);

my $yy = gtkset_sensitive(gtksignal_connect(new Gtk2::Button(N("search")) , clicked => \&search),0);

my $log_text = gtktext_insert(Gtk2::TextView->new, [ [ '' ] ]);

my $log_buf = $log_text->get_buffer;
my $refcount_search;
#### far from window

my %toggle;

gtkadd($my_win->{window},
       gtkpack_(new Gtk2::VBox(0,0),
		if_(!$isExplain && !$::isEmbedded, 0, N("A tool to monitor your logs")),
		if_(!$isFile, 0, gtkadd(new Gtk2::Frame(N("Settings")),
					  gtkpack__(new Gtk2::VBox(0,2),
						    gtkpack__(new Gtk2::VBox(0,2),
							      # N("Show lines"),
							      gtkpack__(new Gtk2::HBox(0,0),
									" " . N("Matching") . " ", my $e_yes = new Gtk2::Entry(),
									" " . N("but not matching") . " ", my $e_no = new Gtk2::Entry()
									)
							      ),
						    gtkpack_(new Gtk2::HBox(0,0),
							     1, gtkadd(gtkset_border_width(new Gtk2::Frame(N("Choose file")),2),
								       gtkpack(gtkset_border_width(Gtk2::VBox->new(0,0),0),
										map { $toggle{$_} = gtksignal_connect(new Gtk2::CheckButton($files{$_}{desc}), 
														      clicked => sub {
															  $refcount_search++; 
															  gtkset_sensitive($yy, $refcount_search);
														      }) } sort keys %files,
										)
								       ),
							     0, gtkadd(gtkset_border_width(new Gtk2::Frame(N("Calendar")),2),
								       gtkpack__(gtkset_border_width(new Gtk2::VBox(0,0),5),
										 $cal_butt, $cal
										 )
								       )
							     ),
						    $yy,
						    )
					  )
		    ),
		!$isExplain ? (1, gtkadd(new Gtk2::Frame(N("Content of the file")),
					   create_scrolled_window($log_text)
					   )) : (1, create_scrolled_window($log_text)),
		if_(!$isExplain, 0, gtkadd(gtkset_border_width(gtkset_layout(Gtk2::HButtonBox->new, 'end'), 5),
					      if_(!$isFile, gtksignal_connect(new Gtk2::Button(N("Mail alert")), 
										clicked => sub { 
                                                      eval { alert_config() };
                                                      my $err = $@;
                                                      $::WizardWindow->destroy if defined $::WizardWindow;
                                                      undef $::WizardWindow;
                                                      if ($err && $err !~ /wizcancel/) {
                                                          err_dialog(N("Error"), N("The alert wizard had unexpectly failled:")
                                                                     . "\n\n" . $err);
                                                      }
                                                  })),
					      gtksignal_connect(Gtk2::Button->new(N("Save")), clicked => \&save),
					      gtksignal_connect(Gtk2::Button->new($::isEmbedded ? N("Cancel") : N("Quit")), clicked => \&quit)
					      )
		    )
		)
       );

$isFile && !$::isEmbedded and gtkset_size_request($log_text, 400, 500);

$my_win->{window}->show_all;
search() if $isFile;
$my_win->main;

sub quit() { ugtk2->exit(0) }

#-------------------------------------------------------------
# search functions
#-------------------------------------------------------------
sub search() {
    return if !$log_text->window;
    $log_text->window->freeze_updates;
    $log_buf->set_text('');
    if ($isFile) {
	parse_file($File, $File);
    } else {
	foreach (keys %files) {
	    parse_file($files{$_}{file}, $files{$_}{desc}) if $toggle{$_}->get_active;
	}
    }
    $log_text->window->thaw_updates;
    $log_text->show;
    gtkflush();
}

my $timer;

my (@logs, %tags);

sub parse_file {
  my ($file, $descr) = @_;
  undef %tags;

  $file =~ s/\.gz$//;
  my ($pbar, $win_pb);
  unless ($::isEmbedded && $isExplain) {
      gtkadd($win_pb = gtkset_modal(new Gtk2::Window('toplevel'), 1),
             gtkpack(new Gtk2::VBox(2,0),
                     new Gtk2::Label(" " . N("please wait, parsing file: %s", $descr) . " "),
                     $pbar = new Gtk2::ProgressBar()
                     )
             );
      $win_pb->set_transient_for($my_win->{rwindow}) unless $::isEmbedded;
      $win_pb->set_position('center');
      $win_pb->realize;
      $win_pb->show_all;
      gtkflush();
  }
  my $ey = $e_yes->get_chars(0, -1);
  my $en = $e_no->get_chars(0, -1);
  $ey =~ s/ OR /|/;
  $ey =~ s/^\*$//;
  $en =~ s/^\*$/.*/;
  $ey = $ey . $Word if $isWord;

  if ($cal_mode) {
      my (undef, $month, $day) = $cal->get_date;
      $ey = $months[$month]."\\s{1,2}$day\\s.*$ey.*\n";
  }

  my @all = catMaybeCompressed($file);

  if ($isExplain) {
      my (@t, $t);
      while (@all) {
	  $t = pop @all;
	  next if $t =~ /logdrake/;
	  last if $t !~ /$Explain/;
	  push @t, $t;
      }
      @all = reverse @t;
  }

  my $taille = @all;
  my $i = 0;
  my $test;
  if ($en && !$ey) {
      $test = sub { !/$en/ };
  } elsif ($ey && !$en) {
      $test = sub { /$ey/ };
  } else {
      $test = sub { /$ey/ && !/$en/ };
  }

  foreach (@all) {
      $i++;
      if ($pbar && $i % 10) { 
	  $pbar->set_fraction($i/$taille);
       $win_pb->window->process_updates(1); # no gtkflush() because we do not want to refresh the TextView
      }
      
      logcolorize($_) if $test->();
  }
  $win_pb->destroy if !$::isEmbedded || !$isExplain;

  if ($isTail) {
      close F;
      open F, $file or die "E: $!";
      local $_;
      while (<F>) {}; #to prevent to output the file twice..
#      $log_text->set_point($log_text->get_length());
      $timer ||= Glib::Timeout->add(1000, sub {
                                       logcolorize($_) while <F>;
                                       seek F, 0, 1;
                                   });
  }     
  log_output__real();
}

sub text_append {
    my ($textview, $t, %opts) = @_;
    my $buffer = $textview->get_buffer;
    $buffer->{tags} ||= {};
    my $gtk_tags = $buffer->{tags};
    if (ref($t) eq 'ARRAY') {
        foreach my $token (@$t) {
            my $iter1 = $buffer->get_end_iter;
            if ($token->[0] =~ /^Gtk2::Gdk::Pixbuf/) {
                $buffer->insert_pixbuf($iter1, $token->[0]);
                next;
            }
            if ($token->[1]) {
                my $tag = $token->[1];
                $gtk_tags->{$tag} ||= $buffer->create_tag($tag, %{$tags{$token->[1]}});
                $buffer->insert_with_tags($iter1, $token->[0], $gtk_tags->{$tag});
            } else {
                $buffer->insert($iter1, $token->[0]);
            }
        }
    } else {
        $buffer->set_text($t);
    }
    #- the following line is needed to move the cursor to the beginning, so that if the
    #- textview has a scrollbar, it won't scroll to the bottom when focusing (#3633)
    $buffer->place_cursor($buffer->get_start_iter);
    $textview->set_wrap_mode($opts{wrap_mode} || 'word');
    $textview->set_editable($opts{editable} || 0);
    $textview->set_cursor_visible($opts{visible} || 0);
}

##########################################################################################

sub logcolorize {
    local $_ = shift; #my ($data) = @_;

    # we get date & time if it is date & time (dmesg)
    s/(\D{3} .. (\d\d:\d\d:\d\d ))//;
    my $timestamp = $isExplain ? $2 : $1;
    my @rec = split;

    log_output($timestamp,  'Bold', 'darkcyan'); # date & time if any...
    # BUG: $col hasn't yet be reseted
    $isExplain or log_output("$rec[0] ",  'Bold', $rec[0] eq $h ? 'blue' : $col);  # hostname
    
    if ($rec[1] eq "last") {
	log_output(" last message repeated ",  undef, 'green');
	log_output($rec[4],  'Bold', 'green');
	log_output(" times\n",  undef, 'green');
	return;
    }
    # Extract PID if present
    if ($rec[1] =~ /\[(\d+)\]:/) {
	my ($pid) = $1;
	$rec[1] =~ s/\[$1\]://;
	log_output($rec[1] . "[",  undef, 'green');
	log_output($pid,  'Bold', 'black');
	log_output("]: ",  undef, 'green');
    }
    else {
	log_output($rec[1] . " ",  undef, 'green');
    }

    foreach my $therest (2 .. $#rec) {
	$col = 'darkcyan';

	# Check for keywords to highlight
	foreach (@word_good) { $col = $col_good if $_ eq $rec[$therest] }
	foreach (@word_warn) { $col = $col_warn if $_ eq $rec[$therest] }
	foreach (@word_bad)  { $col = $col_bad  if $_ eq $rec[$therest] }
	foreach (@word_note) { $col = $col_note if $_ eq $rec[$therest] }
	
 	# Watch for words that indicate entire lines should be highlighted
 	#foreach (@line_good) { $col = $col_good if $_ eq $rec[$therest] }
 	#foreach (@line_warn) { $col = $col_warn if $_ eq $rec[$therest] }
	#foreach (@line_bad)  { $col = $col_bad  if $_ eq $rec[$therest] }
 	
	log_output("$rec[$therest] ", undef, $col);
    }
    log_output("\n",  undef, 'black');
    log_output__real() if $isExplain;
}


#    log_output (Gtk2::TextView, [ [ ... ] ])
sub log_output {
    my ($text, $font, $col) = @_;
    my $tag = join($font, $col);
    push @logs, [ $text, $tag ];
    $tags{$tag} ||= { if_($font, font => $font), foreground => $col };
}

sub log_output__real() {
    text_append($log_text, \@logs);
    undef @logs;
    $log_text->scroll_to_iter($log_buf->get_end_iter, 0, 1, 0.5, 0.5);
}


#-------------------------------------------------------------
# mail/sms alert
#-------------------------------------------------------------

sub alert_config() {
    local $::isEmbedded = 0;
    undef $::WizardTable;
    undef $::WizardWindow;
    my $conffile = "/etc/sysconfig/mail_alert";
    my %options = getVarsFromSh($conffile);
    $options{LOAD} ||= 3;
    $options{MAIL} ||= "root";
    $options{SMTP} ||= "localhost";
    
    my $service = {
		  httpd => N("Apache World Wide Web Server"), 
		  bind => N("Domain Name Resolver"),
		  ftp => N("Ftp Server"),
		  postfix => N("Postfix Mail Server"),
		  samba => N("Samba Server"),
		  sshd => N("SSH Server"),
		  webmin => N("Webmin Service"),
		  xinetd => N("Xinetd Service")
		 };
    my @installed_d = grep { -e "/etc/init.d/$_" } sort keys %$service;
    my %services_to_check = map { $_ => 1 } split(':', $options{SERVICES});

    $::isWizard = 1;
    use wizards;
    my $w = wizards->new;
    my $mode;
    my $cron_file = "/etc/cron.hourly/logdrake_service";
    my %modes = (
                   configure => N("Configure the mail alert system"),
                   disable =>  N("Stop the mail alert system"),
                  );
    my $wiz = {
               defaultimage => "logdrake.png",
               name => N("Mail alert"),
               pages => {
                         welcome => {
                                     name => N("Mail alert configuration") . "\n\n" .
                                     N("Welcome to the mail configuration utility.\n\nHere, you'll be able to set up the alert system.\n"),
                                     no_back => 1,
                                     data => [
                                              { val => \$mode, label => N("What do you want to do?"),
                                                list => [ keys %modes ], format => sub { $modes{$_[0]} },  },
                                              ],

                                     post => sub { $mode eq 'configure' ? 'services' : 'stop' },
                                    },
                         services => {
                                      name => N("Services settings") . "\n\n" .
                                      N("You will receive an alert if one of the selected services is no longer running"),
                                      data => [ map { { label => $_, val => \$services_to_check{$_}, 
                                                          type => "bool", text => $service->{$_} } } @installed_d ],
                                      next => "load",
                                     },
                         load => {
                                  #PO- Here "load" is a noun; that is load refers to the system/CPU) load
                                  name => N("Load setting") . "\n\n" .
                                  N("You will receive an alert if the load is higher than this value"),
                                  data => [ { label => N("_: load here is a noun, the load of the system\nLoad"), 
                                              val => \$options{LOAD}, type => 'range', min => 1, max => 50 } ],
                                  next => "email",
                                 },
                         email => {
                                   name => N("Alert configuration") . "\n\n" .
                                   N("Please enter your email address below ") . "\n" .
                                   N("and enter the name (or the IP) of the SMTP server you whish to use"),
                                   data => [
                                            { label => "Email address", val => \$options{MAIL} },
                                            { label => "Email server", val => \$options{SMTP} },
                                           ],
                                   complete => sub {
                                       if ($options{MAIL} !~ /[\w.-]*\@[\w.-]/ && !member($options{MAIL}, map { $_->[0] } list_passwd())) {
                                           err_dialog(N("Error"), N("\"%s\" neither is a valid email nor is an existing local user!",
                                                                          $options{MAIL}));
                                           return 1;
                                       }
                                       if (member($options{MAIL}, map { $_->[0] } list_passwd()) && $options{SMP} !~ /localhost/) {
                                           err_dialog(N("Error"), N("\"%s\" is a local user, but you did not select a local smtp, so you must use a complete email address!", $options{MAIL}));
                                           return 1;
                                       }
                                   },
                                   next => "end",
                                  },
                         end => {
                                 name => N("Congratulations") . "\n\n" . N("The wizard successfully configured the mail alert."), 
                                 end => 1,
                                 no_back => 1,
                                },
                         stop => {
                                  pre => sub { eval { rm_rf($cron_file) } },
                                  name => N("Congratulations") . "\n\n" . N("The wizard successfully disabled the mail alert."), 
                                  end => 1,
                                  no_back => 1,
                                },
                        },
              };
    $w->process($wiz, $in);
    return if $mode eq 'disable';
    
    $options{SERVICES} = join ':', grep { $services_to_check{$_} } sort keys %services_to_check;

    use Data::Dumper;
    output_with_perm $cron_file, 0755, q(#!/usr/bin/perl
# generated by logdrake
use MDK::Common;
my $r;
my %options = getVarsFromSh("/etc/sysconfig/mail_alert");

#- check services
my ) . Data::Dumper->Dump([ $service ], [qw(*services)]) . q(
foreach (split(':', $options{SERVICES})) {
    next unless $services{$_};
    $r .= "Service $_ ($services{$_} is not running)\\n" unless -e "/var/lock/subsys/$_";
}

#- load
my ($load) = split ' ', first(cat_("/proc/loadavg"));
$r .= "Load is huge: $load\n" if $load > $options{LOAD};

#- report it
if ($r) {
    use Mail::Mailer;
    my $mailer = Mail::Mailer->new('smtp', Server => $options{SMTP});
    $mailer->open({ From    => 'root@localhost',
                    To      => $options{MAIL},
                    Subject => "DrakLog Mail Alert",
                  })
      or die "Can't open: $!\n";
    print $mailer $r;
    $mailer->close;
}

# EOF);
    setVarsInSh($conffile, \%options);
        
    if (defined $::WizardWindow) {
	$::WizardWindow->destroy;
	undef $::WizardWindow;
    }
}


#-------------------------------------------------------------
# menu callback functions
#-------------------------------------------------------------


sub save() {
    $::isWizard = 0;
    my $y = $in->ask_file(N("Save as.."), "/root") or return;
    my $buf = $log_text->get_buffer;
    output($y, $buf->get_text(($buf->get_bounds), 0));
}