<commit_before># DaZeus - A highly functional IRC bot
# Copyright (C) 2007  <NAME> <<EMAIL>>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package DaZeus2Module::DazKarma;
use strict;
use warnings;
no warnings 'redefine';
use base qw(DaZeus2Module);

sub init {
	my $self = shift;
	#$self->set("karmabans", {});
	#$self->set("bans", {});
}

sub seen
{
	my ($self, $mess) = @_;
	my $body = $mess->{raw_body};
	my $p = $mess->{perms};
	my $num=0;
	if(!defined($p)) {
		my $mod = $self->bot->module("DazAuth");
		if(!defined($mod)) {
			return "DazKarma cannot function without DazAuth being loaded.";
		}
		$p = $mod->getPermissions($mess)
	}
	my $w = lc($mess->{who});
	while($body =~ /\[(.+?)\](\+\+|--)/g) {
		last if($num > 4);
		my $thing = lc($1);
		# if someone lowers or increases the karma of something only
		# consisting of +'s or -'s, ignore
		next if $thing =~ /^(-|\+)*$/;

		++$num;

		my $op = $2 eq "++" ? "higher" : "lower";
		my $karma = $self->get("karma_$thing");
		$karma = 0 if(!defined($karma));
		if($thing eq lc($mess->{who}) and !$p->has("dazeus.karma.higher.self") and $op eq "higher") {
			$karma--;
			if($p->has("dazeus.karma.higher.self.message")) {
				$self->bot->reply($mess, "Bah! You're trying to increase your own karma? Bad you! Your karma has been lowered to $karma, ".$mess->{who}.".");
			}
		} else  {
			if($p->has("dazeus.karma.$op")) {
				if($op eq "higher") {
					$karma++;
					$self->bot->reply($mess, $mess->{who}." increased karma of $thing to $karma");
					print "$w increased karma of $thing to $karma.\n";
				} 
				else {
					$karma--;
					$self->bot->reply($mess, $mess->{who}." decreased karma of $thing to $karma");
					print "$w decreased karma of $thing to $karma.\n";
				}
			}
		}
		if($karma!=0) {
			$self->set("karma_$thing", $karma);
		} else {
			$self->unset("karma_$thing");
		}
	}
	while($body =~ /(?:\((.+?)\)|(\S+?))(\+\+|--)/g) {
		last if(++$num>=5);
		my $thing = lc($1) || lc($2);
		my $op = $3 eq "++" ? "higher" : "lower";
		my $karma = $self->get("karma_$thing");
		$karma = 0 if(!defined($karma));
		if($thing eq lc($mess->{who}) and !$p->has("dazeus.karma.higher.self") and $op eq "higher") {
			if($p->has("dazeus.karma.higher.self.message")) {
				$self->bot->reply($mess, "Bah! You're trying to increase your own karma? Bad you!");
			}
			$karma--;
		}
		else {
			if($p->has("dazeus.karma.$op"))
			{
				if($op eq "higher") {
					$karma++;
					print "$w increased karma of $thing to $karma.\n";
				}
				else {
					$karma--;
					print "$w decreased karma of $thing to $karma.\n";
				}
			}
		}
		if($karma!=0) {
			$self->set("karma_$thing", $karma);
		} else {
			$self->unset("karma_$thing");
		}
	}
	if($num>=5) {
		my $newkarma = $self->decrease_karma($w);
		$self->bot->reply($mess, $w."--: You karma-spamwhore!\n");
		print "I lowered karma for $w to $newkarma (karma-spamming).\n";
	}
}

sub setkarma {
	my ($self, $thing, $karma) = @_;
	if($karma!=0) {
		$self->set("karma_$thing", $karma);
	} else {
		$self->unset("karma_$thing");
	}
	return $karma;
}
sub increase_karma {
	my ($self, $thing) = @_;
	$thing=lc($thing);
	my $karma = $self->get("karma_$thing");
	$karma = 0 if(!defined($karma));
	return $self->setkarma($thing, $karma+1);
}
sub decrease_karma {
	my ($self, $thing) = @_;
	$thing=lc($thing);
	my $karma = $self->get("karma_$thing");
	$karma = 0 if(!defined($karma));
	return $self->setkarma($thing, $karma-1);
}

sub told {
	my ($self, $mess) = @_;
	my $p = $mess->{perms};
	my ($command, $rest, @rest) = $self->parseMsg($mess);
	my $who = $mess->{who};
	return if !defined $command;
	if($command eq "karma") {
		return "You don't have dazeus.commands.karma permissions."
			if(!$p->has("dazeus.commands.karma"));
		my $thing = lc($rest);
		if(!defined($thing) or !length($thing)) {
			return "Syntax: karma <forwhat> - will display karma for \$forwhat.";
		}
		my $karma = $self->get("karma_$thing");
		$karma = 0 if(!defined($karma));
		if($karma == 0) {
			return "$thing has neutral karma.";
		} else {
			return "$thing has a karma of $karma.";
		}
	} elsif($command eq "karmafight") {
		return "You don't have dazeus.commands.karma permissions."
			if(!$p->has("dazeus.commands.karma"));
		my $thing = lc($rest);
		if(!defined($thing) or !length($thing)) {
			return "Syntax: karmafight <forwhat> [<forwhat2> [...]]";
		}
		# TODO, match "quoted stuff"
		my @things = @rest;	# THAR I FIXED IT :(
		my %map = ();
		foreach $thingy (@things) {
			my $karma = $self->get("karma_$thing");
			$map{$thingy} = (defined($karma)) ? $karma : 0;
		}
		@things = sort {$map{$b} cmp $map{$a}} keys %map;
		my $out =  $things[0]." wins!!!  results:";
		my $count = 0;
		foreach $thingy (@things) {
			$count++;
			$out .= " $count. $thingy ($map{$thingy})";
		}
		return $out;
	}
}

sub parseMsg {
        my ($self, $mess) = @_;
        my $body = $mess->{body};

        if($body =~ /^}/ or $mess->{channel} eq 'msg' or $mess->{raw_body} =~ /^DazNET(?::|,|;)/i) {
                my ($command, $rest);
                if($body =~ /^}\s*$/i) {
                        return undef;
                } elsif($body =~ /^}\s*(\w+)$/) {<commit_msg>Use the right variables, no useless code, <=> instead of cmp<commit_after># DaZeus - A highly functional IRC bot
# Copyright (C) 2007  <NAME> <<EMAIL>>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package DaZeus2Module::DazKarma;
use strict;
use warnings;
no warnings 'redefine';
use base qw(DaZeus2Module);

sub init {
	my $self = shift;
	#$self->set("karmabans", {});
	#$self->set("bans", {});
}

sub seen
{
	my ($self, $mess) = @_;
	my $body = $mess->{raw_body};
	my $p = $mess->{perms};
	my $num=0;
	if(!defined($p)) {
		my $mod = $self->bot->module("DazAuth");
		if(!defined($mod)) {
			return "DazKarma cannot function without DazAuth being loaded.";
		}
		$p = $mod->getPermissions($mess)
	}
	my $w = lc($mess->{who});
	while($body =~ /\[(.+?)\](\+\+|--)/g) {
		last if($num > 4);
		my $thing = lc($1);
		# if someone lowers or increases the karma of something only
		# consisting of +'s or -'s, ignore
		next if $thing =~ /^(-|\+)*$/;

		++$num;

		my $op = $2 eq "++" ? "higher" : "lower";
		my $karma = $self->get("karma_$thing");
		$karma = 0 if(!defined($karma));
		if($thing eq lc($mess->{who}) and !$p->has("dazeus.karma.higher.self") and $op eq "higher") {
			$karma--;
			if($p->has("dazeus.karma.higher.self.message")) {
				$self->bot->reply($mess, "Bah! You're trying to increase your own karma? Bad you! Your karma has been lowered to $karma, ".$mess->{who}.".");
			}
		} else  {
			if($p->has("dazeus.karma.$op")) {
				if($op eq "higher") {
					$karma++;
					$self->bot->reply($mess, $mess->{who}." increased karma of $thing to $karma");
					print "$w increased karma of $thing to $karma.\n";
				} 
				else {
					$karma--;
					$self->bot->reply($mess, $mess->{who}." decreased karma of $thing to $karma");
					print "$w decreased karma of $thing to $karma.\n";
				}
			}
		}
		if($karma!=0) {
			$self->set("karma_$thing", $karma);
		} else {
			$self->unset("karma_$thing");
		}
	}
	while($body =~ /(?:\((.+?)\)|(\S+?))(\+\+|--)/g) {
		last if(++$num>=5);
		my $thing = lc($1) || lc($2);
		my $op = $3 eq "++" ? "higher" : "lower";
		my $karma = $self->get("karma_$thing");
		$karma = 0 if(!defined($karma));
		if($thing eq lc($mess->{who}) and !$p->has("dazeus.karma.higher.self") and $op eq "higher") {
			if($p->has("dazeus.karma.higher.self.message")) {
				$self->bot->reply($mess, "Bah! You're trying to increase your own karma? Bad you!");
			}
			$karma--;
		}
		else {
			if($p->has("dazeus.karma.$op"))
			{
				if($op eq "higher") {
					$karma++;
					print "$w increased karma of $thing to $karma.\n";
				}
				else {
					$karma--;
					print "$w decreased karma of $thing to $karma.\n";
				}
			}
		}
		if($karma!=0) {
			$self->set("karma_$thing", $karma);
		} else {
			$self->unset("karma_$thing");
		}
	}
	if($num>=5) {
		my $newkarma = $self->decrease_karma($w);
		$self->bot->reply($mess, $w."--: You karma-spamwhore!\n");
		print "I lowered karma for $w to $newkarma (karma-spamming).\n";
	}
}

sub setkarma {
	my ($self, $thing, $karma) = @_;
	if($karma!=0) {
		$self->set("karma_$thing", $karma);
	} else {
		$self->unset("karma_$thing");
	}
	return $karma;
}
sub increase_karma {
	my ($self, $thing) = @_;
	$thing=lc($thing);
	my $karma = $self->get("karma_$thing");
	$karma = 0 if(!defined($karma));
	return $self->setkarma($thing, $karma+1);
}
sub decrease_karma {
	my ($self, $thing) = @_;
	$thing=lc($thing);
	my $karma = $self->get("karma_$thing");
	$karma = 0 if(!defined($karma));
	return $self->setkarma($thing, $karma-1);
}

sub told {
	my ($self, $mess) = @_;
	my $p = $mess->{perms};
	my ($command, $rest, @rest) = $self->parseMsg($mess);
	my $who = $mess->{who};
	return if !defined $command;
	if($command eq "karma") {
		return "You don't have dazeus.commands.karma permissions."
			if(!$p->has("dazeus.commands.karma"));
		my $thing = lc($rest);
		if(!defined($thing) or !length($thing)) {
			return "Syntax: karma <forwhat> - will display karma for \$forwhat.";
		}
		my $karma = $self->get("karma_$thing");
		$karma = 0 if(!defined($karma));
		if($karma == 0) {
			return "$thing has neutral karma.";
		} else {
			return "$thing has a karma of $karma.";
		}
	} elsif($command eq "karmafight") {
		return "You don't have dazeus.commands.karma permissions."
			if(!$p->has("dazeus.commands.karma"));
		if(!$rest) {
			return "Syntax: karmafight <forwhat> [<forwhat2> [...]]";
		}
		# TODO, match "quoted stuff"
		my %map = ();
		foreach $thingy (@rest) {
			my $karma = $self->get("karma_$thing");
			$map{$thingy} = (defined($karma)) ? $karma : 0;
		}
		@rest = sort {$map{$b} <=> $map{$a}} keys %map;
		my $out =  $rest[0]." wins!!!  results:";
		my $count = 0;
		foreach $thingy (@rest) {
			$count++;
			$out .= " $count. $thingy ($map{$thingy})";
		}
		return $out;
	}
}

sub parseMsg {
        my ($self, $mess) = @_;
        my $body = $mess->{body};

        if($body =~ /^}/ or $mess->{channel} eq 'msg' or $mess->{raw_body} =~ /^DazNET(?::|,|;)/i) {
                my ($command, $rest);
                if($body =~ /^}\s*$/i) {
                        return undef;
                } elsif($body =~ /^}\s*(\w+)$/) {
{'fraction_non_alphanumeric': 0.21641919889502761, 'fraction_numerical': 0.007251381215469613, 'mean_word_length': 3.2859785423603403, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3066391', 'n_tokens_mistral': 5021, 'n_tokens_neox': 4454, 'n_words': 1160}