Skip to content

Instantly share code, notes, and snippets.

@paraboul
Created June 14, 2011 09:49
Show Gist options
  • Save paraboul/1024605 to your computer and use it in GitHub Desktop.
Save paraboul/1024605 to your computer and use it in GitHub Desktop.
IRC Bot
var IRCBot = new Class({
Implements: Events,
socket: null,
initialize: function(opts) {
this.socket = new Ape.sockClient(opts.server.port, opts.server.ip, {flushlf: true});
this.socket.onConnect = function() {
Ape.log('connected');
this.write('NICK ' + opts.bot.nick);
this.write('USER APE APE APE APE');
}.bind(this);
this.socket.onRead = function(data) {
if (data.substr(0, 5) == 'PING ') {
this.write('PONG ' + data.substr(5));
} else if (data[0] == ':') {
this.read(data);
} else {
}
}.bind(this);
this.socket.onDisconnect = function() {
}.bind(this);
},
write: function(data) {
this.socket.write(data + "\n");
},
read: function(data) {
data = data.replace("\r", "");
let msg = data.split(':');
let tmp = msg[1].split(' ');
msg.splice(0, 2);
this.onIRCRaw(tmp[1], {from:tmp[0], who:tmp[2], msg: msg.join(':')});
},
onIRCRaw: function(raw, data) {
var nickname = data.from.split('!')[0];
switch(raw) {
case '376':
this.fireEvent('connect', data);
break;
case 'JOIN':
this.fireEvent('join', {channel: data.msg, who: data.from});
break;
case 'PRIVMSG':
var ischannel = (data.who[0] == '#');
this.fireEvent('msg', {from: nickname, where: data.who, msg: data.msg, ischannel: ischannel, reply: function(msg, notice) {
this.msg((ischannel && !notice ? data.who : nickname), msg, notice);
}.bind(this)});
break;
default:
//Ape.log(raw + ' > ' + nickname + ' ' + data.who + ' > ' + data.msg);
break;
}
},
join: function(channel) {
this.write('JOIN #' + channel);
},
msg: function(where, msg, notice) {
notice = notice || false;
this.write((notice ? 'NOTICE ' : 'PRIVMSG ') + where + ' :' + msg);
}
});
var irc = new IRCBot({
server: {
port: 6667,
ip: 'irc.v.nf'
},
bot: {
nick: 'APEBot'
}
});
irc.addEvent('connect', function(ev) {
this.join('bot');
});
irc.addEvent('msg', function(ev) {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment