-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix_msgs.pl
409 lines (326 loc) · 10 KB
/
matrix_msgs.pl
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Copyright (c) 2017 Mattias Hansson, <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
use strict;
use warnings;
use Irssi;
use Irssi::Irc;
use POSIX qw(strftime);
use HTTP::Request::Common;
use JSON::XS;
use LWP::UserAgent();
use vars qw($VERSION %IRSSI);
# Dependencies:
# HTTP::Request::Common
# JSON::XS
# LWP::Protocol::https
# LWP::UserAgent
# Inherit
$VERSION = '1.0';
%IRSSI = (
author => 'Mattias Hansson',
contact => '[email protected]',
url => 'http://github.com/orzen/matrix_msgs',
name => 'matrix_msgs',
description => "Forward private messages/mentions to a matrix account.",
license => "MIT",
);
# Globals
my $FORMAT;
my $agent;
my $room_id_filename;
my %settings_str;
my $settings_section;
my $settings_user;
my $settings_node;
my $settings_password;
my $settings_to_user;
my $settings_to_node;
my $settings_away_only;
my $settings_priv_msgs;
my $settings_mentions;
my $settings_room;
my $window;
$FORMAT = $IRSSI{'name'} . '_crap';
$settings_section = 'misc';
$agent = LWP::UserAgent->new;
$room_id_filename = Irssi::get_irssi_dir . '/matrix_room_id_cache';
# Construct settings strings
%settings_str = (
user => $IRSSI{'name'} . '_user',
node => $IRSSI{'name'} . '_node',
password => $IRSSI{'name'} . '_password',
to_user => $IRSSI{'name'} . '_to_user',
to_node => $IRSSI{'name'} . '_to_node',
room => $IRSSI{'name'} . '_room',
away_only => $IRSSI{'name'} . '_forward_when_away_only',
priv_msgs => $IRSSI{'name'} . '_send_private_messages',
mentions => $IRSSI{'name'} . '_send_mentions',
);
sub irc_log {
my ($str) = @_;
$window->printformat(Irssi::MSGLEVEL_CLIENTCRAP, $FORMAT, $str);
return;
}
sub file_read_line {
my ($filename) = @_;
my $row;
if (open(my $fd, '<:encoding(UTF-8)', $filename)) {
$row = <$fd>;
chomp $row;
close $fd;
} else {
irc_log("failed to open file '$filename' for reading: $!");
return;
}
return $row;
}
sub file_write_line {
my ($line, $filename) = @_;
if (open(my $fd, '>:encoding(UTF-8)', $filename)) {
print $fd $line;
close $fd;
} else {
irc_log("failed to open file '$filename' for writing: $!");
}
return;
}
sub http_dispatch_json {
my ($method, $endpoint, %msg) = @_;
my $decoded_res;
my $json;
my $req;
my $res;
$req = HTTP::Request->new($method, $endpoint);
if (defined(\%msg)) {
$json = encode_json(\%msg);
$req->header('Content-Type' => 'application/json');
$req->content($json);
}
$res = $agent->request($req);
if ($res->is_success) {
$decoded_res = decode_json($res->content);
} else {
return $decoded_res;
}
return %$decoded_res;
}
sub matrix_login {
my ($node, $user, $password) = @_;
my %msg;
my %res;
my $url;
$url = $node . "/_matrix/client/r0/login";
$msg{'type'} = "m.login.password";
$msg{'user'} = $user;
$msg{'password'} = $password;
# Notice: If you get an issue with requests make sure that
# LWP::Protocol::https is installed.
%res = http_dispatch_json('POST', $url, %msg);
return $res{'access_token'};
}
sub matrix_sync {
my ($node, $access_token) = @_;
my %msg;
my $endpoint;
my $url;
my %res;
$endpoint = "/_matrix/client/r0/sync?access_token=" . $access_token;
$url = $node . $endpoint;
%res = http_dispatch_json('GET', $url);
return %res;
}
sub matrix_create_room {
my ($node, $access_token, $to_user, $to_node, $room) = @_;
my $endpoint;
my %msg;
my %res;
my @invite;
my $url;
$endpoint = "/_matrix/client/r0/createRoom?access_token=" . $access_token;
$url = $node . $endpoint;
@invite = ('@' . $to_user . ':' . $to_node);
$msg{'invite'} = \@invite;
$msg{'preset'} = "private_chat";
$msg{'room_alias_name'} = $room;
%res = http_dispatch_json('POST', $url, %msg);
if ($res{'errcode'}) {
irc_log('failed to create room: ' . $res{'error'});
} else {
return $res{'room_id'};
}
return;
}
sub matrix_send_msg {
my ($node, $room_id, $transaction_id, $access_token, $message) = @_;
my $endpoint;
my $url;
my %msg;
my $json;
my $req;
my %res;
$endpoint = "/_matrix/client/r0/rooms/" .
$room_id .
"/send/m.room.message/" .
$transaction_id .
"?access_token=" . $access_token;
$url = $node . $endpoint;
$msg{'msgtype'} = "m.text";
# TODO encode message
$msg{'body'} = $message;
%res = http_dispatch_json('PUT', $url, %msg);
return $res{'event_id'};
}
sub send_message {
my ($node,
$user,
$password,
$to_user,
$to_node,
$room,
$room_id_filename_,
$message) = @_;
my $access_token;
my $event_id;
my $random;
my $transaction_id;
my $room_id;
my $room_id_cached;
my %sync_res;
$access_token = matrix_login($node, $user, $password);
if (not $access_token) {
irc_log("failed to login");
return;
}
if (-e $room_id_filename_) {
$room_id_cached = file_read_line($room_id_filename_);
}
# Get sync-data to verify membership of the room
%sync_res = matrix_sync($node, $access_token);
if (not %sync_res) {
irc_log("failed to get sync data");
}
# Verifying room membership or creating a new room
if (%sync_res and $sync_res{'rooms'}{'join'}{$room_id_cached}){
$room_id = $room_id_cached;
} else {
$room_id = matrix_create_room($node, $access_token, $to_user, $to_node, $room);
}
if ($room_id) {
# No need to re-write the cached room id
if (not $room_id_cached) {
file_write_line($room_id, $room_id_filename_);
}
# Creating a transaction ID
$random = int(rand(1000000));
$transaction_id = "$random";
# TODO come up with a good way to generate transaction id
$event_id = matrix_send_msg($node,
$room_id,
$transaction_id,
$access_token,
$message);
if ($event_id) {
irc_log('successfully sent a message to @' . $user . ':' . $node);
} else {
irc_log('failed to send a message to @' . $user . ':' . $node);
}
} else {
irc_log("failed to create room '" . $room . "'");
}
return;
}
sub handle_pub_msgs {
my ($server, $message, $user, $address, $target) = @_;
my $message_str;
if ((index($message, $server->{nick}) >= 0) &&
$settings_mentions &&
(!$settings_away_only || $server->{usermode_away})) {
$message_str = $user . '@' . $target . ': ' . $message;
send_message($settings_node,
$settings_user,
$settings_password,
$settings_to_user,
$settings_to_node,
$settings_room,
$room_id_filename,
$message_str);
}
return;
}
sub handle_priv_msgs {
my ($server, $message, $user, $address) = @_;
my $message_str;
if ($settings_priv_msgs &&
(!$settings_away_only || $server->{usermode_away})) {
$message_str = "$user (private): $message";
send_message($settings_node,
$settings_user,
$settings_password,
$settings_to_user,
$settings_to_node,
$settings_room,
$room_id_filename,
$message_str);
}
return;
}
sub load_settings {
# Read the values of the settings
$settings_node = Irssi::settings_get_str($settings_str{'node'});
$settings_user = Irssi::settings_get_str($settings_str{'user'});
$settings_password = Irssi::settings_get_str($settings_str{'password'});
$settings_to_user = Irssi::settings_get_str($settings_str{'to_user'});
$settings_to_node = Irssi::settings_get_str($settings_str{'to_node'});
$settings_room = Irssi::settings_get_str($settings_str{'room'});
$settings_away_only = Irssi::settings_get_bool($settings_str{'away_only'});
$settings_priv_msgs = Irssi::settings_get_bool($settings_str{'priv_msgs'});
$settings_mentions = Irssi::settings_get_bool($settings_str{'mentions'});
}
sub init {
Irssi::theme_register(
[
script_loaded => 'Loaded script {hilight $0} v$1',
$FORMAT => '$0',
]);
$window = Irssi::active_win;
# Add settings to irssi
Irssi::settings_add_str($settings_section, $settings_str{'user'}, 'guest');
Irssi::settings_add_str($settings_section, $settings_str{'node'}, 'https://matrix.org');
Irssi::settings_add_str($settings_section, $settings_str{'password'}, '');
Irssi::settings_add_str($settings_section, $settings_str{'to_user'}, '');
Irssi::settings_add_str($settings_section, $settings_str{'to_node'}, 'https://matrix.org');
Irssi::settings_add_str($settings_section, $settings_str{'room'}, '');
Irssi::settings_add_bool($settings_section, $settings_str{'away_only'}, 0);
Irssi::settings_add_bool($settings_section, $settings_str{'priv_msgs'}, 0);
Irssi::settings_add_bool($settings_section, $settings_str{'mentions'}, 0);
load_settings();
# Same callback function for mentions and public messages
if ($settings_mentions) {
Irssi::signal_add_last('message public', 'handle_pub_msgs');
}
if ($settings_priv_msgs) {
Irssi::signal_add_last('message private', 'handle_priv_msgs');
}
Irssi::signal_add_last('setup changed', \&load_settings);
Irssi::printformat(Irssi::MSGLEVEL_CLIENTCRAP,
'script_loaded',
$IRSSI{'name'}, $VERSION);
}
init();