move msgloop into a function

somettimes actions is not available when execution goes into msgloop. Now msgloop should be called in the main script (which is not in this repo).