This commit is contained in:
Alex 2020-02-17 19:02:00 +01:00
parent 86942a34a2
commit 531b59bf95
5 changed files with 103 additions and 50 deletions

View File

@ -2,6 +2,7 @@ package appservice
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
@ -54,114 +55,167 @@ func RemoveAccount(mxUser string, name string) {
}
}
// ----
// ---- Begin event handlers ----
func (a *Account) Joined(roomId RoomID) {
err := a.joinedInternal(roomId)
if err != nil {
log.Warnf("Dropping Account.Joined %s: %s", roomId, err)
}
}
func (a *Account) joinedInternal(roomId RoomID) error {
a.JoinedRooms[roomId] = true
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return
return err
}
log.Printf("Joined %s (%s)\n", roomId, a.MatrixUser)
log.Debugf("Joined %s (%s)\n", roomId, a.MatrixUser)
err = mxRoomInvite(mx_room_id, a.MatrixUser)
if err != nil && strings.Contains(err.Error(), "already in the room") {
err = nil
}
return err
}
// ----
func (a *Account) Left(roomId RoomID) {
err := a.leftInternal(roomId)
if err != nil {
log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
log.Warnf("Dropping Account.Left %s: %s", roomId, err)
}
}
func (a *Account) Left(roomId RoomID) {
func (a *Account) leftInternal(roomId RoomID) error {
delete(a.JoinedRooms, roomId)
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return
return err
}
log.Printf("Joined %s (%s)\n", roomId, a.MatrixUser)
err = mxRoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
return mxRoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
}
// ----
func (a *Account) UserInfoUpdated(user UserID, info *UserInfo) {
err := a.userInfoUpdatedInternal(user, info)
if err != nil {
log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
log.Warnf("Dropping Account.UserInfoUpdated %s: %s", user, err)
}
}
func (a *Account) UserInfoUpdated(user UserID, info *UserInfo) {
func (a *Account) userInfoUpdatedInternal(user UserID, info *UserInfo) error {
mx_user_id, err := dbGetMxUser(a.Protocol, user)
if err != nil {
return
return err
}
if info.DisplayName != "" {
mxProfileDisplayname(mx_user_id, fmt.Sprintf("%s (%s)", info.DisplayName, a.Protocol))
err2 := mxProfileDisplayname(mx_user_id, fmt.Sprintf("%s (%s)", info.DisplayName, a.Protocol))
if err2 != nil {
err = err2
}
}
if info.Avatar != nil {
// TODO
err = fmt.Errorf("Avatar: not implemented")
}
return err
}
// ----
func (a *Account) RoomInfoUpdated(roomId RoomID, author UserID, info *RoomInfo) {
err := a.roomInfoUpdatedInternal(roomId, author, info)
if err != nil {
log.Warnf("Dropping Account.RoomInfoUpdated %s: %s", roomId, err)
}
}
func (a *Account) RoomInfoUpdated(roomId RoomID, author UserID, info *RoomInfo) {
func (a *Account) roomInfoUpdatedInternal(roomId RoomID, author UserID, info *RoomInfo) error {
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return
return err
}
as_mxid := ezbrMxId()
if len(author) > 0 {
mx_user_id, err := dbGetMxUser(a.Protocol, author)
if err == nil {
mx_user_id, err2 := dbGetMxUser(a.Protocol, author)
if err2 == nil {
as_mxid = mx_user_id
}
}
if info.Topic != "" {
mxRoomTopicAs(mx_room_id, info.Topic, as_mxid)
err2 := mxRoomTopicAs(mx_room_id, info.Topic, as_mxid)
if err2 != nil {
err = err2
}
}
if info.Name != "" {
mxRoomNameAs(mx_room_id, info.Name, as_mxid)
err2 := mxRoomNameAs(mx_room_id, info.Name, as_mxid)
if err2 != nil {
err = err2
}
}
if info.Picture != nil {
// TODO
err = fmt.Errorf("Picture: not implemented")
}
return err
}
// ----
func (a *Account) Event(event *Event) {
err := a.eventInternal(event)
if err != nil {
log.Warnf("Dropping Account.Event %s %s %s: %s", event.Author, event.Recipient, event.Room, err)
}
}
func (a *Account) Event(event *Event) {
func (a *Account) eventInternal(event *Event) error {
mx_user_id, err := dbGetMxUser(a.Protocol, event.Author)
if err != nil {
return
return err
}
if event.Type == EVENT_JOIN {
log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
if err != nil {
return
return err
}
err = mxRoomInvite(mx_room_id, mx_user_id)
if err != nil {
log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
if strings.Contains(err.Error(), "already in the room") {
err = nil
}
return err
}
err = mxRoomJoinAs(mx_room_id, mx_user_id)
if err != nil {
log.Printf("Could not join %s as %s", a.MatrixUser, mx_room_id)
}
return mxRoomJoinAs(mx_room_id, mx_user_id)
} else if event.Type == EVENT_LEAVE {
log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
if err != nil {
return
return err
}
err = mxRoomLeaveAs(mx_room_id, mx_user_id)
if err != nil {
log.Printf("Could not leave %s as %s", a.MatrixUser, mx_room_id)
}
return mxRoomLeaveAs(mx_room_id, mx_user_id)
} else {
log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room)
mx_room_id := ""
@ -169,12 +223,12 @@ func (a *Account) Event(event *Event) {
if len(event.Room) > 0 {
mx_room_id, err = dbGetMxRoom(a.Protocol, event.Room)
if err != nil {
return
return err
}
} else {
mx_room_id, err = dbGetMxPmRoom(a.Protocol, event.Author, mx_user_id, a.MatrixUser, a.AccountName)
if err != nil {
return
return err
}
}
@ -183,10 +237,7 @@ func (a *Account) Event(event *Event) {
typ = "m.emote"
}
err = mxSendMessageAs(mx_room_id, typ, event.Text, mx_user_id)
if err != nil {
log.Printf("Could not send %s as %s", event.Text, mx_user_id)
}
return mxSendMessageAs(mx_room_id, typ, event.Text, mx_user_id)
}
}

View File

@ -108,7 +108,7 @@ func dbGetMxRoom(protocol string, roomId connector.RoomID) (string, error) {
}
db.Create(&room)
}
log.Printf("Got room id: %s", room.MxRoomID)
log.Debugf("Got room id: %s", room.MxRoomID)
return room.MxRoomID, nil
}
@ -146,7 +146,7 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
}
db.Create(&room)
}
log.Printf("Got PM room id: %s", room.MxRoomID)
log.Debugf("Got PM room id: %s", room.MxRoomID)
return room.MxRoomID, nil
}

View File

@ -82,7 +82,7 @@ func handleTxn(w http.ResponseWriter, r *http.Request) {
return
}
log.Printf("Got transaction %#v\n", txn)
log.Debugf("Got transaction %#v\n", txn)
for i := range txn.Events {
handleTxnEvent(&txn.Events[i])

View File

@ -126,15 +126,15 @@ func (irc *IRC) SetRoomInfo(roomId RoomID, info *RoomInfo) error {
return err
}
if info.Topic != "" {
irc.conn.Cmd.Topic(ch, info.Topic)
}
if info.Name != "" && info.Name != ch {
return fmt.Errorf("May not change IRC room name to other than %s", ch)
}
if info.Picture != nil {
return fmt.Errorf("Room picture not supported on IRC")
}
if info.Topic != "" {
irc.conn.Cmd.Topic(ch, info.Topic)
}
return nil
}

View File

@ -249,7 +249,6 @@ func (xm *XMPP) handleXMPP() error {
DisplayName: remote[1],
})
}
// Do nothing.
}
}
}
@ -264,11 +263,14 @@ func (xm *XMPP) SetUserInfo(info *UserInfo) error {
func (xm *XMPP) SetRoomInfo(roomId RoomID, info *RoomInfo) error {
if info.Topic != "" {
xm.conn.Send(gxmpp.Chat{
_, err := xm.conn.Send(gxmpp.Chat{
Type: "groupchat",
Remote: string(roomId),
Subject: info.Topic,
})
if err != nil {
return err
}
}
if info.Picture != nil {
@ -297,25 +299,25 @@ func (xm *XMPP) Invite(userId UserID, roomId RoomID) error {
}
func (xm *XMPP) Leave(roomId RoomID) {
// TODO
xm.conn.LeaveMUC(string(roomId))
}
func (xm *XMPP) Send(event *Event) error {
fmt.Printf("xm *XMPP Send %#v\n", event)
if len(event.Recipient) > 0 {
xm.conn.Send(gxmpp.Chat{
_, err := xm.conn.Send(gxmpp.Chat{
Type: "chat",
Remote: string(event.Recipient),
Text: event.Text,
})
return nil
return err
} else if len(event.Room) > 0 {
xm.conn.Send(gxmpp.Chat{
_, err := xm.conn.Send(gxmpp.Chat{
Type: "groupchat",
Remote: string(event.Room),
Text: event.Text,
})
return nil
return err
} else {
return fmt.Errorf("Invalid event")
}