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 $user = $json->{'event'}{'user'};
49 if ($type eq 'message') {
50 if ($json->{'event'}{'message'}{'text'} =~ /(20\d{2}-\d{2}-\d{2})/) {
51 # TODO: What if edits happen out-of-order?
53 my $channel = $json->{'event'}{'channel'};
54 my $ts = $json->{'event'}{'message'}{'ts'};
55 print "Matching message {$channel, $ts} to date $date\n";
56 $dbh->do('INSERT INTO message_sheet_link (channel, ts, sheet_title) VALUES (?,?,?)', undef,
57 $channel, $ts, $date);
59 print STDERR "No date found in message, ignoring\n";
64 my $reaction = $json->{'event'}{'reaction'};
65 my $channel = $json->{'event'}{'item'}{'channel'};
66 my $ts = $json->{'event'}{'item'}{'ts'};
67 my $event_ts = $json->{'event'}{'event_ts'};
69 if (!defined($channel) || !defined($ts)) {
70 print STDERR "Not reacting to a message; ignoring.\n";
71 print CGI->header(-type=>'text/plain');
76 if ($type eq 'reaction_added' || $type eq 'reaction_removed') {
77 $dbh->do('INSERT INTO reaction_log (userid, channel, ts, reaction, event_type, event_ts) VALUES (?,?,?,?,?,?)', undef,
78 $user, $channel, $ts, $reaction, $type, $event_ts);
81 print STDERR "Type is $type (not a reaction added or removed); ignoring.\n";
86 print STDERR "Has no type; igoring.\n";
89 print CGI->header(-type=>'text/plain');