All of the suggested code you've been given so far will break if someone sets several modes at once (ie. /mode +im-oa). You might want to take a look at the code I pasted here which should work with any mode set possible, then you can just use the relevant signal events to put your code in.