diff --git a/config.ini.dist b/config.ini.dist index 9299023..bcd19b4 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -1,39 +1,68 @@ -api_pass = +debug = + +[asterisk] +callfrom = +callto = +callerID = +context = +host = +password = +port = +user = + +[bot] +hostname = +jid = +password = +port = + +[gotify] +key = +message = +priority = +title = api_url = -api_user = -asterisk_callfrom = -asterisk_callto = -asterisk_callerID = -asterisk_context = -asterisk_host = -asterisk_password = -asterisk_port = -asterisk_user = -bot_hostname = -bot_jid = -bot_password = + +[mastodon] +token = +toot = +api_url = + +[mqtt] +message = +password = +server = +topic = +user = + +[tuya] +name = +bright_url = +color_url = +status_url = +toggle_url = + +[unlocks] +api_url = +pass = +user = + +[user] +jid = +name = +server = + +[weather] city_code = city_name = -debug = -gotify_api_key = -gotify_message = -gotify_priority = -gotify_title = -gotify_url = -mastodon_token = -mastodon_toot = -mastodon_url = -mqtt_message = -mqtt_password = -mqtt_server = -mqtt_topic = -mqtt_user = -tuya_name = -tuya_bright_url = -tuya_color_url = -tuya_status_url = -tuya_toggle_url = -user_jid = -user_name = -weather_api_key = -weather_units = +key = +units = + +[xmpp] +api_url = +key = +muc_name = +muc_password = +muc_server = + +[friends] diff --git a/index.js b/index.js index 1ac337a..b709536 100755 --- a/index.js +++ b/index.js @@ -12,56 +12,80 @@ try { const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); const debugging = config.debug; -const api_pass = config.api_pass; -const api_url = config.api_url; -const api_user = config.api_user; -const app_port = config.app_port; -const asterisk_callfrom = config.asterisk_callfrom; -const asterisk_callto = config.asterisk_callerto; -const asterisk_callerID = config.asterisk_callerID; -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; -const bot_hostname = config.bot_hostname; -const bot_jid = config.bot_jid; -const bot_password = config.bot_password; -const bot_port = config.bot_port; -const city_code = config.city_code; -const city_name = config.city_name; -const gotify_api_key = config.gotify_api_key; -const gotify_message = config.gotify_message; -const gotify_priority = config.gotify_priority; -const gotify_title = config.gotify_title; -const gotify_url = config.gotify_url; -const mastodon_token = config.mastodon_token; -const mastodon_toot = config.mastodon_toot; -const mastodon_url = config.mastodon_url; -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; -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; -const user_jid = config.user_jid //.split('/')[0]; -const user_name = config.user_name; -const weather_api_key = config.weather_api_key; -const weather_units = config.weather_units; +// 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; let pc = 'This is a private command. Please see https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start for your own instance!'; -let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${city_code}&appid=${weather_api_key}&units=${weather_units}` +let weather_url = `https://api.openweathermap.org/data/2.5/weather?id=${weather_city_code}&appid=${weather_key}&units=${weather_units}` 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, - api_url: `${mastodon_url}`, + api_url: `${mastodon_api_url}`, }) // thx: https://stackoverflow.com/questions/48750528/get-direction-from-degrees @@ -73,7 +97,7 @@ function getDirection(angle) { // Once we're online, we'll send a message alerting admins xmpp.on('online', function(data, to) { - console.log('Connected with JID: ' + data.jid.user); + console.log('Connected with JID: ' + bot_jid); xmpp.send(user_jid, "[ONLINE]", false); client.publish(mqtt_topic, mqtt_message); M.post('statuses', { status: `${mastodon_toot}` }); @@ -82,12 +106,15 @@ xmpp.on('online', function(data, to) { xmpp.on('chat', function(from, message) { function debug(){ if (debugging === '1'){ + console.log('-------------'); console.log(message); - }; + console.log(from); + console.log('-------------'); + }; } -let gurl = `${gotify_url}/message?token=${gotify_api_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`; -let gurle = encodeURI(gurl); - +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); // HELP if (message === '!help') { xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands'); @@ -110,16 +137,16 @@ let gurle = encodeURI(gurl); weekday[6] = "Saturday"; var n = weekday[d.getDay()]; - request(api_url + "?columns=" + n, (error, res, body) => { + request(unlocks_api_url + "?columns=" + n, (error, res, body) => { if (error) { return console.log(error) }; if (!error && res.statusCode == 200) { - var ulb = new RegExp(':(.*)}'); - var ule = body.match(ulb); - const ur = user_name + " has " + ule[1] + " unlocks for " + n; - xmpp.send(from, ur); + var unlockBody = new RegExp(':(.*)}'); + var unlockBodyMatch = body.match(unlockBody); + const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n; + xmpp.send(from, unlockFinalMessage); debug(); } if (!error && res.statusCode == 401) { @@ -157,7 +184,7 @@ let gurle = encodeURI(gurl); xmpp.send(from, stat); console.log(stat); } else { - xmpp.send(from, "Unknown reply. Please reply ping to reach an admin."); + xmpp.send(from, "Unknown reply from api call. Please reply ping to reach an admin."); console.log("Tuya API: Unknown reply!"); } } @@ -307,7 +334,7 @@ let gurle = encodeURI(gurl); 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!'); - request.post(gurle, function (err, response, body) { + request.post(gotifyEUrl, function (err, response, body) { if(err){ console.log('error:', err); } @@ -334,9 +361,9 @@ let gurle = encodeURI(gurl); console.log('error:', err); } else { let w = JSON.parse(body); - let wm = city_name + " Weather: \r\n Tempature: " + w.main.temp + " degrees. \r\n Conditions: " + w.weather[0].main + "\r\n Pressure: " + + let wm = weather_city_name + " Weather: \r\n Tempature: " + w.main.temp + " degrees. \r\n Conditions: " + w.weather[0].main + "\r\n Pressure: " + w.main.pressure + " \r\n Wind Direction: " + getDirection(w.wind.deg) + "\r\n Wind Speed: " + w.wind.speed + " m.p.h. \r\n"; - let wmc = 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"; client.publish('weather/temp', 'w.main.temp'); client.publish('weather/conditions', 'w.weather[0].main'); @@ -364,7 +391,121 @@ let gurle = encodeURI(gurl); xmpp.send(from, "Bye, have a beautiful time!"); debug(); } - // Shutsdown bot if admin messages "goodbye" + + + // 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) else if (message === "sudo shutdown now -h"){ if (from != user_jid){ xmpp.send(from, pc); @@ -375,7 +516,7 @@ let gurle = encodeURI(gurl); }} // Get users vCard (WIP) - else if (message === 'vcard' && from === user_jid){ + else if (message === '!vcard'){ if (from != user_jid){ xmpp.send(from, pc); } else { @@ -410,6 +551,8 @@ xmpp.connect({ // Checks the XMPP server for new 'friend' requests console.log(xmpp.getRoster()); + + } catch(e) { console.log("Please create a config.ini"); console.log("See https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:install:start");