2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2018-08-13 00:00:23 +02:00
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
import (
|
2018-08-16 14:59:18 +02:00
|
|
|
"maunium.net/go/gomatrix"
|
2018-08-13 22:24:44 +02:00
|
|
|
)
|
2018-08-13 00:00:23 +02:00
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
func (bridge *Bridge) HandleBotInvite(evt *gomatrix.Event) {
|
|
|
|
intent := bridge.AppService.BotIntent()
|
2018-08-16 14:59:18 +02:00
|
|
|
|
2018-08-16 18:20:07 +02:00
|
|
|
resp, err := intent.JoinRoom(evt.RoomID, "", nil)
|
2018-08-16 14:59:18 +02:00
|
|
|
if err != nil {
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.Log.Debugln("Failed to join room", evt.RoomID, "with invite from", evt.Sender)
|
2018-08-16 14:59:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 18:20:07 +02:00
|
|
|
members, err := intent.JoinedMembers(resp.RoomID)
|
2018-08-16 14:59:18 +02:00
|
|
|
if err != nil {
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.Log.Debugln("Failed to get members in room", resp.RoomID, "after accepting invite from", evt.Sender)
|
2018-08-16 18:20:07 +02:00
|
|
|
intent.LeaveRoom(resp.RoomID)
|
2018-08-16 14:59:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(members.Joined) < 2 {
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.Log.Debugln("Leaving empty room", resp.RoomID, "after accepting invite from", evt.Sender)
|
2018-08-16 18:20:07 +02:00
|
|
|
intent.LeaveRoom(resp.RoomID)
|
2018-08-16 14:59:18 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-16 23:11:28 +02:00
|
|
|
|
|
|
|
hasPuppets := false
|
2018-08-16 14:59:18 +02:00
|
|
|
for mxid, _ := range members.Joined {
|
2018-08-16 18:20:07 +02:00
|
|
|
if mxid == intent.UserID || mxid == evt.Sender {
|
2018-08-16 14:59:18 +02:00
|
|
|
continue
|
2018-08-16 23:11:28 +02:00
|
|
|
} else if _, _, ok := bridge.ParsePuppetMXID(mxid); ok {
|
|
|
|
hasPuppets = true
|
2018-08-16 14:59:18 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-08-16 23:11:28 +02:00
|
|
|
bridge.Log.Debugln("Leaving multi-user room", resp.RoomID, "after accepting invite from", evt.Sender)
|
2018-08-16 18:20:07 +02:00
|
|
|
intent.SendNotice(resp.RoomID, "This bridge is user-specific, please don't invite me into rooms with other users.")
|
|
|
|
intent.LeaveRoom(resp.RoomID)
|
2018-08-16 14:59:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
if !hasPuppets {
|
|
|
|
user := bridge.GetUser(evt.Sender)
|
|
|
|
user.ManagementRoom = resp.RoomID
|
|
|
|
user.Update()
|
|
|
|
intent.SendNotice(user.ManagementRoom, "This room has been registered as your bridge management/status room.")
|
|
|
|
bridge.Log.Debugln(resp.RoomID, "registered as a management room with", evt.Sender)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
func (bridge *Bridge) HandleMembership(evt *gomatrix.Event) {
|
|
|
|
bridge.Log.Debugln(evt.Content, evt.Content.Membership, evt.GetStateKey())
|
|
|
|
if evt.Content.Membership == "invite" && evt.GetStateKey() == bridge.AppService.BotMXID() {
|
|
|
|
bridge.HandleBotInvite(evt)
|
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
func (bridge *Bridge) HandleMessage(evt *gomatrix.Event) {
|
2018-08-13 00:00:23 +02:00
|
|
|
}
|