Prevent nil crashes on TelegramSession instances

This commit is contained in:
Bohdan Horbeshko 2026-03-28 16:39:59 -04:00
parent 0e6fbc652f
commit cca759f255
3 changed files with 10 additions and 4 deletions

View file

@ -137,6 +137,11 @@ const (
)
type ChatMemberStatus int
// IsNil is a simple check to use client objects via interfaces safely
func (c *Client) IsNil() bool {
return c == nil
}
// GetContactByUsername resolves username to user id retrieves user and chat information
func (c *Client) GetContactByUsername(username string, own bool) (*client.Chat, *client.User, error) {
if !c.Online() {

View file

@ -839,7 +839,7 @@ func GetDiscoInfo(session TelegramSession, node string, to string) (*stanza.Disc
if node == "" {
var isMuc bool
if session != nil {
if !session.IsNil() {
conf := session.GetPersistenceSession()
if conf.MUC {
if toOk && toIsGroup {
@ -934,7 +934,7 @@ func GetDiscoInfo(session TelegramSession, node string, to string) (*stanza.Disc
disco.AddFeatures("jabber:iq:version")
disco.AddFeatures("urn:xmpp:time")
} else if node == "x-roomuser-item" {
if session != nil {
if !session.IsNil() {
conf := session.GetPersistenceSession()
if conf.MUC {
if toOk && toIsGroup {
@ -950,7 +950,7 @@ func GetDiscoInfo(session TelegramSession, node string, to string) (*stanza.Disc
// noop yet, empty result as intended, TODO: add XHTML whenever supported
} else if strings.HasPrefix(node, capsNode) {
nodeParts := strings.Split(node, "#")
if session != nil && len(nodeParts) == 2 && nodeParts[0] == capsNode {
if !session.IsNil() && len(nodeParts) == 2 && nodeParts[0] == capsNode {
di, ok := session.GetVerDisco(nodeParts[1])
if ok && di != nil {
return di, 0
@ -959,7 +959,7 @@ func GetDiscoInfo(session TelegramSession, node string, to string) (*stanza.Disc
} else {
var chatType ChatType
var chatTypeErr error
if session != nil {
if !session.IsNil() {
chatType, _, chatTypeErr = session.GetChatType(toID, true)
}

View file

@ -209,6 +209,7 @@ func IsCommandForChatType(cmd command, chatType ChatType) bool {
// TelegramSession exists merely to circumvent a circular dependency
type TelegramSession interface {
IsNil() bool
GetPersistenceSession() *persistence.Session
GetContactByID(int64, *client.Chat, bool) (*client.Chat, *client.User, error)
IsGroup(*client.Chat) bool