Class: ICFS::Email::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/icfs/email/basic.rb

Overview

Basic email processing

This looks for ICFS email gateway instructions, and processes attachments.

Constant Summary collapse

StripRx =

Strip regex

/^[^[:graph:]]*([[:graph:]].*[[:graph:]])[^[:graph:]]*$/.freeze
FieldRx =

Fields regex

/^ICFS ([^:[:blank:]]*)[[:blank:]]*:[[:blank:]]*(.*)[[:blank:]]*$/.freeze
StatRx =

Regex for stat

/^([+\-]?\d+(\.\d*)?)[^[:graph:]]+([[:graph:]].*[[:graph:]])$/.freeze

Instance Method Summary collapse

Instance Method Details

#_boolean(str) ⇒ Object

Check for a boolean



46
47
48
49
50
51
52
53
54
55
# File 'lib/icfs/email/basic.rb', line 46

def _boolean(str)
  case str.downcase
  when 'true', 'yes'
    return true
  when 'false', 'no'
    return false
  else
    return nil
  end
end

#_strip(collect) ⇒ Object

Strip spaces from collected lines



35
36
37
38
39
40
# File 'lib/icfs/email/basic.rb', line 35

def _strip(collect)
  collect.map{ |lr|
    ma = StripRx.match(lr) # include wierd UNICODE spaces
    ma ? ma[1] : nil
  }.compact
end

#receive(env) ⇒ Object

Look for instructions in the email and process them



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
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
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
# File 'lib/icfs/email/basic.rb', line 71

def receive(env)

  # we only work on text/plain version of the email
  if env[:msg].multipart?
    txt = env[:msg].text_part
  elsif env[:msg].mime_type == 'text/plain'
    txt = env[:msg]
  end
  return [:continue, nil] if !txt
  lines = txt.decoded.lines

  # User specified values
  collect = nil
  term = nil
  state = nil
  stat_name = nil
  stat_value = nil
  lines.each do |ln|
    # collecting lines
    if collect
      if ln.start_with?(term)
        case state

        when :tags
          tags = _strip(collect)
          unless tags.empty?
            env[:tags] ||= []
            env[:tags] = env[:tags] + tags
          end
          collect = nil

        when :perms
          perms = _strip(collect)
          env[:perms] = perms unless perms.empty?
          collect = nil

        when :stat
          credit = _strip(collect)
          env[:stats] ||= []
          env[:stats] << {
            'name' => stat_name,
            'value' => stat_value,
            'credit' => credit
          }
          collect = nil

        when :content
          cont = collect.map{|lr| lr.delete("\r")}.join('')
          env[:content] = cont unless cont.empty?
          collect = nil

        else
          raise ScriptError
        end
      else
        collect << ln
        next
      end
    end

    next unless ma = FieldRx.match(ln)
    fn = ma[1].downcase

    case fn
    when 'case'
      env[:caseid] = ma[2].strip

    when 'entry'
      enum = ma[2].strip.to_i
      env[:entry] = enum if enum != 0

    when 'title'
      env[:title] = ma[2].strip

    when 'time'
      if env[:user]
        env[:api].user = env[:user]
        tm = ICFS.time_parse(ma[2].strip, env[:api].config)
        env[:time] = tm if tm
      end

    when 'tag'
      env[:tags] ||= []
      env[:tags] << ma[2].strip

    when 'tags'
      collect = []
      state = :tags
      term = 'ICFS'

    when 'perms'
      collect = []
      state = :perms
      term = 'ICFS'

    when 'stat'
      next unless pm = StatRx.match(ma[2].strip)
      stat_name = pm[3]
      stat_value = pm[1].to_f
      collect = []
      state = :stat
      term = 'ICFS'

    when 'content'
      collect = []
      state = :content
      term = ma[2].strip
      term = 'ICFS' if term.empty?

    when 'save_files'
      val = _boolean(ma[2].strip)
      env[:save_files] = val unless val.nil?

    when 'save_original'
      val = _boolean(ma[2].strip)
      env[:save_original] = val unless val.nil?

    when 'save_email'
      val = _boolean(ma[2].strip)
      env[:save_email] = val unless val.nil?
    end

  end

  # time defaults to message date
  unless env[:time]
    env[:time] = env[:msg].date.to_time.to_i
  end

  # title defaults to subject if okay
  unless env[:title]
    # check the subject time
    title = env[:msg].subject
    err = Validate.check(title, Items::FieldTitle)
    env[:title] = title unless err
  end

  # save the edited email defaults to yes
  unless env.key?(:save_email)
    env[:save_email] = true
  end

  # save the raw email defaults to no
  unless env.key?(:save_original)
    env[:save_original] = false
  end

  # save attachments as files
  unless env.key?(:save_files) && !env[:save_files]
    cnt = 0
    env[:msg].attachments.each do |att|
      type = att.header[:content_disposition].disposition_type
      next if type == 'inline'
      cnt += 1
      name = att.filename
      if !name
        ext = MIME::Types[att.content_type].first.extensions.first
        name = 'unnamed_%d.%s' % [cnt, ext]
      end
      env[:files] << { name: name, content: att.decoded }
    end
    env[:msg] = ::Mail.new(env[:msg].without_attachments!.encoded)
  end

  return :continue
end