Is there any way to decode q-encoded strings in Ruby?

I work with mail, and names and topics are sometimes q-encoded, for example:

=?UTF-8?Q?J=2E_Pablo_Fern=C3=A1ndez?= 

Is there any way to decode them in Ruby? It seems that TMail should take care of this, but it does not.

+4
ruby email decoding
source share
4 answers

I use this to parse email topics:

You can try the following:

 str = "=?UTF-8?Q?J=2E_Pablo_Fern=C3=A1ndez?=" if m = /=\?([A-Za-z0-9\-]+)\?(B|Q)\?([!->@-~]+)\?=/i.match(str) case m[2] when "B" # Base64 encoded decoded = Base64.decode64(m[3]) when "Q" # Q encoded decoded = m[3].unpack("M").first.gsub('_',' ') else p "Could not find keyword!!!" end Iconv.conv('utf-8',m[1],decoded) # to convert to utf-8 end 
+6
source share

Ruby includes a decoding method for quoted strings:

 puts "Pablo_Fern=C3=A1ndez".unpack "M" # => Pablo_FernΓ‘ndez 

But this doesn't look like your whole line (including the =?UTF-8?Q? Part at the beginning. Perhaps you can work it out from there.

+3
source share

This is a pretty old question, but TMail :: Unquoter (or its new Mail :: Encodings incarnation) does the job too.

 TMail::Unquoter.unquote_and_convert_to(str, 'utf-8' ) 

or

 Mail::Encodings.unquote_and_convert_to( str, 'utf-8' ) 
+1
source share

Decoding line by line:

 line.unpack("M") 

Convert STDIN or file provided encoded string input to decoded output:

 if ARGV[0] lines = File.read(ARGV[0]).lines else lines = STDIN.each_line.to_a end puts lines.map { |c| c.unpack("M") }.join 
0
source share

All Articles