# -*- mode: perl; coding: utf-8 -*-
# keitairc/lib/plugins/00recent
# 未読発言一覧

# The line number (1 incremented) and filename below must be
# actual. see perlsyn.
# line 10 "keitairc/lib/plugins/00recent"

$plugin = {
	name => 'recent',
	action_imprementation => sub {
		my ($request, $name, $session_id, $param_string) = @_;
		my @loop;
		for my $cid ($::ib->channels()){
			my $channel = $::ib->cid2name($cid);
			if($::ib->unread_lines($cid)){
				my $name = $::ib->cid2name($cid);
				my $p = {};
				$p->{cid} = $cid;
				$p->{cname} = $::ib->simple_escape($::ib->compact_channel_name($cid));
				my ($tp, $np, $mp, $rp) = $::ib->buffer_ptr($cid);
				my @looop;
				if(defined $tp){
					my @range = (0 .. @{$tp}-1);
					@range = reverse @range if $::cf->reverse_recent();
					my ($last_date, $current_date) = ('', undef);
					for my $lp (@range){
						next if @{$rp}[$lp];
						my $pp = {};
						$current_date = $::ib->format_date(@{$tp}[$lp]);
						unless($last_date eq $current_date){
							$last_date = $current_date;
							my $ppp = {};
							$ppp->{date} = $current_date;
							push @looop, $ppp;
						}
						$pp->{time} = $::ib->format_time(@{$tp}[$lp]);
						$pp->{nick} = $::ib->simple_escape(@{$np}[$lp]);
						my $message = $::ib->colorize($::ib->simple_escape(@{$mp}[$lp])) || '';
						for my $name ($::pl->list_replace_plugins()){
							last if $message =~ s/$::pl->{plugins}->{$name}->{message_replace_regexp}/$::pl->{plugins}->{$name}->{message_replace_imprementation}($request, $session_id, $1, $2, $3, $4, $5, $6, $7, $8, $9)/eg;
						}
						$message =~ s/\s+$//;
						$message =~ s/\s+/ /g;
						$pp->{message} = $message;
						@{$rp}[$lp] = 1;
						push @looop, $pp;
					}
				}

				$p->{looop} = \@looop;
				push @loop, $p;
			}
		}

		my $ci = new Keitairc::ClientInfo($request);
		my $view = new Keitairc::View($::cf, $ci);
		return $view->render('recent.html', { loop => \@loop });
	}
};

1;
