2021-06-20 09:54:50 -04:00
|
|
|
const fs = require('fs');
|
|
|
|
const ini = require('ini');
|
|
|
|
const mastodon = require('mastodon');
|
|
|
|
const mqtt = require('mqtt');
|
|
|
|
const request = require('request');
|
2022-01-30 13:55:13 -05:00
|
|
|
const rgbcolor = require('rgb-color');
|
2021-06-20 09:54:50 -04:00
|
|
|
const xmpp = require('simple-xmpp');
|
|
|
|
|
2021-12-12 09:15:09 -05:00
|
|
|
try {
|
|
|
|
fs.readFileSync('./config.ini', 'utf-8');
|
|
|
|
|
2021-06-20 09:54:50 -04:00
|
|
|
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
|
2022-03-27 19:24:35 -04:00
|
|
|
const debugging = config.debug;
|
|
|
|
|
2022-03-31 13:53:16 -04:00
|
|
|
// Asterisk config
|
|
|
|
const asterisk_callfrom = config.asterisk.callfrom;
|
|
|
|
const asterisk_callerID = config.asterisk.callerID;
|
|
|
|
const asterisk_callto = config.asterisk.callerto;
|
|
|
|
const asterisk_context = config.asterisk.context;
|
|
|
|
const asterisk_host = config.asterisk.host;
|
|
|
|
const asterisk_password = config.asterisk.password;
|
|
|
|
const asterisk_port = config.asterisk.port;
|
|
|
|
const asterisk_user = config.asterisk.user;
|
|
|
|
|
|
|
|
// Bot config
|
|
|
|
const bot_hostname = config.bot.hostname;
|
|
|
|
const bot_jid = config.bot.jid;
|
|
|
|
const bot_password = config.bot.password;
|
|
|
|
const bot_port = config.bot.port;
|
|
|
|
|
|
|
|
// Gotify config
|
|
|
|
const gotify_api_url = config.gotify.api_url;
|
|
|
|
const gotify_key = config.gotify.key;
|
|
|
|
const gotify_message = config.gotify.message;
|
|
|
|
const gotify_priority = config.gotify.priority;
|
|
|
|
const gotify_title = config.gotify.title;
|
|
|
|
|
|
|
|
// Mastodon config
|
|
|
|
const mastodon_api_url = config.mastodon.api_url;
|
|
|
|
const mastodon_token = config.mastodon.token;
|
|
|
|
const mastodon_toot = config.mastodon.toot;
|
|
|
|
|
|
|
|
// MQTT config
|
|
|
|
const mqtt_message = config.mqtt.message;
|
|
|
|
const mqtt_password = config.mqtt.password;
|
|
|
|
const mqtt_server = config.mqtt.server;
|
|
|
|
const mqtt_topic = config.mqtt.topic;
|
|
|
|
const mqtt_user = config.mqtt.user;
|
|
|
|
|
|
|
|
// Tuya (Smart Lights) config
|
|
|
|
const tuya_name = config.tuya.name;
|
|
|
|
const tuya_bright_url = config.tuya.bright_url;
|
|
|
|
const tuya_color_url = config.tuya.color_url;
|
|
|
|
const tuya_status_url = config.tuya.status_url;
|
|
|
|
const tuya_toggle_url = config.tuya.toggle_url;
|
|
|
|
|
|
|
|
// Unlocks API config
|
|
|
|
const unlocks_api_url = config.unlocks.api_url;
|
|
|
|
const unlocks_pass = config.unlocks.pass;
|
|
|
|
const unlocks_user = config.unlocks.user;
|
|
|
|
|
|
|
|
// User config (authorized user)
|
|
|
|
const user_jid = config.user.jid //.split('/')[0];
|
|
|
|
const user_name = config.user.name;
|
|
|
|
const user_server = config.user.server;
|
|
|
|
|
|
|
|
// Weather config
|
|
|
|
const weather_key = config.weather.key;
|
|
|
|
const weather_city_code = config.weather.city_code;
|
|
|
|
const weather_city_name = config.weather.city_name;
|
|
|
|
const weather_units = config.weather.units;
|
|
|
|
|
|
|
|
// Private channel creation (uses friends secion of config.ini to invite them to a private chat)
|
|
|
|
const xmpp_api_url = config.xmpp.api_url;
|
|
|
|
const xmpp_auth = config.xmpp.key;
|
|
|
|
const xmpp_muc_name = config.xmpp.muc_name;
|
|
|
|
const xmpp_muc_password = config.xmpp.muc_password;
|
|
|
|
const xmpp_muc_server= config.xmpp.muc_server;
|
2021-06-20 09:54:50 -04:00
|
|
|
|
2022-03-27 19:24:35 -04:00
|
|
|
let pc = 'This is a private command. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start for your own instance!';
|
2022-03-31 13:53:16 -04:00
|
|
|
let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${weather_city_code}&appid=${weather_key}&units=${weather_units}`
|
2021-12-07 16:43:46 -05:00
|
|
|
|
2021-06-20 09:54:50 -04:00
|
|
|
var ami = new require('asterisk-manager')(asterisk_port, asterisk_host, asterisk_user, asterisk_password, false);
|
|
|
|
var client = mqtt.connect('tcp://' + mqtt_user + ':' + mqtt_password + '@' + mqtt_server);
|
|
|
|
var M = new mastodon({
|
|
|
|
access_token: `${mastodon_token}`,
|
|
|
|
timeout_ms: 60*1000,
|
2022-03-31 13:53:16 -04:00
|
|
|
api_url: `${mastodon_api_url}`,
|
2021-06-20 09:54:50 -04:00
|
|
|
})
|
2022-03-27 19:24:35 -04:00
|
|
|
|
|
|
|
// thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees
|
|
|
|
function getDirection(angle) {
|
|
|
|
var directions = ['North', 'North-East', 'East', 'South-East', 'South', 'South-West', 'West', 'North-West'];
|
|
|
|
var index = Math.round(((angle %= 360) < 0 ? angle + 360 : angle) / 45) % 8;
|
|
|
|
return directions[index];
|
|
|
|
}
|
|
|
|
|
2021-06-20 09:54:50 -04:00
|
|
|
// Once we're online, we'll send a message alerting admins
|
|
|
|
xmpp.on('online', function(data, to) {
|
2022-03-31 13:53:16 -04:00
|
|
|
console.log('Connected with JID: ' + bot_jid);
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(user_jid, "[ONLINE]", false);
|
|
|
|
client.publish(mqtt_topic, mqtt_message);
|
|
|
|
M.post('statuses', { status: `${mastodon_toot}` });
|
|
|
|
});
|
|
|
|
|
|
|
|
xmpp.on('chat', function(from, message) {
|
2022-03-27 19:24:35 -04:00
|
|
|
function debug(){
|
|
|
|
if (debugging === '1'){
|
2022-03-31 13:53:16 -04:00
|
|
|
console.log('-------------');
|
2022-03-27 19:24:35 -04:00
|
|
|
console.log(message);
|
2022-03-31 13:53:16 -04:00
|
|
|
console.log(from);
|
|
|
|
console.log('-------------');
|
|
|
|
};
|
2022-03-27 19:24:35 -04:00
|
|
|
}
|
2022-03-31 13:53:16 -04:00
|
|
|
const friends = Object.keys(config.friends);
|
|
|
|
let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
|
|
|
|
let gotifyEUrl = encodeURI(gotifyURL);
|
2021-06-20 09:54:50 -04:00
|
|
|
// HELP
|
2022-03-27 19:24:35 -04:00
|
|
|
if (message === '!help') {
|
2021-12-07 16:17:31 -05:00
|
|
|
xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands');
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UNLOCKS
|
2022-03-27 19:24:35 -04:00
|
|
|
else if (message === '!unlocks') {
|
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2021-06-20 09:54:50 -04:00
|
|
|
var d = new Date();
|
|
|
|
var weekday = new Array(7);
|
|
|
|
weekday[0] = "Sunday";
|
|
|
|
weekday[1] = "Monday";
|
|
|
|
weekday[2] = "Tuesday";
|
|
|
|
weekday[3] = "Wednesday";
|
|
|
|
weekday[4] = "Thursday";
|
|
|
|
weekday[5] = "Friday";
|
|
|
|
weekday[6] = "Saturday";
|
|
|
|
var n = weekday[d.getDay()];
|
|
|
|
|
2022-03-31 13:53:16 -04:00
|
|
|
request(unlocks_api_url + "?columns=" + n, (error, res, body) => {
|
2021-06-20 09:54:50 -04:00
|
|
|
if (error) {
|
2021-12-07 16:43:46 -05:00
|
|
|
return console.log(error)
|
2021-06-20 09:54:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
2022-03-31 13:53:16 -04:00
|
|
|
var unlockBody = new RegExp(':(.*)}');
|
|
|
|
var unlockBodyMatch = body.match(unlockBody);
|
|
|
|
const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n;
|
|
|
|
xmpp.send(from, unlockFinalMessage);
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('Invalid credentials');
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('Record not found');
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('Server error.');
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
|
|
|
}}
|
2022-01-27 10:48:21 -05:00
|
|
|
// Tuya light control
|
2022-03-27 19:24:35 -04:00
|
|
|
else if (message === '!lights') {
|
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2022-01-27 10:48:21 -05:00
|
|
|
request(tuya_status_url,(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return console.log(error)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
if (res.body === "true"){
|
|
|
|
const stat = tuya_name + ": On";
|
|
|
|
xmpp.send(from, stat);
|
|
|
|
console.log(stat);
|
|
|
|
} else if (res.body === "false") {
|
|
|
|
const stat = tuya_name + ": Off";
|
|
|
|
xmpp.send(from, stat);
|
|
|
|
console.log(stat);
|
|
|
|
} else {
|
2022-03-31 13:53:16 -04:00
|
|
|
xmpp.send(from, "Unknown reply from api call. Please reply ping to reach an admin.");
|
2022-01-27 10:48:21 -05:00
|
|
|
console.log("Tuya API: Unknown reply!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
|
|
|
console.log('Invalid credentials');
|
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
|
|
|
console.log('Record not found');
|
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
|
|
|
console.log('Server error.');
|
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
|
|
|
}}
|
2022-01-27 10:48:21 -05:00
|
|
|
else if (message === tuya_name + ' on') {
|
2022-03-27 19:24:35 -04:00
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2022-01-27 10:48:21 -05:00
|
|
|
request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return console.log(error)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
const stat = tuya_name + " has been turned on";
|
|
|
|
xmpp.send(from, stat);
|
|
|
|
console.log(stat);
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
|
|
|
console.log('Invalid credentials');
|
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
|
|
|
console.log('Record not found');
|
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
|
|
|
console.log('Server error.');
|
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
|
|
|
}}
|
2022-01-27 10:48:21 -05:00
|
|
|
else if (message === tuya_name + ' off') {
|
2022-03-27 19:24:35 -04:00
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2022-01-27 10:48:21 -05:00
|
|
|
request(tuya_toggle_url + "?stat=false&devname=" + tuya_name,(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return console.log(error)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
const stat = tuya_name + " has been turned off";
|
|
|
|
xmpp.send(from, stat);
|
|
|
|
console.log(stat);
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
|
|
|
console.log('Invalid credentials');
|
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
|
|
|
console.log('Record not found');
|
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
|
|
|
console.log('Server error.');
|
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
|
|
|
}}
|
|
|
|
else if (message.includes(tuya_name + ' brightness')) {
|
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2022-01-29 22:40:21 -05:00
|
|
|
var per=message.substring(message.indexOf("brightness") + 10);
|
|
|
|
var perc=(per*10)
|
|
|
|
request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name,(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return console.log(error)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
xmpp.send(from, tuya_name + "set to " + per + "%");
|
|
|
|
console.log(tuya_name + "set to " + per + "%");
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
|
|
|
console.log('Invalid credentials');
|
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
|
|
|
console.log('Record not found');
|
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
|
|
|
console.log('Server error.');
|
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
2022-03-27 19:24:35 -04:00
|
|
|
if (config.debug === '1'){
|
|
|
|
console.log('lights turned to ' + per + "%");}
|
|
|
|
}}
|
|
|
|
else if (message.includes(tuya_name + ' color')) {
|
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2022-01-30 13:55:13 -05:00
|
|
|
request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => {})
|
|
|
|
var color=message.substring(message.indexOf("color") + 6);
|
|
|
|
var colorRgb=rgbcolor(color);
|
|
|
|
console.log(color)
|
|
|
|
if (colorRgb.isValid()) {
|
|
|
|
var obj = colorRgb.channels();
|
|
|
|
console.log(obj.r + ', ' + obj.g + ', ' + obj.b);
|
|
|
|
}
|
|
|
|
request(tuya_color_url + "?r=" + colorRgb.r + "&g=" + colorRgb.g + "&b=" + colorRgb.b + "&devname=" + tuya_name,(error, res, body) => {
|
|
|
|
if (error) {
|
|
|
|
return console.log(error)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
|
|
xmpp.send(from, tuya_name + "set to " + color);
|
|
|
|
console.log(tuya_name + "set to " + color);
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 401) {
|
|
|
|
console.log('Invalid credentials');
|
|
|
|
xmpp.send(from, 'Invalid credentials');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 404) {
|
|
|
|
console.log('Record not found');
|
|
|
|
xmpp.send(from, 'Record not found');
|
|
|
|
}
|
|
|
|
if (!error && res.statusCode == 500) {
|
|
|
|
console.log('Server error.');
|
|
|
|
xmpp.send(from, 'Server error. Please ping admin');
|
|
|
|
}
|
|
|
|
});
|
2022-03-27 19:24:35 -04:00
|
|
|
if (config.debug === '1'){
|
|
|
|
console.log(tuya_name + "set to " + color);}
|
|
|
|
}}
|
2022-01-30 13:55:13 -05:00
|
|
|
// Ping a user to let them know you would like to communicate
|
2022-03-27 19:24:35 -04:00
|
|
|
else if (message === '!ping') {
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, "Pinging " + user_name + "! Please stand by.");
|
|
|
|
xmpp.send(user_jid, from + " has pinged you!");
|
|
|
|
client.publish(mqtt_topic, from + ' has pinged you!');
|
2022-03-31 13:53:16 -04:00
|
|
|
request.post(gotifyEUrl, function (err, response, body) {
|
2022-03-27 19:24:35 -04:00
|
|
|
if(err){
|
|
|
|
console.log('error:', err);
|
|
|
|
}
|
|
|
|
});
|
2021-06-20 09:54:50 -04:00
|
|
|
ami.action({
|
|
|
|
'action':'originate',
|
|
|
|
'channel':'SIP/' + asterisk_callfrom,
|
|
|
|
'context': asterisk_context,
|
|
|
|
'callerID' : asterisk_callerID,
|
|
|
|
'data': 'googletts.agi, \"hello. ' + from + ' has pinged you!\"',
|
|
|
|
'application': 'agi'
|
|
|
|
});
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Check the weather using weatherunderground API
|
2022-03-27 19:24:35 -04:00
|
|
|
else if (message === '!weather') {
|
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, "Checking weather. Please hold...\r\n \r\n");
|
|
|
|
request(weather_url, function (err, response, body) {
|
|
|
|
if(err){
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log('error:', err);
|
2021-06-20 09:54:50 -04:00
|
|
|
} else {
|
|
|
|
let w = JSON.parse(body);
|
2022-03-31 13:53:16 -04:00
|
|
|
let wm = weather_city_name + " Weather: \r\n Tempature: " + w.main.temp + " degrees. \r\n Conditions: " + w.weather[0].main + "\r\n Pressure: " +
|
2021-12-07 16:43:46 -05:00
|
|
|
w.main.pressure + " \r\n Wind Direction: " + getDirection(w.wind.deg) + "\r\n Wind Speed: " + w.wind.speed + " m.p.h. \r\n";
|
2022-03-31 13:53:16 -04:00
|
|
|
let wmc = weather_city_name + " Weather: Tempature: " + w.main.temp + " degrees. Conditions: " + w.weather[0].main + ". Pressure: " +
|
2021-12-07 16:43:46 -05:00
|
|
|
w.main.pressure + ". Wind Direction: " + getDirection(w.wind.deg) + ". Wind Speed: " + w.wind.speed + " miles per hour \r\n";
|
2021-06-20 09:54:50 -04:00
|
|
|
client.publish('weather/temp', 'w.main.temp');
|
|
|
|
client.publish('weather/conditions', 'w.weather[0].main');
|
|
|
|
client.publish('weather/pressure', 'w.main.pressure');
|
2021-12-07 16:43:46 -05:00
|
|
|
client.publish('weather/wind/direction', 'getDirection(w.wind.deg)');
|
2021-06-20 09:54:50 -04:00
|
|
|
client.publish('weather/wind/speed', 'w.wind.speed');
|
|
|
|
xmpp.send(from, wm);
|
|
|
|
/*
|
|
|
|
ami.action({
|
|
|
|
'action':'originate',
|
|
|
|
'channel':'SIP/' + asterisk_callfrom,
|
|
|
|
'context': asterisk_context,
|
|
|
|
'callerID' : asterisk_callerID,
|
|
|
|
'data': 'googletts.agi,' + wmc,
|
|
|
|
'application': 'agi'
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
});
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
|
|
|
}}
|
2021-06-20 09:54:50 -04:00
|
|
|
|
2021-12-07 16:43:46 -05:00
|
|
|
// Sends a nice goodbye message
|
2022-03-27 19:24:35 -04:00
|
|
|
else if (message === '!bye'){
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, "Bye, have a beautiful time!");
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
|
|
|
}
|
2022-03-31 13:53:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
// Share-A-Command (#13?)
|
|
|
|
else if (message === '!meet'){
|
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
|
|
|
const createRoom = {
|
|
|
|
method: 'POST',
|
|
|
|
url: xmpp_api_url + '/create_room_with_opts',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: xmpp_auth
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
name: xmpp_muc_name,
|
|
|
|
service: xmpp_muc_server,
|
|
|
|
host: bot_hostname,
|
|
|
|
options: {
|
|
|
|
allow_change_subj: 'false',
|
|
|
|
allow_private_messages: 'false',
|
|
|
|
allow_private_messages_from_visitors: 'false',
|
|
|
|
allow_visitor_nickchange: 'false',
|
|
|
|
allow_visitor_status: 'false',
|
|
|
|
lang: 'en',
|
|
|
|
mam: 'true',
|
|
|
|
members_only: 'true',
|
|
|
|
persistent: 'true',
|
|
|
|
public: 'false',
|
|
|
|
public_list: 'false',
|
|
|
|
title: 'Secure channel generated by https://vxmppb.tk',
|
|
|
|
allow_subscription: 'true'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
request(createRoom, function (error, response, body) {
|
|
|
|
if (error) throw new Error(error);
|
|
|
|
console.log(body);
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
for (const key in config.friends) {
|
|
|
|
xmpp.send(key, "Meeting started: " + xmpp_muc_name + "@" + xmpp_muc_server);
|
|
|
|
const addMember = {
|
|
|
|
method: 'POST',
|
|
|
|
url: xmpp_api_url + 'set_room_affiliation',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: xmpp_auth
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
name: xmpp_muc_name,
|
|
|
|
service: xmpp_muc_server,
|
|
|
|
jid: key,
|
|
|
|
affiliation: 'member'
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
const inviteMember = {
|
|
|
|
method: 'POST',
|
|
|
|
url: xmpp_api_url + '/send_direct_invitation',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: xmpp_auth
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
name: xmpp_muc_name,
|
|
|
|
service: xmpp_muc_server,
|
|
|
|
password: '',
|
|
|
|
reason: 'vXMPPb Meeting Invite',
|
|
|
|
users: key
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
request(addMember, function (error, response, body) {
|
|
|
|
if (error) throw new Error(error);
|
|
|
|
console.log(body);
|
|
|
|
});
|
|
|
|
request(inviteMember, function (error, response, body) {
|
|
|
|
if (error) throw new Error(error);
|
|
|
|
console.log(body);
|
|
|
|
});
|
|
|
|
}}, 2000);
|
|
|
|
///////////////////////////////////////
|
|
|
|
debug();
|
|
|
|
}}
|
|
|
|
|
|
|
|
// End meeting
|
|
|
|
else if (message === '!endmeet'){
|
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
|
|
|
const endMeeting = {
|
|
|
|
method: 'POST',
|
|
|
|
url: xmpp_api_url + '/destroy_room',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: xmpp_auth
|
|
|
|
},
|
|
|
|
body: {name: xmpp_muc_name, service: xmpp_muc_server},
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
request(endMeeting, function (error, response, body) {
|
|
|
|
if (error) throw new Error(error);
|
|
|
|
console.log(body);
|
|
|
|
});
|
|
|
|
xmpp.send(from, "Meeting ended");
|
|
|
|
for (const key in config.friends) {
|
|
|
|
xmpp.send(key, "Meeting ended");
|
|
|
|
}
|
|
|
|
}debug();}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Shutsdown bot if admin sends shutdown command (linux)
|
2022-03-27 19:24:35 -04:00
|
|
|
else if (message === "sudo shutdown now -h"){
|
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.send(from, "Alrighty, shutting down. Goodbye!");
|
2022-03-27 19:24:35 -04:00
|
|
|
debug();
|
2021-12-07 16:43:46 -05:00
|
|
|
process.exit(1);
|
2022-03-27 19:24:35 -04:00
|
|
|
}}
|
2021-06-20 09:54:50 -04:00
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Get users vCard (WIP)
|
2022-03-31 13:53:16 -04:00
|
|
|
else if (message === '!vcard'){
|
2022-03-27 19:24:35 -04:00
|
|
|
if (from != user_jid){
|
|
|
|
xmpp.send(from, pc);
|
|
|
|
} else {
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.getVCard(from, function (vcard) {
|
|
|
|
xmpp.send(from, vcard);
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log(vcard)
|
2021-06-20 09:54:50 -04:00
|
|
|
})
|
2022-03-27 19:24:35 -04:00
|
|
|
}}
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// If the user sends something we don't have logic for, send them info about the project
|
2022-01-27 10:48:21 -05:00
|
|
|
else {
|
2022-03-27 19:24:35 -04:00
|
|
|
xmpp.send(from, "This is an auto replying bot. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands");
|
|
|
|
debug();
|
2021-06-20 09:54:50 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Catch and log XMPP errors
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.on('error', function(err) {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
2022-01-27 20:27:43 -05:00
|
|
|
// Set XMPP status
|
|
|
|
xmpp.setPresence('chat', 'Asterisk,Mastodon, and MQTT. Oh my!');
|
2021-06-20 09:54:50 -04:00
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Alas, we connect to XMPP!
|
2021-06-20 09:54:50 -04:00
|
|
|
xmpp.connect({
|
|
|
|
jid: bot_jid,
|
|
|
|
password: bot_password,
|
|
|
|
host: bot_hostname,
|
2021-12-07 16:17:31 -05:00
|
|
|
port: bot_port
|
2021-06-20 09:54:50 -04:00
|
|
|
});
|
|
|
|
|
2021-12-07 16:17:31 -05:00
|
|
|
// Checks the XMPP server for new 'friend' requests
|
2021-12-07 16:43:46 -05:00
|
|
|
console.log(xmpp.getRoster());
|
2022-03-31 13:53:16 -04:00
|
|
|
|
|
|
|
|
2022-03-27 19:24:35 -04:00
|
|
|
} catch(e) {
|
|
|
|
console.log("Please create a config.ini");
|
|
|
|
console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start");
|
|
|
|
console.log("and choose your prefered install method.")
|
|
|
|
}
|