Mercurial > xmpp2smtp
annotate xmpp2smtp.js @ 7:1776fb122b80 default tip master
Aknowlegements.
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Mon, 20 Jun 2011 17:07:25 +0200 |
parents | 5cf8a08ef7fe |
children |
rev | line source |
---|---|
0 | 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"})); | |
6
5cf8a08ef7fe
Fix errors in disco#info.
Sonny Piers <sonny.piers@gmail.com>
parents:
5
diff
changeset
|
16 info.push(new xmpp.Element("feature", {"var": "jabber:iq:gateway"})); |
5cf8a08ef7fe
Fix errors in disco#info.
Sonny Piers <sonny.piers@gmail.com>
parents:
5
diff
changeset
|
17 info.push(new xmpp.Element("identity", {category: "gateway", type: "smtp", name: config.XMPP.name})); |
0 | 18 |
19 var cl = new xmpp.Component({ | |
20 jid: config.XMPP.jid, | |
21 password: config.XMPP.password, | |
22 host: config.XMPP.host, | |
23 port: config.XMPP.port | |
24 }); | |
25 | |
26 cl.on('stanza', | |
27 function(stanza) { | |
28 var type = stanza.attrs.type; | |
29 //jabber:iq:register remove | |
30 if((stanza.is("iq")) && (type == "set") && (stanza.getChild("query", "jabber:iq:register")) && (stanza.getChild("query").getChild("remove"))) { | |
31 fs.unlink("storage/"+stanza.attrs['from'].split("/")[0], | |
32 function() { | |
33 cl.send(new xmpp.Element( | |
34 "iq", { | |
35 id: stanza.attrs.id, | |
36 to: stanza.attrs.from, | |
37 from: config.XMPP.jid, | |
38 type: 'result' | |
39 } | |
40 )); | |
41 } | |
42 ); | |
43 } | |
44 //jabber:iq:register get | |
45 else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "jabber:iq:register"))) { | |
46 var result = new xmpp.Element( | |
47 "iq", { | |
48 id: stanza.attrs.id, | |
49 to: stanza.attrs.from, | |
50 from: config.XMPP.jid, | |
51 type: 'result' | |
52 } | |
53 ) | |
54 .c("query", {xmlns: "jabber:iq:register"}); | |
55 | |
56 | |
57 fs.readFile("storage/"+stanza.attrs['from'].split("/")[0], | |
58 function(err, data) { | |
59 if(config.easySMTPConf && ispdb) { | |
60 result.c("instructions").t("Please enter your email and password, we will try to find the SMTP configuration for you."); | |
61 result.c("username"); | |
62 result.c("password"); | |
63 } | |
64 else { | |
65 result.c("x", {type: "form", xmlns: "jabber:x:data"}); | |
66 var fields; | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
67 data ? fields = JSON.parse(data) : fields = {host:"smtp.example.com",use_authentication: "login", port:"465", ssl: false, user: "example(@example.com)", pass: "example", sender: "example@example.com"}; |
0 | 68 result.c("field", {type: "hidden", "var": "FORM_TYPE"}).c("value").t("jabber:iq:register"); |
69 result.c("field", {type: "text-single", label: "Host", "var": "host"}).c("required").up().c("value").t(fields.host); | |
70 result.c("field", {type: "text-single", label: "Port", "var": "port"}).c("required").up().c("value").t(fields.port); | |
71 result.c("field", {type: "boolean", label: "Enable SSL", "var": "ssl"}).c("required").up().c("value").t(fields.ssl); | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
72 result.c("field", {type: "boolean", label: "Use authentication", "var": "authentication"}).c("required").up().c("value").t(fields.user_authentication); |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
73 result.c("field", {type: "text-single", label: "Email address", "var": "sender"}).c("required").up().c("value").t(fields.from); |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
74 result.c("field", {type: "text-single", label: "Username", "var": "user"}).c("required").up().c("value").t(fields.username); |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
75 result.c("field", {type: "text-private", label: "Password", "var": "pass"}).c("required").up().c("value").t(fields.password); |
0 | 76 } |
77 cl.send(result); | |
78 } | |
79 ); | |
80 } | |
81 //jabber:iq:register set | |
82 else if((stanza.is("iq")) && (type == "set") && (stanza.getChild("query", "jabber:iq:register"))) { | |
83 var test = {}; | |
84 var path = "storage/"+stanza.attrs["from"].split("/")[0]; | |
85 //easySMTPConfig :) | |
86 if(stanza.getChild("query").getChild("username")) { | |
87 var username = stanza.getChild("query").getChild("username").getText(); | |
88 var password = stanza.getChild("query").getChild("password").getText(); | |
89 ispdb.get(username.split("@")[1], function(data) { | |
90 if(data == null) { | |
91 cl.send(new xmpp.Element("iq", {to: stanza.attrs.from, from: config.XMPP.jid, id: stanza.attrs.id, type: "error"})); | |
92 } | |
93 else { | |
94 var smtp = data.outgoing; | |
95 test.port = smtp.port; | |
96 test.host = smtp.hostname; | |
97 if(smtp.socketType == "SSL") | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
98 test.ssl = true; |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
99 if((smtp.authentication == "secure") || (smtp.authentication == "plain")) |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
100 test.use_authentication = true; |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
101 test.sender = username; |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
102 test.user = username; |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
103 test.pass = password; |
0 | 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; | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
159 smtp.SMTP = settings; |
0 | 160 smtp.send_mail( |
161 { | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
162 sender:settings.sender, |
0 | 163 to: stanza.attrs.to.split("@")[0].replace("%", "@"), |
164 subject: (type == "normal" ? stanza.getChildText("subject") : ""), | |
165 body: stanza.getChildText("body") | |
166 }, | |
167 function(error, success){ | |
168 if(error) { | |
169 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.")); | |
170 } | |
171 } | |
172 ); | |
173 } | |
174 ); | |
175 } | |
176 } | |
177 ); | |
178 cl.on('error', | |
179 function(e) { | |
180 console.log(e); | |
181 } | |
182 ); | |
183 |