]> git.sesse.net Git - skvidarsync/blob - www/event.pl
75106df1d5007f74ca79bfd90a262b6e9016f4f7
[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 $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'};
52
53         if (!defined($channel) || !defined($ts)) {
54                 print STDERR "Not reacting to a message; ignoring.\n";
55                 print CGI->header(-type=>'text/plain');
56                 print "OK\n";
57                 exit;
58         }
59
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);
63                 mark();
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);
67                 mark();
68         } else {
69                 print STDERR "Type is $type (not a reaction added or removed); ignoring.\n";
70         }
71
72         # Fall through.
73 } else {
74         print STDERR "Has no type; igoring.\n";
75 }
76
77 print CGI->header(-type=>'text/plain');
78 print "OK\n";