Commenting code for index.js, adding os independent locations

This commit is contained in:
Stephen Vanderwarker 2025-01-25 17:15:12 -05:00
parent 7c58616ea1
commit 435200ccb3

@ -2,10 +2,14 @@
const fs = require('fs');
const ini = require('ini');
const nostr = require('nostr-tools');
const os = require('os');
const path = require('path');
const wspf = require('websocket-polyfill');
const WebSocket = require('ws');
const xmpp = require('simple-xmpp');
// nostr things
let {
bech32,
bech32m
@ -13,20 +17,47 @@ let {
let letsCID;
let signedEvent;
if (fs.existsSync('./config.ini')) {
// config setup for home folders
// Determine the platform-specific config path
function getConfigPath() {
const platform = os.platform();
if (platform === 'win32') {
// Windows
return path.join(process.env.APPDATA, 'nostrsms', 'config.ini');
} else if (platform === 'darwin') {
// macOS
return path.join(os.homedir(), 'Library', 'Application Support', 'nostrsms', 'config.ini');
} else {
// Linux and other Unix-like systems
return path.join(os.homedir(), '.config', 'nostrsms', 'config.ini');
}
}
// prepare config for parsing
const configPath = getConfigPath();
if (fs.existsSync(configPath)) {
console.log(`Found config at ${configPath}`);
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
// get users nostr private hex
let sk = config.nostr.pkhex;
// calculate pubkey from private
let pk = nostr.getPublicKey(sk);
// only allow users from config.ini -> [authorized_users] -> users
const authorizedUsers = config.authorized_users.users
? config.authorized_users.users.split(',').map(user => user.trim())
: [];
// We've got a connection to XMPP!
xmpp.on('online', function (data, to) {
console.log('Connected with JID: ' + config.xmpp.jid);
console.log('Pubkey: ' + pk);
});
// We've got an XMPP message!
xmpp.on('chat', function (from, message) {
// setup debugging based on config.ini
function debug() {
if (config.debug === '1') {
console.log('[DEBUG]');
@ -34,48 +65,70 @@ if (fs.existsSync('./config.ini')) {
console.log(from);
};
}
// deny users not in config.ini
if (!authorizedUsers.includes(from)) {
xmpp.send(from, 'Not authorized');
debug();
return; // Exit the function early
}
// get the latest documentation for nostrsms
if (message === '!help') {
xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:commands');
debug();
// reauth (I am not sure this is possible so...)
} else if (message === "!auth") {
xmpp.send(from, "https://auth.nostrsms.com");
// Well they didn't use a command, so let's get to the NOSTR STUFF!!1!
} else {
async function newPost() {
// get relays from config.ini
const relay = nostr.relayInit(config.relays.write, WebSocket);
relay.on('connect', () => {
// look at that, already on the nostr
relay.on('connect', () => {
console.log(`connected to ${relay.url}`);
});
// dang...failed connection! Try again sooon, or try one of the thousands of other relays ;)
relay.on('error', () => {
console.log(`failed to connect to ${relay.url}`);
});
// wait for the above to give us the go ahead
await relay.connect();
// let's construct our "note"!
let event = {
// https://git.vanderwarker.family/nostr/nips/src/branch/master/01.md
kind: 1,
// get users public key hex
pubkey: pk,
// create this instant! (timestamp)
created_at: Math.floor(Date.now() / 1000),
// add client tag for UIs to optionally display
tags: [['client','NostrSMS','31990:9be1b8315248eeb20f9d9ab2717d1750e4f27489eab1fa531d679dadd34c2f8d:1726937516']],
// set the nostr content to the incoming xmpp message
content: message,
};
// sign nostr event
const signedEvent = nostr.finishEvent(event, sk);
// get nostr event it
letsCID = signedEvent.id;
showSID(letsCID, pk);
// publish!
await relay.publish(signedEvent);
// done here. we can disconnect! (from nostr relay)
relay.close();
}
// catch errors
newPost().catch((error) => {
console.error(error);
});
// get's id, pubkey, kind, from signed event, and relay from config.ini and create
// a nip-19 encoded entity to send back to the user
// this can be pasted into most nostr clients for immediate fetching/viewing
async function showSID(letsCID, pk) {
console.log(letsCID);
console.log(pk);
@ -92,23 +145,34 @@ if (fs.existsSync('./config.ini')) {
}
}
});
// xmpp seems to have an error, so we show it in the console
xmpp.on('error', function (err) {
console.error(err);
});
// set xmpp presence to nostrsms url (free advertisement! :P)
xmpp.setPresence('chat', 'https://nostrsms.com');
// connect to xmpp server using info from config.ini
xmpp.connect({
jid: config.xmpp.jid,
password: config.xmpp.password,
host: config.xmpp.host,
port: 5222
});
// catch ctrl+c
// TODO: add confirmation ??
process.on('SIGINT', function () {
console.log("SigInt receieved. Shutdown inevitable!")
process.exit();
});
// error out and show install instructions if config.ini isn't found.
} else {
console.log("=_-_+_-_=_-_+_-_=_-_+_-_=_-_+_-_=_-_+_-_=");
console.error("Config file not found at ${configPath}");
console.log("=_-_+_-_=_-_+_-_=_-_+_-_=_-_+_-_=_-_+_-_=");
console.log("");
console.log("Please create a config.ini");
console.log("See https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:install");
console.log("and choose your preferred install method.");
process.exit(1);
}