From e96aca88e57c085c970584158384c1a6206cdea0 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 2 Nov 2023 14:06:30 -0400 Subject: [PATCH] First commit --- .gitea/ISSUE_TEMPLATE.md | 16 +++++ .gitea/PULL_REQUEST_TEMPLATE.md | 7 ++ .gitignore | 9 +++ LICENSE | 1 + README.md | 2 + config.ini.dist | 13 ++++ index.js | 118 ++++++++++++++++++++++++++++++++ package.json | 45 ++++++++++++ 8 files changed, 211 insertions(+) create mode 100644 .gitea/ISSUE_TEMPLATE.md create mode 100644 .gitea/PULL_REQUEST_TEMPLATE.md create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 config.ini.dist create mode 100644 index.js create mode 100644 package.json diff --git a/.gitea/ISSUE_TEMPLATE.md b/.gitea/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..8752467 --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE.md @@ -0,0 +1,16 @@ +## Expected Behavior + + +## Actual Behavior + + +## Steps to Reproduce the Problem +1. +2. +3. + +## Specifications + + - Node JS Version: + - Platform: + - NostrSMS Version: \ No newline at end of file diff --git a/.gitea/PULL_REQUEST_TEMPLATE.md b/.gitea/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..9c2911d --- /dev/null +++ b/.gitea/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,7 @@ +Fixes # + +## Proposed Changes + + - + - + - diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2953167 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +node_modules +package-lock.json +config.ini +build/ +.npmignore +npm-debug.log +.vs/ +*.db +test.js diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c7ffc1a --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +"THE BEER-WARE LICENSE" (Revision 42): wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp diff --git a/README.md b/README.md new file mode 100644 index 0000000..3f37676 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# NostrSMS +### That's right folks, we got SMS to Relay! diff --git a/config.ini.dist b/config.ini.dist new file mode 100644 index 0000000..f308ecd --- /dev/null +++ b/config.ini.dist @@ -0,0 +1,13 @@ +debug = + +[xmpp] +host = +jid = +password = + +[nostr] +pkhex = + +[relays] +read = +write = \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..1ecc189 --- /dev/null +++ b/index.js @@ -0,0 +1,118 @@ +// initialize required packages +const sqlite3 = require('sqlite3'); +const fs = require('fs'); +const ini = require('ini'); +const nostr = require('nostr-tools'); +const wspf = require('websocket-polyfill'); +const WebSocket = require('ws'); +const xmpp = require('simple-xmpp'); + +var db = new sqlite3.Database('delegations.db'); + +let { bech32, bech32m } = require('bech32'); +let letsCID; +let signedEvent; + +if (fs.existsSync('./config.ini')) { + const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); + let sk = config.nostr.pkhex; + let pk = nostr.getPublicKey(sk); + + xmpp.on('online', function (data, to) { + console.log('Connected with JID: ' + config.xmpp.jid); + }); + xmpp.on('chat', function (from, message) { + function debug() { + if (config.debug === '1') { + console.log('[DEBUG]'); + console.log(message); + console.log(from); + }; + } + if (message === '!help') { + xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:commands'); + debug(); + } + else if (message === "!auth"){ + xmpp.send(from,"reauth (nip-26)"); + + const now = new Date(); + const validUntil = now.getTime() + (30 * 24 * 60 * 60 * 1000); + const insertStmt = db.prepare('INSERT INTO users (pnum, token) VALUES (?, ?)'); + insertStmt.run(from, validUntil); + insertStmt.finalize(); + console.log('Data inserted successfully'); + db.close((err) => { + if (err) { + return console.error('Error closing the database:', err.message); + } + console.log('Database connection closed.'); + }); + debug(); + } + else { + async function main() { + const relay = nostr.relayInit(config.relays.write, WebSocket); + relay.on('connect', () => { + console.log(`connected to ${relay.url}`); + }); + relay.on('error', () => { + console.log(`failed to connect to ${relay.url}`); + }); + + await relay.connect(); + + let event = { + kind: 1, + pubkey: pk, + created_at: Math.floor(Date.now() / 1000), + tags: [], + content: message, + }; + + const signedEvent = nostr.finishEvent(event, sk); + letsCID = signedEvent.id; + showSID(letsCID,pk); + await relay.publish(signedEvent); + relay.close(); + } + + main().catch((error) => { + console.error(error); + }); + + async function showSID(letsCID,pk) { + console.log(letsCID); + console.log(pk); + const event = { + id: letsCID, + relays: [config.relays.write], + author: pk, + kind: 1, + }; + const encodedNEvent = nostr.nip19.neventEncode(event); + xmpp.send(from, "nostr:" + encodedNEvent); + debug(); + } + } + }); + xmpp.on('error', function (err) { + console.error(err); + }); + xmpp.setPresence('chat', 'https://nostrsms.com'); + xmpp.connect({ + jid: config.xmpp.jid, + password: config.xmpp.password, + host: config.xmpp.host, + port: 5222 + }); + process.on('SIGINT', function() { + console.log("SigInt receieved. Shutdown inevitable!") + process.exit(); + }); + +} else { + console.log("Please create a config.ini"); + console.log("See https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:install:start"); + console.log("and choose your preferred install method."); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..1c4375c --- /dev/null +++ b/package.json @@ -0,0 +1,45 @@ +{ + "name": "nostrsms", + "description": "A simple NodeJS bot that posts to Nostr from XMPP! 💜🫂", + "version": "0.0.1", + "keywords": [ + "xmpp", + "sms", + "nostr" + ], + "maintainers": [ + { + "name": "Stephen Vanderwarker", + "email": "stephen@vanderwarker.family", + "web": "https://stephen.vanderwarker.family" + } + ], + "dependencies": { + "@types/ws": "8.5.5", + "assert": "^2.1.0", + "bech32": "2.0.0", + "ini": "2.0.0", + "nostr-tools": "1.14.2", + "simple-xmpp": "1.3.0", + "sqlite3": "5.1.6", + "websocket-polyfill": "0.0.3", + "ws": "^8.14.0" + }, + "bugs": { + "url": "https://git.vanderwarker.family/nostr/nostrsms/issues" + }, + "repository": { + "type": "git", + "url": "https://git.vanderwarker.family/nostr/nostrsms.git" + }, + "licenses": [ + { + "type": "The MIT License", + "url": "http://www.opensource.org/licenses/mit-license.php" + } + ], + "homepage": "https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:start", + "scripts": { + "test": "echo \"No tests available. Please consider helping with a pull request!\" && exit 0" + } +}