diff options
Diffstat (limited to 'appservice')
-rw-r--r-- | appservice/account.go | 121 | ||||
-rw-r--r-- | appservice/db.go | 4 | ||||
-rw-r--r-- | appservice/server.go | 2 |
3 files changed, 89 insertions, 38 deletions
diff --git a/appservice/account.go b/appservice/account.go index a20751f..8430cc1 100644 --- a/appservice/account.go +++ b/appservice/account.go @@ -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 { - log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id) + 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.Warnf("Dropping Account.Left %s: %s", roomId, err) + } +} + +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) 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) 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) } } diff --git a/appservice/db.go b/appservice/db.go index e5b2d02..6c215d4 100644 --- a/appservice/db.go +++ b/appservice/db.go @@ -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 } diff --git a/appservice/server.go b/appservice/server.go index b67e813..580d1f1 100644 --- a/appservice/server.go +++ b/appservice/server.go @@ -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]) |