matrix.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "maunium.net/go/gomatrix"
  19. )
  20. func (bridge *Bridge) HandleBotInvite(evt *gomatrix.Event) {
  21. intent := bridge.AppService.BotIntent()
  22. resp, err := intent.JoinRoom(evt.RoomID, "", nil)
  23. if err != nil {
  24. bridge.Log.Debugln("Failed to join room", evt.RoomID, "with invite from", evt.Sender)
  25. return
  26. }
  27. members, err := intent.JoinedMembers(resp.RoomID)
  28. if err != nil {
  29. bridge.Log.Debugln("Failed to get members in room", resp.RoomID, "after accepting invite from", evt.Sender)
  30. intent.LeaveRoom(resp.RoomID)
  31. return
  32. }
  33. if len(members.Joined) < 2 {
  34. bridge.Log.Debugln("Leaving empty room", resp.RoomID, "after accepting invite from", evt.Sender)
  35. intent.LeaveRoom(resp.RoomID)
  36. return
  37. }
  38. hasPuppets := false
  39. for mxid, _ := range members.Joined {
  40. if mxid == intent.UserID || mxid == evt.Sender {
  41. continue
  42. } else if _, _, ok := bridge.ParsePuppetMXID(mxid); ok {
  43. hasPuppets = true
  44. continue
  45. }
  46. bridge.Log.Debugln("Leaving multi-user room", resp.RoomID, "after accepting invite from", evt.Sender)
  47. intent.SendNotice(resp.RoomID, "This bridge is user-specific, please don't invite me into rooms with other users.")
  48. intent.LeaveRoom(resp.RoomID)
  49. return
  50. }
  51. if !hasPuppets {
  52. user := bridge.GetUser(evt.Sender)
  53. user.ManagementRoom = resp.RoomID
  54. user.Update()
  55. intent.SendNotice(user.ManagementRoom, "This room has been registered as your bridge management/status room.")
  56. bridge.Log.Debugln(resp.RoomID, "registered as a management room with", evt.Sender)
  57. }
  58. }
  59. func (bridge *Bridge) HandleMembership(evt *gomatrix.Event) {
  60. bridge.Log.Debugln(evt.Content, evt.Content.Membership, evt.GetStateKey())
  61. if evt.Content.Membership == "invite" && evt.GetStateKey() == bridge.AppService.BotMXID() {
  62. bridge.HandleBotInvite(evt)
  63. }
  64. }
  65. func (bridge *Bridge) HandleMessage(evt *gomatrix.Event) {
  66. }