Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Added CTCP responses #115

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+52 −30
Split
View
@@ -18,6 +18,7 @@ Client
{
userName: 'nodebot',
realName: 'nodeJS IRC client',
+ userInfo: 'node user',
port: 6667,
debug: false,
showErrors: false,
@@ -311,26 +312,19 @@ Events
`function (from, to, text, type) { }`
- Emitted when a CTCP notice or privmsg was received (`type` is either `'notice'`
- or `'privmsg'`).
+ Emitted when a CTCP request/reply is received (`type` is either 'request' or 'reply').
-.. js:data:: 'ctcp-notice'
+.. js:data:: 'ctcp-request'
`function (from, to, text) { }`
- Emitted when a CTCP notice was received.
+ Emitted when a CTCP request is received.
-.. js:data:: 'ctcp-privmsg'
+.. js:data:: 'ctcp-reply'
`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.
+ Emitted when a CTCP reply is received.
.. js:data:: 'nick'
View
@@ -35,6 +35,7 @@ function Client(server, nick, opt) {
password: null,
userName: 'nodebot',
realName: 'nodeJS IRC client',
+ userInfo: 'node user',
port: 6667,
debug: false,
showErrors: false,
@@ -206,7 +207,7 @@ function Client(server, nick, opt) {
}
var text = message.args[1];
if (text[0] === '\1' && text.lastIndexOf('\1') > 0) {
- self._handleCTCP(from, to, text, 'notice');
+ self._handleCTCP(from, to, text, 'reply');
break;
}
self.emit('notice', from, to, text, message);
@@ -466,7 +467,7 @@ function Client(server, nick, opt) {
var to = message.args[0];
var text = message.args[1];
if (text[0] === '\1' && text.lastIndexOf('\1') > 0) {
- self._handleCTCP(from, to, text, 'privmsg');
+ self._handleCTCP(from, to, text, 'request');
break;
}
self.emit('message', from, to, text, message);
@@ -812,22 +813,49 @@ 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');
-}
+Client.prototype._handleCTCP = function(from, to, text, type) { // {{{
+ text = text.slice(1);
+ text = text.slice(0, text.indexOf('\1'));
+ var parts = text.match(/([^\s]*)(?:\s+)?(.*)/).slice(1);
+ if (parts[0] === 'ACTION' && parts[1].length > 1) {
+ return this.emit('action', from, to, parts[1]);
+ } else if (type === 'request') {
+ var request = parts[0].toUpperCase(),
+ query = parts[1];
+ switch (request) {
+ case 'CLIENTINFO':
+ this.ctcp(from, 'reply', 'CLIENTINFO VERSION USERINFO CLIENTINFO ERRMSG PING TIME');
+ break;
+ case 'ERRMSG':
+ this.ctcp(from, 'reply', 'ERRMSG '+query+' :No error has occurred');
+ break;
+ case 'PING':
+ timestamp = new Date().getTime();
+ this.ctcp(from, 'reply', 'PING '+timestamp);
+ break;
+ case 'TIME':
+ var now = new Date().toString();
+ this.ctcp(from, 'reply', 'TIME :'+now); //format \001TIME :human-readable-datetime\001
+ break;
+ case 'USERINFO':
+ this.ctcp(from, 'reply', 'USERINFO :'+this.opt.userInfo); //format \001USERINFO :client-set-string\001
+ break;
+ case 'VERSION':
+ var pkg = require('../package.json');
+ var version = ['node-'+pkg.name,pkg.version,process.platform].join(' ');
+ this.ctcp(from, 'reply', 'VERSION '+version); //format \001VERSION client:version:environment\001
+ break;
+ default:
+ // for FINGER, SOURCE and other unknown requests
+ this.ctcp(from, 'reply', 'ERRMSG '+request+' :Query is unknown');
+ }
+ }
+ this.emit('ctcp', from, to, text, type);
+ this.emit('ctcp-'+type, from, to, text);
+} // }}}
+Client.prototype.ctcp = function(to, type, text) { // {{{
+ return this[type === 'request' ? 'say' : 'notice'](to, '\1'+text+'\1');
+} // }}}
/*
* parseMessage(line, stripColors)