1use strict;
2use vars qw/%IRSSI $VERSION/;
3use Irssi qw(command_bind active_server);
4
5$VERSION= "0.1";
6%IRSSI = (
7	authors     => "David Leadbeater",
8	contact     => "dgl\@dgl.cx",
9	name        => "dccself",
10	description => "/dccself ip port, starts a dcc chat with yourself on that
11	                host/port, best used with /set dcc_autochat_masks.",
12	license     => "GPL",
13);
14
15# I tried using Juerd's style for this script - seems to make it easier to read
16# :)
17
18command_bind('dccself', sub {
19   my $data = shift;
20	my($ip,$port) = split / |:/, $data, 2;
21
22   return unless ref active_server;
23   my $nick = active_server->{nick};
24   $ip = dcc_ip($ip);
25   active_server->command("ctcp $nick DCC CHAT CHAT $ip $port");
26} );
27
28sub dcc_ip {
29   my $ip = shift;
30   # This could block!
31   $ip = sprintf("%d.%d.%d.%d", unpack('C4',(gethostbyname($ip))[4]))
32       unless $ip =~ /\d$/;
33
34   my @a = split /\./, $ip, 4;
35   # Thanks to perlguy/grifferz/AndrewR
36   return $a[0]*0x1000000 + $a[1]*0x10000 + $a[2]*0x100 + $a[3];
37}
38
39