Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ascii encoding #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ require 'rake/testtask'
require 'rbconfig'
require 'fileutils'

spec = eval File.read('ruby-msg.gemspec')
gemspec_file = File.expand_path(File.dirname(__FILE__) + '/ruby-msg.gemspec')
spec = eval File.read(gemspec_file)

task :default => [:test]

Expand Down
4 changes: 2 additions & 2 deletions contrib/wmf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-

# this file will be used later to enhance the msg conversion.

Expand Down Expand Up @@ -52,7 +52,7 @@ def wmf_getdimensions wmf_data
\b\f2\fs20{\object\objemb{\*\objclass PBrush}\objw1320\objh1274{\*\objdata
01050000 <- looks like standard header
02000000 <- not sure
07000000 <- this means length of following is 7.
07000000 <- this means length of following is 7.
50427275736800 <- Pbrush\000 in hex
00000000 <- ?
00000000 <- ?
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'mapi/version'
require 'mapi/base'
require 'mapi/types'
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
module Mapi
#
# Mapi::Item is the base class used for all mapi objects, and is purely a
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/convert.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
# we have two different "backends" for note conversion. we're sticking with
# the current (home grown) mime one until the tmail version is suitably
# polished.
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/convert/contact.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'rubygems'
require 'vpim/vcard'

Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/convert/note-mime.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'base64'
require 'mapi/mime'
require 'time'
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/convert/note-tmail.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'rubygems'
require 'tmail'

Expand Down
15 changes: 12 additions & 3 deletions lib/mapi/mime.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
#
# = Introduction
#
Expand Down Expand Up @@ -106,8 +106,17 @@ def to_s opts={}
opts = {:boundary_counter => 0}.merge opts
if multipart?
boundary = Mime.make_boundary opts[:boundary_counter] += 1, self
@body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts) + "\r\n" }, "--\r\n" + epilogue].
flatten.join("\r\n--" + boundary)
begin
@body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts) + "\r\n" }, "--\r\n" + epilogue].
flatten.join("\r\n--" + boundary)
rescue Encoding::CompatibilityError => e
if ''.respond_to?(:force_encoding)
@body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts).force_encoding('ASCII-8BIT') + "\r\n" }, "--\r\n" + epilogue].
flatten.join("\r\n--" + boundary)
else
raise e
end
end
content_type, attrs = Mime.split_header @headers['Content-Type'][0]
attrs['boundary'] = boundary
@headers['Content-Type'] = [([content_type] + attrs.map { |key, val| %{#{key}="#{val}"} }).join('; ')]
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/msg.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'ole/storage'
require 'mapi'
require 'mapi/rtf'
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/orderedhash.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
# = OrderedHash
#
# == Version
Expand Down
7 changes: 1 addition & 6 deletions lib/mapi/property_set.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'yaml'
require 'mapi/types'
require 'mapi/rtf'
Expand Down Expand Up @@ -282,11 +282,6 @@ def body_html
end
end
end
if @body_html && @body_html.respond_to?(:encoding)
@body_html.force_encoding("utf-8")
@body_html = @body_html.valid_encoding? ? @body_html : @body_html.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
end
@body_html
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/pst.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
#
# = Introduction
#
Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/rtf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'stringio'
require 'strscan'

Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/types.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'rubygems'
require 'ole/types'

Expand Down
2 changes: 1 addition & 1 deletion lib/mapi/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
module Mapi
VERSION = '1.5.2'
end
2 changes: 1 addition & 1 deletion test/test_convert_contact.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'test/unit'

$:.unshift File.dirname(__FILE__) + '/../lib'
Expand Down
2 changes: 1 addition & 1 deletion test/test_convert_note.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'test/unit'

$:.unshift File.dirname(__FILE__) + '/../lib'
Expand Down
2 changes: 1 addition & 1 deletion test/test_mime.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
#! /usr/bin/ruby -w

$: << File.dirname(__FILE__) + '/../lib'
Expand Down
61 changes: 29 additions & 32 deletions test/test_msg.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
#! /usr/bin/ruby

TEST_DIR = File.dirname __FILE__
Expand All @@ -9,39 +9,36 @@
require 'mapi/convert'

class TestMsg < Test::Unit::TestCase
def test_blammo
Mapi::Msg.open "#{TEST_DIR}/test_Blammo.msg" do |msg|
assert_equal '"TripleNickel" <TripleNickel@mapi32.net>', msg.from
assert_equal 'BlammoBlammo', msg.subject
assert_equal 0, msg.recipients.length
assert_equal 0, msg.attachments.length
# this is all properties
assert_equal 66, msg.properties.raw.length
# this is unique named properties
assert_equal 48, msg.properties.to_h.length
# test accessing the named property keys - same name but different namespace
assert_equal 'Yippee555', msg.props['Name4', Ole::Types::Clsid.parse('55555555-5555-5555-c000-000000000046')]
assert_equal 'Yippee666', msg.props['Name4', Ole::Types::Clsid.parse('66666666-6666-6666-c000-000000000046')]
end
end
def test_blammo
Mapi::Msg.open "#{TEST_DIR}/test_Blammo.msg" do |msg|
assert_equal '"TripleNickel" <TripleNickel@mapi32.net>', msg.from
assert_equal 'BlammoBlammo', msg.subject
assert_equal 0, msg.recipients.length
assert_equal 0, msg.attachments.length
# this is all properties
assert_equal 66, msg.properties.raw.length
# this is unique named properties
assert_equal 48, msg.properties.to_h.length
# test accessing the named property keys - same name but different namespace
assert_equal 'Yippee555', msg.props['Name4', Ole::Types::Clsid.parse('55555555-5555-5555-c000-000000000046')]
assert_equal 'Yippee666', msg.props['Name4', Ole::Types::Clsid.parse('66666666-6666-6666-c000-000000000046')]
end
end


def test_rtf_to_html_returns_valid_utf8
msg = Mapi::Msg.open "#{TEST_DIR}/multipart-with-html.msg" do |msg|
assert_equal 1, msg.recipients.length
assert_equal 3, msg.attachments.length
html_part = msg.to_mime.parts[0].parts[1].to_s
if html_part.respond_to?(:encoding)
assert_equal 'UTF-8', html_part.encoding.to_s
assert html_part.valid_encoding?
end
end
end
def test_rendered_string_is_valid_encoding
msg = Mapi::Msg.open "#{TEST_DIR}/multipart-with-html.msg" do |msg|
string_version = msg.to_mime.to_s
if string_version.respond_to?(:valid_encoding?)
assert_equal true, string_version.valid_encoding?
end
end
end

def test_embedded_msg_renders_as_string
msg = Mapi::Msg.open "#{TEST_DIR}/embedded.msg" do |msg|
assert_match "message/rfc822", msg.to_mime.to_s
end
end
def test_embedded_msg_renders_as_string
msg = Mapi::Msg.open "#{TEST_DIR}/embedded.msg" do |msg|
assert_match "message/rfc822", msg.to_mime.to_s
end
end
end

2 changes: 1 addition & 1 deletion test/test_property_set.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'test/unit'

$:.unshift File.dirname(__FILE__) + '/../lib'
Expand Down
2 changes: 1 addition & 1 deletion test/test_types.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- encoding : utf-8 -*-
# -*- encoding : ASCII-8BIT -*-
require 'test/unit'

$:.unshift File.dirname(__FILE__) + '/../lib'
Expand Down