#!/usr/local/bin/perl -w # irc.pl # A simple IRC robot. # Usage: perl irc.pl # We will use a raw socket to connect to the IRC server. use IO::Socket; use Device::SerialPort; my $port = Device::SerialPort->new("/dev/tty.usbserial-FTEG6BW6"); $port->databits(8); $port->baudrate(115200); $port->parity("none"); $port->stopbits(1); # The server to connect to and our details. my $server = "chat1.ustream.tv"; my $nick = "simple_bot"; my $login = "simple_bot"; # The channel which the bot will join. my $channel = "#patrickmccb"; my $goth = 0; # Connect to the IRC server. my $sock = new IO::Socket::INET(PeerAddr => $server, PeerPort => 6667, Proto => 'tcp') or die "Can't connect\n"; # Log on to the server. print $sock "NICK $nick\r\n"; print $sock "USER $login 8 * :Perl IRC Hacks Robot\r\n"; # Read lines from the server until it tells us we have connected. while (my $input = <$sock>) { # Check the numerical responses from the server. if ($input =~ /004/) { # We are now logged in. last; } elsif ($input =~ /433/) { die "Nickname is already in use."; } } # Join the channel. print $sock "JOIN $channel\r\n"; # Keep reading lines from the server. while (my $input = <$sock>) { chop $input; if ($input =~ /^PING(.*)$/i) { # We must respond to PINGs to avoid being disconnected. print $sock "PONG $1\r\n"; } else { # Print the raw line received by the bot. print "$input\n"; # Get the user msg part. example:b7-b8 move my $usermsg = substr($input, -11); #print the user message print"$usermsg\n"; #get the end of message. Should = move. For some reason it is 6 long? my $end_of_msg = substr($input, -6); print"$end_of_msg \n"; my $smove='move'; #check to see if this message contains "move" if ($smove eq $end_of_msg) { print "hurray match \n"; #set usermsg = to the last part of the message. Example: b7-b4 #send message over serial port my $count_out = $port->write($usermsg); warn "write failed\n" unless ($count_out); warn "write incomplete\n" if ( $count_out != length($usermsg) ); } } }