Script: mnick.pl

Change nick on multiple servers using custom masks.
Author: CrazyCat — Version: 0.4 — License: GPL3
For WeeChat ≥ 0.3.0.
Added: 2014-04-01 — Updated: 2019-09-22
Download

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Set WeeChat and plugins options interactively.
#
# Description:
#
# This script allows to change your nick on the different networks you are
# connected, by appending or removing a suffix to your current nick on the
# network, based on the defined mask
# Command: /mnick [suffix] [away reason]
# * if suffix is set and script enabled on network, will do a
#   /nick <current_nick><formatted_suffix>
# * if suffix is not set, will do a
#   /nick <previous_nick>
#
# Settings:
#  * plugins.var.perl.mnick.<network>_enabled : on/off
#  * plugins.var.perl.mnick.<network>_mask : default [%s]
#  * plugins.var.perl.mnick.<network>_away : on/off
#
# Example
# I'm CrazyCat on net1 and net3, GatoLoco on net2
#  * plugins.var.perl.mnick.net1_enabled : on
#  * plugins.var.perl.mnick.net1_mask : [%s]
#  * plugins.var.perl.mnick.net1_away : off
#  * plugins.var.perl.mnick.net2_enabled : on
#  * plugins.var.perl.mnick.net2_mask : |%s
#  * plugins.var.perl.mnick.net1_away : off
#  * plugins.var.perl.mnick.net3_enabled : on
#  * plugins.var.perl.mnick.net3_mask : [%s]
#  * plugins.var.perl.mnick.net1_away : on
# /mnick Test
# => CrazyCat[Test] on net1, GatoLoco|Test on net2, CrazyCat on net3
# => Away status won't change
# /mnick AFK I'm no more here
# => CrazyCat[AFK] on net1, GatoLoco|AFK on net2, CrazyCat on net3
# => I'll be turned away on net2 and net3 with "I'm no more here" reason
# /mnick Test
# => CrazyCat[Test] on net1, GatoLoco|Test on net2, CrazyCat on net3
# => Away status won't change (keep the previous one)
# /mnick
# => CrazyCat on net1 and net3, GatoLoco on net2
# => away status is removed on net2 and net3
#
# History:
# 2019-09-12, CrazyCat <crazycat@c-p-f.org>
#	version 0.4 : add an optionnal away reason
# 2016-05-23, CrazyCat <crazycat@c-p-f.org>:
#    version 0.3 : now, you can use alternate nick without doing
#    a /mnick before
# 2015-01-09, CrazyCat <crazycat@c-p-f.org>:
#    version 0.2 : corrected a stupid bug. Nick change is now only sent
#    to connected networks
# 2014-04-01, CrazyCat <crazycat@c-p-f.org>:
#    version 0.1 : first official version

weechat::register("mnick", "CrazyCat", "0.4", "GPL", "Multi Nick Changer", "", "");

weechat::hook_command(
	"mnick",
	"Multi Nick Changer",
	"mnick [extension] [away reason]",
	"",
	"",
	"mnick_change",
	""
);

sub mnick_setup
{
	$infolist = weechat::infolist_get("irc_server", "", "");
	while (weechat::infolist_next($infolist))
	{
		my $name = weechat::infolist_string($infolist, "name");
		if (!weechat::config_is_set_plugin($name."_mask"))
		{
			weechat::config_set_plugin($name."_mask", "[%s]");
		}
		if (!weechat::config_is_set_plugin($name."_enabled"))
		{
			weechat::config_set_plugin($name."_enabled", "off");
		}
		if (!weechat::config_is_set_plugin($name."_away"))
		{
			weechat::config_set_plugin($name."_away", "off");
		}
	}
	weechat::infolist_free($infolist);
}

sub mnick_change
{
	my ($data, $buffer, $args) = @_;
	my $ext;
	my @reason;
	if ($args ne "") {
		($ext, @reason) = split(" ", $args);
	}
	my $newnick;
	my $nick;
	my @nicks;
	my $name;
	my $hasreason = @reason;
	$infolist = weechat::infolist_get("irc_server", "", "");
	if ($ext)
	{
		while (weechat::infolist_next($infolist))
		{
			$name = weechat::infolist_string($infolist, "name");
			if (weechat::config_is_set_plugin($name."_enabled")
				&& weechat::config_get_plugin($name."_enabled") eq "on"
				&& weechat::infolist_integer($infolist, "is_connected")==1)
			{
				if (!weechat::config_is_set_plugin($name."_backnick")
					|| weechat::config_get_plugin($name."_backnick") eq "")
				{
					$nick = weechat::info_get('irc_nick', $name);
					weechat::config_set_plugin($name."_backnick", $nick);

				} else {
					$nick = weechat::config_get_plugin($name."_backnick");
				}
				$newnick = sprintf($nick . weechat::config_get_plugin($name."_mask"), $ext);
				weechat::command($name, "/quote -server ".$name." nick ".$newnick);
			}
			if ( $hasreason != 0
				&& weechat::infolist_integer($infolist, "is_connected")==1
				&& weechat::config_is_set_plugin($name."_away")
				&& weechat::config_get_plugin($name."_away") eq "on")
			{
				weechat::command($name, "/quote -server ".$name. " away :".join(" ", @reason));
			}
		}
	} else {
		while (weechat::infolist_next($infolist))
		{
			$name = weechat::infolist_string($infolist, "name");
			$nick = weechat::info_get('irc_nick', $name);
			if (weechat::config_is_set_plugin($name."_enabled")
				&& weechat::config_get_plugin($name."_enabled") eq "on"
				&& weechat::infolist_integer($infolist, "is_connected")==1)
			{
				if (weechat::config_is_set_plugin($name."_backnick")
					&& weechat::config_get_plugin($name."_backnick") ne "") {
					$newnick = weechat::config_get_plugin($name."_backnick");
				} else {
					@nicks = split(',', weechat::infolist_string($infolist, "nicks"));
					$newnick = $nicks[0];
				}
				weechat::command($name, "/quote -server ".$name." nick ".$newnick);
				weechat::config_set_plugin($name."_backnick", "");
			}
			if ( weechat::infolist_integer($infolist, "is_connected")==1
				&& weechat::config_is_set_plugin($name."_away")
				&& weechat::config_get_plugin($name."_away") eq "on")
			{
				weechat::command($name, "/quote -server ".$name. " away");
			}
		}
	}
	weechat::infolist_free($infolist);
	return weechat::WEECHAT_RC_OK;
}

mnick_setup;