Riamus2, you forgot to prefix the mode event with an exclamation mark to keep the client running the code from being triggered.
I think you can do it with a tokenize loop to reduce the byte size quite a bit, but then that won't make much of a difference in workability:
on !*:mode:#:{
if (($nick(#,$me,oh) && $regex($1,/([ims])/g))) {
while ($regml($0)) tokenize 32 $1- $v1
mode # $+(-,$remove($2-,$chr(32)))
}
}