weechat

- me personal weechat setup 🔵🟢
git clone git://git.acid.vegas/weechat.git
Log | Files | Refs | Archive | README

keepnick.pl (7968B)

      1 # This script was made to improve upon keepnick.py
      2 
      3 use strict;
      4 use warnings;
      5 
      6 no strict 'subs';
      7 
      8 my $SCRIPT_NAME = 'keepnick';
      9 my $SCRIPT_AUTHOR = 'The Krusty Krab <wowaname@volatile.ch>';
     10 my $SCRIPT_VERSION = '1.0';
     11 my $SCRIPT_LICENCE = 'Public domain';
     12 my $SCRIPT_DESC = 'Keep your primary nickname';
     13 
     14 our (%waiting, %connecting, %noison);
     15 
     16 my %OPTIONS = (
     17 	default_enable => ['Whether to enable keepnick on servers by default', '0'],
     18 	check_time => ['Time between ISON checks (reload script to take effect)', '60'],
     19 );
     20 
     21 
     22 sub arrindex
     23 {
     24 	1 while $_[0] ne pop;
     25 	return @_ - 1;
     26 }
     27 
     28 sub info
     29 {
     30 	weechat::print(weechat::buffer_search('irc', 'server.'.shift),
     31 		weechat::prefix('server').shift);
     32 }
     33 
     34 sub my_nick
     35 {	return lc weechat::info_get('irc_nick', shift); }
     36 
     37 sub is_preferable
     38 {
     39 	my ($server, $target) = @_;
     40 	my ($targidx, $myidx) = (
     41 		arrindex($target, @{$waiting{$server}}),
     42 		arrindex(my_nick($server), @{$waiting{$server}}));
     43 	return ($myidx == -1 or $targidx < $myidx);
     44 }
     45 
     46 sub is_waiting
     47 {
     48 	my $server = lc shift;
     49 	return exists $waiting{$server} ? @{ $waiting{$server} } : ();
     50 }
     51 
     52 
     53 sub disable
     54 {	delete $waiting{lc shift}; }
     55 
     56 sub enable
     57 {
     58 	my $server = shift;
     59 	my $force = shift // 0;
     60 	my @target = @_ ? split ',', shift : ();
     61 	unless (@target) {
     62 		my ($conf_nicks, $confd_nicks) = (
     63 			weechat::config_get("irc.server.$server.nicks"),
     64 			weechat::config_get("irc.server_default.nicks"));
     65 		@target = split ',', weechat::config_string(
     66 			weechat::config_option_is_null($conf_nicks)
     67 			? $confd_nicks : $conf_nicks);
     68 	}
     69 	$server = lc $server;
     70 	my ($defaultconf, $serverconf) = (
     71 		weechat::config_get_plugin('default_enable'),
     72 		weechat::config_get_plugin("server.$server"));
     73 	unless ($force) {
     74 		return () if $serverconf ne '' and !$serverconf;
     75 		return () if $serverconf eq '' and !$defaultconf;
     76 	}
     77 	return () if lc $target[0] eq my_nick($server);
     78 
     79 	$waiting{$server} = \@target;
     80 	return @target;
     81 }
     82 
     83 
     84 sub do_nick
     85 {
     86 	my ($server, $nick) = @_;
     87 	weechat::hook_signal_send('irc_input_send',
     88 		weechat::WEECHAT_HOOK_SIGNAL_STRING,
     89 		"$server;;;;/nick $nick");
     90 	if (lc $waiting{$server}->[0] eq lc $nick) {
     91 		# we're done if we have our primary nick choice, otherwise keep trying
     92 		disable($server);
     93 		info($server, "got your primary nick!");
     94 	} else {
     95 		info($server, "got one of your nicks; still trying for your primary nick");
     96 	}
     97 }
     98 
     99 sub try_nick
    100 {
    101 	my ($server, $oldnick) = @_;
    102 	($server, $oldnick) = (lc $server, lc $oldnick);
    103 	my ($mynick, @confnick) = (my_nick($server), is_waiting($server));
    104 	return unless @confnick;
    105 	return unless is_preferable($server, $oldnick);
    106 
    107 	if ($mynick eq $oldnick) { disable($server); }
    108 	else { lc $_ eq $oldnick and do_nick($server, $_) for @confnick; }
    109 }
    110 
    111 sub ison_check
    112 {
    113 	my $iptr = weechat::infolist_get('irc_server', '', '');
    114 
    115 	while (weechat::infolist_next($iptr)) {
    116 		next unless weechat::infolist_integer($iptr, 'is_connected');
    117 		my $server = lc weechat::infolist_string($iptr, 'name');
    118 		next unless is_waiting($server);
    119 		next unless exists $noison{$server};
    120 		weechat::hook_hsignal_send('irc_redirect_command', {
    121 			server => $server,
    122 			pattern => 'ison',
    123 			signal => $SCRIPT_NAME,
    124 			timeout => weechat::config_get_plugin('check_time') - 1,
    125 			});
    126 		weechat::hook_signal_send('irc_input_send',
    127 			weechat::WEECHAT_HOOK_SIGNAL_STRING,
    128 			"$server;;;;/ison ".(join ' ', @{ $waiting{$server} }));
    129 	}
    130 
    131 	weechat::infolist_free($iptr);
    132 	return weechat::WEECHAT_RC_OK;
    133 }
    134 
    135 sub irc_ison
    136 {
    137 	my %hashtable = %{ pop() };
    138 	unless ($hashtable{output}) {
    139 		$noison{lc $hashtable{server}} = 1;
    140 		return weechat::WEECHAT_RC_ERROR;
    141 	}
    142 	my %nicks = map { lc $_ => 1 }
    143 		split / +/, ($hashtable{output} =~ s/^:[^ ]* 303 [^ ]+ :?//r);
    144 
    145 	for my $confnick (is_waiting($hashtable{server})) {
    146 		next if exists $nicks{lc $confnick}; # still taken
    147 		do_nick($hashtable{server}, $confnick);
    148 		last;
    149 	}
    150 	return weechat::WEECHAT_RC_OK;
    151 }
    152 
    153 sub irc_nick
    154 {
    155 	my (undef, $server, $oldnick, $newnick) = (shift,
    156 		shift =~ /(.+),irc_raw_in_nick/i,
    157 		shift =~ /:([^! ]+)[^ ]* nick :?(.*)/i);
    158 	return weechat::WEECHAT_RC_OK if lc $oldnick eq lc $newnick;
    159 	try_nick($server, $oldnick);
    160 	return weechat::WEECHAT_RC_OK;
    161 }
    162 
    163 sub irc_quit
    164 {
    165 	my (undef, $server, $nick) = (shift,
    166 		shift =~ /(.+),irc_raw_in_quit/i,
    167 		shift =~ /:([^! ]+)[^ ]* quit /i);
    168 	try_nick($server, $nick);
    169 	return weechat::WEECHAT_RC_OK;
    170 }
    171 
    172 sub irc_433
    173 {
    174 	my ($server) = $_[1] =~ /(.+),irc_raw_in2_433/i;
    175 	$server = lc $server;
    176 	# nick is taken when we connect? enable
    177 	if (exists $connecting{$server}) {
    178 		enable($server) and info($server, 'keepnick enabled');
    179 	}
    180 	return weechat::WEECHAT_RC_OK;
    181 }
    182 
    183 sub irc_connecting
    184 {
    185 	$connecting{lc pop} = 1;
    186 	return weechat::WEECHAT_RC_OK;
    187 }
    188 
    189 sub irc_connected
    190 {
    191 	delete $connecting{lc pop};
    192 	return weechat::WEECHAT_RC_OK;
    193 }
    194 
    195 sub irc_disconnect
    196 {
    197 	my $server = lc pop;
    198 	delete $connecting{$server};
    199 	disable($server);
    200 
    201 	return weechat::WEECHAT_RC_OK;
    202 }
    203 
    204 sub cmd_keepnick
    205 {
    206 	my (undef, $buffer, $command) = @_;
    207 	my $server = lc weechat::buffer_get_string($buffer, 'localvar_server');
    208 	for ($command) {
    209 		chomp;
    210 		if (/^$/) {
    211 			info($server, is_waiting($server)
    212 				? "trying to get '".(join "', '", is_waiting($server))."'"
    213 				: "not keeping your nick");
    214 		}
    215 		elsif (/^-enable$/) {
    216 			my @waiting = enable($server, 1);
    217 			if (@waiting) { info($server, "enabled, now waiting for '".(join "', '", @waiting)."'"); }
    218 			else { info($server, "you already have your primary nick! doing nothing"); }
    219 		}
    220 		elsif (/^-disable$/) {
    221 			disable($server);
    222 			info($server, "no longer trying to keep your nick");
    223 		}
    224 		elsif (/^-autoenable$/) {
    225 			weechat::config_get_plugin('default_enable')
    226 				? weechat::config_unset_plugin("server.$server")
    227 				: weechat::config_set_plugin("server.$server", '1');
    228 			enable($server);
    229 			info($server, "keepnick enabled on this server");
    230 		}
    231 		elsif (/^-autodisable$/) {
    232 			weechat::config_get_plugin('default_enable')
    233 				? weechat::config_set_plugin("server.$server", '0')
    234 				: weechat::config_unset_plugin("server.$server");
    235 			disable($server);
    236 			info($server, "keepnick disabled on this server");
    237 		}
    238 		else {
    239 			my @waiting = enable($server, 1, $command =~ s/ /,/gr);
    240 			if (@waiting) { info($server, "now waiting for '".(join "', '", @waiting)."'"); }
    241 			else { info($server, "you already have your primary nick! doing nothing"); }
    242 		}
    243 	}
    244 
    245 	return weechat::WEECHAT_RC_OK;
    246 }
    247 
    248 if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION,
    249  $SCRIPT_LICENCE, $SCRIPT_DESC, '', '')) {
    250 	for my $option (keys %OPTIONS) {
    251 		weechat::config_set_plugin($option, $OPTIONS{$option}[1])
    252 			unless weechat::config_is_set_plugin($option);
    253 		weechat::config_set_desc_plugin($option, $OPTIONS{$option}[0]);
    254 	}
    255 
    256 	weechat::hook_command('keepnick', $SCRIPT_DESC,
    257 		"[nick]\n-enable|-disable\n-autoenable|-autodisable",
    258 		"This command allows you to check the current server keepnick\n".
    259 		"status or to manually specify nicks (separated by comma) to keep.\n".
    260 		"You may also manually -enable or -disable keepnick on the current\n".
    261 		"server. Both -autoenable and -autodisable choices are saved in\n".
    262 		"config under 'plugins.var.perl.$SCRIPT_NAME.server.xxx' where 'xxx'\n".
    263 		"is the server name.",
    264 		'-enable|-disable|-autoenable|-autodisable %-', 'cmd_keepnick', '');
    265 	weechat::hook_signal('*,irc_raw_in_nick', 'irc_nick', '');
    266 	weechat::hook_signal('*,irc_raw_in_quit', 'irc_quit', '');
    267 	weechat::hook_signal('*,irc_raw_in2_433', 'irc_433', '');
    268 	weechat::hook_signal('irc_server_connecting', 'irc_connecting', '');
    269 	weechat::hook_signal('irc_server_connected', 'irc_connected', '');
    270 	weechat::hook_signal('irc_server_disconnected', 'irc_disconnect', '');
    271 	weechat::hook_timer(weechat::config_get_plugin('check_time') * 1000, 0, 0,
    272 		'ison_check', '');
    273 	weechat::hook_hsignal("irc_redirection_${SCRIPT_NAME}_ison", 'irc_ison', '');
    274 
    275 	my $iptr = weechat::infolist_get('irc_server', '', '');
    276 	enable(weechat::infolist_string($iptr, 'name'))
    277 		while (weechat::infolist_next($iptr));
    278 	weechat::infolist_free($iptr);
    279 }