Adding !fetch command. #3

Merged
stephen merged 1 commits from subscribe into main 2023-11-16 22:48:09 -05:00
Showing only changes of commit dfc3985f9d - Show all commits

265
index.js
View File

@ -1,111 +1,154 @@
// initialize required packages // initialize required packages
const sqlite3 = require('sqlite3'); const sqlite3 = require('sqlite3');
const fs = require('fs'); const fs = require('fs');
const ini = require('ini'); const ini = require('ini');
const nostr = require('nostr-tools'); const nostr = require('nostr-tools');
const wspf = require('websocket-polyfill'); const wspf = require('websocket-polyfill');
const WebSocket = require('ws'); const WebSocket = require('ws');
const xmpp = require('simple-xmpp'); const xmpp = require('simple-xmpp');
var db = new sqlite3.Database('nip26.db'); var db = new sqlite3.Database('nip26.db');
let { bech32, bech32m } = require('bech32'); let {
let letsCID; bech32,
let signedEvent; bech32m
} = require('bech32');
if (fs.existsSync('./config.ini')) { let letsCID;
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8')); let signedEvent;
let sk = config.nostr.pkhex;
let pk = nostr.getPublicKey(sk); if (fs.existsSync('./config.ini')) {
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
xmpp.on('online', function (data, to) { let sk = config.nostr.pkhex;
console.log('Connected with JID: ' + config.xmpp.jid); let pk = nostr.getPublicKey(sk);
});
xmpp.on('chat', function (from, message) { xmpp.on('online', function (data, to) {
function debug() { console.log('Connected with JID: ' + config.xmpp.jid);
if (config.debug === '1') { });
console.log('[DEBUG]'); xmpp.on('chat', function (from, message) {
console.log(message); function debug() {
console.log(from); if (config.debug === '1') {
}; console.log('[DEBUG]');
} console.log(message);
if (message === '!help') { console.log(from);
xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:commands'); };
debug(); }
} // Add NIP-05, to get users latest kind 3, and notes.
else if (message === "!auth"){ if (message === "!fetch") {
xmpp.send(from,"reauth (nip-26)"); xmpp.send(from, "Sure. Please standby...");
async function gogoGlobalShoes() {
const now = new Date(); const relay = nostr.relayInit(config.relays.read, WebSocket);
const validUntil = now.getTime() + (30 * 24 * 60 * 60 * 1000); relay.on('connect', () => {
const insertStmt = db.prepare('INSERT INTO users (pnum, pk) VALUES (?, ?)'); console.log(`connected to ${relay.url}`);
insertStmt.run(from.split("@")[0], validUntil); });
insertStmt.finalize(); relay.on('error', () => {
debug(); console.log(`failed to connect to ${relay.url}`);
} });
else {
async function main() { await relay.connect();
const relay = nostr.relayInit(config.relays.write, WebSocket);
relay.on('connect', () => { let sub = relay.sub([{
console.log(`connected to ${relay.url}`); kinds: [1],
}); authors: [pk],
relay.on('error', () => { limit: 5
console.log(`failed to connect to ${relay.url}`); }, ])
}); sub.on('event', event => {
console.log('got event:', event);
await relay.connect();
// Check if the event is already an object
let event = { const conny = typeof event === 'string' ? JSON.parse(event) : event;
kind: 1,
pubkey: pk, if (conny && conny.content && conny.pubkey && conny.created_at) {
created_at: Math.floor(Date.now() / 1000), const content = conny.content;
tags: [], const author = conny.pubkey;
content: message, const createdAt = conny.created_at;
};
// Use the extracted properties as needed
const signedEvent = nostr.finishEvent(event, sk); console.log('Content:', content);
letsCID = signedEvent.id; console.log('Author:', author);
showSID(letsCID,pk); console.log('Created At:', createdAt);
await relay.publish(signedEvent);
relay.close(); // You can send these properties via XMPP or perform other actions here
} xmpp.send(from, `\"${content}\", - ${author} @ ${createdAt}`);
} else {
main().catch((error) => { console.error('Invalid event structure or missing required properties.');
console.error(error); }
}); });
}
async function showSID(letsCID,pk) { gogoGlobalShoes().catch((error) => {
console.log(letsCID); console.error(error);
console.log(pk); });
const event = { } else if (message === '!help') {
id: letsCID, xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:commands');
relays: [config.relays.write], debug();
author: pk, } else if (message === "!auth") {
kind: 1, xmpp.send(from, "reauth (nip-26)");
};
const encodedNEvent = nostr.nip19.neventEncode(event); const now = new Date();
xmpp.send(from, "nostr:" + encodedNEvent); const validUntil = now.getTime() + (30 * 24 * 60 * 60 * 1000);
debug(); const insertStmt = db.prepare('INSERT INTO users (pnum, token) VALUES (?, ?)');
} insertStmt.run(from.split("@")[0], validUntil);
} insertStmt.finalize();
}); debug();
xmpp.on('error', function (err) { } else {
console.error(err); async function newPost() {
}); const relay = nostr.relayInit(config.relays.write, WebSocket);
xmpp.setPresence('chat', 'https://nostrsms.com'); relay.on('connect', () => {
xmpp.connect({ console.log(`connected to ${relay.url}`);
jid: config.xmpp.jid, });
password: config.xmpp.password, relay.on('error', () => {
host: config.xmpp.host, console.log(`failed to connect to ${relay.url}`);
port: 5222 });
});
process.on('SIGINT', function() { await relay.connect();
console.log("SigInt receieved. Shutdown inevitable!")
process.exit(); let event = {
}); kind: 1,
pubkey: pk,
} else { created_at: Math.floor(Date.now() / 1000),
console.log("Please create a config.ini"); tags: [],
console.log("See https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:install"); content: message,
console.log("and choose your preferred install method."); };
} const signedEvent = nostr.finishEvent(event, sk);
letsCID = signedEvent.id;
showSID(letsCID, pk);
await relay.publish(signedEvent);
relay.close();
}
newPost().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");
console.log("and choose your preferred install method.");
}