Fix bugs and #12
This commit is contained in:
+48
-34
@@ -38,6 +38,7 @@ class MautrixTelegram {
|
|||||||
this.telegramUsersByID = new Map()
|
this.telegramUsersByID = new Map()
|
||||||
this.portalsByPeerID = new Map()
|
this.portalsByPeerID = new Map()
|
||||||
this.portalsByRoomID = new Map()
|
this.portalsByRoomID = new Map()
|
||||||
|
this.managementRooms = []
|
||||||
|
|
||||||
const self = this
|
const self = this
|
||||||
this.bridge = new Bridge({
|
this.bridge = new Bridge({
|
||||||
@@ -304,45 +305,69 @@ class MautrixTelegram {
|
|||||||
}, entry)
|
}, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getRoomMembers(roomID) {
|
||||||
|
const roomState = await this.botIntent.roomState(roomID)
|
||||||
|
const members = []
|
||||||
|
for (const event of roomState) {
|
||||||
|
if (event.type === "m.room.member" && event.membership === "join") {
|
||||||
|
members.push(event.user_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return members
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle a single received Matrix event.
|
* Handle a single received Matrix event.
|
||||||
*
|
*
|
||||||
* @param evt The Matrix event that occurred.
|
* @param evt The Matrix event that occurred.
|
||||||
*/
|
*/
|
||||||
async handleMatrixEvent(evt) {
|
async handleMatrixEvent(evt) {
|
||||||
const asBotID = this.bridge.getBot()
|
const user = await this.getMatrixUser(evt.sender)
|
||||||
.getUserId()
|
if (!user.whitelisted) {
|
||||||
if (evt.type === "m.room.member" && evt.state_key === asBotID) {
|
return
|
||||||
if (evt.content.membership === "invite") {
|
}
|
||||||
// Accept all invites
|
|
||||||
this.botIntent.join(evt.room_id)
|
const asBotID = this.bridge.getBot().getUserId()
|
||||||
.catch(err => {
|
if (evt.type === "m.room.member" && evt.state_key === asBotID && evt.content.membership === "invite") {
|
||||||
console.warn(`Failed to join room ${evt.room_id}:`, err)
|
// Accept all invites
|
||||||
if (err instanceof Error) {
|
try {
|
||||||
console.warn(err.stack)
|
await this.botIntent.join(evt.room_id)
|
||||||
}
|
} catch (err) {
|
||||||
})
|
console.error(`Failed to join room ${evt.room_id}:`, err)
|
||||||
|
if (err instanceof Error) {
|
||||||
|
console.error(err.stack)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO to handle joining telegram groups and initiating private chats, we need to handle room member events.
|
||||||
if (evt.sender === asBotID || evt.type !== "m.room.message" || !evt.content) {
|
if (evt.sender === asBotID || evt.type !== "m.room.message" || !evt.content) {
|
||||||
// Ignore own messages and non-message events.
|
// Ignore own messages and non-message events.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await this.getMatrixUser(evt.sender)
|
const portal = await this.getPortalByRoomID(evt.room_id)
|
||||||
|
if (portal) {
|
||||||
|
portal.handleMatrixEvent(user, evt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const cmdprefix = this.config.bridge.commands.prefix
|
let isManagement = this.managementRooms.includes(evt.room_id)
|
||||||
if (evt.content.body.startsWith(`${cmdprefix} `)) {
|
if (!isManagement) {
|
||||||
if (!user.whitelisted) {
|
const roomMembers = await this.getRoomMembers(evt.room_id)
|
||||||
this.botIntent.sendText(evt.room_id, "You are not authorized to use this bridge.")
|
if (roomMembers.length === 2 && roomMembers.includes(asBotID)) {
|
||||||
return
|
this.managementRooms.push(evt.room_id)
|
||||||
|
isManagement = true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
const cmdprefix = this.config.bridge.commands.prefix
|
||||||
|
if (isManagement || evt.content.body.startsWith(`${cmdprefix} `)) {
|
||||||
const prefixLength = cmdprefix.length + 1
|
const prefixLength = cmdprefix.length + 1
|
||||||
const args = evt.content.body.substr(prefixLength)
|
if (evt.content.body.startsWith(`${cmdprefix} `)) {
|
||||||
.split(" ")
|
evt.content.body = evt.content.body.substr(prefixLength)
|
||||||
|
}
|
||||||
|
const args = evt.content.body.split(" ")
|
||||||
const command = args.shift()
|
const command = args.shift()
|
||||||
const replyFunc = (reply, { allowHTML = false, markdown = true } = {}) => {
|
const replyFunc = (reply, { allowHTML = false, markdown = true } = {}) => {
|
||||||
reply = reply.replace("$cmdprefix", cmdprefix)
|
reply = reply.replace("$cmdprefix", cmdprefix)
|
||||||
@@ -351,7 +376,7 @@ class MautrixTelegram {
|
|||||||
}
|
}
|
||||||
if (markdown) {
|
if (markdown) {
|
||||||
reply = marked(reply, {
|
reply = marked(reply, {
|
||||||
sanitize: allowHTML,
|
sanitize: !allowHTML,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
this.botIntent.sendMessage(
|
this.botIntent.sendMessage(
|
||||||
@@ -362,18 +387,7 @@ class MautrixTelegram {
|
|||||||
format: "org.matrix.custom.html",
|
format: "org.matrix.custom.html",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
commands.run(user, command, args, replyFunc, this)
|
commands.run(user, command, args, replyFunc, this, evt)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!user.whitelisted) {
|
|
||||||
// Non-management command from non-whitelisted user -> fail silently.
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const portal = await this.getPortalByRoomID(evt.room_id)
|
|
||||||
if (portal) {
|
|
||||||
portal.handleMatrixEvent(user, evt)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+20
-10
@@ -17,7 +17,7 @@ const makePasswordHash = require("telegram-mtproto").plugins.makePasswordHash
|
|||||||
|
|
||||||
const commands = {}
|
const commands = {}
|
||||||
|
|
||||||
function run(sender, command, args, reply, app) {
|
function run(sender, command, args, reply, app, evt) {
|
||||||
const commandFunc = this.commands[command]
|
const commandFunc = this.commands[command]
|
||||||
if (!commandFunc) {
|
if (!commandFunc) {
|
||||||
if (sender.commandStatus) {
|
if (sender.commandStatus) {
|
||||||
@@ -27,13 +27,13 @@ function run(sender, command, args, reply, app) {
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
args.unshift(command)
|
args.unshift(command)
|
||||||
return sender.commandStatus.next(sender, args, reply, app)
|
return sender.commandStatus.next(sender, args, reply, app, evt)
|
||||||
}
|
}
|
||||||
reply("Unknown command. Try `$cmdprefix help` for help.")
|
reply("Unknown command. Try `$cmdprefix help` for help.")
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return commandFunc(sender, args, reply, app)
|
return commandFunc(sender, args, reply, app, evt)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reply(`Error running command: ${err}.`)
|
reply(`Error running command: ${err}.`)
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
@@ -46,17 +46,28 @@ function run(sender, command, args, reply, app) {
|
|||||||
|
|
||||||
commands.cancel = () => "Nothing to cancel."
|
commands.cancel = () => "Nothing to cancel."
|
||||||
|
|
||||||
commands.help = (sender, args, reply) => {
|
commands.help = (sender, args, reply, app, evt) => {
|
||||||
reply(`All commands are prefixed with **$cmdprefix**.
|
let replyMsg = ""
|
||||||
|
if (app.managementRooms.includes(evt.room_id)) {
|
||||||
|
replyMsg += "This is a management room: prefixing commands with `$cmdprefix` is not required.\n"
|
||||||
|
} else {
|
||||||
|
replyMsg += "This is not a management room: you must prefix commands with `$cmdprefix`.\n"
|
||||||
|
}
|
||||||
|
replyMsg += `
|
||||||
**help** - Show this help message.<br/>
|
**help** - Show this help message.<br/>
|
||||||
**cancel** - Cancel an ongoing action (such as login).
|
**cancel** - Cancel an ongoing action (such as login).
|
||||||
|
|
||||||
**login** <_phone_> - Request an authentication code.<br/>
|
**login** <_phone_> - Request an authentication code.<br/>
|
||||||
**logout** - Log out from Telegram. Currently broken.
|
**logout** - Log out from Telegram. Currently broken.
|
||||||
|
|
||||||
**api** <_method_> <_args_> - Call a Telegram API method. Args is always a JSON object. Disabled by default.
|
**api** <_method_> <_args_> - Call a Telegram API method. Args is always a JSON object. Disabled by default.
|
||||||
`, { allowHTML: true })
|
`
|
||||||
|
reply(replyMsg, { allowHTML: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
commands.setManagement = async (sender, _, reply, app, evt) => {
|
||||||
|
app.managementRooms.push(evt.room_id)
|
||||||
|
reply("Room marked as management room. You can now run commands without the `$cmdprefix` prefix.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -233,7 +244,6 @@ commands.pm = async (sender, args, reply, app) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
// Debug command handlers //
|
// Debug command handlers //
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
|
|||||||
@@ -179,8 +179,7 @@ class TelegramPuppet {
|
|||||||
|
|
||||||
getDisplayName() {
|
getDisplayName() {
|
||||||
if (this.data.firstName || this.data.lastName) {
|
if (this.data.firstName || this.data.lastName) {
|
||||||
return [this.data.firstName, this.data.lastName].filter(s => !!s)
|
return [this.data.firstName, this.data.lastName].filter(s => !!s).join(" ")
|
||||||
.join(" ")
|
|
||||||
} else if (this.data.username) {
|
} else if (this.data.username) {
|
||||||
return this.data.username
|
return this.data.username
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,19 +72,19 @@ class TelegramUser {
|
|||||||
|
|
||||||
async updateInfo(telegramPOV, user, { updateAvatar = false } = {}) {
|
async updateInfo(telegramPOV, user, { updateAvatar = false } = {}) {
|
||||||
let changed = false
|
let changed = false
|
||||||
if (this.firstName !== user.first_name) {
|
if (user.first_name && this.firstName !== user.first_name) {
|
||||||
this.firstName = user.first_name
|
this.firstName = user.first_name
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
if (this.lastName !== user.last_name) {
|
if (user.last_name && this.lastName !== user.last_name) {
|
||||||
this.lastName = user.last_name
|
this.lastName = user.last_name
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
if (this.username !== user.username) {
|
if (user.username && this.username !== user.username) {
|
||||||
this.username = user.username
|
this.username = user.username
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
if (telegramPOV && this.accessHashes.get(telegramPOV.userID) !== user.access_hash) {
|
if (user.access_hash && telegramPOV && this.accessHashes.get(telegramPOV.userID) !== user.access_hash) {
|
||||||
this.accessHashes.set(telegramPOV.userID, user.access_hash)
|
this.accessHashes.set(telegramPOV.userID, user.access_hash)
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
@@ -116,8 +116,7 @@ class TelegramUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getFirstAndLastName() {
|
getFirstAndLastName() {
|
||||||
return [this.firstName, this.lastName].filter(s => !!s)
|
return [this.firstName, this.lastName].filter(s => !!s).join(" ")
|
||||||
.join(" ")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getDisplayName() {
|
getDisplayName() {
|
||||||
|
|||||||
Reference in New Issue
Block a user