html/wordscramble.pl


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