7 use Digest::SHA qw(hmac_sha256_hex);
11 require '../include/config.pm';
14 print STDERR "Marking that a sync is needed.\n";
15 open my $fh, ">", "/srv/skvidar-slack.sesse.net/marker";
19 my $dbh = DBI->connect("dbi:Pg:dbname=$config::dbname;host=127.0.0.1", $config::dbuser, $config::dbpass, {RaiseError => 1})
20 or die "Could not connect to Postgres: " . DBI->errstr;
24 if ($#ARGV >= 0 && $ARGV[0] eq '--stdin') {
28 $post = $cgi->param('POSTDATA');
29 my $ts = $cgi->http('X-Slack-Request-Timestamp');
30 my $sig = $cgi->http('X-Slack-Signature');
32 my $digest = Digest::SHA::hmac_sha256_hex("v0:$ts:$post", $config::signing_secret);
33 die "Failed signature" unless ($sig eq "v0=$digest");
35 print STDERR "JSON: $post\n";
38 my $json = JSON::XS::decode_json($post);
39 if (exists($json->{'challenge'})) {
40 print CGI->header(-type=>'text/plain');
41 print $json->{'challenge'}, "\n";
45 if (exists($json->{'event'}) && exists($json->{'event'}{'type'})) {
46 my $type = $json->{'event'}{'type'};
47 my $reaction = $json->{'event'}{'reaction'};
48 my $channel = $json->{'event'}{'item'}{'channel'};
49 my $ts = $json->{'event'}{'item'}{'ts'};
50 my $event_ts = $json->{'event'}{'event_ts'};
51 my $user = $json->{'event'}{'user'};
53 if (!defined($channel) || !defined($ts)) {
54 print STDERR "Not reacting to a message; ignoring.\n";
55 print CGI->header(-type=>'text/plain');
60 if ($type eq 'reaction_added') {
61 $dbh->do('INSERT INTO reaction_log (userid, channel, ts, reaction, event_type, event_ts) VALUES (?,?,?,?,?,?)', undef,
62 $user, $channel, $ts, $reaction, $type, $event_ts);
64 } elsif ($type eq 'reaction_removed') {
65 $dbh->do('INSERT INTO reaction_log (userid, channel, ts, reaction, event_type, event_ts) VALUES (?,?,?,?,?,?)', undef,
66 $user, $channel, $ts, $reaction, $type, $event_ts);
69 print STDERR "Type is $type (not a reaction added or removed); ignoring.\n";
74 print STDERR "Has no type; igoring.\n";
77 print CGI->header(-type=>'text/plain');