#!/usr/bin/perl -wc
# Prints private messages and notices notices from people in the first
# channel where they are joined with you.
#
# Based on noticemove.pl 1.01 by Timo Sirainen

use Irssi;
use strict;

use vars qw($VERSION %IRSSI);

$VERSION = '20040117';
%IRSSI = (
	authors			=> 'Marco d\'Itri',
	contact			=> 'md@linux.it',
	name			=> 'ircop',
	description		=> 'things useful to ircops',
	license			=> 'GPL v2',
	url				=> 'http://www.linux.it/~md/irssi/',
);

my $in_signal = 0;

Irssi::signal_add('print text' => \&sig_print_text);

sub sig_print_text {
	my ($dest, $text, $stripped) = @_;

	my $server = $dest->{server};

	# ignore non-notices, messages not for us and notices in channels
	return if not $server
		or not ($dest->{level} & MSGLEVEL_NOTICES or
				$dest->{level} & MSGLEVEL_MSGS)
		or $server->ischannel($dest->{target});

	# do not divert queries
	foreach my $query ($server->queries) {
		return if $query->{name} eq $dest->{target};
	}

	return if $in_signal;
	$in_signal = 1;

	# get the list of channels joined by the user
	my @channels;
	foreach my $channel ($server->channels) {
		push(@channels, $channel) if $channel->nick_find($dest->{target});
	}

	if (@channels == 1) {
		$text =~ s/%/%%/g;
		$channels[0]->print($text, MSGLEVEL_MSGS);
		Irssi::signal_stop();
	}

	$in_signal = 0;
}

