]> git.sesse.net Git - skvidarsync/blob - www/event.pl
Remove HKS 2024 code.
[skvidarsync] / www / event.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 no warnings qw(once);
5 use CGI;
6 use JSON::XS;
7 use Digest::SHA qw(hmac_sha256_hex);
8 use LWP::UserAgent;
9 use DBI;
10
11 require '../include/config.pm';
12
13 sub mark {
14         print STDERR "Marking that a sync is needed.\n";
15         open my $fh, ">", "/srv/skvidar-slack.sesse.net/marker";
16         close $fh;
17 }
18
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;
21
22 my $cgi = CGI->new;
23 my $post;
24 if ($#ARGV >= 0 && $ARGV[0] eq '--stdin') {
25         local $/ = undef;
26         $post = <STDIN>;
27 } else {
28         $post = $cgi->param('POSTDATA');
29         my $ts = $cgi->http('X-Slack-Request-Timestamp');
30         my $sig = $cgi->http('X-Slack-Signature');
31
32         my $digest = Digest::SHA::hmac_sha256_hex("v0:$ts:$post", $config::signing_secret);
33         die "Failed signature" unless ($sig eq "v0=$digest");
34
35         print STDERR "JSON: $post\n";
36 }
37
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";
42         exit;
43 }
44
45 if (exists($json->{'event'}) && exists($json->{'event'}{'type'})) {
46         my $type = $json->{'event'}{'type'};
47         my $user = $json->{'event'}{'user'};
48
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?
52                         my $date = $1;
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);
58                 } else {
59                         print STDERR "No date found in message, ignoring\n";
60                 }
61                 exit;
62         }
63
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'};
68
69         if (!defined($channel) || !defined($ts)) {
70                 print STDERR "Not reacting to a message; ignoring.\n";
71                 print CGI->header(-type=>'text/plain');
72                 print "OK\n";
73                 exit;
74         }
75
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);
79                 mark();
80         } else {
81                 print STDERR "Type is $type (not a reaction added or removed); ignoring.\n";
82         }
83
84         # Fall through.
85 } else {
86         print STDERR "Has no type; ignoring.\n";
87 }
88
89 print CGI->header(-type=>'text/plain');
90 print "OK\n";