diff --git a/telegram/utils.go b/telegram/utils.go index 8ff25b2..5e8900c 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -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() { diff --git a/xmpp/gateway/gateway.go b/xmpp/gateway/gateway.go index ac80698..3079fe3 100644 --- a/xmpp/gateway/gateway.go +++ b/xmpp/gateway/gateway.go @@ -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) } diff --git a/xmpp/gateway/telegram_session.go b/xmpp/gateway/telegram_session.go index 251ae96..a2f5525 100644 --- a/xmpp/gateway/telegram_session.go +++ b/xmpp/gateway/telegram_session.go @@ -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