-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cgi
executable file
·160 lines (128 loc) · 3.55 KB
/
index.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/local/bin/perl
use strict;
use utf8;
use CGI;
use Storable;
use Cwd;
use YAML::Tiny;
use Encode 'encode';
my $basedir = getcwd;
my $datadir = "$basedir/data";
my $ConfigFile = "$datadir/slack-archive.conf";
my $userfile = "$datadir/users";
my $channelfile = "$datadir/channels";
my $url = "http://a11yj.ma10.jp/";
if ( -f $ConfigFile ) {
my $config = YAML::Tiny->read("$ConfigFile");
$url = $config->[0]->{url};
}
my $obj = new CGI;
my $channel = $obj->param('channel');
my $action = 'list';
if ( $channel ne '' ) {
$action = 'show';
}
print $obj->header(-type => 'text/html;charset=utf-8');
if ( $action eq 'list' ) {
&list;
} elsif ( $action eq 'show' ) {
&show($channel);
}
exit;
sub list {
binmode(STDOUT, ":utf8");
print << "EOM";
<!DOCTYPE html>
<head>
<title>A11YJ.slack.com チャンネル・アーカイブ</title>
</head>
<body>
<h1>A11YJ.slack.com チャンネル・アーカイブ</h1>
<p>以下のチャンネルのアーカイブを提供しています。</p>
EOM
my $chinfo;
if ( -f $channelfile ) {
$chinfo = retrieve("$channelfile");
}
my @channels;
foreach (@$chinfo) {
if ( -f "$datadir/ch_$_->{name}" ) {
push @channels, $_;
}
}
print "<ul>\n";
foreach (sort { $b->{updated} cmp $a->{updated} } @channels) {
my $ch= $_->{name};
my ($sec, $min, $hr, $day, $mon, $year) = localtime($_->{updated});
$year += 1900;
$mon++;
my $updated = "$year-$mon-$day $hr:$min:$sec";
print "<li><a href=\"$url?channel=$ch\">#$ch ($updated 更新)</a></li>\n";
}
print '</ul><p><a href="https://a11yj.herokuapp.com/">このSlack Teamに参加する</a></p></body></html>';
return 1;
}
sub show {
my $channel = shift;
my $data = retrieve("$datadir/ch_$channel");
my $users = {};
if ( -f $userfile ) {
$users = retrieve("$userfile");
}
my $chinfo;
if ( -f $channelfile ) {
$chinfo = retrieve("$channelfile");
}
print << "EOM";
<!DOCTYPE html>
<head>
<title>#$channel on A11YJ.slack.com チャンネル・アーカイブ</title>
</head>
<body>
<h1>#$channel on a11yj.slack.com チャンネル・アーカイブ</h1>
EOM
my $current_date = '00';
foreach my $msg (@$data) {
my $text = $msg->{text};
my $mention_uid = "";
my $linked_channel_id;
while ( $text =~ m/<\@(U.+?)>/ ) {
$mention_uid = $1;
my $mention_name = $mention_uid;
if ( exists($users->{$mention_uid}) ) {
$mention_name = $users->{$mention_uid};
}
$text =~ s/<\@$mention_uid>/\@$mention_name/;
}
while ( $text =~ m/<#(C.+?)>/ ) {
$linked_channel_id = $1;
my $linked_channel_name = $linked_channel_id;
foreach (@$chinfo) {
if ( $_->{id} eq $linked_channel_id ) {
$linked_channel_name = $_->{name};
}
}
$text =~ s/<#$linked_channel_id>/#$linked_channel_name/;
}
while ( $text =~ m|<(https?://.+?)>| ) {
my $linked_url = $1;
$text =~ s|<\Q$linked_url\E>|<a href="$linked_url">$linked_url</a>|;
}
$text =~ s/\n/<br>/g;
my ($sec, $min, $hr, $day, $mon, $year) = localtime($msg->{time});
$year += 1900;
$mon++;
my $postdate = "$year/$mon/$day";
if ( $current_date eq '00' ) {
print "<h2>$postdate</h2><ul>\n";
$current_date = $postdate ;
}
if ( $current_date ne $postdate ) {
print "</ul><h2>$postdate</h2><ul>\n";
$current_date = $postdate;
}
print "<li>$msg->{user}: " . encode('UTF-8', $text);
print " ($hr:$min:$sec)</li>\n";
}
print '</ul><p><a href="/">アーカイブ・リストに戻る</a> - <a href="https://a11yj.herokuapp.com/">このSlack Teamに参加する</a></p></body></html>';
}