1#!/usr/local/bin/perl -w
2
3# USAGE:
4#
5# /SET default_chanmode <modes>
6#  - sets the desired default chanmodes
7#
8# Written by Jakub Jankowski <shasta@atn.pl>
9# for Irssi 0.7.98.CVS
10#
11# please report any bugs
12
13use strict;
14use vars qw($VERSION %IRSSI);
15
16$VERSION = "1.1";
17%IRSSI = (
18    authors     => 'Jakub Jankowski',
19    contact     => 'shasta@atn.pl',
20    name        => 'Default Chanmode',
21    description => 'Allows your client to automatically set desired chanmode upon a join to an empty channel.',
22    license     => 'GNU GPLv2 or later',
23    url         => 'http://irssi.atn.pl/',
24);
25
26use Irssi 20011211.0107 ();
27use Irssi::Irc;
28
29# defaults
30my $default_chanmode = "";
31
32# str parse_mode($string)
33# gets +a-e+bc-fg xyz
34# returns +abc-efg xyz
35sub parse_mode {
36	my ($string) = @_;
37	my ($modeStr, $rest) = split(/ +/, $string, 2);
38	my @modeParams = split(/ +/, $rest);
39	my $ptr = 0;
40	my ($mode, $plusmodes, $minusmodes, $args, $finalstring);
41
42	# processing the default_chanmode setting
43	foreach my $char (split(//, $modeStr)) {
44		if ($char eq "+") {
45			$mode = "+";
46		} elsif ($char eq "-") {
47			$mode = "-";
48		} else {
49			if ($mode eq "+") {
50				$plusmodes .= $char;
51			} elsif ($mode eq "-") {
52				$minusmodes .= $char;
53			}
54			if ($char =~ /[beIqoOdhvk]/ || ($char eq "l" && $mode eq "+")) {
55				# those are modes with arguments, so increase the pointer
56				$args .= " ".$modeParams[$ptr++];
57			}
58		}
59	}
60
61	# concatenating results
62	$finalstring .= "+".$plusmodes if (length($plusmodes) > 0);
63	$finalstring .= "-".$minusmodes if (length($minusmodes) > 0);
64	$finalstring .= $args if (length($args) > 0);
65
66	# debug stuff if you want
67	# Irssi::print("parse_mode($string) returning '$finalstring'");
68
69	return $finalstring;
70}
71
72# void event_channel_sync($channel)
73# triggered on join
74sub event_channel_sync {
75	my ($channel) = @_;
76
77	# return unless default_chanmode contains something valuable
78	my $mode = parse_mode(Irssi::settings_get_str('default_chanmode'));
79	return unless $mode;
80
81	# return unless $channel is active, synced, not modeless, and we're a chanop
82	return unless ($channel && $channel->{synced} && $channel->{chanop} && !$channel->{no_modes});
83
84	# check if we're the only one visitor
85	my @nicks = $channel->nicks();
86	return unless (scalar(@nicks) == 1);
87
88	# final stage: issue the MODE
89	$channel->command("/MODE ".$channel->{name}." ".$mode);
90}
91
92Irssi::settings_add_str('misc', 'default_chanmode', $default_chanmode);
93Irssi::signal_add_last('channel sync', 'event_channel_sync');
94
95# changes:
96#
97# 25.01.2002: Initial release (v1.0)
98# 24.02.2002: splitted into two subroutines, minor cleanups (v1.1)
99