<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kalkran.COM &#187; irc bot</title>
	<atom:link href="http://kalkran.com/tag/irc-bot/feed/" rel="self" type="application/rss+xml" />
	<link>http://kalkran.com</link>
	<description>Design&#38;Coding</description>
	<lastBuildDate>Fri, 26 Feb 2010 15:48:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>IRC Bot (part 1)</title>
		<link>http://kalkran.com/tutorials/irc-bot-part-1/</link>
		<comments>http://kalkran.com/tutorials/irc-bot-part-1/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 16:59:47 +0000</pubDate>
		<dc:creator>Kalkran</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[irc bot]]></category>
		<category><![CDATA[merry]]></category>
		<category><![CDATA[socket]]></category>

		<guid isPermaLink="false">http://www.kalkran.com/tutorials/kalkran/irc-bot-part-1/</guid>
		<description><![CDATA[Wow! I'm on a roll, tutorial inspiration just keeps coming! Sockets in PHP! Maybe you want to write your own IRC Bot (Like me ^^) or maybe you just want to check on a site every other minute. In Part One of this tutorial we'll cover connecting and staying alive on the IRC server. Without [...]]]></description>
			<content:encoded><![CDATA[<p>Wow! I'm on a roll, tutorial inspiration just keeps coming!</p>
<p>Sockets in PHP! Maybe you want to write your own IRC Bot (Like me ^^) or maybe you just want to check on a site every other minute. In <strong>Part One</strong> of this tutorial we'll cover connecting and staying alive on the IRC server.</p>
<p>Without further ado,  (apart from clicking the jump link)<br />
<span id="more-27"></span></p>
<h3>Opening a connection</h3>
<p>We need to be connected before we can send or receive data from the server. We can open a connection with php by using the fsockopen(); function. It works like this:</p>
<pre class="php">&nbsp;
<a href="http://www.php.net/fsockopen"><span style="color: #000066;">fsockopen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$host</span>, <span style="color: #0000ff;">$port</span>, <span style="color: #0000ff;">$errno</span>, <span style="color: #0000ff;">$errstr</span>, <span style="color: #0000ff;">$timeout</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>The $host can be either a numerical IP address or a hostname (www.kalkran.com for example). $port doesn't really require any explanation. If there are any errors, the fsockopen() function will enter the error number and error string in the $errno and $errstr variables. It does that by using pointers to these strings. $timeout is an integer, that specifies the amount of time it should try before giving up. The function returns a variable, which you'll need to be able to read and write to the connection, so be sure to save it. For this tutorial we'll write an IRC bot:</p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$server</span> = <span style="color: #ff0000;">&quot;ns2.idlemonkeys.net&quot;</span>;
<span style="color: #808080; font-style: italic;">// Set the time limit of the php script to 0. (Continue forever)</span>
	<a href="http://www.php.net/set_time_limit"><span style="color: #000066;">set_time_limit</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Connect and save the handle to $f.</span>
	<span style="color: #0000ff;">$f</span> = <a href="http://www.php.net/fsockopen"><span style="color: #000066;">fsockopen</span></a><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/gethostbyname"><span style="color: #000066;">gethostbyname</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$server</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">6667</span>, <span style="color: #0000ff;">$errno</span>, <span style="color: #0000ff;">$errstr</span>, <span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Error Handling (If we don't have a handle)</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$f</span><span style="color: #66cc66;">&#41;</span>
	<a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Error while connecting!#&quot;</span> . <span style="color: #0000ff;">$errno</span> . <span style="color: #ff0000;">&quot; &quot;</span> . <span style="color: #0000ff;">$errstr</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Should print along the lines of:</span>
	<span style="color: #808080; font-style: italic;">// Error while connecting!</span>
	<span style="color: #808080; font-style: italic;">// #1 Unable to connect!</span></pre>
<p>The timeout is set to negative one so it will <strong>never</strong> reach that value, thus keep going for ever.</p>
<h3>Identifying ourselves</h3>
<p>Okay, now we have a connection to the IRC server. That's good, right? Yeah!<br />
So, we are on the server. Now (according to the IRC protocol), we should send our details to the server.</p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$userdata</span> = <span style="color: #ff0000;">&quot;NICK Merry<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>; <span style="color: #808080; font-style: italic;">// We'll pretend to be called Merry :)</span>
<span style="color: #0000ff;">$userdata</span> .= <span style="color: #ff0000;">&quot;USER k <span style="color: #000099; font-weight: bold;">\&quot;</span>gmail.com<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> . <span style="color: #0000ff;">$server</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> :k<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>; <span style="color: #808080; font-style: italic;">// User k &quot;gmail.com&quot; ns2.idlemonkeys.net</span>
<span style="color: #0000ff;">$userdata</span> .= <span style="color: #ff0000;">&quot;USERHOST Kalkran<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>; <span style="color: #808080; font-style: italic;">// Merry!Kalkran@gmail.com :D</span>
<span style="color: #0000ff;">$userdata</span> .= <span style="color: #ff0000;">&quot;MODE Merry +B<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>; <span style="color: #808080; font-style: italic;">// Mode +B = Bot!</span>
<span style="color: #0000ff;">$userdata</span> .= <span style="color: #ff0000;">&quot;JOIN #kalkran<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>; <span style="color: #808080; font-style: italic;">// Join the #kalkran channel</span>
<span style="color: #808080; font-style: italic;">// Now actually send the data, all at once.</span>
@<a href="http://www.php.net/fputs"><span style="color: #000066;">fputs</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$f</span>, <span style="color: #0000ff;">$userdata</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>Wow. We're now connected as "Merry" to the $server. We've set mode +B to let it know we're a bot and we've joined the #kalkran channel.<br />
But.. Why did I write</p>
<pre class="php">&nbsp;
@<a href="http://www.php.net/fputs"><span style="color: #000066;">fputs</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$f</span>, <span style="color: #0000ff;">$userdata</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>The @ suppresses any error / warning messages.  The fputs() function has two parameters we will be using, the first one is the file handle to which we'll write (or the socket handle in this case) and the second parameter is the actual message we want to send. Make sure to always end your messages with a <strong>\r\n</strong> when connected to IRC. It will read your command, but won't really do anything until it receives a \r\n which is basically a hit on the Return key (Enter?).</p>
<h3>Staying alive</h3>
<p>If you've tried out your bot already, you'll notice it enters #kalkran all fine, but it disconnects after a while thanks to a <strong>PING TIMEOUT</strong>. We'll fix this now.<br />
Firstly, we might want to start a loop that ends when the connection is closed,  just so we don't go through it all and then close the connection. We'll be using a while() loop to keep reading the connection.</p>
<pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">// While the handle still exists and not at the end of file yet :)</span>
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$f</span> || !<a href="http://www.php.net/feof"><span style="color: #000066;">feof</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$f</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0000ff;">$in</span> = <a href="http://www.php.net/fread"><span style="color: #000066;">fread</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$f</span>, <span style="color: #cc66cc;">512</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Read until we have 512 bytes at a time. Save the results in $in.</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/eregi"><span style="color: #000066;">eregi</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;PING&quot;</span>, <span style="color: #0000ff;">$in</span><span style="color: #66cc66;">&#41;</span> &amp;amp;&amp;amp; !<a href="http://www.php.net/eregi"><span style="color: #000066;">eregi</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;PRIVMSG&quot;</span>, <span style="color: #0000ff;">$in</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		@<a href="http://www.php.net/fputs"><span style="color: #000066;">fputs</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$f</span>, <span style="color: #ff0000;">&quot;PONG &quot;</span> . <a href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$in</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// End of loop</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>This is the end of disconnecting due to PING TIMEOUTs.<br />
fread() has two arguments, first one being the handle, second one being the amount of bytes to read.</p>
<pre class="php">&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/eregi"><span style="color: #000066;">eregi</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;PING&quot;</span>, <span style="color: #0000ff;">$in</span> &amp;amp;&amp;amp; !<a href="http://www.php.net/eregi"><span style="color: #000066;">eregi</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;PRIVMSG&quot;</span>, <span style="color: #0000ff;">$in</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre>
<p>If the server sends a "PING" and it's not in a channel / query (They are always "PRIVMSG"s), send back a "PONG %s".<br />
Example:</p>
<pre>
PING idlemonkeys.net
PONG idlemonkeys.net</pre>
<p>That's what the substr() does. It sends back whatever it got from the server. If it would say PING Say hi it would send back PONG Say hi.</p>
<p>That's it for this tutorial. Complete code is attached.</p>
<p><a href="http://www.kalkran.com/wp-content/uploads/2008/03/merry-part1phps.txt" title="IRC Bot (part 1)">IRC Bot (part 1)</a></p>
<img src="http://kalkran.com/?ak_action=api_record_view&id=27&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://kalkran.com/tutorials/irc-bot-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

