Closed Thread Icon

Topic awaiting preservation: PHP & Sockets (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=12974" title="Pages that link to Topic awaiting preservation: PHP &amp;amp; Sockets (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP &amp; Sockets <span class="small">(Page 1 of 1)</span>\

 
Trigger
Paranoid (IV) Inmate

From:
Insane since: Jun 2002

posted posted 11-11-2003 23:13

Hey theres some people who are really great at PHP here and im sure some of you must be familiar with the IRC protocol so can you take a look at this please?

code:
<?
error_reporting (E_ALL); // Give out all errors .
$server = "irc.zoite.net";
$port = "6667";
$nick ="MyIrcBot";
$realname = "Php Test bot";
$channel = "#hackingzone";

$sock = socket_create(AF_INET, SOCK_STREAM, 0);
if(!$sock)
{
print "Error socket could not be created";
exit;
}

$connect = socket_connect($sock,$server,$port);
if(!$connect)
{
print "Error connection could not be made to $server on $port";
exit;
}

socket_write($sock,"USER RHAP RHAP RHAP :RHAP\r\n"); // Send user name to the server
socket_write($sock,"NICK Wispybot \r\n"); // Set your nickname
socket_write($sock,"JOIN #hackingzone \r\n"); // join channel

while($data = socket_read($sock,2046,PHP_NORMAL_READ)) //listen for any data, and print it out
{
//If the value contains ping then run the replacement function and
//send the changed string to the server
if(preg_match("/PING/","$data")){
$pong = substr_replace($data,"PONG",0,4);
socket_write($sock,"$pong \r\n"); //
print $pong;
}
echo $data . "<br>";
}
close_socket($sock);

?>



Ok I know the ircg functions exsist but I cant use them due to my host not supporting them and so I went for sockets.
now so far the script manges to connect to the server, set the username print out the MOTD and stay connected by sending back a pong for each ping.

unfortuantly my "ping handeler" is a bit unefficent because it reads every line of data and checks it for ping. so if the bot does join a channel and some one says the word ping its going to send the irc server a "pong" :S
it also takes in every line which really is unefficent...

So has any one got any Ideas how I could deal with the PING/PONG situation a bit more efficently and has anyone got any Ideas for makeing my code run just a little bit faster?

its not much as of yet its the very bare bones of a irc interface.. but its part of a learning curve,
any input, would be appreciated

Thanks
Trigger

[url=http://hackingzone.org/]
Fatal Industries -- The-bronze webhosting

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 11-11-2003 23:22

Um this isn't too complicated, I wouldn't make too many suggestions at this point except adding the begin line character to your PING preg

this: preg_match("/PING/","$data")){
to this: preg_match("/^PING/","$data")){





.:[ Never resist a perfect moment ]:.

Trigger
Paranoid (IV) Inmate

From:
Insane since: Jun 2002

posted posted 11-11-2003 23:38

Ok BitDamaged thanks for the speedy reply

so no ideas at all how I could more eficently deal with the ping? :S

EDIT : OK it turns out my script dosent work it gets half way down the MOTD and then just hangs.. and dosent finsh doing what its doing....
I think its because im trying to recive too much data and not handeling it correclty..
any Ideas?

[This message has been edited by Trigger (edited 11-11-2003).]

Trigger
Paranoid (IV) Inmate

From:
Insane since: Jun 2002

posted posted 11-12-2003 17:30

*bump*

[url=http://hackingzone.org/]
Fatal Industries -- The-bronze webhosting

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 11-12-2003 17:55

Well since socket_read stops at a newline or carriage return (\n\r), if there are two returns in the message of the day, your read would be empty and would evaluate to false, thus ending the loop.

The other bigger problem I suspect is that as soon as the script is over the connection will be closed, so the bot can't persist on the server. If you just want it to login, do something, and then disconnect then this will be fine. If, however, you want it to stay connected and have some sort of web interface I think you'll need some sort of non-php component (well maybe PHP on the command line would work) to maintain the connection and then talk with your web pages.

-jiblet

Trigger
Paranoid (IV) Inmate

From:
Insane since: Jun 2002

posted posted 11-12-2003 18:38

hmm ok.... that could be the problem
I think I need to have a bit more of a read up on sockets before I continue with this then


Thanks for your help
Trigger


Fatal Industries -- The-bronze webhosting

Trigger
Paranoid (IV) Inmate

From:
Insane since: Jun 2002

posted posted 01-09-2004 23:30

Ok I know this is a preety dead thread, but I was flicking threw my projects on hold and I PHPBot came back up.. I've now got it to join the channel and stay on,and it even responds to some trigger commands
heres the code

code:
<?php

function blog($stuff){
$fp = fopen("bot.log", "a");
fwrite($fp, $stuff."\r\n");
fclose($fp);
}

blog('#########################################');
set_time_limit(0);
$server = "sexy.zoite.net";
$port = "6667";
$nick ="DH_bot";
$realname = "Php bot blame SE";

$sock = socket_create(AF_INET, SOCK_STREAM, 0);
blog('# socket created');
$connect = socket_connect($sock,$server,$port);
if(!$connect){
echo "AGGH!no connection :S";
exit;
}
blog('# connected');
$state = "1";

socket_write($sock,"USER $realname\r\n");
socket_write($sock,"NICK $nick \r\n");
blog('> USER '.$realname);
blog('> NICK '.$nick);
blog('# entering main loop');
while ($state == 1) {
$sread = array($sock);
$result = socket_select($sread, $w = null, $e = null, 0, 300000);
if($result == 1){
$rawdata = socket_read($sock, 10240);
} else {
$rawdata = null;
}

if($rawdata !== null && !empty($rawdata)) {
blog($rawdata);
if(strstr($rawdata, "PING")){
$position = strpos($rawdata, "PING");
$ping = substr($rawdata, $position, "12");
$pong = substr_replace($ping,"PONG",0,4);
socket_write($sock,"$pong \r\n");
blog('> ping recived! replying with "'.$pong.'"');
} else if(strstr($rawdata, "quit_please")) {
socket_write($sock,"QUIT bye\r\n");
blog('> QUIT');
exit;
} else if(strstr($rawdata, "%j")) {
$jpos = strpos($rawdata, "%j");
$room = substr($rawdata, $jpos+2);
socket_write($sock,"JOIN $room\r\n");
blog('> JOIN #inf0junk');
} else if(strstr($rawdata, "%say")) {
$saypos = strpos($rawdata, "%say");
$saythis = substr($rawdata, $saypos+4);
socket_write($sock,"PRIVMSG $saythis\r\n");
blog('> PRIVMSG'.$saythis);
}
}
}

?>


any comments or suggestions on how I could improve it from this would be greatly appreciated..
Note its excuetd via a web browser and not a console..



[This message has been edited by Trigger (edited 01-09-2004).]

« BackwardsOnwards »

Show Forum Drop Down Menu