Skip to content

Commit

Permalink
Add cli args to exec and tty subcommands.
Browse files Browse the repository at this point in the history
  • Loading branch information
brannondorsey committed Feb 10, 2019
1 parent 5e9cfe1 commit 111ecf9
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 43 deletions.
88 changes: 77 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function parseArgs(): any {

send.addArgument(['--to', '-t'], {
type: 'string',
help: 'The recipient\'s callsign, callsign-ssid pair, or chatroom name. (default: "CQ")',
help: 'The recipient\'s callsign, callsign-ssid pair, or chatroom name (default: "CQ").',
defaultValue: 'CQ',
required: false
})
Expand Down Expand Up @@ -133,7 +133,7 @@ function parseArgs(): any {

const addKey: ArgumentParser = subs.addParser('addkey', {
addHelp: true,
description: 'Add a new public key to the keystore associated with a callsign.'
description: 'Add a new public key to the keystore associated with a callsign.'
})

addKey.addArgument('callsign', { type: 'string' })
Expand All @@ -160,19 +160,19 @@ function parseArgs(): any {

const exec: ArgumentParser = subs.addParser('exec', {
addHelp: true,
description: 'Execute a command using chattervox as the standard interface'
description: 'Execute a command using chattervox as the stdin and stdout interface.'
})

exec.addArgument(['--delay', '-s'], {
type: 'int',
help: 'Milliseconds before transmitting stdout after receiving stdin (default: 5000)',
defaultValue: 5000,
help: 'Milliseconds after receiving stdin to wait before transmitting stdout (default: 3000).',
defaultValue: 3000,
required: false
})

exec.addArgument(['--to', '-t'], {
type: 'string',
help: 'The recipient\'s callsign, callsign-ssid pair, or chatroom name. (default: "CQ")',
help: 'The recipient\'s callsign, callsign-ssid pair, or chatroom name (default: "CQ").',
defaultValue: 'CQ',
required: false
})
Expand All @@ -184,26 +184,56 @@ function parseArgs(): any {
required: false
})

exec.addArgument(['--stderr', '-e'], {
exec.addArgument(['--stderr', '-f'], {
action: 'storeTrue',
help: 'Also transmit stderr.',
defaultValue: false,
required: false
})

exec.addArgument(['--allow-unsigned', '-u'], {
action: 'storeTrue',
dest: 'allowUnsigned',
help: 'Receive unsigned messages.',
})

exec.addArgument(['--allow-untrusted', '-e'], {
action: 'storeTrue',
dest: 'allowUntrusted',
help: 'Receive messages signed by senders not in keyring.',
})

exec.addArgument(['--allow-invalid', '-i'], {
action: 'storeTrue',
dest: 'allowInvalid',
help: 'Receive messages with invalid signatures.',
})

exec.addArgument(['--all-recipients', '-g'], {
action: 'storeTrue',
dest: 'allRecipients',
help: 'Receive messages to all callsigns and chat rooms.',
})

exec.addArgument(['--allow-all', '-a'], {
action: 'storeTrue',
dest: 'allowAll',
help: 'Receive all messages, independent of signatures and destinations.',
})

exec.addArgument('command', {
type: 'string',
help: 'A command to be run'
})

const tty: ArgumentParser = subs.addParser('tty', {
addHelp: true,
description: 'A dumb tty interface. Sends what\'s typed, prints what\'s received.'
description: 'A dumb tty interface. Sends what\'s typed, prints what\'s received.'
})

tty.addArgument(['--to', '-t'], {
type: 'string',
help: 'The recipient\'s callsign, callsign-ssid pair, or chatroom name. (default: "CQ")',
help: 'The recipient\'s callsign, callsign-ssid pair, or chatroom name (default: "CQ").',
defaultValue: 'CQ',
required: false
})
Expand All @@ -215,6 +245,37 @@ function parseArgs(): any {
required: false
})

tty.addArgument(['--allow-unsigned', '-u'], {
action: 'storeTrue',
dest: 'allowUnsigned',
help: 'Receive unsigned messages.',
})

tty.addArgument(['--allow-untrusted', '-e'], {
action: 'storeTrue',
dest: 'allowUntrusted',
help: 'Receive messages signed by senders not in keyring.',
})

tty.addArgument(['--allow-invalid', '-i'], {
action: 'storeTrue',
dest: 'allowInvalid',
help: 'Receive messages with invalid signatures.',
})

tty.addArgument(['--all-recipients', '-g'], {
action: 'storeTrue',
dest: 'allRecipients',
help: 'Receive messages to all callsigns and chat rooms.',
})

tty.addArgument(['--allow-all', '-a'], {
action: 'storeTrue',
dest: 'allowAll',
help: 'Receive all messages, independent of signatures and destinations.',
})


return parser.parseArgs()
}

Expand Down Expand Up @@ -268,10 +329,15 @@ function cleanup(): void {
exec.cleanup()
}

function onSignal(): void {
cleanup()
process.exit(0)
}

async function main() {

process.on('SIGINT', cleanup) // catch ctrl-c
process.on('SIGTERM', cleanup) // catch kill
process.on('SIGINT', onSignal) // catch ctrl-c
process.on('SIGTERM', onSignal) // catch kill

const args = parseArgs()
validateArgs(args)
Expand Down
37 changes: 20 additions & 17 deletions src/subcommands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@ export async function main(args: any, conf: Config, ks: Keystore): Promise<numbe
})

messenger.on('message', (message: MessageEvent) => {
promises.push(timeout(args.delay).then(() => writeToProc(proc, message.message)))
// const to: Station = callsignSSIDToStation(args.to)
// if (args.allowAll) writeToProc(proc, message.message)
// else if (args.allRecipients ||
// (message.to.callsign === to.callsign && message.to.ssid == to.ssid)) {
// if (message.verification === Verification.Valid) {
// writeToProc(proc, message.message)
// } else if (args.allowUnsigned && message.verification === Verification.NotSigned) {
// writeToProc(proc, message.message)
// } else if (args.allowUntrusted && message.verification === Verification.KeyNotFound) {
// writeToProc(proc, message.message)
// } else if (args.allowInvalid && message.verification === Verification.Invalid) {
// writeToProc(proc, message.message)
// }
// }
let receive = false
const to: Station = callsignSSIDToStation(args.to)
if (args.allowAll) receive = true
else if (args.allRecipients ||
(message.to.callsign === to.callsign && message.to.ssid == to.ssid)) {
if (message.verification === Verification.Valid) {
receive = true
} else if (args.allowUnsigned && message.verification === Verification.NotSigned) {
receive = true
} else if (args.allowUntrusted && message.verification === Verification.KeyNotFound) {
receive = true
} else if (args.allowInvalid && message.verification === Verification.Invalid) {
receive = true
}
}

if (receive) {
promises.push(timeout(args.delay).then(() => writeToProc(proc, message.message)))
}

})

try {
Expand Down Expand Up @@ -86,8 +91,6 @@ export async function main(args: any, conf: Config, ks: Keystore): Promise<numbe
reject(err)
})

// throw Error('test')

const rl: readline.ReadLine = readline.createInterface({
input: process.stdin,
terminal: false,
Expand Down
29 changes: 14 additions & 15 deletions src/subcommands/tty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ export async function main(args: any, conf: Config, ks: Keystore): Promise<numbe
})

messenger.on('message', (message: MessageEvent) => {
console.log(message.message)
// const to: Station = callsignSSIDToStation(args.to)
// if (args.allowAll) writeToProc(proc, message.message)
// else if (args.allRecipients ||
// (message.to.callsign === to.callsign && message.to.ssid == to.ssid)) {
// if (message.verification === Verification.Valid) {
// writeToProc(proc, message.message)
// } else if (args.allowUnsigned && message.verification === Verification.NotSigned) {
// writeToProc(proc, message.message)
// } else if (args.allowUntrusted && message.verification === Verification.KeyNotFound) {
// writeToProc(proc, message.message)
// } else if (args.allowInvalid && message.verification === Verification.Invalid) {
// writeToProc(proc, message.message)
// }
// }
const to: Station = callsignSSIDToStation(args.to)
if (args.allowAll) console.log(message.message)
else if (args.allRecipients ||
(message.to.callsign === to.callsign && message.to.ssid == to.ssid)) {
if (message.verification === Verification.Valid) {
console.log(message.message)
} else if (args.allowUnsigned && message.verification === Verification.NotSigned) {
console.log(message.message)
} else if (args.allowUntrusted && message.verification === Verification.KeyNotFound) {
console.log(message.message)
} else if (args.allowInvalid && message.verification === Verification.Invalid) {
console.log(message.message)
}
}
})

try {
Expand Down

0 comments on commit 111ecf9

Please sign in to comment.