Merge branch 'xmpp-rest-wrapper'
This commit is contained in:
+2
-34
@@ -5,13 +5,12 @@ var express = require('express');
|
||||
require('express-async-errors');
|
||||
var bodyParser = require('body-parser');
|
||||
var request = require('request');
|
||||
var Telnet = require('telnet-client')
|
||||
|
||||
var app = express();
|
||||
var env = process.env.NODE_ENV || 'development';
|
||||
var config = require('../config/prosody-muc-rest.js')[env];
|
||||
console.log(moment().format("LTS") + ' Using configuration', config);
|
||||
|
||||
var routes = require('../routes/index');
|
||||
|
||||
var servicePort = config.servicePort;
|
||||
|
||||
@@ -19,38 +18,7 @@ app.use(bodyParser.json()); // for parsing application/json
|
||||
app.use(logger('dev'));
|
||||
|
||||
|
||||
app.get('/', async function (req, res) {
|
||||
let connection = new Telnet()
|
||||
|
||||
let params = {
|
||||
host: '127.0.0.1',
|
||||
port: config.prosodyPort,
|
||||
// shellPrompt: '', // or negotiationMandatory: false
|
||||
negotiationMandatory: false,
|
||||
timeout: 15000,
|
||||
debug: true
|
||||
}
|
||||
|
||||
try {
|
||||
await connection.connect(params);
|
||||
} catch(error) {
|
||||
// handle the throw (timeout)
|
||||
}
|
||||
|
||||
let response = await connection.send('muc:list("conference.demo.vnc.de");\n');
|
||||
let rawresults = response.split("| http://prosody.im/doc/console")[1].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);
|
||||
});
|
||||
|
||||
app.use('/api/', routes);
|
||||
|
||||
var server = app.listen(servicePort, function () {
|
||||
var host = server.address().address;
|
||||
|
||||
@@ -1,6 +1,21 @@
|
||||
module.exports = {
|
||||
development: {
|
||||
servicePort: 9511,
|
||||
prosodyPort: 5582
|
||||
telnet: {
|
||||
host: "127.0.0.1",
|
||||
port: 5582,
|
||||
negotiationMandatory: false,
|
||||
timeout: 1500
|
||||
},
|
||||
database: {
|
||||
host: "127.0.0.1",
|
||||
port: 5432,
|
||||
name: "prosody",
|
||||
user: "postgres",
|
||||
pass: "postgres",
|
||||
idleTimeoutMillis: 10000,
|
||||
connectionTimeoutMillis: 10000
|
||||
},
|
||||
mucDomain: "conference.demo.vnc.de"
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,21 @@
|
||||
module.exports = {
|
||||
development: {
|
||||
servicePort: 9511,
|
||||
prosodyPort: 5582
|
||||
telnet: {
|
||||
host: "127.0.0.1",
|
||||
port: 5582,
|
||||
negotiationMandatory: false,
|
||||
timeout: 1500
|
||||
},
|
||||
database: {
|
||||
host: "192.168.21.143",
|
||||
port: 5432,
|
||||
name: "prosody",
|
||||
user: "prosody",
|
||||
pass: "vahG7ooli7thee0iek4Ewoo9Sai6oWah",
|
||||
idleTimeoutMillis: 10000,
|
||||
connectionTimeoutMillis: 10000
|
||||
},
|
||||
mucDomain: "conference.demo.vnc.de"
|
||||
}
|
||||
};
|
||||
|
||||
+3
-1
@@ -21,6 +21,8 @@
|
||||
"moment": "2.22.2",
|
||||
"morgan": "1.8.2",
|
||||
"request": "2.83.0",
|
||||
"telnet-client": "^1.2.7"
|
||||
"telnet-client": "^1.2.7",
|
||||
"pg": "7.6.1",
|
||||
"pg-pool": "2.0.4"
|
||||
}
|
||||
}
|
||||
|
||||
+225
@@ -0,0 +1,225 @@
|
||||
var express = require("express");
|
||||
var router = express.Router();
|
||||
var Telnet = require('telnet-client');
|
||||
var env = process.env.NODE_ENV || 'development';
|
||||
var config = require('../config/prosody-muc-rest.js')[env];
|
||||
var Pool = require('pg-pool');
|
||||
|
||||
var connection = new Telnet();
|
||||
let params = {
|
||||
host: config.telnet.host,
|
||||
port: config.telnet.port,
|
||||
// shellPrompt: '', // or negotiationMandatory: false
|
||||
negotiationMandatory: config.telnet.negotiationMandatory,
|
||||
timeout: config.telnet.timeout,
|
||||
debug: true
|
||||
}
|
||||
|
||||
try {
|
||||
connection.connect(params);
|
||||
} catch(error) {
|
||||
// handle the throw (timeout)
|
||||
console.log("error while connect to telnet:", error);
|
||||
}
|
||||
|
||||
var dbpool = new Pool({
|
||||
database: config.database.name,
|
||||
user: config.database.user,
|
||||
password: config.database.pass,
|
||||
host: config.database.host,
|
||||
port: config.database.port,
|
||||
ssl: true,
|
||||
max: 20, // set pool max size to 20
|
||||
min: 4, // set min pool size to 4
|
||||
idleTimeoutMillis: config.database.idleTimeoutMillis, // close idle clients after 1 second
|
||||
connectionTimeoutMillis: config.database.connectionTimeoutMillis, // return an error after 1 second if connection could not be established
|
||||
});
|
||||
|
||||
/**
|
||||
* Get Group chat list
|
||||
*/
|
||||
router.get('/groupchats', async function (req, res) {
|
||||
var queryparams = [];
|
||||
var sqlquery="select room, created, updated from group_owners where room LIKE '%" + config.mucDomain +"'";
|
||||
if (req.query.created) {
|
||||
sqlquery += " AND created > " + req.query.created;
|
||||
}
|
||||
if (req.query.updated) {
|
||||
sqlquery += " AND updated > " + req.query.updated;
|
||||
}
|
||||
dbpool.query(sqlquery, queryparams, function (derr, dres) {
|
||||
if (derr == null) {
|
||||
if (dres.rowCount > 0) {
|
||||
let mucresults = [];
|
||||
for (var i = 0; i < dres.rowCount; i++) {
|
||||
mucresults.push({ room: dres.rows[i].room, created: dres.rows[i].created, updated: dres.rows[i].updated });
|
||||
}
|
||||
res.send(mucresults);
|
||||
} else {
|
||||
res.status(404).json([]);
|
||||
}
|
||||
} else {
|
||||
res.status(500).json(derr);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Get Group chat info
|
||||
*/
|
||||
router.get('/groupchats/:target', async function (req, res) {
|
||||
var target = req.params.target;
|
||||
if (target == null || target == "") {
|
||||
res.status(500).json({message: '500 empty groupchats target'});
|
||||
} else {
|
||||
var queryparams = [];
|
||||
queryparams[0] = target.split("@")[0];
|
||||
queryparams[1] = target.split("@")[1];
|
||||
var sqlquery="select value, created, updated from prosody INNER JOIN group_owners ON split_part(group_owners.room, '@', 1) = prosody.user AND split_part(group_owners.room, '@', 2) = prosody.host where prosody.store='config' and prosody.key='_data' and prosody.user=$1 and prosody.host=$2";
|
||||
dbpool.query(sqlquery, queryparams, function (derr, dres) {
|
||||
if (derr == null) {
|
||||
if (dres.rowCount > 0) {
|
||||
let groupInfo = JSON.parse(dres.rows[0].value);
|
||||
if (!groupInfo.subject) {
|
||||
groupInfo.subject = queryparams[0].split(".").map(v => v.replace(/^\w/, c => c.toUpperCase())).join(" ");
|
||||
}
|
||||
groupInfo.created = dres.rows[0].created;
|
||||
groupInfo.updated = dres.rows[0].updated;
|
||||
res.send(JSON.stringify(groupInfo));
|
||||
} else {
|
||||
res.status(404).json(null);
|
||||
}
|
||||
} else {
|
||||
res.status(500).json(derr);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Rename existing group subject
|
||||
*/
|
||||
router.put('/groupchats/:target', async function (req, res) {
|
||||
var target = req.params.target;
|
||||
if (target == null || target == "") {
|
||||
res.status(500).json({message: '500 empty groupchats target'});
|
||||
} else {
|
||||
let user = req.body.user;
|
||||
let subject = req.body.subject;
|
||||
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]);
|
||||
}
|
||||
}
|
||||
console.log('async rawresult:', rawresults);
|
||||
console.log('async result:', mucresults);
|
||||
res.status(200).json(mucresults);
|
||||
}
|
||||
} else {
|
||||
res.status(404).json({message: 'connection not available'});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Get affiliations info of group
|
||||
*/
|
||||
router.get('/affiliations/:target', async function (req, res) {
|
||||
var target = req.params.target;
|
||||
if (target == null || target == "") {
|
||||
res.status(500).json({message: '500 empty affiliations target'});
|
||||
} else {
|
||||
var queryparams = [];
|
||||
queryparams[0] = target.split("@")[0];
|
||||
queryparams[1] = target.split("@")[1];
|
||||
var sqlquery="select value from prosody where prosody.store='config' and prosody.key='_affiliations' and prosody.user=$1 and prosody.host=$2";
|
||||
dbpool.query(sqlquery, queryparams, function (derr, dres) {
|
||||
if (derr == null) {
|
||||
if (dres.rowCount > 0) {
|
||||
res.send(dres.rows[0].value);
|
||||
} else {
|
||||
res.status(404).json(null);
|
||||
}
|
||||
} else {
|
||||
res.status(500).json(derr);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* Get Online users detail
|
||||
*/
|
||||
router.get('/occupants/:target', async function (req, res) {
|
||||
var target = req.params.target;
|
||||
if (target == null || target == "") {
|
||||
res.status(500).json({message: '500 empty affiliations target'});
|
||||
} else {
|
||||
var queryparams = [];
|
||||
queryparams[0] = target.split("@")[0];
|
||||
queryparams[1] = target.split("@")[1];
|
||||
var sqlquery="select value from prosody where prosody.store='config' and prosody.key='_occupants' and prosody.user=$1 and prosody.host=$2";
|
||||
dbpool.query(sqlquery, queryparams, function (derr, dres) {
|
||||
if (derr == null) {
|
||||
if (dres.rowCount > 0) {
|
||||
res.send(dres.rows[0].value);
|
||||
} else {
|
||||
res.status(404).json(null);
|
||||
}
|
||||
} else {
|
||||
res.status(500).json(derr);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* add affiliation for member into group
|
||||
*/
|
||||
router.post('/affiliations/:target', async function (req, res) {
|
||||
var target = req.params.target;
|
||||
if (target == null || target == "") {
|
||||
res.status(500).json({message: '500 empty affiliations target'});
|
||||
} else {
|
||||
let user = req.body.user;
|
||||
let affiliation = req.body.affiliation ? req.body.affiliation : "member";
|
||||
if (user == null || user == "") {
|
||||
res.status(500).json({message: '500 empty user'});
|
||||
} 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_affiliation(true, \"'+ user +'\", \"' + affiliation +'\");\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);
|
||||
}
|
||||
} else {
|
||||
res.status(404).json({message: 'connection not available'});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user