How to respond on irc with php

Questions about programming languages and debugging
Post Reply
PopPooB
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 236
Joined: 16 May 2007, 16:00
16
Location: CandyLand
Contact:

How to respond on irc with php

Post by PopPooB »

I got an irc bot but now how do i make it say something in response to someone saying something else

Code: Select all

<?php  
$host = "irc.suck-oold.com"; 
$port=6667; 
$nick="Melina"; 
$ident="Melina"; 
$chan="#suck-o"; 
$readbuffer=""; 
$realname = "Melina"; 

$fp = fsockopen($host, $port, $erno, $errstr, 30); 


if (!$fp) { 
    echo $errstr." (".$errno.")<br />\n"; 
} else { 
    fwrite($fp, "NICK ".$nick."\r\n"); 
    fwrite($fp, "USER ".$ident." ".$host." bla :".$realname."\r\n"); 
    fwrite($fp, "JOIN :".$chan."\r\n"); 
      
    fwrite($fp, "PRIVMSG ".$chan." :Hello World!\r\n"); 
    fwrite($fp, "PRIVMSG ".$chan." :Melina is testing =]\r\n"); 

     while (!feof($fp)) { 
          
        $line =  fgets($fp, 128); 
        echo $line."\n"; 
         
        $line = explode(":ping ", $line); 
         
        echo $line[0]."\n"; 

        if ($line[1]) { 
             
            fwrite($fp, "PONG ".$line[1]."\r\n");  
        } 

    } 
     
    fclose($fp); 
} 
?> 
96% better then all connections global
Kiss My Ass Good Bye

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Well, as you can see in this code

Code: Select all

while (!feof($fp)) {
         
        $line =  fgets($fp, 128);
        echo $line."\n";
         
        $line = explode(":ping ", $line);
         
        echo $line[0]."\n";

        if ($line[1]) {
             
            fwrite($fp, "PONG ".$line[1]."\r\n"); 
        } 
it explodes the raw data in an array... all you have to do is make a bunch of if's that reacts to the different parts of the data. By just observing the packets from and to an IRC server, you will get the idea pretty fast.

Try with using Wireshark
"The best place to hide a tree, is in a forest"

PopPooB
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 236
Joined: 16 May 2007, 16:00
16
Location: CandyLand
Contact:

Post by PopPooB »

Code: Select all

\if (strpos($line, "PRIVMSG ".$channel." :hi")>0){
fwrite($socket, "PRIVMSG ".$channel." :"Hello"\r\n");
hows that looking?
96% better then all connections global
Kiss My Ass Good Bye

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

PopPooB wrote:

Code: Select all

\if (strpos($line, "PRIVMSG ".$channel." :hi")>0){
fwrite($socket, "PRIVMSG ".$channel." :"Hello"\r\n");
hows that looking?

hmm well, my head is to messed up to look in your code and give you any good help (been doing Trigonometry all day, messes you up after 12 hours I tell ya). But I can give you a hint...

Say that you have a packet that looks like this

Code: Select all

PRIVMSG #suck-o :frinds
and you explode it into an array, giving the following

array[0] = PRIVMSG
array[1] = #suck-o
array[2] = :frinds

now, what I usually do is that I try to remove useless stuff like the ":", but that depends on how you want it to function, I mean you might as well use the ":" for identifying the first word, etc, etc...

Anyway, a common line for detecting when someone says "frinds" (missed an 'e') could look like this...

if(array[0] == "PRIVMSG") {

if(array[2] == "frinds") {
privmsg("#suck-o", "yes, I am your friend!");
}
}

of course, this is assuming you made "privmsg" a function that can handle the two parameters which are "channel" and "message" that will be sent.

An example of that function is taken from my own bot...

Code: Select all

function privmsg ($to, $message) {

	return send_data("PRIVMSG $to :$message");
}
and for that I use a send data function (used for sending uncommon data to the server)

Code: Select all

function send_data($message) {

	fputs($_SESSION['SOCKET'], "$message\r\n", 512);
}

anyway, hope this gives you an idea =)
"The best place to hide a tree, is in a forest"

Post Reply