blib/lib/POE/Component/IRC/Plugin/Karma.pm

filter: Statementstmt 32.8% 48 / 146 Branchbran 12.5% 8 / 64 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.0% 11 / 20 Podpod 0.0% 0 / 5 totaltotal 26.6% 68 / 255 CC 40 SCAR 43.0
CC
40
Cov
26%
_get_karma
240.0
S_public
56.0
_add_karma
20.0
CRAP 73.8
↑ Summary
1 #
2 # This file is part of POE-Component-IRC-Plugin-Karma
3 #
4 # This software is copyright (c) 2011 by Apocalypse.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 2 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 package POE::Component::IRC::Plugin::Karma;
11 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 $POE::Component::IRC::Plugin::Karma::VERSION = '0.003';
13 }
14 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 $POE::Component::IRC::Plugin::Karma::AUTHORITY = 'cpan:APOCAL';
16 }
17  
18 # ABSTRACT: A POE::Component::IRC plugin that keeps track of karma
19  
20 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use POE::Component::IRC::Plugin qw( PCI_EAT_NONE );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use POE::Component::IRC::Common qw( parse_user );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # TODO split the datastore stuff into pocoirc-plugin-datastore
27 # so it can be used by other plugins that need to store data
28 # then we can code plugin-seen and plugin-awaymsg stuff and have it use the datastore
29 # make it as braindead ez to use as the bot-basicbot system of saving data :)
30 # Getty wants me to have it use dbic which would make it even more awesome...
31  
32 # TODO do we need a help system? "bot: karma" should return whatever...
33  
34 # TODO do we need a botsnack thingy? bot++ bot--
35 # seen in bot-basicbot-karma where a user tries to karma the bot itself and it replies with something
36  
37 # TODO
38 # <@Hinrik> maybe you should separate the parsing from the IRC plugin
39 # <@Hinrik> so there'd be a Karma module which people could apply to any text (e.g. IRC logs)
40 # <@Hinrik> and also for people like buu who use an entirely different kind of IRC plugin
41  
42 # TODO do we need a warn_selfkarma option so it warns the user trying to karma themselves?
43  
44 # TODO
45 #<Getty> explain duckduckgo
46 #<Getty> explain karma duckduckgo
47 #<Getty> ah not implemented, ok
48  
49 # TODO
50 #<Apocalypse> Hinrik: I was wondering - in my karma stuff I use lc( $nick ) to compare it for selfkarma
51 #<Apocalypse> Should I use the l_irc thingy? What reason does it exist for? :)
52 #<@Hinrik> because according to RFC1459, "foo{" if the lowercase version of and "FOO["
53 #<Apocalypse> parse fail - what did you meant to say? foo{ is the uc equivalent of FOO[ ?
54 #<@Hinrik> l_irc("FOO[") == "foo{"
55 #<@Hinrik> not all servers use the rfc1459 casemapping though, which is why it's safest to call the function with a casemapping parameter, which you can get via $irc->isupport('CASEMAPPING');
56 #<Apocalypse> why is the irc protocol that insane? ;)
57 #<@Hinrik> RFC1459 says that this particular insanity is due to the Finnish keyboard layout, I believe
58 #<Apocalypse> haha
59 #<@Hinrik> where shift+{ gives you [ or something
60 #<Apocalypse> alright thanks for the info, I'll attack it later and see what happens :)
61  
62  
63 2 has 'addressed' => (
64 is => 'rw',
65 isa => 'Bool',
66 default => 0,
67 );
68  
69  
70 2 has 'casesens' => (
71 is => 'rw',
72 isa => 'Bool',
73 default => 0,
74 );
75  
76  
77 2 has 'privmsg' => (
78 is => 'rw',
79 isa => 'Bool',
80 default => 0,
81 );
82  
83  
84 2 has 'selfkarma' => (
85 is => 'rw',
86 isa => 'Bool',
87 default => 0,
88 );
89  
90  
91 2 has 'replykarma' => (
92 is => 'rw',
93 isa => 'Bool',
94 default => 0,
95 );
96  
97  
98 2 has 'extrastats' => (
99 is => 'rw',
100 isa => 'Bool',
101 default => 0,
102 );
103  
104  
105 2 has 'sqlite' => (
106 is => 'ro',
107 isa => 'Str',
108 default => 'karma_stats.db',
109 );
110  
111 0 ▶sub PCI_register {
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my ( $self, $irc ) = @_;
113  
114 0 $irc->plugin_register( $self, 'SERVER', qw( public msg ctcp_action ) );
115  
116 # setup the db
117 0 $self->_setup_dbi( $self->_get_dbi );
118  
119 0 return 1;
120 }
121  
122 0 ▶sub PCI_unregister {
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
123 0 my ( $self, $irc ) = @_;
124  
125 0 return 1;
126 }
127  
128 0 ▶sub S_ctcp_action {
Subroutine S_ctcp_action: not called CC=3 SCAR=24.8
Pod undocumented
129 0 my ( $self, $irc ) = splice @_, 0 , 2;
130 0 my ( $nick, $user, $host ) = parse_user( ${ $_[0] } );
131 0 my $channel = ${ $_[1] }->[0];
132 0 my $msg = ${ $_[2] };
133  
134 my $reply = $self->_karma(
135 nick => $nick,
136 user => $user,
137 host => $host,
138 where => $channel,
139 0 str => ${ $_[2] },
140 );
141  
142 0 ▶ if ( defined $reply ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reply) 0 0 0
143 0 $irc->yield( 'privmsg', $channel, $nick . ': ' . $_ ) for @$reply;
144 }
145  
146 0 return PCI_EAT_NONE;
147 }
148  
149 0 ▶sub S_public {
Subroutine S_public: not called CC=7 SCAR=40.3
Pod undocumented
150 0 my ( $self, $irc ) = splice @_, 0 , 2;
151 0 my ( $nick, $user, $host ) = parse_user( ${ $_[0] } );
152 0 my $channel = ${ $_[1] }->[0];
153 0 my $msg = ${ $_[2] };
154 0 my $string;
155  
156 # check addressed mode first
157 0 my $mynick = $irc->nick_name();
158 0 ($string) = $msg =~ m/^\s*\Q$mynick\E[\:\,\;\.]?\s*(.*)$/i;
159 0 ▶ if ( ! defined $string and ! $self->addressed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string or $self->addressed) 0 0 0
Condition0%0/3
defined $string or $self->addressed
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $string or $self->addressed
defined $string $self->addressed
Truth table0%0/3
A or B
A= defined $stringB= $self->addressed
ABresult
1X1
011
000
160 0 $string = $msg;
161 }
162  
163 0 ▶ if ( defined $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $string) 0 0 0
164 0 my $reply = $self->_karma(
165 nick => $nick,
166 user => $user,
167 host => $host,
168 where => $channel,
169 str => $string,
170 );
171  
172 0 ▶ if ( defined $reply ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reply) 0 0 0
173 0 foreach my $r ( @$reply ) {
174 0 ▶ if ( $self->privmsg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->privmsg) { } 0 0 0
175 0 $irc->yield( 'privmsg', $nick, $r );
176 } else {
177 0 $irc->yield( 'privmsg', $channel, $nick . ': ' . $r );
178 }
179 }
180 }
181 }
182  
183 0 return PCI_EAT_NONE;
184 }
185  
186 0 ▶sub S_msg {
Subroutine S_msg: not called CC=3 SCAR=24.8
Pod undocumented
187 0 my ( $self, $irc ) = splice @_, 0 , 2;
188 0 my ( $nick, $user, $host ) = parse_user( ${ $_[0] } );
189  
190 my $reply = $self->_karma(
191 nick => $nick,
192 user => $user,
193 host => $host,
194 where => 'privmsg',
195 0 str => ${ $_[2] },
196 );
197  
198 0 ▶ if ( defined $reply ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reply) 0 0 0
199 0 $irc->yield( 'privmsg', $nick, $_ ) for @$reply;
200 }
201  
202 0 return PCI_EAT_NONE;
203 }
204  
205 38 ▶sub _karma {
Subroutine _karma: called CC=8 SCAR=23.0
206 38 my( $self, %args ) = @_;
207  
208 # many different ways to get karma...
209 38 ▶ if ( $args{'str'} =~ /^\s*karma\s*(.+)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'str'} =~ /^\s*karma\s*(.+)$/i) { } 0 38 38
210 # return the karma of the requested string
211 0 return [ $self->_get_karma( $1 ) ];
212  
213 # TODO are those worth it to implement?
214 # } elsif ( $args{'str'} =~ /^\s*karmahigh\s*$/i ) {
215 # # return the list of highest karma'd words
216 # return [ $self->_get_karmahigh ];
217 # } elsif ( $args{'str'} =~ /^\s*karmalow\s*$/i ) {
218 # # return the list of lowest karma'd words
219 # return [ $self->_get_karmalow ];
220 # } elsif ( $args{'str'} =~ /^\s*karmalast\s*(.+)$/ ) {
221 # # returns the list of last karma contributors
222 # my $karma = $1;
223 #
224 # # clean the karma
225 # $karma =~ s/^\s+//;
226 # $karma =~ s/\s+$//;
227 #
228 # return [ $self->_get_karmalast( $karma ) ];
229  
230 } else {
231 # get the list of karma matches
232 # TODO still needs a bit more work, see t/parsing.t
233 38 my @matches = ( $args{'str'} =~ /(\([^\)]+\)|\S+)(\+\+|--)\s*(\#.+)?/g );
234 38 ▶ if ( @matches ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) 38 0 38
235 38 my @replies;
236 38 ▶ while ( my( $karma, $op, $comment ) = splice( @matches, 0, 3 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@matches, 0, 3)) 64 38 102
237 # clean the karma of spaces and () as we had to capture them
238 64 $karma =~ s/^[\s\(]+//;
239 64 $karma =~ s/[\s\)]+$//;
240  
241 # Is it a selfkarma?
242 64 ▶ if ( ! $self->selfkarma and lc( $karma ) eq lc( $args{'nick'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->selfkarma and lc $karma eq lc $args{'nick'}) { } 0 64 64
Condition33%1/3
not $self->selfkarma and lc $karma eq lc $args{"nick"}
!ll&&!rl&&r
0640
MC/DC0%0/2
not $self->selfkarma and lc $karma eq lc $args{"nick"}
not $self->selfkarma lc $karma eq lc $args{"nick"}
Truth table33%1/3
not(A) and B
A= not $self->selfkarmaB= lc $karma eq lc $args{"nick"}
ABresult
0X0
100
111
243 0 next;
244 } else {
245 # clean the comment
246 64 ▶ $comment =~ s/^\s*\#\s*// if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 18 46 64
247  
248 64 $self->_add_karma(
249 karma => $karma,
250 op => $op,
251 comment => $comment,
252 %args,
253 );
254  
255 64 ▶ if ( $self->replykarma ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->replykarma) 0 64 64
256 0 push( @replies, $self->_get_karma( $karma ) );
257 }
258 }
259 }
260  
261 38 return \@replies;
262 }
263 }
264  
265 0 return;
266 }
267  
268 0 ▶sub _get_karma {
Subroutine _get_karma: not called CC=15 SCAR=54.8
269 0 my( $self, $karma ) = @_;
270  
271 # case-sensitive search or not?
272 0 my $sql = 'SELECT mode, count(mode) AS count FROM karma WHERE karma = ?';
273 0 ▶ if ( ! $self->casesens ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->casesens) 0 0 0
274 0 $sql .= ' COLLATE NOCASE';
275 }
276 0 $sql .= ' GROUP BY mode';
277  
278 # Get the score from the DB
279 0 my $dbh = $self->_get_dbi;
280 0 ▶ my $sth = $dbh->prepare_cached( $sql ) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare_cached($sql) or die $dbh->errstr 0 0 0
281 0 ▶ $sth->execute( $karma ) or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($karma) or die $sth->errstr 0 0 0
282 0 my( $up, $down ) = ( 0, 0 );
283 0 ▶ while ( my $row = $sth->fetchrow_arrayref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 0 0 0
284 0 ▶ if ( $row->[0] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->[0] == 1) { } 0 0 0
285 0 $up = $row->[1];
286 } else {
287 0 $down = $row->[1];
288 }
289 }
290 0 $sth->finish;
291  
292 0 my $score = $up - $down;
293 0 ▶ $score = undef if ( $up == 0 and $down == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $up == 0 and $down == 0 0 0 0
Condition0%0/3
$up == 0 and $down == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$up == 0 and $down == 0
$up == 0 $down == 0
Truth table0%0/3
A and B
A= $up == 0B= $down == 0
ABresult
0X0
100
111
294  
295 0 my $result;
296 0 ▶ if ( ! defined $score ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $score) { } 0 0 0
297 0 $result = "'$karma' has no karma";
298 } else {
299 0 ▶ if ( $score == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score == 0) { } 0 0 0
300 0 $result = "'$karma' has neutral karma";
301 0 ▶ if ( $self->extrastats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extrastats) 0 0 0
302 0 my $total = $up + $down;
303 0 $result .= " [ $total votes ]";
304 }
305 } else {
306 0 $result = "'$karma' has karma of $score";
307 0 ▶ if ( $self->extrastats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extrastats) 0 0 0
308 0 ▶ if ( $up and $down ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($up and $down) { } 0 0 0
elsif ($up) { } 0 0 0
Condition0%0/3
$up and $down
!ll&&!rl&&r
000
MC/DC0%0/2
$up and $down
$up $down
Truth table0%0/3
A and B
A= $upB= $down
ABresult
0X0
100
111
309 0 $result .= " [ $up ++ and $down -- votes ]";
310 } elsif ( $up ) {
311 0 $result .= " [ $up ++ votes ]";
312 } else {
313 0 $result .= " [ $down -- votes ]";
314 }
315 }
316 }
317 }
318  
319 0 return $result;
320 }
321  
322 0 ▶sub _add_karma {
Subroutine _add_karma: not called CC=4 SCAR=30.0
323 0 my( $self, %args ) = @_;
324  
325 # munge the nick back into original format
326 0 $args{'who'} = $args{'nick'} . '!' . $args{'user'} . '@' . $args{'host'};
327  
328 # insert it into the DB!
329 0 my $dbh = $self->_get_dbi;
330 0 ▶ my $sth = $dbh->prepare_cached( 'INSERT INTO karma ( who, "where", timestamp, karma, mode, comment, said ) VALUES ( ?, ?, ?, ?, ?, ?, ? )' ) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare_cached("INSERT INTO karma ( who, \"where\", timestamp, karma, mode, comment, said ) VALUES ( ?, ?, ?, ?, ?, ?, ? )") or die $dbh->errstr 0 0 0
331 $sth->execute(
332 $args{'who'},
333 $args{'where'},
334 scalar time,
335 $args{'karma'},
336 ( $args{'op'} eq '++' ? 1 : 0 ),
337 $args{'comment'},
338 0 ▶ $args{'str'},
Branch: 2 branches
BranchTrueFalseTotal
$sth->execute($args{"who"}, $args{"where"}, scalar time, $args{"karma"}, $args{"op"} eq "++" ? 1 : 0, $args{"comment"}, $args{"str"}) or die $sth->errstr 0 0 0
$args{'op'} eq '++' ? : 0 0 0
339 ) or die $sth->errstr;
340 0 $sth->finish;
341  
342 0 return;
343 }
344  
345 0 ▶sub _get_dbi {
Subroutine _get_dbi: not called CC=3 SCAR=24.8
346 0 my $self = shift;
347  
348 0 my $dbh = DBI->connect_cached( "dbi:SQLite:dbname=" . $self->sqlite, '', '' );
349  
350 # set some SQLite tweaks
351 0 ▶ $dbh->do( 'PRAGMA synchronous = OFF' ) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("PRAGMA synchronous = OFF") or die $dbh->errstr 0 0 0
352 0 ▶ $dbh->do( 'PRAGMA locking_mode = EXCLUSIVE' ) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("PRAGMA locking_mode = EXCLUSIVE") or die $dbh->errstr 0 0 0
353  
354 0 return $dbh;
355 }
356  
357 0 ▶sub _setup_dbi {
Subroutine _setup_dbi: not called CC=4 SCAR=30.0
358 0 my( $self, $dbh ) = @_;
359  
360 # create the table itself
361 0 ▶ $dbh->do( 'CREATE TABLE IF NOT EXISTS karma ( ' .
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("CREATE TABLE IF NOT EXISTS karma ( who TEXT NOT NULL, \"where\" TEXT NOT NULL, timestamp INTEGER NOT NULL, karma TEXT NOT NULL, mode BOOL NOT NULL, comment TEXT, said TEXT NOT NULL )") or die $dbh->errstr 0 0 0
362 'who TEXT NOT NULL, ' . # who made the karma
363 '"where" TEXT NOT NULL, ' . # privmsg or in chan
364 'timestamp INTEGER NOT NULL, ' . # unix timestamp of karma
365 'karma TEXT NOT NULL, ' . # the stuff being karma'd
366 'mode BOOL NOT NULL, ' . # 1 if it was a ++, 0 if it was a --
367 'comment TEXT, ' . # the comment given with the karma ( optional )
368 'said TEXT NOT NULL ' . # the full text the user said
369 ')' ) or die $dbh->errstr;
370  
371 # create the indexes to speed up searching
372 0 ▶ $dbh->do( 'CREATE INDEX IF NOT EXISTS karma_karma ON karma ( karma )' ) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("CREATE INDEX IF NOT EXISTS karma_karma ON karma ( karma )") or die $dbh->errstr 0 0 0
373 0 ▶ $dbh->do( 'CREATE INDEX IF NOT EXISTS karma_mode ON karma ( mode )' ) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("CREATE INDEX IF NOT EXISTS karma_mode ON karma ( mode )") or die $dbh->errstr 0 0 0
374  
375 0 return;
376 }
377  
378 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
379 2 __PACKAGE__->meta->make_immutable;
380 2 1;
381  
382  
383 __END__
↑ Summary