1use Irssi;
2use strict;
3use vars qw($VERSION %IRSSI);
4
5$VERSION = '0.0.3';
6%IRSSI = (
7	authors     => 'Koenraad Heijlen',
8	contact     => 'vipie@ulyssis.org',
9	commands    => 'ws',
10	name        => 'word_scramble',
11	description => 'A script that scrambles all the letters in a word except the first and last.',
12	license     => 'GNU GPL version 2',
13	url         => 'http://vipie.studentenweb.org/dev/irssi/wordscramble',
14	changed     => '2018-05-11'
15);
16
17#--------------------------------------------------------------------
18# Changelog
19#--------------------------------------------------------------------
20#
21# word_scramble.pl 0.0.3 (2018-05-11)- bw1
22#	- fixed the help bug
23#
24# word_scramble.pl 0.0.2 (2003-09-17)- Koenraad Heijlen
25# 	- fixed the four letter word bug
26# 	- fixed the non alphanummeric characters bug
27# 	- some improvement in returning \n
28#
29# word_scramble.pl 0.0.1 (2003-09-15) - Koenraad Heijlen
30# 	- first draft
31#
32#--------------------------------------------------------------------
33
34#--------------------------------------------------------------------
35# Public Variables
36#--------------------------------------------------------------------
37my %myHELP = ();
38
39
40#--------------------------------------------------------------------
41# Help function
42#--------------------------------------------------------------------
43sub cmd_help {
44	my ($about) = @_;
45
46	%myHELP = (
47		ws =>
48"%9ws - wordscramble%9
49
50  /ws <text>
51
52scrambles the text you type,
53and outputs it in the current (active) channel
54or query.
55",
56	);
57
58	if ( $about =~ /(ws)/i ) {
59		Irssi::print($myHELP{ws},MSGLEVEL_CLIENTCRAP);
60		Irssi::signal_stop;
61	}
62}
63
64#--------------------------------------------------------------------
65# scrambles one word
66#--------------------------------------------------------------------
67sub scrambleWord {
68	# 0 : first
69	# length : last-1
70	# length+1 : last
71	#substr EXPR,OFFSET,LENGTH,REPLACEMENT
72	my $l = 0;
73	my $r = 0;
74	my $out = "";
75	my $word = shift;
76	chomp($word);
77
78	if (length($word) <= 3) {
79		return $word;
80	}
81	my $l = length($word)-2;
82	$l = $l;
83	$out = substr($word,0,1);
84	while ($l != 1) {
85		$r = int(rand()*$l+1);
86
87		if ($r == 0) {
88			next;
89		}
90		#$r == $l is no marginalcase.
91
92		$out .= substr($word,$r,1);
93		substr($word,$r,1,substr($word,$l,1));
94		$l--;
95	}
96	$out .= substr($word,$l,1);
97	$out .= substr($word,length($word)-1,1);
98	return $out;
99}
100
101#--------------------------------------------------------------------
102# scrambles line
103#--------------------------------------------------------------------
104sub scrambleLine{
105	my $line = shift;
106	my $outline = "";
107	my $word = "";
108	my $i=0;
109	my @splitLine;
110
111	#we leave the \n at the end, less interference.
112	#chomp($line);
113	@splitLine=split(/(\W)/,$line);
114
115	# every other item in the array is the split string
116	for ($i=0; $i<= $#splitLine;$i++) {
117		$outline .= scrambleWord($splitLine[$i]);
118		$i++;
119		if ($i <= $#splitLine) {
120			$outline .= $splitLine[$i];
121		}
122	}
123	return $outline;
124}
125
126#--------------------------------------------------------------------
127# Defintion of /ws
128#--------------------------------------------------------------------
129sub cmd_ws {
130	my ($args, $server, $witem) = @_;
131
132	if (!$server || !$server->{connected}) {
133		Irssi::print("Not connected to server");
134		return;
135	}
136
137	my $scrambledLine = scrambleLine($args);
138	if ($witem && ($witem->{type} eq "CHANNEL" ||
139			$witem->{type} eq "QUERY")) {
140		# there's query/channel active in window
141		$witem->command("MSG ".$witem->{name}." $scrambledLine");
142	} else {
143		Irssi::print("Nick not given, and no active channel/query in window");
144	}
145}
146
147#--------------------------------------------------------------------
148# Irssi::Settings / Irssi::command_bind
149#--------------------------------------------------------------------
150
151Irssi::command_bind("ws", "cmd_ws", "Scramble Line");
152Irssi::command_bind("help","cmd_help", "Irssi commands");
153
154#--------------------------------------------------------------------
155# This text is printed at Load time.
156#--------------------------------------------------------------------
157
158#nothing
159
160#- end
161