setctx quantum
bind pub n ` execphp
bind pub n ' exectcl

proc subexecphp {arg} {
	set ctx [getctx]
	set i 1
	foreach temps [parparse "&" "$arg"] {
	catch {subexectcl "$temps"} result
	setctx $ctx
		set arg [string map "{<&${temps}&>} {$result}" $arg]
		if {$i == 100} {
			return "Error: inf php loop"
		}
		incr i
	}
	#set filename "temp.php"
	#catch {set fileId [open $filename "w"]}
	#puts -nonewline $fileId "<?php $arg ?>"
	#close $fileId
	#catch {exec php -f $filename} result
	catch {exec php -r \{$arg\}} result
	return $result
}

proc subexectcl {arg} {
	set ctx [getctx]
	set i 1
	foreach temps [parparse "?" $arg] {
	catch {subexecphp "$temps"} result
	setctx $ctx
		set arg [string map "{<?$temps?>} {$result}" $arg]
		if {$i == 100} {
			return "Error: inf tcl loop"
		}
		incr i
	}
	catch {eval $arg} result
	return $result
}

proc exectcl {nick host hand chan arg} {
	set ctx [getctx]
	catch {subexectcl "$arg"} result
	if {$result == ""} { set result "<null>" }
	foreach sline [split $result \n] {
		putserv "PRIVMSG $chan :( $arg ) = $sline"
	}
}

proc execphp {nick host hand chan arg} {
	set ctx [getctx]
	catch {subexecphp "$arg"} result
	if {$result == ""} { set result "<null>" }
	foreach sline [split $result \n] {
		putserv "PRIVMSG $chan :( $arg ) = $sline"
	}
}

proc parparse {chr str} {
	set append 0;
	set level 0;
	set match "";
	set pre "";
	set return "";
	foreach letter [split $str ""] {
		if {$append} {
			append match $letter
		};
		if {$letter == $chr && $pre == "<"} {
			if {$level == 0} {
				set append 1;
				set match "<$chr"
			};
			incr level
		} elseif {$letter == ">" && $pre == $chr} {
			incr level -1;
			if {$level == 0} {
				lappend return [string range $match 2 end-2]
				set match ""
				set append 0
			}
		}
		set pre $letter
	}
	return $return
}

proc phptcltest {} {
subexectcl {
	if {1 == 2} { return "error" } else { putserv "privmsg #quantum :check 1 passed" }
	putserv "privmsg #quantum :<? echo "check 2 passed"; ?>"
	set var "passed"
	putserv "privmsg #quantum :check 3 $var"
	<? echo "putserv \"privmsg #quantum :check 4 passed\""; ?>
	<? echo "putserv \"privmsg #quantum :check 5 passed\""; ?>
	<?
	$hai = "passed";
	echo "putserv \"privmsg #quantum :check 6 $hai\"";
	?>
}
subexecphp {
	$hai = "passed at <& return [unixtime] &>";
	echo "check 7 $hai";
}
}