<?xjs 
load("sbbsdefs.js");
load("msgutils.js");
sub_code=http_request.query.sub_code[0];
var ANSI_formatted=false;
if(http_request.query.ansi != undefined && http_request.query.ansi[0]=='true')
	ANSI_formatted=true;
msg_number=parseInt(http_request.query.msg_number[0]);
var msgbase=new MsgBase(sub_code);
msgbase.open();
var hdr=msgbase.get_msg_header(msg_number, false /* Speedup */);
var last_mod=new Date();
if(hdr.when_imported_time > file_date(xjs_filename))
	last_mod.setTime(hdr.when_imported_time*1000);
else
	last_mod.setTime(file_date(xjs_filename)*1000);
if(http_request.header['If-Modified-Since'] != undefined) {
	if(last_mod <= new Date(http_request.header['If-Modified-Since'])) {
		http_reply.status="304 Not Modified";
		exit(0);
	}
}
http_reply.header['Last-Modified']=strftime("%a, %d %b %Y %H:%M:%S GMT",last_mod.getTime()/1000);

var body;

function linkify(body) {
	// Magical URL-ify
	urlRE=/(?:https?|ftp|telnet|ssh|gopher|rlogin|news):\/\/[^\s'"'<>()]*|[-\w.+]+@(?:[-\w]+\.)+[\w]{2,6}/gi;
	body=body.replace(urlRE, 
		function (str) {
			var ret=''
			var p=0;
			var link=str.replace(/\.*$/, '');
			var linktext=link;
			if(link.indexOf('://')==-1)
				link='mailto:'+link;
			return('<a href="'+link+'" class="linkified">'+linktext+'</a>'+str.substr(linktext.length));
		}
	);
	return(body);
}

if((!msg_area.sub[sub_code.toLowerCase()].ismoderated) || (hdr.attr & MSG_VALIDATED)!=0) {
	if((hdr.attr & (MSG_PRIVATE|MSG_DELETE))==0) {
		body=msgbase.get_msg_body(true,hdr.offset,hdr);
		body=expand_body(body, system.settings);
	}
}
msgbase.close();
var html_sub_code_string="'"+html_encode(sub_code, true, false, false, false).replace(/'/g,'&#39;')+"'";

if(ANSI_formatted) {
	writeln("<div class=\"ansi_toggle\"><a href=\"#\" onclick=\"load_html(this.parentNode.parentNode, "+html_sub_code_string+");return false\">HTML Mode</a></div>");
	body=html_encode(body, true, false, true, true);
	body=body.replace(/\r?\n+(<\/span>)?$/,'$1');
	body=linkify(body);

	// Get the last line
	var body_m=body.match(/\n([^\n]*)$/);
	if(body_m != null) {
		write("<pre>"+body);
		body_m[1]=body_m[1].replace(/&[^;]*;/g,".");
		body_m[1]=body_m[1].replace(/<[^>]*>/g,"");
		var lenremain=80-body_m[1].length;
		while(lenremain > 0) {
			write("&nbsp;");
			lenremain--;
		}
	}
	else {
		/* If we couldn't get the last line, add a line of 80 columns */
		writeln("<pre>"+body);
		?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</pre><?xjs
	}
}
else {
	if(http_request.cookie.hide_quotes==undefined || http_request.cookie.hide_quotes[0]=='yes')
		var blockquote_start='<div class="quote"><a class="quote-expander" href="#" onclick="toggle_quote(this);return false">Show quote</a><blockquote style="display: none">';
	else
		var blockquote_start='<div class="quote"><a class="quote-expander" href="#" onclick="toggle_quote(this);return false">Hide quote</a><blockquote style="display: block">';
	var blockquote_end='</blockquote></div>';

	writeln("<div class=\"ansi_toggle\"><a href=\"#\" onclick=\"load_ansi(this.parentNode.parentNode, "+html_sub_code_string+");return false\">ANSI Mode</a></div>");
	// Strip CTRL-A
	body=body.replace(/\1./g,'');
	// Strip ANSI
	body=body.replace(/\x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]/g,'');
	body=body.replace(/\x1b[\x40-\x7e]/g,'');
	// Strip unprintable control chars (NULL, BEL, DEL, ESC)
	body=body.replace(/[\x00\x07\x1b\x7f]/g,'');

	// Wrap and encode
	body=word_wrap(body, body.length);
	body=html_encode(body, true, false, false, false);

	// Magical quoting stuff!
	/*
	 * Each /[^ ]{0,3}> / is a quote block
	 */
	var lines=body.split(/\r?\n/);
	var quote_depth=0;
	var prefixes=new Array();
	body='';
	for (l in lines) {
		var line_prefix='';
		var m=lines[l].match(/^((?:\s?[^\s]{0,3}&gt;\s?)+)/);

		if(m!=null) {
			var new_prefixes=m[1].match(/\s?[^\s]{0,3}&gt;\s?/g);
			var p;
			var broken=false;

			line=lines[l];
			
			// If the new length is smaller than the old one, close the extras
			for(p=new_prefixes.length; p<prefixes.length; p++) {
				if(quote_depth > 0) {
					line_prefix = line_prefix + blockquote_end;
					quote_depth--;
				}
				else {
					log("BODY: Depth problem 1");
				}
			}
			for(p in new_prefixes) {
				// Remove prefix from start of line
				line=line.substr(new_prefixes[p].length);

				if(prefixes[p]==undefined) {
					/* New depth */
					line_prefix = line_prefix + blockquote_start;
					quote_depth++;
				}
				else if(broken) {
					line_prefix = line_prefix + blockquote_start;
					quote_depth++;
				}
				else if(prefixes[p].replace(/^\s*(.*?)\s*$/,"$1") != new_prefixes[p].replace(/^\s*(.*?)\s*$/,"$1")) {
					// Close all remaining old prefixes and start one new one
					var o;
					for(o=p; o<prefixes.length && o<new_prefixes.length; o++) {
						if(quote_depth > 0) {
							line_prefix = blockquote_end+line_prefix;
							quote_depth--;
						}
						else {
							log("BODY: Depth problem 2");
						}
					}
					line_prefix = blockquote_start+line_prefix;
					quote_depth++;
					broken=true;
				}
			}
			prefixes=new_prefixes.slice();
			line=line_prefix+line;
		}
		else {
			for(p=0; p<prefixes.length; p++) {
				if(quote_depth > 0) {
					line_prefix = line_prefix + blockquote_end;
					quote_depth--;
				}
				else {
					log("BODY: Depth problem 3");
				}
			}
			prefixes=new Array();
			line=line_prefix + lines[l];
		}
		body = body + line + "\r\n";
	}
	if(quote_depth != 0) {
		log("BODY: Depth problem 4");
		for(;quote_depth > 0; quote_depth--) {
			body += blockquote_end;
		}
	}

	body=linkify(body);
	body=body.replace(/\r\n$/,'');
	body=body.replace(/(\r?\n)/g, "<br>$1");
	write(body);
}?>
