This file is indexed.

/usr/share/irssi/scripts/linkchan.pl is in irssi-scripts 20120326.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = "1.5";
%IRSSI =
(
    authors     => 'Marcin \'Qrczak\' Kowalczyk',
    contact     => 'qrczak@knm.org.pl',
    name        => 'LinkChan',
    description => 'Link several channels on serveral networks',
    license     => 'GNU GPL',
    url         => 'http://qrnik.knm.org.pl/~qrczak/irssi/linkchan.pl',
);

our %links;
our $lock_own = 0;

our $config = Irssi::get_irssi_dir . "/linkchan.cfg";

Irssi::command_bind "link", sub
{
    my ($args, $server, $target) = @_;
    Irssi::command_runsub "link", $args, $server, $target;
};

Irssi::command_bind "link add", sub
{
    my ($args, $server, $target) = @_;
    unless ($args =~ m|^ *([^ /]+)/([^ ]+) +([^ /]+)/([^ ]+) *$|)
    {
        print CLIENTERROR "Usage: /link add <chatnet1>/<channel1> <chatnet2>/<channel2>";
        return;
    }
    my ($chatnet1, $channel1, $chatnet2, $channel2) =
      (lc $1, lc $2, lc $3, lc $4);
    foreach my $link ([$chatnet1, $channel1], [$chatnet2, $channel2])
    {
        my ($chat1, $chan1) = @{$link};
        if ($links{$chat1}{$chan1})
        {
            my ($chat2, $chan2) = @{$links{$chat1}{$chan1}};
            print CLIENTERROR "Channel $chat1/$chan1 is already linked to $chat2/$chan2";
            return;
        }
    }
    $links{$chatnet1}{$channel1} = [$chatnet2, $channel2];
    $links{$chatnet2}{$channel2} = [$chatnet1, $channel1];
    print CLIENTNOTICE "Added link: $chatnet1/$channel1 <-> $chatnet2/$channel2";
};

Irssi::command_bind "link remove", sub
{
    my ($args, $server, $target) = @_;
    unless ($args =~ m|^ *([^ /]+)/([^ ]+) *$|)
    {
        print CLIENTERROR "Usage: /link remove <chatnet>/<channel>";
        return;
    }
    my ($chatnet1, $channel1) = (lc $1, lc $2);
    unless ($links{$chatnet1}{$channel1})
    {
        print CLIENTERROR "Channel $chatnet1/$channel1 was not linked";
        return;
    }
    my ($chatnet2, $channel2) = @{$links{$chatnet1}{$channel1}};
    delete $links{$chatnet1}{$channel1};
    delete $links{$chatnet2}{$channel2};
    print CLIENTNOTICE "Removed link: $chatnet1/$channel1 <-> $chatnet2/$channel2";
};

Irssi::command_bind "link list", sub
{
    my ($args, $server, $target) = @_;
    unless ($args =~ /^ *$/)
    {
        print CLIENTNOTICE "Usage: /link list";
        return;
    }
    print CLIENTNOTICE "The following pairs of channels are linked:";
    my %shown = ();
    foreach my $chatnet1 (sort keys %links)
    {
        foreach my $channel1 (sort keys %{$links{$chatnet1}})
        {
            next if $shown{$chatnet1}{$channel1};
            my ($chatnet2, $channel2) = @{$links{$chatnet1}{$channel1}};
            print CLIENTNOTICE "$chatnet1/$channel1 <-> $chatnet2/$channel2";
            $shown{$chatnet2}{$channel2} = 1;
        }
    }
};

sub save_config()
{
    open CONFIG, ">$config";
    foreach my $chatnet1 (keys %links)
    {
        foreach my $channel1 (keys %{$links{$chatnet1}})
        {
            my ($chatnet2, $channel2) = @{$links{$chatnet1}{$channel1}};
            print CONFIG "$chatnet1/$channel1 $chatnet2/$channel2\n";
        }
    }
    close CONFIG;
}

Irssi::signal_add "setup saved", sub
{
    my ($main_config, $auto) = @_;
    save_config unless $auto;
};

sub load_config()
{
    %links = ();
    open CONFIG, $config or return;
    while (<CONFIG>)
    {
        chomp;
        next if /^ *$/ || /^#/;
        unless (m|^ *([^ /]+)/([^ ]+) +([^ /]+)/([^ ]+) *$|)
        {
            print CLIENTERROR "Syntax error in $config: $_";
            return;
        }
        my ($chatnet1, $channel1, $chatnet2, $channel2) =
          (lc $1, lc $2, lc $3, lc $4);
        $links{$chatnet1}{$channel1} = [$chatnet2, $channel2];
    }
}

Irssi::signal_add "setup reread", \&load_config;

sub message($$)
{
    my ($chan, $msg) = @_;
    $lock_own = 1;
    $chan->{server}->command("msg $chan->{name} $msg");
    $lock_own = 0;
}

sub special_message($$)
{
    my ($chan, $msg) = @_;
    message $chan, "-!- $msg";
}

sub special_message_for($$$)
{
    my ($chan, $nick, $msg) = @_;
    message $chan,
      (defined $nick ? "$nick: " : "") .
      "-!- $msg";
}

sub channel_context($$)
{
    my ($server1, $channel1) = @_;
    my $chatnet1 = lc $server1->{chatnet};
    my $chan1 = $server1->channel_find($channel1) or return undef;
    my $other = $links{$chatnet1}{lc $channel1} or return undef;
    my ($chatnet2, $channel2) = @{$other};
    my $server2 = Irssi::server_find_chatnet($chatnet2) or return;
    my $chan2 = $server2->channel_find($channel2) or return;
    return {
        chatnet1 => $chatnet1,
        server1  => $server1,
        channel1 => $channel1,
        chan1    => $chan1,
        chatnet2 => $chatnet2,
        server2  => $server2,
        channel2 => $channel2,
        chan2    => $chan2,
    };
}

sub channel_contexts_with_nick($$)
{
    my ($server1, $nick1) = @_;
    my $chatnet1 = lc $server1->{chatnet};
    return () unless $links{$chatnet1};
    my @contexts = ();
    foreach my $channel1 (keys %{$links{$chatnet1}})
    {
        my $chan1 = $server1->channel_find($channel1) or next;
        next unless $chan1->nick_find($nick1);
        my ($chatnet2, $channel2) = @{$links{$chatnet1}{$channel1}};
        my $server2 = Irssi::server_find_chatnet($chatnet2) or next;
        my $chan2 = $server2->channel_find($channel2) or next;
        push @contexts, {
            chatnet1 => $chatnet1,
            server1  => $server1,
            channel1 => $channel1,
            chan1    => $chan1,
            chatnet2 => $chatnet2,
            server2  => $server2,
            channel2 => $channel2,
            chan2    => $chan2,
        };
    }
    return @contexts;
}

sub must_be_op($$)
{
    my ($context, $nick) = @_;
    unless (defined $nick ?
            $context->{chan1}->nick_find($nick)->{op} :
            $context->{chan1}->{chanop})
    {
        special_message_for $context->{chan1}, $nick,
          "You're not channel operator in $context->{channel1}";
        return 0;
    }
    unless ($context->{chan2}->{chanop})
    {
        special_message_for $context->{chan1}, $nick,
          "Sorry, I'm not channel operator in $context->{channel2}";
        return 0;
    }
    return 1;
}

sub change_mode($$$)
{
    my ($context, $nick, $mode) = @_;
    return unless must_be_op($context, $nick);
    special_message $context->{chan2},
      "mode/$context->{channel2} [$mode] by $nick"
      if defined $nick;
    $context->{server2}->command("mode $context->{channel2} $mode");
}

sub change_perms($$$$$$)
{
    my ($command, $dir, $mode, $context, $nick, $args) = @_;
    my @nicks = split ' ', $args;
    unless (@nicks)
    {
        special_message_for $context->{chan1}, $nick,
          "Usage: \\$command <nicks>";
        return;
    }
    change_mode $context, $nick, $dir . $mode x @nicks . " @nicks";
}

sub names($$$)
{
    my ($context, $nick, $args) = @_;
    my @nicks = $context->{chan2}->nicks();
    my @ops = grep {$_->{op}} @nicks;
    my @voices = grep {!$_->{op} && $_->{voice}} @nicks;
    my @normal = grep {!$_->{op} && !$_->{voice}} @nicks;
    my @list = (
      map ({['@', $_]} sort {lc $a cmp lc $b} map {$_->{nick}} @ops),
      map ({['+', $_]} sort {lc $a cmp lc $b} map {$_->{nick}} @voices),
      map ({[' ', $_]} sort {lc $a cmp lc $b} map {$_->{nick}} @normal));
    my $max_width = 62 - length $context->{server1}->{nick};
    my $rows = 1;
    my @column_widths;
    while ($rows < @list)
    {
        @column_widths = ();
        my $width = 0;
        my $i = 0;
        while ($i < @list)
        {
            my $column_width = 0;
            foreach my $j ($i .. $i+$rows-1)
            {
                last if $j >= @list;
                my $len = length $list[$j][1];
                $column_width = $len if $column_width < $len;
            }
            push @column_widths, $column_width;
            $width += $column_width + 4;
            $i += $rows;
        }
        last if $width - 1 <= $max_width;
        ++$rows;
    }
    my @output;
    foreach my $i (0..$#list)
    {
        $output[$i % $rows] .=
          sprintf "[%s%*s] ",
          $list[$i][0], -$column_widths[int ($i / $rows)], $list[$i][1];
    }
    foreach my $row (@output)
    {
        chop $row;
        message $context->{chan1}, $row;
    }
}

my %commands =
(
    mode => sub
    {
        my ($context, $nick, $args) = @_;
        unless ($args =~ /^ +\* +(.*)$/ ||
                $args =~ /^ +\Q$context->{channel2}\E +(.*)$/)
        {
            special_message_for $context->{chan1}, $nick,
              "Usage: \\mode * <mode> [<mode parameters>]";
            return;
        }
        change_mode $context, $nick, $1;
    },
    op => sub {&change_perms('op', '+', 'o', @_)},
    deop => sub {&change_perms('deop', '-', 'o', @_)},
    voice => sub {&change_perms('voice', '+', 'v', @_)},
    devoice => sub {&change_perms('devoice', '-', 'v', @_)},
    kick => sub
    {
        my ($context, $nick, $args) = @_;
        unless ($args =~ /^ +([^ ]+)(| .*)$/)
        {
            special_message_for $context->{chan1}, $nick,
              "Usage: \\kick <nicks> [<reason>]";
            return;
        }
        my ($nicks, $reason) = ($1, $2);
        $reason = $reason =~ /^ ?$/ ? " $nick" : " <$nick>$reason"
          if defined $nick;
        return unless must_be_op($context, $nick);
        $context->{server2}->command("kick $context->{channel2} $nicks$reason");
    },
    names => \&names,
);

sub run_command($$$$)
{
    my ($context, $nick, $command, $args) = @_;
    my $func = $commands{lc $command};
    unless ($func)
    {
        special_message_for $context->{chan1}, $nick,
          "Unknown command: $command";
        return;
    }
    $func->($context, $nick, $args);
}

Irssi::signal_add "message public", sub
{
    my ($server1, $msg, $nick, $address, $channel1) = @_;
    my $context = channel_context($server1, $channel1) or return;
    if ($msg =~ /^\\([^ ]+)(| .*)$/)
    {
        Irssi::signal_continue @_;
        run_command $context, $nick, $1, $2;
    }
    elsif ($msg =~ /^<.[^ ]+> /)
    {
        print CLIENTERROR
          "Warning! Channels $context->{chatnet1}/$context->{channel1} " .
          "and $context->{chatnet2}/$context->{channel2} are linked twice.";
        Irssi::command "beep";
    }
    else
    {
        my $nk = $context->{chan1}->nick_find($nick);
        my $perm = $nk->{op} ? '@' : $nk->{voice} ? '+' : ' ';
        message $context->{chan2}, "<$perm$nick> $msg";
    }
};

Irssi::signal_add "message own_public", sub
{
    my ($server1, $msg, $channel1) = @_;
    return if $lock_own;
    my $context = channel_context($server1, $channel1) or return;
    if ($msg !~ s/^\\ // && $msg =~ /^\\([^ ]+)(| .*)$/)
    {
        Irssi::signal_continue @_;
        run_command $context, undef, $1, $2;
    }
    else
    {
        message $context->{chan2}, $msg;
    }
};

Irssi::signal_add "message irc action", sub
{
    my ($server1, $msg, $nick, $address, $channel1) = @_;
    my $context = channel_context($server1, $channel1) or return;
    message $context->{chan2}, " * $nick $msg";
};

Irssi::signal_add "message irc own_action", sub
{
    my ($server1, $msg, $channel1) = @_;
    return if $lock_own;
    my $context = channel_context($server1, $channel1) or return;
    $lock_own = 1;
    $context->{server2}->command("action $context->{channel2} $msg");
    $lock_own = 0;
};

Irssi::signal_add "message join", sub
{
    my ($server1, $channel1, $nick, $address) = @_;
    my $context = channel_context($server1, $channel1) or return;
    special_message $context->{chan2},
      "$nick [$address] has joined $channel1";
};

Irssi::signal_add "message part", sub
{
    my ($server1, $channel1, $nick, $address, $reason) = @_;
    my $context = channel_context($server1, $channel1) or return;
    special_message $context->{chan2},
      "$nick [$address] has left $context->{channel1} [$reason]";
};

Irssi::signal_add "message quit", sub
{
    my ($server1, $nick, $address, $reason) = @_;
    foreach my $context (channel_contexts_with_nick($server1, $nick))
    {
        special_message $context->{chan2},
          "$nick [$address] has quit [$reason]";
    }
};

Irssi::signal_add "message topic", sub
{
    my ($server1, $channel1, $topic, $nick, $address) = @_;
    return if $nick eq $server1->{nick};
    my $context = channel_context($server1, $channel1) or return;
    if ($topic eq "")
    {
        special_message $context->{chan2},
          "Topic unset by $nick on $context->{channel1}";
        $context->{server2}->command("topic -delete $context->{channel2}");
    }
    else
    {
        special_message $context->{chan2},
          "$nick changed the topic of $context->{channel1} to: $topic";
        $context->{server2}->command("topic $context->{channel2} $topic");
    }
};

Irssi::signal_add "message nick", sub
{
    my ($server1, $newnick, $oldnick, $address) = @_;
    foreach my $context (channel_contexts_with_nick($server1, $newnick))
    {
        special_message $context->{chan2},
          "$oldnick is now known as $newnick";
    }
};

Irssi::signal_add "message own_nick", sub
{
    my ($server1, $newnick, $oldnick, $address) = @_;
    foreach my $context (channel_contexts_with_nick($server1, $newnick))
    {
        next if $context->{chatnet1} eq $context->{chatnet2};
        special_message $context->{chan2},
          "$oldnick is now known as $newnick";
    }
};

Irssi::signal_add "message kick", sub
{
    my ($server1, $channel1, $nick, $kicker, $address, $reason) = @_;
    my $context = channel_context($server1, $channel1) or return;
    special_message $context->{chan2},
      "$nick was kicked from $context->{channel1} " .
      "by $kicker [$reason]";
};

Irssi::signal_add "event mode", sub
{
    my ($server1, $data, $nick) = @_;
    $data =~ /^([^ ]*) (.*)$/ or return;
    my ($channel1, $mode) = ($1, $2);
    my $context = channel_context($server1, $channel1) or return;
    special_message $context->{chan2},
      "mode/$context->{channel1} [$mode] by $nick";
};

load_config;