Rex::MIME::Message can now parse as well as write

git-svn-id: file:///home/svn/framework3/trunk@9257 4d416f70-5f16-0410-b530-b9f4589650da
unstable
HD Moore 2010-05-09 16:53:16 +00:00
parent d33dc27e26
commit 14363ca2e8
2 changed files with 61 additions and 33 deletions

View File

@ -25,7 +25,7 @@ class Header
end
var,val = line.split(':')
self.headers << [ var.strip, val.strip ]
self.headers << [ var.to_s.strip, val.to_s.strip ]
prev = self.headers.length - 1
end
end
@ -73,3 +73,4 @@ class Header
end
end
end

View File

@ -8,11 +8,35 @@ class Message
attr_accessor :header, :parts, :bound, :content
def initialize
def initialize(data=nil)
self.header = Rex::MIME::Header.new
self.parts = []
self.bound = "_Part_#{rand(1024)}_#{rand(0xffffffff)}_#{rand(0xffffffff)}"
self.content = ''
if data
head,body = data.split(/\r?\n\r?\n/, 2)
self.header.parse(head)
ctype = self.header.find('Content-Type')
if ctype and ctype[1] and ctype[1] =~ /multipart\/mixed;\s*boundary=([^\s]+)/
self.bound = $1
chunks = body.to_s.split(/--#{self.bound}(--)?\r?\n/)
self.content = chunks.shift.to_s.gsub(/\s+$/, '')
chunks.each do |chunk|
break if chunk == "--"
head,body = chunk.split(/\r?\n\r?\n/, 2)
part = Rex::MIME::Part.new
part.header.parse(head)
part.content = body.gsub(/\s+$/, '')
self.parts << part
end
else
self.content = body.to_s.gsub(/\s+$/, '')
end
end
end
def to
@ -102,7 +126,9 @@ class Message
msg << part.to_s
end
if self.parts.length > 0
msg << "--" + self.bound + "--\r\n"
end
msg
end
@ -110,3 +136,4 @@ class Message
end
end
end