]> git.cworth.org Git - sup/blob - lib/sup/mbox.rb
a4f50462416de8703770ca5629b9bfdb7fb0a6d6
[sup] / lib / sup / mbox.rb
1 require "sup/mbox/loader"
2 require "sup/mbox/ssh-file"
3 require "sup/mbox/ssh-loader"
4
5 module Redwood
6
7 ## some utility functions
8 module MBox
9   BREAK_RE = /^From \S+/
10
11   def read_header f
12     header = {}
13     last = nil
14
15     ## i do it in this weird way because i am trying to speed things up
16     ## when scanning over large mbox files.
17     while(line = f.gets)
18       case line
19       when /^(From):\s+(.*)$/i,
20         /^(To):\s+(.*)$/i,
21         /^(Cc):\s+(.*)$/i,
22         /^(Bcc):\s+(.*)$/i,
23         /^(Subject):\s+(.*)$/i,
24         /^(Date):\s+(.*)$/i,
25         /^(Message-Id):\s+<(.*)>$/i,
26         /^(References):\s+(.*)$/i,
27         /^(In-Reply-To):\s+(.*)$/i,
28         /^(Reply-To):\s+(.*)$/i,
29         /^(List-Post):\s+(.*)$/i,
30         /^(Status):\s+(.*)$/i: header[last = $1] = $2
31
32       ## these next three can occur multiple times, and we want the
33       ## first one
34       when /^(Delivered-To):\s+(.*)$/i,
35         /^(X-Original-To):\s+(.*)$/i,
36         /^(Envelope-To):\s+(.*)$/i: header[last = $1.downcase] ||= $2
37
38       when /^$/: break
39       when /:/: last = nil
40       else
41         header[last] += " " + line.chomp.gsub(/^\s+/, "") if last
42       end
43     end
44     header
45   end
46   
47   def read_body f
48     body = []
49     f.each_line do |l|
50       break if l =~ BREAK_RE
51       body << l.chomp
52     end
53     body
54   end
55
56   module_function :read_header, :read_body
57 end
58 end