Read More for code.
#!/usr/bin/perl -w use strict; use IO::Socket; use Net::AIM; use Data::Dumper; use Getopt::Long; # These are the option variables, they will be undefined or 0 if no op +tion is set my $help = ''; # Will be true if -h or --help is specified my $machine = ''; # Will be true if -i or --ip_address is speci +fied my $port = 0; # Will be true if -p or --port is specified my $from_aim = ''; # Will be true if -f or --from is specified my $word = ''; # Will be true if -w or --word is specified my $to_aim = ''; # Will be true if -t or --to is specified my $udp = 0; # Will be true if -u or --udp is specified my $listen = 0; # Will be true if -l or --listen_tcp is spe +cified my $search_terms; # Will be true if -s or --search_terms is specifi +ed # Other variables my $msg = ''; my $sock = ''; my $listener = ''; my $MAX_TO_READ = 1024; my $datagram = ''; my $flags = ''; my @terms = (); my $term = ''; # Grab the options GetOptions ('h|help|?' => \$help, 'i|ip_address=s' => \$machine, 'p|port=i' => \$port, 'f|from=s' => \$from_aim, 'w|word=s' => \$word, 't|to=s' => \$to_aim, 'u|udp' => \$udp, 'l|listen_tcp' => \$listen, 's|search_terms=s' => \$search_terms); # Need help? if($help) { print "\n"; print "Usage: $0 -i ip_address -p port -f aim_send_account -w send +_account_pwd -t aim_to_account [options] \n"; print "Read Helix Universal Server messages from TCP or UDP socket + and send AIM alerts .\n"; print "The following options are supported:\n\n"; print " -h, --help Print this message\n"; print " -i, --ip_address IP address of HUS (TCP query) or machine + running script (TCP/UDP listener)\n"; print " -p, --port Port number on HUS (TCP query) or machin +e running script (TCP/UDP listener)\n"; print " -f, --from_aim Screen name for sender AIM account\n"; print " -w, --word Password for sender AIM account\n"; print " -t, --to_aim Screen name for receiver AIM account\n"; print " -u, --udp Use UDP listener. (TCP query default) \n +"; print " -l, --listen_tcp Use TCP listener. (TCP query default) \n +"; print " -s, --search_terms Comma-separated list of search terms fo +r msg filtering. Terms with spaces use quotes.\n\n"; print "Usage Examples:\n\n"; print " hus2aim.pl -i hus_ip -p port_number -f from_aim_id -w pass +word -t to_aim_id\n\n"; print " hus2aim.pl -i hus_ip -p port_number -f \"from aim id with +spaces\" -w password -t to_aim_id\n\n"; print " hus2aim.pl -u -i local_ip -p port_number -f from_aim_id -w + password -t to_aim_id -s term1,term2,\"term with spaces\"\n\n"; print " hus2aim.pl -l -i local_ip -p port_number -f from_aim_id -w + password -t to_aim_id\n\n"; print "Have fun!\n\n"; exit; } # If terms for filtering, split out array of search term(s) if ($search_terms) { @terms = split (/,/, $search_terms); } # Set protocol option my $proto = ''; if ($udp) { $proto = 'udp'; } else { $proto = 'tcp'; } # Open socket (Inbound TCP Output Type on Helix Univesal Server; outbo +und on host running this script) if ((!($listen)) && (!($udp))) { $sock = new IO::Socket::INET(PeerAddr => $machine, PeerPort => $port, Proto => $proto); die "cannot open socket" unless ($sock); } # Open socket (Outbound TCP Output Type on Helix Univesal Server; inbo +und on host running this script) if ($listen) { $listener = new IO::Socket::INET(LocalHost => $machine, LocalPort => $port, Proto => $proto, Listen => 1, Reuse => 1) or die "Could not create socket: $!\n"; $sock = $listener->accept(); } # Open socket (Outbound UDP Output Type on Helix Univesal Server; inbo +und on host running this script) if ($udp) { $listener = new IO::Socket::INET(LocalHost => $machine, LocalPort => $port, Proto => $proto) #Type => SOCK_DGRAM, + or die "Could not create socket: $!\n"; while ($msg = $listener->recv($datagram, $MAX_TO_READ)) { print 'udpudpudp'; filter_msg($datagram); } } # Set up AIM connection my $AIM = new Net::AIM; $AIM->newconn(Screenname => $from_aim, Password => $word, AutoReconnect => 1) or die "Unable to open AIM connection.\n"; my $conn = $AIM->getconn(); # Wait for message from Helix Universal Server (if TCP), then call AIM + if a message comes in while (<$sock>) { $msg = $_; if ($search_terms) { filter_msg($msg); } else { send_aim($msg) } } close $sock; # Filter message by search term sub filter_msg { local($_) = shift; if (@terms) { foreach $term (@terms) { if ($_ =~ /$term/) { send_aim($_); last; } } } } #We're clear? Ok. Send an AIM. sub send_aim { local($_) = shift; my $ready = 0; $conn->set_handler(config => sub {$ready = 1}); $AIM->do_one_loop; $conn->send_im($to_aim, "From Helix Server: $_"); }
While this code is by no means elegant and one would never use a script like this in a production environment, it's a decent demo and therefore (IMHO) a CUFP! :-)
|
|---|