Class: Gurgitate::Headers
- Inherits:
-
Object
- Object
- Gurgitate::Headers
- Defined in:
- lib/gurgitate/headers.rb
Overview
A slightly bigger class for all of a message’s headers
Direct Known Subclasses
Instance Method Summary collapse
-
#[](*names) ⇒ Object
- Grab the headers with names
names
names -
The names of the header.
- Grab the headers with names
-
#[]=(name, value) ⇒ Object
- Set the header named
name
tovalue
name -
The name of the header.
- Set the header named
-
#from ⇒ Object
Who the message is from (the envelope from).
-
#from=(newfrom) ⇒ Object
Change the envelope from line to whatever you want.
-
#initialize(headertext = nil, sender = nil, recipient = nil) ⇒ Headers
constructor
Creates a Headers object.
-
#match(name, regex) ⇒ Object
- Match header
name
againstregex
name - A string containing the name of the header to match (for example, “From”) regex
-
The regex to match it against (for example, /@aol.com/).
- Match header
-
#matches(names, regex) ⇒ Object
- Return true if headers
names
matchregex
names - An array of header names (for example, %wReply-To) regex
-
The regex to match the headers against.
- Return true if headers
-
#to ⇒ Object
Who the message is to (the envelope to).
-
#to_mbox ⇒ Object
Returns the headers properly formatted for an email message.
-
#to_s ⇒ Object
Returns the headers formatted for an email message (without the “From ” line.
Constructor Details
#initialize(headertext = nil, sender = nil, recipient = nil) ⇒ Headers
Creates a Headers object.
- headertext
-
The text of the message headers.
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 |
# File 'lib/gurgitate/headers.rb', line 162 def initialize(headertext=nil, sender=nil, recipient=nil) @from = sender @to = recipient @headers = Hash.new(nil) if Hash === headertext @headers_changed = true headertext.each_key do |key| headername = key.to_s.gsub("_","-") header=Header.new(headername, headertext[key]) @headers[header.name] ||= HeaderBag.new @headers[header.name].push(header) end else if headertext @unix_from, @headertext = figure_out_from_line headertext parse_headers if @headertext if sender # then don't believe the mbox separator @from = sender @unix_from="From "+self.from+" "+Time.new.to_s else guess_sender end end end end |
Instance Method Details
#[](*names) ⇒ Object
Grab the headers with names names
- names
-
The names of the header.
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/gurgitate/headers.rb', line 194 def [](*names) if names.inject(false) do |accum,name| accum or @headers.has_key? name end then return HeaderBag.new(names.collect { |name| @headers[name] }.flatten.delete_if { |e| e == nil } ) else return nil end end |
#[]=(name, value) ⇒ Object
Set the header named name
to value
- name
-
The name of the header.
- value
-
The new value of the header.
209 210 211 212 |
# File 'lib/gurgitate/headers.rb', line 209 def []=(name,value) @headers_changed = true @headers[name]=HeaderBag.new([Header.new(name,value)]) end |
#from ⇒ Object
Who the message is from (the envelope from)
222 223 224 |
# File 'lib/gurgitate/headers.rb', line 222 def from return @from || "" end |
#from=(newfrom) ⇒ Object
Change the envelope from line to whatever you want. This might not be particularly neighborly, but oh well.
- newfrom
-
An email address
229 230 231 232 |
# File 'lib/gurgitate/headers.rb', line 229 def from=(newfrom) @from=newfrom @unix_from="From "+self.from+" "+Time.new.to_s end |
#match(name, regex) ⇒ Object
Match header name
against regex
- name
-
A string containing the name of the header to match (for example, “From”)
- regex
-
The regex to match it against (for example, /@aol.com/)
239 240 241 242 243 244 245 246 247 |
# File 'lib/gurgitate/headers.rb', line 239 def match(name,regex) ret=false if(@headers[name]) then @headers[name].each do |h| ret |= h.matches(regex) end end return ret end |
#matches(names, regex) ⇒ Object
Return true if headers names
match regex
- names
-
An array of header names (for example, %wReply-To)
- regex
-
The regex to match the headers against.
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/gurgitate/headers.rb', line 252 def matches(names,regex) ret=false if names.class == String then names=[names] end names.each do |n| ret |= match(n,regex) end return ret end |
#to ⇒ Object
Who the message is to (the envelope to)
Yet another bucket of rants. Unix mail sucks.
217 218 219 |
# File 'lib/gurgitate/headers.rb', line 217 def to return @to || @headers["X-Original-To"] || nil end |
#to_mbox ⇒ Object
Returns the headers properly formatted for an email message.
267 268 269 |
# File 'lib/gurgitate/headers.rb', line 267 def to_mbox return @unix_from+"\n"+to_s end |
#to_s ⇒ Object
Returns the headers formatted for an email message (without the “From ” line
273 274 275 276 277 278 279 280 281 |
# File 'lib/gurgitate/headers.rb', line 273 def to_s if @headers_changed then return @headers.map do |name,hdr| hdr.map do |hdr_content| hdr_content.to_s end.join("\n") end.join("\n") else return @headertext end end |