Sha256: 8b0dca76b706bf69520647fcf3bf0653da0ae4f9350d2316f5f4cb2c1219050d

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8

# The following is an adaptation of ruby 1.9.2's shellwords.rb file,
# it is modified to include '+' in the allowed list to allow for
# sendmail to accept email addresses as the sender with a + in them
#
module Mail
  module ShellEscape
    # Escapes a string so that it can be safely used in a Bourne shell
    # command line.
    #
    # Note that a resulted string should be used unquoted and is not
    # intended for use in double quotes nor in single quotes.
    #
    #   open("| grep #{Shellwords.escape(pattern)} file") { |pipe|
    #     # ...
    #   }
    #
    # +String#shellescape+ is a shorthand for this function.
    #
    #   open("| grep #{pattern.shellescape} file") { |pipe|
    #     # ...
    #   }
    #
    def escape_for_shell(str)
      # An empty argument will be skipped, so return empty quotes.
      return "''" if str.empty?

      str = str.dup

      # Process as a single byte sequence because not all shell
      # implementations are multibyte aware.
      str.gsub!(/([^A-Za-z0-9_\s\+\-.,:\/@])/n, "\\\\\\1")

      # A LF cannot be escaped with a backslash because a backslash + LF
      # combo is regarded as line continuation and simply ignored.
      str.gsub!(/\n/, "'\n'")

      return str
    end

    module_function :escape_for_shell
  end
end

class String
  # call-seq:
  #   str.shellescape => string
  #
  # Escapes +str+ so that it can be safely used in a Bourne shell
  # command line.  See +Shellwords::shellescape+ for details.
  #
  def escape_for_shell
    Mail::ShellEscape.escape_for_shell(self)
  end
end

Version data entries

12 entries across 12 versions & 4 rubygems

Version Path
challah-1.0.0.beta3 vendor/bundle/gems/mail-2.5.3/lib/mail/core_extensions/shell_escape.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/mail-2.5.3/lib/mail/core_extensions/shell_escape.rb
challah-1.0.0.beta2 vendor/bundle/gems/mail-2.5.3/lib/mail/core_extensions/shell_escape.rb
challah-1.0.0.beta vendor/bundle/gems/mail-2.5.3/lib/mail/core_extensions/shell_escape.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/mail-2.5.3/lib/mail/core_extensions/shell_escape.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/mail-2.5.3/lib/mail/core_extensions/shell_escape.rb
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/mail-2.5.3/lib/mail/core_extensions/shell_escape.rb
mail-2.5.3 lib/mail/core_extensions/shell_escape.rb
mail-2.5.2 lib/mail/core_extensions/shell_escape.rb
otherinbox-mail-2.4.4.20121031 lib/mail/core_extensions/shell_escape.rb
otherinbox-mail-2.4.4.20120605 lib/mail/core_extensions/shell_escape.rb
otherinbox-mail-2.4.4 lib/mail/core_extensions/shell_escape.rb