Simple Integration of PJIRC with SMF
I wrote this simple integration just for my own forum, though after a few requests from some friends running their own SMF forums, I'm going to outline just how I did this.
The absolute first thing you need to do is to get and upload some Java files. These are PJIRC, a popular Java IRC Applet. Get them here: SMF IRC Java Files. Upload these files to your forum's base directory.
The chat feature is accesed through the forum, as in via yourforum/index.php?action=chat. We're going to setup this custom "action" ourselves. Don't worry, this is very easy.
Navigate to your forum's base directory, and open up the index.php file. We need to add a new array for when "chat" is requested. Scroll down until you see the following snippet in the file:
// Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function).
$actionArray = array(
You'll see a long list of all the current valid requests. All we need to do here is add in a new line to handle a "chat" request, so add this: 'chat' => array('Chat.php', 'Chat'), .
Here's how mine looks after adding this:
// Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function).
$actionArray = array(
'activate' => array('Register.php', 'Activate'),
'chat' => array('Chat.php', 'Chat'),
As you can see, I've added "chat" after the activate one. That's all we need to do in index.php, so save this file and re-upload it to your forum's base directory. From here-on we'll be creating new files.
The first file we need to create will be called Chat.php. Note the capitalisation! In this file we only need a few lines. Add this to your file:
<?php
if (!defined('SMF'))
die('Hacking attempt...');function chat() {
global $context;
$context['page_title'] = '#Crankeye @ irc.rmrk.net - RMRK\'s IRC Channel';
loadTemplate('Chat');
}
?>
As you can see, there are some things you'll want to edit for your own SMF forum. Namely, the page title variable. Remember to escape single quotes if you use any, like I have.
Once you've saved this file, upload it into the /Sources directory of your SMF directory.
The final file we need to create is a Chat.template.php file. This is the main page that your users will see and it's where we embed the Java files.
<?php
function template_main()
{
global $context, $settings, $options, $txt, $scripturl;//Some stuff to fix up the username first!
$badchar = array(" ");
$newchar = array("_");
$chatusername = str_replace($badchar, $newchar, $context['user']['name']);echo '
<style type="text/css">
.chat_header
{
font-family: \'trebuchet ms\', sans-serif;
font-size: 30px;
font-weight: bold;
letter-spacing: -2px;
line-height: 1em;
margin-bottom: 100px;
margin-top: 10px;
}
</style><table width="100%" border="0" cellspacing="0" cellpadding="3" >
<tr>
<td>', theme_linktree(), '</td>
</tr>
</table><table width="100%" border="0" cellspacing="0" cellpadding="4" align="center" class="tborder">
<tr class="titlebg">
<td align="center" ><span class="chat_header">RMRK Chat - #Crankeye</span></td>
</tr><tr>
<td class="windowbg"><center><table><tr><td class="windowbg2">
<applet name="applet" code=IRCApplet.class archive="irc.jar,pixx.jar" width=640 height=400>
<param name="CABINETS" value="irc.cab,securedirc.cab,pixx.cab"><param name="nick" value="',$chatusername,'">
<param name="alternatenick" value="Guest???">
<param name="name" value="Japplet">
<param name="host" value="irc.acidchat.net">
<param name="gui" value="pixx">
<param name="command1" value="/join #Crankeye">
<param name="quitmessage" value="Quit message!">
<param name="style:highlightlinks" value="true"></applet>
</td></tr></table><br /><br /></center>
</td></tr></table>
</td>
</tr>
</table><br /><br />
';
}?>
Quite a mouthful! Skimming through the above you should notice various bits that are specific and should be changed. I'll take you on a tour:
$badchar = array(" ");
$newchar = array("_");
$chatusername = str_replace($badchar, $newchar, $context['user']['name']);
This is just a really quick basic username fixing snippet, and works on the assumption that the username of almost anybody on your forum is likely to be made up of letters, numbers and spaces. You can't have spaces in an IRC nick, so this will replace spaces with an underscore, which is fine. Without this, a member named King Anesis would enter chat as King, though with this, he would enter as King_Anesis.
Skip ahead until you come to:
<span class="chat_header">RMRK Chat - #Crankeye</span>
You'll of course want to change this to suit your own forum. This is the header that will appear for the page, before the applet. Remember that you are writing inside a PHP echo statement, so if you use a single quote (') anywhere in your text, you must escape it (\') instead.
The applet embedding code needs no editing. Since you uploaded the Java files to your forum's base directory, this page will find them and load them (along with the language files) fine.
Skip ahead some more and we come to the params:
<param name="nick" value="',$chatusername,'">
<param name="alternatenick" value="Guest???">
<param name="name" value="Japplet">
<param name="host" value="irc.acidchat.net">
<param name="gui" value="pixx">
<param name="command1" value="/join #Crankeye">
<param name="quitmessage" value="Quit message!">
<param name="style:highlightlinks" value="true">
These should be obvious enough. The "alternatenick" value can have whatever you like in it, though it's a good idea to include at least 2 question marks in it. These question marks will be replaced by PJIRC with random numbers, ensuring that if there's a guest currently on, new guests will still be able to join. Change the "host" value to point to the IRC network that your own channel is on.
The "command1" value is a command that the client will automatically send upon connecting. Since you want the person to automatically be in your channel, we set this to "/join #yourchannel".
This is pretty much it! You can add whatever extra HTML underneath, around or above the applet if you wanted to.
One neat thing you can do is add buttons onto the chat page that will send data to the applet. Here's an example:
<FORM>
<INPUT TYPE=BUTTON VALUE="Smile" onClick="document.applet.setFieldText(document.applet.getFieldText()+\'=)\');document.applet.requestSourceFocus()">
<INPUT TYPE=BUTTON VALUE="How awesome is RMRK?" onClick="document.applet.sendString(\'RMRK ROX\')">
</FORM>
The first one simply puts text into the text field of the applet. The second one sends the text to the channel automatically, without the user having to send it.
Silverline :: Jun.06.2007 :: SMF :: 1 Comment »
Awesome.