#/###############################################################################\
## Messages Filter script by ongeboren <ongeboren@users.sourceforge.net>         #
## Version: 1.0                                                                  #
## Requires: XChat 2.0.8+                                                        #
## Distributed under the GPL license                                             #
#\###############################################################################/
Xchat::register( "Messages Filter" , "1.0" , "Redirect inbount messages to a single window if not in conversation" );
Xchat::print( "Loading the messages filter script..\n" );

my $all_tab = "-all";
my $time = time();

Xchat::hook_server( "NOTICE" , filter_messages2 );
Xchat::hook_server( "PRIVMSG" , filter_messages2 );


sub filter_messages2 {
  
  my $me   = Xchat::get_info ( "nick" );
  my $serv = Xchat::get_info ( "server" );
  my $dest = $_[0][2];
  my $srce = $_[0][0];
  
  return Xchat::EAT_NONE if ( lc($dest) ne lc($me) ); # if channel text
  return Xchat::EAT_NONE if ( $srce !~ /!/ );         # if not from a person
  return Xchat::EAT_NONE if ( substr($_[1][3], 0, 5) eq ":\001DCC" );    # dcc traffic
  
  $srce =~ /:(\S+)!/;
  my $victim = $1;
  
  if ( !Xchat::find_context($victim, $serv) ) { # if tab does not exist ?
    Xchat::command( "QUERY $all_tab" , undef, $serv);
    Xchat::set_context( Xchat::find_context($all_tab, $serv) );
    my $txt = substr($_[1][3], 1);
    Xchat::emit_print("Channel Message", ($victim, $txt, "", "") );
    if ( $time + 5 < time() && lc($victim) ne lc($me) )
    {
      Xchat::command( "WHOIS $victim" , undef, $serv);
      $time = time();
    }
    return Xchat::EAT_ALL;
  }
  return Xchat::EAT_NONE;
}

Xchat::print( "\0033Messages filter by ongeboren Loaded with success\003\n" );
