1# nocaps.pl
2#
3# Stops people SHOUTING ON IRC
4#
5# Settings:
6#    caps_replace: How to notify you something was changed. Default is
7#                  "<caps>text</caps>". 'text' is replaced with what they said.
8#    caps_sensitivity: If the line is this shorter than this, all caps is
9#                      allowed. Default = 6
10#    caps_percent: If the line has more than this percent caps in it, it's
11#                  transformed to lowercase. Default = 80.
12#
13# Thanks to Johan "Ion" Kiviniemi from #irssi for some of the stuff
14#
15# Example output (all these lines were all caps originally):
16#  [@NoTopic] Boomskdfhh�$(&* [caps]
17#  [@NoTopic] Boomfdkjh. Kdfhkdf. Kddkh. [caps]
18#  [@NoTopic] Jamesoff: Boom*�&$&*�hdfjkhjfksdfljdksjgfkj*&^�* [caps]
19#
20
21use strict;
22use vars qw($VERSION %IRSSI);
23
24use Irssi;
25
26$VERSION = '1.01';
27%IRSSI = (
28    authors	=> 'JamesOff, Ion',
29    contact	=> 'james@jamesoff.net',
30    name	=> 'nocaps',
31    description	=> 'Replaces lines in ALL CAPS with something easier on the eyes',
32    license	=> 'Public Domain',
33    url		=> 'http://www.jamesoff.net',
34    changed	=> '22 March 2002 12:34:38',
35);
36
37
38sub isAllCaps {
39	my ($msg) = @_;
40	#strip out everything that's not letters
41	$msg =~ s/[^A-Za-z]+//g;
42
43	#msgs with no letters in are a waste of time
44	return 0 if (!length($msg));
45	my $capsonly = $msg;
46
47	#only caps
48	$capsonly =~ s/[^A-Z]+//g;
49
50	#if it's all caps and less than caps_sensitivity, return 0
51	my $minimum = Irssi::settings_get_str('caps_sensitivity');
52	return 0 if ((length($capsonly) < $minimum));
53
54	#check percentage
55	my $percentage = Irssi::settings_get_str('caps_percent');
56	if (((length($capsonly) / length($msg)) * 100) > $percentage) {
57		#too many caps!
58		return 1;
59	}
60
61	return 0;
62}
63
64#main event handler
65sub caps_message {
66	my ($server, $data, $nick, $address) = @_;
67	my ($target, $msg) = split(/ :/, $data,2);
68
69	if (isAllCaps($msg)) {
70		#bleh, a line in ALL CAPS�*$&(*(�$&
71		$msg =~ tr/A-Z/a-z/;
72
73		# foo bar biz. blah quux. -> Foo bar biz. Blah quux.
74		$msg =~ s/(^\s*|[.!?]\s+)(\w)/$1 . uc $2/eg;
75
76		# Nick: hello -> Nick: Hello.
77		$msg =~ s/^(\S+:\s*)(\w)/$1 . uc $2/e;
78
79		#:<d|p|o> --> capital letter (for |Saruman| )
80		$msg =~ s/([=:;][dpo])/uc $1/eg;
81
82		my $replacement = Irssi::settings_get_str('caps_replace');
83		$replacement =~ s/text/$msg/;
84
85		#re-emit the signal to make Irssi display it
86		Irssi::signal_emit('event privmsg', ($server, "$target :$replacement", $nick, $address));
87		#and stop
88		Irssi::signal_stop();
89	}
90}
91
92Irssi::signal_add('event privmsg', 'caps_message');
93
94Irssi::settings_add_str('misc', 'caps_replace', "<caps>text</caps>");
95Irssi::settings_add_str('misc', 'caps_sensitivity', "6");
96Irssi::settings_add_str('misc', 'caps_percent', "80");
97