Skip to content
Browse files

Merge branch 'ctcp' of https://github.com/thejh/node-irc into thejh-ctcp

Conflicts:
	lib/irc.js
  • Loading branch information...
2 parents afc39c3 + 5d0aa39 commit f9e68b1f92f9a6da90d2bd0dd4bebd2539b49ee8 @martynsmith committed Apr 15, 2012
Showing with 58 additions and 0 deletions.
  1. +34 −0 docs/API.rst
  2. +24 −0 lib/irc.js
View
34 docs/API.rst
@@ -88,6 +88,15 @@ Client
:param string target: is either a nickname, or a channel.
:param string message: the message to send to the target.
+.. js:function:: Client.ctcp(target, type, text)
+
+ Sends a CTCP message to the specified target.
+
+ :param string target: is either a nickname, or a channel.
+ :param string type: the type of the CTCP message, either "privmsg" for
+ a PRIVMSG or something else for a NOTICE.
+ :param string text: the CTCP message to send.
+
.. js:function:: Client.action(target, message)
Sends an action to the specified target.
@@ -280,6 +289,31 @@ Events
As per 'message' event but only emits when the message is direct to the client.
See the `raw` event for details on the `message` object.
+.. js:data:: 'ctcp'
+
+ `function (from, to, text, type) { }`
+
+ Emitted when a CTCP notice or privmsg was received (`type` is either `'notice'`
+ or `'privmsg'`).
+
+.. js:data:: 'ctcp-notice'
+
+ `function (from, to, text) { }`
+
+ Emitted when a CTCP notice was received.
+
+.. js:data:: 'ctcp-privmsg'
+
+ `function (from, to, text) { }`
+
+ Emitted when a CTCP privmsg was received.
+
+.. js:data:: 'ctcp-version'
+
+ `function (from, to) { }`
+
+ Emitted when a CTCP VERSION request was received.
+
.. js:data:: 'nick'
`function (oldnick, newnick, channels, message) { }`
View
24 lib/irc.js
@@ -122,6 +122,10 @@ function Client(server, nick, opt) {
to = null;
}
var text = message.args[1];
+ if (text[0] === '\1' && text.lastIndexOf('\1') > 0) {
+ self._handleCTCP(from, to, text, 'notice');
+ break;
+ }
self.emit('notice', from, to, text, message);
if ( self.opt.debug && to == self.nick )
@@ -373,6 +377,10 @@ function Client(server, nick, opt) {
var from = message.nick;
var to = message.args[0];
var text = message.args[1];
+ if (text[0] === '\1' && text.lastIndexOf('\1') > 0) {
+ self._handleCTCP(from, to, text, 'privmsg');
+ break;
+ }
self.emit('message', from, to, text, message);
if ( to.match(/^[&#]/) ) {
self.emit('message#', from, to, text, message);
@@ -710,6 +718,22 @@ Client.prototype._clearWhoisData = function(nick) { // {{{
delete this._whoisData[nick];
return data;
} // }}}
+Client.prototype._handleCTCP = function(from, to, text, type) {
+ text = text.slice(1)
+ text = text.slice(0, text.indexOf('\1'))
+ var parts = text.split(' ')
+ this.emit('ctcp', from, to, text, type)
+ this.emit('ctcp-'+type, from, to, text)
+ if (type === 'privmsg' && text === 'VERSION')
+ this.emit('ctcp-version', from, to)
+ if (parts[0] === 'ACTION' && parts.length > 1)
+ this.emit('action', from, to, parts.slice(1).join(' '))
+ if (parts[0] === 'PING' && type === 'privmsg' && parts.length > 1)
+ this.ctcp(from, 'notice', text)
+}
+Client.prototype.ctcp = function(to, type, text) {
+ return this[type === 'privmsg' ? 'say' : 'notice'](to, '\1'+text+'\1');
+}
/*
* parseMessage(line, stripColors)

0 comments on commit f9e68b1

Please sign in to comment.
Something went wrong with that request. Please try again.