summaryrefslogtreecommitdiffstats
path: root/scripts/wordscramble.pl
blob: a05cab3cdf6d4185f733607a8289c3738c8e8d21 (plain)
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
use Irssi;
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = '0.0.3';
%IRSSI = (
	authors     => 'Koenraad Heijlen',
	contact     => 'vipie@ulyssis.org',
	commands    => 'ws',
	name        => 'word_scramble',
	description => 'A script that scrambles all the letters in a word except the first and last.', 
	license     => 'GNU GPL version 2',
	url         => 'http://vipie.studentenweb.org/dev/irssi/wordscramble',
	changed     => '2018-05-11'
);

#--------------------------------------------------------------------
# Changelog
#--------------------------------------------------------------------
#
# word_scramble.pl 0.0.3 (2018-05-11)- bw1
#	- fixed the help bug
#
# word_scramble.pl 0.0.2 (2003-09-17)- Koenraad Heijlen
# 	- fixed the four letter word bug
# 	- fixed the non alphanummeric characters bug
# 	- some improvement in returning \n
#
# word_scramble.pl 0.0.1 (2003-09-15) - Koenraad Heijlen
# 	- first draft
# 
#--------------------------------------------------------------------

#--------------------------------------------------------------------
# Public Variables
#--------------------------------------------------------------------
my %myHELP = ();


#--------------------------------------------------------------------
# Help function
#--------------------------------------------------------------------
sub cmd_help { 
	my ($about) = @_;

	%myHELP = (
		ws =>
"%9ws - wordscramble%9

  /ws <text>

scrambles the text you type,
and outputs it in the current (active) channel
or query.
",
	);

	if ( $about =~ /(ws)/i ) {
		Irssi::print($myHELP{ws},MSGLEVEL_CLIENTCRAP);
		Irssi::signal_stop;
	}
}

#--------------------------------------------------------------------
# scrambles one word
#--------------------------------------------------------------------
sub scrambleWord {
	# 0 : first
	# length : last-1
	# length+1 : last
	#substr EXPR,OFFSET,LENGTH,REPLACEMENT
	my $l = 0;
	my $r = 0; 
	my $out = "";
	my $word = shift;
	chomp($word);

	if (length($word) <= 3) {
		return $word;
	}
	my $l = length($word)-2;
	$l = $l;
	$out = substr($word,0,1);
	while ($l != 1) {
		$r = int(rand()*$l+1);

		if ($r == 0) {
			next;
		}
		#$r == $l is no marginalcase.

		$out .= substr($word,$r,1);
		substr($word,$r,1,substr($word,$l,1));
		$l--;
	}
	$out .= substr($word,$l,1);
	$out .= substr($word,length($word)-1,1);
	return $out;
}

#--------------------------------------------------------------------
# scrambles line
#--------------------------------------------------------------------
sub scrambleLine{
	my $line = shift;
	my $outline = "";
	my $word = "";
	my $i=0;
	my @splitLine;
	
	#we leave the \n at the end, less interference.
	#chomp($line);
	@splitLine=split(/(\W)/,$line);
	
	# every other item in the array is the split string
	for ($i=0; $i<= $#splitLine;$i++) {
		$outline .= scrambleWord($splitLine[$i]);
		$i++;
		if ($i <= $#splitLine) {
			$outline .= $splitLine[$i]; 
		}
	}
	return $outline;
}

#--------------------------------------------------------------------
# Defintion of /ws
#--------------------------------------------------------------------
sub cmd_ws {
	my ($args, $server, $witem) = @_;

	if (!$server || !$server->{connected}) {
		Irssi::print("Not connected to server");
		return;
	}

	my $scrambledLine = scrambleLine($args);
	if ($witem && ($witem->{type} eq "CHANNEL" ||
			$witem->{type} eq "QUERY")) {
		# there's query/channel active in window
		$witem->command("MSG ".$witem->{name}." $scrambledLine");
	} else {
		Irssi::print("Nick not given, and no active channel/query in window");
	}
}

#--------------------------------------------------------------------
# Irssi::Settings / Irssi::command_bind
#--------------------------------------------------------------------

Irssi::command_bind("ws", "cmd_ws", "Scramble Line");
Irssi::command_bind("help","cmd_help", "Irssi commands");

#--------------------------------------------------------------------
# This text is printed at Load time.
#--------------------------------------------------------------------

#nothing

#- end