-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHermod.pm
303 lines (244 loc) · 9.17 KB
/
Hermod.pm
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package Hermod;
use JSON;
use TOML;
use WWW::Curl::Easy;
use WWW::Curl::Form;
use URI::Escape;
use Capture::Tiny 'capture';
use Encode qw(encode_utf8);
use IO::Socket::INET;
sub bridge {
my ($msg,$cfg) = @_;
return "missing fields in message\n"
unless $msg->{user} and $msg->{prefix} and $msg->{text} and $msg->{token} and $msg->{chat};
my $json;
eval {
$json = encode_json $msg;
};
return "Error in encode_json, sub bridge\n" if $@;
# auto-flush on socket
$| = 1;
# create a connecting socket
my $socket = new IO::Socket::INET (
PeerHost => '127.0.0.1',
PeerPort => (defined $cfg->{common}{port}) ? $cfg->{common}{port} : '31337',
Proto => 'tcp',
);
return "cannot connect to the server $!\n" unless $socket;
my $send = $socket->send($json);
$socket->shutdown(SHUT_RDWR);
return $json;
}
sub getmmlink {
my ($id,$mm,$dbg) = @_;
return unless defined $mm;
my $json = JSON->new->allow_nonref;
my ($out, $err, $ret) = capture {
system("curl", "-s", "-XGET", "-H", "Authorization: Bearer $mm->{bearer}", "$mm->{api}/files/$id/link");
};
print $dbg $out, $err if defined $dbg;
my $jsonret;
eval { $jsonret = $json->decode($out); };
return (defined $jsonret->{link}) ? $jsonret->{link} : '';
}
sub relay2mm {
my ($text,$mm,$dbg) = @_;
return unless defined $mm;
my $json = JSON->new->allow_nonref;
$text = $json->encode({attachments => [{text => $text}], username => $mm->{user_name}});
my $curl = WWW::Curl::Easy->new;
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);
$curl->setopt(CURLOPT_URL, $mm->{url});
$curl->setopt(WWW::Curl::Easy::CURLOPT_NOPROGRESS(), 1);
$curl->setopt(WWW::Curl::Easy::CURLOPT_HTTPHEADER(), ['Content-Type: application/json; charset=UTF-8']);
$curl->setopt(WWW::Curl::Easy::CURLOPT_POST(), 1);
$curl->setopt(WWW::Curl::Easy::CURLOPT_POSTFIELDS, $text);
my $retcode = $curl->perform;
if ($retcode != 0) {
print $dbg "An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n" if $dbg;
}
}
sub relayFile2mm {
my ($line,$mm,$dbg) = @_;
return unless defined $mm;
if ($line =~ /^FILE!/) {
$line = substr $line,5;
my ($fileinfo,$caption) = split / /, $line, 2;
my ($url,$mime,$file) = split /!/, $fileinfo;
my $json = JSON->new->allow_nonref;
my $bearer = "Authorization: Bearer $mm->{bearer}";
my ($out, $err, $ret) = capture {
system("curl", "-s", "-XPOST", "-H", "$bearer", "-F", "channel_id=$mm->{channel_id}", "-F", "files=\@$file", "$mm->{api}/files" );
};
print $dbg $out, $err if defined $dbg;
my $jsonret;
eval { $jsonret = $json->decode($out); };
print $dbg $@ if defined $dbg and $@;
if (defined $jsonret->{file_infos} and ref $jsonret->{file_infos} eq "ARRAY") {
my $jh = {
channel_id => $mm->{channel_id},
message => $caption,
file_ids => [ $jsonret->{file_infos}[0]{id} ]
};
my $jsonstr = $json->encode($jh);
my $curl = WWW::Curl::Easy->new;
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);
$curl->setopt(CURLOPT_URL, "$mm->{api}/posts");
$curl->setopt(WWW::Curl::Easy::CURLOPT_NOPROGRESS(), 1);
$curl->setopt(WWW::Curl::Easy::CURLOPT_VERBOSE, 0);
$curl->setopt(WWW::Curl::Easy::CURLOPT_HTTPHEADER(), ['Content-Type: application/json; charset=UTF-8', $bearer]);
$curl->setopt(WWW::Curl::Easy::CURLOPT_POST(), 1);
$curl->setopt(WWW::Curl::Easy::CURLOPT_POSTFIELDS, $jsonstr);
my $retcode = $curl->perform;
if ($retcode != 0) {
print "An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n";
}
print "Response: $response_body\n";
}
}
}
sub relay2mtx {
my ($line,$mat,$dbg) = @_;
return unless defined $mat;
# we relay straight to matrix
(my $posturl = $mat->{posturl}) =~ s/__ROOM__/$mat->{room}/;
$posturl =~ s/__TOKEN__/$mat->{token}/;
if ($line =~ /^FILE!/) {
# send photo's, documents
$line = substr $line,5;
my ($fileinfo,$caption) = split / /, $line, 2;
my ($url,$mime,$filepath) = split /!/, $fileinfo;
# TODO upload file
next;
}
chomp $line;
$line =~ s/"/\\"/g;
$line =~ s/\n/\\n/g;
my $body = '{"msgtype":"m.text", "body":"'.$line.'"}';
print $dbg "body: $body\n" if defined $dbg;
my ($out, $err, $ret) = capture {
system("curl", "-s", "-XPOST", "-d", "$body", "$posturl");
};
print $dbg $out, $err if defined $dbg;
}
sub relay2Tel {
my ($text,$tel,$dbg) = @_;
return unless defined $tel;
# we relay straight to telegram
my $telmsg;
$text = encode_utf8($text);
eval { $telmsg = uri_escape($text); };
$telmsg = uri_escape_utf8($text) if $@;
my ($out, $err, $ret) = capture {
system("curl", "-s", "https://api.telegram.org/bot$tel->{token}/sendMessage?chat_id=$tel->{chat_id}&parse_mode=MarkdownV2&text=$telmsg");
};
print $dbg $out, $err if defined $dbg;
}
sub relay2tel {
my ($tel,$text,$dbg) = @_;
return unless defined $tel;
# we relay straight to telegram
my $telmsg;
$text = encode_utf8($text);
eval { $telmsg = uri_escape($text); };
$telmsg = uri_escape_utf8($text) if $@;
my ($out, $err, $ret) = capture {
system("curl", "-s", "https://api.telegram.org/bot$tel->{token}/sendMessage?chat_id=$tel->{chat_id}&text=$telmsg");
};
print $dbg $out, $err if defined $dbg;
}
sub relayFile2tel {
my ($line,$tel,$type,$dbg) = @_;
return unless defined $tel;
if ($line =~ /^FILE!/ and $tel->{token} and $tel->{chat_id} and $type) {
$line = substr $line,5;
my ($fileinfo,$caption) = split / /, $line, 2;
my ($url,$mime,$file) = split /!/, $fileinfo;
my $URL = ($type eq "photo") ? "https://api.telegram.org/bot$tel->{token}/sendPhoto"
: "https://api.telegram.org/bot$tel->{token}/sendDocument";
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_URL, $URL);
my $curlf = WWW::Curl::Form->new;
$curlf->formaddfile($file, $type, "multipart/form-data");
$curlf->formadd("chat_id", "$tel->{chat_id}");
$curlf->formadd("caption", $caption);
$curl->setopt(CURLOPT_HTTPPOST, $curlf);
my $retcode = $curl->perform;
if ($retcode != 0) {
print $dbg "An error happened in relayFile2tel: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n" if defined $dbg;
}
}
}
sub relay2dis {
my ($text,$dis,$dbg) = @_;
return unless defined $dis;
# we relay straight to discord
my $json = JSON->new->allow_nonref;
my $dismsg = {
username => $dis->{username},
content => $text,
};
$text = $json->encode($dismsg);
my ($out, $err, $ret) = capture {
system("curl", "-s", "-H", "Content-Type: application/json", "-d", "$text", "$dis->{webhook}");
};
print $dbg $out, $err if defined $dbg;
}
sub relay2irc {
my ($text,$irc,$pre,$dbg) = @_;
return unless defined $irc;
return unless $irc->{infile} and $text;
my @lines = split /\n/, $text;
open my $w, ">>:utf8", $irc->{infile} or return;
for my $msg (@lines) {
next unless $msg;
# send to IRC, split lines in chunks of ~maxmsg size if necessary
if (length $msg > $irc->{maxmsg}) {
$msg =~ s/(.{1,$irc->{maxmsg}}\S|\S+)\s+/$1\n/g;
eval { print $w "$pre$_\n" for split /\n/, $msg; };
print $dbg $@ if $@ and defined $dbg;
} else {
eval { print $w "$pre$msg\n"; };
print $dbg $@ if $@ and defined $dbg;
}
}
close $w;
}
sub relayToFile {
my ($line,$infile,$dbg) = @_;
return unless $infile and $line;
open my $w, ">>:utf8", $infile;
print $w $line if defined $w;
close $w;
}
sub relay2sig {
my ($line,$sig,$dbg) = @_;
return unless defined $sig;
return unless $line;
# revert to old behaviour unless we defined signal->transport->dbus
if (not defined $sig->{transport} or $sig->{transport} =~ /file/i) {
relayToFile($line,$sig->{infile},$dbg);
}
my $text = '';
if ($line =~ /^FILE!/) {
# send photo's, documents
$line = substr $line,5;
my ($fileinfo,$caption) = split / /, $line, 2;
my ($url,$mime,$file) = split /!/, $fileinfo;
my ($out, $err, $ret) = capture {
system($sig->{cli},"--dbus","send","-g",$sig->{gid},"-m","$caption","-a","$file");
};
print $dbg $out, $err if defined $dbg;
} else {
my $text = ''; my $sav = $line;
eval { $text .= decode_utf8($msg, Encode::FB_QUIET) while $line; };
$text = $sav if $@; # try undecoded string as last resort
my ($out, $err, $ret) = capture {
system($sig->{cli},"--dbus","send","-g",$sig->{gid},"-m","$text");
};
print $dbg $out, $err if defined $dbg;
}
}
1;