1#  Copyright (c) 2006 Christoph Berg <cb@df7cb.de>
2#  All rights reserved.
3#
4#  Redistribution and use in source and binary forms, with or without
5#  modification, are permitted provided that the following conditions
6#  are met:
7#  1. Redistributions of source code must retain the above copyright
8#     notice, this list of conditions and the following disclaimer.
9#  2. Redistributions in binary form must reproduce the above copyright
10#     notice, this list of conditions and the following disclaimer in the
11#     documentation and/or other materials provided with the distribution.
12#
13#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23#  SUCH DAMAGE.
24
25#  This script adds support for +q (quiet user) channel modes to irssi.
26
27use strict;
28use Irssi;
29use Irssi::Irc;
30use vars qw($VERSION %IRSSI);
31
32$VERSION = '0.2';
33%IRSSI = (
34    authors     => 'Christoph Berg',
35    contact     => 'cb@df7cb.de',
36    name        => 'quiet',
37    description => 'support for +q (quiet user) channel mode',
38    license     => '2-clause BSD',
39);
40
41# :helium.oftc.net 344 Tauon #test test!*@* cryogen!stu@o.net 1164222156
42# :helium.oftc.net 345 Tauon #test :End of Channel Quiet List
43
44sub event_quiet_list
45{
46	my ($server, $data, $srvname) = @_;
47	my ($target, $channel, $mask, $by, $time) = split(/\s+/, $data);
48	$time = time() - $time if $time;
49	$server->window_find_item($channel)->printformat(MSGLEVEL_CRAP,
50		$by ? "quietlist_long" : "quietlist", $channel, $mask, $by, $time);
51}
52
53sub event_quiet_list_end
54{
55	my ($server, $data, $srvname) = @_;
56	my ($target, $channel, $text) = split(/\s+/, $data, 3);
57	$text =~ s/^://;
58	$server->window_find_item($channel)->print($text, MSGLEVEL_CRAP);
59}
60
61sub do_quiet
62{
63	my ($data, $server, $witem, $quiet) = @_;
64	my $support = $server->isupport("CHANMODES");
65	if ($support !~ /q/) {
66		Irssi::print("This server does not support channel mode +q");
67		return;
68	}
69	if (!$witem or $witem->{type} ne "CHANNEL") {
70		Irssi::print("Not joined to any channel");
71		return;
72	}
73	my @data = split /\s+/, $data;
74	my $mode = @data > 0 ? ($quiet ? "+" : "-") . ("q" x (@data)) . " @data" : "+q";
75	$witem->command("mode $witem->{name} $mode");
76}
77
78sub quiet { do_quiet(@_, 1); }
79sub unquiet { do_quiet(@_, 0); }
80
81Irssi::theme_register([
82	"quietlist" => '{channel $0}: ban quiet {ban $1}',
83	"quietlist_long" => '{channel $0}: ban quiet {ban $1} {comment by {nick $2}, $3 secs ago}',
84]);
85
86Irssi::signal_add("event 344", "event_quiet_list");
87Irssi::signal_add("event 345", "event_quiet_list_end");
88
89Irssi::command_bind('quiet', 'quiet');
90Irssi::command_bind('unquiet', 'unquiet');
91