Mercurial > psgxs
annotate 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 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env node |
2 | |
3 /* | |
4 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
5 * | |
6 * This file is part of PSĜS, a PubSub server written in JavaScript. | |
7 * | |
8 * PSĜS is free software: you can redistribute it and/or modify | |
9 * it under the terms of the GNU Affero General Public License as | |
10 * published by the Free Software Foundation, either version 3 of the | |
11 * License. | |
12 * | |
13 * PSĜS is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU Affero General Public License for more details. | |
17 * | |
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/>. | |
20 */ | |
21 | |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
22 //'use strict'; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
23 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
24 var config = require('./configuration'); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
25 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
26 var xmpp = require('node-xmpp'); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
27 var conn = new xmpp.Component({ |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
28 jid: config.jid, |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
29 password: config.password, |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
30 host: 'localhost', |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
31 port: 5347 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
32 }); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
33 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
34 if (config.debug) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
35 (function() { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
36 var send = conn.send; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
37 conn.send = function(s) { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
38 console.log('Sent: [1;32m' + s + '[0m'); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
39 send.call(conn, s); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
40 }; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
41 })(); |
43
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
42 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
43 conn.on('stanza', function (stanza) { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
44 if (config.debug) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
45 console.log('Recv: [1;34m' + stanza + '[0m'); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
46 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
47 if (stanza.is('iq')) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
48 onIq(stanza); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
49 else if (stanza.is('message')) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
50 onMessage(stanza); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
51 else if (stanza.is('presence')) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
52 onPresence(stanza); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
53 }); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
54 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
55 conn._uniqueId = 42; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
56 conn.getUniqueId = function(suffix) { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
57 return ++this._uniqueId + (suffix?(":"+suffix):""); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
58 }; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
59 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
60 var Element = xmpp.Element; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
61 Element.prototype.getAttribute = function(name) { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
62 return this.attrs[name]; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
63 }; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
64 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
65 |
0 | 66 require('./iso8601'); |
67 var storage = require('./storage'); | |
68 var errors = require('./errors'); | |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
69 var makeError = errors.makeError; |
0 | 70 var forms = require('./forms'); |
71 | |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
72 var notifs = require('./notifs'); |
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
73 notifs.setConnection(conn); |
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
74 |
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
75 var modules = require('./modules'); |
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
76 |
12
9a6b8b3357c6
Use new functions like console.log instead of sys.puts, and aerate a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
77 function _(obj, color) { |
9a6b8b3357c6
Use new functions like console.log instead of sys.puts, and aerate a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
78 var str = require('sys').inspect(obj, false, null); |
9a6b8b3357c6
Use new functions like console.log instead of sys.puts, and aerate a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
79 if (color) |
43
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
80 console.log('\x1b['+c+';1m' + str + '\x1b[0m'); |
12
9a6b8b3357c6
Use new functions like console.log instead of sys.puts, and aerate a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
81 else |
9a6b8b3357c6
Use new functions like console.log instead of sys.puts, and aerate a bit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
82 console.log(str); |
0 | 83 }; |
84 | |
11
0ed3c06c5191
Add an uncaught exceptions caughter to prevent the server from crashing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
10
diff
changeset
|
85 process.addListener('uncaughtException', function (err) { |
43
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
86 console.log('\x1b[41;1mUncaught exception (' + err + '), this should never happen:\x1b[0m\n' + err.stack); |
11
0ed3c06c5191
Add an uncaught exceptions caughter to prevent the server from crashing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
10
diff
changeset
|
87 }); |
0ed3c06c5191
Add an uncaught exceptions caughter to prevent the server from crashing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
10
diff
changeset
|
88 |
0 | 89 function onIq(stanza) { |
90 var type = stanza.getAttribute('type'); | |
91 var from = stanza.getAttribute('to'); | |
92 var to = stanza.getAttribute('from'); | |
93 var id = stanza.getAttribute('id'); | |
94 | |
95 var response; | |
96 if (id) | |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
97 response = new Element('iq', {to: to, from: from, type: 'result', id: id}); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
98 else |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
99 response = new Element('iq', {to: to, from: from, type: 'result'}); |
0 | 100 |
44
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
101 var send = {}; |
30
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
102 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
103 if (stanza.children.length != 1) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
104 return makeError(response, errors.bad_request.n); |
30
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
105 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
106 var payload = stanza.children[0]; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
107 var tag = payload.name; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
108 var ns = payload.attrs.xmlns; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
109 var contents = payload.children; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
110 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
111 if (tag == 'pubsub') { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
112 for (var j in contents) { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
113 var child = contents[j]; |
9
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
114 |
30
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
115 for (var k in modules) { |
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
116 var module = modules[k]; |
0 | 117 |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
118 if (module.stanza && (module.stanza != 'iq')) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
119 continue; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
120 |
30
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
121 if (module.type && (module.type != type)) |
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
122 continue; |
0 | 123 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
124 if (module.child && (module.child != tag)) |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
125 continue; |
30
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
126 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
127 if (module.ns && (module.ns != child.getNS())) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
128 continue; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
129 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
130 if (module.child2 && (module.child2 != child.name)) |
30
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
131 continue; |
0 | 132 |
30
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
133 if (module.number && (module.number != k)) |
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
134 continue; |
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
135 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
136 send.response = module.func(response, stanza, payload, to, from); |
44
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
137 if (send.response) { |
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
138 response = send.response; |
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
139 send.good = true; |
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
140 delete send.response; |
30
b2faacfefb90
Rewrite of the module manager; fix of some modules; fix of the directory backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
141 } |
0 | 142 } |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
143 } |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
144 } else { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
145 for (var k in modules) { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
146 var module = modules[k]; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
147 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
148 if (module.stanza && (module.stanza != 'iq')) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
149 continue; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
150 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
151 if (module.type && (module.type != type)) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
152 continue; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
153 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
154 if (module.child && (module.child != tag)) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
155 continue; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
156 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
157 if (module.ns && (module.ns != ns)) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
158 continue; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
159 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
160 if (module.number && (module.number != k)) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
161 continue; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
162 |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
163 send.response = module.func(response, stanza, payload, to, from); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
164 if (send.response) { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
165 response = send.response; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
166 send.good = true; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
167 delete send.response; |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
168 } |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
169 } |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
170 } |
0 | 171 |
44
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
172 conn.send(send.good? response: makeError(response, errors.feature_not_implemented.n)); |
0 | 173 } |
174 | |
175 function onMessage(stanza) { | |
176 var from = stanza.getAttribute('to'); | |
177 var to = stanza.getAttribute('from'); | |
178 var id = stanza.getAttribute('id'); | |
179 | |
180 var response; | |
181 if (id) | |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
182 response = new Element('message', {to: to, from: from, id: id}); |
0 | 183 else |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
184 response = new Element('message', {to: to, from: from}); |
0 | 185 |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
186 var send = false; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
187 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
188 for (var i in stanza.children) { |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
189 var child = stanza.children[i]; |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
190 for (var k in modules) { |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
191 var module = modules[k]; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
192 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
193 if (module.stanza !== 'message') |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
194 continue; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
195 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
196 if (module.type && (module.type != type)) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
197 continue; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
198 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
199 if (module.child && (module.child != child.name)) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
200 continue; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
201 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
202 if (module.ns && (module.ns != child.attrs.xmlns)) |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
203 continue; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
204 |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
205 if (module.number && (module.number != k)) |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
206 continue; |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
207 |
44
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
208 send.response = module.func(response, stanza, child, to, from); |
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
209 if (send.response) { |
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
210 response = send.response; |
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
211 send.good = true; |
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
212 delete send.response; |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
213 } |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
214 } |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
215 } |
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
216 |
46
3126f8d6a5e3
Typo in 3e0ca96d2dce.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
45
diff
changeset
|
217 conn.send(send.good? response: makeError(response, errors.feature_not_implemented.n)); |
44
3e0ca96d2dce
Fix an error in strict mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
218 return; |
34
dcf1f09f8cee
Add message to publish module and various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
31
diff
changeset
|
219 |
0 | 220 var x = stanza.getChild('x', 'jabber:x:data'); |
221 if (x) { | |
222 var form = forms.parse(x); | |
223 if (form.type == 'submit' && form.fields.FORM_TYPE.value == 'subscribe_authorization') { | |
224 if (form.fields.subid && form.fields.subid.value) | |
225 var subID = form.fields.subid.value; | |
226 if (form.fields.node && form.fields.node.value) | |
227 var nodeID = form.fields.node.value; | |
228 if (form.fields.subscriber_jid && form.fields.subscriber_jid.value) | |
229 var jid = form.fields.subscriber_jid.value; | |
230 if (form.fields.allow && form.fields.allow.value) | |
231 var allow = form.fields.allow.value; | |
232 | |
233 var type = allow? 'subscribed': 'none'; | |
234 var set = storage.subscribe(nodeID, jid, type) | |
235 //if (set.subid != subID) //TODO: support the multi-subscribe feature | |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
236 notifs.send(jid, 'subscription', nodeID, {jid: jid, subscription: type}); |
0 | 237 } else |
238 return makeError(response, errors.feature_not_implemented.n); | |
239 } else | |
240 return makeError(response, errors.feature_not_implemented.n); | |
241 conn.send(response) | |
242 } | |
243 | |
244 function onPresence(stanza) { | |
245 var from = stanza.getAttribute('to'); | |
246 var to = stanza.getAttribute('from'); | |
247 var id = stanza.getAttribute('id'); | |
248 | |
249 var response; | |
250 if (id) | |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
251 response = new Element('presence', {to: to, from: from, id: id}); |
0 | 252 else |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
253 response = new Element('presence', {to: to, from: from}); |
0 | 254 |
255 makeError(response, errors.feature_not_implemented.n); | |
256 } | |
257 | |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
258 if (process.argv.length >= 3) |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
259 storage.load(process.argv[2]); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
260 else |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
261 storage.load(); |
0 | 262 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
263 var stdin = process.openStdin(); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
264 stdin.setEncoding('utf8'); |
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
265 stdin.addListener('data', storage.debug); |