Created
August 9, 2013 11:07
-
-
Save FND/6192846 to your computer and use it in GitHub Desktop.
simplistic XMPP logging bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint vars: true, node: true, white: true */ | |
"use strict"; | |
var xmpp = require("node-xmpp"); | |
// XXX: hard-coded | |
var ROOM = "innoq@conference.jabber.innoq.com"; | |
var NICK = "bot"; | |
function main() { | |
var args = process.argv; | |
if(args.length !== 4) { | |
console.error("Usage: `node echo_bot.js <jid> <password>`"); | |
process.exit(1); | |
} | |
var jid = args[2]; | |
var password = args[3]; | |
var client = new xmpp.Client({ jid: jid, password: password }); | |
client.on("online", onOnline); | |
client.on("stanza", onStanza); | |
client.on("error", onError); | |
} | |
function onOnline() { | |
setStatus(this, "im in ur network, loggin' ur messages"); | |
joinRoom(this, ROOM, NICK); | |
sendMessage(this, ROOM, "hello from Node", true); | |
} | |
function onStanza(stanza) { | |
// XXX: DEBUG | |
console.log("STANZA", stanza.attrs.from, stanza.attrs["xmlns:stream"]); | |
console.log("=====", stanza, "-----"); | |
if(stanza.attrs.type === "error") { | |
console.error("ERROR:", stanza); | |
} | |
var sender = stanza.attrs.from; | |
if(sender.indexOf(ROOM + "/") !== 0) { // ignore out-of-room messages | |
return; | |
} else if(sender === ROOM + "/" + NICK) { // ignore own messages | |
return; | |
} | |
var body = stanza.getChild("body"); | |
if(!body) { // probably a topic change | |
return; | |
} | |
var message = body.getText(); | |
console.log(sender, message); | |
} | |
function onError(err) { | |
console.error("ERROR:", err); | |
} | |
function sendMessage(client, recipient, message, groupchat) { | |
var type = groupchat ? "groupchat" : "chat"; | |
var msg = new xmpp.Element("message", { type: type, to: recipient }). | |
c("body").t(message); | |
client.send(msg); | |
} | |
function joinRoom(client, room, nick) { | |
var msg = new xmpp.Element("presence", { to: room + "/" + nick }). | |
c("x", { xmlns: "http://jabber.org/protocol/muc" }); | |
client.send(msg); | |
} | |
function setStatus(client, status) { | |
var msg = new xmpp.Element("presence", {}).c("show").t("chat").up(). | |
c("status").t(status); | |
client.send(msg); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment