Skip to content

Commit

Permalink
Use HTML instead of Markdown in the default messages
Browse files Browse the repository at this point in the history
Before this commit, Markdown was used in all the default messages. This
was fine for most of them, but it causes problems in the `/help`
command if someone puts any markdown special characters in the
docstring of any commands.

This commit switches all the default messages to forced HTML, because
it supports escaping unwanted entities.

Fixes: GH-51
  • Loading branch information
Pietro Albini committed Mar 30, 2016
1 parent d45177e commit 88f5e39
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 67 deletions.
33 changes: 18 additions & 15 deletions botogram/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def start_command(self, bot, chat):
message.append("")
message.append(bot._("Use /help to get a list of all the commands."))

chat.send("\n".join(message))
chat.send("\n".join(message), syntax="html")

@decorators.help_message_for(start_command)
def _start_command_help(bot):
Expand All @@ -45,19 +45,20 @@ def _start_command_help(bot):
def help_command(self, bot, chat, args):
commands = bot._get_commands()
if len(args) > 1:
message = [bot._("*Error!* The `/help` command allows up to one "
"argument.")]
message = [bot._("<b>Error!</b> The <code>/help</code> command "
"allows up to one argument.")]
elif len(args) == 1:
if args[0] in commands:
message = self._help_command_message(bot, commands, args[0])
else:
message = [bot._("*Unknown command:* `/%(name)s`",
message = [bot._("<b>Unknown command:</b> "
"<code>/%(name)s</code>",
name=args[0]),
bot._("Use /help to get a list of the commands.")]
else:
message = self._help_generic_message(bot, commands)

chat.send("\n".join(message))
chat.send("\n".join(message), syntax="html")

def _help_generic_message(self, bot, commands):
"""Generate an help message"""
Expand All @@ -74,7 +75,7 @@ def _help_generic_message(self, bot, commands):

# Show help on commands
if len(commands) > 0:
message.append(bot._("*This bot supports those commands:*"))
message.append(bot._("<b>This bot supports those commands:</b>"))
for name in sorted(commands.keys()):
# Allow to hide commands in the help message
if name in bot.hide_commands:
Expand All @@ -83,12 +84,13 @@ def _help_generic_message(self, bot, commands):
func = commands[name]
docstring = utils.docstring_of(func, bot, format=True) \
.split("\n", 1)[0]
message.append("/%s `-` %s" % (name, docstring))
message.append("/%s <code>-</code> %s" % (name, docstring))
message.append("")
message.append(bot._("You can also use `/help <command>` to get "
"help about a specific command."))
message.append(bot._("You can also use <code>/help &lt;command&gt;"
"</code> to get help about a specific "
"command."))
else:
message.append(bot._("_This bot has no commands._"))
message.append(bot._("<i>This bot has no commands.</i>"))

if len(bot.after_help):
message.append("")
Expand All @@ -109,7 +111,7 @@ def _help_command_message(self, bot, commands, command):

func = commands[command]
docstring = utils.docstring_of(func, bot, format=True)
message.append("/%s `-` %s" % (command, docstring))
message.append("/%s <code>-</code> %s" % (command, docstring))

# Show the owner informations
if bot.owner:
Expand All @@ -125,8 +127,8 @@ def _help_command_help(bot):
"""Get the help message of this command"""
return "\n".join([
bot._("Show this help message."),
bot._("You can also use `/help <command>` to get help about a "
"specific command."),
bot._("You can also use <code>/help &lt;command&gt;</code> to get "
"help about a specific command."),
])

# An hook which displays "Command not found" if needed
Expand All @@ -148,7 +150,8 @@ def no_commands_hook(self, bot, chat, message):
single_user = chat.type == "private"
if mentioned or single_user:
chat.send("\n".join([
bot._("*Unknown command:* `/%(name)s`", name=command),
bot._("<b>Unknown command:</b> <code>/%(name)s</code>",
name=command),
bot._("Use /help to get a list of the commands."),
]))
]), syntax="html")
return True
2 changes: 1 addition & 1 deletion botogram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def docstring_of(func, bot=None, component_id=None, format=False):
docstring = "No description available."

if format:
docstring = "_%s_" % docstring
docstring = "<i>%s</i>" % docstring

return format_docstr(docstring)

Expand Down
12 changes: 12 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ botogram changelog

Here you can see what changed in every botogram release.

.. _changelog-0.2.1:

botogram 0.2.1
==============

*Bugfix release, not yet released*

* Fix ``/help`` command crash if using markdown bits in the docstring (`issue
51`_)

.. _issue 51: https://github.com/pietroalbini/botogram/issues/51

.. _changelog-0.2:

botogram 0.2
Expand Down
32 changes: 17 additions & 15 deletions i18n/botogram.pot
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ msgid ""
msgstr ""
"Project-Id-Version: botogram 1.0.dev0\n"
"Report-Msgid-Bugs-To: https://github.com/pietroalbini/botogram/issues\n"
"POT-Creation-Date: 2016-03-22 22:09+0100\n"
"POT-Creation-Date: 2016-03-30 22:31+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.2.0\n"
"Generated-By: Babel 2.1.1\n"

#: botogram/defaults.py:32
msgid "Use /help to get a list of all the commands."
Expand All @@ -29,40 +29,42 @@ msgid "This shows a greeting message."
msgstr ""

#: botogram/defaults.py:48
msgid "*Error!* The `/help` command allows up to one argument."
msgid "<b>Error!</b> The <code>/help</code> command allows up to one argument."
msgstr ""

#: botogram/defaults.py:54 botogram/defaults.py:151
#: botogram/defaults.py:54 botogram/defaults.py:153
#, python-format
msgid "*Unknown command:* `/%(name)s`"
msgid "<b>Unknown command:</b> <code>/%(name)s</code>"
msgstr ""

#: botogram/defaults.py:56 botogram/defaults.py:152
#: botogram/defaults.py:57 botogram/defaults.py:155
msgid "Use /help to get a list of the commands."
msgstr ""

#: botogram/defaults.py:77
msgid "*This bot supports those commands:*"
#: botogram/defaults.py:78
msgid "<b>This bot supports those commands:</b>"
msgstr ""

#: botogram/defaults.py:88 botogram/defaults.py:128
msgid "You can also use `/help <command>` to get help about a specific command."
#: botogram/defaults.py:89 botogram/defaults.py:130
msgid ""
"You can also use <code>/help &lt;command&gt;</code> to get help about a "
"specific command."
msgstr ""

#: botogram/defaults.py:91
msgid "_This bot has no commands._"
#: botogram/defaults.py:93
msgid "<i>This bot has no commands.</i>"
msgstr ""

#: botogram/defaults.py:100 botogram/defaults.py:117
#: botogram/defaults.py:102 botogram/defaults.py:119
#, python-format
msgid "Please contact %(owner)s if you have problems with this bot."
msgstr ""

#: botogram/defaults.py:127
#: botogram/defaults.py:129
msgid "Show this help message."
msgstr ""

#: botogram/utils.py:138
#: botogram/utils.py:144
msgid "No description available."
msgstr ""

33 changes: 17 additions & 16 deletions i18n/langs/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: botogram 1.0.dev0\n"
"Report-Msgid-Bugs-To: https://github.com/pietroalbini/botogram/issues\n"
"POT-Creation-Date: 2016-03-22 22:09+0100\n"
"POT-Creation-Date: 2016-03-30 22:31+0200\n"
"PO-Revision-Date: 2015-08-01 17:20+0200\n"
"Last-Translator: Pietro Albini <[email protected]>\n"
"Language: en\n"
Expand All @@ -16,7 +16,7 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.2.0\n"
"Generated-By: Babel 2.1.1\n"

#: botogram/defaults.py:32
msgid "Use /help to get a list of all the commands."
Expand All @@ -31,40 +31,41 @@ msgid "This shows a greeting message."
msgstr ""

#: botogram/defaults.py:48
msgid "*Error!* The `/help` command allows up to one argument."
msgid "<b>Error!</b> The <code>/help</code> command allows up to one argument."
msgstr ""

#: botogram/defaults.py:54 botogram/defaults.py:151
#: botogram/defaults.py:54 botogram/defaults.py:153
#, python-format
msgid "*Unknown command:* `/%(name)s`"
msgid "<b>Unknown command:</b> <code>/%(name)s</code>"
msgstr ""

#: botogram/defaults.py:56 botogram/defaults.py:152
#: botogram/defaults.py:57 botogram/defaults.py:155
msgid "Use /help to get a list of the commands."
msgstr ""

#: botogram/defaults.py:77
msgid "*This bot supports those commands:*"
#: botogram/defaults.py:78
msgid "<b>This bot supports those commands:</b>"
msgstr ""

#: botogram/defaults.py:88 botogram/defaults.py:128
msgid "You can also use `/help <command>` to get help about a specific command."
#: botogram/defaults.py:89 botogram/defaults.py:130
msgid ""
"You can also use <code>/help &lt;command&gt;</code> to get help about a "
"specific command."
msgstr ""

#: botogram/defaults.py:91
msgid "_This bot has no commands._"
#: botogram/defaults.py:93
msgid "<i>This bot has no commands.</i>"
msgstr ""

#: botogram/defaults.py:100 botogram/defaults.py:117
#: botogram/defaults.py:102 botogram/defaults.py:119
#, python-format
msgid "Please contact %(owner)s if you have problems with this bot."
msgstr ""

#: botogram/defaults.py:127
#: botogram/defaults.py:129
msgid "Show this help message."
msgstr ""

#: botogram/utils.py:138
#: botogram/utils.py:144
msgid "No description available."
msgstr ""

42 changes: 22 additions & 20 deletions i18n/langs/it.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: botogram 1.0.dev0\n"
"Report-Msgid-Bugs-To: https://github.com/pietroalbini/botogram/issues\n"
"POT-Creation-Date: 2016-03-22 22:09+0100\n"
"POT-Creation-Date: 2016-03-30 22:31+0200\n"
"PO-Revision-Date: 2015-08-01 17:06+0200\n"
"Last-Translator: Pietro Albini <[email protected]>\n"
"Language: it\n"
Expand All @@ -16,7 +16,7 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.2.0\n"
"Generated-By: Babel 2.1.1\n"

#: botogram/defaults.py:32
msgid "Use /help to get a list of all the commands."
Expand All @@ -31,40 +31,42 @@ msgid "This shows a greeting message."
msgstr "Questo comando visualizza un messaggio di benvenuto."

#: botogram/defaults.py:48
msgid "*Error!* The `/help` command allows up to one argument."
msgstr "*Errore!* Il comando `/help` non accetta più di un argomento."
msgid "<b>Error!</b> The <code>/help</code> command allows up to one argument."
msgstr "<b>Errore!</b> Il comando <code>/help</code> non accetta più di un argomento."

#: botogram/defaults.py:54 botogram/defaults.py:151
#: botogram/defaults.py:54 botogram/defaults.py:153
#, python-format
msgid "*Unknown command:* `/%(name)s`"
msgstr "*Comando sconosciuto:* `/%(name)s`"
msgid "<b>Unknown command:</b> <code>/%(name)s</code>"
msgstr "<b>Comando sconosciuto:</b> <code>/%(name)s</code>"

#: botogram/defaults.py:56 botogram/defaults.py:152
#: botogram/defaults.py:57 botogram/defaults.py:155
msgid "Use /help to get a list of the commands."
msgstr "Utilizza /help per ottenere la lista dei comandi."

#: botogram/defaults.py:77
msgid "*This bot supports those commands:*"
msgstr "*Questo bot supporta i seguenti comandi:*"
#: botogram/defaults.py:78
msgid "<b>This bot supports those commands:</b>"
msgstr "<b>Questo bot supporta i seguenti comandi:</b>"

#: botogram/defaults.py:88 botogram/defaults.py:128
msgid "You can also use `/help <command>` to get help about a specific command."
msgstr "Puoi anche usare `/help <comando>` per ottenere aiuto su un comando."
#: botogram/defaults.py:89 botogram/defaults.py:130
msgid ""
"You can also use <code>/help &lt;command&gt;</code> to get help about a "
"specific command."
msgstr "Puoi anche usare <code>/help &lt;comando&gt;</code> per ottenere aiuto su un comando."

#: botogram/defaults.py:91
msgid "_This bot has no commands._"
msgstr "_Questo bot non ha comandi._"
#: botogram/defaults.py:93
msgid "<i>This bot has no commands.</i>"
msgstr "<i>Questo bot non ha comandi.</i>"

#: botogram/defaults.py:100 botogram/defaults.py:117
#: botogram/defaults.py:102 botogram/defaults.py:119
#, python-format
msgid "Please contact %(owner)s if you have problems with this bot."
msgstr "Contatta %(owner)s se hai problemi con questo bot."

#: botogram/defaults.py:127
#: botogram/defaults.py:129
msgid "Show this help message."
msgstr "Visualizza questo messaggio di aiuto."

#: botogram/utils.py:138
#: botogram/utils.py:144
msgid "No description available."
msgstr "Nessuna descrizione disponibile."

0 comments on commit 88f5e39

Please sign in to comment.