changeset 0:e459f344d8bd

First commit.
author Sonny Piers <sonny.piers@gmail.com>
date Wed, 15 Jun 2011 06:10:47 +0200
parents
children 56b67e2b35b1
files configuration.js xmpp2smtp.js
diffstat 2 files changed, 213 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
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;
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<fields.length;i++) {
+					if(fields[i].attrs["var"] != "FORM_TYPE")
+						test[fields[i].attrs["var"]] = fields[i].getChild("value").getText();
+				}
+				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"}));
+				});
+			}
+		}
+		//jabber:iq:version
+		else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "jabber:iq:version"))) {
+			var result = new xmpp.Element(
+				"iq", {
+					to: stanza.attrs.from,
+					from: config.XMPP.jid,
+					id: stanza.attrs.id,
+					type: 'result'
+				}
+			).c("query", {xmlns: "jabber:iq:version"})
+			stanza.c("name").t("xmpp2smtp");
+			stanza.c("version").t("0.1");
+			stanza.c("os").t(os.type()+" "+os.release());
+			cl.send(result);
+		}
+		//disco#info
+		else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "http://jabber.org/protocol/disco#info"))) {
+			var stanza = new xmpp.Element(
+				'iq', {
+					to: stanza.attrs.from,
+					from: config.XMPP.jid,
+					id: stanza.attrs.id,
+					type: 'result'
+				}
+			);
+			for(var i=0;i<info.length;i++) {
+				stanza.cnode(info[i]);
+			}
+			cl.send(stanza);
+		}
+		//message
+	  else if (stanza.is('message') && (type == 'chat')||(type == 'normal')) {
+			fs.readFile("storage/"+stanza.attrs['from'].split("/")[0],
+				function(err, data) {
+					var settings;
+					data ? settings = JSON.parse(data) : settings = config.SMTP;
+					smtp.SMTP = {
+						host: settings.host,
+						port: settings.port,
+						use_authentication: true,
+						user: settings.username,
+						pass: settings.password
+					}
+					smtp.send_mail(
+						{
+							sender: settings.from, 
+							to: stanza.attrs.to.split("@")[0].replace("%", "@"),
+							subject: (type == "normal" ? stanza.getChildText("subject") : ""),
+							body: stanza.getChildText("body")
+						},
+						function(error, success){
+							if(error) {
+								cl.send(new xmpp.Element('message', {type: stanza.attrs.type, to: stanza.attrs.from, from: config.XMPP.jid}).c("body").t("Sorry we haven't been able to deliver the mail, check your settings."));
+							}
+						}
+					);
+				}
+			);
+		}
+  }
+);
+cl.on('error',
+	function(e) {
+		console.log(e);
+  }
+);
+