def utf8 message
return nil unless message
message.force_encoding("utf-8")
detect = CharlockHolmes::EncodingDetector.detect(message)
return message if detect[:type] == :binary
return message if message.valid_encoding?
if detect[:encoding]
message.force_encoding(detect[:encoding])
message.encode!("utf-8", detect[:encoding], undef: :replace, replace: "", invalid: :replace)
end
message.valid_encoding? ? message : raise
rescue
encoding = detect ? detect[:encoding] : "unknown"
"--broken encoding: #{encoding}"
end