Simple IRC Socket for usage with IRC libraries.
Simple IRC Socket - handles communication between an IRC server and consumers.
The socket is a thin wrapper around a generic socket,
npm install simple-irc-socket
var IrcSocket = require'simple-irc-socket';var myConnection = IrcSocketserver: 'irc.yournet.net'password: 'server-password'nick: 'aBot'user: 'node'realname: 'Node Simple Socket'port: 6667secure: falsecapab: true;
capab: true is passed to the configuration object the library will send
CAP LS first to initiate a capabilities negotiation.
A simple irc socket uses a
net.Socket socket by default. You can pass a
seperate generic socket instead in the second parameter.
If you pass
secure: true in the network configuration object, this parameter is ignored.
var myConnection = IrcSocket;mySocketonce'ready'mySocketend;mySocketconnect;
To send messages to the server, use socket.raw(). It accepts either a string or an array which it will convert into a string by joining with a space. The message '''must''' follow the [IRC protocol](https://irc-wiki.org/RFC 1459).
var details = ;var myConnection = Ircsocketdetails;mySocketconnect;mySocketonce'ready'// Using a string.mySocketraw"JOIN #biscuits";mySocketon'data'message = messagesplit" ";// Numeric 333 is sent when a user joins a channel.if message1 === "333" &&message2 === detailsnick &&message3 === "#biscuits"// Using an array instead of a string.mySocketraw"PRIVMSG" "#biscuits" ":Hello world.";mySocketon'data'// This is sent when you do /quit too.if messageslice0 5 === "ERROR"mySocketend;
The raw method does not allow the usage of newline characters. This is mostly a security concern, so that if the user of the Socket doesn't validate input, an evil user can't send a command causing the bot to quit:
<eviluser>!say SUCKAS \nQUIT :Mua ha ha
You do not need to handle PING messages. They are filtered from the messages emitted by the socket.
All other messages are emitted via a 'data' event. Receiving callbacks to this event will receive the message as the first parameter.
Examples of reading messages are in the previous example. Messages generally look like the following:
:irc.uk.mibbit.net 376 Havvy :End of /MOTD command.:NyanCat!Mibbit@mib-FFFFFFFF.redacted.com QUIT :Quit: ajax IRC ClientERROR :Closing Link: Havvy[127-00-00-00.redacted.com] (Quit: I got the messages I want.)
This method will return true when the socket is started, but not ended. It will otherwise return false.
This method returns the realname (also called gecos) of the connection.
The basic-irc-socket is an event emitter. It emits three events.
- ready(): Once the first 001 message has been acknowledged.
- data(message: String): Every message (including the 001) from the sender (inclusive) the the newline (exclusive).
- close(): Once the implementing socket has been closed.
Install jasmine-node globally, and then test via npm.
npm install -g jasmine-nodenpm test
The socket you pass gets ignored if you pass
secure: true in the network config.
The irc-message module will quickly parse the strings you pass into objects.
For a full IRC framework, take a look at Tennu.