MIN_DATE = Time.at 0
MAX_DATE = Time.at(2**31-1)
+ HookManager.register "custom-search", <<EOS
+Executes before a string search is applied to the index,
+returning a new search string.
+Variables:
+ subs: The string being searched.
+EOS
+
def initialize dir=BASE_DIR
super
source = SourceManager[entry[:source_id]]
raise "invalid source #{entry[:source_id]}" unless source
- mk_addrs = lambda { |l| l.map { |e,n| "#{n} <#{e}>" } * ', ' }
- mk_refs = lambda { |l| l.map { |r| "<#{r}>" } * ' ' }
- fake_header = {
- 'message-id' => entry[:message_id],
- 'date' => Time.at(entry[:date]),
- 'subject' => entry[:subject],
- 'from' => mk_addrs[[entry[:from]]],
- 'to' => mk_addrs[entry[:to]],
- 'cc' => mk_addrs[entry[:cc]],
- 'bcc' => mk_addrs[entry[:bcc]],
- 'reply-tos' => mk_refs[entry[:replytos]],
- 'references' => mk_refs[entry[:refs]],
- }
-
- m = Message.new :source => source, :source_info => entry[:source_info],
- :labels => entry[:labels],
- :snippet => entry[:snippet]
- m.parse_header fake_header
- m
+ m = Message.new :source => source, :source_info => entry[:source_info],
+ :labels => entry[:labels], :snippet => entry[:snippet]
+
+ mk_person = lambda { |x| Person.new(*x.reverse!) }
+ entry[:from] = mk_person[entry[:from]]
+ entry[:to].map!(&mk_person)
+ entry[:cc].map!(&mk_person)
+ entry[:bcc].map!(&mk_person)
+
+ m.load_from_index! entry
+ m
end
+ def add_message m; sync_message m end
+ def update_message m; sync_message m end
+ def update_message_state m; sync_message m end
+
def sync_message m, opts={}
entry = synchronize { get_entry m.id }
snippet = m.snippet
:source_info => m.source_info,
:date => (entry[:date] || m.date),
:snippet => snippet,
- :labels => labels.uniq,
+ :labels => labels,
:from => (entry[:from] || [m.from.email, m.from.name]),
:to => (entry[:to] || m.to.map { |p| [p.email, p.name] }),
:cc => (entry[:cc] || m.cc.map { |p| [p.email, p.name] }),
:replytos => (entry[:replytos] || m.replytos),
}
- m.labels.each { |l| LabelManager << l }
+ labels.each { |l| LabelManager << l }
synchronize do
index_message m, d, opts
end
true
end
+ private :sync_message
def num_results_for query={}
xapian_query = build_xapian_query query
def parse_query s
query = {}
+ subs = HookManager.run("custom-search", :subs => s) || s
subs = s.gsub(/\b(to|from):(\S+)\b/) do
field, name = $1, $2
if(p = ContactManager.contact_for(name))
field, name = $1, ($3 || $4)
case field
when "filename"
- Redwood::log "filename - translated #{field}:#{name} to attachment:\"#{name.downcase}\""
+ debug "filename: translated #{field}:#{name} to attachment:\"#{name.downcase}\""
"attachment:\"#{name.downcase}\""
when "filetype"
- Redwood::log "filetype - translated #{field}:#{name} to attachment_extension:#{name.downcase}"
+ debug "filetype: translated #{field}:#{name} to attachment_extension:#{name.downcase}"
"attachment_extension:#{name.downcase}"
end
end
if realdate
case field
when "after"
- Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate.end}"
+ debug "chronic: translated #{field}:#{datestr} to #{realdate.end}"
"date:#{realdate.end.to_i}..#{lastdate}"
when "before"
- Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate.begin}"
+ debug "chronic: translated #{field}:#{datestr} to #{realdate.begin}"
"date:#{firstdate}..#{realdate.end.to_i}"
else
- Redwood::log "chronic: translated #{field}:#{datestr} to #{realdate}"
+ debug "chronic: translated #{field}:#{datestr} to #{realdate}"
"date:#{realdate.begin.to_i}..#{realdate.end.to_i}"
end
else
end
def find_docid id
- term_docids(mkterm(:msgid,id)).tap { |x| fail unless x.size <= 1 }.first
+ docids = term_docids(mkterm(:msgid,id))
+ fail unless docids.size <= 1
+ docids.first
end
def find_doc id
m.attachments.each { |a| text << [a, PREFIX['attachment']] }
truncated_date = if m.date < MIN_DATE
- Redwood::log "warning: adjusting too-low date #{m.date} for indexing"
+ debug "warning: adjusting too-low date #{m.date} for indexing"
MIN_DATE
elsif m.date > MAX_DATE
- Redwood::log "warning: adjusting too-high date #{m.date} for indexing"
+ debug "warning: adjusting too-high date #{m.date} for indexing"
MAX_DATE
else
m.date