use strict; use warnings; use Xchat qw(:all); # Description: # Move notices to the front if one comes in for the focused network # if it is not for the focused network then move it to a query tab if # one is opened matching the sender # if there isn't a query tab opened matching the sender then move it to # the server tab # ### Configuration ############################################################# # if set to 1 then notices will be copied to a separate tab my $USE_TAB = 1; # when set to 1 a separate tab is created for each network # when set to 0 notices from all networks will be merged together my $USE_PER_NETWORK_TAB = 1; # the name of the tab that notices will be copied to if $USE_TAB is enabled my $TAB_NAME = ""; # always move the notice to a query or server tab regardless of focus my $ALWAYS_MOVE = 0; ############################################################################### register( "Notice to Front", "1.0201", "Move all notices to the front tab", ); hook_print( "Notice", sub { my $conn_id = get_info "id"; my $origin_ctx = get_context; # switch to the front context set_context( find_context ); # send the notice to a query tab or the server tab if the front tab # is on a different network if( $ALWAYS_MOVE or $conn_id != get_info "id" ) { # back to the original context set_context( $origin_ctx ); # try to switch to a query tab if there is one if( set_context( $_[0][0] , get_info( "server" ) ) ) { if( $conn_id != get_info "id" ) { # found a query tab but it's on the wrong connection # connected to the same server twice and ... unless( switch_to_server( $conn_id ) ) { return EAT_NONE; } } } else { # no query tab either, send the message to the server tab unless( switch_to_server( $conn_id ) ) { # no server tab either?! return EAT_NONE; } } } emit_print( "Notice", @{$_[0]} ); if( $USE_TAB && get_info( "channel" ) ne $TAB_NAME && switch_to_tab( $TAB_NAME, { tab_per_network => $USE_PER_NETWORK_TAB } ) ) { # put a copy in the notice tab emit_print( "Notice", @{$_[0]} ); } return EAT_ALL; }, { priority => PRI_HIGHEST } ); # switch to the server tab for the current connection # return true if successful sub switch_to_server { my $connection_id = shift; for my $tab( get_list "channels") { if( $tab->{id} == $connection_id && $tab->{type} == 1 ) { return set_context( $tab->{context} ); } } return; } # switch to the notice tab based on the current config # return true if successful sub switch_to_tab { my $tab_name = shift; my $opts = shift; my $per_network = $opts->{tab_per_network}; my $tab_type = $per_network ? "dialog" : "server"; my $context = get_tab( $tab_name, { type => $tab_type } ); if( $context ) { return set_context( $context ); } return; } sub get_tab { my $tab_name = shift; my $opts = shift; my $type = $opts->{type}; my $tab_opts = { type => $type }; my $tab = find_tab( $tab_name, $tab_opts ); unless( $tab ) { create_tab( $tab_name, $tab_opts ); $tab = find_tab( $tab_name, $tab_opts ); } return $tab; } sub find_tab { my $tab_name = shift; my $opts = shift; my $type = $opts->{type} eq 'server' ? 1 : 3; my $tab; if( $opts->{type} eq 'server' ) { ($tab) = grep { $_->{channel} eq $tab_name && $_->{type} == $type } get_list( "channels" ); } else { my $conn_id = get_info "id"; ($tab) = grep { $_->{channel} eq $tab_name && $_->{type} == $type && $_->{id} == $conn_id } get_list( "channels" ); } if( $tab ) { return $tab->{context}; } return; } sub create_tab { my $tab_name = shift; my $opts = shift; my $type = $opts->{type}; if( $type eq 'server' ) { command( "NEWSERVER -noconnect $tab_name" ); } else { command( "QUERY -nofocus $tab_name" ) } }