Chapter 4. Examples

Example 4.1. 

<?php
      /*
       * PHP 5.3 example
       */
     
        if(!extension_loaded('phurple')) {
        dl('phurple.' . PHP_SHLIB_SUFFIX);
        }
        
        use Phurple\Client;
        use Phurple\Account;
        use Phurple\Conversation;
        use Phurple\Connection;
        
        class MyClient extends Client {
        
            private $someVar;
        
            protected function initInternal()
            {
                $this->someVar = "Hello World";
            }
            
            protected function writeIM($conversation, $buddy, $message, $flags, $time)
            {
                if(Client::MESSAGE_RECV == $flags) {
                    printf( "(%s) %s %s: %s\n",
                                $conversation->getName() ? $conversation->getName() : $buddy->getName(),
                                date("H:i:s", $time),
                                is_object($buddy) ? $buddy->getAlias() : $buddy,
                                $message
                        );
                }
            }
            
            protected function onSignedOn($connection)
            {
                print $this->justForFun($this->someVar);
            }
            
            public function justForFun($param)
            {
                return "just for fun, the param is: $param";
            }
        }
        
        try {
    
            $user_dir = "/tmp/phphurple-test";
            if(!file_exists($user_dir) || !is_dir($user_dir)) {
                mkdir($user_dir);
            }
        
            Client::setUserDir($user_dir);
            Client::setDebug(true);
            Client::setUiId("TestUI");
        
            $client = MyClient::getInstance();
            
            $client->addAccount("msn://nick@hotmail.com:password@messenger.hotmail.com:1863");
            
            $client->connect();
    
            $client->runLoop();
        } catch (Exception $e) {
            echo "[Phurple]: " . $e->getMessage() . "\n";
            die();
        }
?>