diff --git a/lib/modules/MIME.pmod b/lib/modules/MIME.pmod index 30cd633bd7e7fba1b0322a2b01870e4860e48716..3d28a19e1eabd8b9f7f2a2813578df1886315759 100644 --- a/lib/modules/MIME.pmod +++ b/lib/modules/MIME.pmod @@ -5,19 +5,21 @@ class support { string generate_boundary( ) { - return "'ThIs-RaNdOm-StRiNg-/=_."+random(1000000000)+":"; + return "'ThIs-RaNdOm-StRiNg-/=_."+random( 1000000000 )+":"; } string decode( string data, string encoding ) { - switch(lower_case(encoding||"binary")) { + switch (lower_case( encoding || "binary" )) { case "base64": return decode_base64( data ); case "quoted-printable": return decode_qp( data ); case "x-uue": return decode_uue( data ); - case "7bit": case "8bit": case "binary": + case "7bit": + case "8bit": + case "binary": return data; default: throw(({ "unknown transfer encoding "+encoding+"\n", @@ -27,14 +29,16 @@ class support { string encode( string data, string encoding, void|string filename ) { - switch(lower_case(encoding||"binary")) { + switch (lower_case( encoding || "binary" )) { case "base64": return encode_base64( data ); case "quoted-printable": return encode_qp( data ); case "x-uue": return encode_uue( data, filename ); - case "7bit": case "8bit": case "binary": + case "7bit": + case "8bit": + case "binary": return data; default: throw(({ "unknown transfer encoding "+encoding+"\n", @@ -42,51 +46,52 @@ class support { } } - - - /* rfc1522 */ - array(string) decode_word( string word ) { string charset, encoding, encoded_text; - if(sscanf(word, "=?%[^][ \t()<>@,;:\"\\/?.=]?%[^][ \t()<>@,;:\"\\/?.=]?%s?=", - charset, encoding, encoded_text) == 3) { - switch(lower_case(encoding)) { + if (sscanf( word, + "=?%[^][ \t()<>@,;:\"\\/?.=]?%[^][ \t()<>@,;:\"\\/?.=]?%s?=", + charset, encoding, encoded_text) == 3 ) { + switch (lower_case( encoding )) { case "b": - encoding = "base64"; break; + encoding = "base64"; + break; case "q": - encoding = "quoted-printable"; break; + encoding = "quoted-printable"; + break; default: - throw(({ "invalid rfc1522 encoding "+encoding+"\n", backtrace() })); + throw (({ "invalid rfc1522 encoding "+encoding+"\n", backtrace() })); } - return ({ decode( replace(encoded_text, "_", " "), encoding ), - lower_case(charset) }); + return ({ decode( replace( encoded_text, "_", " " ), encoding ), + lower_case( charset ) }); } else return ({ word, 0 }); } string encode_word( array(string) word, string encoding ) { - if(!encoding || !word[1]) + if (!encoding || !word[1]) return word[0]; - switch(lower_case(encoding)) { + switch (lower_case(encoding)) { case "b": case "base64": - encoding = "base64"; break; + encoding = "base64"; + break; case "q": case "quoted-printable": - encoding = "quoted-printable"; break; + encoding = "quoted-printable"; + break; default: - throw(({ "invalid rfc1522 encoding "+encoding+"\n", backtrace() })); + throw (({ "invalid rfc1522 encoding "+encoding+"\n", backtrace() })); } return "=?"+word[1]+"?"+encoding[0..0]+"?"+ replace( encode( word[0], encoding ), ({ "?", "_" }), ({ "=3F", "=5F" }))+"?="; } - string guess_subtype(string typ) + string guess_subtype( string typ ) { - switch(typ) { + switch (typ) { case "text": return "plain"; case "message": @@ -125,7 +130,7 @@ class Message { void setdata( string data ) { - if(data != decoded_data) { + if (data != decoded_data) { decoded_data = data; encoded_data = 0; } @@ -133,15 +138,15 @@ class Message { string getdata( ) { - if(encoded_data && !decoded_data) - decoded_data=decode(encoded_data, transfer_encoding); + if (encoded_data && !decoded_data) + decoded_data = decode( encoded_data, transfer_encoding ); return decoded_data; } string getencoded( ) { - if(decoded_data && !encoded_data) - encoded_data=encode(decoded_data, transfer_encoding, get_filename( )); + if (decoded_data && !encoded_data) + encoded_data = encode( decoded_data, transfer_encoding, get_filename() ); return encoded_data; } @@ -149,7 +154,8 @@ class Message { { if(encoded_data && !decoded_data) decoded_data = getdata( ); - headers["content-transfer-encoding"]=transfer_encoding=lower_case(encoding); + headers["content-transfer-encoding"] = transfer_encoding = + lower_case( encoding ); encoded_data = 0; } @@ -158,8 +164,12 @@ class Message { param = lower_case(param); params[param] = value; switch(param) { - case "charset": charset = value; break; - case "boundary": boundary = value; break; + case "charset": + charset = value; + break; + case "boundary": + boundary = value; + break; case "name": if(transfer_encoding != "x-uue") break; @@ -177,13 +187,13 @@ class Message { void setdisp_param( string param, string value ) { - param = lower_case(param); + param = lower_case( param ); disp_params[param] = value; - switch(param) { + switch (param) { case "filename": - if(transfer_encoding != "x-uue") + if (transfer_encoding != "x-uue") break; - if(encoded_data && !decoded_data) + if (encoded_data && !decoded_data) decoded_data = getdata( ); encoded_data = 0; break; @@ -210,16 +220,19 @@ class Message { string data; object body_part; - if(dest_type != "string") + if (dest_type != "string") throw(({ "can't cast Message to "+dest_type+"\n", backtrace() })); data = getencoded( ); - if(body_parts) { + if (body_parts) { - if(!boundary) { - if(type != "multipart") { type="multipart"; subtype="mixed"; } - setboundary(generate_boundary()); + if (!boundary) { + if (type != "multipart") { + type = "multipart"; + subtype = "mixed"; + } + setboundary( generate_boundary( ) ); } data += "\r\n"; @@ -230,11 +243,11 @@ class Message { headers["content-length"] = ""+strlen(data); - return map(indices(headers), - lambda(string hname){ + return map( indices(headers), + lambda(string hname){ return replace(map(hname/"-", String.capitalize)*"-", "Mime", "MIME")+ ": "+headers[hname]; - })*"\r\n"+"\r\n\r\n"+data; + } )*"\r\n" + "\r\n\r\n" + data; } void create(void | string message, @@ -254,13 +267,13 @@ class Message { disposition = 0; if (hdrs || parts) { string hname; - if(message) + if (message) decoded_data = message; else decoded_data = (parts? "This is a multi-part message in MIME format.\r\n": ""); - if(hdrs) + if (hdrs) foreach( indices(hdrs), hname ) headers[lower_case(hname)] = hdrs[hname]; body_parts = parts; @@ -274,7 +287,7 @@ class Message { (mesgsep2<0? mesgsep1 : (mesgsep1<mesgsep2? mesgsep1 : mesgsep2))); } - if(mesgsep<0) { + if (mesgsep<0) { head = message; body = ""; } else { @@ -288,7 +301,7 @@ class Message { } encoded_data = body; } - if(headers["content-type"]) { + if (headers["content-type"]) { array(array(string|int)) arr = tokenize(headers["content-type"]) / ({';'}); array(string|int) p; @@ -309,7 +322,7 @@ class Message { charset = lower_case(params["charset"] || charset); boundary = params["boundary"]; } - if(headers["content-disposition"]) { + if (headers["content-disposition"]) { array(array(string|int)) arr = tokenize(headers["content-disposition"]) / ({';'}); array(string|int) p; @@ -318,24 +331,28 @@ class Message { disposition = lower_case(arr[0][0]); foreach( arr[1..], p ) { if(sizeof(p)<3 || p[1]!='=' || !stringp(p[0])) - throw(({ "invalid parameter in Content-Disposition\n", backtrace() })); + throw(({ "invalid parameter in Content-Disposition\n", + backtrace() })); disp_params[ lower_case(p[0]) ] = p[2..]*""; } } - if(headers["content-transfer-encoding"]) { + if (headers["content-transfer-encoding"]) { array(string) arr=tokenize(headers["content-transfer-encoding"]); - if(sizeof(arr)!=1 || !stringp(arr[0])) - throw(({"invalid Content-Transfer-Encoding in message\n", backtrace()})); + if (sizeof(arr)!=1 || !stringp(arr[0])) + throw (({ "invalid Content-Transfer-Encoding in message\n", + backtrace() })); transfer_encoding = lower_case(arr[0]); } - if(boundary && type=="multipart" && !body_parts && + if (boundary && type=="multipart" && !body_parts && (encoded_data || decoded_data)) { array(string) parts = ("\n"+getdata())/("\n--"+boundary); - if(parts[-1][0..3]!="--\n" && parts[-1][0..3]!="--\r\n") + if (parts[-1][0..3]!="--\n" && parts[-1][0..3]!="--\r\n") throw(({ "multipart message improperly terminated\n", backtrace() })); encoded_data = 0; decoded_data = parts[0][1..]; - body_parts = map(parts[1..sizeof(parts)-2], lambda(string part){ return object_program(this_object())(part[1..]); }); + body_parts = map(parts[1..sizeof(parts)-2], lambda(string part){ + return object_program(this_object())(part[1..]); + }); } }