This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
Manager.coffee
264 lines (225 loc) · 8.12 KB
/
Manager.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# This program is distributed under the terms of the MIT license.
# Copyright 2012 (c) Markus Kohlhase <mail@markus-kohlhase.de>
events = require "events"
joap = require "node-xmpp-joap"
ltx = require "ltx"
class Manager extends events.EventEmitter
constructor: (@xmpp) ->
@classes = {}
@serverDescription={'en-US':"JOAP Server"}
@serverAttributes = {}
@serverMethods = {}
@router = new joap.Router @xmpp
@router.on "describe", @onDescribe
@router.on "add", @onAdd
@router.on "read", @onRead
@router.on "edit", @onEdit
@router.on "delete", @onDelete
@router.on "search", @onSearch
@objects = {}
@getArgNames: (fn) ->
args = fn.toString().match(/function\b[^(]*\(([^)]*)\)/)[1]
args.split /\s*,\s*/
# override if you want to manipule the request
before:
add: (a, callback) -> callback null, a
# override if you want to manipule the reading of instances
read: (a, callback) -> callback null, a
# override if you want to manipule the editing of instances
edit: (a, callback) -> callback null, a
# override if you want to manipule the deletion of instances
delete: (a, callback) -> callback null, a
# override if you want to manipule the description of instances
describe: (a, callback) -> callback null, a
# override if you want to manipule the search of instances
search: (a, callback) -> callback null, a
# override if you want to use a database
saveInstance: (clazz, id, obj, next) ->
@objects[clazz][id] = obj
next null
# override if you want to use a database
loadInstance: (clazz, id, next) -> next null, @objects[clazz][id]
# override if you want to use a database
queryInstances: (clazz, attrs, next) ->
if typeof attrs is "function"
next = attrs
attrs = null
if attrs?
items = []
for id, o of @objects[clazz]
items.push id for k,v of attrs when o[k] is v
next null, items
else
next null, (k for k,v of @objects[clazz])
# override if you want to use a database
deleteInstance: (clazz, id, next) ->
delete @objects[clazz][id]
next null
# Public method to override by the main application
hasPermission: (action) -> true
addClass: (name, creator, required=[], protected=[], objects={}) ->
if typeof creator is "function" and not @classes[name]?
@classes[name] = { creator:creator, required:required, protected:protected }
@objects[name] = objects
true
else false
createInstance: (a, next) ->
clazz = @classes[a.class]
argNames = Manager.getArgNames clazz.creator
x = new clazz.creator (a.attributes[n] for n in argNames when n isnt "")...
x.id = joap.uniqueId() if not x.id
@saveInstance a.class, x.id, x, (err) =>
next err, @getAddress a.class, x.id
getAddress: (clazz, instance) ->
addr = ""
addr += "#{clazz}@" if clazz if typeof clazz is "string"
addr += @router.xmpp.jid
addr += "/#{instance}" if typeof instance is "string"
addr
onRead: (a) =>
if @grant(a) and @classExists(a)
@instanceExists a, (err, exists) =>
if exists and not err? and @areExistingAttributes(a)
@before.read a, (err, a) =>
if err?
@sendInternalServerError a
else
res = {}
@loadInstance a.class, a.instance, (err, inst) =>
if a.limits
res[k] = v for k,v of inst when k in a.limits and typeof v isnt "function"
else
res[k] = v for k,v of inst when typeof v isnt "function"
@router.sendResponse a, res
onAdd: (a) =>
if @grant(a) and @isClassAddress(a) and @classExists(a) and @areRequiredAttributes(a)
@before.add a, (err, a) =>
if err?
@sendInternalServerError a
else
@createInstance a, (err, address) =>
if err?
@sendInternalServerError a
else
@router.sendResponse a, address
onEdit: (a) =>
if @grant(a)
#TODO: Is it possible to edit the server attributes?
@instanceExists a, (err, exists) =>
if exists and not err? and @areWritableAttributes(a)
@before.edit a, (err, a) =>
if err?
@sendInternalServerError a
else
@loadInstance a.class, a.instance, (err, inst) =>
if err?
@sendInternalServerError a
else
inst[k] = v for k,v of a.attributes
@saveInstance a.class, a.instance, inst, (err) =>
if err?
@sendInternalServerError a
else
@router.sendResponse a
onDelete: (a) =>
if @grant(a) and @isInstanceAddress(a)
@instanceExists a, (err, exists) =>
if exists and not err?
@before.delete a, (err, a) =>
if err?
@sendInternalServerError a
else
@deleteInstance a.class, a.instance, (err) =>
if err?
@sendInternalServerError a
else
@router.sendResponse a
onDescribe: (a) =>
if @grant(a)
@before.describe a, (err, a) =>
if err?
@sendInternalServerError a
else
data = null
if not a.class?
data = desc: @serverDescription
data.attributes = @serverAttributes
#TODO: Implement class descriptions
# data.methods = @getMethodDescriptions @serverMethods
classes = (k for k,v of @classes)
data.classes = classes if classes.length > 0
@router.sendResponse a, data
#TODO: Implement class descriptions
# else if a.class? and not a.instance?
#TODO: Implement object descriptions
# else if a.class? and a.instance?
else
@sendInternalServerError a
onSearch: (a) =>
if @grant(a) and @isClassAddress(a) and @classExists(a)
@before.search a, (err, a) =>
if err?
@sendInternalServerError a
else
@queryInstances a.class, a.attributes, (err, items) =>
if err? or not items?
@sendInternalServerError a
else
@router.sendResponse a, (@getAddress a.class, id for id in items)
grant: (a) ->
if not @hasPermission a
@router.sendError a, 403, "You are not authorized"
false
else true
instanceExists: (a, next) ->
@loadInstance a.class, a.instance, (err, inst) =>
if not inst? or err?
@router.sendError a, 404, "Object '#{a.instance}' does not exists"
next err, false
else next err, true
classExists: (a) ->
if not @classes[a.class]?
@router.sendError a, 404, "Class '#{a.class}' does not exists"
false
else true
isClassAddress: (a) ->
if not a.class? or a.instance?
@router.sendError a, 405, "'#{a.iq.attrs.to}' isn't a class"
false
else true
isInstanceAddress: (a) ->
if not a.class? or not a.instance?
@router.sendError a, 405, "'#{a.iq.attrs.to}' is not an instance"
false
else true
areRequiredAttributes: (a) ->
for r in @classes[a.class].required
if not a.attributes?[r]?
@router.sendError a, 406, "Invalid constructor parameters"
return false
true
areExistingAttributes: (a) ->
if a.limits?
inst = @objects[a.class][a.instance]
for l in a.limits
if inst[l] is undefined
@router.sendError a, 406, "Requested attribute '#{l}' doesn't exists"
return false
true
areWritableAttributes: (a) ->
p = @classes[a.class].protected
for k,v of a.attributes
if k in p
@router.sendError a, 406, "Attribute '#{k}' of class '#{a.class}' is not writeable"
return false
true
sendInternalServerError: (a) ->
err = new ltx.Element "iq",
to: a.iq.attrs.from
from: a.iq.attrs.to
id: a.iq.attrs.id
type: 'error'
err.c("error", type:'cancel')
.c("internal-server-error", xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas')
@router.send err
exports.Manager = Manager