By request: Perl script to connect MQ telnet to IRC

A forum for the general posts relating to MacroQuest. *DEPRECATED: This forum is no longer in public use, but remains here for your reading pleasure. Enjoy

Moderator: MacroQuest Developers

Scrime
a ghoul
a ghoul
Posts: 86
Joined: Sun Sep 21, 2003 5:48 pm
Contact:

By request: Perl script to connect MQ telnet to IRC

Post by Scrime » Fri Oct 24, 2003 4:18 pm

You'll need some incarnation of Perl, the Telnet and IRC modules from CPAN, and you'll need to mod the script a bit for your machine. On my machine, I have two IPs so I had to tell it which to use (in $local_host). If you machine only has a single IP, then you can probably get by without using $local_host. If so, then you have to remove the bit about LocalAddr => $local_host in the IRC connection call.

It's not very secure, as it only uses your IRC nickname to identify who can send commands to MQ. I suggest that if you are going to use it seriously, that you implement some sort of password or id system. It also does not split up messages from MQ into different IRC channels, but to do so would be almost trivial if you spend a few minutes with it.

I make no claims about being a Perl guru, so if someone sees some things that I could have done in a much more efficient way it would not suprise me.

Code: Select all

#!/usr/bin/perl

#get the telnet mo1dule
use Net::Telnet;
#get the IRC module
use Net::IRC;

$local_host = "xxx.xxx.xxx.xxx";

# telnet information should be that of your machine running MQ
$telnet_host = "xxx.xxx.xxx.xxx";
$telnet_port = 23;
$telnet_password = "password";

$irc_nick = "botname";
$irc_host = "irc.forever-hacking.net";
$irc_chan = "#testing";
$irc_port = 6667;

# open the telnet connection
$telnet_conn = Net::Telnet->new( 	Host => $telnet_host,
					Port => $telnet_port );

$telnet_conn->waitfor('/Password:.*$/')
	or die "Didn't see Password:, saw: ", $telnet_conn->lastline;
$ok = $telnet_conn->print( $telnet_password );

$ok = $telnet_conn->waitfor('/Connected.*$/')
	or die "Didn't see Connected, saw: ", $telnet_conn->lastline;

print "Successfully connected to $telnet_host, on port $telnet_port\n";

print "connecting to $irc_host.\n";
# connect to the IRC server
$irc = new Net::IRC;
$irc->timeout( 300 );

$connected_to_irc = 0;
$irc_conn = $irc->newconn( 	Nick 	=> $irc_nick,
				Server 	=> $irc_host,
				Port 	=> $irc_port,
				LocalAddr => $local_host );

print "installing handlers.\n";
$irc_conn->add_global_handler('376', \&irc_on_connect);
$irc_conn->add_global_handler('422', \&irc_on_connect);
$irc_conn->add_handler('public', \&on_public);
$irc_conn->add_handler('msg', \&on_msg);

until( $connected_to_irc ) {
	 $irc->do_one_loop;
}

$telnet_conn->timeout( 1 );
$irc->timeout( 0 );
print "entering main loop.\n";

until ($telnet_conn->eof)
{
	# get any input from the telnet connection
	$line = $telnet_conn->getline( Errmode => 'return', Timeout => "0" );

	if( defined($line) ) {
		chomp $line;
		# remove the link highlighting codes: #######-#####-#####-#####-#####-################
		# 1234567-12345-12345-12345-12345-1234567890123456
		# 0006687-00001-00001-00001-00001-00001 02C0606F
		# 7-5-5-5-5-16 = 43bytes
		$line =~ s/[\dA-Fa-f]{7}-[\dA-Fa-f]{5}-[\dA-Fa-f]{5}-[\dA-Fa-f]{5}-[\dA-Fa-f]{5}-[\dA-Fa-f]{13}//g;
		print "$line\n";

		# mark any words surrounded by 0x12 as bold
		$line =~ s/\x12([^\x12]+)\x12/\x02$1\x02/g;

		if( $line =~ /out of character,/i ) {
			$colorline = "\x033 $line";
		} elsif( $line =~ /shouts,/i ) {
			$colorline = "\x034 $line";
		} elsif( $line =~ /auctions,/i ) {
			$colorline = "\x033 $line";
		} elsif( $line =~ /tells you,/i ) {
			$colorline = "\x036\x02 $line\x02";
		} elsif( $line =~ /tells the group,/i ) {

			$colorline = "\x0310 $line";
		} elsif( $line =~ /^LOADING, PLEASE WAIT...$/ ) {
			$colorline = "\x034\x02 $line\x02";
		} elsif( $line =~ /begins to cast a spell.$/ ) {
			$colorline = "\x037 $line";
		} elsif( $line =~ /^\[MacroQuest\]/ ) {
			$colorline = "\x0314 $line";
		} else {
			$colorline = "\x031 $line";
		}

		$irc_conn->privmsg( $irc_chan, $colorline );
	}

	# let the irc object process one round of events
	$irc->do_one_loop;
}

$telnet_conn->close;
print "telnet connection closed.\n";

$irc_conn->close;
print "IRC connection closed.\n";


sub irc_on_connect {
	my $self = shift;

	print "connected to $irc_host:$irc_port\n";
	print "changing nick to $irc_nick.\n";
	$self->nick( $irc_nick );
	print "joining $irc_chan\n";
	$self->join( $irc_chan );

	$connected_to_irc = 1;
}

sub on_public {
	my ($self, $event) = @_;
	my $nick = $event->from;
	my ($arg) = $event->args;

	print "<$nick> $arg\n";
	#if( $nick =~ /scrime/i ) {
	#	$telnet_conn->print("$arg");
	#}
}

sub on_msg {
	my ($self, $event) = @_;
	my $nick = $event->from;
	my ($arg) = $event->args;

	print "*$nick* $arg\n";
	if( $nick =~ /scrime/i ) {
		$telnet_conn->print("/$arg");
	}
}
Have fun and macro responsibly.

User avatar
dont_know_at_all
Developer
Developer
Posts: 5450
Joined: Sun Dec 01, 2002 4:15 am
Location: Florida, USA
Contact:

Post by dont_know_at_all » Fri Oct 24, 2003 4:32 pm

Hahahahaha. THis is great!

don'tdoit
a lesser mummy
a lesser mummy
Posts: 43
Joined: Tue Dec 31, 2002 12:24 pm

Post by don'tdoit » Fri Oct 24, 2003 7:30 pm

thanks for posting it...

User avatar
Curious
a ghoul
a ghoul
Posts: 88
Joined: Wed Aug 27, 2003 5:19 am

Post by Curious » Sat Oct 25, 2003 4:35 am

I thought that it was decided to remove perl functionality

Mckorr
Developer
Developer
Posts: 2326
Joined: Fri Oct 18, 2002 1:16 pm
Location: Texas

Post by Mckorr » Sat Oct 25, 2003 7:24 am

It isn't using the PERL functionality of MQ. It's using PERL as a programming language to bridge IRC and Telnet so you can control your toon via IRC instead of a Telnet client.
MQ2: Think of it as Evolution in action.