aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2022-07-08 03:38:06 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2022-07-08 03:38:06 +0300
commit6abb7ff9c238f0343351c15c09210d746086f5f6 (patch)
tree9d0f97cca26997cba7ac1c7fd308ae289baa35ab
parentafa21e10be45a6186b99a33e4d8f22d820677efb (diff)
Respond to disco with conference identity and groups list
-rw-r--r--telegram/utils.go24
-rw-r--r--xmpp/handlers.go58
2 files changed, 82 insertions, 0 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index 17e9861..7747acb 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -1065,6 +1065,30 @@ func (c *Client) GetChatDescription(chat *client.Chat) string {
return ""
}
+// GetGroupChats obtains all group chats
+func (c *Client) GetGroupChats() []*client.Chat {
+ var groupChats []*client.Chat
+
+ chats, err := c.client.GetChats(&client.GetChatsRequest{
+ Limit: chatsLimit,
+ })
+ if err == nil {
+ for _, id := range chats.ChatIds {
+ chat, _, _ := c.GetContactByID(id, nil)
+ if chat != nil {
+ typ := chat.Type.ChatTypeType()
+ if typ == client.TypeChatTypeBasicGroup {
+ groupChats = append(groupChats, chat)
+ }
+ }
+ }
+ } else {
+ log.Errorf("Could not retrieve chats: %v", err)
+ }
+
+ return groupChats
+}
+
// subscribe to a Telegram ID
func (c *Client) subscribeToID(id int64, chat *client.Chat) {
var args []args.V
diff --git a/xmpp/handlers.go b/xmpp/handlers.go
index 7c671d9..64915a1 100644
--- a/xmpp/handlers.go
+++ b/xmpp/handlers.go
@@ -55,6 +55,11 @@ func HandleIq(s xmpp.Sender, p stanza.Packet) {
go handleGetDiscoInfo(s, iq)
return
}
+ _, ok = iq.Payload.(*stanza.DiscoItems)
+ if ok {
+ go handleGetDiscoItems(s, iq)
+ return
+ }
}
}
@@ -333,6 +338,59 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ) {
} else {
disco.AddIdentity("Telegram Gateway", "gateway", "telegram")
}
+ bare, _, ok := splitFrom(iq.From)
+ if ok {
+ session, ok := sessions[bare]
+ if ok && session.Session.MUC {
+ disco.AddFeatures(stanza.NSDiscoItems)
+ disco.AddIdentity("Telegram group chats", "conference", "text")
+ }
+ }
+ answer.Payload = disco
+
+ log.Debugf("%#v", answer)
+
+ component, ok := s.(*xmpp.Component)
+ if !ok {
+ log.Error("Not a component")
+ return
+ }
+
+ _ = gateway.ResumableSend(component, answer)
+}
+
+func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ) {
+ answer, err := stanza.NewIQ(stanza.Attrs{
+ Type: stanza.IQTypeResult,
+ From: iq.To,
+ To: iq.From,
+ Id: iq.Id,
+ Lang: "en",
+ })
+ if err != nil {
+ log.Errorf("Failed to create answer IQ: %v", err)
+ return
+ }
+
+ disco := answer.DiscoItems()
+
+ _, ok := toToID(iq.To)
+ if !ok {
+ bare, _, ok := splitFrom(iq.From)
+ if ok {
+ // raw access, no need to create a new instance if not connected
+ session, ok := sessions[bare]
+ if ok && session.Session.MUC {
+ bareJid := gateway.Jid.Bare()
+ disco.AddItem(bareJid, "", "Telegram group chats")
+ for _, chat := range session.GetGroupChats() {
+ jid := strconv.FormatInt(chat.Id, 10) + "@" + bareJid
+ disco.AddItem(jid, "", chat.Title)
+ }
+ }
+ }
+ }
+
answer.Payload = disco
log.Debugf("%#v", answer)