Here are some improvements I've made over the latest messaging versions. It requires a version update to add a capability. It also adds a setting to allow site admins to force messaging only between users that share a course. Also, I placed a limit on the amount of people that can be returned on a messages user search of 200. All file differences are documented (except version update to allow upgrade of capabilities)
By default all users are given unrestricted messaging rights. That assumes of course that the user can message at all. If they can message and they are unrestricted, they can message as usual. If they are a restricted role, they can message unrestricted roles, but they can't message other restricted roles. On our site, we denote our teachers on the site level as "Course Creators". They are given unrestricted status, but all other authenticated users are restricted. Then we force the course only messaging setting. This means teachers can message anyone, but students can only message their teacher.