implement Mime;
include "mime.m";
include "sys.m";
sys: Sys;
EOL : con "\r\n"; # End of Line
BOB: con "--"; # Begining of Boundary
Keyval.to_string(k : self ref Keyval) : string
{
return k.key + "=\"" + k.value + "\"";
}
Disposition.header(d : self ref Disposition) : string
{
a : ref Keyval;
s := "Content-Disposition: " + d.d_type;
for(ats := d.attributes; ats != nil; ats = tl ats) {
a = hd ats;
s += "; " + a.to_string();
}
s += EOL;
if(d.content_type != "")
s += "Content-Type: " + d.content_type + EOL;
return s;
}
Document.bytes(m : self ref Document) : array of byte
{
sys = load Sys Sys->PATH;
content_length := m.payload_size + (len array of byte BOB + len array of byte m.boundary + len array of byte EOL + len array of byte EOL) * (len m.parts) + len array of byte BOB + len array of byte m.boundary + len array of byte ("--" + EOL);
bytes := array[content_length] of byte;
bi := 0;
for(ps := m.parts; ps != nil; ps = tl ps) {
bytes[bi:] = array of byte BOB;
bi += len array of byte BOB;
bytes[bi:] = array of byte m.boundary;
bi += len array of byte m.boundary;
bytes[bi:] = array of byte EOL;
bi += len array of byte EOL;
bytes[bi:] = array of byte hd ps;
bi += len array of byte hd ps;
bytes[bi:] = array of byte EOL;
bi += len array of byte EOL;
}
bytes[bi:] = array of byte BOB;
bi += len array of byte BOB;
bytes[bi:] = array of byte m.boundary;
bi += len array of byte m.boundary;
bytes[bi:] = array of byte ("--" + EOL);
return bytes;
}
Document.add_part(m : self ref Document, disposition : ref Disposition)
{
header := array of byte (disposition.header() + EOL);
bytes := array[len header + len disposition.data] of byte;
bytes[0:] = header;
bytes[len header:] = disposition.data;
m.parts = bytes :: m.parts;
m.payload_size += len bytes;
}
make_boundary() : string
{
return "X-----------------MIME__-_BoUndary0x0r";
}
new_document() : ref Document
{
d := ref Document;
d.payload_size = 0;
d.boundary = make_boundary();
return d;
}
|