lib/sup.rb in sup-0.13.2.1 vs lib/sup.rb in sup-0.14.0

- old
+ new

@@ -1,13 +1,10 @@ -require 'rubygems' +# encoding: utf-8 -require 'syck' +require 'rubygems' require 'yaml' -if YAML.const_defined? :ENGINE - YAML::ENGINE.yamler = 'syck' -end - +YAML::ENGINE.yamler = 'psych' require 'zlib' require 'thread' require 'fileutils' require 'locale' require 'curses' @@ -26,22 +23,27 @@ end class Module def yaml_properties *props props = props.map { |p| p.to_s } - vars = props.map { |p| "@#{p}" } - klass = self - path = klass.name.gsub(/::/, "/") - klass.instance_eval do - define_method(:to_yaml_properties) { vars } - define_method(:to_yaml_type) { "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" } + path = name.gsub(/::/, "/") + yaml_tag "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" + + define_method :init_with do |coder| + initialize(*coder.map.values_at(*props)) end - YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val| - klass.new(*props.map { |p| val[p] }) + define_method :encode_with do |coder| + coder.map = props.inject({}) do |hash, key| + hash[key] = instance_variable_get("@#{key}") + hash + end end + + # Legacy + Psych.load_tags["!#{Redwood::LEGACY_YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}"] = self end end module Redwood BASE_DIR = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup") @@ -56,11 +58,12 @@ SUICIDE_FN = File.join(BASE_DIR, "please-kill-yourself") HOOK_DIR = File.join(BASE_DIR, "hooks") SEARCH_FN = File.join(BASE_DIR, "searches.txt") LOG_FN = File.join(BASE_DIR, "log") - YAML_DOMAIN = "masanjin.net" + YAML_DOMAIN = "supmua.org" + LEGACY_YAML_DOMAIN = "masanjin.net" YAML_DATE = "2006-10-01" ## record exceptions thrown in threads nicely @exceptions = [] @exception_mutex = Mutex.new @@ -266,11 +269,11 @@ abort "#{filename} is not a valid configuration file (it's a #{config.class}, not a hash)" unless config.is_a?(Hash) default_config.merge config else require 'etc' require 'socket' - name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first rescue nil + name = Etc.getpwnam(ENV["USER"]).gecos.split(/,/).first.force_encoding($encoding).fix_encoding rescue nil name ||= ENV["USER"] email = ENV["USER"] + "@" + begin Socket.gethostbyname(Socket.gethostname).first rescue SocketError @@ -278,12 +281,12 @@ end config = { :accounts => { :default => { - :name => name, - :email => email, + :name => name.fix_encoding, + :email => email.fix_encoding, :alternates => [], :sendmail => "/usr/sbin/sendmail -oem -ti", :signature => File.join(ENV["HOME"], ".signature"), :gpgkey => "" } @@ -312,13 +315,24 @@ require "sup/logger/singleton" ## determine encoding and character set $encoding = Locale.current.charset $encoding = "UTF-8" if $encoding == "utf8" +$encoding = "UTF-8" if $encoding == "UTF8" if $encoding debug "using character set encoding #{$encoding.inspect}" else warn "can't find character set by using locale, defaulting to utf-8" + $encoding = "UTF-8" +end + +# test encoding +teststr = "test" +teststr.encode('UTF-8') +begin + teststr.encode($encoding) +rescue Encoding::ConverterNotFoundError + warn "locale encoding is invalid, defaulting to utf-8" $encoding = "UTF-8" end require "sup/buffer" require "sup/keymap"