plugins/Plugin.pm: fix a security issue on state

The save_state had a race condiction between check an save. It also didn't save
atomically. This uses a temporary file, and an atomic rename().
plugins/Plugin.pm: fix bad state file handling