Mercurial > xmpp2smtp
comparison xmpp2smtp.js @ 0:e459f344d8bd
First commit.
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Wed, 15 Jun 2011 06:10:47 +0200 |
parents | |
children | c1a19866a233 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e459f344d8bd |
---|---|
1 var os = require('os'); | |
2 var fs = require('fs'); | |
3 | |
4 var xmpp = require('node-xmpp'); | |
5 var ispdb = require('./node-ispdb'); | |
6 var smtp = require("./Nodemailer/lib/mail"); | |
7 | |
8 var config = require("./configuration"); | |
9 | |
10 var info = []; | |
11 //~ info.push(new xmpp.Element("feature", {"var": "http://jabber.org/protocol/caps"})); | |
12 //~ info.push(new xmpp.Element("feature", {"var": "http://jabber.org/protocol/commands"})); | |
13 info.push(new xmpp.Element("feature", {"var": "http://jabber.org/protocol/disco#info"})); | |
14 info.push(new xmpp.Element("feature", {"var": "jabber:iq:register"})); | |
15 info.push(new xmpp.Element("feature", {"var": "jabber:iq:version"})); | |
16 info.push(new xmpp.Element("identity", {category: "gateway", type: "irc", name: config.XMPP.name})); | |
17 | |
18 var cl = new xmpp.Component({ | |
19 jid: config.XMPP.jid, | |
20 password: config.XMPP.password, | |
21 host: config.XMPP.host, | |
22 port: config.XMPP.port | |
23 }); | |
24 | |
25 cl.on('stanza', | |
26 function(stanza) { | |
27 var type = stanza.attrs.type; | |
28 //jabber:iq:register remove | |
29 if((stanza.is("iq")) && (type == "set") && (stanza.getChild("query", "jabber:iq:register")) && (stanza.getChild("query").getChild("remove"))) { | |
30 fs.unlink("storage/"+stanza.attrs['from'].split("/")[0], | |
31 function() { | |
32 cl.send(new xmpp.Element( | |
33 "iq", { | |
34 id: stanza.attrs.id, | |
35 to: stanza.attrs.from, | |
36 from: config.XMPP.jid, | |
37 type: 'result' | |
38 } | |
39 )); | |
40 } | |
41 ); | |
42 } | |
43 //jabber:iq:register get | |
44 else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "jabber:iq:register"))) { | |
45 var result = new xmpp.Element( | |
46 "iq", { | |
47 id: stanza.attrs.id, | |
48 to: stanza.attrs.from, | |
49 from: config.XMPP.jid, | |
50 type: 'result' | |
51 } | |
52 ) | |
53 .c("query", {xmlns: "jabber:iq:register"}); | |
54 | |
55 | |
56 fs.readFile("storage/"+stanza.attrs['from'].split("/")[0], | |
57 function(err, data) { | |
58 if(config.easySMTPConf && ispdb) { | |
59 result.c("instructions").t("Please enter your email and password, we will try to find the SMTP configuration for you."); | |
60 result.c("username"); | |
61 result.c("password"); | |
62 } | |
63 else { | |
64 result.c("x", {type: "form", xmlns: "jabber:x:data"}); | |
65 var fields; | |
66 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"}; | |
67 result.c("field", {type: "hidden", "var": "FORM_TYPE"}).c("value").t("jabber:iq:register"); | |
68 result.c("field", {type: "text-single", label: "Host", "var": "host"}).c("required").up().c("value").t(fields.host); | |
69 result.c("field", {type: "text-single", label: "Port", "var": "port"}).c("required").up().c("value").t(fields.port); | |
70 result.c("field", {type: "boolean", label: "Enable SSL", "var": "ssl"}).c("required").up().c("value").t(fields.ssl); | |
71 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"); | |
72 result.c("field", {type: "text-single", label: "Email address", "var": "from"}).c("required").up().c("value").t(fields.from); | |
73 result.c("field", {type: "text-single", label: "Username", "var": "username"}).c("required").up().c("value").t(fields.username); | |
74 result.c("field", {type: "text-private", label: "Password", "var": "password"}).c("required").up().c("value").t(fields.password); | |
75 } | |
76 cl.send(result); | |
77 } | |
78 ); | |
79 } | |
80 //jabber:iq:register set | |
81 else if((stanza.is("iq")) && (type == "set") && (stanza.getChild("query", "jabber:iq:register"))) { | |
82 var test = {}; | |
83 var path = "storage/"+stanza.attrs["from"].split("/")[0]; | |
84 //easySMTPConfig :) | |
85 if(stanza.getChild("query").getChild("username")) { | |
86 var username = stanza.getChild("query").getChild("username").getText(); | |
87 var password = stanza.getChild("query").getChild("password").getText(); | |
88 ispdb.get(username.split("@")[1], function(data) { | |
89 if(data == null) { | |
90 cl.send(new xmpp.Element("iq", {to: stanza.attrs.from, from: config.XMPP.jid, id: stanza.attrs.id, type: "error"})); | |
91 } | |
92 else { | |
93 var smtp = data.outgoing; | |
94 test.port = smtp.port; | |
95 test.host = smtp.hostname; | |
96 if(smtp.socketType == "SSL") | |
97 test.ssl = 1; | |
98 else | |
99 test.ssl = 0; | |
100 test.authentication = "login"; | |
101 test.from = username; | |
102 test.username = username; | |
103 test.password = password; | |
104 } | |
105 fs.writeFile(path, JSON.stringify(test), function () { | |
106 cl.send(new xmpp.Element("iq", {to: stanza.attrs.from, from: config.XMPP.jid, id: stanza.attrs.id, type: "result"})); | |
107 }); | |
108 }); | |
109 | |
110 } | |
111 //not easy | |
112 else { | |
113 var fields = stanza.getChild("query").getChild("x").getChildren("field"); | |
114 for (var i=0;i<fields.length;i++) { | |
115 if(fields[i].attrs["var"] != "FORM_TYPE") | |
116 test[fields[i].attrs["var"]] = fields[i].getChild("value").getText(); | |
117 } | |
118 fs.writeFile(path, JSON.stringify(test), function () { | |
119 cl.send(new xmpp.Element("iq", {to: stanza.attrs.from, from: config.XMPP.jid, id: stanza.attrs.id, type: "result"})); | |
120 }); | |
121 } | |
122 } | |
123 //jabber:iq:version | |
124 else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "jabber:iq:version"))) { | |
125 var result = new xmpp.Element( | |
126 "iq", { | |
127 to: stanza.attrs.from, | |
128 from: config.XMPP.jid, | |
129 id: stanza.attrs.id, | |
130 type: 'result' | |
131 } | |
132 ).c("query", {xmlns: "jabber:iq:version"}) | |
133 stanza.c("name").t("xmpp2smtp"); | |
134 stanza.c("version").t("0.1"); | |
135 stanza.c("os").t(os.type()+" "+os.release()); | |
136 cl.send(result); | |
137 } | |
138 //disco#info | |
139 else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "http://jabber.org/protocol/disco#info"))) { | |
140 var stanza = new xmpp.Element( | |
141 'iq', { | |
142 to: stanza.attrs.from, | |
143 from: config.XMPP.jid, | |
144 id: stanza.attrs.id, | |
145 type: 'result' | |
146 } | |
147 ); | |
148 for(var i=0;i<info.length;i++) { | |
149 stanza.cnode(info[i]); | |
150 } | |
151 cl.send(stanza); | |
152 } | |
153 //message | |
154 else if (stanza.is('message') && (type == 'chat')||(type == 'normal')) { | |
155 fs.readFile("storage/"+stanza.attrs['from'].split("/")[0], | |
156 function(err, data) { | |
157 var settings; | |
158 data ? settings = JSON.parse(data) : settings = config.SMTP; | |
159 smtp.SMTP = { | |
160 host: settings.host, | |
161 port: settings.port, | |
162 use_authentication: true, | |
163 user: settings.username, | |
164 pass: settings.password | |
165 } | |
166 smtp.send_mail( | |
167 { | |
168 sender: settings.from, | |
169 to: stanza.attrs.to.split("@")[0].replace("%", "@"), | |
170 subject: (type == "normal" ? stanza.getChildText("subject") : ""), | |
171 body: stanza.getChildText("body") | |
172 }, | |
173 function(error, success){ | |
174 if(error) { | |
175 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.")); | |
176 } | |
177 } | |
178 ); | |
179 } | |
180 ); | |
181 } | |
182 } | |
183 ); | |
184 cl.on('error', | |
185 function(e) { | |
186 console.log(e); | |
187 } | |
188 ); | |
189 |