robotirc

Multifonction IRC bot written in Node.js
Log | Files | Refs | README | LICENSE

commit fe763ca791e07e1c85d5f478c36efa204fd8d920
parent 1fded7c3aa3852b7b34ba8ae5688bc05d5192486
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date:   Mon,  6 Jan 2014 20:43:27 +0100

Refactoring and adding proper executable

Diffstat:
A.gitignore | 1+
Abin/robotirc | 10++++++++++
Aindex.js | 1+
Mlib/robotirc.js | 228++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mpackage.json | 5+++--
5 files changed, 126 insertions(+), 119 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/bin/robotirc b/bin/robotirc @@ -0,0 +1,10 @@ +#!/usr/bin/env node + +var log = require('npmlog'); +var robotirc = require('../lib/robotirc'); + +var config = require("../config.json"); + +log.info("", "RobotIRC is running. Press Ctrl+C to stop."); + +robotirc(config); diff --git a/index.js b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/robotirc'); diff --git a/lib/robotirc.js b/lib/robotirc.js @@ -1,12 +1,10 @@ -#!/usr/bin/env node - /*****************************************************************************/ /* */ /* RobotIRC 0.1.1 (c) by Frederic Cambus 2013-2014 */ /* https://github.com/fcambus/robotirc */ /* */ /* Created: 2013/12/17 */ -/* Last Updated: 2014/01/05 */ +/* Last Updated: 2014/01/06 */ /* */ /* RobotIRC is released under the BSD 2-Clause license. */ /* See LICENSE file for details. */ @@ -23,167 +21,163 @@ var net = require('net'); var alexa = require('alexarank'); var irc = require('irc'); -var log = require('npmlog') +var log = require('npmlog'); var request = require('request'); -/**[ Configuration ]**********************************************************/ - -var config = require("../config.json"); - -var client = new irc.Client(config.server, config.nickname, config.options); +/**[ RobotIRC ]***************************************************************/ +module.exports = function(config) { + var client = new irc.Client(config.server, config.nickname, config.options); + /**[ Connection ]*********************************************************/ -/**[ Connection ]*************************************************************/ + client.addListener('registered', function(message) { + log.info("", "RobotIRC is now connected to " + config.server); + }); -log.info("", "RobotIRC is running. Press Ctrl+C to stop."); -client.addListener('registered', function(message) { - log.info("", "RobotIRC is now connected to " + config.server); -}); + /**[ Error handler ]******************************************************/ + client.addListener('error', function(message) { + log.error("", message.args.splice(1, message.args.length).join(" - ")); + }); -/**[ Error handler ]**********************************************************/ -client.addListener('error', function(message) { - log.error("", message.args.splice(1, message.args.length).join(" - ")); -}); + /**[ CTCP VERSION handler ]***********************************************/ + client.addListener('ctcp-version', function(from, to, message) { + client.ctcp(from, 'notice', "VERSION " + config.options.realName); + }); -/**[ CTCP VERSION handler ]***************************************************/ -client.addListener('ctcp-version', function(from, to, message) { - client.ctcp(from, 'notice', "VERSION " + config.options.realName); -}); + /**[ Message handler ]****************************************************/ + client.addListener('message', function(from, to, message) { + var params = message.split(' ').slice(1).join(' '); -/**[ Message handler ]********************************************************/ - -client.addListener('message', function(from, to, message) { - var params = message.split(' ').slice(1).join(' '); + if (to == client.nick) { // Handling private messages + to = from; + } - if (to == client.nick) { // Handling private messages - to = from; - } + /**********************************************************[ !help ]**/ - /**************************************************************[ !help ]**/ + if (message.match(/^!help/)) { + var help = "RobotIRC 0.1.1 supports the following commands :\n" + + "!alexa => Get Alexa traffic rank for a domain or URL\n" + + "!date => Display server local time\n" + + "!expand => Expand a shortened URL\n" + + "!geoip => IP address Geolocation\n" + + "!headers => Display HTTP headers for queried URL\n" + + "!resolve => Get A records (IPv4) and AAAA records (IPv6) for queried domain\n" + + "!reverse => Get reverse (PTR) records from IPv4 or IPv6 addresses\n" + + "!wikipedia => Query Wikipedia for an article summary\n"; - if (message.match(/^!help/)) { - var help = "RobotIRC 0.1.1 supports the following commands :\n" + - "!alexa => Get Alexa traffic rank for a domain or URL\n" + - "!date => Display server local time\n" + - "!expand => Expand a shortened URL\n" + - "!geoip => IP address Geolocation\n" + - "!headers => Display HTTP headers for queried URL\n" + - "!resolve => Get A records (IPv4) and AAAA records (IPv6) for queried domain\n" + - "!reverse => Get reverse (PTR) records from IPv4 or IPv6 addresses\n" + - "!wikipedia => Query Wikipedia for an article summary\n"; + client.say(to, help); + } - client.say(to, help); - } + /*********************************************************[ !alexa ]**/ - /*************************************************************[ !alexa ]**/ + if (message.match(/^!alexa/)) { + alexa(params, function(error, result) { + if (!error && typeof result.rank != "undefined") { + client.say(to, "Alexa Traffic Rank for " + result.idn.slice(0, -1) + " : " + result.rank); + } + }); + } - if (message.match(/^!alexa/)) { - alexa(params, function(error, result) { - if (!error && typeof result.rank != "undefined") { - client.say(to, "Alexa Traffic Rank for " + result.idn.slice(0, -1) + " : " + result.rank); - } - }); - } + /**********************************************************[ !date ]**/ - /**************************************************************[ !date ]**/ + if (message.match(/^!date/)) { + client.say(to, Date()); + } - if (message.match(/^!date/)) { - client.say(to, Date()); - } + /********************************************************[ !expand ]**/ + + if (message.match(/^!expand/)) { + request({ + method: "HEAD", + url: params, + followAllRedirects: true + }, + function(error, response) { + if (!error && response.statusCode == 200) { + client.say(to, response.request.href); + } + }); + } - /************************************************************[ !expand ]**/ + /*********************************************************[ !geoip ]**/ - if (message.match(/^!expand/)) { - request({ - method: "HEAD", - url: params, - followAllRedirects: true - }, - function(error, response) { + if (message.match(/^!geoip/)) { + request('http://www.telize.com/geoip/' + params, function(error, response, body) { if (!error && response.statusCode == 200) { - client.say(to, response.request.href); - } - }); - } + var geoip = JSON.parse(response.body); - /*************************************************************[ !geoip ]**/ - - if (message.match(/^!geoip/)) { - request('http://www.telize.com/geoip/' + params, function(error, response, body) { - if (!error && response.statusCode == 200) { - var geoip = JSON.parse(response.body); - - for (var item in geoip) { - client.say(to, item + ": " + geoip[item] + "\n"); + for (var item in geoip) { + client.say(to, item + ": " + geoip[item] + "\n"); + } } - } - }); - } + }); + } - /***********************************************************[ !headers ]**/ + /*******************************************************[ !headers ]**/ - if (message.match(/^!headers/)) { - request(params, function(error, response, body) { - if (!error && response.statusCode == 200) { - for (var item in response.headers) { - client.say(to, item + ": " + response.headers[item] + "\n"); + if (message.match(/^!headers/)) { + request(params, function(error, response, body) { + if (!error && response.statusCode == 200) { + for (var item in response.headers) { + client.say(to, item + ": " + response.headers[item] + "\n"); + } } - } - }); - } + }); + } - /***********************************************************[ !resolve ]**/ + /*******************************************************[ !resolve ]**/ - if (message.match(/^!resolve/)) { - dns.resolve4(params, function(error, addresses) { - if (!error) { - for (var item in addresses) { - client.say(to, addresses[item] + "\n"); - } - } - dns.resolve6(params, function(error, addresses) { + if (message.match(/^!resolve/)) { + dns.resolve4(params, function(error, addresses) { if (!error) { for (var item in addresses) { client.say(to, addresses[item] + "\n"); } } + dns.resolve6(params, function(error, addresses) { + if (!error) { + for (var item in addresses) { + client.say(to, addresses[item] + "\n"); + } + } + }); }); - }); - } + } - /***********************************************************[ !reverse ]**/ + /*******************************************************[ !reverse ]**/ - if (message.match(/^!reverse/)) { - if (net.isIP(params)) { - dns.reverse(params, function(error, domains) { - if (!error) { - client.say(to, domains[0]); - } - }); + if (message.match(/^!reverse/)) { + if (net.isIP(params)) { + dns.reverse(params, function(error, domains) { + if (!error) { + client.say(to, domains[0]); + } + }); + } } - } - /*********************************************************[ !wikipedia ]**/ + /*****************************************************[ !wikipedia ]**/ - if (message.match(/^!wikipedia/)) { - params.split(' ').join('_'); + if (message.match(/^!wikipedia/)) { + params.split(' ').join('_'); - dns.resolveTxt(params + ".wp.dg.cx", function(error, txt) { - if (!error) { - client.say(to, txt[0]); - } - }); - } -}); + dns.resolveTxt(params + ".wp.dg.cx", function(error, txt) { + if (!error) { + client.say(to, txt[0]); + } + }); + } + }); +}; diff --git a/package.json b/package.json @@ -2,9 +2,10 @@ "name": "robotirc", "version": "0.1.1", "description": "Multifonction IRC bot written in Node", + "main": "index.js", "preferGlobal": "true", "bin": { - "robotirc": "lib/robotirc.js" + "robotirc": "./bin/robotirc" }, "repository": { "type": "git", @@ -33,6 +34,6 @@ "irc": "~0.3.6", "request": "~2.30.0", "alexarank": "~0.1.0", - "npmlog": "0.0.6" + "npmlog": "~0.0.6" } }