-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhermod
executable file
·102 lines (91 loc) · 4.18 KB
/
hermod
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
#!/usr/bin/perl
use warnings;
use lib '/home/ruben/src/git/signal-irc-telegram-gateway';
use POSIX;
use POE qw(Component::Server::TCP);
use JSON;
use TOML;
use Hermod;
################################################################################
# Hermod server #
# #
# Provides connectivity between relay bots for different chat platforms #
# Uses Hermod.pm module for relaying incoming JSON messages to the configured #
# bots. #
# #
# example incoming JSON format: #
# { #
# "token": "xxxxxxxyyyyyyyzzzzzzz", #
# "chat": "signal", #
# "user": "John Smith", #
# "prefix": "sig", #
# "text": "Hi there folks!", #
# "file": "/path/to/file" #
# } #
# #
# All fields are mandatory, except "file" #
# #
# 2021, Ruben de Groot #
# #
################################################################################
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";
}
# set process name
$0 = 'hermod';
POSIX::setsid or die "setsid: $!";
my $out = (defined $cfg->{common}{debug}) ? $cfg->{common}{debug} : '/dev/null';
my $pid = fork() // die $!; #//
exit(0) if $pid;
chdir "/";
umask 0;
open(STDIN,"</dev/null");
open(STDOUT,">>$out");
open(STDERR,">&STDOUT");
# daemon is now running
POE::Component::Server::TCP->new(
Alias => "hermod",
Port => (defined $cfg->{common}{port}) ? $cfg->{common}{port} : 31337,
ClientInput => sub {
my ($kernel, $heap,$input) = @_[KERNEL, HEAP, ARG0];
print "Input: $input\n";
$kernel->yield("shutdown");
print handle($input,$cfg);
},
);
# Start the server.
$poe_kernel->run();
sub handle {
my ($input,$cfg) = @_;
return unless $input =~ /^{.*}$/;
my $msg;
eval { $msg = decode_json $input; };
if ($@) {
print "Failed to decode $input: $@\n";
return;
}
return "$input is not a hash\n" unless ref $msg eq "HASH";
return "Missing fields\n" unless $msg->{user} and $msg->{prefix} and $msg->{text} and $msg->{token} and $msg->{chat};
return "Token $msg->{token} mismatch\n" unless defined $msg->{token}
and $msg->{token} eq $cfg->{common}{token};
if ($msg->{file}) {
} else {
my $text;
unless ($msg->{chat} eq "telegram") {
($text = $msg->{text}) =~ s/_/\\_/g;
$text =~ s/\*/\\*/g;
$text =~ s/\[/\\[/g;
$text =~ s/\]/\\]/g;
$text = '`['.$msg->{prefix}.'] '.$msg->{user}.':` '.$text."\n";
Hermod::relay2Tel($text, $cfg->{telegram});
}
Hermod::relay2irc($msg->{text}, $cfg->{irc}, "[$msg->{prefix}] $msg->{user}: ") unless $msg->{chat} eq "irc";
$text = "[$msg->{prefix}] $msg->{user}: $msg->{text}\n";
Hermod::relay2mm($text, $cfg->{mattermost}) unless $msg->{chat} eq "mattermost";
Hermod::relay2mtx($text, $cfg->{matrix}) unless $msg->{chat} eq "matrix";
Hermod::relay2dis($text, $cfg->{discord}) unless $msg->{chat} eq "discord";
Hermod::relay2sig($text, $cfg->{signal}) unless $msg->{chat} eq "signal";
}
}