-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalBotHelper.pm
More file actions
132 lines (104 loc) · 3.78 KB
/
SignalBotHelper.pm
File metadata and controls
132 lines (104 loc) · 3.78 KB
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
package SignalBotHelper;
use strict;
use warnings;
use Mojo::Base -base;
use SignalConfig;
use LWP::UserAgent ();
use DBI;
use Timer;
has config => sub { SignalConfig->new };
has timer => undef;
has dbh => undef;
has dbh_humhub => undef;
has signal_cli => undef;
my $daemonName = "signalBot";
my $logging = 1; # 1= logging is on
my $logFilePath = "log/"; # log file path
my $logFile = $logFilePath . $daemonName . ".log";
sub init {
my $self = shift;
$self->init_dbi->init_signal_cli->init_timer->init_dbi_humhub;
return $self;
}
sub init_dbi {
my $self = shift;
my $dbh = DBI->connect($self->config->mysql_dns, $self->config->mysql_user, $self->config->mysql_pw);
$dbh->do("USE ".$self->config->mysql_db.";");
$dbh->do('SET NAMES \'utf8\'');
$dbh->do('SET CHARACTER SET \'utf8\'');
$dbh->{mysql_auto_reconnect} = 1;
$self->dbh($dbh);
return $self;
}
sub init_dbi_humhub {
my $self = shift;
my $dbh = DBI->connect($self->config->humhub_mysql_dns, $self->config->humhub_mysql_user, $self->config->humhub_mysql_pw);
$dbh->do("USE ".$self->config->humhub_mysql_db.";");
$dbh->do('SET NAMES \'utf8\'');
$dbh->do('SET CHARACTER SET \'utf8\'');
$dbh->{mysql_auto_reconnect} = 1;
$self->dbh_humhub($dbh);
return $self;
}
sub init_signal_cli {
my $self = shift;
# Switch Signal Client
# It is possible to use dbus or work with a fake input/output to test it local
if ($self->config->signal_cli eq "dbus") {
require SignalCliDBus;
$self->signal_cli(SignalCliDBus->new->signalBot($self));
} else {
require SignalCliDebug;
$self->signal_cli(SignalCliDebug->new->signalBot($self));
}
# TODO: implementation via signal_cli system command
return $self;
}
sub init_timer {
my $self = shift;
$self->timer(Timer->new->signalBot($self));
return $self;
}
# turn on logging
if (SignalConfig->new->fileLogs) {
open LOG, ">>$logFile";
select((select(LOG), $|=1)[0]); # make the log file "hot" - turn off buffering
}
# add a line to the log file
sub logEntry {
my $self=shift;
my ($logText) = @_;
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
my $dateTime = sprintf "%4d-%02d-%02d %02d:%02d:%02d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
print LOG "$dateTime $logText\n" if ($self->config->fileLogs);
warn "$dateTime $logText\n" if ($self->config->warnLogs);
}
# do this stuff when exit() is called.
END {
if (SignalConfig->new->fileLogs) { close LOG }
}
sub humhub_post {
my $msg = shift;
# setup UserAgent
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
$ua->timeout(10);
$ua->env_proxy;
$ua->cookie_jar( {} );
push @{ $ua->requests_redirectable }, 'POST';
# Get Loginform (we need a token)
my $response = $ua->get('https://ritualspace.quadmonah.ch');
return $response->status_line unless ($response->is_success); # Return when error
# POST login
my $login_form = {"_csrf" => _humhub_extract_csrf_token($response->decoded_content), "Login[username]" => get_humhub_user(), "Login[password]" => get_humhub_pw(), "Login[rememberMe]" => 0};
$response = $ua->post("https://ritualspace.quadmonah.ch/user/auth/login", $login_form );
return $response->status_line unless ($response->is_success); # Return when error
# POST message
my $post_form = {"_csrf" => _humhub_extract_csrf_token($response->decoded_content), "message" => $msg, "containerGuid" => "6787f49c-9b88-4f19-a6ec-2324e3265ea4", "containerClass" => 'humhub\modules\space\models\Space'};
$response = $ua->post("https://ritualspace.quadmonah.ch/s/bot-post-test/post/post/post", $post_form );
return $response->status_line unless ($response->is_success); # Return when error
return "";
}
sub _humhub_extract_csrf_token {
shift() =~ m/\<meta name="csrf-token" content="(.*?)"\>/gi;
return $1;
}