| 1 | package Amethyst::Brain::Infobot::Module::Karma; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw(@ISA); | ||
| 5 | use Amethyst::Message; | ||
| 6 | use Amethyst::Store; | ||
| 7 | use Amethyst::Brain::Infobot; | ||
| 8 | use Amethyst::Brain::Infobot::Module; | ||
| 9 | |||
| 10 | @ISA = qw(Amethyst::Brain::Infobot::Module); | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | my $self = $class->SUPER::new( | ||
| 15 | Name => 'Karma', | ||
| 16 | Regex => qr/(?:karma|\+\+|--)/i, | ||
| 17 | Usage => '<foo>++|<foo>--|karma <foo>', | ||
| 18 | Description => "Karma handler", | ||
| 19 | @_ | ||
| 20 | ); | ||
| 21 | |||
| 22 | $self->{Store} = new Amethyst::Store( | ||
| 23 | Source => 'karma', | ||
| 24 | ); | ||
| 25 | |||
| 26 | return bless $self, $class; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub karma_get { return $_[0]->{Store}->get($_[1]); } | ||
| 30 | |||
| 31 | # These two can warn about undef | ||
| 32 | sub karma_inc { | ||
| 33 | my ($self, $term) = @_; | ||
| 34 | my $store = $self->{Store}; | ||
| 35 | my $val = $store->get($term); | ||
| 36 | $store->set($term, $val ? ($val + 1) : 1); # Avoid warning | ||
| 37 | } | ||
| 38 | |||
| 39 | sub karma_dec { | ||
| 40 | my ($self, $term) = @_; | ||
| 41 | my $store = $self->{Store}; | ||
| 42 | $store->set($term, $store->get($term) - 1); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub action { | ||
| 46 | my ($self, $message) = @_; | ||
| 47 | |||
| 48 | my $content = $message->content; | ||
| 49 | |||
| 50 | $content =~ s/\s+/ /g; | ||
| 51 | |||
| 52 | my $re = qr/(\(.*?\)|[^(++)(--)\s]+)(\+\+|--)/; | ||
| 53 | |||
| 54 | if ($content =~ /^\s*karma (.*)/) { | ||
| 55 | my $term = lc $1; | ||
| 56 | $term =~ s/^[ (]*//g; | ||
| 57 | $term =~ s/[ )]*$//g; | ||
| 58 | |||
| 59 | my $karma = $self->karma_get($term); | ||
| 60 | |||
| 61 | my $reply = $self->reply_to($message, "$term has " . | ||
| 62 | ($karma ? "a karma of $karma" : "no karma")); | ||
| 63 | $reply->send; | ||
| 64 | |||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | my $retval = undef; | ||
| 69 | |||
| 70 | my %seen = (); | ||
| 71 | |||
| 72 | # Regex stolen from original InfoBot. | ||
| 73 | while ($content =~ s/(\(.*?\)|[^(++)(--)\s]+)(\+\+|--)//) { | ||
| 74 | my ($term, $inc) = (lc $1, $2); | ||
| 75 | |||
| 76 | $retval = 1; | ||
| 77 | |||
| 78 | $term =~ s/^[ (]*//g; | ||
| 79 | $term =~ s/[ )]*$//g; | ||
| 80 | |||
| 81 | next if $seen{$term}; | ||
| 82 | $seen{$term} = 1; | ||
| 83 | |||
| 84 | # Require public | ||
| 85 | |||
| 86 | if ($message->channel eq CHAN_PRIVATE) { | ||
| 87 | my $reply = $self->reply_to($message, "Karma must " . | ||
| 88 | "be altered in public."); | ||
| 89 | $reply->channel(CHAN_PRIVATE); | ||
| 90 | $reply->send; | ||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | elsif ($term eq lc $message->user) { | ||
| 94 | my $reply = $self->reply_to($message, "You cannot " . | ||
| 95 | "alter your own karma"); | ||
| 96 | $reply->channel(CHAN_PRIVATE); | ||
| 97 | $reply->send; | ||
| 98 | } | ||
| 99 | elsif ($inc eq '++') { | ||
| 100 | $self->karma_inc($term); | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | $self->karma_dec($term); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | return $retval; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; |