OSDN Git Service

2338dffea0264a29ae7c7ea5769370fbf0b5ad45
[keitairc/keitairc.git] / t / 02_Plugins / 01index
1 # -*- mode: perl; coding: utf-8 -*-
2
3 # WARNING: This file is a part of Keitairc::Plugins test suite, not
4 # actual working code.
5
6 $plugin = {
7         name => 'index',
8         action_imprementation => sub {
9                 my ($request, $name, $session_id, $param_string) = @_;
10
11                 ::send_message($request);
12
13                 my $unread_channels = 0;
14                 my $accesskey = 1;
15
16                 my $format_mtime = sub{
17                         use 'utf8';
18                         my $mtime = shift;
19                         return if($mtime <= 0);
20                         my $timediff = time - $mtime;
21                         if($timediff < 60){
22                                 return $timediff . '秒';
23                         }
24                         if($timediff < 3600){
25                                 return int($timediff/60) . '分';
26                         }
27                         if($timediff < 86400){
28                                 return int($timediff/3600) . '時間';
29                         }
30                         if($timediff < 86400 * 30){
31                                 return int($timediff/86400) . '日';
32                         }
33                         if($timediff < 86400 * 365){
34                                 return int($timediff/86400/30) . 'ヶ月';
35                         }
36                         return int($timediff/86400/365) . '年';
37                         no encoding 'utf8';
38                 };
39
40                 my @loop;
41                 for my $cid ($::ib->channels()){
42                         my $p = {};
43                         my $channel = $::ib->cid2name($cid);
44                         my $cname = $::ib->simple_escape(encode($::cf->web_charset(), $::ib->compact_channel_name($cid)));
45                         if($accesskey < 10){
46                                 $p->{link} =
47                                         sprintf('<a accesskey="%1d" href="all/%d">[%1d] %s</a>',
48                                                 $accesskey,
49                                                 $cid,
50                                                 $accesskey,
51                                                 $cname);
52                         }else{
53                                 $p->{link} =
54                                         sprintf('<a href="all/%d">&nbsp;&nbsp;&nbsp; %s</a>',
55                                                 $cid, $cname);
56                         }
57                         $accesskey++;
58
59                         # 未読行数
60                         if($::ib->unread_lines($cid)){
61                                 $p->{unread} =
62                                         sprintf(' <a href="unread/%d">%s</a>',
63                                                 $cid,
64                                                 $::ib->unread_lines($cid));
65                                 $unread_channels++;
66                         }
67
68                         $p->{mtime} = Encode::encode($::cf->web_charset(), $format_mtime->($::ib->mtime($cid)));
69                         push @loop, $p;
70                 }
71
72                 my $ci = new Keitairc::ClientInfo($request);
73                 my $view = new Keitairc::View($::cf, $ci);
74                 return $view->render('index.html',
75                                      {
76                                              loop => \@loop,
77                                              unread => $unread_channels,
78                                      });
79         }
80 };
81
82 1;