Merge pull request 'main' (#21) from stephen/vxmppb:main into main

Reviewed-on: #21
This commit is contained in:
Stephen Vanderwarker 2022-04-24 08:06:20 -04:00
commit b5258b7e30
3 changed files with 92 additions and 27 deletions

View File

@ -11,6 +11,6 @@
## Specifications ## Specifications
- Version: - Node JS Version:
- Platform: - Platform:
- Subsystem: - vXMPPb Version:

113
index.js
View File

@ -44,6 +44,7 @@ try {
const mastodon_api_url = config.mastodon.api_url; const mastodon_api_url = config.mastodon.api_url;
const mastodon_token = config.mastodon.token; const mastodon_token = config.mastodon.token;
const mastodon_toot = config.mastodon.toot; const mastodon_toot = config.mastodon.toot;
const mastodon_user = config.mastodon.user;
// MQTT config // MQTT config
const mqtt_message = config.mqtt.message; const mqtt_message = config.mqtt.message;
@ -106,16 +107,22 @@ try {
return directions[index]; return directions[index];
} }
// setup friends list
const friends = Object.keys(config.friends);
xmpp.on('online', function(data, to) { xmpp.on('online', function(data, to) {
// once we're online, we'll send a message alerting xmpp admins // once we're online, we'll send a message alerting xmpp admins
console.log('Connected with JID: ' + bot_jid); console.log('Connected with JID: ' + bot_jid);
xmpp.send(user_jid, "[ONLINE]", false); xmpp.send(user_jid, "[ONLINE]", false);
// send mqtt online message // send mqtt online message
client.publish(mqtt_topic, mqtt_message); client.publish(mqtt_topic, mqtt_message);
// toot online message // toot online message
/*
M.post('statuses', { M.post('statuses', {
status: `${mastodon_toot}` status: `${mastodon_toot}`
}); });
*/
}); });
// wait for incoming messages // wait for incoming messages
xmpp.on('chat', function(from, message) { xmpp.on('chat', function(from, message) {
@ -128,8 +135,6 @@ try {
console.log('-------------'); console.log('-------------');
}; };
} }
// setup friends list
const friends = Object.keys(config.friends);
// setup gotify url // setup gotify url
let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`; let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
let gotifyEUrl = encodeURI(gotifyURL); let gotifyEUrl = encodeURI(gotifyURL);
@ -183,6 +188,53 @@ try {
debug(); debug();
} }
} }
// Toot Unlocks
else if (message === '!tootunlocks') {
if (from != user_jid) {
xmpp.send(from, pc);
} else {
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()];
// get latest unlocks
request(unlocks_api_url + "?columns=" + n, (error, res, body) => {
if (error) {
return console.log(error)
};
if (!error && res.statusCode == 200) {
var unlockBody = new RegExp(':(.*)}');
var unlockBodyMatch = body.match(unlockBody);
const unlockFinalMessage = "@" + config.mastodon.user + " has " + unlockBodyMatch[1] + " unlocks for " + n;
M.post('statuses', {
status: unlockFinalMessage
});
debug();
}
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');
}
});
debug();
}
}
// Tuya light control // Tuya light control
else if (message === '!lights') { else if (message === '!lights') {
if (from != user_jid) { if (from != user_jid) {
@ -218,7 +270,7 @@ try {
} }
if (!error && res.statusCode == 500) { if (!error && res.statusCode == 500) {
console.log('Server error.'); console.log('Server error.');
xmpp.send(from, 'Server error. Please ping admin'); xmpp.send(from, 'Server error. Please !ping');
} }
}); });
debug(); debug();
@ -398,11 +450,11 @@ try {
let wmc = weather_city_name + " Weather: Tempature: " + w.main.temp + " degrees. Conditions: " + w.weather[0].main + ". Pressure: " + let wmc = weather_city_name + " Weather: Tempature: " + w.main.temp + " degrees. Conditions: " + w.weather[0].main + ". Pressure: " +
w.main.pressure + ". Wind Direction: " + getDirection(w.wind.deg) + ". Wind Speed: " + w.wind.speed + " miles per hour \r\n"; w.main.pressure + ". Wind Direction: " + getDirection(w.wind.deg) + ". Wind Speed: " + w.wind.speed + " miles per hour \r\n";
// send weather to mqtt // send weather to mqtt
client.publish('weather/temp', 'w.main.temp'); client.publish('weather/temp', '' + w.main.temp + '');
client.publish('weather/conditions', 'w.weather[0].main'); client.publish('weather/conditions', '' + w.weather[0].main + '');
client.publish('weather/pressure', 'w.main.pressure'); client.publish('weather/pressure', '' + w.main.pressure + '');
client.publish('weather/wind/direction', 'getDirection(w.wind.deg)'); client.publish('weather/wind/direction', '' + getDirection(w.wind.deg) + '');
client.publish('weather/wind/speed', 'w.wind.speed'); client.publish('weather/wind/speed', '' + w.wind.speed + '');
// send weather to xmpp admin // send weather to xmpp admin
xmpp.send(from, wm); xmpp.send(from, wm);
// call admin with latest weather report // call admin with latest weather report
@ -429,7 +481,12 @@ try {
} }
// Share-A-Command (#13?) // Share-A-Command which was #13, but kinda turned into a different command
// This will generate a private channel, and add the users in the friends
// secion from config.ini
// Please not this section requires you to have an Ejabberd server.
// I am welcome to pull requests for other servers (maybe set type in config.ini)
else if (message === '!meet') { else if (message === '!meet') {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, pc);
@ -455,7 +512,7 @@ try {
lang: 'en', lang: 'en',
mam: 'true', mam: 'true',
members_only: 'true', members_only: 'true',
persistent: 'true', persistent: 'false',
public: 'false', public: 'false',
public_list: 'false', public_list: 'false',
title: 'Secure channel generated by https://vxmppb.tk', title: 'Secure channel generated by https://vxmppb.tk',
@ -474,7 +531,7 @@ try {
// loop through friends, authorize users, and send invites // loop through friends, authorize users, and send invites
for (const key in config.friends) { for (const key in config.friends) {
// send xmpp message to friends alerting of new room // send xmpp message to friends alerting of new room
xmpp.send(key, "Meeting started: " + xmpp_muc_name + "@" + xmpp_muc_server); xmpp.send(key, "Meeting started");
// authorize users // authorize users
const addMember = { const addMember = {
method: 'POST', method: 'POST',
@ -546,22 +603,19 @@ try {
if (error) throw new Error(error); if (error) throw new Error(error);
console.log(body); console.log(body);
}); });
// send meeting ended to user_jid
xmpp.send(from, "Meeting ended");
// send meeting ended to friends
for (const key in config.friends) { for (const key in config.friends) {
// alert friends meeting has ended
xmpp.send(key, "Meeting ended"); xmpp.send(key, "Meeting ended");
} }
} }
debug(); debug();
} }
// Shutsdown bot if admin sends shutdown command (linux) // Shutsdown bot if admin sends shutdown command (linux)
else if (message === "sudo shutdown now -h") { else if (message === "sudo shutdown now -h") {
if (from != user_jid) { if (from != user_jid) {
xmpp.send(from, pc); xmpp.send(from, from + " is not in the sudoers file. This incident will be reported.");
console.log(from + ' tried to shut me down!');
} else { } else {
// send shutdown message // send shutdown message
xmpp.send(from, "Alrighty, shutting down. Goodbye!"); xmpp.send(from, "Alrighty, shutting down. Goodbye!");
@ -597,7 +651,7 @@ try {
}); });
// Set XMPP status // Set XMPP status
xmpp.setPresence('chat', 'Asterisk,Mastodon, and MQTT. Oh my!'); xmpp.setPresence('chat', 'https://vxmppb.tk');
// Alas, we connect to XMPP! // Alas, we connect to XMPP!
xmpp.connect({ xmpp.connect({
@ -606,13 +660,24 @@ try {
host: bot_hostname, host: bot_hostname,
port: bot_port port: bot_port
}); });
// subscribe to friends
// Checks the XMPP server for new 'friend' requests xmpp.on('subscribe', function(from) {
console.log(xmpp.getRoster()); if (from in config.friends == true) {
console.log('accepting subsciption request from ' + from);
// accept pending subscription requests
xmpp.acceptSubscription(from);
// check for incoming subscription requests
xmpp.getRoster();
// subscribe to any incoming subscription requests
console.log('requesting subscription to ' + from);
xmpp.subscribe(from);
} else {
console.log('denied subscription request from: ' + from + " as they are not in the friends section of config.ini");
}
});
} catch (e) { } catch (e) {
console.log("Please create a config.ini"); console.log("Please create a config.ini");
console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start"); console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start");
console.log("and choose your prefered install method.") console.log("and choose your prefered install method.");
} }

View File

@ -23,7 +23,7 @@
"asterisk-manager": "0.1.16", "asterisk-manager": "0.1.16",
"ini": "2.0.0", "ini": "2.0.0",
"mastodon": "1.2.2", "mastodon": "1.2.2",
"mqtt": "4.2.6", "mqtt": "4.3.7",
"request": "2.88.0", "request": "2.88.0",
"rgb-color": "2.1.2", "rgb-color": "2.1.2",
"simple-xmpp": "1.3.0" "simple-xmpp": "1.3.0"