Mercurial > xmpp2smtp
annotate xmpp2smtp.js @ 5:c1a19866a233 default/master
Fix some bugs with SMTP settings.
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Wed, 15 Jun 2011 07:55:28 +0200 |
parents | e459f344d8bd |
children | 5cf8a08ef7fe |
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"})); | |
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; | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
66 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 | 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); | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 result.c("field", {type: "text-private", label: "Password", "var": "pass"}).c("required").up().c("value").t(fields.password); |
0 | 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") | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
97 test.ssl = true; |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
98 if((smtp.authentication == "secure") || (smtp.authentication == "plain")) |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
99 test.use_authentication = true; |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
100 test.sender = username; |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
101 test.user = username; |
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
102 test.pass = password; |
0 | 103 } |
104 fs.writeFile(path, JSON.stringify(test), function () { | |
105 cl.send(new xmpp.Element("iq", {to: stanza.attrs.from, from: config.XMPP.jid, id: stanza.attrs.id, type: "result"})); | |
106 }); | |
107 }); | |
108 | |
109 } | |
110 //not easy | |
111 else { | |
112 var fields = stanza.getChild("query").getChild("x").getChildren("field"); | |
113 for (var i=0;i<fields.length;i++) { | |
114 if(fields[i].attrs["var"] != "FORM_TYPE") | |
115 test[fields[i].attrs["var"]] = fields[i].getChild("value").getText(); | |
116 } | |
117 fs.writeFile(path, JSON.stringify(test), function () { | |
118 cl.send(new xmpp.Element("iq", {to: stanza.attrs.from, from: config.XMPP.jid, id: stanza.attrs.id, type: "result"})); | |
119 }); | |
120 } | |
121 } | |
122 //jabber:iq:version | |
123 else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "jabber:iq:version"))) { | |
124 var result = new xmpp.Element( | |
125 "iq", { | |
126 to: stanza.attrs.from, | |
127 from: config.XMPP.jid, | |
128 id: stanza.attrs.id, | |
129 type: 'result' | |
130 } | |
131 ).c("query", {xmlns: "jabber:iq:version"}) | |
132 stanza.c("name").t("xmpp2smtp"); | |
133 stanza.c("version").t("0.1"); | |
134 stanza.c("os").t(os.type()+" "+os.release()); | |
135 cl.send(result); | |
136 } | |
137 //disco#info | |
138 else if((stanza.is("iq")) && (type == "get") && (stanza.getChild("query", "http://jabber.org/protocol/disco#info"))) { | |
139 var stanza = new xmpp.Element( | |
140 'iq', { | |
141 to: stanza.attrs.from, | |
142 from: config.XMPP.jid, | |
143 id: stanza.attrs.id, | |
144 type: 'result' | |
145 } | |
146 ); | |
147 for(var i=0;i<info.length;i++) { | |
148 stanza.cnode(info[i]); | |
149 } | |
150 cl.send(stanza); | |
151 } | |
152 //message | |
153 else if (stanza.is('message') && (type == 'chat')||(type == 'normal')) { | |
154 fs.readFile("storage/"+stanza.attrs['from'].split("/")[0], | |
155 function(err, data) { | |
156 var settings; | |
157 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
|
158 smtp.SMTP = settings; |
0 | 159 smtp.send_mail( |
160 { | |
5
c1a19866a233
Fix some bugs with SMTP settings.
Sonny Piers <sonny.piers@gmail.com>
parents:
0
diff
changeset
|
161 sender:settings.sender, |
0 | 162 to: stanza.attrs.to.split("@")[0].replace("%", "@"), |
163 subject: (type == "normal" ? stanza.getChildText("subject") : ""), | |
164 body: stanza.getChildText("body") | |
165 }, | |
166 function(error, success){ | |
167 if(error) { | |
168 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.")); | |
169 } | |
170 } | |
171 ); | |
172 } | |
173 ); | |
174 } | |
175 } | |
176 ); | |
177 cl.on('error', | |
178 function(e) { | |
179 console.log(e); | |
180 } | |
181 ); | |
182 |