naughty: Try ignoring all markup before giving up

naughty now tries to ignore all pango markup by using :set_text() instead of
:set_markup(). If this fails, too, we must have been fed invalid utf8 which we
cannot do anything about.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2011-01-14 16:16:04 +01:00
parent ec72ee1b43
commit ecb8376c44
1 changed files with 10 additions and 4 deletions

View File

@ -356,16 +356,22 @@ function notify(args)
marginbox:set_widget(textbox) marginbox:set_widget(textbox)
textbox.valign = "middle" textbox.valign = "middle"
local function setText(pattern, replacements) local function setMarkup(pattern, replacements)
textbox:set_markup(string.format('<span font_desc="%s"><b>%s</b>%s</span>', font, title, text:gsub(pattern, replacements))) textbox:set_markup(string.format('<span font_desc="%s"><b>%s</b>%s</span>', font, title, text:gsub(pattern, replacements)))
end end
local function setText()
textbox:set_text(string.format('%s %s', title, text)
end
-- First try to set the text while only interpreting <br>. -- First try to set the text while only interpreting <br>.
-- (Setting a textbox' .text to an invalid pattern throws a lua error) -- (Setting a textbox' .text to an invalid pattern throws a lua error)
if not pcall(setText, "<br.->", "\n") then if not pcall(setMarkup, "<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango -- That failed, escape everything which might cause an error from pango
if not pcall(setText, "[<>&]", { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }) then if not pcall(setMarkup, "[<>&]", { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" }) then
textbox:set_markup("<i>&lt;Invalid markup, cannot display message&gt;</i>") -- Ok, just ignore all pango markup. If this fails, we got some invalid utf8
if not pcall(setText) then
textbox:set_markup("<i>&lt;Invalid markup or UTF8, cannot display message&gt;</i>")
end
end end
end end