-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsession.pl
531 lines (400 loc) · 11.9 KB
/
session.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use common::sense;
#only use threads if we can
my $threads = eval {
use threads;
use Thread::Queue;
1;
};
use Xchat;
use Storable qw|nstore retrieve freeze thaw|;
use File::Spec;
use Data::Dumper;
#automatically save sessions
my $autosave = 1;
#warn if not restoring session automatically
my $warn = 1;
#notify when sessions are being automatically saved
my $notify = 1;
#restore tabs to users
my $restore_queries = 1;
my $restore_nicks = 1;
#delay between connecting to each server
my $delay = 1000;
#file to store session
my $file = File::Spec->catfile(Xchat::get_info('configdir'), 'session.dat');
Xchat::register 'Session', '1.01', 'Restores your last used networks and channels.', \&unload;
Xchat::hook_command 'restore', \&restore;
Xchat::hook_command 'save', \&save;
if ($autosave) {
#automatically save changes when...
Xchat::hook_print $_, \&change for
#a tab is opened or closed
'Open Context', 'Close Context',
#a channel key is changed
'Channel Remove Keyword', 'Channel Set Key',
'Channel Modes',
#your nick is changed
($restore_nicks ? 'Your Nick Changing' : ()),
;
#a network name is seen (for networks not in the network list)
Xchat::hook_server '005', \&change;
}
Xchat::hook_print 'Connecting', \&connecting_info;
Xchat::hook_print 'SSL Message', \&ssl_info;
#avoid saving too frequently
my $last_hook;
my $last_change;
#info for channels not in the network list
my $info = { };
#channels to join
my $join = [ ];
#avoid saving session for a while after loading this script
my $connecting = 1;
#motd hook is stored here uppon successfully restoring
my $motd;
#initialize threads
my $queue = Thread::Queue->new() if $threads;
my $thr = threads->create(\&worker) if $threads;
load();
sub change {
#don't save during start up / connecting
if ($connecting) {
return Xchat::EAT_NONE;
}
#avoid saving too frequently
if (time - $last_change < 2 && $last_hook) {
Xchat::unhook $last_hook;
}
#make sure changes take effect before saving
$last_hook = Xchat::hook_timer 1_000, sub {
save();
return Xchat::REMOVE;
};
$last_change = time;
return Xchat::EAT_NONE;
}
sub save {
my $session = [ ];
my $ids = { };
#separate networks and their dialogs by ids
for (Xchat::get_list 'channels') {
next if !$_->{'network'} || !$_->{'channel'};
push @{ $ids->{ $_->{'id'} } //= [ ] }, $_;
}
#sort by network or by id
my @sorted_ids = sort {
fc $ids->{ $a }[0]{'network'} cmp fc $ids->{ $b }[0]{'network'} ||
$ids->{ $a }[0]{'id'} <=> $ids->{ $b }[0]{'id'}
} keys $ids;
my @seen_ids; #create a list of seen ids and delete the ones not seen (closed)
network:
for (0.. $#sorted_ids) {
my $index = $_;
#put server tabs first
my @channels = sort { $a->{'type'} <=> $b->{'type'} } @{ $ids->{ $sorted_ids[ $index ] } };
my $sess = { }; #server session
for (@channels) {
my $channel = $_->{'channel'};
#network tabs
if ($_->{'type'} == 1) {
my $id = $_->{'id'};
my $network = $_->{'network'};
#avoid saving a network that doesn't have a network name (most likely disconnected)
next network if !keys get_network($network) && !$info->{ $id };
#connection info for networks not in the network list
if (exists $info->{ $id }) {
$sess->{ $_ } = '' . $info->{ $id }{ $_ } for qw|host port ssl|;
}
push @seen_ids, $id;
$sess->{'nick'} = Xchat::context_info($_->{'context'})->{'nick'};
$sess->{'network'} = $_->{'network'};
}
#channel tabs
elsif ($_->{'type'} == 2) {
my $key = $_->{'channelkey'};
push @{ $sess->{'channels'} //= [ ] }, $channel . ($key ? " $key" : ());
}
#query tabs
elsif ($_->{'type'} == 3) {
push @{ $sess->{'queries'} //= [ ] }, $channel;
}
}
#sort the channels by name
@{ $sess->{'channels'} } = sort @{ $sess->{'channels'} };
#generate join strings
$sess->{'channels'} = (grep { / / } @{ $sess->{'channels'} }) ? #check if any have keys
join_with_key(@{ $sess->{'channels'} }) :
join_no_key (@{ $sess->{'channels'} })
;
push $session, $sess;
}
#remove old ids
for my $id (keys $info) {
delete $info->{ $id } if !grep { $_ == $id } @seen_ids;
}
if ($threads) {
$queue->enqueue([ freeze($session), $file ]);
}
else {
nstore $session, $file;
}
return 1;
}
sub load {
#check if there are any tabs with channel (existing tab) in case the script is reloaded
if (grep { $_->{'channel'} } Xchat::get_list 'channels') {
$connecting = 0;
if ($warn) {
Xchat::print "*\tOpen tabs detected. Restoring sessions with active tabs is not tested and is not recommended.";
Xchat::print "*\tAny changes made will be saved. Networks not in the network list won't be saved correctly unless you reconnect to them.";
Xchat::print "*\tUse /restore to force restore the session and enable saving.";
}
notify() if $notify && $autosave;
return 0;
}
restore();
return 1;
}
sub restore {
if (!-e $file) {
$connecting = 0;
Xchat::print "*\tNo session file found!";
notify() if $notify && $autosave;
return 0;
}
eval {
my $session = retrieve $file;
#restoring session
my $n = 0;
for my $sess (@$session) {
#/url doesn't reuse the first tab, so we have to use /server for it
#and making sure the current tab is being used
if ($n == 0 && !Xchat::context_info->{'network'}) {
Xchat::hook_timer $n * $delay, sub {
connect_first($sess);
Xchat::REMOVE;
};
$n++;
next;
}
Xchat::hook_timer $n * $delay, sub {
_connect($sess);
Xchat::REMOVE;
};
$n++;
}
#put info to know what to restore on 376/422
$join = $session;
#remove saving restriction after 1 minute + $delay for every server
#it should be enough to connect to most servers
Xchat::hook_timer 60_000 + ($delay * scalar @$session), sub {
$connecting = 0;
notify() if $notify && $autosave;
return Xchat::REMOVE;
};
#join channels on motd end
$motd = Xchat::hook_server $_, \&join_channels, for qw|376 422|;
};
if ($@) {
$connecting = 0;
Xchat::print "*\tCould not restore session ($@)";
notify() if $notify && $autosave;
return 0;
}
return 1;
}
sub join_channels {
my $context = Xchat::get_context;
my $id = Xchat::get_info 'id';
my $network = Xchat::get_info 'network';
#find same networks and sort by id
my @channels =
sort { $a->{'id'} <=> $b->{'id'} }
grep {
$_->{'type'} == 1 && $_->{'network'} eq $network;
}
Xchat::get_list 'channels'
;
#postpone restoring if earlier connections don't have a network name or haven't received one yet
my $not_connected = grep {
my $network = $_->{'network'};
$_->{'type'} == 1 &&
$_->{'id'} < $id &&
#basically check if the current network can be found in the $join
!grep { $_->{'network'} eq $network } @$join
} Xchat::get_list 'channels';
if ($not_connected) {
Xchat::hook_timer 10_000, sub {
state $n = 0; $n++;
#remove timer if context is removed or two minutes (plus 1s per network) have passed
return Xchat::REMOVE if !Xchat::set_context $context;
return Xchat::REMOVE if $n >= 12 + (scalar @$join / 10);
join_channels();
return Xchat::REMOVE;
};
return Xchat::EAT_NONE;
}
#find our offset by id (networks connected earlier have smaller ids)
my $offset = 0;
for (@channels) {
$offset++;
last if $_->{'id'} == $id;
}
#e.g. the first connection to a network will both have a smaller id
#and it's session will come before the next one
my $index = 0;
for (@$join) {
$index++;
#decrease offset for each same network we see
$offset-- if $_->{'network'} eq $network;
last if $offset == 0;
}
$index--;
my $restore = $join->[ $index ];
if (exists $restore->{'channels'}) {
Xchat::command "join $_" for @{ $restore->{'channels'} };
delete $restore->{'channels'};
}
if ($restore_queries && exists $restore->{'queries'}) {
Xchat::command "query -nofocus $_" for @{ $restore->{'channels'} };
delete $restore->{'queries'};
}
if ($restore_nicks && exists $restore->{'nick'}) {
Xchat::command "nick $restore->{'nick'}";
delete $restore->{'nick'};
}
$join->[ $index ] = {
'network' => $join->[ $index ]{'network'},
};
#remove hook if we have restored everything
if (!grep { exists $_->{'channels'} } @$join) {
Xchat::unhook $motd;
}
return Xchat::EAT_NONE;
}
sub worker {
while (defined(my $data = $queue->dequeue())) {
my ($session, $file) = @$data;
nstore thaw($session), $file;
}
return 1;
}
sub unload {
if ($threads) {
$queue->end();
$thr->join();
}
return Xchat::EAT_NONE;
}
#save connecting information if the network is not in the network list
sub connecting_info {
my ($host, $ip, $port) = @{ $_[0] };
my $id = Xchat::context_info->{'id'};
if (!Xchat::context_info->{'network'} || !keys get_network(Xchat::context_info->{'network'})) {
$info->{ $id } = {
'host' => $host,
'port' => $port,
};
}
return Xchat::EAT_NONE;
};
#same as above but for ssl and only check during the first ssl message
sub ssl_info {
my $id = Xchat::context_info->{'id'};
if (exists $info->{ $id }) {
$info->{ $id }{'ssl'} = 1;
}
return Xchat::EAT_NONE;
};
#command used to connect on an unused tab
sub connect_first {
my ($session) = @_;
my ($host, $port, $ssl) = map { $session->{ $_ } } qw|host port ssl|;
my $network = get_network($session->{'network'});
#network is in the network list
if (keys $network) {
$host = $network->{'servers'}[0]{'host'};
$port = $network->{'servers'}[0]{'port'};
$ssl = $network->{'flags'}{'use_ssl'};
$ssl = 1 if $port =~ s/^\+//;
}
$ssl = $ssl ? ' -ssl ' : ' ';
Xchat::command "server$ssl$host $port";
return 1;
}
#commaned used to connect with new tabs being created
sub _connect {
my ($session) = @_;
if (keys get_network($session->{'network'})) {
Xchat::command "url irc://\"$session->{'network'}\"/";
}
else {
my ($host, $port) = map { $session->{ $_ } } qw|host port|;
$port = "+$port" if $session->{'ssl'};
Xchat::command "url irc://\"$host:$port\"";
}
return 1;
}
#check if a network is in the network list
sub get_network {
my ($network_name) = @_;
my ($network) = grep { $_->{'network'} eq $network_name } Xchat::get_list 'networks';
return $network if $network && keys $network;
return { };
}
#create a join string for channels at least one of which has a key
sub join_with_key {
return [ ] if !@_; #avoid creating an undef item if no channels are joined
my @channels = @_;
my @parts;
my @current_channels;
my @current_keys;
for (@channels) {
my ($channel, $key) = split / /, $_;
#recreate the string to check the new lengh
my $channels_str = join_channels_keys([ @current_channels, $channel ], [ @current_keys, $key ]);
#505 = 512 - "\r\n" - "JOIN "
if (length $channels_str > 505) {
push @parts, join_channels_keys(\@current_channels, \@current_keys);
@current_channels = ($channel);
@current_keys = ($key);
next;
}
push @current_channels, $channel;
push @current_keys, $key;
}
push @parts, join_channels_keys(\@current_channels, \@current_keys);
return [ @parts ];
}
#create a join string for channel none of which have a key
#basically join them with a comma and make sure they're not too long
sub join_no_key {
return [ ] if !@_;
my @channels = @_;
my @parts;
my $current_part = shift @channels;
for (@channels) {
if (length "$current_part,$_" > 505) {
push @parts, $current_part;
$current_part = $_;
next;
}
$current_part .= ",$_";
}
push @parts, $current_part;
return [ @parts ];
}
#create a string with fillers in the following format "#channelnokey,#channelkey x,key"
#using x as a filler for channels that don't have a key
sub join_channels_keys {
my @channels = @{ +shift };
my @keys = @{ +shift };
#if no keys are provided, return shorter string
return join_no_key(@channels) if !grep { $_ } @keys;
#make sure we have filler key
@keys = map { $_ || 'x' } @keys;
return join(',', @channels) . ' ' . join(',', @keys);
}
sub notify {
return Xchat::print 'Sessions are now being automatically saved.';
}