This commit is contained in:
Bohdan Horbeshko 2022-02-08 15:25:58 -05:00
parent fe7346a530
commit ec49d5d412
7 changed files with 37 additions and 36 deletions

View file

@ -122,15 +122,15 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
} }
return &Client{ return &Client{
parameters: &parameters, parameters: &parameters,
resource: resource, resource: resource,
xmpp: component, xmpp: component,
jid: jid, jid: jid,
Session: session, Session: session,
resources: make(map[string]bool), resources: make(map[string]bool),
content: &conf.Content, content: &conf.Content,
cache: cache.NewCache(), cache: cache.NewCache(),
options: options, options: options,
DelayedStatuses: make(map[int64]*DelayedStatus), DelayedStatuses: make(map[int64]*DelayedStatus),
locks: clientLocks{ locks: clientLocks{
chatMessageLocks: make(map[int64]*sync.Mutex), chatMessageLocks: make(map[int64]*sync.Mutex),

View file

@ -17,6 +17,7 @@ import (
const notEnoughArguments string = "Not enough arguments" const notEnoughArguments string = "Not enough arguments"
const telegramNotInitialized string = "Telegram connection is not initialized yet" const telegramNotInitialized string = "Telegram connection is not initialized yet"
const notOnline string = "Not online" const notOnline string = "Not online"
var permissionsAdmin = client.ChatMemberStatusAdministrator{ var permissionsAdmin = client.ChatMemberStatusAdministrator{
CanBeEdited: true, CanBeEdited: true,
CanChangeInfo: true, CanChangeInfo: true,
@ -82,8 +83,8 @@ var chatCommands = map[string]command{
} }
var transportConfigurationOptions = map[string]configurationOption{ var transportConfigurationOptions = map[string]configurationOption{
"timezone": configurationOption{"<timezone>", "adjust timezone for Telegram user statuses (example: +02:00)"}, "timezone": configurationOption{"<timezone>", "adjust timezone for Telegram user statuses (example: +02:00)"},
"keeponline": configurationOption{"<bool>", "always keep telegram session online and rely on jabber offline messages (example: true)"}, "keeponline": configurationOption{"<bool>", "always keep telegram session online and rely on jabber offline messages (example: true)"},
"rawmessages": configurationOption{"<bool>", "do not add additional info (message id, origin etc.) to incoming messages (example: true)"}, "rawmessages": configurationOption{"<bool>", "do not add additional info (message id, origin etc.) to incoming messages (example: true)"},
} }
@ -144,13 +145,13 @@ func parseCommand(cmdline string) (string, []string) {
return bodyFields[0][1:], bodyFields[1:] return bodyFields[0][1:], bodyFields[1:]
} }
func rawCmdArguments(cmdline string, start uint8) (string) { func rawCmdArguments(cmdline string, start uint8) string {
var state uint var state uint
// /cmd ababa galamaga // /cmd ababa galamaga
// 01 2 3 45 // 01 2 3 45
startState := uint(3 + 2 * start) startState := uint(3 + 2*start)
for i, r := range cmdline { for i, r := range cmdline {
isOdd := state % 2 == 1 isOdd := state%2 == 1
isSpace := unicode.IsSpace(r) isSpace := unicode.IsSpace(r)
if (!isOdd && !isSpace) || (isOdd && isSpace) { if (!isOdd && !isSpace) || (isOdd && isSpace) {
state += 1 state += 1
@ -611,7 +612,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID, ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id}, MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &client.ChatMemberStatusRestricted{ Status: &client.ChatMemberStatusRestricted{
IsMember: true, IsMember: true,
RestrictedUntilDate: c.formatBantime(hours), RestrictedUntilDate: c.formatBantime(hours),
Permissions: &permissionsReadonly, Permissions: &permissionsReadonly,
@ -634,7 +635,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID, ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id}, MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &client.ChatMemberStatusRestricted{ Status: &client.ChatMemberStatusRestricted{
IsMember: true, IsMember: true,
RestrictedUntilDate: 0, RestrictedUntilDate: 0,
Permissions: &permissionsMember, Permissions: &permissionsMember,
@ -663,7 +664,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID, ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id}, MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &client.ChatMemberStatusBanned{ Status: &client.ChatMemberStatusBanned{
BannedUntilDate: c.formatBantime(hours), BannedUntilDate: c.formatBantime(hours),
@ -684,9 +685,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID, ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id}, MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &client.ChatMemberStatusMember{}, Status: &client.ChatMemberStatusMember{},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -710,9 +711,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID, ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id}, MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &status, Status: &status,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true

View file

@ -134,7 +134,7 @@ func (c *Client) Connect(resource string) error {
go func() { go func() {
_, err = c.client.GetChats(&client.GetChatsRequest{ _, err = c.client.GetChats(&client.GetChatsRequest{
Limit: chatsLimit, Limit: chatsLimit,
}) })
if err != nil { if err != nil {
log.Errorf("Could not retrieve chats: %v", err) log.Errorf("Could not retrieve chats: %v", err)

View file

@ -212,7 +212,7 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
c.cache.SetStatus(chatID, show, status) c.cache.SetStatus(chatID, show, status)
newArgs := []args.V { newArgs := []args.V{
gateway.SPFrom(strconv.FormatInt(chatID, 10)), gateway.SPFrom(strconv.FormatInt(chatID, 10)),
gateway.SPShow(show), gateway.SPShow(show),
gateway.SPStatus(status), gateway.SPStatus(status),
@ -331,7 +331,7 @@ func (c *Client) formatForward(fwd *client.MessageForwardInfo) string {
if originChat.AuthorSignature != "" { if originChat.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature) signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature)
} }
return c.formatContact(originChat.SenderChatId)+signature return c.formatContact(originChat.SenderChatId) + signature
case client.TypeMessageForwardOriginHiddenUser: case client.TypeMessageForwardOriginHiddenUser:
originUser := fwd.Origin.(*client.MessageForwardOriginHiddenUser) originUser := fwd.Origin.(*client.MessageForwardOriginHiddenUser)
return originUser.SenderName return originUser.SenderName
@ -341,7 +341,7 @@ func (c *Client) formatForward(fwd *client.MessageForwardInfo) string {
if channel.AuthorSignature != "" { if channel.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", channel.AuthorSignature) signature = fmt.Sprintf(" (%s)", channel.AuthorSignature)
} }
return c.formatContact(channel.ChatId)+signature return c.formatContact(channel.ChatId) + signature
case client.TypeMessageForwardOriginMessageImport: case client.TypeMessageForwardOriginMessageImport:
originImport := fwd.Origin.(*client.MessageForwardOriginMessageImport) originImport := fwd.Origin.(*client.MessageForwardOriginMessageImport)
return originImport.SenderName return originImport.SenderName

View file

@ -37,9 +37,9 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
Address: conf.Host + ":" + conf.Port, Address: conf.Host + ":" + conf.Port,
Domain: conf.Jid, Domain: conf.Jid,
}, },
Domain: conf.Jid, Domain: conf.Jid,
Secret: conf.Password, Secret: conf.Password,
Name: "telegabber", Name: "telegabber",
} }
router := xmpp.NewRouter() router := xmpp.NewRouter()

View file

@ -26,12 +26,12 @@ type PresenceXVCardUpdatePhoto struct {
// IqVcardTemp is from XEP-0054 // IqVcardTemp is from XEP-0054
type IqVcardTemp struct { type IqVcardTemp struct {
XMLName xml.Name `xml:"vcard-temp vCard"` XMLName xml.Name `xml:"vcard-temp vCard"`
Fn IqVcardFn Fn IqVcardFn
Nickname IqVcardNickname Nickname IqVcardNickname
N IqVcardN N IqVcardN
Tel IqVcardTel Tel IqVcardTel
Photo IqVcardPhoto Photo IqVcardPhoto
ResultSet *stanza.ResultSet `xml:"set,omitempty"` ResultSet *stanza.ResultSet `xml:"set,omitempty"`
} }

View file

@ -305,8 +305,8 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ) {
answer, err := stanza.NewIQ(stanza.Attrs{ answer, err := stanza.NewIQ(stanza.Attrs{
Type: stanza.IQTypeResult, Type: stanza.IQTypeResult,
From: iq.To, From: iq.To,
To: iq.From, To: iq.From,
Id: iq.Id, Id: iq.Id,
Lang: "en", Lang: "en",
}) })
if err != nil { if err != nil {