1#!/usr/local/bin/perl
2#
3
4use strict;
5use vars qw($VERSION %IRSSI);
6$VERSION = '2003071904';
7%IRSSI = (
8    authors     => 'Stefan \'tommie\' Tomanek',
9    contact     => 'stefan@pico.ruhr.de',
10    name        => 'Forward',
11    description => 'forward incoming messages to another nick',
12    license     => 'GPLv2',
13    url         => 'http://irssi.org/scripts/',
14    changed     => $VERSION,
15    modules     => '',
16    commands    => "forward"
17);
18
19use Irssi 20020324;
20
21use vars qw(%forwards);
22
23sub show_help() {
24    my $help = $IRSSI{name}." ".$VERSION."
25/forward to <nick>
26    Forward incoming messages to <nick>
27/forward remove
28    Disable forwarding in the current chatnet
29
30You can remotely en- or disable forwarding by sending an
31ctcp command to your client. Set a password and use
32 /CTCP <nickname> forward <password>
33or
34 /CTCP <nickname> noforward
35to enable or diable forwarding to your current nick.
36";
37    my $text='';
38    foreach (split(/\n/, $help)) {
39        $_ =~ s/^\/(.*)$/%9\/$1%9/;
40        $text .= $_."\n";
41    }
42    print CLIENTCRAP &draw_box($IRSSI{name}, $text, $IRSSI{name}." help", 1);
43}
44
45sub draw_box ($$$$) {
46    my ($title, $text, $footer, $colour) = @_;
47    my $box = '';
48    $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
49    foreach (split(/\n/, $text)) {
50        $box .= '%R|%n '.$_."\n";
51    }
52    $box .= '%R`--<%n'.$footer.'%R>->%n';
53    $box =~ s/%.//g unless $colour;
54    return $box;
55}
56
57sub sig_message_private ($$$$) {
58    my ($server, $msg, $nick, $address) = @_;
59    my $chatnet = $server->{chatnet};
60    return unless defined $forwards{$chatnet};
61    if ($forwards{$chatnet}{active}) {
62	my $to = $forwards{$chatnet}{to};
63	my $text = "[forwarded MSG from ".$nick."] ".$msg;
64	$server->command("notice $to ".$text);
65    }
66}
67
68sub sig_ctcp_msg_forward ($$$$$) {
69    my ($server, $args, $nick, $address, $target) = @_;
70    my $pass = Irssi::settings_get_str('forward_remote_password');
71    unless ($pass) {
72	print CLIENTCRAP '%R>>%n No forward password set, forwarding not enabled!';
73	$server->command("nctcp ".$nick." FORWARD Forwarding forbidden!");
74	return 0;
75    }
76    if ($pass eq $args) {
77	$server->command("nctcp ".$nick." FORWARD Forwarding enabled");
78	set_forward($server->{chatnet}, $nick);
79    }
80}
81
82sub sig_ctcp_msg_noforward ($$$$$) {
83    my ($server, $args, $nick, $address, $target) = @_;
84    my $chatnet = $server->{chatnet};
85    return unless defined $forwards{$chatnet};
86    return unless ($forwards{$chatnet}{to} eq $nick);
87    $server->command("nctcp ".$nick." NOFORWARD Forwarding disabled");
88    remove_forward($server->{chatnet});
89}
90
91
92sub set_forward ($$) {
93    my ($chatnet, $nick) = @_;
94    print CLIENTCRAP "%B>>%n Forwarding messages from $chatnet to > $nick <";
95    $forwards{$chatnet}{to} = $nick;
96    $forwards{$chatnet}{active} = 1;
97}
98
99sub remove_forward ($) {
100    my ($chatnet) = @_;
101    delete $forwards{$chatnet};
102    print CLIENTCRAP "%B>>%n No longer forwarding messages from $chatnet";
103}
104
105sub cmd_forward ($$$) {
106    my ($arg, $server, $witem) = @_;
107    return unless defined $server;
108    my @args = split(/ /, $arg);
109    if (@args < 1 || $args[0] eq 'help') {
110	show_help();
111    } elsif (@args[0] eq 'to') {
112	shift @args;
113	return unless @args;
114	set_forward($server->{chatnet}, $args[0]);
115    } elsif (@args[0] eq 'remove') {
116	remove_forward($server->{chatnet});
117    }
118}
119
120
121Irssi::signal_add('message private', \&sig_message_private);
122Irssi::signal_add('ctcp msg forward', \&sig_ctcp_msg_forward);
123Irssi::signal_add('ctcp msg noforward', \&sig_ctcp_msg_noforward);
124Irssi::settings_add_str($IRSSI{name}, 'forward_remote_password', '');
125
126Irssi::command_bind('forward' => \&cmd_forward);
127
128print CLIENTCRAP '%B>>%n '.$IRSSI{name}.' '.$VERSION.' loaded: /forward help for help';
129