Class: Whisper::Mbox

Inherits:
Object
  • Object
show all
Includes:
Loggy
Defined in:
lib/whisper/mbox.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn, start_offset) ⇒ Mbox

Returns a new instance of Mbox.



22
23
24
25
26
# File 'lib/whisper/mbox.rb', line 22

def initialize fn, start_offset
  @fn = fn
  @offset = start_offset
  @mutex = Mutex.new
end

Instance Attribute Details

#offsetObject (readonly)

Returns the value of attribute offset.



20
21
22
# File 'lib/whisper/mbox.rb', line 20

def offset
  @offset
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/whisper/mbox.rb', line 28

def eof?
  @mutex.synchronize { @offset >= File.size(@fn) }
end

#next_messageObject



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
# File 'lib/whisper/mbox.rb', line 32

def next_message
  m = @mutex.synchronize do
    debug "reading #{@fn}@#{@offset}..."
    with_filehandle do |f|
      string = ""
      l = f.gets
      string << l until f.eof? || is_break_line?(l = f.gets)
      debug "read #{string.size} bytes"
      return nil if string.empty?
      @offset += string.size
      begin
        RMail::Parser.read string
      rescue ArgumentError # sigh. invalid utf throws this completely generic exception
        nil
      end
    end
  end

  return if m.nil? # hit EOF

  headers = {
    "message-id" => m.header["message-id"],
    "in-reply-to" => m.header["in-reply-to"],
    "date" => m.header["date"],
    "from" => Rfc2047.decode_to("utf-8", m.header["from"]),
  }

  text_part = find_text_part m
  debug "text part of message has size #{text_part.body.size}: #{text_part.body[0..50].inspect}..."
  body = if text_part
    x = text_part.decode
    x = Iconv.easy_decode("utf-8", text_part.charset, x) if text_part.charset
    munge_body x
  else
    "_[This message contains no text/plain part. Bad commenter, no cookie! --ed.]_\n"
  end

  [body, headers]
end