Skip to content

Commit de65d97

Browse files
Fixed markdown character escaping
1 parent 7a8d50f commit de65d97

12 files changed

+37
-25
lines changed

NotifierBot/Sources/Notifier/Commands/AddCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct AddCommand: Command {
1212

1313
let name = "Add"
1414
let commands = ["/add"]
15-
let syntax = "/add <name> <URL> \\[x y width height]"
15+
let syntax = "/add <name> <URL> [x y width height]"
1616
let description = "Adds a new website with an optional screenshot area to the list"
1717
let permission = BotPermission.mod
1818

NotifierBot/Sources/Notifier/Commands/Command.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ protocol Command {
1313
var name: String { get }
1414
var commands: [String] { get }
1515
var permission: BotPermission { get }
16+
/// Syntax of the command, used when showing the the usage. Contents of this string will be escaped so they can be used in markdownV2 format.
1617
var syntax: String { get }
1718
var usage: String { get }
1819
var description: String { get }

NotifierBot/Sources/Notifier/Commands/FetchURLCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct FetchURLCommand: Command {
1212

1313
let name = "Fetch URL"
1414
let commands = ["/fetchurl"]
15-
let syntax = "/fetchurl <URL> \\[x y width height]"
15+
let syntax = "/fetchurl <URL> [x y width height]"
1616
let description = "Takes a screenshot of the given website and settings and sends it into this chat"
1717
let permission = BotPermission.mod
1818

NotifierBot/Sources/Notifier/Commands/GetPermissionsCommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct GetPermissionsCommand: Command {
1212

1313
let name = "Get Permissions"
1414
let commands = ["/getpermissions"]
15-
let syntax = "/getpermissions \\[id]"
15+
let syntax = "/getpermissions [id]"
1616
let description = "Returns the permission level of the author of the message, replied to or the user id provided"
1717
let permission = BotPermission.admin
1818

@@ -47,6 +47,6 @@ struct GetPermissionsCommand: Command {
4747
username = "\(id)"
4848
}
4949
let level = ConfigParser.shared.permissionGroup(user: userID)
50-
try bot.sendMessage("The permission level of \(username!) is *\(level.rawValue)*.", to: chatID)
50+
try bot.sendMessage("The permission level of \(username!.escaped()) is *\(level.rawValue)*\\.", to: chatID, parseMode: .markdownV2)
5151
}
5252
}

NotifierBot/Sources/Notifier/Commands/HelpCommand.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ struct HelpCommand: Command {
2828
for permission in BotPermission.allCases {
2929
let commands = allCommands.filter({ $0.permission == permission })
3030
// Print the commands for this group
31-
usage += "*\(permission.rawValue.capitalized)*\n"
31+
usage += "*\(permission.rawValue.capitalized.escaped())*\n"
3232
for command in commands {
33-
usage += "\(command.syntax)\n\(command.description)\n"
33+
usage += "\(command.syntax.escaped())\n\(command.description.escaped())\n"
3434
}
3535
// Extra space between permission groups
3636
usage += "\n"
3737
}
3838
if usage.hasSuffix("\n") {
3939
usage.removeLast()
4040
}
41-
try bot.sendMessage(usage, to: chatID)
41+
try bot.sendMessage(usage, to: chatID, parseMode: .markdownV2)
4242
}
4343

4444
}

NotifierBot/Sources/Notifier/Commands/ListAllCommand.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ struct ListAllCommand: Command {
3131
list += "*\(chatID):*\n"
3232
for entry in entries.filter({ $0.chatID == chatID }) {
3333
// For each entry in this group
34-
list += "- \(entry.name): \(entry.url)"
34+
list += "- \(entry.name): \(entry.url)".escaped()
3535
if entry.area.width != 0 && entry.area.height != 0 {
36-
list += " (\(entry.area.width)x\(entry.area.height)+\(entry.area.x)+\(entry.area.y))"
36+
list += " (\(entry.area.width)x\(entry.area.height)+\(entry.area.x)+\(entry.area.y))".escaped()
3737
}
3838
list += "\n"
3939
}
4040
// Extra empty line between groups
4141
list += "\n"
4242
}
43-
if list.hasSuffix("\n") {
43+
while list.hasSuffix("\n") {
4444
list.removeLast()
4545
}
4646
// In case the file was empty
4747
if entries.isEmpty {
4848
list += "\n_None_"
4949
}
50-
try bot.sendMessage(list, to: chatID)
50+
try bot.sendMessage(list, to: chatID, parseMode: .markdownV2)
5151
}
5252

5353
}

NotifierBot/Sources/Notifier/Commands/ListCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct ListCommand: Command {
2020
let chatID = try update.chatID()
2121
let entries = try ConfigParser.getConfig().filter({ $0.chatID == chatID })
2222
let list = JFUtils.entryList(entries, listArea: true, listURLs: false)
23-
try bot.sendMessage(list, to: chatID)
23+
try bot.sendMessage(list, to: chatID, parseMode: .markdownV2)
2424
}
2525

2626
}

NotifierBot/Sources/Notifier/Commands/ListURLsCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct ListURLsCommand: Command {
2020
let chatID = try update.chatID()
2121
let entries = try ConfigParser.getConfig().filter({ $0.chatID == chatID })
2222
let list = JFUtils.entryList(entries, listArea: false, listURLs: true)
23-
try bot.sendMessage(list, to: chatID)
23+
try bot.sendMessage(list, to: chatID, parseMode: .markdownV2)
2424
}
2525

2626
}

NotifierBot/Sources/Notifier/Commands/MyIDCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ struct MyIDCommand: Command {
2222
try bot.sendMessage("Unable to determine user.", to: chatID)
2323
return
2424
}
25-
try bot.sendMessage("The user ID of \(user.username ?? "<Unknown>") is `\(user.id)`", to: chatID)
25+
try bot.sendMessage("The user ID of \(user.username?.escaped() ?? "<Unknown>") is `\(user.id)`", to: chatID, parseMode: .markdownV2)
2626
}
2727
}

NotifierBot/Sources/Notifier/Commands/SetPermissionsCommand.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct SetPermissionsCommand: Command {
1212

1313
let name = "Set Permissions"
1414
let commands = ["/setpermissions"]
15-
let syntax = "/setpermissions <level> \\[id]"
15+
let syntax = "/setpermissions <level> [id]"
1616
let description = "Sets the permission level of the author of the message, replied to or the user id provided"
1717
let permission = BotPermission.admin
1818

@@ -43,10 +43,10 @@ struct SetPermissionsCommand: Command {
4343
username = "\(id)"
4444
}
4545
guard let level = BotPermission(rawValue: args[0].trimmingCharacters(in: .whitespaces)) else {
46-
try bot.sendMessage("Error: Please specify a valid bot permission. (\(BotPermission.allCases.map({ $0.rawValue }).joined(separator: ", ")))", to: chatID)
46+
try bot.sendMessage("Error: Please specify a valid bot permission: \(BotPermission.allCases.map({ $0.rawValue }).joined(separator: ", "))", to: chatID)
4747
return
4848
}
4949
try ConfigParser.shared.setPermissionGroup(user: userID, level: level)
50-
try bot.sendMessage("Successfully set the permission level of \(username!) to *\(level.rawValue)*.", to: chatID)
50+
try bot.sendMessage("Successfully set the permission level of \(username!.escaped()) to *\(level.rawValue)*.", to: chatID, parseMode: .markdownV2)
5151
}
5252
}

0 commit comments

Comments
 (0)