# HG changeset patch # User Sonny Piers # Date 1308111047 -7200 # Node ID e459f344d8bdcca7085b9bcb2125a2c5988d731f First commit. diff --git a/configuration.js b/configuration.js new file mode 100644 --- /dev/null +++ b/configuration.js @@ -0,0 +1,24 @@ +var config = exports; + +//This is the configuraton for the XMPP component +config.XMPP = { + jid: "mailer.localhost", + password: "54321", + host: "localhost", + port: "5347", + name: "SMTP gateway that allows you to send mail" +}; +//This is the default SMTP configuration used only if config.defaultSMTP is set to true +config.SMTP = { + host: "smtp.example.com", + port: 465, + authentication: "login", + username: "example(@example.com)", + from: "example@example.com", + password: "exemple", + ssl: true +}; +//Enable easy SMTP configuration for user when register to the component (use http://ispdb.mozillamessaging.com/ database) +config.easySMTPConf = true; +//Enable user to send mail without registering to the component, will use config.SMTP settings +config.defaultSMTP = true; diff --git a/xmpp2smtp.js b/xmpp2smtp.js new file mode 100755 --- /dev/null +++ b/xmpp2smtp.js @@ -0,0 +1,189 @@ +var os = require('os'); +var fs = require('fs'); + +var xmpp = require('node-xmpp'); +var ispdb = require('./node-ispdb'); +var smtp = require("./Nodemailer/lib/mail"); + +var config = require("./configuration"); + +var info = []; +//~ info.push(new xmpp.Element("feature", {"var": "http://jabber.org/protocol/caps"})); +//~ info.push(new xmpp.Element("feature", {"var": "http://jabber.org/protocol/commands"})); +info.push(new xmpp.Element("feature", {"var": "http://jabber.org/protocol/disco#info"})); +info.push(new xmpp.Element("feature", {"var": "jabber:iq:register"})); +info.push(new xmpp.Element("feature", {"var": "jabber:iq:version"})); +info.push(new xmpp.Element("identity", {category: "gateway", type: "irc", name: config.XMPP.name})); + +var cl = new xmpp.Component({ + jid: config.XMPP.jid, + password: config.XMPP.password, + host: config.XMPP.host, + port: config.XMPP.port +}); + +cl.on('stanza', + function(stanza) { + var type = stanza.attrs.type; + //jabber:iq:register remove + if((stanza.is("iq")) && (type == "set") && (stanza.getChild("query", "jabber:iq:register")) && (stanza.getChild("query").getChild("remove"))) { + fs.unlink("storage/"+stanza.attrs['from'].split("/")[0], + function() { + cl.send(new xmpp.Element( + "iq", { + id: stanza.attrs.id, + to: stanza.attrs.from, + from: config.XMPP.jid, + type: 'result' + } + )); + } + ); + } + //jabber:iq:register get + else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "jabber:iq:register"))) { + var result = new xmpp.Element( + "iq", { + id: stanza.attrs.id, + to: stanza.attrs.from, + from: config.XMPP.jid, + type: 'result' + } + ) + .c("query", {xmlns: "jabber:iq:register"}); + + + fs.readFile("storage/"+stanza.attrs['from'].split("/")[0], + function(err, data) { + if(config.easySMTPConf && ispdb) { + result.c("instructions").t("Please enter your email and password, we will try to find the SMTP configuration for you."); + result.c("username"); + result.c("password"); + } + else { + result.c("x", {type: "form", xmlns: "jabber:x:data"}); + var fields; + data ? fields = JSON.parse(data) : fields = {host:"smtp.example.com",authentication: "login", port:"465", ssl: true, username: "example(@example.com)",password: "example", from: "example@example.com"}; + result.c("field", {type: "hidden", "var": "FORM_TYPE"}).c("value").t("jabber:iq:register"); + result.c("field", {type: "text-single", label: "Host", "var": "host"}).c("required").up().c("value").t(fields.host); + result.c("field", {type: "text-single", label: "Port", "var": "port"}).c("required").up().c("value").t(fields.port); + result.c("field", {type: "boolean", label: "Enable SSL", "var": "ssl"}).c("required").up().c("value").t(fields.ssl); + result.c("field", {type: "list-single", label: "Authentication method", "var": "authentication"}).c("required").up().c("value").t(fields.authentication).up().c("option", {label: "login"}).c("value").t("login").up().up().c("option", {label: "anonymous"}).c("value").t("anonymous"); + result.c("field", {type: "text-single", label: "Email address", "var": "from"}).c("required").up().c("value").t(fields.from); + result.c("field", {type: "text-single", label: "Username", "var": "username"}).c("required").up().c("value").t(fields.username); + result.c("field", {type: "text-private", label: "Password", "var": "password"}).c("required").up().c("value").t(fields.password); + } + cl.send(result); + } + ); + } + //jabber:iq:register set + else if((stanza.is("iq")) && (type == "set") && (stanza.getChild("query", "jabber:iq:register"))) { + var test = {}; + var path = "storage/"+stanza.attrs["from"].split("/")[0]; + //easySMTPConfig :) + if(stanza.getChild("query").getChild("username")) { + var username = stanza.getChild("query").getChild("username").getText(); + var password = stanza.getChild("query").getChild("password").getText(); + ispdb.get(username.split("@")[1], function(data) { + if(data == null) { + cl.send(new xmpp.Element("iq", {to: stanza.attrs.from, from: config.XMPP.jid, id: stanza.attrs.id, type: "error"})); + } + else { + var smtp = data.outgoing; + test.port = smtp.port; + test.host = smtp.hostname; + if(smtp.socketType == "SSL") + test.ssl = 1; + else + test.ssl = 0; + test.authentication = "login"; + test.from = username; + test.username = username; + test.password = password; + } + fs.writeFile(path, JSON.stringify(test), function () { + cl.send(new xmpp.Element("iq", {to: stanza.attrs.from, from: config.XMPP.jid, id: stanza.attrs.id, type: "result"})); + }); + }); + + } + //not easy + else { + var fields = stanza.getChild("query").getChild("x").getChildren("field"); + for (var i=0;i