1#!/usr/local/bin/perl -w
2
3## Bugreports and Licence disclaimer.
4#
5# For bugreports and other improvements contact Geert Hauwaerts <geert@hauwaerts.be>
6#
7#   This program is free software; you can redistribute it and/or modify it
8#   under the terms of the GNU General Public License as published by the Free
9#   Software Foundation; either version 2 of the License, or (at your option)
10#   any later version.
11#
12#   This program is distributed in the hope that it will be useful, but WITHOUT
13#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14#   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15#   more details.
16#
17#   You should have received a copy of the GNU General Public License along with
18#   this script; if not, write to the Free Software Foundation, Inc., 59 Temple
19#   Place, Suite 330, Boston, MA  02111-1307  USA.
20##
21
22
23## Documentation.
24#
25# Versioning:
26#
27#   This script uses the YEAR.FEATURE.REVISION versioning scheme and must abide
28#   by the follwing rules:
29#
30#       1) when adding a new feature, you must increase the FEATURE
31#          numeric by one;
32#
33#       2) when fixing a bug, you must increase the REVISION numeric
34#          by one; and
35#
36#       3) the first feature or bug change in any given year must set the YEAR
37#          numeric to the two digit representation of the current year, and
38#          reset the FEATURE and REVISION numerics to 01.
39#
40# Settings:
41#
42#   active_notice_show_in_status_window
43#
44#       When enabled, notices will also be sent to the status window.
45##
46
47
48##
49# Load the required libraries.
50##
51
52use strict;
53use Irssi;
54use vars qw($VERSION %IRSSI);
55
56
57##
58# Declare the administrative information.
59##
60
61$VERSION = '18.01.01';
62
63%IRSSI = (
64    authors     => 'Geert Hauwaerts',
65    contact     => 'geert@hauwaerts.be',
66    name        => 'active_notice.pl',
67    description => 'This script shows incoming notices into the active channel.',
68    license     => 'GNU General Public License',
69    url         => 'https://github.com/GeertHauwaerts/irssi-scripts/blob/master/src/active_notice.pl',
70    changed     => '2018-07-27',
71);
72
73
74##
75# Register the custom theme formats.
76##
77
78Irssi::theme_register([
79    'active_notice_loaded', '%R>>%n %_Scriptinfo:%_ Loaded $0 version $1 by $2.'
80]);
81
82
83## Function.
84#
85# Irssi::active_notice::notice_move() function.
86#
87#   Function:       notice_move()
88#   Arguments:      The destination.
89#                   The text.
90#                   The stripped text.
91#
92#   Description:    Print received notices into the active window.
93##
94
95sub notice_move {
96
97
98    ##
99    # Parse the parameters.
100    ##
101
102    my ($dest, $text, $stripped) = @_;
103    my $server                   = $dest->{'server'};
104
105
106    ##
107    # Check whether the message is irrelevant.
108    ##
109
110    if (!$server || !($dest->{level} & MSGLEVEL_NOTICES) || $server->ischannel($dest->{'target'})) {
111        return;
112    }
113
114
115    ##
116    # Fetch the source, destination and status windows.
117    ##
118
119    my $witem  = $server->window_item_find($dest->{'target'});
120    my $status = Irssi::window_find_name("(status)");
121    my $awin   = Irssi::active_win();
122
123
124    ##
125    # Check whether we have a window for the source of the notice.
126    ##
127
128    if (!$witem) {
129
130
131        ##
132        # Check whether the notice originated from the status window.
133        ##
134
135        if ($awin->{'name'} eq "(status)") {
136            return;
137        }
138
139        ##
140        # replace a single % with %%. (by  Dwarf <dwarf@rizon.net>)
141        ##
142
143		$text =~ s/%/%%/g;
144
145        ##
146        # Print the notice in the active window.
147        ###
148
149        $awin->print($text, MSGLEVEL_NOTICES);
150
151
152        ##
153        # Check whether the notice needs to be printed in the status window.
154        ##
155
156        if (!Irssi::settings_get_bool('active_notice_show_in_status_window')) {
157            Irssi::signal_stop();
158        }
159    } else {
160
161
162        ##
163        # Check whether we need to print the notice in the status window.
164        ##
165
166        if (($awin->{'name'} ne "(status)") && (Irssi::settings_get_bool('active_notice_show_in_status_window'))) {
167            $status->print($text, MSGLEVEL_NOTICES);
168        }
169
170
171        ##
172        # Check whether the notice originated from the active window.
173        ##
174
175        if ($witem->{'_irssi'} == $awin->{'active'}->{'_irssi'}) {
176            return;
177        }
178
179
180        ##
181        # Print the notice in the active window.
182        ##
183
184        $awin->print($text, MSGLEVEL_NOTICES);
185    }
186}
187
188
189##
190# Register the signals to hook on.
191##
192
193Irssi::signal_add('print text', 'notice_move');
194
195
196##
197# Register the custom settings.
198##
199
200Irssi::settings_add_bool('active_notice', 'active_notice_show_in_status_window', 1);
201
202
203##
204# Display the script banner.
205##
206
207Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'active_notice_loaded', $IRSSI{name}, $VERSION, $IRSSI{authors});
208