-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixpoller
executable file
·106 lines (88 loc) · 3.38 KB
/
matrixpoller
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/perl -w
#
# Matrix Poller for Hermod Gateway Bot.
#
# Keeping a tail on the tomatrix file for messages to send to matrix.
# Polling the matrix group for new messages. These are send to telegram,
# signal and/or irc
#
# 2020, Ruben de Groot
use strict;
use JSON;
use TOML;
use URI::Escape;
use Capture::Tiny 'capture';
use Hermod;
my $msg;
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 $mat = $cfg->{matrix} if defined $cfg->{matrix};
unless (defined $mat->{token} and defined $mat->{room} and defined $mat->{since}
and defined $mat->{syncurl} and defined $mat->{posturl}) {
print "Please define matrix->token, room, since, syncurl and posturl\n";
print "Press <Ctrl>-C to continue\n";
sleep; exit;
}
open my $dbg, ">>", $mat->{debug} if defined $mat->{debug};
my $sig = $cfg->{signal} if defined $cfg->{signal};
my $tel = $cfg->{telegram} if defined $cfg->{telegram};
my $irc = $cfg->{irc} if defined $cfg->{irc};
my $mm = $cfg->{mattermost} if defined $cfg->{mattermost};
my $dis = $cfg->{discord} if defined $cfg->{discord};
(my $syncurl = $mat->{syncurl}) =~ s/__SINCE__/$mat->{since}/;
$syncurl =~ s/__TOKEN__/$mat->{token}/;
(my $posturl = $mat->{posturl}) =~ s/__ROOM__/$mat->{room}/;
$posturl =~ s/__TOKEN__/$mat->{token}/;
# query first for next_batch
my $json = JSON->new->allow_nonref;
my ($out, $err, $ret) = capture {
system("curl", "-XGET", "-s", "$syncurl")
};
eval {
$msg = $json->decode($out);
$mat->{since} = $msg->{next_batch} if defined $msg->{next_batch};
};
if ($@) {
print "Error in json decode $@\n";
print $out, $err;
print $dbg $out, $err if defined $dbg;
exit 1;
}
for (;;) {
sleep 5; # not to get too tight loop
# poll the matrix group for new messages
($syncurl = $mat->{syncurl}) =~ s/__SINCE__/$mat->{since}/;
$syncurl =~ s/__TOKEN__/$mat->{token}/;
my ($out, $err, $ret) = capture {
system("curl", "-s", "-XGET", "$syncurl")
};
print $dbg $out, $err if defined $dbg;
eval {
$msg = $json->decode($out);
next unless defined $msg->{next_batch};
$mat->{since} = $msg->{next_batch};
if (defined $msg->{rooms}->{join}->{$mat->{room}}->{timeline}->{events}) {
for my $ev (@{$msg->{rooms}->{join}->{$mat->{room}}->{timeline}->{events}}) {
my $sender = (defined $ev->{sender}) ? $ev->{sender} : "";
my $text = (defined $ev->{content}->{body}) ? $ev->{content}->{body} : "";
next if $mat->{bot} eq $sender or not $text;
my $url = (defined $ev->{content}->{url}) ? $ev->{content}->{url} : "";
if ($url =~ /^mxc:/) {
# rework url to something browsers understand
(my $srv = $url) =~ s#mxc://([a-z\.]+)/.*$#$1#;
(my $id = $url) =~ s#.*/(\w*)#$1#;
$text = "https://$srv/_matrix/media/r0/download/$srv/$id ($text)";
}
my $pre = "[mtx] $sender: ";
Hermod::relay2tel($tel,"$pre$text\n");
Hermod::relay2irc("$text\n",$irc,$pre);
Hermod::relay2mm("$pre$text\n",$mm);
Hermod::relay2dis("$pre$text\n",$dis);
Hermod::relay2sig("$pre$text\n",$sig);
}
}
};
}