Subject: [sup-talk] Strange problem in before-edit hook

From: [email-masked] (Marcus Williams)
Date: Wed, 13 May 2009 16:28:06 +0100

The automatic setting of the from address on a reply works really well
in sup, but I've been trying to get new emails do something similar
with the hook mechanism.

I've got the before-edit hook to do set my from address by picking up
the previously used from address from a search using the sent label.
Basically this means if I send a new email to someone using a specific
from address, it picks up my last used from address as the default the
next time I send them an email.  This saves having to write something
specific for every email/domain I send to.

It works really well but I get the odd crash in the hook because the
header['To'] sometimes is an array rather than the expected string.
I've got around this by doing a to_s on the header but I cant figure
out why I'd ever get an array. Anyone got any ideas?

Heres the hook:

### start of before-edit
fr = nil
emailto = ""

hdrto = header["To"].to_s

hdrto.split(' ').find() { |e| e.include?("@") }.each() do |ee| 
    if ee.include?("<")
        emailto = ee[1..-2] 
    else 
        emailto = ee 
    end 
end

Index.index.search_each("to:\"#{emailto}\" AND label:sent") { |id, score| m = Index.build_message(id); fr = m.from }

header["From"] = fr.full_address unless fr.nil?
header["Return-Path"] = header["From"]
### end of before-edit