Try this:

Code:
alias -l _msg {
  inc %_msg_n
  if ($me ison $2) && ($isfile($3-)) && ($read($3-,%_msg_n) != $null) { msg $2 $v1 }
  else { .timermsg $+ $1 $+ $2 off }
}
on *:text:!add ?*:#:{ write messages.txt $2- }
on *:text:!enable:#:{ 
  unset %_msg_n
  .timermsg $+ $cid $+ $chan 0 900 _msg $cid $chan messages.txt 
}