Skip to content

Instantly share code, notes, and snippets.

@Thyrst
Created March 21, 2014 20:02
Show Gist options
  • Save Thyrst/9695115 to your computer and use it in GitHub Desktop.
Save Thyrst/9695115 to your computer and use it in GitHub Desktop.
BuzerBot
#!/usr/bin/perl
use IO::Socket;
use IO::Socket::SSL;
use strict;
$|++;
our $server = shift() || die "Missing arguments!\n";
our $port = shift() || die "Missing port!\n";
our $channels = shift() || die "Missing channels! (without \#)\n";
our $nick ="BuzerBot";
our $ssl = chop($port) if $port =~ /s$/i;
our $s;
END { &quit; }
$SIG{INT} = \&quit;
$SIG{CHLD} = \&quit;
$SIG{TERM} = \&quit;
$SIG{ALRM} = \&quit;
$SIG{HUP} = 'IGNORE';
sub quit {
if ($s) {
print $s "QUIT\r\n";
$s->shutdown(0);
close($s);
}
exit 0;
}
if (fork()) {
buzer() while sleep(86400); # 24 hours (i hope so)
}
con();
session();
sub con {
$s = new IO::Socket::INET(
Proto => 'tcp',
PeerPort => $port,
PeerAddr => $server
) or die "Can't connect to $server!\n";
if ($ssl eq 's') {
IO::Socket::SSL->start_SSL(
$s,
SSL_verify_mode => 0,
) or die "SSL handshake failed\n";
}
select($s);
print "NICK ".$nick."\r\n";
print "USER ".$nick." 8 * : Johny Derp\r\n";
while (<$s>) {
last if $_ =~ /004/;
if ($_ =~ /433/) {
$nick = $nick."_";
print "NICK $nick\r\n";
}
if ($_ =~ /^PING(.*)$/i) {
print "PONG$1";
}
}
my @channels = split("\x2C", $channels);
print "JOIN #$_\r\n" foreach @channels;
}
sub session {
$/ = "\x0D\x0A";
while (my $input = <$s>) {
if ($input =~ /^PING(.*)$/i) {
print "PONG".$1;
next;
}
chomp($input);
if ($input =~ m/^:((?:.*?)!(?:.*?)@(?:.*?)) PRIVMSG (?:(?:\#.*?)|$nick) :(.*)$/i) { # maybe fix this
next unless $2;
my $user = $1;
my $mess = $2;
CASE:{
$mess =~ m/^(?:$nick: )?todo\s+<<\s+/i and do{write2db($user, $'); last CASE;};
$mess =~ m/^(?:$nick: )?\/(.*?)\/\s+done\.$/i and do{rmwork($user, $1) if $1; last CASE;}; # todo << fix this
$mess =~ m/^(?:$nick: )?todo\s+\/(.*?)\/\s+>>$/i and do{echotodo($user, $1) if $1; last CASE;}; # todo << fix this
$mess =~ m/^(?:$nick: )?todo\s+>>$/i and do{echotodo($user); last CASE;};
$mess =~ m/^(?:$nick: )?what\s+to\s*do\?*$/i and do{echotodo($user); last CASE;}; # todo << fix this (fuck regex)
# you d'not gota understand for mine code or for mine english
}
print "PING :".$server."\r\n";
next;
}
if ($input =~ /^:(.*?)!(?:.*?)@(?:.*?) KICK (\#.*?) :(.*)$/i) {
print "JOIN #$2\r\n";
next;
}
if ($input =~ m/^:$nick!(?:.*?)@(?:.*?) QUIT :Ping timeout\x3A(.*)$/i || $input =~ m/^ERROR :Closing Link: $nick\x5B(.*?)\x5D \(Ping timeout\)$/i) {
con();
next;
}
}
}
sub write2db {
my ($user, $data) = @_;
return unless $data =~ /\w+/;
open(DB, ">>todos.dat") or die $!;
print DB $user." :: ".$data."\r\n";
close(DB);
}
sub getdata {
my ($user) = @_;
my @todo;
open(DB, "<todos.dat") or die $!;
my $file = <DB>;
my @lines = split(/\n/, $file);
foreach (@lines) {
my @data = split(/ :: /, $_);
if ($data[0] eq $user) {
push(@todo, ($data[1]));
print STDERR $data[1];
}
}
close(DB);
return @todo;
}
sub echotodo {
my ($user, $pattern) = @_;
my $filtr;
return unless $user =~ /\x21/;
$nick = $`;
$pattern =~ /\w+/ ? $filtr = 1 : $filtr = 0;
my @data = getdata($user);
foreach (@data) {
if ($filtr) {
if ($_ =~ m/$pattern/i) {
print "PRIVMSG ".$nick." :".$_."\r\n";
}
} else {
print "PRIVMSG ".$nick." :".$_."\r\n";
}
sleep(1);
}
}
sub rmwork {
my ($user, $work) = @_;
open(DB, "<todos.dat") or die $!;
my @lines = <DB>;
close(DB);
open(DB, ">todos.dat") or die $!;
foreach (@lines) {
unless ($_ =~ m/^$user :: / && $' =~ m/$work/i) {
print DB $_;
}
}
close(DB);
}
sub buzer {
open(DB, "<todos.dat") or die $!;
my @users;
my $condition;
foreach(<DB>) {
$condition = 1;
my @tmp = split(/ :: /, $_);
foreach (@users) {
if ($_ eq $tmp[0]) {
$condition = 0;
last;
}
}
push(@users, $tmp[0]) if $condition;
}
foreach(@users) {
return unless $_ =~ /\x21/;
$nick = $`;
print "PRIVMSG ".$nick." :Do something you motherfucker!\r\n";
echotodo($_);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment