Editing IRC Client

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 18: Line 18:
</pre>
</pre>
Upon successful registration the server will respond with a numeric messages 001..004.
Upon successful registration the server will respond with a numeric messages 001..004.
This is the complete code for a basic "bot" that tests some code and tells teh time
<pre>
package
{
import flash.display.Sprite;
import flash.net.Socket;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.KeyboardEvent;
import flash.text.TextField;
public class IRCc extends Sprite
{
public var keyHit:Sprite;
public var text_txt:TextField;
private var socket:Socket;
private var hostPath:String = "irc.freenode.net";
private var hostPort:int = 6667;
private var nickname:String = "debugUser";
private var username:String = "Test User";
private var hostname:String = "TEST-HOST";
private var realname:String = "NO NAME";
private var connectionState:String = "INIT";
public function IRCc()
{
text_txt.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
initSocket();
}
private function initSocket():void
{
socket = new Socket(hostPath, hostPort);
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
connectionState = "CONNECTING";
}
private function sendNick():void
{
/*
Command: NICK
  Parameters: <nickname> [ <hopcount> ]
Command: USER
Parameters: <username> <hostname> <servername> <realname>
*/
// connectionState = "HANDSHAKE";
socket.writeUTFBytes("NICK " + nickname + "\r\n");
socket.writeUTFBytes("USER " + username + " " + hostname + " " + hostPath + ": " + realname + "\r\n");
socket.flush();
}
private function quit():void
{
trace("quit");
connectionState = "QUIT";
socket.writeUTFBytes("QUIT :i quit there 4...\r\n");
socket.flush();
}
private function joinChannel(channel:String):void
{
trace("JOIN: " + channel);
socket.writeUTFBytes("JOIN " + channel + "\r\n");
socket.flush();
}
private function sendMessage(msg:String):void
{
trace("MSG: " + msg);
socket.writeUTFBytes("PRIVMSG #fort :test\r\n");
socket.flush();
}
private function sendTime():void
{
var now:Date = new Date();
trace("TIME: " + now.toString());
socket.writeUTFBytes("PRIVMSG #fort :teh time is now " + now.toString() + "\r\n");
socket.flush();
}
private function pongServer(daemon:String):void
{
trace("PONG: *" + daemon + "*");
socket.writeUTFBytes("PONG " + daemon + "\r\n");
socket.flush();
}
private function readData():void
{
var socketData:String = socket.readUTFBytes(socket.bytesAvailable);
trace(socketData);
if (socketData.split(' ', 1)[0] == "PING")
{
// PING Command, send PONG
pongServer(socketData.split(':')[1]);
}
else
{
var msg:String = socketData.substr(socketData.lastIndexOf(":"));
trace("MSG: " + msg);
if (msg.toLowerCase().indexOf("time") >= 0)
{
sendTime();
}
else if (msg.toLowerCase().indexOf("test") >= 0)
{
sendMessage("test");
}
}
}
private function socketDataHandler(e:ProgressEvent):void
{
trace("Data");
readData();
}
private function connectHandler(e:Event):void
{
trace("Connect");
connectionState = "CONNECTED";
sendNick();
}
private function closeHandler(e:Event):void
{
trace("Close");
connectionState = "CLOSED";
}
private function keyHandler(e:KeyboardEvent):void
{
trace("key: " + e.keyCode);
if (e.keyCode == 74)
{
joinChannel("#fort");
}
else if (e.keyCode == 77)
{
sendMessage("test");
}
else if (e.keyCode == 84)
{
sendTime();
}
else if (e.keyCode == 81)
{
quit();
}
}
}
}
</pre>


Sample connection to irc.freenode.net, captured socket data to/from babel client.
Sample connection to irc.freenode.net, captured socket data to/from babel client.
Please note that all contributions to Noisebridge are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Noisebridge:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel Editing help (opens in new window)