0

Hello all!

I'm trying to make an IRCbot, but I have an issue I just can't seem to get through.

<?php
	class irc
	{
		// server connect
		public $host = "irc.freenode.net";
		public $port = 6665;
		public $channel = "#dinksmallwood";
		
		// client-side
		public $username = "GlennBOT";
		public $hostname = "ti0184a340-0784.bb.online.no";
		public $server = array();
		public $crap = 0;
		
		public function __construct()
		{
			error_reporting(E_ALL);
			set_time_limit(0);
		}
		
		public function ircConnect()
		{
			// Connect to IRC server
			$this->server['SOCKET'] = fsockopen($this->host, $this->port, $errno, $errstr, 2);
			// Needed headers in order to connect, otherwise we recieve error.
			$this->server['HEADERS'] .= "NICK " . $this->username . "\n\r";
			$this->server['HEADERS'] .= "USER " . $this->username . ' ' . $this->hostname . ' ' . $this->host . ' :' . $this->username . "\n\r";
			// Sending headers.
			$this->sendCmd($this->server['HEADERS'], true);
		}
		
		public function irc_loop()
		{
			if($this->server['SOCKET'])
			{
				while(!feof($this->server['SOCKET']))
				{		
					$this->server['READ_BUFFER'] = fgets($this->server['SOCKET'], 1024);
					echo "[RECEIVE] ".$this->server['READ_BUFFER']."\n\r";

					if (strpos($this->server['READ_BUFFER'], "376"))
					{
						$this->sendCmd("JOIN ".$this->channel." \n\r", true);
						$this->sendCmd("PRIVMSG ".$this->channel." Hello, I'm a baby \n\r", true);
					}
										
					if(substr($this->server['READ_BUFFER'], 0, 6) == "PING :") 
					{
						$this->sendCmd("PONG :".substr($this->server['READ_BUFFER'], 6)."\n\r", true);
					}
					flush(); 
				}
			}
		}
		
		public function sendCmd($command, $showConsole = false)
		{
        	@fputs($this->server['SOCKET'], $command, strlen($command));
			if ($showConsole == true)
			{
        		echo "[SEND] ".$command." <br>";
			}
		}
	}
?>

It really does connect and talks on startup, but because this script is stuck in the while(!feof($this->server)) loop, I can't seem to send commands to the server, since it's just checking after new responses from the server all the time.

Could anyone guide me here and tell me how I go about sending commands t the server while still looping and checkingfor server responses?

Thanks in advance!

2
Contributors
6
Replies
9
Views
7 Years
Discussion Span
Last Post by rbrt13
1

Hello,

First I would like to say that if you are only connecting to one server, that a class is completely unnecessary, however since you have a class started, I will try to help you make it work.

One thing that I would do (I know from experience that this makes life easier) is change $this->server['READ_BUFFER'] = fgets($this->server['SOCKET'], 1024); to $this->server['READ_BUFFER'] = explode (" ", trim (fgets($this->server['SOCKET'], 1024))); This allows many tasks to run smoother, especially complicated ones.

If you follow the previous advice, then you can simply check what the message is like this:

<?php
switch $this->server["READ_BUFFER"][3] {
  case ":some_command":
    do something;
    break;
  }
?>

If you have any questions, feel free to contact me, or refer to a simpler structured bot that I made a while back (http://downloads.rbrtdllnbck.com/r-bot-1.0.zip)

Hope I can be of some assistance, especially since PHP IRC bots are what I do most

Edited by rbrt13: My wording could have misportrayed what I was trying to say

Votes + Comments
Thanks for helping, but I don't understand much really by looking your "simple" bot, hehe. I added you to some IM's and hope we can get in touch and solve this issue!
0

Okey, so I sort of understand what you are trying to say, but by using the code you gave me, how will I make the bot join a channel using that code?

0

OK, this is where a knowledge of the IRC protocol comes in handy. You don't need to know all the bits of it, but it you use what i mentioned, then it is a simple as fputs ( $this->server["SOCKET"], "JOIN " . $this->server["READ_BUFFER"][4] . "\n" ); assuming that it is something like ".join #channel" that you are saying to it (IRC syntax for the join command to the server is "JOIN #channel\n")

Hope this helps answer your question a bit better

0

OK, this is where a knowledge of the IRC protocol comes in handy. You don't need to know all the bits of it, but it you use what i mentioned, then it is a simple as fputs ( $this->server["SOCKET"], "JOIN " . $this->server["READ_BUFFER"][4] . "\n" ); assuming that it is something like ".join #channel" that you are saying to it (IRC syntax for the join command to the server is "JOIN #channel\n")

Hope this helps answer your question a bit better

0

Yeah, I understand that now, thank you, but wouldn't this pose a problem in the loop? I mean, it's running an infinite loop because feof returns false and that would make it try to join the channel each time the loop is runned?

0

no, you set it up so that that command is only issues when the bot receives notification to join a channel

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.