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
- Version:
- Node JS Version:
- Platform:
- Subsystem:
- vXMPPb Version:

113
index.js
View File

@ -44,6 +44,7 @@ try {
const mastodon_api_url = config.mastodon.api_url;
const mastodon_token = config.mastodon.token;
const mastodon_toot = config.mastodon.toot;
const mastodon_user = config.mastodon.user;
// MQTT config
const mqtt_message = config.mqtt.message;
@ -106,16 +107,22 @@ try {
return directions[index];
}
// setup friends list
const friends = Object.keys(config.friends);
xmpp.on('online', function(data, to) {
// once we're online, we'll send a message alerting xmpp admins
console.log('Connected with JID: ' + bot_jid);
xmpp.send(user_jid, "[ONLINE]", false);
// send mqtt online message
client.publish(mqtt_topic, mqtt_message);
// toot online message
// toot online message
/*
M.post('statuses', {
status: `${mastodon_toot}`
});
*/
});
// wait for incoming messages
xmpp.on('chat', function(from, message) {
@ -128,8 +135,6 @@ try {
console.log('-------------');
};
}
// setup friends list
const friends = Object.keys(config.friends);
// setup gotify url
let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
let gotifyEUrl = encodeURI(gotifyURL);
@ -183,6 +188,53 @@ try {
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
else if (message === '!lights') {
if (from != user_jid) {
@ -218,7 +270,7 @@ try {
}
if (!error && res.statusCode == 500) {
console.log('Server error.');
xmpp.send(from, 'Server error. Please ping admin');
xmpp.send(from, 'Server error. Please !ping');
}
});
debug();
@ -398,11 +450,11 @@ try {
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";
// send weather to mqtt
client.publish('weather/temp', 'w.main.temp');
client.publish('weather/conditions', 'w.weather[0].main');
client.publish('weather/pressure', 'w.main.pressure');
client.publish('weather/wind/direction', 'getDirection(w.wind.deg)');
client.publish('weather/wind/speed', 'w.wind.speed');
client.publish('weather/temp', '' + w.main.temp + '');
client.publish('weather/conditions', '' + w.weather[0].main + '');
client.publish('weather/pressure', '' + w.main.pressure + '');
client.publish('weather/wind/direction', '' + getDirection(w.wind.deg) + '');
client.publish('weather/wind/speed', '' + w.wind.speed + '');
// send weather to xmpp admin
xmpp.send(from, wm);
// 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') {
if (from != user_jid) {
xmpp.send(from, pc);
@ -455,7 +512,7 @@ try {
lang: 'en',
mam: 'true',
members_only: 'true',
persistent: 'true',
persistent: 'false',
public: 'false',
public_list: 'false',
title: 'Secure channel generated by https://vxmppb.tk',
@ -474,7 +531,7 @@ try {
// loop through friends, authorize users, and send invites
for (const key in config.friends) {
// 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
const addMember = {
method: 'POST',
@ -546,22 +603,19 @@ try {
if (error) throw new Error(error);
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) {
// alert friends meeting has ended
xmpp.send(key, "Meeting ended");
}
}
debug();
}
// Shutsdown bot if admin sends shutdown command (linux)
else if (message === "sudo shutdown now -h") {
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 {
// send shutdown message
xmpp.send(from, "Alrighty, shutting down. Goodbye!");
@ -597,7 +651,7 @@ try {
});
// Set XMPP status
xmpp.setPresence('chat', 'Asterisk,Mastodon, and MQTT. Oh my!');
xmpp.setPresence('chat', 'https://vxmppb.tk');
// Alas, we connect to XMPP!
xmpp.connect({
@ -606,13 +660,24 @@ try {
host: bot_hostname,
port: bot_port
});
// Checks the XMPP server for new 'friend' requests
console.log(xmpp.getRoster());
// subscribe to friends
xmpp.on('subscribe', function(from) {
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) {
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.")
console.log("and choose your prefered install method.");
}

View File

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