Skip to content

Commit

Permalink
Add scrollback module, blergit, improve stuff, add events like mad
Browse files Browse the repository at this point in the history
  • Loading branch information
euank committed Jun 20, 2014
1 parent 75195a1 commit b5a7373
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 46 deletions.
160 changes: 137 additions & 23 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,8 @@ bot.getConfig = function(name, cb) {
});
};

bot.getDataFolder = function(name) {
return bot.datafolders[name];
};

bot.initDataFolders = function(cb) {
bot.datafolders = {};
//TODO
cb(null);
bot.getDataFolder = function(namespace) {
return path.join(bot.datafolder, namespace);
};


Expand Down Expand Up @@ -142,6 +136,14 @@ bot.client = new irc.Client(conf.server, conf.nick, {
bot.client.on('error', function(err) { console.log(err);});
bot.conf = conf;

bot.sayTo = function(target, args) {
// Todo, make this use stringifyArgs.
var tosay = [];
for(var i=1;i<arguments.length;i++) {
tosay.push(arguments[i]);
}
bot.client.say(target, tosay.join(' '));
};
/* say("one", "two") => "one two" */
bot.say = function(args) {
var tosay = [];
Expand All @@ -160,28 +162,86 @@ bot.joinChannels = function(cb) {
}, cb);
};

bot.stringifyArgs = function(args) {
var strParts = [];
for(var i=0;i<arguments.length;i++) {
if(typeof arguments[i] === 'string') {
strParts.push(arguments[i]);
} else if(Array.isArray(arguments[i])) {
strParts.push(bot.stringifyArgs.apply(this, arguments[i]));
} else if(arguments[i] === undefined || arguments[i] === null) {
strParts.push('');
} else{
strParts.push(arguments[i].toString());
}
}
return strParts.join(' ');
};

bot.isChannel = function(name) {
return _.some(_.map(bot.config.channelPrefixes.split(''), function(el) { return name[0] == el; }));
};


bot.getReply = function(chan) {
var stringifyArgs = function(args) {
var strParts = [];
for(var i=0;i<arguments.length;i++) {
if(typeof arguments[i] === 'string') {
strParts.push(arguments[i]);
} else if(Array.isArray(arguments[i])) {
strParts.push(stringifyArgs.apply(this, arguments[i]));
} else if(arguments[i] === undefined || arguments[i] === null) {
strParts.push('');
} else{
strParts.push(arguments[i].toString());
}
return function(args) {
var repStr = bot.stringifyArgs.apply(this, arguments);
if(bot.isChannel(chan)) {
bot.callModuleFn('chansay', [bot.client.nick, chan, repStr]);
} else {
bot.callModuleFn('pmsay', [bot.client.nick, chan, repStr]);
}
return strParts.join(' ');
bot.client.say(chan, repStr);
};
};

bot.getNoticeReply = function(to) {
return function(args) {
bot.client.say(chan, stringifyArgs.apply(this, arguments));
var repStr = bot.stringifyArgs.apply(this, arguments);

if(bot.isChannel(chan)) {
bot.callModuleFn('channotice', [bot.client.nick, to, repStr]);
} else {
bot.callModuleFn('pmnotice', [bot.client.nick, to, repStr]);
}
bot.client.notice(to, repStr);
};
};
bot.getActionReply = function(to) {
return function(args) {
var repStr = bot.stringifyArgs.apply(this, arguments);

if(bot.isChannel(chan)) {
bot.callModuleFn('channotice', [bot.client.nick, to, repStr]);
} else {
bot.callModuleFn('pmnotice', [bot.client.nick, to, repStr]);
}
bot.client.action(to, repStr);
};
};

bot.client.on('join', function(channel, nick, raw) {
bot.callModuleFn('join', [channel, nick, raw]);
});
bot.client.on('part', function(channel, nick, raw) {
bot.callModuleFn('part', [channel, nick, raw]);
});
bot.client.on('quit', function(nick,reason,channels,raw) {
bot.callModuleFn('quit', [nick, reason, channels, raw]);
});


bot.client.on('notice', function(from, to, text, raw) {
var primaryFrom = (to == bot.client.nick) ? from : to;

bot.callModuleFn('notice', [text, from, to, bot.getNoticeReply(primaryFrom), raw]);
if(to == bot.client.nick) {
bot.callModuleFn('pmnotice', [text, from, bot.getNoticeReply(primaryFrom), raw]);
} else {
bot.callModuleFn('channotice', [text, to, from, bot.getNoticeReply(primaryFrom), raw]);
}

});

bot.client.on('message', function(from, to, text, raw) {
var primaryFrom = (to == bot.client.nick) ? from : to;
Expand Down Expand Up @@ -212,8 +272,63 @@ bot.client.on('ctcp', function(from, to, text, type, raw) {
} else {
moduleMan.callModuleFn('ctcp', [text, type, from, to, raw]);
}

if(raw.args && raw.args[1] && /^\u0001ACTION.*\u0001$/.test(raw.args[1])) {
if(/^ACTION /.test(text)) text = text.substring("ACTION ".length);

var primaryFrom = (to == bot.client.nick) ? from : to;
moduleMan.callModuleFn('action', [text, from, to, bot.getActionReply(primaryFrom), raw]);
if(to == bot.client.nick) {
moduleMan.callModuleFn('pmaction', [text, from, bot.getActionReply(primaryFrom), raw]);
} else {
moduleMan.callModuleFn('chanaction', [text, to, from, bot.getActionReply(primaryFrom), raw]);
}
}
});

bot.createPathIfNeeded = function(fullPath, cb) {
var dirname = path.dirname(fullPath);
fs.mkdir(dirname, function(err) {
if(err && err.code != 'EEXIST') {
// Directory doesn't already exist and couldn't be made
cb(err);
} else {
// Made or already exists.
cb(null);
}
});
};

bot.fsStoreData = function(namespace, filePath, data, flag, cb) {
// Flags is an optional argument
if(typeof flag == 'function') {
cb = flag;
flag = 'w';
}

var basePath = bot.getDataFolder(namespace);
var finalPath = path.join(basePath, filePath);

bot.createPathIfNeeded(finalPath, function(err, res) {
if(err) return cb(err);
fs.writeFile(finalPath, data, {flag: flag}, cb);
});
};

bot.fsGetData = function(namespace, filePath, cb) {
var basePath = bot.getDataFolder(namespace);
var finalPath = path.join(basePath, filePath);

fs.readFile(finalPath, cb);
};

bot.fsListData = function(namespace, listPath, cb) {
var basePath = bot.getDataFolder(namespace);
var finalPath = path.join(basePath, listPath);

fs.readdir(finalPath, cb);
};

async.series([
function(cb) {
bot.conf = bot.loadConfig();
Expand All @@ -227,7 +342,6 @@ async.series([
console.log("Connected!");
cb(null);
},
bot.initDataFolders,
bot.initModuleManager,
moduleMan.loadModules,
moduleMan.initModules,
Expand Down
5 changes: 2 additions & 3 deletions conf/blerg.example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"watch": [
{"nick": "god","username": "god", "password": "heaven"}
]
"username": "god",
"password": "heaven"
}
41 changes: 27 additions & 14 deletions modules/blerg/blerg.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
var config;
var bot;

var watches = {};
var blerg = require('blerg');

module.exports.init = function(bot) {
module.exports.init = function(b) {
bot = b;
bot.getConfig("blerg.json", function(err, conf) {
if(err) console.log(err);
config = conf;

for(var i=0;i<conf.watch.length;i++) {
var watch = conf.watch[i];
var ablerg = require('blerg');
ablerg.login(watch.username, watch.password, function(err) {
blerg.login(config.username, config.password, function(err) {
if(err) return console.log("Trouble logging into blerg account: " + err);
watches[watch.nick] = ablerg;
});
}
});
});
};

module.exports.commands = ['qub', 'blergit'];

module.exports.run = function(rem, parts, reply, command, from, to, text, raw) {
if(to[0] != '#') return; // Channels can start with & too? Really? Wow, good thing noone does that


var scrollBackModule = bot.modules['sirc-scrollback'];

if(!scrollbackModule) return console.log("No scrollback, can't blerg");

module.exports.msg = function(text, from, reply, raw) {
if(watches[from.toLowerCase()]) {
watches[from.toLowerCase()].put(text, function(err) {
if(err) reply("Error blerging: " + err);
scrollbackModule.getFormattedScrollbackLinesFromRanges(channel, parts, function(err, res) {
if(err) return reply(err);

var indented = res.split("\n").map(function(l) {
return " " + l; // 4 spaces, pre sorta thing
});

blerg.put(indented, function(err) {
if(err) return reply(err);
bot.sayTo(from, "Blerged it!");
});
}
});
};

3 changes: 2 additions & 1 deletion modules/blerg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"blerg": "0.0.1"
"blerg": "0.0.2",
"parse-numeric-range": "0.0.1"
}
}
Loading

0 comments on commit b5a7373

Please sign in to comment.