super basic

This commit is contained in:
Karmanyaah Malhotra 2021-02-21 00:58:50 -05:00
parent eafc18099d
commit 7cb9983da8
17 changed files with 965 additions and 926 deletions

View File

@ -20,13 +20,13 @@ import (
// "errors"
"context"
"fmt"
// "math"
"sort"
"strconv"
"strings"
"github.com/Rhymen/go-whatsapp"
"maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix"
@ -34,9 +34,6 @@ import (
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
// "maunium.net/go/mautrix-whatsapp/database"
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
)
type CommandHandler struct {
@ -382,10 +379,10 @@ const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client`
// CommandLogin handles login command
func (handler *CommandHandler) CommandLogin(ce *CommandEvent) {
if !ce.User.Connect(true) {
ce.User.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.")
return
}
// if !ce.User.Connect(true) {
// ce.User.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.")
// return
// }
ce.User.Login(ce)
}
@ -418,8 +415,13 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
// ce.User.log.Warnln("Error while disconnecting after logout:", err)
// }
// ce.User.Conn.RemoveHandlers()
// ce.User.Conn = nil
// ce.User.removeFromJIDMap()
ce.User.Conn.Stop(context.TODO())
ce.User.Conn = nil
ce.User.removeFromJIDMap()
ce.User.Token = ""
ce.User.JID = ""
ce.User.Client = nil
ce.User.Update()
// // TODO this causes a foreign key violation, which should be fixed
// //ce.User.JID = ""
// ce.User.SetSession(nil)
@ -489,15 +491,16 @@ func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) {
const cmdReconnectHelp = `reconnect - Reconnect to WhatsApp`
func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
// if ce.User.Conn == nil {
// if ce.User.Session == nil {
// ce.Reply("No existing connection and no session. Did you mean `login`?")
// } else {
// ce.Reply("No existing connection, creating one...")
// ce.User.Connect(false)
// }
// return
// }
fmt.Println(ce.User.Conn)
if ce.User.Conn == nil {
if len(ce.User.Token) == 0 {
ce.Reply("No existing connection and no token. Did you mean `login`?")
} else {
ce.Reply("No existing connection, creating one...")
ce.User.Connect()
}
return
}
// wasConnected := true
// sess, err := ce.User.Conn.Disconnect()
@ -750,18 +753,18 @@ func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) {
const cmdListHelp = `list <contacts|groups> [page] [items per page] - Get a list of all contacts and groups.`
func formatContacts(contacts bool, input map[string]whatsapp.Contact) (result []string) {
for jid, contact := range input {
if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) != contacts {
continue
}
if contacts {
result = append(result, fmt.Sprintf("* %s / %s - `%s`", contact.Name, contact.Notify, contact.Jid[:len(contact.Jid)-len(whatsappExt.NewUserSuffix)]))
} else {
result = append(result, fmt.Sprintf("* %s - `%s`", contact.Name, contact.Jid))
}
}
sort.Sort(sort.StringSlice(result))
// for jid, contact := range input {
// if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) != contacts {
// continue
// }
//
// if contacts {
// result = append(result, fmt.Sprintf("* %s / %s - `%s`", contact.Name, contact.Notify, contact.Jid[:len(contact.Jid)-len(whatsappExt.NewUserSuffix)]))
// } else {
// result = append(result, fmt.Sprintf("* %s - `%s`", contact.Name, contact.Jid))
// }
// }
// sort.Sort(sort.StringSlice(result))
return
}

View File

@ -17,6 +17,8 @@
package database
import (
"strings"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
@ -26,6 +28,7 @@ import (
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
type Database struct {
@ -52,6 +55,9 @@ func New(dbType string, uri string, baseLog log.Logger) (*Database, error) {
print("no")
gdb, err := gorm.Open(conn, &gorm.Config{
// Logger: baseLog,
NamingStrategy: schema.NamingStrategy{
NameReplacer: strings.NewReplacer("JID", "Jid", "MXID", "Mxid"),
},
})
if err != nil {
panic("failed to connect database")
@ -81,6 +87,7 @@ func New(dbType string, uri string, baseLog log.Logger) (*Database, error) {
}
func (db *Database) Init() error {
println("actual upgrade")
err := db.AutoMigrate(&Portal{})
if err != nil {
return err
@ -104,6 +111,14 @@ func (db *Database) Init() error {
return err
}
err = db.AutoMigrate(&User{})
if err != nil {
return err
}
err = db.AutoMigrate(&UserPortal{})
if err != nil {
return err
}
return upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB)
}

View File

@ -46,8 +46,8 @@ func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) {
func (mq *MessageQuery) GetByJID(chat PortalKey, jid types.WhatsAppMessageID) *Message {
var message Message
ans := mq.db.Where("chat_jid = ? AND chat_receiver = ? AND jid = ?", chat.JID, chat.Receiver, jid).Take(&message)
if ans.Error != nil {
ans := mq.db.Where("chat_jid = ? AND chat_receiver = ? AND jid = ?", chat.JID, chat.Receiver, jid).Limit(1).Find(&message)
if ans.Error != nil || ans.RowsAffected == 0 {
return nil
}
return &message
@ -55,8 +55,8 @@ func (mq *MessageQuery) GetByJID(chat PortalKey, jid types.WhatsAppMessageID) *M
func (mq *MessageQuery) GetByMXID(mxid id.EventID) *Message {
var message Message
ans := mq.db.Where("mxid = ?", mxid).Take(&message)
if ans.Error != nil {
ans := mq.db.Where("mxid = ?", mxid).Limit(1).Find(&message)
if ans.Error != nil || ans.RowsAffected == 0 {
return nil
}
return &message
@ -64,8 +64,8 @@ func (mq *MessageQuery) GetByMXID(mxid id.EventID) *Message {
func (mq *MessageQuery) GetLastInChat(chat PortalKey) *Message {
var message Message
ans := mq.db.Where("chat_jid = ? AND chat_receiver = ?", chat.JID, chat.Receiver).Order("timestamp desc").First(&message)
if ans.Error != nil {
ans := mq.db.Where("chat_jid = ? AND chat_receiver = ?", chat.JID, chat.Receiver).Order("timestamp desc").Limit(1).Find(&message)
if ans.Error != nil || ans.RowsAffected == 0 {
return nil
}
return &message
@ -77,7 +77,7 @@ type Message struct {
log log.Logger
Chat PortalKey `gorm:"primaryKey;embedded;embeddedPrefix:chat_"`
JID types.WhatsAppMessageID `gorm:"primaryKey"`
JID types.GroupMeID `gorm:"primaryKey"`
MXID id.EventID `gorm:"unique;notNull"`
Sender types.GroupMeID `gorm:"notNull"`
Timestamp uint64 `gorm:"notNull;default:0"`

View File

@ -73,7 +73,7 @@ func (pq *PortalQuery) GetAll() []*Portal {
}
func (pq *PortalQuery) GetByJID(key PortalKey) *Portal {
return pq.get(pq.db.DB.Where("jit = ? AND receiver = ?", key.JID, key.Receiver))
return pq.get(pq.db.DB.Where("jid = ? AND receiver = ?", key.JID, key.Receiver))
}
@ -97,16 +97,23 @@ func (pq *PortalQuery) getAll(db *gorm.DB) (portals []*Portal) {
if ans.Error != nil || len(portals) == 0 {
return nil
}
for _, i := range portals {
i.db = pq.db
i.log = pq.log
}
return
}
func (pq *PortalQuery) get(db *gorm.DB) *Portal {
var portal Portal
ans := db.Take(&portal)
if ans.Error != nil {
ans := db.Limit(1).Find(&portal)
if ans.Error != nil || db.RowsAffected == 0 {
return nil
}
portal.db = pq.db
portal.log = pq.log
return &portal
}
@ -155,8 +162,7 @@ func (portal *Portal) Insert() {
}
func (portal *Portal) Update() {
ans := portal.db.Model(&portal).Updates(portal)
ans := portal.db.Where("jid = ? AND receiver = ?", portal.Key.JID, portal.Key.Receiver).Save(&portal)
print("check .model vs not")
if ans.Error != nil {
@ -172,6 +178,7 @@ func (portal *Portal) Delete() {
}
func (portal *Portal) GetUserIDs() []id.UserID {
println("HI AAAAAAAAAAAAAAAAAAAa")
rows, err := portal.db.Raw(`SELECT "user".mxid FROM "user", user_portal
WHERE "user".jid=user_portal.user_jid
AND user_portal.portal_jid=$1

View File

@ -53,8 +53,8 @@ func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
func (pq *PuppetQuery) Get(jid types.GroupMeID) *Puppet {
puppet := Puppet{}
ans := pq.db.Where("jid = ?", jid).Take(puppet)
if ans.Error != nil {
ans := pq.db.Where("jid = ?", jid).Limit(1).Find(&puppet)
if ans.Error != nil || ans.RowsAffected == 0 {
return nil
}
return &puppet
@ -62,8 +62,8 @@ func (pq *PuppetQuery) Get(jid types.GroupMeID) *Puppet {
func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet {
puppet := Puppet{}
ans := pq.db.Where("custom_mxid = ?", mxid).Take(puppet)
if ans.Error != nil {
ans := pq.db.Where("custom_mxid = ?", mxid).Limit(1).Find(&puppet)
if ans.Error != nil || ans.RowsAffected == 0 {
return nil
}
return &puppet
@ -93,7 +93,7 @@ type Puppet struct {
Displayname string
NameQuality int8
CustomMXID id.UserID
CustomMXID id.UserID `gorm:"column:custom_mxid;"`
AccessToken string
NextBatch string
EnablePresence bool `gorm:"notNull;default:true"`

View File

@ -55,7 +55,8 @@ func NewSQLStateStore(db *Database) *SQLStateStore {
func (store *SQLStateStore) IsRegistered(userID id.UserID) bool {
v := mxRegistered{UserID: userID.String()}
ans := store.db.First(&v)
var count int64
ans := store.db.Model(&mxRegistered{}).Where(&v).Count(&count)
if errors.Is(ans.Error, gorm.ErrRecordNotFound) {
return false
@ -63,7 +64,7 @@ func (store *SQLStateStore) IsRegistered(userID id.UserID) bool {
if ans.Error != nil {
store.log.Warnfln("Failed to scan registration existence for %s: %v", userID, ans.Error)
}
return true
return count >= 1
}
func (store *SQLStateStore) MarkRegistered(userID id.UserID) {
@ -113,7 +114,7 @@ func (store *SQLStateStore) GetRoomMembers(roomID id.RoomID) map[id.UserID]*even
func (store *SQLStateStore) GetMembership(roomID id.RoomID, userID id.UserID) event.Membership {
var user mxUserProfile
ans := store.db.Where("room_id = ? AND user_id = ?", roomID, userID).Take(&user)
ans := store.db.Where("room_id = ? AND user_id = ?", roomID, userID).Limit(1).Find(&user)
membership := event.MembershipLeave
if ans.Error != nil && ans.Error != gorm.ErrRecordNotFound {
store.log.Warnfln("Failed to scan membership of %s in %s: %v", userID, roomID, ans.Error)
@ -197,8 +198,8 @@ func (store *SQLStateStore) SetMembership(roomID id.RoomID, userID id.UserID, me
print("weird thing 2 502650285")
print(user.Membership)
ans := store.db.Select("roomID", "userID", "membership").Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "roomID"}, {Name: "userID"}},
ans := store.db.Debug().Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "room_id"}, {Name: "user_id"}},
DoUpdates: clause.AssignmentColumns([]string{"membership"}),
}).Create(&user)
@ -217,7 +218,7 @@ func (store *SQLStateStore) SetMember(roomID id.RoomID, userID id.UserID, member
AvatarURL: string(member.AvatarURL),
}
ans := store.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "roomID"}, {Name: "userID"}},
Columns: []clause.Column{{Name: "room_id"}, {Name: "user_id"}},
DoUpdates: clause.AssignmentColumns([]string{"membership"}),
}).Create(&user)

View File

@ -17,15 +17,12 @@
package database
import (
"database/sql"
"strings"
"github.com/Rhymen/go-whatsapp"
"time"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix-whatsapp/types"
whatsappExt "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
"maunium.net/go/mautrix/id"
)
@ -42,72 +39,77 @@ func (uq *UserQuery) New() *User {
}
func (uq *UserQuery) GetAll() (users []*User) {
// rows, err := uq.db.Query(`SELECT mxid, jid, management_room, last_connection, client_id, client_token, server_token, enc_key, mac_key FROM "user"`)
// if err != nil || rows == nil {
// return nil
// }
// defer rows.Close()
// for rows.Next() {
// users = append(users, uq.New().Scan(rows))
// }
ans := uq.db.Find(&users)
if ans.Error != nil || len(users) == 0 {
return nil
}
for _, i := range users {
i.db = uq.db
i.log = uq.log
}
return
}
func (uq *UserQuery) GetByMXID(userID id.UserID) *User {
// row := uq.db.QueryRow(`SELECT mxid, jid, management_room, last_connection, client_id, client_token, server_token, enc_key, mac_key FROM "user" WHERE mxid=$1`, userID)
// if row == nil {
// return nil
// }
// return uq.New().Scan(row)
var user User
ans := uq.db.Where("mxid = ?", userID).Take(&user)
user.db = uq.db
user.log = uq.log
if ans.Error != nil {
return nil
}
return &user
}
func (uq *UserQuery) GetByJID(userID types.GroupMeID) *User {
// row := uq.db.QueryRow(`SELECT mxid, jid, management_room, last_connection, client_id, client_token, server_token, enc_key, mac_key FROM "user" WHERE jid=$1`, stripSuffix(userID))
// if row == nil {
// return nil
// }
// return uq.New().Scan(row)
var user User
ans := uq.db.Where("jid = ?", userID).Limit(1).Find(&user)
if ans.Error != nil || ans.RowsAffected == 0 {
return nil
}
user.db = uq.db
user.log = uq.log
return &user
}
type User struct {
db *Database
log log.Logger
MXID id.UserID
JID types.GroupMeID
MXID id.UserID `gorm:"primaryKey"`
JID types.GroupMeID `gorm:"unique"`
Token types.AuthToken
ManagementRoom id.RoomID
LastConnection uint64
LastConnection uint64 `gorm:"notNull;default:0"`
}
func (user *User) Scan(row Scannable) *User {
var jid, clientID, clientToken, serverToken sql.NullString
var encKey, macKey []byte
err := row.Scan(&user.MXID, &jid, &user.ManagementRoom, &user.LastConnection, &clientID, &clientToken, &serverToken, &encKey, &macKey)
if err != nil {
if err != sql.ErrNoRows {
user.log.Errorln("Database scan failed:", err)
}
return nil
}
if len(jid.String) > 0 && len(clientID.String) > 0 {
user.JID = jid.String + whatsappExt.NewUserSuffix
// user.Session = &whatsapp.Session{
// ClientId: clientID.String,
// ClientToken: clientToken.String,
// ServerToken: serverToken.String,
// EncKey: encKey,
// MacKey: macKey,
// Wid: jid.String + whatsappExt.OldUserSuffix,
// }
}// else {
// user.Session = nil
// }
return user
}
//func (user *User) Scan(row Scannable) *User {
// var jid, clientID, clientToken, serverToken sql.NullString
// var encKey, macKey []byte
// err := row.Scan(&user.MXID, &jid, &user.ManagementRoom, &user.LastConnection, &clientID, &clientToken, &serverToken, &encKey, &macKey)
// if err != nil {
// if err != sql.ErrNoRows {
// user.log.Errorln("Database scan failed:", err)
// }
// return nil
// }
// if len(jid.String) > 0 && len(clientID.String) > 0 {
// user.JID = jid.String + whatsappExt.NewUserSuffix
// // user.Session = &whatsapp.Session{
// // ClientId: clientID.String,
// // ClientToken: clientToken.String,
// // ServerToken: serverToken.String,
// // EncKey: encKey,
// // MacKey: macKey,
// // Wid: jid.String + whatsappExt.OldUserSuffix,
// // }
// } // else {
// // user.Session = nil
// // }
// return user
//}
func stripSuffix(jid types.GroupMeID) string {
if len(jid) == 0 {
@ -130,42 +132,30 @@ func (user *User) jidPtr() *string {
return nil
}
func (user *User) sessionUnptr() (sess whatsapp.Session) {
// if user.Session != nil {
// sess = *user.Session
// }
return
}
//func (user *User) sessionUnptr() (sess whatsapp.Session) {
// // if user.Session != nil {
// // sess = *user.Session
// // }
// return
//}
func (user *User) Insert() {
// sess := user.sessionUnptr()
// _, err := user.db.Exec(`INSERT INTO "user" (mxid, jid, management_room, last_connection, client_id, client_token, server_token, enc_key, mac_key) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
// user.MXID, user.jidPtr(),
// user.ManagementRoom, user.LastConnection,
// sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey)
// if err != nil {
// user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
// }
ans := user.db.Create(&user)
if ans.Error != nil {
user.log.Warnfln("Failed to insert %s: %v", user.MXID, ans.Error)
}
}
func (user *User) UpdateLastConnection() {
// user.LastConnection = uint64(time.Now().Unix())
// _, err := user.db.Exec(`UPDATE "user" SET last_connection=$1 WHERE mxid=$2`,
// user.LastConnection, user.MXID)
// if err != nil {
// user.log.Warnfln("Failed to update last connection ts: %v", err)
// }
user.LastConnection = uint64(time.Now().Unix())
user.Update()
}
func (user *User) Update() {
// sess := user.sessionUnptr()
// _, err := user.db.Exec(`UPDATE "user" SET jid=$1, management_room=$2, last_connection=$3, client_id=$4, client_token=$5, server_token=$6, enc_key=$7, mac_key=$8 WHERE mxid=$9`,
// user.jidPtr(), user.ManagementRoom, user.LastConnection,
// sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey,
// user.MXID)
// if err != nil {
// user.log.Warnfln("Failed to update %s: %v", user.MXID, err)
// }
ans := user.db.Save(&user)
if ans.Error != nil {
user.log.Warnfln("Failed to update last connection ts: %v", ans.Error)
}
}
type PortalKeyWithMeta struct {
@ -173,82 +163,85 @@ type PortalKeyWithMeta struct {
InCommunity bool
}
type UserPortal struct {
UserJID types.GroupMeID `gorm:"primaryKey;"`
PortalJID types.GroupMeID `gorm:"primaryKey;"`
PortalReceiver types.GroupMeID `gorm:"primaryKey;"`
InCommunity bool `gorm:"notNull;default:false;"`
User User `gorm:"foreignKey:UserJID;references:jid;constraint:OnDelete:CASCADE;"`
Portal Portal `gorm:"foreignKey:PortalJID,PortalReceiver;references:JID,Receiver;constraint:OnDelete:CASCADE;"`
}
func (user *User) SetPortalKeys(newKeys []PortalKeyWithMeta) error {
// tx, err := user.db.Begin()
// if err != nil {
// return err
// }
// _, err = tx.Exec("DELETE FROM user_portal WHERE user_jid=$1", user.jidPtr())
// if err != nil {
// _ = tx.Rollback()
// return err
// }
// valueStrings := make([]string, len(newKeys))
// values := make([]interface{}, len(newKeys)*4)
// for i, key := range newKeys {
// pos := i * 4
// valueStrings[i] = fmt.Sprintf("($%d, $%d, $%d, $%d)", pos+1, pos+2, pos+3, pos+4)
// values[pos] = user.jidPtr()
// values[pos+1] = key.JID
// values[pos+2] = key.Receiver
// values[pos+3] = key.InCommunity
// }
// query := fmt.Sprintf("INSERT INTO user_portal (user_jid, portal_jid, portal_receiver, in_community) VALUES %s",
// strings.Join(valueStrings, ", "))
// _, err = tx.Exec(query, values...)
// if err != nil {
// _ = tx.Rollback()
// return err
// }
// return tx.Commit()
return nil
tx := user.db.Begin()
ans := tx.Where("user_jid = ?", *user.jidPtr()).Delete(&UserPortal{})
print("make sure all are deletede")
if ans.Error != nil {
_ = tx.Rollback()
return ans.Error
}
for _, key := range newKeys {
ans = tx.Create(&UserPortal{
UserJID: *user.jidPtr(),
PortalJID: key.JID,
PortalReceiver: key.Receiver,
InCommunity: key.InCommunity,
})
if ans.Error != nil {
_ = tx.Rollback()
return ans.Error
}
}
return tx.Commit().Error
}
func (user *User) IsInPortal(key PortalKey) bool {
// row := user.db.QueryRow(`SELECT EXISTS(SELECT 1 FROM user_portal WHERE user_jid=$1 AND portal_jid=$2 AND portal_receiver=$3)`, user.jidPtr(), &key.JID, &key.Receiver)
// var exists bool
// _ = row.Scan(&exists)
// return exists
return false
var count int64
user.db.Find(&UserPortal{
UserJID: *user.jidPtr(),
PortalJID: key.JID,
PortalReceiver: key.Receiver,
}).Count(&count) //TODO: efficient
return count > 0
}
func (user *User) GetPortalKeys() []PortalKey {
// rows, err := user.db.Query(`SELECT portal_jid, portal_receiver FROM user_portal WHERE user_jid=$1`, user.jidPtr())
// if err != nil {
// user.log.Warnln("Failed to get user portal keys:", err)
// return nil
// }
// var keys []PortalKey
// for rows.Next() {
// var key PortalKey
// err = rows.Scan(&key.JID, &key.Receiver)
// if err != nil {
// user.log.Warnln("Failed to scan row:", err)
// continue
// }
// keys = append(keys, key)
// }
// return keys
var up []UserPortal
ans := user.db.Where("user_jid = ?", *user.jidPtr()).Find(&up)
if ans.Error != nil {
user.log.Warnln("Failed to get user portal keys:", ans.Error)
return nil
}
var keys []PortalKey
for _, i := range up {
key := PortalKey{
JID: i.UserJID,
Receiver: i.PortalReceiver,
}
keys = append(keys, key)
}
return keys
}
func (user *User) GetInCommunityMap() map[PortalKey]bool {
// rows, err := user.db.Query(`SELECT portal_jid, portal_receiver, in_community FROM user_portal WHERE user_jid=$1`, user.jidPtr())
// if err != nil {
// user.log.Warnln("Failed to get user portal keys:", err)
// return nil
// }
// keys := make(map[PortalKey]bool)
// for rows.Next() {
// var key PortalKey
// var inCommunity bool
// err = rows.Scan(&key.JID, &key.Receiver, &inCommunity)
// if err != nil {
// user.log.Warnln("Failed to scan row:", err)
// continue
// }
// keys[key] = inCommunity
// }
// return keys
var up []UserPortal
ans := user.db.Where("user_jid = ?", *user.jidPtr()).Find(&up)
if ans.Error != nil {
user.log.Warnln("Failed to get user portal keys:", ans.Error)
return nil
}
keys := make(map[PortalKey]bool)
for _, i := range up {
key := PortalKey{
JID: i.PortalJID,
Receiver: i.PortalReceiver,
}
keys[key] = i.InCommunity
}
return keys
}

View File

@ -1,3 +0,0 @@
package groupmeExt
type Session struct{}

52
groupmeExt/client.go Normal file
View File

@ -0,0 +1,52 @@
package groupmeExt
import (
"context"
"fmt"
"github.com/karmanyaahm/groupme"
)
type Client struct {
*groupme.Client
}
// NewClient creates a new GroupMe API Client
func NewClient(authToken string) *Client {
n := Client{
Client: groupme.NewClient(authToken),
}
return &n
}
func (c Client) IndexAllGroups() ([]*groupme.Group, error) {
return c.IndexGroups(context.TODO(), &groupme.GroupsQuery{
Omit: "memberships",
PerPage: 100, //TODO: Configurable and add multipage support
})
}
func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFromMe bool, num int) ([]*groupme.Message, error) {
//TODO: limit max 100
i, e := c.IndexMessages(context.TODO(), groupme.ID(groupID), &groupme.IndexMessagesQuery{
AfterID: groupme.ID(lastMessageID),
Limit: num,
})
if e != nil {
return nil, e
}
return i.Messages, nil
}
func (c Client) LoadMessagesBefore(groupID, lastMessageID string, num int) ([]*groupme.Message, error) {
//TODO: limit max 100
i, e := c.IndexMessages(context.TODO(), groupme.ID(groupID), &groupme.IndexMessagesQuery{
BeforeID: groupme.ID(lastMessageID),
Limit: num,
})
fmt.Println(groupID, lastMessageID, num, i.Count, e)
if e != nil {
return nil, e
}
return i.Messages, nil
}

6
groupmeExt/user.go Normal file
View File

@ -0,0 +1,6 @@
package groupmeExt
const (
OldUserSuffix = "@c.groupme.com"
NewUserSuffix = "@groupme.com"
)

View File

@ -327,7 +327,7 @@ func (bridge *Bridge) LoadRelaybot() {
}
bridge.Relaybot.ManagementRoom = bridge.Config.Bridge.Relaybot.ManagementRoom
bridge.Relaybot.IsRelaybot = true
bridge.Relaybot.Connect(false)
bridge.Relaybot.Connect()
}
func (bridge *Bridge) UpdateBotProfile() {
@ -361,7 +361,7 @@ func (bridge *Bridge) UpdateBotProfile() {
func (bridge *Bridge) StartUsers() {
bridge.Log.Debugln("Starting users")
for _, user := range bridge.GetAllUsers() {
go user.Connect(false)
go user.Connect()
}
bridge.Log.Debugln("Starting custom puppets")
for _, loopuppet := range bridge.GetAllPuppetsWithCustomMXID() {

View File

@ -336,6 +336,7 @@ func (mx *MatrixHandler) shouldIgnoreEvent(evt *event.Event) bool {
const sessionWaitTimeout = 5 * time.Second
func (mx *MatrixHandler) HandleEncrypted(evt *event.Event) {
println("IDK iF encryption works yet")
defer mx.bridge.Metrics.TrackEvent(evt.Type)()
if mx.shouldIgnoreEvent(evt) || mx.bridge.Crypto == nil {
return

984
portal.go

File diff suppressed because it is too large Load Diff

View File

@ -45,7 +45,7 @@ func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (types.GroupMeID, bool) {
return "", false
}
jid := types.GroupMeID(match[1] + whatsappExt.NewUserSuffix)
jid := types.GroupMeID(match[1])
return jid, true
}
@ -159,6 +159,7 @@ type Puppet struct {
}
func (puppet *Puppet) PhoneNumber() string {
println("phone num")
return strings.Replace(puppet.JID, whatsappExt.NewUserSuffix, "", 1)
}

View File

@ -24,3 +24,5 @@ type WhatsAppMessageID = string
//AuthToken is the authentication token
type AuthToken = string
type TmpID = GroupMeID

377
user.go
View File

@ -22,11 +22,11 @@ import (
"errors"
"fmt"
"net/http"
"sort"
"strings"
"sync"
"time"
"github.com/skip2/go-qrcode"
log "maunium.net/go/maulogger/v2"
"github.com/Rhymen/go-whatsapp"
@ -40,6 +40,7 @@ import (
"github.com/karmanyaahm/groupme"
"maunium.net/go/mautrix-whatsapp/database"
"maunium.net/go/mautrix-whatsapp/groupmeExt"
"maunium.net/go/mautrix-whatsapp/types"
whatsappExt "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
)
@ -57,7 +58,7 @@ type User struct {
IsRelaybot bool
Client *groupme.Client
Client *groupmeExt.Client
ConnectionErrors int
CommunityID string
@ -230,10 +231,10 @@ func (user *User) SetSession(session *whatsapp.Session) {
// user.Update()
}
func (user *User) Connect(evenIfNoLogin bool) bool {
func (user *User) Connect() bool {
if user.Conn != nil {
return true
} else if !evenIfNoLogin && len(user.Token) < 0 {
} else if len(user.Token) == 0 {
return false
}
user.log.Debugln("Connecting to WhatsApp")
@ -260,8 +261,6 @@ func (user *User) Connect(evenIfNoLogin bool) bool {
func (user *User) RestoreSession() bool {
if len(user.Token) > 0 {
//sess, err :=
user.Conn.SubscribeToUser(context.TODO(), groupme.ID(user.GetJID()), user.Token)
// if err == whatsapp.ErrAlreadyLoggedIn {
// return true
// } else if err != nil {
@ -275,7 +274,6 @@ func (user *User) RestoreSession() bool {
// }
// user.log.Debugln("Disconnecting due to failed session restore...")
// _, err :=
user.Conn.Stop(context.TODO())
// if err != nil {
// user.log.Errorln("Failed to disconnect after failed session restore:", err)
// }
@ -283,29 +281,31 @@ func (user *User) RestoreSession() bool {
// }
user.Conn.SubscribeToUser(context.TODO(), groupme.ID(user.JID), user.Token)
//TODO: typing notifics
user.ConnectionErrors = 0
//user.SetSession(&sess)
user.log.Debugln("Session restored successfully")
user.PostLogin()
}
return true
} else {
user.log.Debugln("tried login but no token")
return false
}
}
func (user *User) HasSession() bool {
return user.Conn != nil && len(user.Token) > 0
return len(user.Token) > 0
}
func (user *User) IsConnected() bool {
// return user.Conn != nil && user.Conn.IsConnected() && user.Conn.IsLoggedIn()
return true
println("better connectoin check TODO")
return user.Conn != nil
}
func (user *User) IsLoggedIn() bool {
return true
}
func (user *User) IsLoginInProgress() bool {
// return user.Conn != nil && user.Conn.IsLoginInProgress()
return false
@ -318,59 +318,6 @@ func (user *User) GetJID() types.GroupMeID {
}
return user.JID
}
func (user *User) loginQrChannel(ce *CommandEvent, qrChan <-chan string, eventIDChan chan<- id.EventID) {
var qrEventID id.EventID
for code := range qrChan {
if code == "stop" {
return
}
qrCode, err := qrcode.Encode(code, qrcode.Low, 256)
if err != nil {
user.log.Errorln("Failed to encode QR code:", err)
ce.Reply("Failed to encode QR code: %v", err)
return
}
bot := user.bridge.AS.BotClient()
resp, err := bot.UploadBytes(qrCode, "image/png")
if err != nil {
user.log.Errorln("Failed to upload QR code:", err)
ce.Reply("Failed to upload QR code: %v", err)
return
}
if qrEventID == "" {
sendResp, err := bot.SendImage(ce.RoomID, code, resp.ContentURI)
if err != nil {
user.log.Errorln("Failed to send QR code to user:", err)
return
}
qrEventID = sendResp.EventID
eventIDChan <- qrEventID
} else {
_, err = bot.SendMessageEvent(ce.RoomID, event.EventMessage, &event.MessageEventContent{
MsgType: event.MsgImage,
Body: code,
URL: resp.ContentURI.CUString(),
NewContent: &event.MessageEventContent{
MsgType: event.MsgImage,
Body: code,
URL: resp.ContentURI.CUString(),
},
RelatesTo: &event.RelatesTo{
Type: event.RelReplace,
EventID: qrEventID,
},
})
if err != nil {
user.log.Errorln("Failed to send edited QR code to user:", err)
}
}
}
}
func (user *User) Login(ce *CommandEvent) {
// qrChan := make(chan string, 3)
// eventIDChan := make(chan id.EventID, 1)
@ -416,13 +363,13 @@ func (user *User) Login(ce *CommandEvent) {
user.addToJIDMap()
//user.SetSession(&session)
ce.Reply("Successfully logged in, synchronizing chats...")
// user.PostLogin()
user.PostLogin()
}
type Chat struct {
Portal *Portal
LastMessageTime uint64
Contact whatsapp.Contact
Group groupme.Group
}
type ChatList []Chat
@ -521,10 +468,19 @@ func (user *User) postConnPing() bool {
func (user *User) intPostLogin() {
defer user.syncWait.Done()
user.lastReconnection = time.Now().Unix()
user.Client = groupmeExt.NewClient(user.Token)
myuser, err := user.Client.MyUser(context.TODO())
if err != nil {
log.Fatal(err) //TODO
}
user.JID = myuser.ID.String()
user.Update()
user.createCommunity()
user.tryAutomaticDoublePuppeting()
user.log.Debugln("Waiting for chat list receive confirmation")
user.HandleChatList()
select {
case <-user.chatListReceived:
user.log.Debugln("Chat list receive confirmation received in PostLogin")
@ -565,84 +521,79 @@ func (user *User) HandleStreamEvent(evt whatsappExt.StreamEvent) {
}
}
func (user *User) HandleChatList(chats []whatsapp.Chat) {
// user.log.Infoln("Chat list received")
// chatMap := make(map[string]whatsapp.Chat)
// for _, chat := range user.Conn.Store.Chats {
// chatMap[chat.Jid] = chat
// }
// for _, chat := range chats {
// chatMap[chat.Jid] = chat
// }
// select {
// case user.chatListReceived <- struct{}{}:
// default:
// }
// go user.syncPortals(chatMap, false)
func (user *User) HandleChatList() {
chatMap := make(map[string]groupme.Group)
chats, err := user.Client.IndexAllGroups()
if err != nil {
log.Fatal(err) //TODO: handle
}
for _, chat := range chats {
chatMap[chat.ID.String()] = *chat
}
user.chatListReceived <- struct{}{}
user.log.Infoln("Chat list received")
go user.syncPortals(chatMap, false)
}
func (user *User) syncPortals(chatMap map[string]whatsapp.Chat, createAll bool) {
// if chatMap == nil {
func (user *User) syncPortals(chatMap map[string]groupme.Group, createAll bool) {
if chatMap == nil {
// chatMap = user.Conn.Store.Chats
// }
// user.log.Infoln("Reading chat list")
// chats := make(ChatList, 0, len(chatMap))
// existingKeys := user.GetInCommunityMap()
// portalKeys := make([]database.PortalKeyWithMeta, 0, len(chatMap))
// for _, chat := range chatMap {
// ts, err := strconv.ParseUint(chat.LastMessageTime, 10, 64)
// if err != nil {
// user.log.Warnfln("Non-integer last message time in %s: %s", chat.Jid, chat.LastMessageTime)
// continue
// }
// portal := user.GetPortalByJID(chat.Jid)
log.Fatal("chatmap nil major oops")
}
user.log.Infoln("Reading chat list")
// chats = append(chats, Chat{
// Portal: portal,
// Contact: user.Conn.Store.Contacts[chat.Jid],
// LastMessageTime: ts,
// })
// var inCommunity, ok bool
// if inCommunity, ok = existingKeys[portal.Key]; !ok || !inCommunity {
// inCommunity = user.addPortalToCommunity(portal)
// if portal.IsPrivateChat() {
// puppet := user.bridge.GetPuppetByJID(portal.Key.JID)
// user.addPuppetToCommunity(puppet)
// }
// }
// portalKeys = append(portalKeys, database.PortalKeyWithMeta{PortalKey: portal.Key, InCommunity: inCommunity})
// }
// user.log.Infoln("Read chat list, updating user-portal mapping")
// err := user.SetPortalKeys(portalKeys)
// if err != nil {
// user.log.Warnln("Failed to update user-portal mapping:", err)
// }
// sort.Sort(chats)
// limit := user.bridge.Config.Bridge.InitialChatSync
// if limit < 0 {
// limit = len(chats)
// }
// now := uint64(time.Now().Unix())
// user.log.Infoln("Syncing portals")
// for i, chat := range chats {
// if chat.LastMessageTime+user.bridge.Config.Bridge.SyncChatMaxAge < now {
// break
// }
// create := (chat.LastMessageTime >= user.LastConnection && user.LastConnection > 0) || i < limit
// if len(chat.Portal.MXID) > 0 || create || createAll {
// chat.Portal.Sync(user, chat.Contact)
// err := chat.Portal.BackfillHistory(user, chat.LastMessageTime)
// if err != nil {
// chat.Portal.log.Errorln("Error backfilling history:", err)
// }
// }
// }
// user.UpdateDirectChats(nil)
// user.log.Infoln("Finished syncing portals")
// select {
// case user.syncPortalsDone <- struct{}{}:
// default:
// }
chats := make(ChatList, 0, len(chatMap))
existingKeys := user.GetInCommunityMap()
portalKeys := make([]database.PortalKeyWithMeta, 0, len(chatMap))
for _, chat := range chatMap {
portal := user.GetPortalByJID(chat.ID.String())
chats = append(chats, Chat{
Portal: portal,
LastMessageTime: uint64(time.Now().Unix()),
Group: chat,
})
var inCommunity, ok bool
if inCommunity, ok = existingKeys[portal.Key]; !ok || !inCommunity {
inCommunity = user.addPortalToCommunity(portal)
if portal.IsPrivateChat() {
puppet := user.bridge.GetPuppetByJID(portal.Key.JID)
user.addPuppetToCommunity(puppet)
}
}
portalKeys = append(portalKeys, database.PortalKeyWithMeta{PortalKey: portal.Key, InCommunity: inCommunity})
}
user.log.Infoln("Read chat list, updating user-portal mapping")
err := user.SetPortalKeys(portalKeys)
if err != nil {
user.log.Warnln("Failed to update user-portal mapping:", err)
}
sort.Sort(chats)
limit := user.bridge.Config.Bridge.InitialChatSync
if limit < 0 {
limit = len(chats)
}
now := uint64(time.Now().Unix())
user.log.Infoln("Syncing portals")
for i, chat := range chats {
if chat.LastMessageTime+user.bridge.Config.Bridge.SyncChatMaxAge < now {
break
}
create := (chat.LastMessageTime >= user.LastConnection && user.LastConnection > 0) || i < limit
if len(chat.Portal.MXID) > 0 || create || createAll {
chat.Portal.Sync(user, chat.Group)
err := chat.Portal.BackfillHistory(user, chat.LastMessageTime)
if err != nil {
chat.Portal.log.Errorln("Error backfilling history:", err)
}
}
}
user.UpdateDirectChats(nil)
user.log.Infoln("Finished syncing portals")
select {
case user.syncPortalsDone <- struct{}{}:
default:
}
}
func (user *User) getDirectChats() map[id.UserID][]id.RoomID {
@ -881,41 +832,41 @@ func (user *User) HandleBatteryMessage(battery whatsapp.BatteryMessage) {
}
}
func (user *User) HandleTextMessage(message whatsapp.TextMessage) {
user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
func (user *User) HandleTextMessage(message groupme.Message) {
user.messageInput <- PortalMessage{message.GroupID.String(), user, &message, uint64(message.CreatedAt.ToTime().Unix())}
}
func (user *User) HandleImageMessage(message whatsapp.ImageMessage) {
user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
}
func (user *User) HandleStickerMessage(message whatsapp.StickerMessage) {
user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
}
func (user *User) HandleVideoMessage(message whatsapp.VideoMessage) {
user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
}
func (user *User) HandleAudioMessage(message whatsapp.AudioMessage) {
user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
}
func (user *User) HandleDocumentMessage(message whatsapp.DocumentMessage) {
user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
}
func (user *User) HandleContactMessage(message whatsapp.ContactMessage) {
user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
}
func (user *User) HandleLocationMessage(message whatsapp.LocationMessage) {
user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
}
func (user *User) HandleMessageRevoke(message whatsappExt.MessageRevocation) {
user.messageInput <- PortalMessage{message.RemoteJid, user, message, 0}
}
//func (user *User) HandleImageMessage(message whatsapp.ImageMessage) {
// user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
//}
//
//func (user *User) HandleStickerMessage(message whatsapp.StickerMessage) {
// user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
//}
//
//func (user *User) HandleVideoMessage(message whatsapp.VideoMessage) {
// user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
//}
//
//func (user *User) HandleAudioMessage(message whatsapp.AudioMessage) {
// user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
//}
//
//func (user *User) HandleDocumentMessage(message whatsapp.DocumentMessage) {
// user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
//}
//
//func (user *User) HandleContactMessage(message whatsapp.ContactMessage) {
// user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
//}
//
//func (user *User) HandleLocationMessage(message whatsapp.LocationMessage) {
// user.messageInput <- PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}
//}
//
//func (user *User) HandleMessageRevoke(message whatsappExt.MessageRevocation) {
// user.messageInput <- PortalMessage{message.RemoteJid, user, message, 0}
//}
type FakeMessage struct {
Text string
@ -923,40 +874,40 @@ type FakeMessage struct {
Alert bool
}
func (user *User) HandleCallInfo(info whatsappExt.CallInfo) {
if info.Data != nil {
return
}
data := FakeMessage{
ID: info.ID,
}
switch info.Type {
case whatsappExt.CallOffer:
if !user.bridge.Config.Bridge.CallNotices.Start {
return
}
data.Text = "Incoming call"
data.Alert = true
case whatsappExt.CallOfferVideo:
if !user.bridge.Config.Bridge.CallNotices.Start {
return
}
data.Text = "Incoming video call"
data.Alert = true
case whatsappExt.CallTerminate:
if !user.bridge.Config.Bridge.CallNotices.End {
return
}
data.Text = "Call ended"
data.ID += "E"
default:
return
}
portal := user.GetPortalByJID(info.From)
if portal != nil {
portal.messages <- PortalMessage{info.From, user, data, 0}
}
}
//func (user *User) HandleCallInfo(info whatsappExt.CallInfo) {
// if info.Data != nil {
// return
// }
// data := FakeMessage{
// ID: info.ID,
// }
// switch info.Type {
// case whatsappExt.CallOffer:
// if !user.bridge.Config.Bridge.CallNotices.Start {
// return
// }
// data.Text = "Incoming call"
// data.Alert = true
// case whatsappExt.CallOfferVideo:
// if !user.bridge.Config.Bridge.CallNotices.Start {
// return
// }
// data.Text = "Incoming video call"
// data.Alert = true
// case whatsappExt.CallTerminate:
// if !user.bridge.Config.Bridge.CallNotices.End {
// return
// }
// data.Text = "Call ended"
// data.ID += "E"
// default:
// return
// }
// portal := user.GetPortalByJID(info.From)
// if portal != nil {
// portal.messages <- PortalMessage{info.From, user, data, 0}
// }
//}
func (user *User) HandlePresence(info whatsappExt.Presence) {
puppet := user.bridge.GetPuppetByJID(info.SenderJID)
@ -1119,10 +1070,10 @@ func (user *User) HandleChatUpdate(cmd whatsappExt.ChatUpdate) {
go portal.HandleWhatsAppKick(cmd.Data.SenderJID, cmd.Data.UserChange.JIDs)
case whatsappExt.ChatActionAdd:
go portal.HandleWhatsAppInvite(cmd.Data.SenderJID, cmd.Data.UserChange.JIDs)
case whatsappExt.ChatActionIntroduce:
if cmd.Data.SenderJID != "unknown" {
go portal.Sync(user, whatsapp.Contact{Jid: portal.Key.JID})
}
//case whatsappExt.ChatActionIntroduce:
// if cmd.Data.SenderJID != "unknown" {
// go portal.Sync(user, whatsapp.Contact{Jid: portal.Key.JID})
// }
}
}