PHP AOL Instant Messenger (AIM) bot

dirtywerm
dirtywerm used Ask the Experts™
on
I'm working on making a AOL Instant Messenger bot in PHP using TOC.  I can get it to log in and everything, but it never shows up as being online and I can't send any messages.  If anyone can help me with this I would really appreciate it.  I'll post the code below in a comment.  I can give out more points if this is difficult to fix.

Thanks
WERM
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
<?
define("MAX_PACKLENGTH",65535);
define("SFLAP_TYPE_SIGNON",1);
define("SFLAP_TYPE_DATA",2);
define("SFLAP_TYPE_ERROR",3);
define("SFLAP_TYPE_SIGNOFF",4);
define("SFLAP_TYPE_KEEPALIVE",5);
define("SFLAP_MAX_LENGTH",1024);
define("SFLAP_SUCCESS",0);
define("SFLAP_ERR_UNKNOWN",1);
define("SFLAP_ERR_ARGS",2);
define("SFLAP_ERR_LENGTH",3);
define("SFLAP_ERR_READ",4);
define("SFLAP_ERR_SEND",5);
define("SFLAP_FLAP_VERSION",1);
define("SFLAP_TLV_TAG",1);
define("SFLAP_HEADER_LEN",6);


$ERROR_MSGS = array ( 0 => 'Success',
1 => 'AOLIM Error: Unknown Error',
2 => 'AOLIM Error: Incorrect Arguments',
3 => 'AOLIM Error: Exceeded Max Packet Length (1024)',
4 => 'AOLIM Error: Reading from server',
5 => 'AOLIM Error: Sending to server',
6 => 'AOLIM Error: Login timeout',
901 => 'General Error: $ERR_ARG not currently available',
902 => 'General Error: Warning of $ERR_ARG not currently available',
903 => 'General Error: A message has been dropped, you are exceeding the server speed limit',
950 => 'Chat Error: Chat in $ERR_ARG is unavailable',
960 => 'IM and Info Error: You are sending messages too fast to $ERR_ARG',
961 => 'IM and Info Error: You missed an IM from $ERR_ARG because it was too big',
962 => 'IM and Info Error: You missed an IM from $ERR_ARG because it was sent too fast',
970 => 'Dir Error: Failure',
971 => 'Dir Error: Too many matches',
972 => 'Dir Error: Need more qualifiers',
973 => 'Dir Error: Dir service temporarily unavailble',
974 => 'Dir Error: Email lookup restricted',
975 => 'Dir Error: Keyword ignored',
976 => 'Dir Error: No keywords',
977 => 'Dir Error: Language not supported',
978 => 'Dir Error: Country not supported',
979 => 'Dir Error: Failure unknown $ERR_ARG',
980 => 'Auth Error: Incorrect nickname or password',
981 => 'Auth Error: The service is temporarily unavailable',
982 => 'Auth Error: Your warning level is too high to sign on',
983 => 'Auth Error: You have been connecting and disconnecting too frequently. Wait 10 minutes and try again. If you continue to try, you will need to wait even longer.',
989 => 'Auth Error: An unknown signon error has occurred $ERR_ARG' );

$socket = null;
$clientSequenceNumber = 0;

$screenName="screenName";
$password = "password";

$tocHost = "toc.oscar.aol.com";
$tocPort = 5190;
$authHost =  "login.oscar.aol.com";
$authPort = 5159;


function RoastPassword($password)
{
      $roastString = 'Tic/Toc';
      $roastedPassword = '0x';

      for ($i = 0; $i < strlen($password); $i++)
            $roastedPassword .= bin2hex($password[$i] ^ $roastString[($i % 7)]);

      return $roastedPassword;
}


function readFlap()
{
      global $socket;

      $header = fread($socket, SFLAP_HEADER_LEN);

      if(strlen($header) < SFLAP_HEADER_LEN)
            return(0);

      $headerArray = unpack ("aasterisk/CframeType/nsequenceNumber/ndataLength", $header);

      $packet = fread($socket, $headerArray['dataLength']);

      if($headerArray['frameType'] == SFLAP_TYPE_SIGNON)
            $packetArray = unpack("Ndata", $packet);
      else
            $packetArray = unpack("a*data", $packet);

      $data = array_merge($headerArray, $packetArray);

      return $data;
}

function sendRaw($data)
{
      global $socket;
      
      if(fwrite($socket, $data) == FALSE)
            return 0;
      
      return 1;
}



function sendFlap($frameType, $data)
{
      global $socket, $clientSequenceNumber;

      if( strlen($data) > SFLAP_MAX_LENGTH)
      {
            $data = substr($data, 0, (SFLAP_MAX_LENGTH-2) );
            $data .= '"';
      }

      $header = pack("aCnn", '*', $frameType, $clientSequenceNumber, strlen($data));

      $packet = $header . $data;

      if(fwrite($socket, $packet) == FALSE)
            die("Unable to send the packet.<BR>\n");

      $clientSequenceNumber++;
      
      return $packet;
}

function sendTocFlapSignon()
{
      global $screenName;
      
      $data = pack("Nnna".strlen($screenName), 1, 1, strlen($screenName), $screenName);

      $result = sendFlap(SFLAP_TYPE_SIGNON, $data);

      return $result;
}

function tocSignon()
{
      global $screenName, $password;
      global $authHost, $authPort;
      
      $roastedPassword = RoastPassword($password);
      
      $tocSignon = 'toc_signon ' . $authHost . ' ' . $authPort . ' ' . $screenName . ' ' . $roastedPassword . ' "english" "AOLIM:\$Version 1.1\$"';
      $tocSignon .= "\0";

      $result = sendFlap(SFLAP_TYPE_DATA, $tocSignon);
      
      return $result;
}      



function normalize($screenName)
{
      return eregi_replace("[[:space:]]+","",strtolower($screenName));
}

function signOn()
{
      global $screenName, $roastedPassword, $socket, $clientSequenceNumber, $ERROR_MSGS;
      global $tocHost, $tocPort;      
      
      echo "Signing on using $screenName<BR>\n";

      if( !($socket = fsockopen($tocHost, $tocPort, $errorno, $errorstr, 30)) )
            die("$errorstr ($errorno)");

      echo "Connected to the sever<BR>\n";

      if( !sendRaw("FLAPON\r\n\r\n") )
                  die("Error sending the FLAPON packet.");

      echo "FLAPON packet sent<BR>\n";

      if( !( $result = readFlap() ) )
            die("No response from server.<BR>\n");

      if($result['asterisk'] != '*' && $result['frameType'] != 1 && $result['dataLength'] != 4 && $result['data'] != 1)
            die("Invalid FLAP SIGNON response from the server.<BR>\n");            

      sendTocFlapSignon();
      
      echo "SIGN_ON packet sent<BR>\n";

      tocSignon();

      echo "toc_signon packet sent<BR>\nWaiting for reply...<BR>\n";

      $result = readFlap();
      
      if($result['asterisk'] != '*' && $result['frameType'] != 2 )
            die("Invalid response from server.<BR>\n");            

      if($result['data'] == "SIGN_ON:TOC1.0")
            echo "toc_signon success SIGN_ON <br>";

      else if(substr($result['data'], 0, 6) == "ERROR:")
      {
            $where = strpos( $result['data'], ":");
            die($ERROR_MSGS[chop(substr($result['data'], $where+1))] . "<br>\n");
      }

      $result = readFlap(); // NICK
      echo $result['data'] . "<BR>\n";
      
      $result = readFlap(); // NICK
      echo $result['data'] . "<BR>\n";
      
      $result = readFlap(); // Buddies
      echo $result['data'] . "<BR>\n";

      sendFlap(SFLAP_TYPE_DATA, "toc_add_permit");
      sendFlap(SFLAP_TYPE_DATA, "toc_add_deny");
      
      $tocAddBuddy = "toc_add_buddy " . $screenName;
      sendFlap(SFLAP_TYPE_DATA, $tocAddBuddy);

      sendFlap(SFLAP_TYPE_DATA, "toc_init_done");
      echo "sent toc_init_done<BR>\n";

/*
      $done = 0;
      while(!$done)
      {
            $result = readFlap();
      
            if($result)
            {      
                  echo $result['asterisk'] . " - ";                  
                  echo $result['frameType'] . " - ";                  
                  echo $result['sequenceNumber'] . " - ";                  
                  echo $result['dataLength'] . "\n";                  
            }
      }
*/

}


signOn();

?>
VGR

Commented:
beautiful code ;-)

if it is logging in successfully, why wouldn't it be shown as "logged in" ? ;-)

it is apparently not a FW issue concerning ports

I don't know. It's more a AOL IM problem I guess

Author

Commented:
I've gone a lot off of the program MIA a C++ aol instant messanger.  I think I do everything that they do.  But MIA can send messages.  So there has got to be something i'm missing.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Author

Commented:
i've also asked this question

http://www.experts-exchange.com/Programming/Q_20450778.html

if you can help me I will give you credit for both questions.

Commented:
I've looked this code up and down.. I don't know what's wrong.
The only possible thing I see is that the toc_init_done should be null terminated, but it doesnt look to be. I tried doing that, but it didnt work.

Let me know if you figure it out.

Commented:
I was wanting to write a PHP client for the same thing...  I stumbled across this and thought it would be a good start for working on mine -- but I figured I'd just write almost everything from scratch to avoid whatever bug was causing you problems.

My code is completely different, as it's object oriented, but I was having the same trouble.

I found out that the TOC servers don't seem to acknowledge your existence unless you send permit/deny or add_buddy type command before the toc_init_done.  For whatever reason, I don't know.  So I just started making it always do a toc_add_buddy before the toc_init_done, and it always adds itself to it's own list.

Now it has no trouble signing on.

Maybe 7 months late, but at least it's an answer.
01x

Commented:
The toc_add_buddy deal isn't your problem at all.  You aren't properly terminating all your strings, fix it by adding a check in sendFlap

function sendFlap($frameType, $data)
{
    global $socket, $clientSequenceNumber;

    if( strlen($data) > SFLAP_MAX_LENGTH)
    {
         $data = substr($data, 0, (SFLAP_MAX_LENGTH-2) );
         $data .= '"';
    }
   
+   $data = rtrim($data);
+   $data .= "\0";
    $header = pack("aCnn", '*', $frameType, $clientSequenceNumber, strlen($data));

   
    $packet = $header . $data;

    if(fwrite($socket, $packet) == FALSE)
         die("Unable to send the packet.<BR>\n");

    $clientSequenceNumber++;
   
     return $packet;
}
Commented:
I've started playing around with your code.  The reason it doesn't show up online or allow you to send messages is becuase after logging in php is finished with the code and closes closes the socket(s).

A very quick 'n dirty hack is to add the following to the end of the code.


$x=false;
while($x==false)
{
      sleep(3); // this is just to pause the reading for 3 secs...can be less
      $result = readFlap(); //grab the data

      if(ereg('^IM_IN',$result['data']))
      {
            $nick = explode(':',$result['data']);
            $nick = normalize($nick[1]);
            if(ereg('quit', $result['data']) && $nick = 'YOUR NICKNAME')
            {
                  sendFlap(SFLAP_TYPE_DATA,'toc_send_im ' . $nick . 'Good bye');
                  //couldn't get toc_signoff to do anything so I closed the socket
                  fclose($socket);
                  exit;
            }
            if(ereg('hello',$result['data']))
            {
                  $nick = explode(':',$result['data']);
                  $nick = normalize($nick[1]);
                  sendFlap(SFLAP_TYPE_DATA,'toc_send_im ' . $nick . ' "Hello!"');
            }
            else
            {
                  $nick = explode(':',$result['data']);
                  $nick = normalize($nick[1]);
                  sendFlap(SFLAP_TYPE_DATA,'toc_send_im ' . $nick . ' "I don\'t understand you!"');
            }
      }
}

Now when you start the bot and say "hello" it will say "Hello!", if you say "quit" it will say "Good Bye" and quit. If you say anything else it will tell you it doesn't understand.  I guess you could add a check to see if you are still connected (set $x=false) but I figured if you get disconnected or something the script will terminate anyway (atleast when used as CLI)

Hope this helps.
any final version of this script?
doesnt look like it
:(

Commented:
Peppe316,
I am trying to make a bot and I tried the asker's code with your code added on at the end.  It doesn't work.  The username never signs on.  Could you post your working code?

Thanks,
Matt

Commented:
was anyone able to get a working version of this?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial