Code:
alias -l _msg {
  inc %_msg_n
  if ($me ison $2) && ($isfile($3-)) && ($read($3-,%_msg_n) != $null) { msg $2 $v1 }
  else { 
    if ($isfile($3-)) && (%_msg_n > $lines($3-)) {
      %_msg_n = 0
      .timer 1 0 _msg $1-
    }
    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 
}