Skip to content

Instantly share code, notes, and snippets.

@akoenig
Last active August 29, 2015 14:13
Show Gist options
  • Save akoenig/db0dd4a6baa7cdb16958 to your computer and use it in GitHub Desktop.
Save akoenig/db0dd4a6baa7cdb16958 to your computer and use it in GitHub Desktop.
logbot - Simple IRC log bot
#!/usr/bin/env node
/*
* logbot
*
* Copyright(c) 2015 André König <[email protected]>
* MIT Licensed
*
*
*/
'use strict';
var fs = require('fs');
var http = require('http');
var path = require('path');
var app = require('express')();
var dateformat = require('dateformat');
var debug = require('debug')('logbot');
var irc = require('irc');
var mkdirp = require('mkdirp');
var pkg = require('./package.json');
var network = process.env.NETWORK || 'irc.freenode.net';
var channels = (process.env.CHANNELS || '#node.js,#io.js').split(',');
var nick = process.env.NICK || 'logbot';
var port = process.env.PORT || 8080;
var directory = path.join(process.cwd(), 'logs');
mkdirp.sync(directory);
var client = new irc.Client(network, nick, {
channels: channels
});
app.get('/logs/:channel', function (req, res) {
var channel = req.params.channel;
fs
.createReadStream(path.join(directory, channel))
.on('error', function (err) {
debug('[ERROR]: %s', err);
res.status(404).send(http.STATUS_CODES[404]);
})
.pipe(res);
});
client.addListener('message', function onMessage (from, to, message) {
debug('[%s] New message from "%s": %s', to, from, message);
to = to.replace('#', '');
message = [dateformat(new Date(), 'dd.mm.yyyy HH:MM:ss'), '-', from + ':', message, '\n'].join(' ');
fs.appendFile(path.join(directory, to), message, function (err) {
if (err) {
debug('ERROR! %s', err);
}
});
});
app.listen(port);
debug('"%s" is running ...', pkg.name);
{
"name": "logbot",
"version": "1.0.0",
"description": "A simple IRC log bot.",
"bin": "index.js",
"main": "index.js",
"scripts": {
"test": "npm test"
},
"repository": {
"type": "git",
"url": "[email protected]:/db0dd4a6baa7cdb16958.git"
},
"keywords": [
"irc",
"log"
],
"author": "André König <[email protected]>",
"license": "MIT",
"dependencies": {
"dateformat": "^1.0.11",
"debug": "^2.1.1",
"express": "^4.10.7",
"irc": "^0.3.7",
"mkdirp": "^0.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment