Project

General

Profile

« Previous | Next » 

Revision bd9c8c28

Added by Marc Dequènes over 14 years ago

  • ID bd9c8c28cfa0edb2e542eb3dec0cb2c5b7502d3b

[evol] reorganized TMail extra code, and extra methods in lang_additions

View differences:

lib/cyborghood/base/lang_additions.rb
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
# add Rails load path for Debian, until rails framework is split properly
DEB_RAILS_PATH = "/usr/share/rails" unless Object.constants.include?("DEB_RAILS_PATH")
Dir.new(DEB_RAILS_PATH).each do |file|
next if file =~ /^\./
path = File.join(DEB_RAILS_PATH, file, "lib")
$: << path if File.directory?(path)
end
require 'ostruct'
class Hash
......
end
end
# WARNING: the TMail send_to() method is _unsuable_, even with the following fixes
require 'tmail'
require 'action_mailer/quoting'
require 'action_mailer/utils'
module TMail
class Mail
extend ActionMailer::Quoting
include ActionMailer::Utils
# fix method using obsoleted from_address() method
def do_send_to( smtp )
from = from_addrs or raise ArgumentError, 'no from address'
(dests = destinations).empty? and raise ArgumentError, 'no receipient'
yield
send_to_0 smtp, from, dests
end
# fix method using encoded() with a wrong number of args
def send_to_0( smtp, from, to )
smtp.ready(from, to) do |f|
encoded "\r\n", 'j', f
end
end
# extra methods
def quoted_printable_body=(txt)
self.transfer_encoding = "quoted-printable"
self.body = [normalize_new_lines(txt)].pack("M")
end
def self.header_value_pretty(v)
Unquoter.unquote_and_convert_to(v.to_s, "UTF-8")
end
def self.header_value_smtp(v)
quote_address_if_necessary(v, "UTF-8")
end
["from", "to", "cc", "bcc", "reply_to"].each do |h|
class_eval <<-EOF
def #{h}_addrs_pretty
return nil if self.#{h}_addrs.nil?
self.#{h}_addrs.collect do |a|
self.class.header_value_pretty(a)
end
end
def #{h}_pretty
(self.#{h}_addrs_pretty || []).join(", ")
end
EOF
end
def subject_pretty
self.class.header_value_pretty(self.subject)
end
private
# reuse current boundary if already exist, not to break signed parts
def with_multipart_encoding( strategy )
if parts().empty? # DO NOT USE @parts
yield
else
bound = ::TMail.new_boundary
if @header.key? 'content-type'
if @header['content-type'].params.key? 'boundary'
bound = @header['content-type'].params['boundary']
else
@header['content-type'].params['boundary'] = bound
end
else
store 'Content-Type', %<multipart/mixed; boundary="#{bound}">
end
yield
parts().each do |tm|
strategy.puts
strategy.puts '--' + bound
tm.accept strategy
end
strategy.puts
strategy.puts '--' + bound + '--'
strategy.write epilogue()
end
end
end
end
lib/cyborghood/mail.rb
require 'delegate'
require 'tmail'
require 'tmail_gpg'
require 'tmail_extra'
require 'net/smtp'
require 'fileutils'
require 'digest/md5'
lib/tmail_extra.rb
#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
# add Rails load path for Debian, until rails framework is split properly
DEB_RAILS_PATH = "/usr/share/rails" unless Object.constants.include?("DEB_RAILS_PATH")
Dir.new(DEB_RAILS_PATH).each do |file|
next if file =~ /^\./
path = File.join(DEB_RAILS_PATH, file, "lib")
$: << path if File.directory?(path)
end
require 'tmail'
require 'action_mailer/quoting'
require 'action_mailer/utils'
module TMail
class Mail
extend ActionMailer::Quoting
include ActionMailer::Utils
# WARNING: the TMail send_to() method is _unsuable_, even with the following fixes
# fix method using obsoleted from_address() method
def do_send_to( smtp )
from = from_addrs or raise ArgumentError, 'no from address'
(dests = destinations).empty? and raise ArgumentError, 'no receipient'
yield
send_to_0 smtp, from, dests
end
# fix method using encoded() with a wrong number of args
def send_to_0( smtp, from, to )
smtp.ready(from, to) do |f|
encoded "\r\n", 'j', f
end
end
# extra methods
def quoted_printable_body=(txt)
self.transfer_encoding = "quoted-printable"
self.body = [normalize_new_lines(txt)].pack("M")
end
def self.header_value_pretty(v)
Unquoter.unquote_and_convert_to(v.to_s, "UTF-8")
end
def self.header_value_smtp(v)
quote_address_if_necessary(v, "UTF-8")
end
["from", "to", "cc", "bcc", "reply_to"].each do |h|
class_eval <<-EOF
def #{h}_addrs_pretty
return nil if self.#{h}_addrs.nil?
self.#{h}_addrs.collect do |a|
self.class.header_value_pretty(a)
end
end
def #{h}_pretty
(self.#{h}_addrs_pretty || []).join(", ")
end
EOF
end
def subject_pretty
self.class.header_value_pretty(self.subject)
end
private
# reuse current boundary if already exist, not to break signed parts
def with_multipart_encoding( strategy )
if parts().empty? # DO NOT USE @parts
yield
else
bound = ::TMail.new_boundary
if @header.key? 'content-type'
if @header['content-type'].params.key? 'boundary'
bound = @header['content-type'].params['boundary']
else
@header['content-type'].params['boundary'] = bound
end
else
store 'Content-Type', %<multipart/mixed; boundary="#{bound}">
end
yield
parts().each do |tm|
strategy.puts
strategy.puts '--' + bound
tm.accept strategy
end
strategy.puts
strategy.puts '--' + bound + '--'
strategy.write epilogue()
end
end
end
end

Also available in: Unified diff