ICQ-bot based on micq

I decided to make ICQ-bot, mostly for ping servers in home network using ICQ.

Micq has such power thing as scripting. By default, named pipe ~/.micq/scripting is created after micq has started. If you write into it, this is as if you enter into micq console. Thus we have one side of interaction. To receive commands we'll read logs of conversations from history dir.

I wrote script in perl using File::Tail module.

Log file has such format:

# 20070629123241/ [icq8:xxxxxxxx]!icqbot@foreveryoung.yar <- xxxxxxxxx[icq8:xxxxxxxxx+10000000 online] +1

+1 - means how many lines of message follows.

Here is the script:

#!/usr/bin/perl
$_ = 1;
exit if( $#ARGV < 2 )
name = "/home/icqbot/.micq/history/${ARGV[0]}.log"
file = File::Tail->new( name=>$name, maxinterval=>1, interval=>1, reset_tail=>0 );
open LOG, ">>${ARGV[0]}_bot.log";
$skip = 0;
$cmd = 0;
while( defined( $_ = $file->read ) )
{
  if( $skip > 0 )
  {
    $skip--;
    next;
  }
  # this is sent by bot, just skip
  if( $_ =~ /# \d+\/ \[[^]]+\]\S+ -> [^[]*\[[^]]+\] [+](\d+)/ )
  {
    $skip = $1;
    $cmd = 0;
    next;
  }
  # after this maybe goes command
  if( $_ =~ /# \d+\/ \[[^]]+\]\S+ <- [^[]*\[[^]]+\] [+](\d+)/ )
  {
    $cmd = 1;
    next;
  }
  if( !$cmd )
  {
    next;
  }
  if( $_ =~ /^ping (.+)$/ )
  {
    print LOG "Pinging $1\n";
    $ping = `ping -c 10 $1 2>&1`;
    open OUT, ">.micq/scripting";
    print OUT "/msg ${ARGV[0]}\n";
    print OUT $ping;
    print OUT ".\n";
    close OUT;
    next;
  }
}

To run script: script &

micq is launched under screen.

blog comments powered by Disqus