Mercurial > psgxs
comparison psgxs.js @ 56:99bd1d1ac071
Migration to node-xmpp, done!
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 10 Aug 2011 15:11:22 -0700 |
parents | 0d3f18bb1d36 |
children | addbf6bbfaa8 |
comparison
equal
deleted
inserted
replaced
55:fd69d35cf2e6 | 56:99bd1d1ac071 |
---|---|
17 * | 17 * |
18 * You should have received a copy of the GNU Affero General Public License | 18 * You should have received a copy of the GNU Affero General Public License |
19 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. | 19 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. |
20 */ | 20 */ |
21 | 21 |
22 'use strict'; | 22 //'use strict'; |
23 | 23 |
24 var xmpp = require('xmpp'); | 24 var config = require('./configuration'); |
25 var sha1 = require('sha1'); | 25 |
26 var xmpp = require('node-xmpp'); | |
27 var conn = new xmpp.Component({ | |
28 jid: config.jid, | |
29 password: config.password, | |
30 host: 'localhost', | |
31 port: 5347 | |
32 }); | |
33 | |
34 if (config.debug) | |
35 (function() { | |
36 var send = conn.send; | |
37 conn.send = function(s) { | |
38 console.log('Sent: [1;32m' + s + '[0m'); | |
39 send.call(conn, s); | |
40 }; | |
41 })(); | |
42 | |
43 conn.on('stanza', function (stanza) { | |
44 if (config.debug) | |
45 console.log('Recv: [1;34m' + stanza + '[0m'); | |
46 | |
47 if (stanza.is('iq')) | |
48 onIq(stanza); | |
49 else if (stanza.is('message')) | |
50 onMessage(stanza); | |
51 else if (stanza.is('presence')) | |
52 onPresence(stanza); | |
53 }); | |
54 | |
55 conn._uniqueId = 42; | |
56 conn.getUniqueId = function(suffix) { | |
57 return ++this._uniqueId + (suffix?(":"+suffix):""); | |
58 }; | |
59 | |
60 var Element = xmpp.Element; | |
61 Element.prototype.getAttribute = function(name) { | |
62 return this.attrs[name]; | |
63 }; | |
64 | |
65 | |
26 require('./iso8601'); | 66 require('./iso8601'); |
27 var storage = require('./storage'); | 67 var storage = require('./storage'); |
28 var errors = require('./errors'); | 68 var errors = require('./errors'); |
29 var makeError = errors.makeError; | 69 var makeError = errors.makeError; |
30 var fdsq = require('./fdsq'); | |
31 var toBare = fdsq.toBare; | |
32 var config = require('./configuration'); | |
33 var forms = require('./forms'); | 70 var forms = require('./forms'); |
34 var conn = new xmpp.Connection(config.host, config.port); | |
35 | 71 |
36 var notifs = require('./notifs'); | 72 var notifs = require('./notifs'); |
37 notifs.setConnection(conn); | 73 notifs.setConnection(conn); |
38 | 74 |
39 var modules = require('./modules'); | 75 var modules = require('./modules'); |
40 | |
41 var service_configuration = config.service_configuration; | |
42 var componentJID = config.jid; | |
43 var componentPassword = config.password; | |
44 | |
45 conn.log = function (_, m) { console.log(m); }; | |
46 | 76 |
47 function _(obj, color) { | 77 function _(obj, color) { |
48 var str = require('sys').inspect(obj, false, null); | 78 var str = require('sys').inspect(obj, false, null); |
49 if (color) | 79 if (color) |
50 console.log('\x1b['+c+';1m' + str + '\x1b[0m'); | 80 console.log('\x1b['+c+';1m' + str + '\x1b[0m'); |
54 | 84 |
55 process.addListener('uncaughtException', function (err) { | 85 process.addListener('uncaughtException', function (err) { |
56 console.log('\x1b[41;1mUncaught exception (' + err + '), this should never happen:\x1b[0m\n' + err.stack); | 86 console.log('\x1b[41;1mUncaught exception (' + err + '), this should never happen:\x1b[0m\n' + err.stack); |
57 }); | 87 }); |
58 | 88 |
59 if (typeof xmpp.StanzaBuilder.cnode != 'function' || typeof xmpp.StanzaBuilder.prototype.cnode != 'function') { | |
60 xmpp.StanzaBuilder.prototype.cnode = function (stanza) | |
61 { | |
62 var parent = this.last_node[this.last_node.length-1]; | |
63 parent.tags.push(stanza); | |
64 parent.children.push(stanza); | |
65 this.last_node.push(stanza); | |
66 return this; | |
67 }; | |
68 } | |
69 | |
70 function onIq(stanza) { | 89 function onIq(stanza) { |
71 var type = stanza.getAttribute('type'); | 90 var type = stanza.getAttribute('type'); |
72 var from = stanza.getAttribute('to'); | 91 var from = stanza.getAttribute('to'); |
73 var to = stanza.getAttribute('from'); | 92 var to = stanza.getAttribute('from'); |
74 var id = stanza.getAttribute('id'); | 93 var id = stanza.getAttribute('id'); |
75 | 94 |
76 var response; | 95 var response; |
77 if (id) | 96 if (id) |
78 response = xmpp.iq({to: to, from: from, type: 'result', id: id}); | 97 response = new Element('iq', {to: to, from: from, type: 'result', id: id}); |
79 else | 98 else |
80 response = xmpp.iq({to: to, from: from, type: 'result'}); | 99 response = new Element('iq', {to: to, from: from, type: 'result'}); |
81 | 100 |
82 var send = {}; | 101 var send = {}; |
83 | 102 |
84 for (var i in stanza.tags) { | 103 if (stanza.children.length != 1) |
85 var child1 = stanza.tags[i]; | 104 return makeError(response, errors.bad_request.n); |
86 if (child1.name == 'pubsub') { | 105 |
87 for (var j in child1.tags) { | 106 var payload = stanza.children[0]; |
88 var child2 = child1.tags[j]; | 107 var tag = payload.name; |
89 | 108 var ns = payload.attrs.xmlns; |
90 for (var k in modules) { | 109 var contents = payload.children; |
91 var module = modules[k]; | 110 |
92 | 111 if (tag == 'pubsub') { |
93 if (module.stanza && (module.stanza != 'iq')) | 112 for (var j in contents) { |
94 continue; | 113 var child = contents[j]; |
95 | 114 |
96 if (module.type && (module.type != type)) | |
97 continue; | |
98 | |
99 if (module.child2 && (module.child2 != child2.name)) | |
100 continue; | |
101 | |
102 if (module.ns && (module.ns != child2.attr.xmlns)) | |
103 continue; | |
104 | |
105 if (module.number && (module.number != j)) | |
106 continue; | |
107 | |
108 send.response = module.func(response, stanza, child2, to, from); | |
109 if (send.response) { | |
110 response = send.response; | |
111 send.good = true; | |
112 delete send.response; | |
113 } | |
114 } | |
115 } | |
116 } else { | |
117 for (var k in modules) { | 115 for (var k in modules) { |
118 var module = modules[k]; | 116 var module = modules[k]; |
119 | 117 |
120 if (module.stanza && (module.stanza != 'iq')) | 118 if (module.stanza && (module.stanza != 'iq')) |
121 continue; | 119 continue; |
122 | 120 |
123 if (module.type && (module.type != type)) | 121 if (module.type && (module.type != type)) |
124 continue; | 122 continue; |
125 | 123 |
126 if (module.child && (module.child != child1.name)) | 124 if (module.child && (module.child != tag)) |
127 continue; | 125 continue; |
128 | 126 |
129 if (module.ns && (module.ns != child1.attr.xmlns)) | 127 if (module.ns && (module.ns != child.getNS())) |
128 continue; | |
129 | |
130 if (module.child2 && (module.child2 != child.name)) | |
130 continue; | 131 continue; |
131 | 132 |
132 if (module.number && (module.number != k)) | 133 if (module.number && (module.number != k)) |
133 continue; | 134 continue; |
134 | 135 |
135 send.response = module.func(response, stanza, child1, to, from); | 136 send.response = module.func(response, stanza, payload, to, from); |
136 if (send.response) { | 137 if (send.response) { |
137 response = send.response; | 138 response = send.response; |
138 send.good = true; | 139 send.good = true; |
139 delete send.response; | 140 delete send.response; |
140 } | 141 } |
141 } | 142 } |
142 } | 143 } |
144 } else { | |
145 for (var k in modules) { | |
146 var module = modules[k]; | |
147 | |
148 if (module.stanza && (module.stanza != 'iq')) | |
149 continue; | |
150 | |
151 if (module.type && (module.type != type)) | |
152 continue; | |
153 | |
154 if (module.child && (module.child != tag)) | |
155 continue; | |
156 | |
157 if (module.ns && (module.ns != ns)) | |
158 continue; | |
159 | |
160 if (module.number && (module.number != k)) | |
161 continue; | |
162 | |
163 send.response = module.func(response, stanza, payload, to, from); | |
164 if (send.response) { | |
165 response = send.response; | |
166 send.good = true; | |
167 delete send.response; | |
168 } | |
169 } | |
143 } | 170 } |
144 | 171 |
145 conn.send(send.good? response: makeError(response, errors.feature_not_implemented.n)); | 172 conn.send(send.good? response: makeError(response, errors.feature_not_implemented.n)); |
146 } | 173 } |
147 | 174 |
150 var to = stanza.getAttribute('from'); | 177 var to = stanza.getAttribute('from'); |
151 var id = stanza.getAttribute('id'); | 178 var id = stanza.getAttribute('id'); |
152 | 179 |
153 var response; | 180 var response; |
154 if (id) | 181 if (id) |
155 response = xmpp.message({to: to, from: from, id: id}); | 182 response = new Element('message', {to: to, from: from, id: id}); |
156 else | 183 else |
157 response = xmpp.message({to: to, from: from}); | 184 response = new Element('message', {to: to, from: from}); |
158 | 185 |
159 var send = false; | 186 var send = false; |
160 | 187 |
161 for (var i in stanza.tags) { | 188 for (var i in stanza.children) { |
162 var child = stanza.tags[i]; | 189 var child = stanza.children[i]; |
163 for (var k in modules) { | 190 for (var k in modules) { |
164 var module = modules[k]; | 191 var module = modules[k]; |
165 | 192 |
166 if (module.stanza !== 'message') | 193 if (module.stanza !== 'message') |
167 continue; | 194 continue; |
170 continue; | 197 continue; |
171 | 198 |
172 if (module.child && (module.child != child.name)) | 199 if (module.child && (module.child != child.name)) |
173 continue; | 200 continue; |
174 | 201 |
175 if (module.ns && (module.ns != child.attr.xmlns)) | 202 if (module.ns && (module.ns != child.attrs.xmlns)) |
176 continue; | 203 continue; |
177 | 204 |
178 if (module.number && (module.number != k)) | 205 if (module.number && (module.number != k)) |
179 continue; | 206 continue; |
180 | 207 |
219 var to = stanza.getAttribute('from'); | 246 var to = stanza.getAttribute('from'); |
220 var id = stanza.getAttribute('id'); | 247 var id = stanza.getAttribute('id'); |
221 | 248 |
222 var response; | 249 var response; |
223 if (id) | 250 if (id) |
224 response = xmpp.presence({to: to, from: from, id: id}); | 251 response = new Element('presence', {to: to, from: from, id: id}); |
225 else | 252 else |
226 response = xmpp.presence({to: to, from: from}); | 253 response = new Element('presence', {to: to, from: from}); |
227 | 254 |
228 makeError(response, errors.feature_not_implemented.n); | 255 makeError(response, errors.feature_not_implemented.n); |
229 } | 256 } |
230 | 257 |
231 conn.connect(componentJID, componentPassword, function (status, condition) { | 258 if (process.argv.length >= 3) |
232 if (status == xmpp.Status.CONNECTED) { | 259 storage.load(process.argv[2]); |
233 conn.addHandler(onMessage, null, 'message', null, null, null); | 260 else |
234 conn.addHandler(onIq, null, 'iq', null, null, null); | 261 storage.load(); |
235 conn.addHandler(onPresence, null, 'presence', null, null, null); | 262 |
236 | 263 var stdin = process.openStdin(); |
237 if (process.argv.length >= 3) | 264 stdin.setEncoding('utf8'); |
238 storage.load(process.argv[2]); | 265 stdin.addListener('data', storage.debug); |
239 else | |
240 storage.load(); | |
241 | |
242 var stdin = process.openStdin(); | |
243 stdin.setEncoding('utf8'); | |
244 stdin.addListener('data', storage.debug); | |
245 } else | |
246 conn.log(xmpp.LogLevel.DEBUG, 'New connection status: ' + status + (condition? (' ('+condition+')'): '')); | |
247 }); |