2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// 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-16 14:59:18 +02:00
|
|
|
|
|
|
|
import (
|
2018-08-24 18:46:14 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-08-16 14:59:18 +02:00
|
|
|
"time"
|
2018-08-24 18:46:14 +02:00
|
|
|
|
|
|
|
"github.com/Rhymen/go-whatsapp"
|
2018-08-16 14:59:18 +02:00
|
|
|
"github.com/skip2/go-qrcode"
|
|
|
|
log "maunium.net/go/maulogger"
|
2018-08-24 18:46:14 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
2018-08-16 23:11:28 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/types"
|
2018-08-23 00:12:26 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
2018-08-16 14:59:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
*database.User
|
2018-08-23 00:12:26 +02:00
|
|
|
Conn *whatsapp_ext.ExtendedConn
|
2018-08-16 14:59:18 +02:00
|
|
|
|
|
|
|
bridge *Bridge
|
2018-08-16 18:20:07 +02:00
|
|
|
log log.Logger
|
2018-08-16 14:59:18 +02:00
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
portalsByMXID map[types.MatrixRoomID]*Portal
|
|
|
|
portalsByJID map[types.WhatsAppID]*Portal
|
2018-08-23 00:12:26 +02:00
|
|
|
portalsLock sync.Mutex
|
2018-08-16 23:11:28 +02:00
|
|
|
puppets map[types.WhatsAppID]*Puppet
|
2018-08-23 00:12:26 +02:00
|
|
|
puppetsLock sync.Mutex
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
func (bridge *Bridge) GetUser(userID types.MatrixUserID) *User {
|
2018-08-16 14:59:18 +02:00
|
|
|
user, ok := bridge.users[userID]
|
|
|
|
if !ok {
|
|
|
|
dbUser := bridge.DB.User.Get(userID)
|
|
|
|
if dbUser == nil {
|
|
|
|
dbUser = bridge.DB.User.New()
|
2018-08-18 21:57:08 +02:00
|
|
|
dbUser.ID = userID
|
2018-08-16 14:59:18 +02:00
|
|
|
dbUser.Insert()
|
|
|
|
}
|
|
|
|
user = bridge.NewUser(dbUser)
|
2018-08-18 21:57:08 +02:00
|
|
|
bridge.users[user.ID] = user
|
|
|
|
if len(user.ManagementRoom) > 0 {
|
|
|
|
bridge.managementRooms[user.ManagementRoom] = user
|
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) GetAllUsers() []*User {
|
|
|
|
dbUsers := bridge.DB.User.GetAll()
|
|
|
|
output := make([]*User, len(dbUsers))
|
|
|
|
for index, dbUser := range dbUsers {
|
2018-08-18 21:57:08 +02:00
|
|
|
user, ok := bridge.users[dbUser.ID]
|
2018-08-16 14:59:18 +02:00
|
|
|
if !ok {
|
|
|
|
user = bridge.NewUser(dbUser)
|
2018-08-18 21:57:08 +02:00
|
|
|
bridge.users[user.ID] = user
|
|
|
|
if len(user.ManagementRoom) > 0 {
|
|
|
|
bridge.managementRooms[user.ManagementRoom] = user
|
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
output[index] = user
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
func (bridge *Bridge) NewUser(dbUser *database.User) *User {
|
|
|
|
return &User{
|
|
|
|
User: dbUser,
|
|
|
|
bridge: bridge,
|
|
|
|
log: bridge.Log.Sub("User").Sub(string(dbUser.ID)),
|
|
|
|
portalsByMXID: make(map[types.MatrixRoomID]*Portal),
|
|
|
|
portalsByJID: make(map[types.WhatsAppID]*Portal),
|
|
|
|
puppets: make(map[types.WhatsAppID]*Puppet),
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
func (user *User) SetManagementRoom(roomID types.MatrixRoomID) {
|
|
|
|
existingUser, ok := user.bridge.managementRooms[roomID]
|
|
|
|
if ok {
|
|
|
|
existingUser.ManagementRoom = ""
|
|
|
|
existingUser.Update()
|
|
|
|
}
|
|
|
|
|
|
|
|
user.ManagementRoom = roomID
|
|
|
|
user.bridge.managementRooms[user.ManagementRoom] = user
|
|
|
|
user.Update()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) SetSession(session *whatsapp.Session) {
|
|
|
|
user.Session = session
|
|
|
|
user.Update()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) Start() {
|
|
|
|
if user.Connect(false) {
|
|
|
|
user.Sync()
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
func (user *User) Connect(evenIfNoSession bool) bool {
|
|
|
|
if user.Conn != nil {
|
|
|
|
return true
|
|
|
|
} else if !evenIfNoSession && user.Session == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
user.log.Debugln("Connecting to WhatsApp")
|
2018-08-23 00:12:26 +02:00
|
|
|
conn, err := whatsapp.NewConn(20 * time.Second)
|
2018-08-16 14:59:18 +02:00
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to connect to WhatsApp:", err)
|
2018-08-18 21:57:08 +02:00
|
|
|
return false
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
user.Conn = whatsapp_ext.ExtendConn(conn)
|
2018-08-18 21:57:08 +02:00
|
|
|
user.log.Debugln("WhatsApp connection successful")
|
2018-08-16 14:59:18 +02:00
|
|
|
user.Conn.AddHandler(user)
|
2018-08-18 21:57:08 +02:00
|
|
|
return user.RestoreSession()
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
func (user *User) RestoreSession() bool {
|
2018-08-16 14:59:18 +02:00
|
|
|
if user.Session != nil {
|
|
|
|
sess, err := user.Conn.RestoreSession(*user.Session)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to restore session:", err)
|
2018-08-18 21:57:08 +02:00
|
|
|
//user.SetSession(nil)
|
|
|
|
return false
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
user.SetSession(&sess)
|
|
|
|
user.log.Debugln("Session restored successfully")
|
|
|
|
return true
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
return false
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
func (user *User) Login(roomID types.MatrixRoomID) {
|
2018-08-16 14:59:18 +02:00
|
|
|
bot := user.bridge.AppService.BotClient()
|
|
|
|
|
|
|
|
qrChan := make(chan string, 2)
|
|
|
|
go func() {
|
|
|
|
code := <-qrChan
|
|
|
|
if code == "error" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
qrCode, err := qrcode.Encode(code, qrcode.Low, 256)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to encode QR code:", err)
|
|
|
|
bot.SendNotice(roomID, "Failed to encode QR code (see logs for details)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.UploadBytes(qrCode, "image/png")
|
|
|
|
if err != nil {
|
|
|
|
user.log.Errorln("Failed to upload QR code:", err)
|
|
|
|
bot.SendNotice(roomID, "Failed to upload QR code (see logs for details)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
bot.SendImage(roomID, string(code), resp.ContentURI)
|
2018-08-16 14:59:18 +02:00
|
|
|
}()
|
|
|
|
session, err := user.Conn.Login(qrChan)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnln("Failed to log in:", err)
|
|
|
|
bot.SendNotice(roomID, "Failed to log in: "+err.Error())
|
|
|
|
qrChan <- "error"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.Session = &session
|
|
|
|
user.Update()
|
|
|
|
bot.SendNotice(roomID, "Successfully logged in. Synchronizing chats...")
|
|
|
|
go user.Sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) Sync() {
|
2018-08-18 21:57:08 +02:00
|
|
|
user.log.Debugln("Syncing...")
|
|
|
|
user.Conn.Contacts()
|
|
|
|
for jid, contact := range user.Conn.Store.Contacts {
|
2018-08-24 18:46:14 +02:00
|
|
|
if strings.HasSuffix(jid, whatsapp_ext.NewUserSuffix) {
|
2018-08-19 17:21:38 +02:00
|
|
|
puppet := user.GetPuppetByJID(contact.Jid)
|
|
|
|
puppet.Sync(contact)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(contact.Notify) == 0 && !strings.HasSuffix(jid, "@g.us") {
|
2018-08-23 00:12:26 +02:00
|
|
|
// No messages sent -> don't bridge
|
2018-08-18 21:57:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-19 17:21:38 +02:00
|
|
|
portal := user.GetPortalByJID(contact.Jid)
|
|
|
|
portal.Sync(contact)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleError(err error) {
|
|
|
|
user.log.Errorln("WhatsApp error:", err)
|
|
|
|
}
|
|
|
|
|
2018-08-24 18:46:14 +02:00
|
|
|
func (user *User) HandleJSONParseError(err error) {
|
|
|
|
user.log.Errorln("WhatsApp JSON parse error:", err)
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:59:18 +02:00
|
|
|
func (user *User) HandleTextMessage(message whatsapp.TextMessage) {
|
2018-08-19 17:21:38 +02:00
|
|
|
user.log.Debugln("Received text message:", message)
|
|
|
|
portal := user.GetPortalByJID(message.Info.RemoteJid)
|
|
|
|
portal.HandleTextMessage(message)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleImageMessage(message whatsapp.ImageMessage) {
|
2018-08-19 17:21:38 +02:00
|
|
|
portal := user.GetPortalByJID(message.Info.RemoteJid)
|
2018-08-23 23:52:06 +02:00
|
|
|
portal.HandleMediaMessage(message.Download, message.Thumbnail, message.Info, message.Type, message.Caption)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleVideoMessage(message whatsapp.VideoMessage) {
|
2018-08-19 17:21:38 +02:00
|
|
|
portal := user.GetPortalByJID(message.Info.RemoteJid)
|
2018-08-23 23:52:06 +02:00
|
|
|
portal.HandleMediaMessage(message.Download, message.Thumbnail, message.Info, message.Type, message.Caption)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleAudioMessage(message whatsapp.AudioMessage) {
|
|
|
|
portal := user.GetPortalByJID(message.Info.RemoteJid)
|
|
|
|
portal.HandleMediaMessage(message.Download, nil, message.Info, message.Type, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleDocumentMessage(message whatsapp.DocumentMessage) {
|
|
|
|
portal := user.GetPortalByJID(message.Info.RemoteJid)
|
|
|
|
portal.HandleMediaMessage(message.Download, message.Thumbnail, message.Info, message.Type, message.Title)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:46:14 +02:00
|
|
|
func (user *User) HandleStreamEvent(stream whatsapp_ext.StreamEvent) {
|
|
|
|
if len(user.ManagementRoom) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch stream.Type {
|
|
|
|
case whatsapp_ext.StreamSleep:
|
|
|
|
user.bridge.AppService.BotIntent().SendNotice(user.ManagementRoom, "WhatsApp client disconnected.")
|
|
|
|
case whatsapp_ext.StreamUpdate:
|
|
|
|
if user.Conn.Info != nil && user.Conn.Info.Phone != nil {
|
|
|
|
user.bridge.AppService.BotIntent().SendNotice(user.ManagementRoom,
|
|
|
|
fmt.Sprintf("WhatsApp v%s client connected from %s %s (OS v%s).",
|
|
|
|
user.Conn.Info.Phone.WaVersion, user.Conn.Info.Phone.DeviceManufacturer, user.Conn.Info.Phone.DeviceModel, user.Conn.Info.Phone.OsVersion))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) HandleConnInfo(info whatsapp_ext.ConnInfo) {
|
|
|
|
if len(user.ManagementRoom) > 0 && len(info.ProtocolVersion) > 0 {
|
|
|
|
user.bridge.AppService.BotIntent().SendNotice(user.ManagementRoom,
|
|
|
|
fmt.Sprintf("WhatsApp v%s client connected from %s %s (OS v%s).",
|
|
|
|
info.Phone.WhatsAppVersion, info.Phone.DeviceManufacturer, info.Phone.DeviceModel, info.Phone.OSVersion))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 19:02:38 +02:00
|
|
|
func (user *User) HandlePresence(info whatsapp_ext.Presence) {
|
|
|
|
puppet := user.GetPuppetByJID(info.SenderJID)
|
|
|
|
switch info.Status {
|
|
|
|
case whatsapp_ext.PresenceUnavailable:
|
|
|
|
puppet.Intent().SetPresence("offline")
|
|
|
|
case whatsapp_ext.PresenceAvailable:
|
2018-08-24 21:06:17 +02:00
|
|
|
if len(puppet.typingIn) > 0 && puppet.typingAt + 15 > time.Now().Unix() {
|
2018-08-24 19:02:38 +02:00
|
|
|
puppet.Intent().UserTyping(puppet.typingIn, false, 0)
|
|
|
|
puppet.typingIn = ""
|
2018-08-24 21:06:17 +02:00
|
|
|
puppet.typingAt = 0
|
2018-08-24 19:02:38 +02:00
|
|
|
} else {
|
|
|
|
puppet.Intent().SetPresence("online")
|
|
|
|
}
|
|
|
|
case whatsapp_ext.PresenceComposing:
|
|
|
|
portal := user.GetPortalByJID(info.JID)
|
|
|
|
puppet.typingIn = portal.MXID
|
2018-08-24 21:06:17 +02:00
|
|
|
puppet.typingAt = time.Now().Unix()
|
2018-08-24 19:02:38 +02:00
|
|
|
puppet.Intent().UserTyping(portal.MXID, true, 15 * 1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 18:46:14 +02:00
|
|
|
func (user *User) HandleMsgInfo(info whatsapp_ext.MsgInfo) {
|
|
|
|
if (info.Command == whatsapp_ext.MsgInfoCommandAck || info.Command == whatsapp_ext.MsgInfoCommandAcks) && info.Acknowledgement == whatsapp_ext.AckMessageRead {
|
|
|
|
portal := user.GetPortalByJID(info.ToJID)
|
|
|
|
if len(portal.MXID) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
intent := user.GetPuppetByJID(info.SenderJID).Intent()
|
|
|
|
user.log.Debugln(info.IDs)
|
|
|
|
for _, id := range info.IDs {
|
|
|
|
msg := user.bridge.DB.Message.GetByJID(user.ID, id)
|
|
|
|
if msg == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := intent.MarkRead(portal.MXID, msg.MXID)
|
|
|
|
if err != nil {
|
|
|
|
user.log.Warnln("Failed to mark message %s as read by %s: %v", msg.MXID, info.SenderJID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:59:18 +02:00
|
|
|
func (user *User) HandleJsonMessage(message string) {
|
2018-08-18 21:57:08 +02:00
|
|
|
user.log.Debugln("JSON message:", message)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|