[PHP] Per PHP Script Message in IRC CHannel senden

Dieses Thema im Forum "Webentwicklung" wurde erstellt von DOWNandOUT, 18. März 2011 .

Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 18. März 2011
    Per PHP Script Message in IRC CHannel senden

    Hi,

    es geht um folgendes:

    ich möchte gerne das wenn ich ein PHP Script von mir aufrufe ein Bot auf den IRC-Server connected, nem Channel joint und dort im Channel 3 Nachrichten schreibt.

    Jetzt habe ich per Google viele IRC-Klassen gefunden, allerdings hat keine eine Funktion eine Nachricht in den Channel zu schreiben sondern nur PRIVMSG an bestimmte User.

    Weiss jemand wie das mit dem Posten im Channel machbar ist ?

    grüße
     
  2. 18. März 2011
    AW: Per PHP Script Message in IRC CHannel senden

    PRIVMSG #channel :nachricht

    http://tools.ietf.org/html/rfc1459
     
  3. 19. März 2011
    AW: Per PHP Script Message in IRC CHannel senden

    Danke, klappt perfekt. JEtzt kommt allerdings ein neues Problem dazu, unzwar will ich das er eine Nachricht ausgibt, dann 10 sekunden wartet, dann die 2te ausgibt und dann wieder 10 sekunden wartet und dann die dritte ausgibt.

    Hab das dann quasi so gemacht:
    SendCommand("PRIVMSG #hallo :nachricht1\r\n");
    sleep(10);
    SendCommand("PRIVMSG #hallo :nachricht2\r\n");
    sleep(10);
    SendCommand("PRIVMSG #hallo :nachricht3\r\n");
    sleep(10);

    Allerdings läuft das irgendwie immer in einer Schleife.. er gibt nach 10 Sekunden Nachricht1 aus, 10 sekunden später Nachricht1,Nachricht2 und dann 10 sek später Nachricht1,Nachricht2,Nachricht3. Das soll er aber nicht, er soll ja jeweils nur eins ausgeben.. das ist mal der übrige code:

    PHP:
    <? php
    //First lets set the timeout limit to 0 so the page wont time out.
    set_time_limit ( 0 );
    //The server host is the IP or DNS of the IRC server.
    $server_host  "127.0.0.1" ;
    //Server Port, this is the port that the irc server is running on. Deafult: 6667
    $server_port  6667 ;
    //Server Chanel, After connecting to the IRC server this is the channel it will join.
    $server_chan  "#channel" ;
    //Second lets grab our data from our form.
    $nickname  "BotMasterMC" ;
    //Now lets check to see if there is a nickname set.
    if(empty( $nickname ))
    {
        
    //Whoops we dont have a nickname set.
        
    echo  "<form name=\"form1\" method=\"post\" action=\"index.php\">\n\r" ;
        echo 
    "<p align=\"center\">Please Insert a Nickname.\n\r" ;
        echo 
    "<input type=\"text\" name=\"nick\"> \n\r" ;
        echo 
    "</p>\n\r" ;
        echo 
    "<p align=\"center\">\n\r" ;
        echo 
    "<input type=\"submit\" name=\"Submit\" value=\"Join IRC\">\n\r" ;
        echo 
    "</p>\n\r" ;
        echo 
    "</form>\n\r" ;
    }
    else
    {
        
    //Ok, We have a nickname, now lets connect.
        
    $server  = array();  //we will use an array to store all the server data.
        //Open the socket connection to the IRC server
        
    $server [ 'SOCKET' ] = @ fsockopen ( $server_host $server_port $errno $errstr 2 );
        if(
    $server [ 'SOCKET' ])
        {
            
    //Ok, we have connected to the server, now we have to send the login commands.
            
    SendCommand ( "PASS NOPASS\n\r" );  //Sends the password not needed for most servers
              
    SendCommand ( "NICK  $nickname \n\r" );  //sends the nickname
              
    SendCommand ( "USER  $nickname  USING PHP IRC\n\r" );  //sends the user must have 4 paramters
            
    while(! feof ( $server [ 'SOCKET' ]))  //while we are connected to the server
            
    {
                
    $server [ 'READ_BUFFER' ] =  fgets ( $server [ 'SOCKET' ],  1024 );  //get a line of data from the server
                
    echo  "[RECIVE] " . $server [ 'READ_BUFFER' ]. "<br>\n\r" //display the recived data from the server

                /*
                IRC Sends a "PING" command to the client which must be anwsered with a "PONG"
                Or the client gets Disconnected
                */
                //Now lets check to see if we have joined the server
                
    if( strpos ( $server [ 'READ_BUFFER' ],  "422" ))  //422 is the message number of the MOTD for the server (The last thing displayed after a successful connection)
                
    {
                    
    //If we have joined the server

                    
    SendCommand ( "JOIN  $server_chan \n\r" );  //Join the chanel
                
    }
                if(
    substr ( $server [ 'READ_BUFFER' ],  0 6 ) ==  "PING :" //If the server has sent the ping command
                
    {
                    
    SendCommand ( "PONG :" . substr ( $server [ 'READ_BUFFER' ],  6 ). "\n\r" );  //Reply with pong
                    //As you can see i dont have it reply with just "PONG"
                    //It sends PONG and the data recived after the "PING" text on that recived line
                    //Reason being is some irc servers have a "No Spoof" feature that sends a key after the PING
                    //Command that must be replied with PONG and the same key sent.
                
    }
                
    flush ();  //This flushes the output buffer forcing the text in the while loop to be displayed "On demand"
            
    }
        }
    }
    function 
    SendCommand  ( $cmd )
    {
        global 
    $server //Extends our $server array to this function
        
    @ fwrite ( $server [ 'SOCKET' ],  $cmd strlen ( $cmd ));  //sends the command to the server
        
    echo  "[SEND]  $cmd  <br>" //displays it on the screen
    }
    ?>;
    weiss jmd ne lösung ? gruß
     
  4. 19. März 2011
    AW: Per PHP Script Message in IRC CHannel senden

    sleep() ist fehl am platz!
    das script darf nicht blockieren, sonst reist dir möglicherweise die verbindung ab.

    PHP:
    <? php

    function  set_timeout ( $fn $args $timeout ) {
        global 
    $__set_timeout ;
        
        if (!
    $__set_timeout )
            
    $__set_timeout  = array();
            
        return 
    array_push ( $__set_timeout , array( time () +  $timeout $fn $args )) -  1 ;
    }

    function 
    clear_timeout ( $id ) {
        global 
    $__set_timeout ;
        
        if (isset(
    $__set_timeout [ $id ]))
            unset(
    $__set_timeout [ $id ]);
    }

    function 
    exec_timeout ( $id ) {
        global 
    $__set_timeout ;
        
        if (!isset(
    $__set_timeout [ $id ]))    
            return;
            
        list (, 
    $fn $args ) =  $__set_timeout [ $id ];
        unset(
    $__set_timeout [ $id ]);
        
        
    call_user_func ( $fn $args );
    }

    function 
    exec_timeouts () {
        global 
    $__set_timeout ;
        
        
    $now  time ();
        
        foreach (
    $__set_timeouts  as  $id  =>  $timeout )
            if (
    $timeout [ 0 ] <=  $now )
                
    exec_timeout ( $id );
    }  
    und in deine while-schleife schreibst du über flush(); noch exec_timeouts();

    jetzt kannst du timeouts verwenden, die dein script nicht blockieren.

    PHP:
    set_timeout ( 'SendCommand' 'PRIVMSG #foo :foo' 10 );
    set_timeout ( 'SendCommand' 'PRIVMSG #foo :bar' 20 );
    set_timeout ( 'SendCommand' 'PRIVMSG #foo :baz' 30 );
     
  5. 19. März 2011
    AW: Per PHP Script Message in IRC CHannel senden

    PHP:
    <? php
    //First lets set the timeout limit to 0 so the page wont time out.
    set_time_limit ( 0 );
    //The server host is the IP or DNS of the IRC server.
    $server_host  "127.0.0.1" ;
    //Server Port, this is the port that the irc server is running on. Deafult: 6667
    $server_port  6667 ;
    //Server Chanel, After connecting to the IRC server this is the channel it will join.
    $server_chan  "#hallo" ;
    //Second lets grab our data from our form.
    $nickname  "BotMasterMC" ;
    //Now lets check to see if there is a nickname set.
    ###
    function  set_timeout ( $fn $args $timeout ) {
        global 
    $__set_timeout ;

        if (!
    $__set_timeout )
            
    $__set_timeout  = array();

        return 
    array_push ( $__set_timeout , array( time () +  $timeout $fn $args )) -  1 ;
    }

    function 
    clear_timeout ( $id ) {
        global 
    $__set_timeout ;

        if (isset(
    $__set_timeout [ $id ]))
            unset(
    $__set_timeout [ $id ]);
    }

    function 
    exec_timeout ( $id ) {
        global 
    $__set_timeout ;

        if (!isset(
    $__set_timeout [ $id ]))
            return;

        list (, 
    $fn $args ) =  $__set_timeout [ $id ];
        unset(
    $__set_timeout [ $id ]);

        
    call_user_func ( $fn $args );
    }

    function 
    exec_timeouts () {
        global 
    $__set_timeout ;

        
    $now  time ();

        foreach (
    $__set_timeouts  as  $id  =>  $timeout )
            if (
    $timeout [ 0 ] <=  $now )
                
    exec_timeout ( $id );
    }
    ####
    if(empty( $nickname ))
    {
        
    //Whoops we dont have a nickname set.
        
    echo  "<form name=\"form1\" method=\"post\" action=\"index.php\">\n\r" ;
        echo 
    "<p align=\"center\">Please Insert a Nickname.\n\r" ;
        echo 
    "<input type=\"text\" name=\"nick\"> \n\r" ;
        echo 
    "</p>\n\r" ;
        echo 
    "<p align=\"center\">\n\r" ;
        echo 
    "<input type=\"submit\" name=\"Submit\" value=\"Join IRC\">\n\r" ;
        echo 
    "</p>\n\r" ;
        echo 
    "</form>\n\r" ;
    }
    else
    {
        
    //Ok, We have a nickname, now lets connect.
        
    $server  = array();  //we will use an array to store all the server data.
        //Open the socket connection to the IRC server
        
    $server [ 'SOCKET' ] = @ fsockopen ( $server_host $server_port $errno $errstr 2 );
        if(
    $server [ 'SOCKET' ])
        {
            
    $i  0 ;
            
    //Ok, we have connected to the server, now we have to send the login commands.
            
    SendCommand ( "PASS NOPASS\n\r" );  //Sends the password not needed for most servers
              
    SendCommand ( "NICK  $nickname \n\r" );  //sends the nickname
              
    SendCommand ( "USER  $nickname  USING PHP IRC\n\r" );  //sends the user must have 4 paramters
            
    while(! feof ( $server [ 'SOCKET' ]))  //while we are connected to the server
            
    {
                
    $server [ 'READ_BUFFER' ] =  fgets ( $server [ 'SOCKET' ],  1024 );  //get a line of data from the server
                
    echo  "[RECIVE] " . $server [ 'READ_BUFFER' ]. "<br>\n\r" //display the recived data from the server

                /*
                IRC Sends a "PING" command to the client which must be anwsered with a "PONG"
                Or the client gets Disconnected
                */
                //Now lets check to see if we have joined the server
                
    if( strpos ( $server [ 'READ_BUFFER' ],  "422" ))  //422 is the message number of the MOTD for the server (The last thing displayed after a successful connection)
                
    {
                    
    //If we have joined the server

                    
    SendCommand ( "JOIN  $server_chan \n\r" );  //Join the chanel
                
    }
                if(
    substr ( $server [ 'READ_BUFFER' ],  0 6 ) ==  "PING :" //If the server has sent the ping command
                
    {
                    
    SendCommand ( "PONG :" . substr ( $server [ 'READ_BUFFER' ],  6 ). "\n\r" );  //Reply with pong
                    //As you can see i dont have it reply with just "PONG"
                    //It sends PONG and the data recived after the "PING" text on that recived line
                    //Reason being is some irc servers have a "No Spoof" feature that sends a key after the PING
                    //Command that must be replied with PONG and the same key sent.
                
    }
                
    exec_timeouts ();
                
    flush ();  //This flushes the output buffer forcing the text in the while loop to be displayed "On demand"
            
    }
        }
    }
    function 
    SendCommand  ( $cmd )
    {
        global 
    $server //Extends our $server array to this function
        
    @ fwrite ( $server [ 'SOCKET' ],  $cmd strlen ( $cmd ));  //sends the command to the server
        
    echo  "[SEND]  $cmd  <br>" //displays it on the screen
    }
    set_timeout ( 'SendCommand' 'PRIVMSG #hallo :foo' 10 );
    set_timeout ( 'SendCommand' 'PRIVMSG #hallo :bar' 20 );
    set_timeout ( 'SendCommand' 'PRIVMSG #hallo :baz' 30 );
    ?>
    Habs jetzt so, allerdings gibt er im IRC keine Nachricht aus.
     
  6. 19. März 2011
    AW: Per PHP Script Message in IRC CHannel senden

    dein script ansich ist mmn. garnicht wirklich geeignet anständig mit einem irc-server zu kommunizieren.

    ich denke du hast garnicht wirklich verstanden wie der ablauf von senden/empfangen von statten gehen muss. am besten du schaust dir den rfc nochmal genauer an und versuchst dein script daran anzupassen.

    zudem würde ich dir raten echte sockes zu verwenden, die ja in php zur verfügung stehen.

    code vorkauen werde ich persönlich an dieser stelle nicht, da das thema 1. recht komplex ist und 2. zu viel zeit in anspruch nehmen würde.
     
  7. 19. März 2011
    AW: Per PHP Script Message in IRC CHannel senden

    Zumal Du \r\n und nicht \n\r anhängen musst.

    Mach es dir doch leichter und verbinde dich mit dem Server und les zeilenweise aus, was Du empfängst. Diese Zeile parsed Du dann in einer seperaten Funktion.

    Wenn Du dann "PING :xyz" empfängst, übergibst Du den Befehl deiner Eventfunktion "onPing".

    Pseudocode:

    Spoiler
    Code:
    Verbinden zum Server
    
    Unendlichschleife
     Lese Zeile
     parse Zeile in Funktion parseCommand()
    
    
    parseCommand(cmd):
     wenn cmd mit "PING :" anfängt, dann
     rufe Funktion onPing auf
    
    onPing(cmd): 
     Zerlege den Befehl bis zum Integer
     Hänge den Integer an "PONG :" an
    
     Rufe Funktion sendCommand() mit dem neuen Befehl auf
    
    sendCommand(cmd):
     Hänge \r\n an cmd an
     Sende den Befehl
    

    So hast Du gleich ein kleines Framework (Am besten mit Klassen herrichten), das Du dann ergänzen kannst.
     
  8. 19. März 2011
    AW: Per PHP Script Message in IRC CHannel senden

    Ich hab was OOP Programmierung angeht nicht viel Erfahrung, hab jetzt aber mal versucht, bzw angefangen ne Klasse selbst zu schreiben:

    PHP:
    <? php
    class  IRC  {
        var 
    $Host ;
        var 
    $Port ;
        var 
    $Nick ;
        var 
    $Ping ;
        var 
    $IRCsocket ;
        var 
    $IRC ;
        
        function 
    Connect ( $Host $Port $Nick ) {
            
    set_time_limit ( 0 );
            
    ob_end_flush ();
            
    $this -> Host  $Host ;
            
    $this -> Port  $Port ;
            
    $this -> Nick  $Nick ;
            
    $this -> IRCsocket  $IRCsocket ;
            
    $this -> IRCsocket  socket_create ( AF_INET SOCK_STREAM SOL_TCP );
            
    socket_connect ( $this -> IRCsocket $this -> Host $this -> Port );
            
    socket_write ( $this -> IRCsocket "USER " . $this -> Nick . " 127.0.0.1 irc.localhost.net :Max Mustermann\r\n" );
            
    socket_write ( $this -> IRCsocket "NICK " . $this -> Nick . "\r\n" );
        }
        
        function 
    Pong () {
            
    $this -> Connect ( $this -> Host $this -> Port $this -> Nick );
            while(
    $text  socket_read ( $this -> IRCsocket 2048 )) {
                return 
    $text ;
            }
        }
    }
    $IRC  = new  IRC ;
    $IRC -> Connect ( "127.0.0.1" "6667" "BotMasterMC" );
    $IRC -> Pong ();
    ?>
    Es kommt zwar kein Error, aber auch keine Ausgabe und zum IRC connected er auch nicht, was isn da falsch ?
     
  9. 19. März 2011
    Zuletzt von einem Moderator bearbeitet: 15. April 2017
    AW: Per PHP Script Message in IRC CHannel senden

    nochmal, bitte lies dir den rfc durch: http://tools.ietf.org/html/rfc1459

    wo ist die endlosschleife die du benötigst um verbunden zu bleiben?
    wo ist die stelle bei der du die antworten vom server verarbeitest?

    es sieht sehr danach aus, als ob du nicht nur in sachen oop wenig erfahrung hast, sondern in php generell.

    ich empfehle dir daher eine anfrage in diesem thread zu stellen.
     
  10. 20. März 2011
    AW: Per PHP Script Message in IRC CHannel senden

    Ich komme mit PHP relativ gut klar, nur sind halt Sockets neuland für mich.

    Habe jetzt mit Sockets soweit einen IRC-Connect hinbekommen und auch das er einem Channel joint. Hab jetzt deine Funktionen von oben eingebaut und dann wie oben angegeben versucht sie anzuwenden, allerdings ohne jeglichen Erfolg, hat sich da vielleicht ein Fehler eingeschlichen ?

    PHP:
    <? php
    set_time_limit
    ( 0 );
    #ob_end_flush();
    $Host  "127.0.0.1" ;
    $Port  "6667" ;
    $Nick  "BotMasterMC" ;
    $Channel  "#hallo" ;

    function 
    set_timeout ( $fn $args $timeout ) {
        global 
    $__set_timeout ;

        if (!
    $__set_timeout )
            
    $__set_timeout  = array();

        return 
    array_push ( $__set_timeout , array( time () +  $timeout $fn $args )) -  1 ;
    }

    function 
    clear_timeout ( $id ) {
        global 
    $__set_timeout ;

        if (isset(
    $__set_timeout [ $id ]))
            unset(
    $__set_timeout [ $id ]);
    }

    function 
    exec_timeout ( $id ) {
        global 
    $__set_timeout ;

        if (!isset(
    $__set_timeout [ $id ]))
            return;

        list (, 
    $fn $args ) =  $__set_timeout [ $id ];
        unset(
    $__set_timeout [ $id ]);

        
    call_user_func ( $fn $args );
    }

    function 
    exec_timeouts () {
        global 
    $__set_timeout ;

        
    $now  time ();

        foreach (
    $__set_timeouts  as  $id  =>  $timeout )
            if (
    $timeout [ 0 ] <=  $now )
                
    exec_timeout ( $id );
    }

    function 
    SendCommand ( $cmd )
    {
        global 
    $socket ;
        
    socket_write ( $socket $cmd );
    }

    $socket  socket_create ( AF_INET SOCK_STREAM SOL_TCP );
    socket_connect ( $socket $Host $Port );
    socket_write ( $socket "USER Test Test Test :MaxTest\nNICK  $Nick \r\n" );
    while (
    $read  socket_read ( $socket 2048 )) {
        
    $hack  explode ( "\n" $read );
        foreach (
    $hack  as  $this_hack ) {
        
    $token  explode ( " " $this_hack );
        
    $id  explode ( "!" strtolower ( $token [ 0 ]));
        
    $nick  str_replace ( ":" "" $id [ 0 ]);
        
    $ident  explode ( "@" $id [ 1 ]);
        
    $address  $ident [ 1 ];
        
    $ident  $ident [ 0 ];
        
    $channel  str_replace ( ":" "" trim ( strtolower ( $token [ 2 ])));
        if (
    $token [ 0 ] ==  "PING" ) {  socket_write ( $socket "PONG  $token [ 1 ] \r\n" ); }
        
    socket_write ( $socket "JOIN  $Channel \r\n" );
        
    set_timeout ( 'SendCommand' 'PRIVMSG #hallo :hi\r\n' 10 );
        
    set_timeout ( 'SendCommand' 'PRIVMSG #hallo :hi2\r\n' 20 );
        
    set_timeout ( 'SendCommand' 'PRIVMSG #hallo :#logout\r\n' 30 );
        
    set_timeout ( 'SendCommand' 'QUIT Unexpected\r\n' 40 );
        
    exec_timeouts ();
        
    flush ();
        }
    }
    ?>
     
  11. 21. März 2011
    Zuletzt von einem Moderator bearbeitet: 15. April 2017
    AW: Per PHP Script Message in IRC CHannel senden

    sorry, aber da überschätzt du dich.

    copy & paste macht deinen code auch nicht besser.
    das wird nichts mehr.

    selbst wenn ich dir jetzt erkläre wie man das ganze macht, wüsstest du nachher auch nicht mehr, da du dich ja permanent gegen ratschläge verweigerst.

    in deinem script versucht dein "bot" permanent in den channel zu joinen und nachrichten zu schreiben...

    wie gesagt, such dir jemanden in diesem thread.

    closed!
     
  12. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.