#!/usr/bin/perl -w # Copyright (c) 2010 Fabian Kurz, DJ1YFK # # 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. # # Win-Test Gab Window <-> IRC bridge # # http://fkurz.net/ham/stuff.html#irc2wt # # Broadcast address: my $bc = "192.168.0.255"; # # Your call: my $mycall = "N0CALL"; # # IRC server: my $server = "irc.freenode.net"; # # Channel: my $channel = "#test12345"; # # use Net::IRC; use IO::Socket; use strict; my $udprx = IO::Socket::INET->new( LocalPort => '9871', Proto => "udp", Blocking => 0, PeerPort => 9871, ReuseAddr => 1, Timeout => 1) or die "rx: $@\n"; my $udptx = IO::Socket::INET->new( LocalPort => '9872', Proto => "udp", Blocking => 1, PeerPort => 9871, Broadcast => 1, PeerAddr => $bc, ReuseAddr => 1 ) or die "tx: $@\n"; my $irc = new Net::IRC; my $conn = $irc->newconn( Server => $server, Port => '6667', Nick => $mycall, Ircname => 'WT<->IRC gateway', Username => 'wt2irc' ); $conn->{channel} = $channel; sub on_connect { my $conn = shift; $conn->join($conn->{channel}); $conn->privmsg($conn->{channel}, 'WinTest <> IRC gateway established.'); $conn->{connected} = 1; } sub on_public { my ($conn, $event) = @_; my $text = normalize($event->{args}[0]); my $nick = $event->{nick}; $text =~ s/"/'/g; my $msg = "GAB: \"IRC\" \"\" \"$nick: $text\""; $udptx->send($msg.chr(checksum($msg))); } sub on_join { my ($conn, $event) = @_; my $nick = $event->{nick}; my $msg = "GAB: \"IRC\" \"\" \"$nick joined...\""; $udptx->send($msg.chr(checksum($msg))); } sub on_quit { my ($conn, $event) = @_; my $nick = $event->{nick}; my $msg = "GAB: \"IRC\" \"\" \"$nick left...\""; $udptx->send($msg.chr(checksum($msg))); } $conn->add_handler('376', \&on_connect); $conn->add_handler('public', \&on_public); $conn->add_handler('join', \&on_join); $conn->add_handler('leaving', \&on_quit); $conn->add_handler('part', \&on_quit); my $data; while (1) { $udprx->recv($data,256); if ($data) { print $data."\n"; } if ($data =~ /^GAB:/) { next if ($data =~ /GAB: "IRC"/); my @tmp = split(/\s+/, $data, 4); $tmp[1] =~ s/"//g; substr($tmp[3],-3,) = ""; substr($tmp[3],0,1) = ""; $conn->privmsg($conn->{channel}, $tmp[1].": ".$tmp[3]); } elsif ($data =~ /^SUMMARY.+SCORE.+\s(\d+)/) { $conn->privmsg($conn->{channel}, "Current score: $1"); } $irc->do_one_loop(); } sub checksum () { my $d = shift; my $sum = 0; for (0..length($d)) { $sum += ord(substr($d, $_, 1)); } $sum = $sum % 128; $sum += 128; return $sum; } # Remove non-ASCII stuff sub normalize { my $t = shift; for (0..length($t)) { if (ord(substr($t, $_, 1)) > ord('z')) { substr($t, $_, 1) = "_"; } } return $t; }