<commit_before>package RoboBot::Plugin::Logger;

use strict;
use warnings;

sub commands { qw( * last ) }
sub usage { "[nick[,nick,...]] [offset[,count]]" }

sub handle_message {
    my ($class, $bot, $sender, $channel, $command, $original, $timestamp, $message) = @_;

    if ($command eq 'last') {
        # collapse whitespace around commas, so it's easier for people to list multiple nicks
        $message =~ s{\s*,\s*}{,}ogs;

        my @opt = split(/\s+/, $message);

        my ($nick, $num);

        if (scalar(@opt) == 2 && $opt[1] =~ m{^\s*\d+(,\d+)?\s*$}o) {
            ($nick, $num) = @opt;
        } elsif (scalar(@opt) == 1 && $opt[0] =~ m{^\s*\d+(,\d+)?\s*$}o) {
            $num = $opt[0];
        } elsif (scalar(@opt) == 1) {
            $nick = $opt[0];
            $num = 1;
        }

        return last_message($bot, $channel, $nick, $num);
    } elsif (length($command) == 0) {
        log_message($bot, $sender, $channel, $message);
    }

    return -1;
}

sub last_message {
    my ($bot, $channel, $nick, $num) = @_;

    my @nicks;

    if (defined $nick && $nick =~ m{\w+}o) {
        @nicks = map { $_ =~ s{(^\s+|\s+$)}{}ogs; lc($_) } split(/,/, $nick);
    }

    my $limit = 1;
    my $offset = 0;

    if (defined $num && $num =~ m{^\s*(\d+)\s*$}o) {
        $offset = $1 - 1;
    } elsif (defined $num && $num =~ m{^\s*(\d+),(\d+)\s*$}o) {
        $offset = $1 - 1;
        $limit = $2;
    }

    # for now, force a limit of 5 messages (for sanity)
    $limit = 5 if $limit > 5;

    # because the offset is moving backwards, we need to make sure the limit doesn't
    # exceed it (as the limit then moves the end point back toward the most recent entry)
    $limit = ($offset + 1) if $limit >= $offset;

    # then we move the offset to mark the end point (closest to most recent) since we'll
    # be grabbing these in reverse order initially
    $offset = ($offset + 1) - $limit;

    my $res;

    if (scalar(@nicks) > 0) {
        $res = $bot->db->do(q{
            select d.*
            from ( select n.nick, l.message, l.posted_at
                   from nicks n
                       join logger_log l on (l.nick_id = n.id)
                       join channels c on (c.id = l.channel_id)
                   where lower(n.nick) in ??? and lower(c.name) = lower(?)
                   order by l.posted_at desc
                   limit ? offset ?
                 ) d<commit_msg>fixed bug in last command from Logger plugin where an offset of 2 would result in getting back the second most and most recent entries, not just the second-most<commit_after>package RoboBot::Plugin::Logger;

use strict;
use warnings;

sub commands { qw( * last ) }
sub usage { "[nick[,nick,...]] [offset[,count]]" }

sub handle_message {
    my ($class, $bot, $sender, $channel, $command, $original, $timestamp, $message) = @_;

    if ($command eq 'last') {
        # collapse whitespace around commas, so it's easier for people to list multiple nicks
        $message =~ s{\s*,\s*}{,}ogs;

        my @opt = split(/\s+/, $message);

        my ($nick, $num);

        if (scalar(@opt) == 2 && $opt[1] =~ m{^\s*\d+(,\d+)?\s*$}o) {
            ($nick, $num) = @opt;
        } elsif (scalar(@opt) == 1 && $opt[0] =~ m{^\s*\d+(,\d+)?\s*$}o) {
            $num = $opt[0];
        } elsif (scalar(@opt) == 1) {
            $nick = $opt[0];
            $num = 1;
        }

        return last_message($bot, $channel, $nick, $num);
    } elsif (length($command) == 0) {
        log_message($bot, $sender, $channel, $message);
    }

    return -1;
}

sub last_message {
    my ($bot, $channel, $nick, $num) = @_;

    my @nicks;

    if (defined $nick && $nick =~ m{\w+}o) {
        @nicks = map { $_ =~ s{(^\s+|\s+$)}{}ogs; lc($_) } split(/,/, $nick);
    }

    my $limit = 1;
    my $offset = 0;

    if (defined $num && $num =~ m{^\s*(\d+)\s*$}o) {
        $offset = $1 - 1;
    } elsif (defined $num && $num =~ m{^\s*(\d+),(\d+)\s*$}o) {
        $offset = $1 - 1;
        $limit = $2;
    }

    # for now, force a limit of 5 messages (for sanity)
    $limit = 5 if $limit > 5;

    # because the offset is moving backwards, we need to make sure the limit doesn't
    # exceed it (as the limit then moves the end point back toward the most recent entry)
    $limit = ($offset + 1) if $limit > $offset;

    # then we move the offset to mark the end point (closest to most recent) since we'll
    # be grabbing these in reverse order initially
    $offset = ($offset + 1) - $limit;

    my $res;

    if (scalar(@nicks) > 0) {
        $res = $bot->db->do(q{
            select d.*
            from ( select n.nick, l.message, l.posted_at
                   from nicks n
                       join logger_log l on (l.nick_id = n.id)
                       join channels c on (c.id = l.channel_id)
                   where lower(n.nick) in ??? and lower(c.name) = lower(?)
                   order by l.posted_at desc
                   limit ? offset ?
                 ) d
{'fraction_non_alphanumeric': 0.17867146858743496, 'fraction_numerical': 0.009403761504601841, 'mean_word_length': 1.9685273159144894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2240102', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1687, 'n_words': 529}