# -*-perl-*-
# keitairc/lib/plugins/00index
# `lꗗ
# $Id: 00index,v 1.4 2008/01/18 16:50:35 morimoto Exp $
# $Source: /cvsroot/keitairc/lib/plugins/00index,v $

$plugin = {
	name => 'index',
	action_imprementation => sub {
		my ($request, $name, $session_id, $param_string) = @_;
		my $unread_channels = 0;
		my $accesskey = 1;

		my $format_mtime = sub{
			my $mtime = shift;
			return unless $mtime;
			my $timediff = time - $mtime;
			if($timediff < 60){
				return $timediff . 'b';
			}
			if($timediff < 3600){
				return int($timediff/60) . '';
			}
			if($timediff < 86400){
				return int($timediff/3600) . '';
			}
			if($timediff < 86400 * 30){
				return int($timediff/86400) . '';
			}
			if($timediff < 86400 * 365){
				return int($timediff/86400/30) . '';
			}
			return int($timediff/86400/365) . 'N';
		};

		my @loop;
		for my $cid ($::ib->channels()){
			my $p = {};
			my $channel = $::ib->cid2name($cid);
			my $cname = $::ib->compact_channel_name($cid);
			if($accesskey < 10){
				$p->{link} =
					sprintf('<a accesskey="%1d" href="all/%d">[%1d] %s</a>',
						$accesskey,
						$cid,
						$accesskey,
						$cname);
			}else{
				$p->{link} =
					sprintf('<a href="all/%d">&nbsp;&nbsp;&nbsp; %s</a>',
						$cid, $cname);
			}
			$accesskey++;

			# Ǎs
			if($::ib->unread_lines($cid)){
				$p->{unread} =
					sprintf(' <a href="unread/%d">%s</a>',
						$cid,
						$::ib->unread_lines($cid));
				$unread_channels++;
			}

			$p->{mtime} = $format_mtime->($::ib->mtime($cid));
			push @loop, $p;
		}

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

1;
