Break and/or improve things

This commit is contained in:
Tulir Asokan
2017-11-13 23:47:26 +02:00
parent 0639f26a2c
commit 0539af129b
7 changed files with 409 additions and 89 deletions
+71 -50
View File
@@ -25,85 +25,110 @@ function run(sender, command, args, reply, app) {
return
}
args.unshift(command)
sender.commandStatus.next(sender, args, reply, app)
return
return sender.commandStatus.next(sender, args, reply, app)
}
command = this.commands[command]
if (!command) {
reply("Unknown command. Try \"$cmdprefix help\" for help.")
return
}
command(sender, args, reply, app)
return command(sender, args, reply, app)
}
commands.cancel = () => "Nothing to cancel."
const enterPassword = (sender, args, reply) => {
commands.help = (sender, args, reply) => {
reply("Help not yet implemented 3:")
}
/////////////////////////////
// Authentication handlers //
/////////////////////////////
/**
* Two-factor authentication handler.
*/
const enterPassword = async (sender, args, reply) => {
if (args.length === 0) {
reply("Usage: $cmdprefix <password>")
return
}
const hash = makePasswordHash(sender.commandStatus.salt, args[0])
sender.checkPassword(hash)
.then(() => {
// TODO show who the user logged in as
reply(`Logged in successfully as @${sender.telegramPuppet.getDisplayName()}.`)
sender.commandStatus = undefined
}, err => {
reply(`Login failed: ${err}`)
console.log(err)
})
try {
await sender.checkPassword(hash)
reply(`Logged in successfully as @${sender.telegramPuppet.getDisplayName()}.`)
sender.commandStatus = undefined
} catch (err) {
reply(`Login failed: ${err}`)
console.log(err)
}
}
const enterCode = (sender, args, reply) => {
/*
* Login code send handler.
*/
const enterCode = async (sender, args, reply) => {
if (args.length === 0) {
reply("Usage: $cmdprefix <authentication code>")
return
}
sender.signInToTelegram(args[0])
.then(data => {
if (data.status === "ok") {
// TODO show who the user logged in as
reply(`Logged in successfully as @${sender.telegramPuppet.getDisplayName()}.`)
sender.commandStatus = undefined
} else if (data.status === "need-password") {
reply(`You have two-factor authentication enabled. Password hint: ${data.hint} \nEnter your password using "$cmdprefix <password>"`)
sender.commandStatus = {
action: "Two-factor authentication",
next: enterPassword,
salt: data.salt,
}
} else {
reply(`Unexpected sign in response, status=${data.status}`)
try {
const data = await sender.signInToTelegram(args[0])
if (data.status === "ok") {
// TODO show who the user logged in as
reply(`Logged in successfully as @${sender.telegramPuppet.getDisplayName()}.`)
sender.commandStatus = undefined
} else if (data.status === "need-password") {
reply(`You have two-factor authentication enabled. Password hint: ${data.hint}\nEnter your password using "$cmdprefix <password>"`)
sender.commandStatus = {
action: "Two-factor authentication",
next: enterPassword,
salt: data.salt,
}
}, err => {
reply(`Login failed: ${err}`)
console.log(err)
})
} else {
reply(`Unexpected sign in response, status=${data.status}`)
}
} catch (err) {
reply(`Login failed: ${err}`)
console.log(err)
}
}
commands.login = (sender, args, reply) => {
/*
* Login code request handler.
*/
commands.login = async (sender, args, reply) => {
if (args.length === 0) {
reply("Usage: $cmdprefix login <phone number>")
return
}
sender.sendTelegramCode(args[0])
.then(data => {
reply(`Login code sent to ${args[0]}. \nEnter the code using "$cmdprefix <code>"`)
sender.commandStatus = {
action: "Phone code authentication",
next: enterCode,
}
console.log(data)
}, err => {
reply(`Failed to send code: ${err}`)
console.log(err)
})
try {
const data = await sender.sendTelegramCode(args[0])
reply(`Login code sent to ${args[0]}.\nEnter the code using "$cmdprefix <code>"`)
sender.commandStatus = {
action: "Phone code authentication",
next: enterCode,
}
console.log(data)
} catch (err) {
reply(`Failed to send code: ${err}`)
console.log(err)
}
}
//////////////////////////////
// General command handlers //
//////////////////////////////
////////////////////////////
// Debug command handlers //
////////////////////////////
commands.api = async (sender, args, reply, app) => {
if (!app.config.telegram.allow_direct_api_calls) {
reply("Direct API calls are forbidden on this mautrix-telegram instance.")
@@ -126,10 +151,6 @@ commands.api = async (sender, args, reply, app) => {
}
}
commands.help = (sender, args, reply) => {
reply("Help not yet implemented 3:")
}
module.exports = {
commands,
run,