-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.cgi
executable file
·92 lines (77 loc) · 2.14 KB
/
post.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
#!/usr/local/bin/perl
use strict;
use CGI;
use Storable;
use Cwd;
use YAML::Tiny;
use Encode 'decode';
# Reasonable defaults
my $basedir = getcwd;
my $datadir = "$basedir/data";
my $userfile = "$datadir/users";
my $ConfigFile = "$datadir/slack-archive.conf";
my $channelfile = "$datadir/channels";
my @tokens;
if ( -f $ConfigFile ) {
my $config = YAML::Tiny->read($ConfigFile);
@tokens = @{$config->[0]->{tokens}};
}
my $obj = new CGI;
# parameters and contents that will be POSTed by slack
# token=MtdvdIpJjhYfBdtVNbuISBVL
# team_id=T0001
# channel_id=C2147483705
# channel_name=test
# timestamp=1355517523.000005
# user_id=U2147483697
# user_name=Steve
# text=googlebot: What is the air-speed velocity of an unladen swallow?
# trigger_word=googlebot:
my $token = $obj->param('token');
if ( $token eq '' or grep(/$token/, @tokens) == 0 ) {
print $obj->header(-status => 403,
-type => "text/html");
print "Error!";
exit;
}
my $logfile = $obj->param('channel_name');
$logfile =~s|[^a-zA-Z0-9_-]||g;
my $data = [];
my $log = "$datadir/ch_$logfile";
if ( -f "$log" ) {
$data = retrieve("$log");
}
push @$data, {
text => decode('UTF-8', $obj->param('text')),
user => scalar($obj->param('user_name')),
time => scalar($obj->param('timestamp'))
};
store($data, "$log");
my $users = {};
if ( -f "$userfile" ) {
$users = retrieve("$userfile");
}
$users->{$obj->param('user_id')} = $obj->param('user_name');
store($users, "$userfile");
my $channels = [];
if ( -f "$channelfile" ) {
$channels = retrieve("$channelfile");
}
my $ch = { id => $obj->param('channel_id'),
name => $obj->param('channel_name'),
updated => time
};
for (my $i = @$channels-1; $i >= 0; $i--) {
if ( $channels->[$i]->{name} eq $ch->{name} ) {
splice(@$channels, $i, 1);
}
}
push @$channels, $ch;
store($channels, "$channelfile");
open(OUT, " >> $datadir/_ch_$logfile.csv");
my $text = $obj->param('text');
$text =~s/"/""/;
print OUT $obj->param('token') . ", " . $obj->param('channel_name') . ", " . $obj->param('timestamp') . ", " . $obj->param('user_name') . ', ' . '"' . $text . '"' . "\n";
close(OUT);
print $obj->header(-type => 'text/html;charset=utf-8');
exit;