-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatterhook
executable file
·87 lines (71 loc) · 2.75 KB
/
matterhook
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
#!/usr/bin/perl -w
use strict;
use CGI::Fast qw/:standard/;
use JSON;
use TOML;
use Text::Unidecode;
use URI::Escape;
use Hermod;
open my $fh, '<', "/etc/hermod.toml" or die "error opening configuration $!";
my ($cfg, $e) = from_toml do { local $/; <$fh> };
unless ($cfg) {
die "Error parsing toml: $e";
}
my $tel = $cfg->{telegram} if defined $cfg->{telegram};
my $irc = $cfg->{irc} if defined $cfg->{irc};
my $mat = $cfg->{matrix} if defined $cfg->{matrix};
my $sig = $cfg->{signal} if defined $cfg->{signal};
my $dis = $cfg->{discord} if defined $cfg->{discord};
my $mm = $cfg->{mattermost};
unless (defined $mm->{intoken} and defined $mm->{channel_id} and defined $mm->{user_id}) {
print CGI->header(-type => 'application/json');
print '{"ok": false, "status": 500, "error": "token, channel_id or user_id undefined"}'."\n\n";
exit;
}
open my $dbg, ">>", $mm->{debug} if defined $mm->{debug};
while (my $cgi = CGI::Fast->new) {
my $body = (defined $cgi->param('POSTDATA')) ? $cgi->param('POSTDATA') : '';
unless ($body) { nok("missing body"); next; }
print $dbg "$body\n" if defined $dbg;
my $dj;
eval {
$dj = decode_json( $body );
};
if ($@) { nok($@); next; }
# check intoken, channel and not from me
if ($dj->{token} eq $mm->{intoken} and $dj->{channel_id} eq $mm->{channel_id} and $dj->{user_id} ne $mm->{user_id}) {
my $text = "$dj->{text}\n";
my $pre = "[mm] $dj->{user_name}: ";
# send to other chats
Hermod::relay2irc($text,$irc,$pre,$dbg);
Hermod::relay2tel($tel,"$pre$text",$dbg);
Hermod::relay2mtx("$pre$text", $mat, $dbg);
Hermod::relay2dis("$pre$text", $dis, $dbg);
Hermod::relayToFile("$pre$text", $sig->{infile}, $dbg) if defined $sig->{infile};
if ($dj->{file_ids}) {
# download or get public link(s)
my $filemsg;
for my $id (split /,/,$dj->{file_ids}) {
if (my $link = Hermod::getmmlink($id,$mm)) {
$pre = "[mm] **$dj->{user_name} sends a file: ";
# Relay links to other chats
Hermod::relay2irc($link,$irc,$pre,$dbg);
Hermod::relay2tel($tel,"$pre$link\n",$dbg);
Hermod::relay2mtx("$pre$link\n",$mat,$dbg);
Hermod::relay2dis("$pre$link\n",$dis,$dbg);
Hermod::relayToFile("$pre$link\n",$sig->{infile},$dbg) if defined $sig->{infile};
}
}
}
}
ok();
}
sub ok {
print CGI->header(-type => 'application/json');
print '{"ok":true,"status":200}'."\n\n";
}
sub nok {
my $result = shift;
print CGI->header(-type => 'application/json');
print '{"ok":false,"status":502,"error":"'.$result.'}'."\n\n";
}