I'm sure there's plenty of scripts around that can do this for you. I wrote one years ago that does pretty much everything you've said.. (http://www.bekar.id.au/~bekar/mirc/scripts/away.mrc)

If that's no joy, it should give you the idea of what's needed.