So if you frequent IRC often like I do, you may notice some eggdrop bots displaying messages which come from a website. The most common times you will see this sort of script in action is when they are hooked into forum posts or torrent listings. You will more than likely have seen messages saying “New forum topic…X Y by Z” or “[PRE] such.and.such.movie.torrent”. Today I’m going to show you how to do that by sending messages to an eggdrop via PHP.

It’s actually relatively easy (as you are about to see) and really only requires a few lines of PHP code on the web end and the same again in TCL on the eggdrop. The general idea is that the eggdrop will open up a socket and sit listening waiting for data to come through before displaying the message. For the PHP end it is as simple as connecting to said socket and sending through some data.

First things first, the TCL script sock-listen.tcl:

set output_chan "#chan"
# eggdrop port to bindset 
port 7878
# eggdrop ip to bind
set host xxx.xxx.xxx.xxx
set serverSocket [socket -server main -myaddr $host $port]
proc main { sock host port } {
  fconfigure $sock -buffering line
  fileevent $sock readable [action $sock $host $port]
}
proc action { chan host port } {
  global output_chan
  if {![eof $chan]} {
    set soc_data [gets $chan]
    if {$soc_data != ""} {
      putquick "PRIVMSG $output_chan :$host | $port $soc_data"
    }
  } { close $chan }
}
putlog "sock-listen.tcl loaded"
TCL

That’s it! Simple enough. Create a socket which will sit and listen waiting for something to connect and send through data. Once a connection is made call the ‘action’ function which will  send a message to the channel with the host, port and socket data.

To get that up and running just copy sock-listen.tcl into the scripts folder of your eggdrop and include it in your config file. Don’t forget to change the channel, IP address and port. Once you rehash or reconnect the bot the socket will automatically start listening.

Now to send messages from PHP is very straight forward, just open up a socket and connect to the IP/port specified in the TCL script. Send through a message and watch it come out the other end (IRC channel).

To do this we create a php file called send.php and place the following code:

<?php
//eggdrop port
$egg_port = 7878;
//eggdrop ip
$egg_ip 'xxx.xxx.xxx.xxx';
// message to send
$msg 'Sent from a PHP page';
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, $egg_ip, $egg_port) or die ('failed to connect!');
socket_write($sock, $msg);
socket_close($sock);
?>
PHP

Again, easy enough. Make sure you set the correct IP address and port and then upload the file. Point your browser to it and watch the magic happen. You should see the following message appear on the IRC channel:

xxx.xxx.xxx.xxx | 7878 | Sent from a PHP page

To make it easier I’ve added a small form which will allow you to customize the message to your liking. Again send.php:

<?php
//eggdrop port$egg_port = 7878;
//eggdrop ip
$egg_ip 'xxx.xxx.xxx.xxx';

if (isset($_POST['message'])) {
  $msg $_POST['message'];   
  $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_connect($sock, $egg_ip, $egg_port) or die ('failed to connect!');
  socket_write($sock, $msg);
  socket_close($sock);
  header("Location: send.php");
}
?>
<form action='send.php' method='POST'>
<input type='text' name='message' cols='20'><br>
<input type='submit' value='send'>
</form>
PHP

It isn’t all that secure but gives you a general idea of what you can do. To implement it into the examples I posted at the start all you really need to do is edit the submit/post PHP files and place the socket code above with a customized message in.

This isn’t limited to just PHP. Something similar could easily be set up in a number or any other languages as well. Simply by opening the socket and sending the data.

Something simple for  the day. Have a play around and let me know if you make any improvements. Feel free to ask any questions, I’ll do my best to answer them :)

Also feel free to follow me on twitter: @JAGracie