Jump to content

PHP IRC Bot Help


LeyZe

Recommended Posts

I am messing around with PHP and the IRC protocol. I am just running into a few errors maybe someone here would know the solution to.

 

I would like to make this bot auto-join the specified channels on start-up, however, the JOIN message must be sent AFTER the servers first PING request. If you try to send the JOIN message before, Quakenet (maybe other networks) tells you that you must first register, then disconnects you. Right now, I had it so it would join the channels when the MOTD is finished being displayed, however that just causes the script to hang and not get past Ident checking (see below quote)

 

NOTICE AUTH :*** Looking up your hostname

 

NOTICE AUTH :*** Checking Ident

 

NOTICE AUTH :*** Your forward and reverse DNS do not match, ignoring hostname.

 

NOTICE AUTH :*** No ident response

 

 

My next (and final issue) is that sometimes the bot doesn't send the PONG after the first PING, so the script hangs up there, and gives a similar message as the one in the quote above. How can I fix this hang up?

 

<?php

$chans = array("#randomchan");

$bot = new bot($chans, "PHP-Test-Bot");

class bot {

function __construct($chans = array(), $ident, $server = "irc.quakenet.org", $nick = "LeyZeBot", $port = 6667) {
	$this->server($server);
	$this->nick($nick);
	$this->port($port);
	$this->ident($ident);
	$this->chans = $chans;
	$this->main();
}

function join () {

	if (count($this->chans) == 0) return "Error";

	for ($i = 0; $i < count($this->chans); $i++) {
		$this->send("JOIN " . $this->chans[$i]);
	}
}

function main() {

	$this->connect();
	$buffer = 512;

	while ($data = socket_read($this->sock(), $buffer)) {
		echo $data;

		$in = explode(" ", trim($data));

		if ($in[0] == "PING") {
			$this->send("PONG " . $in[1]);
		}

		// TODO:
		// need to add auto-join channels on start up
		//if ($in[2] == ":End") {
		//	$this->join();
		//}

		$command = str_replace(chr(10), "", $in[3]);
		$command = str_replace(chr(13), "", $in[3]);

		if ($command == ":!quit") {
			$this->send("PRIVMSG :" . $in[2] . " Quitting IRC");
			$this->send("QUIT :Closing stream.");
			die();
		} else if ($command == ":!join" && $in[4] != NULL) {
			$this->send("JOIN " . $in[4]);
			$this->send("PRIVMSG #randomchan :yo lol");
		} else if ($command == ":!part") {
			if ($in[4] == NULL) {
				$this->send("PART " . $in[2]);
			} else {
				$this->send("PART " . $in[4]);
			}
		} 

	}

}


function send($data) {
	echo "[DEBUG]: $data\r\n";
	socket_write($this->sock(), $data . "\r\n");

}

function connect() {

	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

	if (!$socket) die("Could not create socket.");

	$socket_connect = socket_connect($socket, $this->server(), $this->port());

	if (!$socket_connect) die("Could not connect to socket.");

	$this->sock($socket);
	$this->send("NICK " . $this->nick());
	$this->send("USER " . $this->ident() . " mikhail " . $this->nick() . " :" . $this->nick());

}


function sock($s = NULL) {
	if (!$s)
		return $this->socket;
	$this->socket = $s;
}

function ident($i = NULL) {
	if (!$i)
		return $this->ident;
	$this->ident = $i;
}

function port($p = NULL) {
	if (!$p)
		return $this->port;
	$this->port = $p;
}

function nick($n = NULL) {
	if (!$n)
		return $this->nick;
	$this->nick = $n;
}

function server($s = NULL) {
	if (!$s)
		return $this->server;
	$this->server = $s;
}

}

?>

 

After I get these kinks out, I'm going to be splitting the bot into separate classes and be making it a bit more complex. I know the "commands" for the bot can be done a better way, I just have these commands so I can control the script for now. I will be rewriting it.

Link to comment
https://forums.phpfreaks.com/topic/154665-php-irc-bot-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.