diff options
Diffstat (limited to 'appservice/account.go')
-rw-r--r-- | appservice/account.go | 121 |
1 files changed, 86 insertions, 35 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) } } |