This file is indexed.

/usr/share/perl5/Bot/BasicBot/Pluggable/Module/Karma.pm is in libbot-basicbot-pluggable-perl 1.20-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package Bot::BasicBot::Pluggable::Module::Karma;
$Bot::BasicBot::Pluggable::Module::Karma::VERSION = '1.20';
use base qw(Bot::BasicBot::Pluggable::Module);
use warnings;
use strict;

sub init {
    my $self = shift;
    $self->config(
        {
            user_ignore_selfkarma  => 1,
            user_num_comments      => 3,
            user_show_givers       => 1,
            user_randomize_reasons => 1,
	    user_karma_change_response => 1,
        }
    );
}

sub help {
    return
"Gives karma for or against a particular thing. Usage: <thing>++ # comment, <thing>-- # comment, karma <thing>, explain <thing>.";
}

sub told {
    my ( $self, $mess ) = @_;
    my $body = $mess->{body};
    return 0 unless defined $body;

    # If someone is trying to change the bot's karma, we'll have our bot nick in
    # {addressed}, and '++' or '-' in the body ('-' rather than '--' because
    # Bot::BasicBot removes one of the dashes as it considers it part of the
    # address)
    if ( $mess->{address} && ($body eq '++' or $body eq '-') ) {
        $body = '--' if $body eq '-';
        $body = $mess->{address} . $body;
    }

    my $op_re      = qr{ ( \-\- | \+\+ )        }x;
    my $comment_re = qr{ (?: \s* \# \s* (.+) )? }x;
    for my $regex (
        qr{^   (\S+)     $op_re $comment_re  }x, # singleword++
        qr{^ \( (.+)  \) $op_re $comment_re  }x  # (more words)++
    ) {
        if (my($thing, $op, $comment) = $body =~ $regex) {
            my $add = $op eq '++' ? 1 : 0;
            if ( 
                ( $1 eq $mess->{who} ) and $self->get("user_ignore_selfkarma") 
            ){
                return;
            }
            my $reply = $self->add_karma( $thing, $add, $comment, $mess->{who} );
            if (lc $thing eq lc $self->bot->nick) {
                $reply .= ' ' . ($add ? '(thanks!)' : '(pffft)');
            }
            return $reply;
        }
    }

    # OK, handle "karma" / "explain" commands
    my ( $command, $param ) = split( /\s+/, $body, 2 );
    $command = lc($command);

    if ( $command eq "karma" ) {
		$param =~ s/\?+$//; # handle interrogatives - lop off trailing question marks
        if ($param && $param eq 'chameleon') {
            return "Karma karma karma karma karma chameleon, "
                . "you come and go, you come and go...";
        }
        $param ||= $mess->{who};
        return "$param has karma of " . $self->get_karma($param) . ".";
    
    } elsif ( $command eq "explain" and $param ) {
        $param =~ s/^karma\s+//i;
        my ( $karma, $good, $bad ) = $self->get_karma($param);
        my $reply = "positive: " . $self->format_reasons($good) . "; ";
        $reply .= "negative: " . $self->format_reasons($bad) . "; ";
        $reply .= "overall: $karma.";

        return $reply;
    }
}


sub format_reasons {
    my ( $self, $reason ) = @_;
    my $num_comments = $self->get('user_num_comments');

    if ( $num_comments == 0 ) {
        return scalar( $reason->() );
    }

    my @reasons     = $reason->();
    my $num_reasons = @reasons;

    if ( $num_reasons == 0 ) {
        return 'nothing';
    }

    if ( $num_reasons == 1 ) {
        return ( $self->maybe_add_giver(@reasons) )[0];
    }

    $self->trim_list( \@reasons, $num_comments );
    return join( ', ', $self->maybe_add_giver(@reasons) );
}

sub maybe_add_giver {
    my ( $self, @reasons ) = @_;
    if ( $self->get('user_show_givers') ) {

        # adding a (user) string to the all reasons
        return map { $_->{reason} . ' (' . $_->{who} . ')' } @reasons;
    }
    else {

        # just returning the reason string of the reason hash referenes
        return map { $_->{reason} } @reasons;
    }
}

sub get_karma {
    my ( $self, $thing ) = @_;
    $thing = lc($thing);
    $thing =~ s/-/ /g;

    my @changes = grep { ref } @{ $self->get("karma_$thing") || [] };

    my ( @good, @bad );
    my $karma    = 0;
    my $positive = 0;
    my $negative = 0;

    for my $row (@changes) {

        # just push non empty reasons on the array
        my $reason = $row->{reason};
        if ( $row->{positive} ) { $positive++; push( @good, +{ %$row } ) if $reason }
        else                    { $negative++; push( @bad, +{ %$row } ) if $reason }
    }
    $karma = $positive - $negative;

    # The subroutine references return differant values when called.
    # If they are called in scalar context, they return the overall
    # positive or negative karma, but when called in list context you
    # get an array of hash references with all non empty reasons back.

    return wantarray()
      ? (
        $karma,
        sub { return wantarray ? @good : $positive },
        sub { return wantarray ? @bad  : $negative }
      )
      : $karma;
}

sub add_karma {
    my ( $self, $thing, $good, $reason, $who ) = @_;
    $thing = lc($thing);
    $thing =~ s/-/ /g;
    my $row =
      { reason => $reason, who => $who, timestamp => time, positive => $good };
    my @changes = map { +{ %$_ } } grep { ref } @{ $self->get("karma_$thing") || [] };
    push @changes, $row;
    $self->set( "karma_$thing" => \@changes );
    my $respond = $self->get('user_karma_change_response');
    $respond = 1 if !defined $respond;
    return $respond ?
        "Karma for $thing is now " . scalar $self->get_karma($thing) : 1;
}

sub trim_list {
    my ( $self, $list, $count ) = @_;

    # If randomization isn't requested we just return the reasons
    # in reversed chronological order

    if ( $self->get('user_randomize_reasons') ) {
        fisher_yates_shuffle($list);
    }
    else {
        @$list = reverse sort { $b->{timestamp} cmp $a->{timestamp} } @$list;
    }

    if ( scalar(@$list) > $count ) {
        @$list = splice( @$list, 0, $count );
    }
}

sub fisher_yates_shuffle {
    my $array = shift;
    my $i     = @$array;
    while ( $i-- ) {
        my $j = int rand( $i + 1 );
        @$array[ $i, $j ] = @$array[ $j, $i ];
    }
}

1;

__END__

=head1 NAME

Bot::BasicBot::Pluggable::Module::Karma - tracks karma for various concepts

=head1 VERSION

version 1.20

=head1 IRC USAGE

=over 4

=item <thing>++ # <comment>

Increases the karma for <thing>.

Responds with the new karma for <thing> unless C<karma_change_response> is set 
to a false value.

=item <thing>-- # <comment>

Decreases the karma for <thing>.

Responds with the new karma for <thing> unless C<karma_change_response> is set 
to a false value.

=item karma <thing>

Replies with the karma rating for <thing>.

=item explain <thing>

Lists three each good and bad things said about <thing>:

  <user> explain Morbus
  <bot> positive: committing lots of bot documentation; fixing the
        fisher_yates; negative: filling the dev list. overall: 5

=back

=head1 METHODS

=over 4

=item get_karma($username)

Returns either a string representing the total number of karma
points for the passed C<$username> or the total number of karma
points and subroutine reference for good and bad karma comments.
These references return the according karma levels when called in
scalar context or a array of hash reference. Every hash reference
has entries for the timestamp (timestamp), the giver (who) and the
explanation string (reason) for its karma action.

=item add_karma($thing, $good, $reason, $who)

Adds or subtracts from the passed C<$thing>'s karma. C<$good> is either 1 (to
add a karma point to the C<$thing> or 0 (to subtract). C<$reason> is an 
optional string commenting on the reason for the change, and C<$who> is the
person modifying the karma of C<$thing>. Nothing is returned.

=back

=head1 VARS

=over 4

=item ignore_selfkarma

Defaults to 1; determines whether to respect selfkarmaing or not.

=item num_comments

Defaults to 3; number of good and bad comments to display on
explanations. Set this variable to 0 if you do not want to list
reasons at all.

=item show_givers

Defaults to 1; whether to show who gave good or bad comments on
explanations.

=item randomize_reasons

Defaults to 1; whether to randomize the order of reasons. If set
to 0, the reasons are sorted in reversed chronological order.

=item karma_change_response

Defaults to 1; whether to show a response when the karma of a
thing is changed.  If true, the bot will reply with the new karma.
If set to 0, the bot will silently update the karma, without
a response.

=back

=head1 AUTHOR

Mario Domgoergen <mdom@cpan.org>

This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.