Class: ItemOfMail

Inherits:
Hash show all
Includes:
Config
Defined in:
lib/rubymta/item_of_mail.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail = nil) ⇒ ItemOfMail

Returns a new instance of ItemOfMail.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubymta/item_of_mail.rb', line 12

def initialize(mail=nil)
  if mail
    # clone, if a mail is provided
    mail.each {|k,v| self[k]=v.deepclone }
  else
    # assign a new message id for new mail
    new_id = []
    new_id[0] = Time.now.tv_sec.to_b(MessageIdBase)
    new_id[1] = ("00000"+(2176782336*rand).to_i.to_b(MessageIdBase))[-6..-1]
    new_id[2] = ("00"+(Time.now.usec/1000).to_i.to_b(MessageIdBase))[-2..-1]
    self[:mail_id] = new_id.join("-")
  end

  # always set the time of creation
  self[:time] = Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
  self[:saved] = nil
  self[:accepted] = nil
end

Class Method Details

.retrieve_mail_from_queue_folder(mail_id) ⇒ Object

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rubymta/item_of_mail.rb', line 112

def self::retrieve_mail_from_queue_folder(mail_id)
  m = mail_id.match(/^[0-9A-Za-z]{6}.[0-9A-Za-z]{6}.[0-9A-Za-z]{2}$/)
  raise ArgumentError.new("*666* #{mail_id.inspect} is invalid") if m.nil?

  item = nil
  begin
    # Make sure the file that matches the parcel record exists
    if !File::exist?("#{MailQueue}/#{mail_id}")
      # file missing: mark the parcels none and date them
      parcels = S3DB[:parcels].where(:mail_id=>mail_id).all
      parcels.each do |parcel|
        S3DB[:parcels].where(:id=>parcel[:id]).update(:delivery=>'none', :delivery_at=>Time.now)
      end
      return nil
    end

    tmp = nil; File::open("#{MailQueue}/#{mail_id}","r") { |f| tmp = f.read }
    if tmp.nil?
      LOG.error(mail_id) {"Failed to read data in 'ItemOfMail::retrieve_mail_from_queue_folder'"}
    end

    data = tmp.split("\n")

    # get the number of lines in the hash, and cut that out first
    n = data[0].to_i
    a = data[1..n]
    b = data[n+1..-1]

    # convert the hash
begin
    mail = eval(a.join("\n"))
rescue => e
LOG.error(mail_id) {"--> X800X file=>'#{MailQueue}/#{mail_id}'"}
LOG.error(mail_id) {"--> X800X tmp.size=>#{tmp.size}, tmp.class=>#{tmp.class.name}"}
LOG.error(mail_id) {"--> X800X data.size=>#{data.size}, data.class=>#{data.class.name}"}
LOG.error(mail_id) {"--> X800X e=>#{e.inspect}"}
LOG.error(mail_id) {"--> X800X a=>#{a.inspect}"}
return nil
end

    # create the ItemOfMail structure and insert the text
    item = ItemOfMail::new(mail)
    item[:data][:text] = b
  rescue => e
    LOG.error(mail_id) {e.inspect}
    e.backtrace.each { |line| LOG.error(mail_id) {line} }
  end
  return item
end

Instance Method Details

#insert_parcelsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubymta/item_of_mail.rb', line 56

def insert_parcels
  begin
    return true if !self[:rcptto]
    self[:rcptto].each do |rcptto|
      # make entries into the database for tracking the deliveries
      parcel = {}
      parcel[:id] = nil
      parcel[:contact_id] = self[:contact_id]
      parcel[:mail_id] = self[:mail_id]
      parcel[:from_url] = self[:mailfrom][:url]
      parcel[:to_url] = rcptto[:url]
      parcel[:delivery_msg] = rcptto[:message]
      parcel[:retry_at] = nil
      if self[:accepted] && rcptto[:accepted]
        parcel[:delivery] = rcptto[:delivery].to_s
      else
        parcel[:delivery] = 'none'
        parcel[:delivery_at] = Time.now
      end
      parcel[:created_at] = parcel[:updated_at] = Time.now.strftime("%Y-%m-%d %H:%M:%S")
      rcptto[:parcel_id] = S3DB[:parcels].insert(parcel)
    end
    return true
  rescue => e
    LOG.error(self[:mail_id]) {e.inspect}
    e.backtrace.each { |line| LOG.error(self[:mail_id]) {line} }
    return false
  end
end

#parse_headersObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubymta/item_of_mail.rb', line 31

def parse_headers
  self[:data][:headers] = {}
  header = ""
  self[:data][:text].each do |line|
    case
    when line.nil?
      break
    when line =~ /^[ \t]/
      header << String::new(line)
    when line.empty?
      break
    when !header.empty?
      keyword, value = header.split(":", 2)
      self[:data][:headers][keyword.downcase.gsub("-","_").to_sym] = value.strip if !value.nil?
      header = String::new(line)
    else
      header = String::new(line)
    end
  end
  if !header.empty?
    keyword, value = header.split(":", 2)
    self[:data][:headers][keyword.downcase.gsub("-","_").to_sym] = if !value.nil? then value.strip else "" end
  end
end

#save_mail_into_queue_folderObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubymta/item_of_mail.rb', line 90

def save_mail_into_queue_folder
  # split the ItemOfMail into a hash and a text (which may contain any UTF-8)
  hash = self.dup
  text = hash[:data].delete(:text)
  begin
    # save the mail into the Queue folder
    File::open("#{MailQueue}/#{self[:mail_id]}","w") do |f|
      tmp = hash.pretty_inspect
      f.write("#{tmp.lines.count}\n")
      f.write(tmp)
      f.write("\n")
      text_out = text_in = text.join("\n")
      f.write(text_out.utf8)
    end
    self[:saved] = true
  rescue => e
    LOG.error(self[:mail_id]) {e.inspect}
    e.backtrace.each { |line| LOG.error(self[:mail_id]) {line} }
    return false
  end
end

#update_parcels(parcels) ⇒ Object



86
87
88
# File 'lib/rubymta/item_of_mail.rb', line 86

def update_parcels(parcels)
  parcels.each { |parcel| S3DB[:parcels].where(:id=>parcel[:id]).update(parcel) }
end