Skip to content

Instantly share code, notes, and snippets.

@rupa
Created October 22, 2010 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rupa/641408 to your computer and use it in GitHub Desktop.
Save rupa/641408 to your computer and use it in GitHub Desktop.
capslock enforcement bot
#!/usr/bin/perl
use POSIX;
use IO::Socket;
use utf8;
use Encode;
#fork && exit;
setsid;
$nick = "CAPSLOCKENFORCEMENTBOT";
die "need server and channel(s).\n" if !$ARGV[0] || !$ARGV[1];
($server, @channels) = @ARGV;
# open connection
$sock = new IO::Socket::INET(
PeerAddr => $server,
PeerPort => 6667,
Proto => 'tcp'
) or die "Can't connect.\n";
print $sock "PASS " . time . "\r\n";
print $sock "USER $nick $nick.cx * :$nick\r\n";
print $sock "NICK $nick\r\n";
while( $input = <$sock> ) {
die "nick is already in use.\n" if $input =~ /433/;
last if $input =~ /004/;
print $sock "PONG $1\r\n" if $input =~ /^PING(.*)$/i;
}
print "JOIN $_\r\n" foreach (@channels);
# monitor input
while( $input = <$sock> ) {
chop $input;
print $sock "PONG $1\r\n" if $input =~ /^PING(.*)$/i;
@in = split(" ", $input);
$host = shift(@in);
$type = shift(@in);
$chan = shift(@in);
@them = split("!", $host);
$theirnick = $them[0];
$theirnick =~ s/^://;
$data = join(" ", @in);
$data =~ s/^://;
$data = decode('UTF-8', $data);
if( $type eq "KICK" ) {
print $sock "JOIN $chan\r\n";
} elsif( ($type eq "PRIVMSG") && ($chan ne $nick) ) {
handler();
} elsif( ($type eq "NOTICE") && ($chan ne $nick) ) {
handler();
}
print strftime("%D %H:%M", localtime) . $input . "\n";
}
sub exceptions {
# October 22nd only
return 1 if "10/22" ne strftime("%m/%d", localtime);
# allow links
return 1 if $data =~ /https?:/;
return 1 if $data =~ /ftp:/;
}
sub handler {
return if exceptions();
if( $data =~ /\p{IsLower}/ ) {
print $sock "KICK $chan $theirnick CAPSLOCK VIOLATION!\r\n";
} elsif( $theirnick =~ /\p{IsLower}/ ) {
print $sock "KICK $chan $theirnick NICK IS NOT UPPERCASE!\r\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment