Class: Omah

Inherits:
Object
  • Object
show all
Includes:
Library, RXFHelperModule
Defined in:
lib/omah.rb

Instance Method Summary collapse

Methods included from Library

#fetch_file

Constructor Details

#initialize(user: 'user', filepath: '.', options: {xslt: 'listing.xsl', url_base: 'http://localhost/' }, plugins: [], webpath: '/email', debug: false) ⇒ Omah

Returns a new instance of Omah.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/omah.rb', line 25

def initialize(user: 'user', filepath: '.', \
           options: {xslt: 'listing.xsl', url_base: 'http://localhost/' }, 
               plugins: [], webpath: '/email', debug: false)

  
  puts 'inside Omah::initialize' if @debug
  
  @user = user
  @xslt = options[:xslt]
  @css = options[:css]
  @variables ||= {}

  @filepath_user = File.join(filepath, @user)
  @webpath_user = webpath +'/' + @user
  @url_base = options[:url_base] || '.'
  @debug = debug

  puts 'Omah::initialize filepath: ' + filepath.inspect if @debug
  
  FileX.chdir filepath

  puts 'Omah::initialize making directory ' + @filepath_user if @debug
  FileX.mkdir_p @filepath_user 

  FileX.chdir @filepath_user
  
  dailyfile = File.join(@filepath_user, 'dynarexdaily.xml')
  
  x = if FileX.exists? dailyfile then dailyfile

  else
    
    'messages[date, prev_date, next_date]/message(msg_id, tags, from, ' + \
    'to, subject, date, txt_filepath, html_filepath, attachment1, ' + \
    'attachment2, attachment3, attachments)'        

  end

  puts 'Omah::initialize before DynarexDaily' if @debug
  @dd = DynarexDaily.new x, dir_archive: :yearly, debug: @debug   
  puts 'Omah::initialize after DynarexDaily' if @debug
  # is it a new day?
  
  if @dd.records.empty? then
          
    date_yesterday = File.join(@filepath_user, 
                              (Date.today - 1).strftime("%Y/%b/%d").downcase)
    
    @dd.prev_date = File.join(@webpath_user, 
                              (Date.today - 1).strftime("%Y/%b/%d").downcase)
    
    # add the next_day field value to the previous day file
    
    file_yesterday = date_yesterday + '/index.xml'

    if FileX.exists? file_yesterday then
      
      dx_yesterday = Dynarex.new file_yesterday
      dx_yesterday.next_date = File.join(@webpath_user, 
                                    (Date.today).strftime("%Y/%b/%d").downcase)
      dx_yesterday.xslt = options[:archive_xsl] if options[:archive_xsl]
      dx_yesterday.save
    end
    
  end
  
  # intialize plugins
      
  puts 'Omah::initialize before plugins' if @debug
  
  @plugins = plugins.inject([]) do |r, plugin|
    
    name, settings = plugin
    
    puts 'Omah::initialize plugin: ' + name.inspect if @debug
    
    return r if settings[:active] == false and !settings[:active]
    
    klass_name = 'OmahPlugin' + name.to_s.split(/[-_]/)\
        .map{|x| x.capitalize}.join

    r << Kernel.const_get(klass_name).new(settings: settings, 
                                          variables: @variables, debug: @debug)

  end    

end

Instance Method Details

#store(messages) ⇒ Object



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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/omah.rb', line 113

def store(messages)

  messages.each.with_index do |x,i|
    
    email, msg = x
    
    begin
      
      puts "i: %d msg: %s" % [i, msg] if @debug
      subject = msg[:subject] || ''
      
      title = subject.gsub(/\W+/,'-')[0,30].sub(/^-/,'').sub(/-$/,'')

      a = @dd.all.select {|x| x.subject == subject}

      ordinal = a.any? ? '.' + a.length.to_s : ''

      x_file = title + ordinal

      id = msg[:msg_id]

      next if @dd.find_by_msg_id id

      path = archive()

      x_filepath = File.join(path, x_file)            

      puts 'FileX.pwd ' + FileX.pwd if @debug
      puts 'Omah::store before mkdir_p path: ' + path.inspect if @debug
      FileX.mkdir_p path

      if msg[:raw_source] then
        FileX.write File.join(@filepath_user, x_filepath + '.eml'), \
                    msg[:raw_source]
      end

      header = %i(from to subject).inject({}) {|r,x| r.merge(x => msg[x]) }
      filepath = File.join(@filepath_user, x_filepath + '.kvx')
      Kvx.new(header, debug: false).save filepath
      
      txt_filepath = x_filepath + '.txt'
      FileX.write File.join(@filepath_user, txt_filepath), \
                                      text_sanitiser(msg[:body_text].to_s)

      html_filepath = x_filepath + '.html'
      FileX.write File.join(@filepath_user, html_filepath), \
                                      html_sanitiser(msg[:body_html].to_s)
      
      parts_path = []
      
      # save the attachments
      if msg[:attachments].length > 0 then
        
        attachment_path = File.join(path, title + ordinal)
        FileX.mkdir_p attachment_path
        
        msg[:attachments][0..2].each.with_index do |x, i|
          
          name, buffer = x
          parts_path[i] = File.join(attachment_path, name.gsub('/',''))
          begin
            FileX.write File.join(@filepath_user, parts_path[i]), buffer
          rescue
            puts ($!)
          end
          
        end          
        
        h[:attachments] = msg[:attachments].map(&:first)

      end
      
      msg.delete :attachments

      h = msg.merge(txt_filepath: txt_filepath, \
                      html_filepath: html_filepath)
      parts_path.each.with_index do |path, i|
        h.merge!("attachment#{i+1}" => @webpath_user + '/' + path)
      end
      
      if parts_path.any? then
        
        attachments = parts_path.map do |path|
          "<li><a href='%s'>%s</a></li>" % [@webpath_user + '/' + path, 
                                            File.basename(path)]
        end
        
        
html_page= %Q(
<html>
<head>
  <title>#{title}</title>
  <meta content="">
  <style></style>
</head>
<body>
  <iframe src='../#{File.basename(html_filepath)}'></iframe>   
  <h2>attachments</h2>
  <ul>
    #{attachments.join("\n")}
  </ul>
</body>
</html>
)

        FileX.write File.join(attachment_path, 'index.html'), html_page
        h[:html_filepath] = File.join(attachment_path, 'index.html')
      end

      h[:link] = File.join(@url_base, @webpath_user, html_filepath)

      @plugins.each {|x| x.on_newmessage(h) if x.respond_to? :on_newmessage }
      
      @dd.create h
    
    
      # remove the message from the server
      #jr250918 email.delete
      
    rescue
      puts 'Omah::store warning: ' + ($!).inspect
    end

  end
  
  if @xslt then
    
    unless FileX.exists? @xslt then
      FileX.write File.expand_path(@xslt), fetch_file(@xslt)
      FileX.write File.expand_path(@css), fetch_file(@css) if @css and \
                                                        not FileX.exists? @css
    end
    
    @dd.xslt = @xslt

  end
  
  
  doc = @dd.to_doc

  doc.root.xpath('records/message').each do |message|
    
    classify message.element('from')
    classify message.element('to')
    
  end

  @plugins.each do |x| 
    x.on_newmail(messages, doc) if x.respond_to? :on_newmail
  end
  
  FileX.write File.join(@filepath_user, 'dynarexdaily.xml'), \
                                                      doc.xml(pretty: true)
  
end