Basic idea is correct.. also, you can use unset %variable rather than set %variable $false

You have a slight coding error in the raw section
Code:
raw 205:*:{
  if (%ontext) {
    ...
    unset %ontext
  }
  elseif (%onjoin) {
    ...
    unset %onjoin
  }
  elseif (%smatch) {
    ...
    unset %smatch
  }
  haltdef
}