diff options
author | Alex Auvolat <alex@adnab.me> | 2020-02-17 16:28:32 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-02-17 16:28:32 +0100 |
commit | 86942a34a2aa086dee76c9e4e0b6942848e1b979 (patch) | |
tree | e004efb1a284e6d158ed9aaab7f3a582df19026f /connector/xmpp | |
parent | 584312f30805680711557ff6fbe291d2404367fb (diff) | |
download | easybridge-86942a34a2aa086dee76c9e4e0b6942848e1b979.tar.gz easybridge-86942a34a2aa086dee76c9e4e0b6942848e1b979.zip |
Fix XMPP && transmit nicknames from bridges to matrix
Diffstat (limited to 'connector/xmpp')
-rw-r--r-- | connector/xmpp/xmpp.go | 110 |
1 files changed, 63 insertions, 47 deletions
diff --git a/connector/xmpp/xmpp.go b/connector/xmpp/xmpp.go index 7516ebc..e50bb58 100644 --- a/connector/xmpp/xmpp.go +++ b/connector/xmpp/xmpp.go @@ -181,74 +181,73 @@ func (xm *XMPP) handleXMPP() error { switch v := m.(type) { case gxmpp.Chat: - log.Printf("== Receiving %#v\n", v) + remote_sp := strings.Split(v.Remote, "/") - if v.Text == "" && v.Type == "groupchat" { - // Empty message when we joined group chat - if !strings.Contains(v.Remote, "/") { - xm.handler.Joined(RoomID(v.Remote)) - } - if v.Subject != "" { - author := UserID("") - remote_sp := strings.Split(v.Remote, "/") - if len(remote_sp) == 2 { - author = UserID(remote_sp[1] + "@" + remote_sp[0]) - } - xm.handler.RoomInfoUpdated(RoomID(v.Remote), author, &RoomInfo{ - Topic: v.Subject, - }) - } - continue - } - - if v.Text == "" || v.Remote == xm.jid { + // Skip self-sent events + if v.Remote == xm.jid || (v.Type == "groupchat" && len(remote_sp) == 2 && remote_sp[1] == xm.nickname) { continue } - event := &Event{ - Type: EVENT_MESSAGE, - Text: v.Text, + // If empty text, make sure we joined the room + // We would do this at every incoming message if it were not so costly + if v.Text == "" && v.Type == "groupchat" { + xm.handler.Joined(RoomID(remote_sp[0])) } - log.Printf("Remote: %s\n", v.Remote) - if strings.HasPrefix(event.Text, "/me ") { - event.Type = EVENT_ACTION - event.Text = strings.Replace(event.Text, "/me ", "", 1) + // Handle subject change in group chats + if v.Subject != "" && v.Type == "groupchat" { + author := UserID("") + if len(remote_sp) == 2 { + author = UserID(remote_sp[1] + "@" + remote_sp[0]) + } + xm.handler.RoomInfoUpdated(RoomID(remote_sp[0]), author, &RoomInfo{ + Topic: v.Subject, + }) } - if v.Type == "chat" { - remote_jid := strings.Split(v.Remote, "/")[0] - event.Author = UserID(remote_jid) - xm.handler.Event(event) - } - if v.Type == "groupchat" { - remote := strings.Split(v.Remote, "/") - if len(remote) != 2 { - log.Printf("Invalid remote: %s\n", v.Remote) - continue + // Handle text message + if v.Text != "" { + event := &Event{ + Type: EVENT_MESSAGE, + Text: v.Text, } - event.Room = RoomID(remote[0]) - event.Author = UserID(remote[1] + "@" + remote[0]) - if strings.Contains(v.Text, "has set the subject to:") { - // TODO - continue + if strings.HasPrefix(event.Text, "/me ") { + event.Type = EVENT_ACTION + event.Text = strings.Replace(event.Text, "/me ", "", 1) } - xm.handler.Event(event) + if v.Type == "chat" { + event.Author = UserID(remote_sp[0]) + xm.handler.Event(event) + } + if v.Type == "groupchat" && len(remote_sp) == 2 { + event.Room = RoomID(remote_sp[0]) + event.Author = UserID(remote_sp[1] + "@" + remote_sp[0]) + xm.handler.Event(event) + } } case gxmpp.Presence: remote := strings.Split(v.From, "/") - if muc, ok := xm.isMUC[remote[0]]; ok && muc && len(remote) == 2 { + if ismuc, ok := xm.isMUC[remote[0]]; ok && ismuc { + // skip presence with no user and self-presence + if len(remote) < 2 || remote[1] == xm.nickname { + continue + } + + user := UserID(remote[1] + "@" + remote[0]) event := &Event{ Type: EVENT_JOIN, Room: RoomID(remote[0]), - Author: UserID(remote[1] + "@" + remote[0]), + Author: user, } if v.Type == "unavailable" { event.Type = EVENT_LEAVE } xm.handler.Event(event) + xm.handler.UserInfoUpdated(user, &UserInfo{ + DisplayName: remote[1], + }) } // Do nothing. } @@ -264,8 +263,24 @@ func (xm *XMPP) SetUserInfo(info *UserInfo) error { } func (xm *XMPP) SetRoomInfo(roomId RoomID, info *RoomInfo) error { - // TODO - return fmt.Errorf("Not implemented") + if info.Topic != "" { + xm.conn.Send(gxmpp.Chat{ + Type: "groupchat", + Remote: string(roomId), + Subject: info.Topic, + }) + } + + if info.Picture != nil { + // TODO + return fmt.Errorf("Room picture change not implemented on xmpp") + } + + if info.Name != "" && info.Name != string(roomId) { + // TODO + return fmt.Errorf("Room name change not implemented on xmpp") + } + return nil } func (xm *XMPP) Join(roomId RoomID) error { @@ -286,6 +301,7 @@ func (xm *XMPP) Leave(roomId 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{ Type: "chat", |