Script: rslap.pl

Slaps with random messages.
Author: KenjiE20 — Version: 1.4 — License: GPL3
For WeeChat ≥ 0.3.0.
Tags: irc
Added: 2009-07-20 — Updated: 2021-05-11

Download GitHub Repository

  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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#
# rslap.pl - Random slap strings for weechat 0.3.0
#
# Let's you /slap a nick but with a random string
# Customisable via the 'rslap' file in your config dir
# The rslap file is plain text, with one message per line
# Use '$nick' to denote where a nick should go
#
# Usage:
# /rslap <nick> [<entry]>
#  Slaps <nick> with a random slap, entry will use that entry
#  number instead of a random one
#
# /rslap_info
#  This tells you how many messages there are, and prints them
#
# /rslap_add <string to add>
# /rslap_remove <entry id>
#  Adds / removes string/id from the available list and attempts
#  to update the rslap file
#
# /set plugins.var.perl.rslap.slapback
#  Sets the slapback, takes "off", "on/random", or "n" where n
#  is a valid entry number

# History:
# 2021-05-05, Sébastien Helleu <flashcode@flashtux.org>:
#       v1.4: add compatibility with XDG directories (WeeChat >= 3.2)
# 2010-12-30, KenjiE20 <longbow@longbowslair.co.uk>:
#	v1.3.1	-fix: uninitialised variable error
# 2010-04-25, KenjiE20 <longbow@longbowslair.co.uk>:
#	v1.3	-feature: Ability to add/remove entries
#		-feature: Can specify which string /rslap will use
#		-feature: Slapback with specified/random string
# 2009-08-10, KenjiE20 <longbow@longbowslair.co.uk>:
#	v1.2:	Correct /help format to match weechat base
# 2009-07-28, KenjiE20 <longbow@longbowslair.co.uk>:
#	v1.1:	-fix: Make file loading more robust
#		and strip out comments/blank lines
# 2009-07-09, KenjiE20 <longbow@longbowslair.co.uk>:
#	v1.0:	Initial Public Release

# Copyright (c) 2009-2010 by KenjiE20 <longbow@longbowslair.co.uk>
#
# 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/>.
#

weechat::register("rslap", "KenjiE20", "1.4", "GPL3", "Slap Randomiser", "", "");

my $weechat_dir = weechat::info_get("weechat_data_dir", "");
$weechat_dir = weechat::info_get("weechat_dir", "") if (!$weechat_dir);
$file = $weechat_dir."/rslap";
my @lines;
$lastrun = 0;
$rslap_slapback_hook = 0;
rslap_start();
rslap_slapback_toggle("","",weechat::config_get_plugin ("slapback"));

sub rslap_start
{
	if (-r $file)
	{
		weechat::hook_command("rslap", "Slap a nick with a random string", "nickname [entry]", "nickname: Nick to slap\n   entry: which entry number to use (/rslap_info for the list)\n\n /set plugins.var.perl.rslap.slapback\n  Sets the slapback, takes \"off\", \"on/random\", or \"n\" where n is a valid entry number", "%(nicks)", "rslap", "");
		weechat::hook_command("rslap_info", "Prints out the current strings /rslap will use", "", "", "", "rslap_info", "");
		weechat::hook_command("rslap_add", "Add a new slap entry", "[slap string]", "", "", "rslap_add", "");
		weechat::hook_command("rslap_remove", "Remove a slap entry", "[entry number]", "", "", "rslap_remove", "");

		weechat::hook_config("plugins.var.perl.rslap.slapback", "rslap_slapback_toggle", "");

		if (!(weechat::config_is_set_plugin ("slapback")))
		{
			weechat::config_set_plugin("slapback", "off");
		}

		open FILE, $file;
		@lines = <FILE>;
		close (FILE);

		foreach (@lines)
		{
			s/^#.*$//;
			chomp;
		}
		@lines = grep /\S/, @lines;
	}
	else
	{
		rslap_make_file();
	}
	return weechat::WEECHAT_RC_OK;
}

sub rslap_info
{
	weechat::print ("", "Number of available strings: ".weechat::color("bold").@lines.weechat::color("-bold")."\n");
	$max_align = length(@lines);
	$count = 1;
	foreach (@lines)
	{
		weechat::print ("","\t ".(" " x ($max_align - length($count))).$count.": ".$_."\n");
		$count++;
	}
	return weechat::WEECHAT_RC_OK;
}

sub rslap_add
{
	my $text = $_[2] if ($_[2]);
	if ($text)
	{
		push (@lines, $text);
		weechat::print("", "Added entry ".@lines." as: \"".$text."\"");
		rslap_update_file();
		return weechat::WEECHAT_RC_OK;
	}
	else
	{
		return weechat::WEECHAT_RC_OK;
	}
}

sub rslap_remove
{
	my $entry = $_[2] if ($_[2]);
	if ($entry =~ m/^\d+/)
	{
		$entry--;
		if ($lines[$entry])
		{
			$removed = $lines[$entry];
			$lines[$entry] = '';
			@lines = grep /\S/, @lines;
			weechat::print("", "Removed entry ".weechat::color("bold").($entry + 1).weechat::color("-bold")." (".$removed.")");
			rslap_update_file();
			return weechat::WEECHAT_RC_OK;
		}
		else
		{
			weechat::print ("", weechat::prefix("error")."Not a valid entry");
		}
	}
	else
	{
		return weechat::WEECHAT_RC_OK;
	}
}

sub rslap_slapback_toggle
{
	$point = $_[0];
	$name = $_[1];
	$value = $_[2];
	
	if ($value eq "off")
	{
		if ($rslap_slapback_hook)
		{
			weechat::unhook($rslap_slapback_hook);
			$rslap_slapback_hook = 0;
		}
	}
	elsif ($value ne "off")
	{
		if (!$rslap_slapback_hook)
		{
			$rslap_slapback_hook = weechat::hook_print("", "", "", 1, "rslap_slapback_cb", "");
		}
	}
	return weechat::WEECHAT_RC_OK;
}

sub rslap
{
	$buffer = $_[1];
	$args = $_[2];
	if (weechat::buffer_get_string($buffer, "plugin") eq "irc")
	{
		($nick, $number) = split(/ /,$args);
		if ($nick eq "")
		{
			weechat::print ("", weechat::prefix("error")."No nick given");
		}
		else
		{
			if (defined $number && $number =~ m/^\d+$/)
			{
				$number--;
				if (!$lines[$number])
				{
					weechat::print ($buffer, weechat::prefix("error")."Not a valid entry");
					return weechat::WEECHAT_RC_OK;
				}
			}
			else
			{
				$number = int(rand(@lines));
			}
			$str = $lines[$number];
			$str =~ s/\$nick/$nick/;
			$lastrun = time;
			weechat::command ($buffer, "/me ".$str);
		}
	}
	else
	{
		weechat::print ($buffer, weechat::prefix("error")."Must be used on an IRC buffer");
	}
	return weechat::WEECHAT_RC_OK;
}

sub rslap_slapback_cb
{
	$cb_datap = $_[0];
	$cb_bufferp = $_[1];
	$cb_date = $_[2];
	$cb_tags = $_[3];
	$cb_disp = $_[4];
	$cb_high = $_[5];
	$cb_prefix = $_[6];
	$cb_msg = $_[7];
	
	$bufname = weechat::buffer_get_string($cb_bufferp, 'name');
	# Only do something if a) IRC message b) is an action c) displayed and d) is a channel
	if ($cb_tags =~ /irc_privmsg/ && $cb_tags =~ /irc_action/ && $cb_disp eq "1" && $bufname =~ /.*\.[#&\+!].*/)
	{
		# Anti-recursive
		if ((time - $lastrun) < 10)
		{
			return weechat::WEECHAT_RC_OK;
		}
		# Strip colour
		$cb_msg = weechat::string_remove_color($cb_msg, "");
		# Snip sender from message
		$from_nick = substr($cb_msg, 0, index($cb_msg, " "));
		$cb_msg = substr($cb_msg, length($from_nick));
		# check for our nick and slap in message
		$cur_nick = weechat::buffer_get_string($cb_bufferp, "localvar_nick");
		if ($from_nick ne $cur_nick && $cb_msg =~ /slap/ && $cb_msg =~ /\s$cur_nick(\s|$)/)
		{
			if (weechat::config_get_plugin("slapback") =~ m/^\d+$/)
			{
				rslap("", $cb_bufferp, $from_nick." ".weechat::config_get_plugin("slapback"));
			}
			else
			{
				rslap("", $cb_bufferp, $from_nick);
			}
		}
	}
	return weechat::WEECHAT_RC_OK;
}

sub rslap_make_file
{
	weechat::print ("", "Attempting to create default file at: $file");

	open FILE, ">", $file;
 	$defs = "slaps \$nick around a bit with a large trout\n".
 		"gives \$nick a clout round the head with a fresh copy of WeeChat\n".
 		"slaps \$nick with a large smelly trout\n".
 		"breaks out the slapping rod and looks sternly at \$nick\n".
 		"slaps \$nick's bottom and grins cheekily\n".
 		"slaps \$nick a few times\n".
		"slaps \$nick and starts getting carried away\n".
		"would slap \$nick, but is not being violent today\n".
		"gives \$nick a hearty slap\n".
		"finds the closest large object and gives \$nick a slap with it\n".
		"likes slapping people and randomly picks \$nick to slap\n".
		"dusts off a kitchen towel and slaps it at \$nick";
	print FILE $defs;
	close (FILE);
	if (!(-r $file))
	{
		weechat::print ("", weechat::prefix("error")."Problem creating file: $file\n".
		weechat::prefix("error")."Make sure you can write to the location.");
		return weechat::WEECHAT_RC_ERROR;
	}
	else
	{
		weechat::print ("", "File created at: $file successfully");
		rslap_start();
		return weechat::WEECHAT_RC_OK;
	}
}

sub rslap_update_file
{
	$defs = '';
	foreach (@lines)
	{
		$defs = $defs."\n".$_;
	}
	unless(open (FILE, ">", $file))
	{
		weechat::print ("", weechat::prefix("error")."Cannot write to file: $file");
		return weechat::WEECHAT_RC_ERROR;
	}
	print FILE $defs;
	close (FILE);
	return weechat::WEECHAT_RC_OK;
}