msnp9.class.php: Nachricht versenden

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • msnp9.class.php: Nachricht versenden

    Hallo ihr PHP-Gurus

    Ich habe mich mal ein bisschen umgeschaut, wie man wohl MSN über ein PHP-Script verwenden kann und bin auf die zwei Klassen hier gestossen: PHP MSN Messenger Class : flumpCakes

    Da ist auch ein sample dabei, allerdings beinhaltet dies nur das einloggen (msnp9.class.php). Für das senden der Message braucht mann dann die andere Klasse (msn_sb.class.php).

    Ich habe jedoch keine Ahnung, wie ich nun eine Nachricht senden kann, da ich irgendwie noch die Daten der anderen Klasse brauch, nur welche?

    Ich kann OO und auch PHP, aber mit den klassen komme ich nicht zurecht.

    Kann vielleicht jemand ein ganz kleines Beispiel machen, wie man da erfolgreich eine Nachricht senden kann ?
    Das wäre super !

    sample.php (Verbindung aufbauen, funktioniert)
    PHP-Code:
    <?php
        error_reporting
    (E_ALL);
        
    ob_implicit_flush();
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
        <title>MSN Protocol 9 Class Example Usage</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript"></script>
        <style type="text/css" media="screen" title="default">
        body {
            font: 76%/1.4 tahoma, verdana, arial, helvetica, sans-serif;
        }
        .r {
            color: red;
        }
        .g {
            color: green;
        }
        </style>
      </head>
      <body>



    <?php


        
    require_once('msnp9.class.php');
        require_once(
    'msn_sb.class.php');

        
    $msn = new msn;

        if (
    $msn->connect('mail''xxx'))
        {
            
    // we're connected
            // run rx_data function to 'idle' on the network
            // rx_state will loop until the connection is dropped

            
    $msn->rx_data();

            echo 
    '<p>Connection dropped</p>';
        }
        else
        {
            
    // wrong username and password?
            
    echo '<p>Error Connecting to the MSN Network</p>';
        }
    ?>
    </body>
    </html>
    msn_sb.class.php (hier wird wohl die Methode tx_im() gebraucht... nur wie?)
    PHP-Code:
    <?php

    class switchboard
    {
        
    // font colours/styles
        
    var $font_fn 'Arial';
        var 
    $font_co '333333';
        var 
    $font_ef '';


        
    // other
        
    var $debug 1;
        var 
    $trID 1;
        var 
    $email '';


        function 
    switchboard()
        {
            
    $this->session_start_time time();
        }


        
    /**
         *
         * desc    :    send IM message
         *
         * in    :    $ns        =    notification server connection
         *        $msg        =    message to send
         *        $passport    =    current logged in user
         *        $email        =    user to send message to
         *
         * out    :    true on success else return false
         *
         */

        
    function tx_im($ns$msg$passport$email)
        {
            
    $message "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nX-MMS-IM-Format: FN=$this->font_fn; EF=$this->font_ef; CO=$this->font_co; CS=0; PF=22\r\n\r\n$msg";
            
    $message "MSG 20 N ".strlen($message)."\r\n$message";

            if (@
    is_resource($this->sb))
            {
                
    // switchboard session already open
                
    $this->_put($message);

                return 
    true;
            }
            else
            {
                
    // open switchboard session through NS
                
    fputs($ns"XFR $this->trID SB\r\n");


                
    $ns_data fgets($ns4096);

                @list(
    $xfr,,, $server,, $as) = explode(' '$ns_data);

                if (
    $xfr != 'XFR')
                {
                    echo 
    'unable to read NS info. last message: ';
                    echo 
    $ns_data;

                    return 
    false;
                }



                list(
    $server$port) = explode(':'$server);

                if (
    $this->sb = @fsockopen($server$port$errno$errstr5))
                {
                    
    $this->_put("USR $this->trID $passport $as\r\n");
                    
    $this->_get();

                    if (
    is_array($email))
                    {
                        foreach(
    $email as $key => $value)
                        {
                            
    $this->_put("CAL $this->trID $value\r\n");

                            if (
    strstr($this->_get(), 'CAL'))
                            {
                                
    $this->_get(); // should be JOI...
                            
    }
                        }
                    }
                    else
                    {
                        
    $this->_put("CAL $this->trID $email\r\n");

                        if (
    strstr($this->_get(), 'CAL'))
                        {
                            
    $this->_get(); // should be JOI...
                        
    }
                    }



                    
    $this->_put($message);

                    return 
    true;
                }
            }

            return 
    false;
        }


        
    /**
         *
         * desc    :    recieve an IM from the switchboard
         *
         * in    :    none
         * out    :    a. null on fail/no message
         *        b. message string
         *
         */

        
    function rx_im()
        {
            
    $message null;
            
    $msglen null;

            
    stream_set_timeout($this->sb1);

            while (!
    feof($this->sb))
            {
                
    $data = ($msglen) ? $this->_get($msglen) : $this->_get();


                switch (
    substr($data03))
                {
                    default:
                        
    //if (empty($msglen)) continue;

                        
    $message.= $data;

                        if (
    strlen($message) >= $msglen && !empty($msglen))
                        {
                            
    $mesg explode("\n"trim($message));

                            
    $last end($mesg);


                            
    //if (@substr($last, 0, 10) != 'TypingUser')
                            
    if (!strstr($message'TypingUser'))
                            {
                                
    // this isn't a notification that the user is typing a message
                                
    return $last;
                            }


                            
    $msglen null;
                            
    $message null;
                        }

                        if (
    $this->session_start_time 10 time())
                        {
                            
    // looks like we've been idle for a while
                            
    echo 'IM timed out';
                            
    $this->im_close();
                            return 
    null;
                        }
                    break;
                    case 
    'MSG':
                        list(,,, 
    $msglen) = explode (' '$data);
                    break;
                    case 
    'BYE':
                        return 
    null;
                    break;
                }
            }

            return 
    null;
        }


        
    /**
         *
         * desc    :    authorise with switchboard from an IM invitation
         *
         * in    :    $server        =    switchboard server ip
         *        $port        =    switchboard server port
         *        $passport    =    logged in users passport email
         *        $sID        =    session id
         *        $as        =    auth string
         *
         * out    :    true on success else return false
         *
         */

        
    function auth($server$port$passport$sID$as)
        {
            if (
    $this->sb = @fsockopen($server$port$errno$errstr5))
            {
                
    $this->_put("ANS $this->trID $passport $as $sID\r\n");

                if (!
    $this->rx_iro()) return false;

                return 
    true;
            }

            return 
    false;
        }


        
    /**
         *
         * desc    :    recieve IRO commands from IM session
         *
         * in    :    none
         * out    :    true on success else return false
         *
         */

        
    function rx_iro()
        {
            if (
    $data $this->_get())
            {
                @list(
    $iro, , $cur_num$tot$email$name) = explode(' '$data);

                
    $sbsess->email $email;

                if (
    $iro != 'IRO')
                {
                    echo 
    "** BAD data in rx_iro(): see line above **\n";
                    return 
    false;
                }

                
    // recieve names/list of others connected
                
    for ($i=1$i<$tot$i++)
                {
                    if (!
    $data $this->_get())
                    {
                        echo 
    "** BAD data in rx_iro(): see line above **\n";
                        return 
    false;
                    }

                }

                @list(
    $ans) = explode(' '$this->_get());

                if (
    $ans != 'ANS') return false;

                return 
    true;
            }

            return 
    false;
        }


        
    /**
         *
         * desc    :    close switchboard connection
         *
         * in    :    none
         * out    :    none
         *
         */

        
    function im_close()
        {
            
    $this->_put("OUT\r\n");
            @
    fclose($this->sb);
        }

    [...]
    }

    ?>
    Zuletzt geändert von pascal007; 18.08.2009, 21:24.

  • #2
    Habs nur mal kurz überflogen, außerdem ist das uraltes PHP4. Da bin ich schon ein wenig draussen. Die Methode tx_im wird übrigens in der msnp9.class.php initialisiert. Wie sieht es mit deinem error_reporting aus?

    Peter

    Btw: das solltest du sofort unkenntlich machen, ich tue es nicht mehr
    PHP-Code:
    if ($msn->connect('xxx''xxx')) 
    Nukular, das Wort ist N-u-k-u-l-a-r (Homer Simpson)
    Meine Seite

    Kommentar


    • #3
      Danke für den Hinweis Ja das error_reporting spuckt nix aus. Das interesante passiert eben in der msnp9.class.php (hab ich nun herausgefunden):

      PHP-Code:
          function rx_data()
          {
              
      $this->_put("SYN $this->trID 0\r\n");
              
      $this->_put("CHG $this->trID NLN\r\n");

              while (! 
      feof($this->fp))
              {
                  
      $data $this->_get();

                  if (
      $data)
                  {
                      
      //echo $data.'<br />';

                      
      switch($code substr($data03))
                      {
                          default:
                              
      // uncommenting this line here would probably give a load of "error code not found" messages.
                              //echo $this->_get_error($code);
                          
      break;
                          case 
      'RNG':
                              
      // someone's trying to talk to us
                              
      list(, $sid$server, , $as$email$name) = explode(' '$data);
                              list(
      $sb_ip$sb_port) = explode(':'$server);


                              
      $sbsess = new switchboard;

                              if (
      $sbsess->auth($sb_ip$sb_port$this->passport$sid$as))
                              {
                                  
      // sb session opened
                                  // recieve users message
                                  
      if ($msg $sbsess->rx_im())
                                  {
                                      
      // send the message straight back!
                                      
      $sbsess->tx_im($this->fp"KACKEN!"$this->passport$email);

                                      
      // close IM sessions
                                      
      $sbsess->im_close();
                                  }
                                  else
                                  {
                                      echo 
      'No message was received from user.';
                                  }
                              }
                              else
                              {
                                  echo 
      'Unable to authenticate with switchboard.';
                              }
                          break;
                      }
                  }
              }
          } 
      Hier wird diese Klasse initalisiert und danach wieder geschlossen. Nun diese Methode schickt, sollte jemand schreiben das selbe wieder zrück, wie der Kontakt schreibt (was auch funktioniert ).

      Die Frage ist nur, wie kann ich das nun in ein Formular auslagern, das ich eine Antwort per formualr senden kann, denn:

      - das Script darf nicht beendet werden, sonst ist die verbindung futsch

      Ich wollte schon was mit AJAX machen, aber da kann ich ja auch nicht das laufende Script aufrufen... auch an SESSIONS habe ich schon gedacht und die wichtigen Daten für auth() und tx_im() in die Session speichern... aber ja... mir würde auch schon ein fertiger Web Messenger für meinen eigenen Webspace reichen, gibts nicht, also muss ich selber ran.

      Kommentar

      Lädt...
      X