# -*- mode: perl; coding: utf-8 -*-
# keitairc/lib/plugins/00index
# チャネル一覧
# $Id: 00index,v 1.9.2.1 2008-07-20 02:22:32 morimoto Exp $
# $Source: /home/ishikawa/work/keitairc/tmp/keitairc/lib/plugins/00index,v $

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

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

		::send_message($request);

		my $unread_channels = 0;
		my $accesskey = 1;

		my $format_mtime = sub{
			use encoding 'utf8';
			my $mtime = shift;
			return if($mtime <= 0);
			my $timediff = time - $mtime;
			if($timediff < 60){
				return $timediff . '秒';
			}
			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) . '年';
			no encoding 'utf8';
		};

		my @loop;
		for my $cid ($::ib->channels()){
			my $p = {};
			my $channel = $::ib->cid2name($cid);
			my $cname = $::ib->simple_escape(encode($::cf->web_charset(), $::ib->compact_channel_name($cid)));
			$p->{sid} = $session_id;
			$p->{cid} = $cid;
			$p->{cname} = $cname;
			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++;

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

			$p->{mtime} = Encode::encode($::cf->web_charset(), $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,
					     sid => $session_id,
				     });
	}
};

1;
