fix: new capabilities for groudata

This commit is contained in:
2020-12-22 17:22:20 +01:00
parent 5c461fd5d8
commit 9db8610f72
+170 -19
View File
@@ -22,6 +22,109 @@ try {
console.log("error while connect to telnet:", error);
}
function setMucData(actor, muc, data) {
var query = "select value::jsonb as config from prosody where prosody.user=$1 and prosody.host=$2 and prosody.key=$3;"
var queryparams = [];
queryparams = muc.split("@");
queryparams[2] = "_data";
dbpool.query(query, queryparams, function (derr, dres) {
if (derr == null) {
console.log("got data: ", dres.rows[0]);
let e2e = "0";
if (dres.rows[0].config.e2e) {
e2e = "1";
}
let persistentroom = "0";
if (dres.rows[0].config.persistent) {
persistentroom = "1";
}
let changesubject = "0";
if (dres.rows[0].config.changesubject) {
changesubject = "1";
}
let publicroom = "0";
if (dres.rows[0].config.publicroom) {
publicroom = "1";
}
let roomname = "";
if (dres.rows[0].config.roomname) {
roomname = dres.rows[0].config.roomname;
}
let moderatedroom = "0";
let membersonly = "0";
let whois = "";
if (dres.rows[0].config.whois) {
whois = dres.rows[0].config.whois;
}
let historylength = 5;
if (dres.rows[0].config.historylength) {
historylength = dres.rows[0].config.historylength;
}
let vdata = JSON.stringify(data);
vdata = vdata.replace('/\&/g', '&');
vdata = vdata.replace('/\"/g', '"');
vdata = vdata.replace('/</g', '&lt;');
vdata = vdata.replace('/>/g', '&gt;');
vdata = vdata.replace('/"/g', '&quot;');
var messageStanza = "";
var msg_id = "";
var dictionary = "abcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 12; i++) {
msg_id += dictionary.charAt(Math.floor(Math.random() * 12));
}
messageStanza = "<iq xmlns=\"jabber:client\" from=\"" + actor + "\" to=\"" + muc +"\" type=\"set\" id=\"" + msg_id + "\">";
messageStanza += "<query xmlns=\"http://jabber.org/protocol/muc#owner\"><x xmlns=\"jabber:x:data\" type=\"submit\"><field var=\"FORM_TYPE\"><value>http://jabber.org/protocol/muc#roomconfig</value></field> ";
messageStanza += "<field var=\"muc#roomconfig_persistentroom\"><value>" + persistentroom + "</value></field>";
messageStanza += "<field var=\"muc#roomconfig_changesubject\"><value>" + changesubject + "</value></field>";
messageStanza += "<field var=\"muc#roomconfig_publicroom\"><value>" + publicroom + "</value></field>";
messageStanza += "<field var=\"muc#roomconfig_roomname\"><value>" + roomname + "</value></field>";
messageStanza += "<field var=\"muc#roomconfig_moderatedroom\"><value>0</value></field>";
messageStanza += "<field var=\"muc#roomconfig_membersonly\"><value>0</value></field>";
messageStanza += "<field var=\"muc#roomconfig_whois\"><value>" + whois + "</value></field>";
messageStanza += "<field var=\"muc#roomconfig_historylength\"><value>" + historylength.toString() + "</value></field>";
messageStanza += "<field var=\"muc#roomconfig_e2e\"><value>" + e2e + "</value></field>";
messageStanza += "<field var=\"muc#roomconfig_vdata\"><value>" + vdata + "</value></field></x></query></iq>";
var authHeader = "";
if (config.prosodyRESTuser && config.prosodyRESTuser !="") {
authHeader = "Basic " + new Buffer(config.prosodyRESTuser + ":" + config.prosodyRESTsecret).toString("base64");
} else {
authHeader = "Basic " + new Buffer("prosody:" + config.prosodyRESTsecret).toString("base64");
}
console.log(moment().format("LTS") + " [xmpp-rest] messageStanza", messageStanza);
request.post(
{
url: config.prosodyRESTUrl,
body: messageStanza,
headers: {
"Authorization" : authHeader,
"Content-Type": "text/xml"
}
},
function (error, response, body) {
if (!error && (response.statusCode == 200 || response.statusCode == 201)) {
console.log(moment().format("LTS") + " [xmpp-rest succes for] ", actor);
return true;
} else {
console.log(moment().format("LTS") + " [xmpp-rest error] ", body, error);
return false;
}
}
);
} else {
logging("setMucData", "called for actor:" + actor + " with target: " + muc + " created error: " + derr);
return derr;
};
});
}
var dbpool = new Pool({
database: config.database.name,
user: config.database.user,
@@ -106,29 +209,77 @@ router.put('/groupchats/:target', async function (req, res) {
} else {
let user = req.body.user;
let subject = req.body.subject;
let valid = false;
let data;
try {
data = JSON.stringify(req.body.data);
valid = true;
} catch (e) {
console.log("error parsing data: ", e);
valid = false;
}
let has_subject = true;
let has_data = true;
if (subject == null || subject == "") {
res.status(500).json({message: '500 empty subject'});
} else {
if ( connection && connection.socket) {
if ( connection.socket.writable === false ) {
res.status(500).json({message: 'telnet connection is not established.'});
} else {
let response = await connection.send('muc:room(\"'+ target +'\"):set_subject(\"'+ user +'\", \"'+ subject +'\");\n');
let rawresults = response.split("\n");
let mucresults = [];
for (var i = 0; i < rawresults.length - 1 ; i++) {
if ((rawresults[i].startsWith("\u0000| ") || rawresults[i].startsWith("\| ")) && !rawresults[i].startsWith("| OK") && !rawresults[i].startsWith("\u0000| Result") ) {
console.log("adding: ", rawresults[i].split("| ")[1].split("\r")[0]);
mucresults.push(rawresults[i].split("| ")[1].split("\r")[0]);
has_subject = false;
};
if (data == null) {
has_data = false;
}
if (has_data || has_subject) {
if (has_subject) {
if ( connection && connection.socket) {
if ( connection.socket.writable === false ) {
res.status(500).json({message: 'telnet connection is not established.'});
} else {
let response = await connection.send('muc:room(\"'+ target +'\"):set_subject(\"'+ user +'\", \"'+ subject +'\");\n');
let rawresults = response.split("\n");
let mucresults = [];
for (var i = 0; i < rawresults.length - 1 ; i++) {
if ((rawresults[i].startsWith("\u0000| ") || rawresults[i].startsWith("\| ")) && !rawresults[i].startsWith("| OK") && !rawresults[i].startsWith("\u0000| Result") ) {
console.log("adding: ", rawresults[i].split("| ")[1].split("\r")[0]);
mucresults.push(rawresults[i].split("| ")[1].split("\r")[0]);
}
}
console.log('async rawresult:', rawresults);
console.log('async result:', mucresults);
res.status(200).json(mucresults);
}
console.log('async rawresult:', rawresults);
console.log('async result:', mucresults);
res.status(200).json(mucresults);
} else {
res.status(404).json({message: 'connection not available'});
}
} else {
res.status(404).json({message: 'connection not available'});
if (has_data) {
var queryparams = [];
queryparams[0] = target.split("@")[0];
queryparams[1] = target.split("@")[1];
queryparams[2] = '_affiliations';
queryparams[3] = '":"admin';
queryparams[4] = '"';
queryparams[5] = '":"owner';
var actor = "";
var query = "select split_part(split_part(prosody.value, $4, 1), $5, 2) as actor from prosody where prosody.user=$1 and prosody.host=$2 and prosody.key=$3";
query += "union select split_part(split_part(prosody.value, $6, 1), $5, 2) as actor from prosody where prosody.user=$1 and prosody.host=$2 and prosody.key=$3";
dbpool.query(query, queryparams, function (derr, dres) {
if (derr == null) {
if (dres.rowCount > 0) {
actor = dres.rows[0].actor;
setMucData(actor, req.params.target, req.body.data);
res.status(200).json(null);
} else {
res.status(404).json({err: "target not found"});
}
} else {
res.status(500).json(derr);
}
});
} else {
res.status(500).json({message: '500 insufficient data'});
}
}
} else {
res.status(500).json({message: '500 insufficient data'});
}
}
});
@@ -160,7 +311,7 @@ router.get('/affiliations/:target', async function (req, res) {
});
/*
* Get Online users detail
* Get Online users detail
*/
router.get('/occupants/:target', async function (req, res) {
var target = req.params.target;
@@ -222,4 +373,4 @@ router.post('/affiliations/:target', async function (req, res) {
}
});
module.exports = router;
module.exports = router;