-
Notifications
You must be signed in to change notification settings - Fork 7
/
handleButtonMessage.js
439 lines (367 loc) · 17.9 KB
/
handleButtonMessage.js
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
var crypto = require('crypto');
var padManager = require("ep_etherpad-lite/node/db/PadManager");
var fs = require('fs');
var settings = require('ep_etherpad-lite/node/utils/Settings');
var authorManager = require('ep_etherpad-lite/node/db/AuthorManager');
var sessionManager = require('ep_etherpad-lite/node/db/SessionManager');
// this should be overridden in codepad conf
var project_path = '/tmp/';
if (!settings.ep_codepad) {
console.log("CODEPAD needs ep_codepad parameters in settings.json.");
} else {
if (!settings.ep_codepad.project_path) {
console.log("CODEPAD No ep_codepad.project_path set in settings.json.");
} else project_path = settings.ep_codepad.project_path + "/";
}
// messages constants
var msg_push = 'PUSH_TO_FILESYSTEM';
var msg_write = 'WRITE_TO_FILESYSTEM';
var msg_read = 'READ_FROM_FILESYSTEM';
var msg_check = 'CHECK_ON_FILESYSTEM';
var err_msg = {};
// code beutifier
var beautify = require('js-beautify').js_beautify;
var beautify_css = require('js-beautify').css;
var beautify_html = require('js-beautify').html;
var exec = require("child_process").exec;
var getBrush = require('./extensions.js').getBrush;
// code syntax checker
var jshint = require('jshint').JSHINT;
var cb = function() {};
var padMessageHandler = require("ep_etherpad-lite/node/handler/PadMessageHandler");
var send_ok = function(padid) {
var ok_msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
from: "codepad",
errors: null
}
}
};
padMessageHandler.handleCustomObjectMessage(ok_msg, undefined, cb);
};
exports.handleMessage = async function(hook_name, context, callback) {
if (context.message && context.message.data) {
var msg = context.message.data.type;
var uid = context.message.data.userId;
var padid = context.message.data.padId;
var uid = await authorManager.getAuthorName(uid);
// proxy for editing events - for highlighting lines of other users
if (msg === "EDIT") {
console.log("EDIT EVENT RECIEVED " + context.message.data.line);
var replymsg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
userId: uid,
line: context.message.data.line,
from: "ace"
}
}
};
padMessageHandler.handleCustomObjectMessage(replymsg, undefined, cb);
}
if (msg === msg_check) {
var pmpad = await padManager.getPad(padid);
if (pmpad) { // we might want to use pmpad.head !== 0 here..
var path = project_path + padid;
// remove newline character from the end of the string.
//var text = pmpad.atext.text.slice(0, -1);
var text = pmpad.atext.text;
fs.readFile(path, function(frerr, data) {
if (frerr) {
// notify user about the read error
console.log("CODEPAD NTC - file doesent exist " + path);
//callback(null);
} else {
var adat = data.toString();
if (adat !== text.slice(0, -1)) {
console.log("READ from filesystem. " + path);
err_msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
from: "check",
errors: "File inconsistent with pad content " + adat.length + "/" + (text.length - 1)
}
}
};
padMessageHandler.handleCustomObjectMessage(err_msg, undefined, cb);
} else
send_ok(padid);
}
});
} else console.log("CODEPAD ERR - getPad failed!");
//callback(null);
}
if (msg === msg_read) {
var pmpad = await padManager.getPad(padid);
if (pmpad === null) {
var path = project_path + padid;
// remove newline character from the end of the string.
//var text = pmpad.atext.text.slice(0, -1);
var text = pmpad.atext.text;
fs.readFile(path, function(frerr, data) {
if (frerr) {
// notify user about the read error
console.log("CODEPAD ERR - file read error " + path);
err_msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
from: "fs",
errors: frerr
}
}
};
padMessageHandler.handleCustomObjectMessage(err_msg, undefined, cb);
//callback(null);
} else {
var adat = data.toString();
if (adat !== text) {
console.log("READ from filesystem. " + path);
pmpad.setText(adat);
padMessageHandler.updatePadClients(pmpad, cb);
}
send_ok(padid);
}
});
} else console.log("CODEPAD ERR - getPad failed!");
//callback(null);
}
if (msg === msg_write || msg === msg_push) {
var pmpad = await padManager.getPad(padid);
var api = require('ep_etherpad-lite/node/db/API');
// if all ok, send only one OK message.
var all_is_ok = true;
// check if anyone editing while a push was sent.
var now = new Date().getTime();
var padusr = await api.padUsers(padid)
if (padusr !== null) {
var pushusr = '';
var pusherr = '';
if (msg == msg_push) {
// check each user
for (var i = 0; i < padus.padUsers.length; i++) {
var obi = padus.padUsers[i];
// exept the one who requested the push - timout limit is 5 sec
if (obi.id !== uid && now - obi.timestamp < 5000) {
all_is_ok = false;
if (obi.name) pusherr += obi.name.toString();
else pusherr += obi.id.toString();
pusherr += " ";
console.log("CODEPAD push denied " + obi.name + " " + obi.id + " is editing the pad. " + padid);
}
// to let other users know
if (obi.name) {
if (obi.id == uid) pushusr = obi.name.toString();
}
}
}
if (!all_is_ok) {
err_msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
from: "push",
user: uid,
name: pushusr,
errors: pusherr
}
}
};
padMessageHandler.handleCustomObjectMessage(err_msg, undefined, cb);
} else {
var padsi = padid.indexOf('/');
var folder = '';
// since EEXISTS is an error, we skip that error
// there will be a message if the write failes anyway
mkdir_err = function(err) {};
// create subfolders
while (padsi > 0) {
folder = padid.substring(0, padsi);
fs.mkdir(project_path + folder, mkdir_err);
padsi = padid.indexOf('/', 1 + folder.length);
}
// full path to write the file to
var path = project_path + padid;
// get the file extension/brush
var ext = getBrush(padid);
// the beutified or the raw text of the pad
// remove newline character from the end of the string.
//var text = pmpad.atext.text.slice(0, -1);
var text = pmpad.atext.text;
var beat = text;
// if .js file beautify
if (ext === 'js') {
//check for missing semiciolons forst
if (msg == msg_push && !jshint(text)) {
// determine if semicolons should be added at the end of line
var check = function(errors, line, length) {
var count = 0;
var chpos = 0;
errors.forEach(function(err) {
if (err) {
// err.line range is 1..n while line is 1..m
if (err.line == line + 1) {
count++;
if (err.code == 'W033') chpos = err.character;
// TODO ... maybe it should abort the whole process if there was another error
}
}
});
if (count === 1 && chpos !== 0 && length < chpos) return true;
else return false;
};
// Add the semicolons
var lines = text.split('\n');
beat = '';
for (var j = 0; j < lines.length; j++) {
//code here using lines[i] which will give you each line
if (check(jshint.errors, j, lines[j].length))
beat += lines[j] + ';' + '\n';
else beat += lines[j] + '\n';
}
}
// if push and jshint said ok, then beautify text for real
if (msg == msg_push && jshint(text)) beat = beautify(text, {
indent_size: 4
});
// re-check the new beautified code
if (!jshint(beat)) {
// still has errors, we put them to the console log on the server
jshint.errors.forEach(function(err) {
if (err) {
console.info(" ! " + padid + ":" + err.line + ":" + err.character + " " + err.reason + "|" + err.evidence);
// more detailed if you wish
//console.info(" ! " + padid + ":" + err.line + ":" + err.character + " " + err.reason + " !" + err.scope + "|" + err.evidence + "|" + err.id + "|" + err.code + "|" + err.raw);
}
});
all_is_ok = false;
err_msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
from: "jshint",
errors: jshint.errors
}
}
};
padMessageHandler.handleCustomObjectMessage(err_msg, undefined, cb);
}
}
// if .css file beautify
if (ext === 'css' && msg == msg_push) {
beat = beautify_css(text, {
indent_size: 4
});
}
// if .html file beautify
if (ext === 'xml' && msg == msg_push) {
beat = beautify_html(text, {
indent_size: 4
});
}
// TODO add bash beautifier and syntax checker here
// text and beat may be different, depending on jsHint and the beutifier
if (text !== beat) {
pmpad.setText(beat);
padMessageHandler.updatePadClients(pmpad, cb);
}
// If we write an empty file -- we actually will delete the file // but not delete pad
if (text.length <= 1 && text.charCodeAt(0) == 10) {
if (fs.existsSync(path)) {
fs.unlinkSync(path);
console.log("CODEPAD delete file " + path);
err_msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
from: "codepad",
errors: "File deleted."
}
}
};
padMessageHandler.handleCustomObjectMessage(err_msg, undefined, cb);
}
} else
// WRITE to the FILE
fs.writeFile(path, beat, function(err) {
if (err) {
all_is_ok = false;
console.log("CODEPAD Failed to write text to " + path);
err_msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
from: "fs",
errors: err
}
}
};
padMessageHandler.handleCustomObjectMessage(err_msg, undefined, cb);
} else {
console.log("CODEPAD Wrote pad contents to " + path);
// if push_action is defined in settings.json, it will run here, use it for git/svn/hg ... or whatever.
if (settings.ep_codepad && msg == msg_push) {
if (settings.ep_codepad.push_action) {
exec(settings.ep_codepad.push_action, function(exec_err, stdout, stderr) {
if (stdout) console.log("CODEPAD push_action stdout: " + stdout);
if (stderr) console.log("CODEPAD push_action stderr: " + stderr);
if (exec_err) {
console.log('CODEPAD push_action error: ', exec_err);
err_msg = {
type: 'COLLABROOM',
data: {
type: "CUSTOM",
payload: {
padId: padid,
from: "exec",
errors: exec_err
}
}
};
padMessageHandler.handleCustomObjectMessage(err_msg, undefined, cb);
} else if (all_is_ok) send_ok(padid);
});
} else if (all_is_ok) send_ok(padid);
} else if (all_is_ok) send_ok(padid);
}
}); // WRITE
} // else all_is_ok
} else console.log("CODEPAD error - Could not determine users."); // if paduserr
//callback(null);
} //END if (msg == msg_write || msg == msg_push)
} //END if (context.message && context.message.data)
callback();
}; //END exports.handleMessage
/// jshint - quickhelp
/* An example from err.
"id": "(error)",
"raw": "Missing semicolon.",
"code": "W033",
"evidence": " };",
"line": 6,
"character": 5,
"scope": "(main)",
"reason": "Missing semicolon."
*/
//
//