Class: Rexlite::MIME::Message
- Inherits:
-
Object
- Object
- Rexlite::MIME::Message
- Includes:
- Encoding
- Defined in:
- lib/nexpose/rexlite/mime/message.rb
Instance Attribute Summary collapse
-
#bound ⇒ Object
Returns the value of attribute bound.
-
#content ⇒ Object
Returns the value of attribute content.
-
#header ⇒ Object
Returns the value of attribute header.
-
#parts ⇒ Object
Returns the value of attribute parts.
Instance Method Summary collapse
- #add_part(data = '', content_type = 'text/plain', transfer_encoding = "8bit", content_disposition = nil) ⇒ Object
- #add_part_attachment(data, name) ⇒ Object
- #add_part_inline_attachment(data, name) ⇒ Object
- #encode_base64(str, delim = '') ⇒ Object
- #from ⇒ Object
- #from=(val) ⇒ Object
-
#initialize(data = nil) ⇒ Message
constructor
A new instance of Message.
- #mime_defaults ⇒ Object
- #rand_text_alpha(len, bad = '') ⇒ Object
- #rand_text_alphanumeric(len, bad = '') ⇒ Object
- #subject ⇒ Object
- #subject=(val) ⇒ Object
- #to ⇒ Object
- #to=(val) ⇒ Object
- #to_s ⇒ Object
Methods included from Encoding
Constructor Details
#initialize(data = nil) ⇒ Message
Returns a new instance of Message.
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 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 15 def initialize(data=nil) self.header = Rexlite::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 && ctype[1] && ctype[1] =~ /multipart\/mixed;\s*boundary="?([A-Za-z0-9'\(\)\+\_,\-\.\/:=\?^\s]+)"?/ self.bound = $1 chunks = body.to_s.split(/--#{self.bound}(--)?\r?\n/) self.content = chunks.shift.to_s.gsub(/\s+$/, '') self.content << "\r\n" unless self.content.empty? chunks.each do |chunk| break if chunk == "--" head,body = chunk.split(/\r?\n\r?\n/, 2) part = Rexlite::MIME::Part.new part.header.parse(head) part.content = body.gsub(/\s+$/, '') self.parts << part end else self.content = body.to_s.gsub(/\s+$/, '') + "\r\n" end end end |
Instance Attribute Details
#bound ⇒ Object
Returns the value of attribute bound.
12 13 14 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 12 def bound @bound end |
#content ⇒ Object
Returns the value of attribute content.
12 13 14 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 12 def content @content end |
#header ⇒ Object
Returns the value of attribute header.
12 13 14 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 12 def header @header end |
#parts ⇒ Object
Returns the value of attribute parts.
12 13 14 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 12 def parts @parts end |
Instance Method Details
#add_part(data = '', content_type = 'text/plain', transfer_encoding = "8bit", content_disposition = nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 101 def add_part(data='', content_type='text/plain', transfer_encoding="8bit", content_disposition=nil) part = Rexlite::MIME::Part.new if content_disposition part.header.set("Content-Disposition", content_disposition) end part.header.set("Content-Type", content_type) if content_type if transfer_encoding part.header.set("Content-Transfer-Encoding", transfer_encoding) end part.content = data self.parts << part part end |
#add_part_attachment(data, name) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 119 def (data, name) self.add_part( encode_base64(data, "\r\n"), "application/octet-stream; name=\"#{name}\"", "base64", "attachment; filename=\"#{name}\"" ) end |
#add_part_inline_attachment(data, name) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 129 def (data, name) self.add_part( encode_base64(data, "\r\n"), "application/octet-stream; name=\"#{name}\"", "base64", "inline; filename=\"#{name}\"" ) end |
#encode_base64(str, delim = '') ⇒ Object
138 139 140 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 138 def encode_base64(str, delim='') [str.to_s].pack("m").gsub(/\s+/, delim) end |
#from ⇒ Object
58 59 60 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 58 def from (self.header.find('From') || [nil, nil])[1] end |
#from=(val) ⇒ Object
54 55 56 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 54 def from=(val) self.header.set("From", val) end |
#mime_defaults ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 70 def mime_defaults self.header.set("MIME-Version", "1.0") self.header.set("Content-Type", "multipart/mixed; boundary=\"#{self.bound}\"") self.header.set("Subject", '') # placeholder self.header.set("Date", Time.now.strftime("%a,%e %b %Y %H:%M:%S %z")) self.header.set("Message-ID", "<"+ rand_text_alphanumeric(rand(20)+40)+ "@"+ rand_text_alpha(rand(20)+3)+ ">" ) self.header.set("From", '') # placeholder self.header.set("To", '') # placeholder end |
#rand_text_alpha(len, bad = '') ⇒ Object
94 95 96 97 98 99 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 94 def rand_text_alpha(len, bad='') foo = [] foo += ('A' .. 'Z').to_a foo += ('a' .. 'z').to_a rand_base(len, bad, *foo ) end |
#rand_text_alphanumeric(len, bad = '') ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 86 def rand_text_alphanumeric(len, bad='') foo = [] foo += ('A' .. 'Z').to_a foo += ('a' .. 'z').to_a foo += ('0' .. '9').to_a rand_base(len, bad, *foo ) end |
#subject ⇒ Object
66 67 68 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 66 def subject (self.header.find('Subject') || [nil, nil])[1] end |
#subject=(val) ⇒ Object
62 63 64 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 62 def subject=(val) self.header.set("Subject", val) end |
#to ⇒ Object
46 47 48 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 46 def to (self.header.find('To') || [nil, nil])[1] end |
#to=(val) ⇒ Object
50 51 52 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 50 def to=(val) self.header.set("To", val) end |
#to_s ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/nexpose/rexlite/mime/message.rb', line 143 def to_s header_string = self.header.to_s msg = header_string.empty? ? '' : force_crlf(self.header.to_s + "\r\n") msg << force_crlf(self.content + "\r\n") unless self.content.empty? self.parts.each do |part| msg << force_crlf("--" + self.bound + "\r\n") msg << part.to_s end msg << force_crlf("--" + self.bound + "--\r\n") if self.parts.length > 0 msg end |