Compare commits

...

6 Commits
master ... muc

6 changed files with 166 additions and 13 deletions
Split View
  1. +1
    -1
      go.mod
  2. +2
    -0
      go.sum
  3. +11
    -0
      persistence/sessions.go
  4. +2
    -0
      persistence/sessions_test.go
  5. +59
    -3
      telegram/utils.go
  6. +91
    -9
      xmpp/handlers.go

+ 1
- 1
go.mod View File

@ -13,4 +13,4 @@ require (
gosrc.io/xmpp v0.5.2-0.20211214110136-5f99e1cd06e1
)
replace gosrc.io/xmpp => dev.narayana.im/narayana/go-xmpp v0.0.0-20220524203317-306b4ff58e8f
replace gosrc.io/xmpp => dev.narayana.im/narayana/go-xmpp v0.0.0-20220708184440-35d9cd68e55f

+ 2
- 0
go.sum View File

@ -2,6 +2,8 @@ dev.narayana.im/narayana/go-xmpp v0.0.0-20211218155535-e55463fc9829 h1:qe81G6+t1
dev.narayana.im/narayana/go-xmpp v0.0.0-20211218155535-e55463fc9829/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
dev.narayana.im/narayana/go-xmpp v0.0.0-20220524203317-306b4ff58e8f h1:6249ajbMjgYz53Oq0IjTvjHXbxTfu29Mj1J/6swRHs4=
dev.narayana.im/narayana/go-xmpp v0.0.0-20220524203317-306b4ff58e8f/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
dev.narayana.im/narayana/go-xmpp v0.0.0-20220708184440-35d9cd68e55f h1:aT50UsPH1dLje9CCAquRRhr7I9ZvL3kQU6WIWTe8PZ0=
dev.narayana.im/narayana/go-xmpp v0.0.0-20220708184440-35d9cd68e55f/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
github.com/Arman92/go-tdlib v0.0.0-20191002071913-526f4e1d15f7 h1:GbV1Lv3lVHsSeKAqPTBem72OCsGjXntW4jfJdXciE+w=
github.com/Arman92/go-tdlib v0.0.0-20191002071913-526f4e1d15f7/go.mod h1:ZzkRfuaFj8etIYMj/ECtXtgfz72RE6U+dos27b3XIwk=
github.com/agnivade/wasmbrowsertest v0.3.1/go.mod h1:zQt6ZTdl338xxRaMW395qccVE2eQm0SjC/SDz0mPWQI=


+ 11
- 0
persistence/sessions.go View File

@ -39,6 +39,7 @@ type Session struct {
KeepOnline bool `yaml:":keeponline"`
RawMessages bool `yaml:":rawmessages"`
AsciiArrows bool `yaml:":asciiarrows"`
MUC bool `yaml:":muc"`
}
var configKeys = []string{
@ -46,6 +47,7 @@ var configKeys = []string{
"keeponline",
"rawmessages",
"asciiarrows",
"muc",
}
var sessionDB *SessionsYamlDB
@ -118,6 +120,8 @@ func (s *Session) Get(key string) (string, error) {
return fromBool(s.RawMessages), nil
case "asciiarrows":
return fromBool(s.AsciiArrows), nil
case "muc":
return fromBool(s.MUC), nil
}
return "", errors.New("Unknown session property")
@ -161,6 +165,13 @@ func (s *Session) Set(key string, value string) (string, error) {
}
s.AsciiArrows = b
return value, nil
case "muc":
b, err := toBool(value)
if err != nil {
return "", err
}
s.MUC = b
return value, nil
}
return "", errors.New("Unknown session property")


+ 2
- 0
persistence/sessions_test.go View File

@ -47,11 +47,13 @@ func TestSessionToMap(t *testing.T) {
session := Session{
Timezone: "klsf",
RawMessages: true,
MUC: true,
}
m := session.ToMap()
sample := map[string]string{
"timezone": "klsf",
"keeponline": "false",
"muc": "true",
"rawmessages": "true",
"asciiarrows": "false",
}


+ 59
- 3
telegram/utils.go View File

@ -1039,7 +1039,7 @@ func (c *Client) GetChatDescription(chat *client.Chat) string {
return fullInfo.Description
}
} else {
log.Warnf("Coudln't retrieve private chat info: %v", err.Error())
log.Warnf("Couldn't retrieve private chat info: %v", err.Error())
}
} else if chatType == client.TypeChatTypeBasicGroup {
basicGroupType, _ := chat.Type.(*client.ChatTypeBasicGroup)
@ -1049,7 +1049,7 @@ func (c *Client) GetChatDescription(chat *client.Chat) string {
if err == nil {
return fullInfo.Description
} else {
log.Warnf("Coudln't retrieve basic group info: %v", err.Error())
log.Warnf("Couldn't retrieve basic group info: %v", err.Error())
}
} else if chatType == client.TypeChatTypeSupergroup {
supergroupType, _ := chat.Type.(*client.ChatTypeSupergroup)
@ -1059,12 +1059,68 @@ func (c *Client) GetChatDescription(chat *client.Chat) string {
if err == nil {
return fullInfo.Description
} else {
log.Warnf("Coudln't retrieve supergroup info: %v", err.Error())
log.Warnf("Couldn't retrieve supergroup info: %v", err.Error())
}
}
return ""
}
// GetChatMemberCount obtains the member count depending on the chat type
func (c *Client) GetChatMemberCount(chat *client.Chat) int32 {
chatType := chat.Type.ChatTypeType()
if chatType == client.TypeChatTypePrivate {
return 2
} else if chatType == client.TypeChatTypeBasicGroup {
basicGroupType, _ := chat.Type.(*client.ChatTypeBasicGroup)
basicGroup, err := c.client.GetBasicGroup(&client.GetBasicGroupRequest{
BasicGroupId: basicGroupType.BasicGroupId,
})
if err == nil {
return basicGroup.MemberCount
} else {
log.Warnf("Couldn't retrieve basic group: %v", err.Error())
}
} else if chatType == client.TypeChatTypeSupergroup {
supergroupType, _ := chat.Type.(*client.ChatTypeSupergroup)
supergroup, err := c.client.GetSupergroup(&client.GetSupergroupRequest{
SupergroupId: supergroupType.SupergroupId,
})
if err == nil {
return supergroup.MemberCount
} else {
log.Warnf("Couldn't retrieve supergroup: %v", err.Error())
}
}
return 0
}
// 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 && c.IsGroup(chat) {
groupChats = append(groupChats, chat)
}
}
} else {
log.Errorf("Could not retrieve chats: %v", err)
}
return groupChats
}
// IsGroup determines if a chat is eligible to be represented as MUC
func (c *Client) IsGroup(chat *client.Chat) bool {
typ := chat.Type.ChatTypeType()
return typ == client.TypeChatTypeBasicGroup
}
// subscribe to a Telegram ID
func (c *Client) subscribeToID(id int64, chat *client.Chat) {
var args []args.V


+ 91
- 9
xmpp/handlers.go View File

@ -24,6 +24,12 @@ const (
)
const NodeVCard4 string = "urn:xmpp:vcard4"
type discoType int
const (
discoTypeInfo discoType = iota
discoTypeItems
)
func logPacketType(p stanza.Packet) {
log.Warnf("Ignoring packet: %T\n", p)
}
@ -52,7 +58,12 @@ func HandleIq(s xmpp.Sender, p stanza.Packet) {
}
_, ok = iq.Payload.(*stanza.DiscoInfo)
if ok {
go handleGetDiscoInfo(s, iq)
go handleGetDisco(discoTypeInfo, s, iq)
return
}
_, ok = iq.Payload.(*stanza.DiscoItems)
if ok {
go handleGetDisco(discoTypeItems, s, iq)
return
}
}
@ -313,7 +324,7 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
_ = gateway.ResumableSend(component, &answer)
}
func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ) {
func handleGetDisco(dt discoType, s xmpp.Sender, iq *stanza.IQ) {
answer, err := stanza.NewIQ(stanza.Attrs{
Type: stanza.IQTypeResult,
From: iq.To,
@ -326,14 +337,85 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ) {
return
}
disco := answer.DiscoInfo()
_, ok := toToID(iq.To)
if ok {
disco.AddIdentity("", "account", "registered")
} else {
disco.AddIdentity("Telegram Gateway", "gateway", "telegram")
if dt == discoTypeInfo {
disco := answer.DiscoInfo()
toID, toOk := toToID(iq.To)
if !toOk {
disco.AddIdentity("Telegram Gateway", "gateway", "telegram")
}
var isMuc bool
bare, _, fromOk := splitFrom(iq.From)
if fromOk {
session, sessionOk := sessions[bare]
if sessionOk && session.Session.MUC {
if toOk {
chat, _, err := session.GetContactByID(toID, nil)
if err == nil && session.IsGroup(chat) {
isMuc = true
disco.AddIdentity(chat.Title, "conference", "text")
disco.AddFeatures(
"http://jabber.org/protocol/muc",
"muc_persistent",
"muc_hidden",
"muc_membersonly",
"muc_unmoderated",
"muc_nonanonymous",
"muc_unsecured",
)
fields := []*stanza.Field{
&stanza.Field{
Var: "FORM_TYPE",
Type: "hidden",
ValuesList: []string{"http://jabber.org/protocol/muc#roominfo"},
},
&stanza.Field{
Var: "muc#roominfo_description",
Label: "Description",
ValuesList: []string{session.GetChatDescription(chat)},
},
&stanza.Field{
Var: "muc#roominfo_occupants",
Label: "Number of occupants",
ValuesList: []string{strconv.FormatInt(int64(session.GetChatMemberCount(chat)), 10)},
},
}
disco.Form = stanza.NewForm(fields, "result")
}
} else {
disco.AddFeatures(stanza.NSDiscoItems)
disco.AddIdentity("Telegram group chats", "conference", "text")
}
}
}
if toOk && !isMuc {
disco.AddIdentity("", "account", "registered")
}
answer.Payload = disco
} else if dt == discoTypeItems {
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
}
answer.Payload = disco
log.Debugf("%#v", answer)


Loading…
Cancel
Save