1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
237
238
|
require "gdbm"
require "fileutils"
require "tempfile"
require "time"
module Puke
class << self
attr_reader :datadir, :listen_address, :listen_port, :secret
def verbose?() @verbose end
end
module_function
def setup(verbose: false, datadir:, listen:, secret: nil)
@verbose = verbose
@datadir = datadir
l = listen.split(":")
@listen_address = l[0]
@listen_port = Integer(l[1])
@secret = secret
FileUtils.mkdir_p(@datadir)
FileUtils.mkdir_p(File.join(@datadir, "tmp"))
gpath = File.join(@datadir, "puke.gdbm")
@gdbm = GDBM.new(gpath, 0644, GDBM::WRCREAT)
end
def close
@gdbm.close
@gdbm = :closed
end
def tmpdir
File.join(datadir, "tmp")
end
# Returns the largest tid ever, or -1 if no messages are stored
def last_tid
@gdbm.fetch("last-tid", "-1").to_i
end
def thread(tid)
@gdbm.fetch("t#{tid}").split("\0")
end
def tid_for(mid)
m = Puke.metadata(mid) and m[:subject] and m[:tid]
end
# Note that the value for key :subject may be nil if we don't have the
# actual message
def metadata(mid)
data = @gdbm["m#{mid}"] or return
tid, date, subject = data.split("\0")
date = Time.rfc2822(date)
{ mid: mid, tid: tid.to_i, date: date, subject: subject }
end
def open(mid, &blk)
File.open(File.join(datadir, mangle_mid(mid) + ".mbox"), &blk)
end
CreateMutex = Thread::Mutex.new
def create0(file)
mid, subject, date, references, in_reply_to = parse_file(file)
CreateMutex.synchronize {
if existing = Puke.metadata(mid) and existing[:subject]
# TODO: Found a non-dummy existing entry. How to deal with this case?
raise "duplicate Message-ID; existing=#{existing[:subject]} and " \
"new=#{subject}"
end
tid = existing[:tid] if existing
tid = Puke.metadata(in_reply_to)&.fetch(:tid) if !tid && in_reply_to
references.each { |i|
tid = Puke.metadata(i)&.fetch(:tid) and break
} if !tid
if tid
# Yucks
new_mids = Puke.thread(tid)
.reject { |m| m == mid }
.map { |m| Puke.metadata(m).values_at(:date, :subject, :mid) }
.push([date, subject, mid])
.sort
.map { |i| i.last }
else
tid = Puke.last_tid + 1
@gdbm["last-tid"] = tid.to_s
new_mids = [mid]
end
[references.first, in_reply_to].compact.each do |i|
next if new_mids.include?(i)
if Puke.metadata(i)
# Hmm, the possible-parent message belongs to another thread.
# TODO: Do we want to unite them?
else
new_mids.unshift(i)
@gdbm["m#{i}"] = [tid, Time.utc(0).rfc2822].join("\0")
end
end
@gdbm["t#{tid}"] = new_mids.join("\0")
@gdbm["m#{mid}"] = [tid, date.rfc2822, subject].join("\0")
File.link(file, File.join(datadir, mangle_mid(mid) + ".mbox"))
}
mid
end
def create(io, is_raw = false)
if is_raw
Tempfile.create("unnamed", tmpdir) { |t|
t.binmode
# Is this really correct??
t << "From mboxrd Thu Jan 1 00:00:00 1970\n"
x = nil
io.each_line do |line|
x = line[-1] == "\n"
line.gsub!(/\A(>*)From /, "\\1>From ")
line.gsub!(/\r\n\z/, "\n")
t << line
end
if x
t << "\n\n"
else
t << "\n"
end
t.rewind
create0(t)
}
else
# io is already in mboxrd format and may contain multiple messages
mids = []
begin
t = nil
io.each_line do |line|
# Beginning of a new message
if line.start_with?("From ")
if t
t.rewind
mids << create0(t)
t.close!
end
t = Tempfile.open("unnamed", tmpdir)
t.binmode
end
raise "invalid mbox header" unless t
t << line
end
if t
t.rewind
mids << create0(t)
t.close!
end
ensure
t and not t.closed? and t.close!
end
mids
end
end
def parse_file(io)
# We can assume the input is in mboxrd format
mid = nil
references = []
in_reply_to = nil
subject = nil
date = nil
state = nil
io.each_line do |line|
# We use \0 as a delimiter so don't allow that
line.delete("\0")
if line == "\n"
# End of headers
break
elsif line.start_with?(" ") || line.start_with?("\t")
case state
when nil
# This is invalid and must not happen...
when :subject
subject << line.chomp
when :references
references.concat(line.scan(/(<[^<>]+>)/).flatten)
end
else
h, c = line.split(":", 2)
h.downcase!
c or next # Hmm, invalid header?
c.strip!
case h
when "date"
date = c
state = nil
when "message-id"
next if mid
mid = c
state = nil
when "in-reply-to"
next if in_reply_to
in_reply_to = c
state = nil
when "references"
references.concat(c.scan(/(<[^<>]+>)/).flatten)
state = :references
when "subject"
next if subject
subject = c
state = :subject
else
state = nil
end
end
end
mid or raise "Message-ID not found"
mid = strip_angle_bracket(mid) or raise "Message-ID is invalid"
subject or raise "Subject not found"
date or raise "Date not found"
date = Time.rfc2822(date)
# Ignore errors
in_reply_to and in_reply_to = strip_angle_bracket(in_reply_to)
references.map! { |t| strip_angle_bracket(t) }.compact!
[mid, subject, date, references, in_reply_to]
end
def strip_angle_bracket(t)
/\A<(?<i>.+@.+)>\z/ =~ t and i.ascii_only? or return
i
end
def mangle_mid(mid)
mid.gsub(/[\/_]/, { "/" => "_", "_" => "__" })
end
end
|