Compare commits
51 commits
message-ni
...
master
Author | SHA1 | Date | |
---|---|---|---|
Bohdan Horbeshko | ba8f4c08cf | ||
Bohdan Horbeshko | af07773b07 | ||
Bohdan Horbeshko | a74e2bcb7d | ||
Bohdan Horbeshko | a3f6d5f774 | ||
Bohdan Horbeshko | 2459b14948 | ||
Bohdan Horbeshko | f15e44436b | ||
Bohdan Horbeshko | a36856b768 | ||
Bohdan Horbeshko | b499992148 | ||
Bohdan Horbeshko | 144c5724ea | ||
Bohdan Horbeshko | 3e772be7a6 | ||
Bohdan Horbeshko | 908bd76aac | ||
Bohdan Horbeshko | 67c38823f2 | ||
Bohdan Horbeshko | f56e6ac187 | ||
Bohdan Horbeshko | 20e6d2558e | ||
Bohdan Horbeshko | 3a60a1cfaa | ||
Bohdan Horbeshko | ea004b7f7c | ||
Bohdan Horbeshko | c141c4ad2b | ||
Bohdan Horbeshko | 599cf16cdb | ||
Bohdan Horbeshko | 81fc3ea370 | ||
Bohdan Horbeshko | e37c428c67 | ||
Bohdan Horbeshko | b9b6ba14a4 | ||
Bohdan Horbeshko | b40ccf4a4d | ||
Bohdan Horbeshko | 4532748c84 | ||
Bohdan Horbeshko | f2807779aa | ||
Bohdan Horbeshko | 705cfc1d49 | ||
Bohdan Horbeshko | dcb802358b | ||
Bohdan Horbeshko | 6bd8379114 | ||
Bohdan Horbeshko | 576acba0d1 | ||
Bohdan Horbeshko | 67b8ad57f0 | ||
Bohdan Horbeshko | 282a6fc21b | ||
Bohdan Horbeshko | 4588170d1e | ||
Bohdan Horbeshko | aa561c5be6 | ||
Bohdan Horbeshko | 20994e2995 | ||
Bohdan Horbeshko | 8ba7596ab5 | ||
Bohdan Horbeshko | 64515e2c66 | ||
Bohdan Horbeshko | 9377d7a155 | ||
Bohdan Horbeshko | c03ccfdfb7 | ||
Bohdan Horbeshko | 608f675512 | ||
Bohdan Horbeshko | 3c917c1698 | ||
Bohdan Horbeshko | 8fc9edd7e7 | ||
a5f6c60035 | |||
Bohdan Horbeshko | 131f6eba38 | ||
Bohdan Horbeshko | a595d9db0a | ||
Bohdan Horbeshko | ef831fc972 | ||
Bohdan Horbeshko | 748366ad6a | ||
Bohdan Horbeshko | eadef987be | ||
Bohdan Horbeshko | 563cb2d624 | ||
Bohdan Horbeshko | e954c73bd2 | ||
Bohdan Horbeshko | 959dc061ff | ||
Bohdan Horbeshko | 30b3fd1615 | ||
Bohdan Horbeshko | f8ad8c0204 |
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -3,3 +3,5 @@ telegabber
|
||||||
sessions/
|
sessions/
|
||||||
session.dat
|
session.dat
|
||||||
session.dat.new
|
session.dat.new
|
||||||
|
release/
|
||||||
|
tdlib/
|
||||||
|
|
36
Dockerfile
Normal file
36
Dockerfile
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
FROM golang:1.19-bookworm AS base
|
||||||
|
|
||||||
|
RUN apt-get update
|
||||||
|
run apt-get install -y libssl-dev cmake build-essential gperf libz-dev make git
|
||||||
|
|
||||||
|
FROM base AS tdlib
|
||||||
|
|
||||||
|
ARG TD_COMMIT
|
||||||
|
ARG MAKEOPTS
|
||||||
|
RUN git clone https://github.com/tdlib/td /src/
|
||||||
|
RUN git -C /src/ checkout "${TD_COMMIT}"
|
||||||
|
RUN mkdir build
|
||||||
|
WORKDIR /build/
|
||||||
|
RUN cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/compiled/ /src/
|
||||||
|
RUN cmake --build . ${MAKEOPTS}
|
||||||
|
RUN make install
|
||||||
|
|
||||||
|
FROM base AS cache
|
||||||
|
ARG VERSION
|
||||||
|
COPY --from=tdlib /compiled/ /usr/local/
|
||||||
|
COPY ./ /src
|
||||||
|
RUN git -C /src checkout "${VERSION}"
|
||||||
|
WORKDIR /src
|
||||||
|
RUN go get
|
||||||
|
|
||||||
|
FROM cache AS build
|
||||||
|
ARG MAKEOPTS
|
||||||
|
WORKDIR /src
|
||||||
|
RUN make ${MAKEOPTS}
|
||||||
|
|
||||||
|
FROM scratch AS telegabber
|
||||||
|
COPY --from=build /src/release/telegabber /usr/local/bin/
|
||||||
|
ENTRYPOINT ["/usr/local/bin/telegabber"]
|
||||||
|
|
||||||
|
FROM scratch AS binaries
|
||||||
|
COPY --from=telegabber /usr/local/bin/telegabber /
|
15
Makefile
15
Makefile
|
@ -1,12 +1,25 @@
|
||||||
.PHONY: all test
|
.PHONY: all test
|
||||||
|
|
||||||
COMMIT := $(shell git rev-parse --short HEAD)
|
COMMIT := $(shell git rev-parse --short HEAD)
|
||||||
|
TD_COMMIT := "5bbfc1cf5dab94f82e02f3430ded7241d4653551"
|
||||||
|
VERSION := "v1.9.6"
|
||||||
|
MAKEOPTS := "-j4"
|
||||||
|
|
||||||
all:
|
all:
|
||||||
go build -ldflags "-X main.commit=${COMMIT}" -o telegabber
|
mkdir -p release
|
||||||
|
go build -ldflags "-X main.commit=${COMMIT}" -o release/telegabber
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -v ./config ./ ./telegram ./xmpp ./xmpp/gateway ./persistence ./telegram/formatter ./badger
|
go test -v ./config ./ ./telegram ./xmpp ./xmpp/gateway ./persistence ./telegram/formatter ./badger
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
$(GOPATH)/bin/golint ./...
|
$(GOPATH)/bin/golint ./...
|
||||||
|
|
||||||
|
build_indocker:
|
||||||
|
docker build --build-arg "TD_COMMIT=${TD_COMMIT}" --build-arg "VERSION=${VERSION}" --build-arg "MAKEOPTS=${MAKEOPTS}" --output=release --target binaries .
|
||||||
|
|
||||||
|
build_indocker_staging:
|
||||||
|
DOCKER_BUILDKIT=1 docker build --build-arg "TD_COMMIT=${TD_COMMIT}" --build-arg "MAKEOPTS=${MAKEOPTS}" --network host --output=release --target binaries -f staging.Dockerfile .
|
||||||
|
|
||||||
|
build_tdlib:
|
||||||
|
DOCKER_BUILDKIT=1 docker build --build-arg "TD_COMMIT=${TD_COMMIT}" --build-arg "MAKEOPTS=${MAKEOPTS}" --output=tdlib --target binaries -f tdlib.Dockerfile .
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -34,3 +34,4 @@ require (
|
||||||
)
|
)
|
||||||
|
|
||||||
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-20220524203317-306b4ff58e8f
|
||||||
|
replace github.com/zelenin/go-tdlib => dev.narayana.im/narayana/go-tdlib v0.0.0-20240124222245-b4c12addb061
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -1,4 +1,10 @@
|
||||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
|
dev.narayana.im/narayana/go-tdlib v0.0.0-20230730021136-47da33180615 h1:RRUZJSro+k8FkazNx7QEYLVoO4wZtchvsd0Y2RBWjeU=
|
||||||
|
dev.narayana.im/narayana/go-tdlib v0.0.0-20230730021136-47da33180615/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
||||||
|
dev.narayana.im/narayana/go-tdlib v0.0.0-20231111182840-bc2f985e6268 h1:NCbc2bYuUGQsb/3z5SCIia3N34Ktwq3FwaUAfgF/WEU=
|
||||||
|
dev.narayana.im/narayana/go-tdlib v0.0.0-20231111182840-bc2f985e6268/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
||||||
|
dev.narayana.im/narayana/go-tdlib v0.0.0-20240124222245-b4c12addb061 h1:CWAQT74LwQne/3Po5KXDvudu3N0FBWm3XZZZhtl5j2w=
|
||||||
|
dev.narayana.im/narayana/go-tdlib v0.0.0-20240124222245-b4c12addb061/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
||||||
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 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-20220524203317-306b4ff58e8f/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
|
|
@ -3,6 +3,7 @@ package persistence
|
||||||
import (
|
import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"dev.narayana.im/narayana/telegabber/yamldb"
|
"dev.narayana.im/narayana/telegabber/yamldb"
|
||||||
|
@ -42,6 +43,10 @@ type Session struct {
|
||||||
OOBMode bool `yaml:":oobmode"`
|
OOBMode bool `yaml:":oobmode"`
|
||||||
Carbons bool `yaml:":carbons"`
|
Carbons bool `yaml:":carbons"`
|
||||||
HideIds bool `yaml:":hideids"`
|
HideIds bool `yaml:":hideids"`
|
||||||
|
Receipts bool `yaml:":receipts"`
|
||||||
|
NativeEdits bool `yaml:":nativeedits"`
|
||||||
|
IgnoredChats []int64 `yaml:":ignoredchats"`
|
||||||
|
ignoredChatsMap map[int64]bool `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var configKeys = []string{
|
var configKeys = []string{
|
||||||
|
@ -52,17 +57,26 @@ var configKeys = []string{
|
||||||
"oobmode",
|
"oobmode",
|
||||||
"carbons",
|
"carbons",
|
||||||
"hideids",
|
"hideids",
|
||||||
|
"receipts",
|
||||||
|
"nativeedits",
|
||||||
}
|
}
|
||||||
|
|
||||||
var sessionDB *SessionsYamlDB
|
var sessionDB *SessionsYamlDB
|
||||||
|
var sessionsLock sync.Mutex
|
||||||
|
|
||||||
// SessionMarshaller implementation for YamlDB
|
// SessionMarshaller implementation for YamlDB
|
||||||
func SessionMarshaller() ([]byte, error) {
|
func SessionMarshaller() ([]byte, error) {
|
||||||
cleanedMap := SessionsMap{}
|
cleanedMap := SessionsMap{}
|
||||||
emptySessionsMap(&cleanedMap)
|
emptySessionsMap(&cleanedMap)
|
||||||
|
|
||||||
|
sessionsLock.Lock()
|
||||||
|
defer sessionsLock.Unlock()
|
||||||
for jid, session := range sessionDB.Data.Sessions {
|
for jid, session := range sessionDB.Data.Sessions {
|
||||||
if session.Login != "" {
|
if session.Login != "" {
|
||||||
|
session.IgnoredChats = make([]int64, 0, len(session.ignoredChatsMap))
|
||||||
|
for chatID := range session.ignoredChatsMap {
|
||||||
|
session.IgnoredChats = append(session.IgnoredChats, chatID)
|
||||||
|
}
|
||||||
cleanedMap.Sessions[jid] = session
|
cleanedMap.Sessions[jid] = session
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,6 +118,16 @@ func initYamlDB(path string, dataPtr *SessionsMap) (*SessionsYamlDB, error) {
|
||||||
emptySessionsMap(dataPtr)
|
emptySessionsMap(dataPtr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convert ignored users slice to map
|
||||||
|
for jid, session := range dataPtr.Sessions {
|
||||||
|
session.ignoredChatsMap = make(map[int64]bool)
|
||||||
|
for _, chatID := range session.IgnoredChats {
|
||||||
|
session.ignoredChatsMap[chatID] = true
|
||||||
|
}
|
||||||
|
session.IgnoredChats = nil
|
||||||
|
dataPtr.Sessions[jid] = session
|
||||||
|
}
|
||||||
|
|
||||||
return &SessionsYamlDB{
|
return &SessionsYamlDB{
|
||||||
YamlDB: yamldb.YamlDB{
|
YamlDB: yamldb.YamlDB{
|
||||||
Path: path,
|
Path: path,
|
||||||
|
@ -115,6 +139,13 @@ func initYamlDB(path string, dataPtr *SessionsMap) (*SessionsYamlDB, error) {
|
||||||
|
|
||||||
// Get retrieves a session value
|
// Get retrieves a session value
|
||||||
func (s *Session) Get(key string) (string, error) {
|
func (s *Session) Get(key string) (string, error) {
|
||||||
|
sessionsLock.Lock()
|
||||||
|
defer sessionsLock.Unlock()
|
||||||
|
|
||||||
|
return s.get(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) get(key string) (string, error) {
|
||||||
switch key {
|
switch key {
|
||||||
case "timezone":
|
case "timezone":
|
||||||
return s.Timezone, nil
|
return s.Timezone, nil
|
||||||
|
@ -130,6 +161,10 @@ func (s *Session) Get(key string) (string, error) {
|
||||||
return fromBool(s.Carbons), nil
|
return fromBool(s.Carbons), nil
|
||||||
case "hideids":
|
case "hideids":
|
||||||
return fromBool(s.HideIds), nil
|
return fromBool(s.HideIds), nil
|
||||||
|
case "receipts":
|
||||||
|
return fromBool(s.Receipts), nil
|
||||||
|
case "nativeedits":
|
||||||
|
return fromBool(s.NativeEdits), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("Unknown session property")
|
return "", errors.New("Unknown session property")
|
||||||
|
@ -137,9 +172,12 @@ func (s *Session) Get(key string) (string, error) {
|
||||||
|
|
||||||
// ToMap converts the session to a map
|
// ToMap converts the session to a map
|
||||||
func (s *Session) ToMap() map[string]string {
|
func (s *Session) ToMap() map[string]string {
|
||||||
|
sessionsLock.Lock()
|
||||||
|
defer sessionsLock.Unlock()
|
||||||
|
|
||||||
m := make(map[string]string)
|
m := make(map[string]string)
|
||||||
for _, configKey := range configKeys {
|
for _, configKey := range configKeys {
|
||||||
value, _ := s.Get(configKey)
|
value, _ := s.get(configKey)
|
||||||
m[configKey] = value
|
m[configKey] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,6 +186,9 @@ func (s *Session) ToMap() map[string]string {
|
||||||
|
|
||||||
// Set sets a session value
|
// Set sets a session value
|
||||||
func (s *Session) Set(key string, value string) (string, error) {
|
func (s *Session) Set(key string, value string) (string, error) {
|
||||||
|
sessionsLock.Lock()
|
||||||
|
defer sessionsLock.Unlock()
|
||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
case "timezone":
|
case "timezone":
|
||||||
s.Timezone = value
|
s.Timezone = value
|
||||||
|
@ -194,6 +235,20 @@ func (s *Session) Set(key string, value string) (string, error) {
|
||||||
}
|
}
|
||||||
s.HideIds = b
|
s.HideIds = b
|
||||||
return value, nil
|
return value, nil
|
||||||
|
case "receipts":
|
||||||
|
b, err := toBool(value)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
s.Receipts = b
|
||||||
|
return value, nil
|
||||||
|
case "nativeedits":
|
||||||
|
b, err := toBool(value)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
s.NativeEdits = b
|
||||||
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("Unknown session property")
|
return "", errors.New("Unknown session property")
|
||||||
|
@ -210,6 +265,51 @@ func (s *Session) TimezoneToLocation() *time.Location {
|
||||||
return zeroLocation
|
return zeroLocation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IgnoreChat adds a chat id to ignore list, returns false if already ignored
|
||||||
|
func (s *Session) IgnoreChat(chatID int64) bool {
|
||||||
|
sessionsLock.Lock()
|
||||||
|
defer sessionsLock.Unlock()
|
||||||
|
|
||||||
|
if s.ignoredChatsMap == nil {
|
||||||
|
s.ignoredChatsMap = make(map[int64]bool)
|
||||||
|
} else if _, ok := s.ignoredChatsMap[chatID]; ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
s.ignoredChatsMap[chatID] = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnignoreChat removes a chat id from ignore list, returns false if not already ignored
|
||||||
|
func (s *Session) UnignoreChat(chatID int64) bool {
|
||||||
|
sessionsLock.Lock()
|
||||||
|
defer sessionsLock.Unlock()
|
||||||
|
|
||||||
|
if s.ignoredChatsMap == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := s.ignoredChatsMap[chatID]; !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(s.ignoredChatsMap, chatID)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsChatIgnored checks the chat id against the ignore list
|
||||||
|
func (s *Session) IsChatIgnored(chatID int64) bool {
|
||||||
|
sessionsLock.Lock()
|
||||||
|
defer sessionsLock.Unlock()
|
||||||
|
|
||||||
|
if s.ignoredChatsMap == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ok := s.ignoredChatsMap[chatID]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
func fromBool(b bool) string {
|
func fromBool(b bool) string {
|
||||||
if b {
|
if b {
|
||||||
return "true"
|
return "true"
|
||||||
|
|
|
@ -48,6 +48,7 @@ func TestSessionToMap(t *testing.T) {
|
||||||
Timezone: "klsf",
|
Timezone: "klsf",
|
||||||
RawMessages: true,
|
RawMessages: true,
|
||||||
OOBMode: true,
|
OOBMode: true,
|
||||||
|
Receipts: true,
|
||||||
}
|
}
|
||||||
m := session.ToMap()
|
m := session.ToMap()
|
||||||
sample := map[string]string{
|
sample := map[string]string{
|
||||||
|
@ -58,6 +59,8 @@ func TestSessionToMap(t *testing.T) {
|
||||||
"oobmode": "true",
|
"oobmode": "true",
|
||||||
"carbons": "false",
|
"carbons": "false",
|
||||||
"hideids": "false",
|
"hideids": "false",
|
||||||
|
"receipts": "true",
|
||||||
|
"nativeedits": "false",
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(m, sample) {
|
if !reflect.DeepEqual(m, sample) {
|
||||||
t.Errorf("Map does not match the sample: %v", m)
|
t.Errorf("Map does not match the sample: %v", m)
|
||||||
|
@ -85,3 +88,31 @@ func TestSessionSetAbsent(t *testing.T) {
|
||||||
t.Error("There shouldn't come a donkey!")
|
t.Error("There shouldn't come a donkey!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSessionIgnore(t *testing.T) {
|
||||||
|
session := Session{}
|
||||||
|
if session.IsChatIgnored(3) {
|
||||||
|
t.Error("Shouldn't be ignored yet")
|
||||||
|
}
|
||||||
|
if !session.IgnoreChat(3) {
|
||||||
|
t.Error("Shouldn't have been ignored")
|
||||||
|
}
|
||||||
|
if session.IgnoreChat(3) {
|
||||||
|
t.Error("Shouldn't ignore second time")
|
||||||
|
}
|
||||||
|
if !session.IsChatIgnored(3) {
|
||||||
|
t.Error("Should be ignored already")
|
||||||
|
}
|
||||||
|
if session.IsChatIgnored(-145) {
|
||||||
|
t.Error("Wrong chat is ignored")
|
||||||
|
}
|
||||||
|
if !session.UnignoreChat(3) {
|
||||||
|
t.Error("Should successfully unignore")
|
||||||
|
}
|
||||||
|
if session.UnignoreChat(3) {
|
||||||
|
t.Error("Should unignore second time")
|
||||||
|
}
|
||||||
|
if session.IsChatIgnored(3) {
|
||||||
|
t.Error("Shouldn't be ignored already")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
46
staging.Dockerfile
Normal file
46
staging.Dockerfile
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
FROM golang:1.19-bullseye AS base
|
||||||
|
|
||||||
|
RUN apt-get update
|
||||||
|
RUN apt-get install -y libssl-dev cmake build-essential gperf libz-dev make git php
|
||||||
|
|
||||||
|
FROM base AS tdlib
|
||||||
|
|
||||||
|
ARG TD_COMMIT
|
||||||
|
ARG MAKEOPTS
|
||||||
|
RUN git clone https://github.com/tdlib/td /src/
|
||||||
|
RUN git -C /src/ checkout "${TD_COMMIT}"
|
||||||
|
RUN mkdir build
|
||||||
|
WORKDIR /build/
|
||||||
|
RUN cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/compiled/ /src/
|
||||||
|
RUN cmake --build . --target prepare_cross_compiling ${MAKEOPTS}
|
||||||
|
WORKDIR /src/
|
||||||
|
RUN php SplitSource.php
|
||||||
|
WORKDIR /build/
|
||||||
|
RUN cmake --build . ${MAKEOPTS}
|
||||||
|
RUN make install
|
||||||
|
|
||||||
|
FROM base AS cache
|
||||||
|
ARG VERSION
|
||||||
|
COPY --from=tdlib /compiled/ /usr/local/
|
||||||
|
WORKDIR /src
|
||||||
|
RUN go env -w GOCACHE=/go-cache
|
||||||
|
RUN go env -w GOMODCACHE=/gomod-cache
|
||||||
|
RUN --mount=type=cache,target=/gomod-cache \
|
||||||
|
--mount=type=bind,source=./,target=/src \
|
||||||
|
go mod download
|
||||||
|
|
||||||
|
FROM cache AS build
|
||||||
|
ARG MAKEOPTS
|
||||||
|
WORKDIR /src
|
||||||
|
RUN --mount=type=bind,source=./,target=/src,rw \
|
||||||
|
--mount=type=cache,target=/go-cache \
|
||||||
|
--mount=type=cache,target=/gomod-cache \
|
||||||
|
--mount=type=cache,destination=/src/release \
|
||||||
|
make ${MAKEOPTS}
|
||||||
|
|
||||||
|
FROM build AS release
|
||||||
|
RUN --mount=type=cache,destination=/src/release \
|
||||||
|
cp /src/release/telegabber /
|
||||||
|
|
||||||
|
FROM scratch AS binaries
|
||||||
|
COPY --from=release /telegabber /
|
23
tdlib.Dockerfile
Normal file
23
tdlib.Dockerfile
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
FROM golang:1.19-bullseye AS base
|
||||||
|
|
||||||
|
RUN apt-get update
|
||||||
|
RUN apt-get install -y libssl-dev cmake build-essential gperf libz-dev make git php
|
||||||
|
|
||||||
|
FROM base AS tdlib
|
||||||
|
|
||||||
|
ARG TD_COMMIT
|
||||||
|
ARG MAKEOPTS
|
||||||
|
RUN git clone https://github.com/tdlib/td /src/
|
||||||
|
RUN git -C /src/ checkout "${TD_COMMIT}"
|
||||||
|
RUN mkdir build
|
||||||
|
WORKDIR /build/
|
||||||
|
RUN cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/compiled/ /src/
|
||||||
|
RUN cmake --build . --target prepare_cross_compiling ${MAKEOPTS}
|
||||||
|
WORKDIR /src/
|
||||||
|
RUN php SplitSource.php
|
||||||
|
WORKDIR /build/
|
||||||
|
RUN cmake --build . ${MAKEOPTS}
|
||||||
|
RUN make install
|
||||||
|
|
||||||
|
FROM scratch AS binaries
|
||||||
|
COPY --from=tdlib /compiled/ /
|
|
@ -12,10 +12,11 @@ import (
|
||||||
"dev.narayana.im/narayana/telegabber/xmpp"
|
"dev.narayana.im/narayana/telegabber/xmpp"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/zelenin/go-tdlib/client"
|
||||||
goxmpp "gosrc.io/xmpp"
|
goxmpp "gosrc.io/xmpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version string = "1.6.1-dev"
|
var version string = "1.9.6"
|
||||||
var commit string
|
var commit string
|
||||||
|
|
||||||
var sm *goxmpp.StreamManager
|
var sm *goxmpp.StreamManager
|
||||||
|
@ -60,6 +61,9 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
|
||||||
|
NewVerbosityLevel: stringToTdlibLogConstant(config.Telegram.Loglevel),
|
||||||
|
})
|
||||||
SetLogrusLevel(config.XMPP.Loglevel)
|
SetLogrusLevel(config.XMPP.Loglevel)
|
||||||
|
|
||||||
log.Infof("Starting telegabber version %v", version)
|
log.Infof("Starting telegabber version %v", version)
|
||||||
|
@ -89,6 +93,25 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tdlibLogConstants = map[string]int32{
|
||||||
|
":fatal": 0,
|
||||||
|
":error": 1,
|
||||||
|
":warn": 2,
|
||||||
|
":info": 3,
|
||||||
|
":debug": 4,
|
||||||
|
":verbose": 5,
|
||||||
|
":all": 1023,
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringToTdlibLogConstant(c string) int32 {
|
||||||
|
level, ok := tdlibLogConstants[c]
|
||||||
|
if !ok {
|
||||||
|
level = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return level
|
||||||
|
}
|
||||||
|
|
||||||
func exit() {
|
func exit() {
|
||||||
xmpp.Close(component)
|
xmpp.Close(component)
|
||||||
close(cleanupDone)
|
close(cleanupDone)
|
||||||
|
|
19
telegabber_test.go
Normal file
19
telegabber_test.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTdlibLogInfo(t *testing.T) {
|
||||||
|
tdlibConstant := stringToTdlibLogConstant(":info")
|
||||||
|
if tdlibConstant != 3 {
|
||||||
|
t.Errorf("Wrong TDlib constant for info")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTdlibLogInvalid(t *testing.T) {
|
||||||
|
tdlibConstant := stringToTdlibLogConstant("ziz")
|
||||||
|
if tdlibConstant != 0 {
|
||||||
|
t.Errorf("Unknown strings should return fatal loglevel")
|
||||||
|
}
|
||||||
|
}
|
10
telegram/cache/cache.go
vendored
10
telegram/cache/cache.go
vendored
|
@ -133,3 +133,13 @@ func (cache *Cache) SetStatus(id int64, show string, status string) {
|
||||||
Description: status,
|
Description: status,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Destruct splits a cached status into show, description and type
|
||||||
|
func (status *Status) Destruct() (show, description, typ string) {
|
||||||
|
show, description = status.XMPP, status.Description
|
||||||
|
if show == "unavailable" {
|
||||||
|
typ = show
|
||||||
|
show = ""
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"hash/maphash"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -15,25 +16,6 @@ import (
|
||||||
"gosrc.io/xmpp"
|
"gosrc.io/xmpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logConstants = map[string]int32{
|
|
||||||
":fatal": 0,
|
|
||||||
":error": 1,
|
|
||||||
":warn": 2,
|
|
||||||
":info": 3,
|
|
||||||
":debug": 4,
|
|
||||||
":verbose": 5,
|
|
||||||
":all": 1023,
|
|
||||||
}
|
|
||||||
|
|
||||||
func stringToLogConstant(c string) int32 {
|
|
||||||
level, ok := logConstants[c]
|
|
||||||
if !ok {
|
|
||||||
level = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return level
|
|
||||||
}
|
|
||||||
|
|
||||||
// DelayedStatus describes an online status expiring on timeout
|
// DelayedStatus describes an online status expiring on timeout
|
||||||
type DelayedStatus struct {
|
type DelayedStatus struct {
|
||||||
TimestampOnline int64
|
TimestampOnline int64
|
||||||
|
@ -44,7 +26,7 @@ type DelayedStatus struct {
|
||||||
type Client struct {
|
type Client struct {
|
||||||
client *client.Client
|
client *client.Client
|
||||||
authorizer *clientAuthorizer
|
authorizer *clientAuthorizer
|
||||||
parameters *client.TdlibParameters
|
parameters *client.SetTdlibParametersRequest
|
||||||
options []client.Option
|
options []client.Option
|
||||||
me *client.User
|
me *client.User
|
||||||
|
|
||||||
|
@ -56,26 +38,37 @@ type Client struct {
|
||||||
cache *cache.Cache
|
cache *cache.Cache
|
||||||
online bool
|
online bool
|
||||||
|
|
||||||
|
outbox map[string]string
|
||||||
|
editOutbox map[string]string
|
||||||
|
|
||||||
DelayedStatuses map[int64]*DelayedStatus
|
DelayedStatuses map[int64]*DelayedStatus
|
||||||
DelayedStatusesLock sync.Mutex
|
DelayedStatusesLock sync.Mutex
|
||||||
|
|
||||||
|
lastMsgHashes map[int64]uint64
|
||||||
|
lastMsgIds map[int64]string
|
||||||
|
msgHashSeed maphash.Seed
|
||||||
|
|
||||||
locks clientLocks
|
locks clientLocks
|
||||||
|
SendMessageLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientLocks struct {
|
type clientLocks struct {
|
||||||
authorizationReady sync.Mutex
|
authorizationReady sync.Mutex
|
||||||
chatMessageLocks map[int64]*sync.Mutex
|
chatMessageLocks map[int64]*sync.Mutex
|
||||||
resourcesLock sync.Mutex
|
resourcesLock sync.Mutex
|
||||||
|
outboxLock sync.Mutex
|
||||||
|
editOutboxLock sync.Mutex
|
||||||
|
lastMsgHashesLock sync.Mutex
|
||||||
|
lastMsgIdsLock sync.RWMutex
|
||||||
|
|
||||||
|
authorizerReadLock sync.Mutex
|
||||||
|
authorizerWriteLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient instantiates a Telegram App
|
// NewClient instantiates a Telegram App
|
||||||
func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component, session *persistence.Session) (*Client, error) {
|
func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component, session *persistence.Session) (*Client, error) {
|
||||||
var options []client.Option
|
var options []client.Option
|
||||||
|
|
||||||
options = append(options, client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
|
|
||||||
NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
|
|
||||||
}))
|
|
||||||
|
|
||||||
if conf.Tdlib.Client.CatchTimeout != 0 {
|
if conf.Tdlib.Client.CatchTimeout != 0 {
|
||||||
options = append(options, client.WithCatchTimeout(
|
options = append(options, client.WithCatchTimeout(
|
||||||
time.Duration(conf.Tdlib.Client.CatchTimeout)*time.Second,
|
time.Duration(conf.Tdlib.Client.CatchTimeout)*time.Second,
|
||||||
|
@ -92,7 +85,7 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
|
||||||
datadir = "./sessions/" // ye olde defaute
|
datadir = "./sessions/" // ye olde defaute
|
||||||
}
|
}
|
||||||
|
|
||||||
parameters := client.TdlibParameters{
|
parameters := client.SetTdlibParametersRequest{
|
||||||
UseTestDc: false,
|
UseTestDc: false,
|
||||||
|
|
||||||
DatabaseDirectory: filepath.Join(datadir, jid),
|
DatabaseDirectory: filepath.Join(datadir, jid),
|
||||||
|
@ -123,8 +116,13 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
|
||||||
resources: make(map[string]bool),
|
resources: make(map[string]bool),
|
||||||
content: &conf.Content,
|
content: &conf.Content,
|
||||||
cache: cache.NewCache(),
|
cache: cache.NewCache(),
|
||||||
|
outbox: make(map[string]string),
|
||||||
|
editOutbox: make(map[string]string),
|
||||||
options: options,
|
options: options,
|
||||||
DelayedStatuses: make(map[int64]*DelayedStatus),
|
DelayedStatuses: make(map[int64]*DelayedStatus),
|
||||||
|
lastMsgHashes: make(map[int64]uint64),
|
||||||
|
lastMsgIds: make(map[int64]string),
|
||||||
|
msgHashSeed: maphash.MakeSeed(),
|
||||||
locks: clientLocks{
|
locks: clientLocks{
|
||||||
chatMessageLocks: make(map[int64]*sync.Mutex),
|
chatMessageLocks: make(map[int64]*sync.Mutex),
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
package telegram
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestLogInfo(t *testing.T) {
|
|
||||||
tdlibConstant := stringToLogConstant(":info")
|
|
||||||
if tdlibConstant != 3 {
|
|
||||||
t.Errorf("Wrong TDlib constant for info")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogInvalid(t *testing.T) {
|
|
||||||
tdlibConstant := stringToLogConstant("ziz")
|
|
||||||
if tdlibConstant != 0 {
|
|
||||||
t.Errorf("Unknown strings should return fatal loglevel")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -15,11 +15,11 @@ 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 TelegramAuthDone string = "Authorization is done already"
|
||||||
const notOnline string = "Not online"
|
const notOnline string = "Not online"
|
||||||
|
|
||||||
var permissionsAdmin = client.ChatMemberStatusAdministrator{
|
var permissionsAdmin = client.ChatAdministratorRights{
|
||||||
CanBeEdited: true,
|
|
||||||
CanChangeInfo: true,
|
CanChangeInfo: true,
|
||||||
CanPostMessages: true,
|
CanPostMessages: true,
|
||||||
CanEditMessages: true,
|
CanEditMessages: true,
|
||||||
|
@ -30,14 +30,20 @@ var permissionsAdmin = client.ChatMemberStatusAdministrator{
|
||||||
CanPromoteMembers: false,
|
CanPromoteMembers: false,
|
||||||
}
|
}
|
||||||
var permissionsMember = client.ChatPermissions{
|
var permissionsMember = client.ChatPermissions{
|
||||||
CanSendMessages: true,
|
CanSendBasicMessages: true,
|
||||||
CanSendMediaMessages: true,
|
CanSendAudios: true,
|
||||||
|
CanSendDocuments: true,
|
||||||
|
CanSendPhotos: true,
|
||||||
|
CanSendVideos: true,
|
||||||
|
CanSendVideoNotes: true,
|
||||||
|
CanSendVoiceNotes: true,
|
||||||
CanSendPolls: true,
|
CanSendPolls: true,
|
||||||
CanSendOtherMessages: true,
|
CanSendOtherMessages: true,
|
||||||
CanAddWebPagePreviews: true,
|
CanAddWebPagePreviews: true,
|
||||||
CanChangeInfo: true,
|
CanChangeInfo: true,
|
||||||
CanInviteUsers: true,
|
CanInviteUsers: true,
|
||||||
CanPinMessages: true,
|
CanPinMessages: true,
|
||||||
|
CanManageTopics: true,
|
||||||
}
|
}
|
||||||
var permissionsReadonly = client.ChatPermissions{}
|
var permissionsReadonly = client.ChatPermissions{}
|
||||||
|
|
||||||
|
@ -79,8 +85,8 @@ var chatCommands = map[string]command{
|
||||||
"invite": command{"id or @username", "add user to current chat"},
|
"invite": command{"id or @username", "add user to current chat"},
|
||||||
"link": command{"", "get invite link for current chat"},
|
"link": command{"", "get invite link for current chat"},
|
||||||
"kick": command{"id or @username", "remove user to current chat"},
|
"kick": command{"id or @username", "remove user to current chat"},
|
||||||
"mute": command{"id or @username [hours]", "mute user in current chat"},
|
"mute": command{"[id or @username] [hours]", "mute the whole chat or a user in current chat"},
|
||||||
"unmute": command{"id or @username", "unrestrict user from current chat"},
|
"unmute": command{"[id or @username]", "unmute the whole chat or a user in the current chat"},
|
||||||
"ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
|
"ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
|
||||||
"unban": command{"id or @username", "unbans @username in current chat (and devotes from admins)"},
|
"unban": command{"id or @username", "unbans @username in current chat (and devotes from admins)"},
|
||||||
"promote": command{"id or @username [title]", "promote user to admin in current chat"},
|
"promote": command{"id or @username [title]", "promote user to admin in current chat"},
|
||||||
|
@ -179,18 +185,14 @@ func keyValueString(key, value string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) unsubscribe(chatID int64) error {
|
func (c *Client) unsubscribe(chatID int64) error {
|
||||||
return gateway.SendPresence(
|
args := gateway.SimplePresence(chatID, "unsubscribed")
|
||||||
c.xmpp,
|
return c.sendPresence(args...)
|
||||||
c.jid,
|
|
||||||
gateway.SPFrom(strconv.FormatInt(chatID, 10)),
|
|
||||||
gateway.SPType("unsubscribed"),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
|
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
|
||||||
for i := len(messages) - 1; i >= 0; i-- {
|
for i := len(messages) - 1; i >= 0; i-- {
|
||||||
message := messages[i]
|
message := messages[i]
|
||||||
reply, _ := c.getMessageReply(message)
|
reply, _ := c.getMessageReply(message, false, true)
|
||||||
|
|
||||||
gateway.SendMessage(
|
gateway.SendMessage(
|
||||||
c.jid,
|
c.jid,
|
||||||
|
@ -199,8 +201,9 @@ func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
|
||||||
strconv.FormatInt(message.Id, 10),
|
strconv.FormatInt(message.Id, 10),
|
||||||
c.xmpp,
|
c.xmpp,
|
||||||
reply,
|
reply,
|
||||||
false,
|
|
||||||
"",
|
"",
|
||||||
|
false,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -240,34 +243,28 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd == "login" {
|
if cmd == "login" {
|
||||||
wasSessionLoginEmpty := c.Session.Login == ""
|
err := c.TryLogin(resource, args[0])
|
||||||
c.Session.Login = args[0]
|
|
||||||
|
|
||||||
if wasSessionLoginEmpty && c.authorizer == nil {
|
|
||||||
go func() {
|
|
||||||
err := c.Connect(resource)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(errors.Wrap(err, "TDlib connection failure"))
|
return err.Error()
|
||||||
}
|
|
||||||
}()
|
|
||||||
// a quirk for authorizer to become ready. If it's still not,
|
|
||||||
// nothing bad: the command just needs to be resent again
|
|
||||||
time.Sleep(1e5)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
|
defer c.locks.authorizerWriteLock.Unlock()
|
||||||
|
|
||||||
|
c.authorizer.PhoneNumber <- args[0]
|
||||||
|
} else {
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
|
defer c.locks.authorizerWriteLock.Unlock()
|
||||||
|
|
||||||
if c.authorizer == nil {
|
if c.authorizer == nil {
|
||||||
return telegramNotInitialized
|
return TelegramNotInitialized
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.authorizer.isClosed {
|
if c.authorizer.isClosed {
|
||||||
return "Authorization is done already"
|
return TelegramAuthDone
|
||||||
}
|
}
|
||||||
|
|
||||||
switch cmd {
|
switch cmd {
|
||||||
// sign in
|
|
||||||
case "login":
|
|
||||||
c.authorizer.PhoneNumber <- args[0]
|
|
||||||
// check auth code
|
// check auth code
|
||||||
case "code":
|
case "code":
|
||||||
c.authorizer.Code <- args[0]
|
c.authorizer.Code <- args[0]
|
||||||
|
@ -275,22 +272,22 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
||||||
case "password":
|
case "password":
|
||||||
c.authorizer.Password <- args[0]
|
c.authorizer.Password <- args[0]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// sign out
|
// sign out
|
||||||
case "logout":
|
case "logout":
|
||||||
if !c.Online() {
|
if !c.Online() {
|
||||||
return notOnline
|
return notOnline
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, id := range c.cache.ChatsKeys() {
|
|
||||||
c.unsubscribe(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := c.client.LogOut()
|
_, err := c.client.LogOut()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.forceClose()
|
|
||||||
return errors.Wrap(err, "Logout error").Error()
|
return errors.Wrap(err, "Logout error").Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, id := range c.cache.ChatsKeys() {
|
||||||
|
c.unsubscribe(id)
|
||||||
|
}
|
||||||
|
|
||||||
c.Session.Login = ""
|
c.Session.Login = ""
|
||||||
// cancel auth
|
// cancel auth
|
||||||
case "cancelauth":
|
case "cancelauth":
|
||||||
|
@ -330,10 +327,13 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
||||||
lastname = rawCmdArguments(cmdline, 1)
|
lastname = rawCmdArguments(cmdline, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
if c.authorizer != nil && !c.authorizer.isClosed {
|
if c.authorizer != nil && !c.authorizer.isClosed {
|
||||||
c.authorizer.FirstName <- firstname
|
c.authorizer.FirstName <- firstname
|
||||||
c.authorizer.LastName <- lastname
|
c.authorizer.LastName <- lastname
|
||||||
|
c.locks.authorizerWriteLock.Unlock()
|
||||||
} else {
|
} else {
|
||||||
|
c.locks.authorizerWriteLock.Unlock()
|
||||||
if !c.Online() {
|
if !c.Online() {
|
||||||
return notOnline
|
return notOnline
|
||||||
}
|
}
|
||||||
|
@ -380,7 +380,8 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
||||||
}
|
}
|
||||||
case "config":
|
case "config":
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
if !gateway.MessageOutgoingPermission && args[0] == "carbons" && args[1] == "true" {
|
var msg string
|
||||||
|
if gateway.MessageOutgoingPermissionVersion == 0 && args[0] == "carbons" && args[1] == "true" {
|
||||||
return "The server did not allow to enable carbons"
|
return "The server did not allow to enable carbons"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -390,7 +391,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
||||||
}
|
}
|
||||||
gateway.DirtySessions = true
|
gateway.DirtySessions = true
|
||||||
|
|
||||||
return fmt.Sprintf("%s set to %s", args[0], value)
|
return fmt.Sprintf("%s%s set to %s", msg, args[0], value)
|
||||||
} else if len(args) > 0 {
|
} else if len(args) > 0 {
|
||||||
value, err := c.Session.Get(args[0])
|
value, err := c.Session.Get(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -419,7 +420,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
||||||
text := rawCmdArguments(cmdline, 1)
|
text := rawCmdArguments(cmdline, 1)
|
||||||
_, err = c.client.ReportChat(&client.ReportChatRequest{
|
_, err = c.client.ReportChat(&client.ReportChatRequest{
|
||||||
ChatId: contact.Id,
|
ChatId: contact.Id,
|
||||||
Reason: &client.ChatReportReasonCustom{},
|
Reason: &client.ReportReasonCustom{},
|
||||||
Text: text,
|
Text: text,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -514,11 +515,14 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0))
|
content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0))
|
||||||
|
|
||||||
if content != nil {
|
if content != nil {
|
||||||
c.client.EditMessageText(&client.EditMessageTextRequest{
|
_, err = c.client.EditMessageText(&client.EditMessageTextRequest{
|
||||||
ChatId: chatID,
|
ChatId: chatID,
|
||||||
MessageId: message.Id,
|
MessageId: message.Id,
|
||||||
InputMessageContent: content,
|
InputMessageContent: content,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "Message editing error", true
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return "Message processing error", true
|
return "Message processing error", true
|
||||||
}
|
}
|
||||||
|
@ -664,7 +668,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
entries := []string{
|
entries := []string{
|
||||||
keyValueString("Chat title", info.Fn),
|
keyValueString("Chat title", info.Fn),
|
||||||
keyValueString("Photo", link),
|
keyValueString("Photo", link),
|
||||||
keyValueString("Username", info.Nickname),
|
keyValueString("Usernames", c.usernamesToString(info.Nicknames)),
|
||||||
keyValueString("Full name", info.Given+" "+info.Family),
|
keyValueString("Full name", info.Given+" "+info.Family),
|
||||||
keyValueString("Phone number", info.Tel),
|
keyValueString("Phone number", info.Tel),
|
||||||
}
|
}
|
||||||
|
@ -704,18 +708,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
}
|
}
|
||||||
// blacklists current user
|
// blacklists current user
|
||||||
case "block":
|
case "block":
|
||||||
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
|
_, err := c.client.SetMessageSenderBlockList(&client.SetMessageSenderBlockListRequest{
|
||||||
SenderId: &client.MessageSenderUser{UserId: chatID},
|
SenderId: &client.MessageSenderUser{UserId: chatID},
|
||||||
IsBlocked: true,
|
BlockList: &client.BlockListMain{},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error(), true
|
return err.Error(), true
|
||||||
}
|
}
|
||||||
// unblacklists current user
|
// unblacklists current user
|
||||||
case "unblock":
|
case "unblock":
|
||||||
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
|
_, err := c.client.SetMessageSenderBlockList(&client.SetMessageSenderBlockListRequest{
|
||||||
SenderId: &client.MessageSenderUser{UserId: chatID},
|
SenderId: &client.MessageSenderUser{UserId: chatID},
|
||||||
IsBlocked: false,
|
BlockList: nil,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error(), true
|
return err.Error(), true
|
||||||
|
@ -767,12 +771,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error(), true
|
return err.Error(), true
|
||||||
}
|
}
|
||||||
// mute @username [n hours]
|
// mute [@username [n hours]]
|
||||||
case "mute":
|
case "mute":
|
||||||
if len(args) < 1 {
|
if len(args) > 0 {
|
||||||
return notEnoughArguments, true
|
|
||||||
}
|
|
||||||
|
|
||||||
contact, _, err := c.GetContactByUsername(args[0])
|
contact, _, err := c.GetContactByUsername(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error(), true
|
return err.Error(), true
|
||||||
|
@ -798,12 +799,15 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error(), true
|
return err.Error(), true
|
||||||
}
|
}
|
||||||
// unmute @username
|
} else {
|
||||||
case "unmute":
|
if !c.Session.IgnoreChat(chatID) {
|
||||||
if len(args) < 1 {
|
return "Chat is already ignored", true
|
||||||
return notEnoughArguments, true
|
|
||||||
}
|
}
|
||||||
|
gateway.DirtySessions = true
|
||||||
|
}
|
||||||
|
// unmute [@username]
|
||||||
|
case "unmute":
|
||||||
|
if len(args) > 0 {
|
||||||
contact, _, err := c.GetContactByUsername(args[0])
|
contact, _, err := c.GetContactByUsername(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error(), true
|
return err.Error(), true
|
||||||
|
@ -821,6 +825,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error(), true
|
return err.Error(), true
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if !c.Session.UnignoreChat(chatID) {
|
||||||
|
return "Chat wasn't ignored", true
|
||||||
|
}
|
||||||
|
gateway.DirtySessions = true
|
||||||
|
}
|
||||||
// ban @username from current chat [for N hours]
|
// ban @username from current chat [for N hours]
|
||||||
case "ban":
|
case "ban":
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
|
@ -881,7 +891,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// clone the permissions
|
// clone the permissions
|
||||||
status := permissionsAdmin
|
status := client.ChatMemberStatusAdministrator{
|
||||||
|
CanBeEdited: true,
|
||||||
|
Rights: &permissionsAdmin,
|
||||||
|
}
|
||||||
|
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
status.CustomTitle = args[1]
|
status.CustomTitle = args[1]
|
||||||
|
@ -931,9 +944,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
return "Invalid TTL", true
|
return "Invalid TTL", true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_, err = c.client.SetChatMessageTtl(&client.SetChatMessageTtlRequest{
|
_, err = c.client.SetChatMessageAutoDeleteTime(&client.SetChatMessageAutoDeleteTimeRequest{
|
||||||
ChatId: chatID,
|
ChatId: chatID,
|
||||||
Ttl: int32(ttl),
|
MessageAutoDeleteTime: int32(ttl),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,7 +2,7 @@ package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"strconv"
|
"time"
|
||||||
|
|
||||||
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ import (
|
||||||
const chatsLimit int32 = 999
|
const chatsLimit int32 = 999
|
||||||
|
|
||||||
type clientAuthorizer struct {
|
type clientAuthorizer struct {
|
||||||
TdlibParameters chan *client.TdlibParameters
|
TdlibParameters chan *client.SetTdlibParametersRequest
|
||||||
PhoneNumber chan string
|
PhoneNumber chan string
|
||||||
Code chan string
|
Code chan string
|
||||||
State chan client.AuthorizationState
|
State chan client.AuthorizationState
|
||||||
|
@ -31,13 +31,7 @@ func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.Auth
|
||||||
|
|
||||||
switch state.AuthorizationStateType() {
|
switch state.AuthorizationStateType() {
|
||||||
case client.TypeAuthorizationStateWaitTdlibParameters:
|
case client.TypeAuthorizationStateWaitTdlibParameters:
|
||||||
_, err := c.SetTdlibParameters(&client.SetTdlibParametersRequest{
|
_, err := c.SetTdlibParameters(<-stateHandler.TdlibParameters)
|
||||||
Parameters: <-stateHandler.TdlibParameters,
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
|
|
||||||
case client.TypeAuthorizationStateWaitEncryptionKey:
|
|
||||||
_, err := c.CheckDatabaseEncryptionKey(&client.CheckDatabaseEncryptionKeyRequest{})
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
case client.TypeAuthorizationStateWaitPhoneNumber:
|
case client.TypeAuthorizationStateWaitPhoneNumber:
|
||||||
|
@ -74,10 +68,10 @@ func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.Auth
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case client.TypeAuthorizationStateLoggingOut:
|
case client.TypeAuthorizationStateLoggingOut:
|
||||||
return client.ErrNotSupportedAuthorizationState
|
return nil
|
||||||
|
|
||||||
case client.TypeAuthorizationStateClosing:
|
case client.TypeAuthorizationStateClosing:
|
||||||
return client.ErrNotSupportedAuthorizationState
|
return nil
|
||||||
|
|
||||||
case client.TypeAuthorizationStateClosed:
|
case client.TypeAuthorizationStateClosed:
|
||||||
return client.ErrNotSupportedAuthorizationState
|
return client.ErrNotSupportedAuthorizationState
|
||||||
|
@ -115,8 +109,9 @@ func (c *Client) Connect(resource string) error {
|
||||||
|
|
||||||
log.Warn("Connecting to Telegram network...")
|
log.Warn("Connecting to Telegram network...")
|
||||||
|
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
c.authorizer = &clientAuthorizer{
|
c.authorizer = &clientAuthorizer{
|
||||||
TdlibParameters: make(chan *client.TdlibParameters, 1),
|
TdlibParameters: make(chan *client.SetTdlibParametersRequest, 1),
|
||||||
PhoneNumber: make(chan string, 1),
|
PhoneNumber: make(chan string, 1),
|
||||||
Code: make(chan string, 1),
|
Code: make(chan string, 1),
|
||||||
State: make(chan client.AuthorizationState, 10),
|
State: make(chan client.AuthorizationState, 10),
|
||||||
|
@ -126,8 +121,10 @@ func (c *Client) Connect(resource string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
go c.interactor()
|
go c.interactor()
|
||||||
|
log.Warn("Interactor launched")
|
||||||
|
|
||||||
c.authorizer.TdlibParameters <- c.parameters
|
c.authorizer.TdlibParameters <- c.parameters
|
||||||
|
c.locks.authorizerWriteLock.Unlock()
|
||||||
|
|
||||||
tdlibClient, err := client.NewClient(c.authorizer, c.options...)
|
tdlibClient, err := client.NewClient(c.authorizer, c.options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -160,14 +157,55 @@ func (c *Client) Connect(resource string) error {
|
||||||
log.Errorf("Could not retrieve chats: %v", err)
|
log.Errorf("Could not retrieve chats: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribe"))
|
gateway.SubscribeToTransport(c.xmpp, c.jid)
|
||||||
gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribed"))
|
c.sendPresence(gateway.SPStatus("Logged in as: " + c.Session.Login))
|
||||||
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login))
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) TryLogin(resource string, login string) error {
|
||||||
|
wasSessionLoginEmpty := c.Session.Login == ""
|
||||||
|
c.Session.Login = login
|
||||||
|
|
||||||
|
if wasSessionLoginEmpty && c.authorizer == nil {
|
||||||
|
go func() {
|
||||||
|
err := c.Connect(resource)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(errors.Wrap(err, "TDlib connection failure"))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
// a quirk for authorizer to become ready. If it's still not,
|
||||||
|
// nothing bad: just re-login again
|
||||||
|
time.Sleep(1e5)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
|
defer c.locks.authorizerWriteLock.Unlock()
|
||||||
|
|
||||||
|
if c.authorizer == nil {
|
||||||
|
return errors.New(TelegramNotInitialized)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.authorizer.isClosed {
|
||||||
|
return errors.New(TelegramAuthDone)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) SetPhoneNumber(login string) error {
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
|
defer c.locks.authorizerWriteLock.Unlock()
|
||||||
|
|
||||||
|
if c.authorizer == nil || c.authorizer.isClosed {
|
||||||
|
return errors.New("Authorization not needed")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.authorizer.PhoneNumber <- login
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Disconnect drops TDlib connection and
|
// Disconnect drops TDlib connection and
|
||||||
// returns the flag indicating if disconnecting is permitted
|
// returns the flag indicating if disconnecting is permitted
|
||||||
func (c *Client) Disconnect(resource string, quit bool) bool {
|
func (c *Client) Disconnect(resource string, quit bool) bool {
|
||||||
|
@ -189,12 +227,8 @@ func (c *Client) Disconnect(resource string, quit bool) bool {
|
||||||
|
|
||||||
// we're offline (unsubscribe if logout)
|
// we're offline (unsubscribe if logout)
|
||||||
for _, id := range c.cache.ChatsKeys() {
|
for _, id := range c.cache.ChatsKeys() {
|
||||||
gateway.SendPresence(
|
args := gateway.SimplePresence(id, "unavailable")
|
||||||
c.xmpp,
|
c.sendPresence(args...)
|
||||||
c.jid,
|
|
||||||
gateway.SPFrom(strconv.FormatInt(id, 10)),
|
|
||||||
gateway.SPType("unavailable"),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.close()
|
c.close()
|
||||||
|
@ -204,9 +238,16 @@ func (c *Client) Disconnect(resource string, quit bool) bool {
|
||||||
|
|
||||||
func (c *Client) interactor() {
|
func (c *Client) interactor() {
|
||||||
for {
|
for {
|
||||||
|
c.locks.authorizerReadLock.Lock()
|
||||||
|
if c.authorizer == nil {
|
||||||
|
log.Warn("Authorizer is lost, halting the interactor")
|
||||||
|
c.locks.authorizerReadLock.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
state, ok := <-c.authorizer.State
|
state, ok := <-c.authorizer.State
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Warn("Interactor is disconnected")
|
log.Warn("Interactor is disconnected")
|
||||||
|
c.locks.authorizerReadLock.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,18 +277,27 @@ func (c *Client) interactor() {
|
||||||
log.Warn("Waiting for 2FA password...")
|
log.Warn("Waiting for 2FA password...")
|
||||||
gateway.SendServiceMessage(c.jid, "Please, enter 2FA passphrase via /password 12345", c.xmpp)
|
gateway.SendServiceMessage(c.jid, "Please, enter 2FA passphrase via /password 12345", c.xmpp)
|
||||||
}
|
}
|
||||||
|
c.locks.authorizerReadLock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) forceClose() {
|
func (c *Client) forceClose() {
|
||||||
|
c.locks.authorizerReadLock.Lock()
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
|
defer c.locks.authorizerReadLock.Unlock()
|
||||||
|
defer c.locks.authorizerWriteLock.Unlock()
|
||||||
|
|
||||||
c.online = false
|
c.online = false
|
||||||
c.authorizer = nil
|
c.authorizer = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) close() {
|
func (c *Client) close() {
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
if c.authorizer != nil && !c.authorizer.isClosed {
|
if c.authorizer != nil && !c.authorizer.isClosed {
|
||||||
c.authorizer.Close()
|
c.authorizer.Close()
|
||||||
}
|
}
|
||||||
|
c.locks.authorizerWriteLock.Unlock()
|
||||||
|
|
||||||
if c.client != nil {
|
if c.client != nil {
|
||||||
_, err := c.client.Close()
|
_, err := c.client.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -8,15 +8,31 @@ import (
|
||||||
"github.com/zelenin/go-tdlib/client"
|
"github.com/zelenin/go-tdlib/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Insertion is a piece of text in given position
|
type insertionType int
|
||||||
type Insertion struct {
|
|
||||||
|
const (
|
||||||
|
insertionOpening insertionType = iota
|
||||||
|
insertionClosing
|
||||||
|
insertionUnpaired
|
||||||
|
)
|
||||||
|
|
||||||
|
type MarkupModeType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
MarkupModeXEP0393 MarkupModeType = iota
|
||||||
|
MarkupModeMarkdown
|
||||||
|
)
|
||||||
|
|
||||||
|
// insertion is a piece of text in given position
|
||||||
|
type insertion struct {
|
||||||
Offset int32
|
Offset int32
|
||||||
Runes []rune
|
Runes []rune
|
||||||
|
Type insertionType
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertionStack contains the sequence of insertions
|
// insertionStack contains the sequence of insertions
|
||||||
// from the start or from the end
|
// from the start or from the end
|
||||||
type InsertionStack []*Insertion
|
type insertionStack []*insertion
|
||||||
|
|
||||||
var boldRunesMarkdown = []rune("**")
|
var boldRunesMarkdown = []rune("**")
|
||||||
var boldRunesXEP0393 = []rune("*")
|
var boldRunesXEP0393 = []rune("*")
|
||||||
|
@ -24,13 +40,18 @@ var italicRunes = []rune("_")
|
||||||
var strikeRunesMarkdown = []rune("~~")
|
var strikeRunesMarkdown = []rune("~~")
|
||||||
var strikeRunesXEP0393 = []rune("~")
|
var strikeRunesXEP0393 = []rune("~")
|
||||||
var codeRunes = []rune("`")
|
var codeRunes = []rune("`")
|
||||||
var preRuneStart = []rune("```\n")
|
var preRunesStart = []rune("```\n")
|
||||||
var preRuneEnd = []rune("\n```")
|
var preRunesEnd = []rune("\n```")
|
||||||
|
var quoteRunes = []rune("> ")
|
||||||
|
var newlineRunes = []rune("\n")
|
||||||
|
var doubleNewlineRunes = []rune("\n\n")
|
||||||
|
var newlineCode = rune(0x0000000a)
|
||||||
|
var bmpCeil = rune(0x0000ffff)
|
||||||
|
|
||||||
// rebalance pumps all the values until the given offset to current stack (growing
|
// rebalance pumps all the values until the given offset to current stack (growing
|
||||||
// from start) from given stack (growing from end); should be called
|
// from start) from given stack (growing from end); should be called
|
||||||
// before any insertions to the current stack at the given offset
|
// before any insertions to the current stack at the given offset
|
||||||
func (s InsertionStack) rebalance(s2 InsertionStack, offset int32) (InsertionStack, InsertionStack) {
|
func (s insertionStack) rebalance(s2 insertionStack, offset int32) (insertionStack, insertionStack) {
|
||||||
for len(s2) > 0 && s2[len(s2)-1].Offset <= offset {
|
for len(s2) > 0 && s2[len(s2)-1].Offset <= offset {
|
||||||
s = append(s, s2[len(s2)-1])
|
s = append(s, s2[len(s2)-1])
|
||||||
s2 = s2[:len(s2)-1]
|
s2 = s2[:len(s2)-1]
|
||||||
|
@ -41,10 +62,10 @@ func (s InsertionStack) rebalance(s2 InsertionStack, offset int32) (InsertionSta
|
||||||
|
|
||||||
// NewIterator is a second order function that sequentially scans and returns
|
// NewIterator is a second order function that sequentially scans and returns
|
||||||
// stack elements; starts returning nil when elements are ended
|
// stack elements; starts returning nil when elements are ended
|
||||||
func (s InsertionStack) NewIterator() func() *Insertion {
|
func (s insertionStack) NewIterator() func() *insertion {
|
||||||
i := -1
|
i := -1
|
||||||
|
|
||||||
return func() *Insertion {
|
return func() *insertion {
|
||||||
i++
|
i++
|
||||||
if i < len(s) {
|
if i < len(s) {
|
||||||
return s[i]
|
return s[i]
|
||||||
|
@ -120,21 +141,10 @@ func MergeAdjacentEntities(entities []*client.TextEntity) []*client.TextEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClaspDirectives to the following span as required by XEP-0393
|
// ClaspDirectives to the following span as required by XEP-0393
|
||||||
func ClaspDirectives(text string, entities []*client.TextEntity) []*client.TextEntity {
|
func ClaspDirectives(doubledRunes []rune, entities []*client.TextEntity) []*client.TextEntity {
|
||||||
alignedEntities := make([]*client.TextEntity, len(entities))
|
alignedEntities := make([]*client.TextEntity, len(entities))
|
||||||
copy(alignedEntities, entities)
|
copy(alignedEntities, entities)
|
||||||
|
|
||||||
// transform the source text into a form with uniform runes and code points,
|
|
||||||
// by duplicating the Basic Multilingual Plane
|
|
||||||
doubledRunes := make([]rune, 0, len(text)*2)
|
|
||||||
|
|
||||||
for _, cp := range text {
|
|
||||||
if cp > 0x0000ffff {
|
|
||||||
doubledRunes = append(doubledRunes, cp, cp)
|
|
||||||
} else {
|
|
||||||
doubledRunes = append(doubledRunes, cp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for i, entity := range alignedEntities {
|
for i, entity := range alignedEntities {
|
||||||
var dirty bool
|
var dirty bool
|
||||||
endOffset := entity.Offset + entity.Length
|
endOffset := entity.Offset + entity.Length
|
||||||
|
@ -167,18 +177,89 @@ func ClaspDirectives(text string, entities []*client.TextEntity) []*client.TextE
|
||||||
return alignedEntities
|
return alignedEntities
|
||||||
}
|
}
|
||||||
|
|
||||||
func markupBraces(entity *client.TextEntity, lbrace, rbrace []rune) (*Insertion, *Insertion) {
|
func markupBraces(entity *client.TextEntity, lbrace, rbrace []rune) []*insertion {
|
||||||
return &Insertion{
|
return []*insertion{
|
||||||
|
&insertion{
|
||||||
Offset: entity.Offset,
|
Offset: entity.Offset,
|
||||||
Runes: lbrace,
|
Runes: lbrace,
|
||||||
}, &Insertion{
|
Type: insertionOpening,
|
||||||
|
},
|
||||||
|
&insertion{
|
||||||
Offset: entity.Offset + entity.Length,
|
Offset: entity.Offset + entity.Length,
|
||||||
Runes: rbrace,
|
Runes: rbrace,
|
||||||
|
Type: insertionClosing,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EntityToMarkdown generates the wrapping Markdown tags
|
func quotePrependNewlines(entity *client.TextEntity, doubledRunes []rune, markupMode MarkupModeType) []*insertion {
|
||||||
func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) {
|
if len(doubledRunes) == 0 {
|
||||||
|
return []*insertion{}
|
||||||
|
}
|
||||||
|
|
||||||
|
startRunes := []rune("\n> ")
|
||||||
|
if entity.Offset == 0 || doubledRunes[entity.Offset-1] == newlineCode {
|
||||||
|
startRunes = quoteRunes
|
||||||
|
}
|
||||||
|
insertions := []*insertion{
|
||||||
|
&insertion{
|
||||||
|
Offset: entity.Offset,
|
||||||
|
Runes: startRunes,
|
||||||
|
Type: insertionUnpaired,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
entityEnd := entity.Offset + entity.Length
|
||||||
|
entityEndInt := int(entityEnd)
|
||||||
|
|
||||||
|
var wasNewline bool
|
||||||
|
// last newline is omitted, there's no need to put quote mark after the quote
|
||||||
|
for i := entity.Offset; i < entityEnd-1; i++ {
|
||||||
|
isNewline := doubledRunes[i] == newlineCode
|
||||||
|
if (isNewline && markupMode == MarkupModeXEP0393) || (wasNewline && isNewline && markupMode == MarkupModeMarkdown) {
|
||||||
|
insertions = append(insertions, &insertion{
|
||||||
|
Offset: i + 1,
|
||||||
|
Runes: quoteRunes,
|
||||||
|
Type: insertionUnpaired,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if isNewline {
|
||||||
|
wasNewline = true
|
||||||
|
} else {
|
||||||
|
wasNewline = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var rbrace []rune
|
||||||
|
if len(doubledRunes) > entityEndInt {
|
||||||
|
if doubledRunes[entityEnd] == newlineCode {
|
||||||
|
if markupMode == MarkupModeMarkdown && len(doubledRunes) > entityEndInt+1 && doubledRunes[entityEndInt+1] != newlineCode {
|
||||||
|
rbrace = newlineRunes
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if markupMode == MarkupModeMarkdown {
|
||||||
|
rbrace = doubleNewlineRunes
|
||||||
|
} else {
|
||||||
|
rbrace = newlineRunes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
insertions = append(insertions, &insertion{
|
||||||
|
Offset: entityEnd,
|
||||||
|
Runes: rbrace,
|
||||||
|
Type: insertionClosing,
|
||||||
|
})
|
||||||
|
|
||||||
|
return insertions
|
||||||
|
}
|
||||||
|
|
||||||
|
// entityToMarkdown generates the wrapping Markdown tags
|
||||||
|
func entityToMarkdown(entity *client.TextEntity, doubledRunes []rune, markupMode MarkupModeType) []*insertion {
|
||||||
|
if entity == nil || entity.Type == nil {
|
||||||
|
return []*insertion{}
|
||||||
|
}
|
||||||
|
|
||||||
switch entity.Type.TextEntityTypeType() {
|
switch entity.Type.TextEntityTypeType() {
|
||||||
case client.TypeTextEntityTypeBold:
|
case client.TypeTextEntityTypeBold:
|
||||||
return markupBraces(entity, boldRunesMarkdown, boldRunesMarkdown)
|
return markupBraces(entity, boldRunesMarkdown, boldRunesMarkdown)
|
||||||
|
@ -189,22 +270,24 @@ func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) {
|
||||||
case client.TypeTextEntityTypeCode:
|
case client.TypeTextEntityTypeCode:
|
||||||
return markupBraces(entity, codeRunes, codeRunes)
|
return markupBraces(entity, codeRunes, codeRunes)
|
||||||
case client.TypeTextEntityTypePre:
|
case client.TypeTextEntityTypePre:
|
||||||
return markupBraces(entity, preRuneStart, preRuneEnd)
|
return markupBraces(entity, preRunesStart, preRunesEnd)
|
||||||
case client.TypeTextEntityTypePreCode:
|
case client.TypeTextEntityTypePreCode:
|
||||||
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
|
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
|
||||||
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
|
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), preRunesEnd)
|
||||||
|
case client.TypeTextEntityTypeBlockQuote:
|
||||||
|
return quotePrependNewlines(entity, doubledRunes, MarkupModeMarkdown)
|
||||||
case client.TypeTextEntityTypeTextUrl:
|
case client.TypeTextEntityTypeTextUrl:
|
||||||
textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
|
textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
|
||||||
return markupBraces(entity, []rune("["), []rune("]("+textURL.Url+")"))
|
return markupBraces(entity, []rune("["), []rune("]("+textURL.Url+")"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return []*insertion{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EntityToXEP0393 generates the wrapping XEP-0393 tags
|
// entityToXEP0393 generates the wrapping XEP-0393 tags
|
||||||
func EntityToXEP0393(entity *client.TextEntity) (*Insertion, *Insertion) {
|
func entityToXEP0393(entity *client.TextEntity, doubledRunes []rune, markupMode MarkupModeType) []*insertion {
|
||||||
if entity == nil || entity.Type == nil {
|
if entity == nil || entity.Type == nil {
|
||||||
return nil, nil
|
return []*insertion{}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch entity.Type.TextEntityTypeType() {
|
switch entity.Type.TextEntityTypeType() {
|
||||||
|
@ -217,33 +300,59 @@ func EntityToXEP0393(entity *client.TextEntity) (*Insertion, *Insertion) {
|
||||||
case client.TypeTextEntityTypeCode:
|
case client.TypeTextEntityTypeCode:
|
||||||
return markupBraces(entity, codeRunes, codeRunes)
|
return markupBraces(entity, codeRunes, codeRunes)
|
||||||
case client.TypeTextEntityTypePre:
|
case client.TypeTextEntityTypePre:
|
||||||
return markupBraces(entity, preRuneStart, preRuneEnd)
|
return markupBraces(entity, preRunesStart, preRunesEnd)
|
||||||
case client.TypeTextEntityTypePreCode:
|
case client.TypeTextEntityTypePreCode:
|
||||||
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
|
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
|
||||||
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
|
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), preRunesEnd)
|
||||||
|
case client.TypeTextEntityTypeBlockQuote:
|
||||||
|
return quotePrependNewlines(entity, doubledRunes, MarkupModeXEP0393)
|
||||||
case client.TypeTextEntityTypeTextUrl:
|
case client.TypeTextEntityTypeTextUrl:
|
||||||
textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
|
textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
|
||||||
// non-standard, Pidgin-specific
|
// non-standard, Pidgin-specific
|
||||||
return markupBraces(entity, []rune{}, []rune(" <"+textURL.Url+">"))
|
return markupBraces(entity, []rune{}, []rune(" <"+textURL.Url+">"))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return []*insertion{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// transform the source text into a form with uniform runes and code points,
|
||||||
|
// by duplicating anything beyond the Basic Multilingual Plane
|
||||||
|
func textToDoubledRunes(text string) []rune {
|
||||||
|
doubledRunes := make([]rune, 0, len(text)*2)
|
||||||
|
for _, cp := range text {
|
||||||
|
if cp > bmpCeil {
|
||||||
|
doubledRunes = append(doubledRunes, cp, cp)
|
||||||
|
} else {
|
||||||
|
doubledRunes = append(doubledRunes, cp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return doubledRunes
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format traverses an already sorted list of entities and wraps the text in a markup
|
// Format traverses an already sorted list of entities and wraps the text in a markup
|
||||||
func Format(
|
func Format(
|
||||||
sourceText string,
|
sourceText string,
|
||||||
entities []*client.TextEntity,
|
entities []*client.TextEntity,
|
||||||
entityToMarkup func(*client.TextEntity) (*Insertion, *Insertion),
|
markupMode MarkupModeType,
|
||||||
) string {
|
) string {
|
||||||
if len(entities) == 0 {
|
if len(entities) == 0 {
|
||||||
return sourceText
|
return sourceText
|
||||||
}
|
}
|
||||||
|
|
||||||
mergedEntities := SortEntities(ClaspDirectives(sourceText, MergeAdjacentEntities(SortEntities(entities))))
|
var entityToMarkup func(*client.TextEntity, []rune, MarkupModeType) []*insertion
|
||||||
|
if markupMode == MarkupModeXEP0393 {
|
||||||
|
entityToMarkup = entityToXEP0393
|
||||||
|
} else {
|
||||||
|
entityToMarkup = entityToMarkdown
|
||||||
|
}
|
||||||
|
|
||||||
startStack := make(InsertionStack, 0, len(sourceText))
|
doubledRunes := textToDoubledRunes(sourceText)
|
||||||
endStack := make(InsertionStack, 0, len(sourceText))
|
|
||||||
|
mergedEntities := SortEntities(ClaspDirectives(doubledRunes, MergeAdjacentEntities(SortEntities(entities))))
|
||||||
|
|
||||||
|
startStack := make(insertionStack, 0, len(sourceText))
|
||||||
|
endStack := make(insertionStack, 0, len(sourceText))
|
||||||
|
|
||||||
// convert entities to a stack of brackets
|
// convert entities to a stack of brackets
|
||||||
var maxEndOffset int32
|
var maxEndOffset int32
|
||||||
|
@ -260,36 +369,70 @@ func Format(
|
||||||
|
|
||||||
startStack, endStack = startStack.rebalance(endStack, entity.Offset)
|
startStack, endStack = startStack.rebalance(endStack, entity.Offset)
|
||||||
|
|
||||||
startInsertion, endInsertion := entityToMarkup(entity)
|
insertions := entityToMarkup(entity, doubledRunes, markupMode)
|
||||||
if startInsertion != nil {
|
if len(insertions) > 1 {
|
||||||
startStack = append(startStack, startInsertion)
|
startStack = append(startStack, insertions[0:len(insertions)-1]...)
|
||||||
}
|
}
|
||||||
if endInsertion != nil {
|
if len(insertions) > 0 {
|
||||||
endStack = append(endStack, endInsertion)
|
endStack = append(endStack, insertions[len(insertions)-1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// flush the closing brackets that still remain in endStack
|
// flush the closing brackets that still remain in endStack
|
||||||
startStack, endStack = startStack.rebalance(endStack, maxEndOffset)
|
startStack, endStack = startStack.rebalance(endStack, maxEndOffset)
|
||||||
|
// sort unpaired insertions
|
||||||
|
sort.SliceStable(startStack, func(i int, j int) bool {
|
||||||
|
ins1 := startStack[i]
|
||||||
|
ins2 := startStack[j]
|
||||||
|
if ins1.Type == insertionUnpaired && ins2.Type == insertionUnpaired {
|
||||||
|
return ins1.Offset < ins2.Offset
|
||||||
|
}
|
||||||
|
if ins1.Type == insertionUnpaired {
|
||||||
|
if ins1.Offset == ins2.Offset {
|
||||||
|
if ins2.Type == insertionOpening { // > **
|
||||||
|
return true
|
||||||
|
} else if ins2.Type == insertionClosing { // **>
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return ins1.Offset < ins2.Offset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ins2.Type == insertionUnpaired {
|
||||||
|
if ins1.Offset == ins2.Offset {
|
||||||
|
if ins1.Type == insertionOpening { // > **
|
||||||
|
return false
|
||||||
|
} else if ins1.Type == insertionClosing { // **>
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return ins1.Offset < ins2.Offset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
// merge brackets into text
|
// merge brackets into text
|
||||||
markupRunes := make([]rune, 0, len(sourceText))
|
markupRunes := make([]rune, 0, len(sourceText))
|
||||||
|
|
||||||
nextInsertion := startStack.NewIterator()
|
nextInsertion := startStack.NewIterator()
|
||||||
insertion := nextInsertion()
|
insertion := nextInsertion()
|
||||||
var runeI int32
|
var skipNext bool
|
||||||
|
|
||||||
for _, cp := range sourceText {
|
for i, cp := range doubledRunes {
|
||||||
for insertion != nil && insertion.Offset <= runeI {
|
if skipNext {
|
||||||
|
skipNext = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for insertion != nil && int(insertion.Offset) <= i {
|
||||||
markupRunes = append(markupRunes, insertion.Runes...)
|
markupRunes = append(markupRunes, insertion.Runes...)
|
||||||
insertion = nextInsertion()
|
insertion = nextInsertion()
|
||||||
}
|
}
|
||||||
|
|
||||||
markupRunes = append(markupRunes, cp)
|
markupRunes = append(markupRunes, cp)
|
||||||
// skip two UTF-16 code units (not points actually!) if needed
|
// skip two UTF-16 code units (not points actually!) if needed
|
||||||
if cp > 0x0000ffff {
|
if cp > bmpCeil {
|
||||||
runeI += 2
|
skipNext = true
|
||||||
} else {
|
|
||||||
runeI++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for insertion != nil {
|
for insertion != nil {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNoFormatting(t *testing.T) {
|
func TestNoFormatting(t *testing.T) {
|
||||||
markup := Format("abc\ndef", []*client.TextEntity{}, EntityToMarkdown)
|
markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeMarkdown)
|
||||||
if markup != "abc\ndef" {
|
if markup != "abc\ndef" {
|
||||||
t.Errorf("No formatting expected, but: %v", markup)
|
t.Errorf("No formatting expected, but: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ func TestFormattingSimple(t *testing.T) {
|
||||||
Length: 4,
|
Length: 4,
|
||||||
Type: &client.TextEntityTypeBold{},
|
Type: &client.TextEntityTypeBold{},
|
||||||
},
|
},
|
||||||
}, EntityToMarkdown)
|
}, MarkupModeMarkdown)
|
||||||
if markup != "👙**🐧🐖**" {
|
if markup != "👙**🐧🐖**" {
|
||||||
t.Errorf("Wrong simple formatting: %v", markup)
|
t.Errorf("Wrong simple formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ func TestFormattingAdjacent(t *testing.T) {
|
||||||
Url: "https://narayana.im/",
|
Url: "https://narayana.im/",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, EntityToMarkdown)
|
}, MarkupModeMarkdown)
|
||||||
if markup != "a👙_🐧_[🐖](https://narayana.im/)" {
|
if markup != "a👙_🐧_[🐖](https://narayana.im/)" {
|
||||||
t.Errorf("Wrong adjacent formatting: %v", markup)
|
t.Errorf("Wrong adjacent formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -63,18 +63,18 @@ func TestFormattingAdjacentAndNested(t *testing.T) {
|
||||||
Length: 2,
|
Length: 2,
|
||||||
Type: &client.TextEntityTypeItalic{},
|
Type: &client.TextEntityTypeItalic{},
|
||||||
},
|
},
|
||||||
}, EntityToMarkdown)
|
}, MarkupModeMarkdown)
|
||||||
if markup != "```\n**👙**🐧\n```_🐖_" {
|
if markup != "```\n**👙**🐧\n```_🐖_" {
|
||||||
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
|
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRebalanceTwoZero(t *testing.T) {
|
func TestRebalanceTwoZero(t *testing.T) {
|
||||||
s1 := InsertionStack{
|
s1 := insertionStack{
|
||||||
&Insertion{Offset: 7},
|
&insertion{Offset: 7},
|
||||||
&Insertion{Offset: 8},
|
&insertion{Offset: 8},
|
||||||
}
|
}
|
||||||
s2 := InsertionStack{}
|
s2 := insertionStack{}
|
||||||
s1, s2 = s1.rebalance(s2, 7)
|
s1, s2 = s1.rebalance(s2, 7)
|
||||||
if !(len(s1) == 2 && len(s2) == 0 && s1[0].Offset == 7 && s1[1].Offset == 8) {
|
if !(len(s1) == 2 && len(s2) == 0 && s1[0].Offset == 7 && s1[1].Offset == 8) {
|
||||||
t.Errorf("Wrong rebalance 2–0: %#v %#v", s1, s2)
|
t.Errorf("Wrong rebalance 2–0: %#v %#v", s1, s2)
|
||||||
|
@ -82,13 +82,13 @@ func TestRebalanceTwoZero(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRebalanceNeeded(t *testing.T) {
|
func TestRebalanceNeeded(t *testing.T) {
|
||||||
s1 := InsertionStack{
|
s1 := insertionStack{
|
||||||
&Insertion{Offset: 7},
|
&insertion{Offset: 7},
|
||||||
&Insertion{Offset: 8},
|
&insertion{Offset: 8},
|
||||||
}
|
}
|
||||||
s2 := InsertionStack{
|
s2 := insertionStack{
|
||||||
&Insertion{Offset: 10},
|
&insertion{Offset: 10},
|
||||||
&Insertion{Offset: 9},
|
&insertion{Offset: 9},
|
||||||
}
|
}
|
||||||
s1, s2 = s1.rebalance(s2, 9)
|
s1, s2 = s1.rebalance(s2, 9)
|
||||||
if !(len(s1) == 3 && len(s2) == 1 &&
|
if !(len(s1) == 3 && len(s2) == 1 &&
|
||||||
|
@ -99,13 +99,13 @@ func TestRebalanceNeeded(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRebalanceNotNeeded(t *testing.T) {
|
func TestRebalanceNotNeeded(t *testing.T) {
|
||||||
s1 := InsertionStack{
|
s1 := insertionStack{
|
||||||
&Insertion{Offset: 7},
|
&insertion{Offset: 7},
|
||||||
&Insertion{Offset: 8},
|
&insertion{Offset: 8},
|
||||||
}
|
}
|
||||||
s2 := InsertionStack{
|
s2 := insertionStack{
|
||||||
&Insertion{Offset: 10},
|
&insertion{Offset: 10},
|
||||||
&Insertion{Offset: 9},
|
&insertion{Offset: 9},
|
||||||
}
|
}
|
||||||
s1, s2 = s1.rebalance(s2, 8)
|
s1, s2 = s1.rebalance(s2, 8)
|
||||||
if !(len(s1) == 2 && len(s2) == 2 &&
|
if !(len(s1) == 2 && len(s2) == 2 &&
|
||||||
|
@ -116,13 +116,13 @@ func TestRebalanceNotNeeded(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRebalanceLate(t *testing.T) {
|
func TestRebalanceLate(t *testing.T) {
|
||||||
s1 := InsertionStack{
|
s1 := insertionStack{
|
||||||
&Insertion{Offset: 7},
|
&insertion{Offset: 7},
|
||||||
&Insertion{Offset: 8},
|
&insertion{Offset: 8},
|
||||||
}
|
}
|
||||||
s2 := InsertionStack{
|
s2 := insertionStack{
|
||||||
&Insertion{Offset: 10},
|
&insertion{Offset: 10},
|
||||||
&Insertion{Offset: 9},
|
&insertion{Offset: 9},
|
||||||
}
|
}
|
||||||
s1, s2 = s1.rebalance(s2, 10)
|
s1, s2 = s1.rebalance(s2, 10)
|
||||||
if !(len(s1) == 4 && len(s2) == 0 &&
|
if !(len(s1) == 4 && len(s2) == 0 &&
|
||||||
|
@ -133,7 +133,7 @@ func TestRebalanceLate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIteratorEmpty(t *testing.T) {
|
func TestIteratorEmpty(t *testing.T) {
|
||||||
s := InsertionStack{}
|
s := insertionStack{}
|
||||||
g := s.NewIterator()
|
g := s.NewIterator()
|
||||||
v := g()
|
v := g()
|
||||||
if v != nil {
|
if v != nil {
|
||||||
|
@ -142,9 +142,9 @@ func TestIteratorEmpty(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIterator(t *testing.T) {
|
func TestIterator(t *testing.T) {
|
||||||
s := InsertionStack{
|
s := insertionStack{
|
||||||
&Insertion{Offset: 7},
|
&insertion{Offset: 7},
|
||||||
&Insertion{Offset: 8},
|
&insertion{Offset: 8},
|
||||||
}
|
}
|
||||||
g := s.NewIterator()
|
g := s.NewIterator()
|
||||||
v := g()
|
v := g()
|
||||||
|
@ -208,7 +208,7 @@ func TestSortEmpty(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoFormattingXEP0393(t *testing.T) {
|
func TestNoFormattingXEP0393(t *testing.T) {
|
||||||
markup := Format("abc\ndef", []*client.TextEntity{}, EntityToXEP0393)
|
markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeXEP0393)
|
||||||
if markup != "abc\ndef" {
|
if markup != "abc\ndef" {
|
||||||
t.Errorf("No formatting expected, but: %v", markup)
|
t.Errorf("No formatting expected, but: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -221,7 +221,7 @@ func TestFormattingXEP0393Simple(t *testing.T) {
|
||||||
Length: 4,
|
Length: 4,
|
||||||
Type: &client.TextEntityTypeBold{},
|
Type: &client.TextEntityTypeBold{},
|
||||||
},
|
},
|
||||||
}, EntityToXEP0393)
|
}, MarkupModeXEP0393)
|
||||||
if markup != "👙*🐧🐖*" {
|
if markup != "👙*🐧🐖*" {
|
||||||
t.Errorf("Wrong simple formatting: %v", markup)
|
t.Errorf("Wrong simple formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -241,7 +241,7 @@ func TestFormattingXEP0393Adjacent(t *testing.T) {
|
||||||
Url: "https://narayana.im/",
|
Url: "https://narayana.im/",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, EntityToXEP0393)
|
}, MarkupModeXEP0393)
|
||||||
if markup != "a👙_🐧_🐖 <https://narayana.im/>" {
|
if markup != "a👙_🐧_🐖 <https://narayana.im/>" {
|
||||||
t.Errorf("Wrong adjacent formatting: %v", markup)
|
t.Errorf("Wrong adjacent formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -264,7 +264,7 @@ func TestFormattingXEP0393AdjacentAndNested(t *testing.T) {
|
||||||
Length: 2,
|
Length: 2,
|
||||||
Type: &client.TextEntityTypeItalic{},
|
Type: &client.TextEntityTypeItalic{},
|
||||||
},
|
},
|
||||||
}, EntityToXEP0393)
|
}, MarkupModeXEP0393)
|
||||||
if markup != "```\n*👙*🐧\n```_🐖_" {
|
if markup != "```\n*👙*🐧\n```_🐖_" {
|
||||||
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
|
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -287,7 +287,7 @@ func TestFormattingXEP0393AdjacentItalicBoldItalic(t *testing.T) {
|
||||||
Length: 69,
|
Length: 69,
|
||||||
Type: &client.TextEntityTypeItalic{},
|
Type: &client.TextEntityTypeItalic{},
|
||||||
},
|
},
|
||||||
}, EntityToXEP0393)
|
}, MarkupModeXEP0393)
|
||||||
if markup != "_раса двуногих крысолюдей, *которую так редко замечают, что многие отрицают само их существование*_" {
|
if markup != "_раса двуногих крысолюдей, *которую так редко замечают, что многие отрицают само их существование*_" {
|
||||||
t.Errorf("Wrong adjacent italic/bold-italic formatting: %v", markup)
|
t.Errorf("Wrong adjacent italic/bold-italic formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -315,7 +315,7 @@ func TestFormattingXEP0393MultipleAdjacent(t *testing.T) {
|
||||||
Length: 1,
|
Length: 1,
|
||||||
Type: &client.TextEntityTypeItalic{},
|
Type: &client.TextEntityTypeItalic{},
|
||||||
},
|
},
|
||||||
}, EntityToXEP0393)
|
}, MarkupModeXEP0393)
|
||||||
if markup != "a*bcd*_e_" {
|
if markup != "a*bcd*_e_" {
|
||||||
t.Errorf("Wrong multiple adjacent formatting: %v", markup)
|
t.Errorf("Wrong multiple adjacent formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -343,7 +343,7 @@ func TestFormattingXEP0393Intersecting(t *testing.T) {
|
||||||
Length: 1,
|
Length: 1,
|
||||||
Type: &client.TextEntityTypeBold{},
|
Type: &client.TextEntityTypeBold{},
|
||||||
},
|
},
|
||||||
}, EntityToXEP0393)
|
}, MarkupModeXEP0393)
|
||||||
if markup != "a*b*_*cd*e_" {
|
if markup != "a*b*_*cd*e_" {
|
||||||
t.Errorf("Wrong intersecting formatting: %v", markup)
|
t.Errorf("Wrong intersecting formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -361,7 +361,7 @@ func TestFormattingXEP0393InlineCode(t *testing.T) {
|
||||||
Length: 25,
|
Length: 25,
|
||||||
Type: &client.TextEntityTypePre{},
|
Type: &client.TextEntityTypePre{},
|
||||||
},
|
},
|
||||||
}, EntityToXEP0393)
|
}, MarkupModeXEP0393)
|
||||||
if markup != "Is `Gajim` a thing?\n\n```\necho 'Hello'\necho 'world'\n```\n\nhruck(" {
|
if markup != "Is `Gajim` a thing?\n\n```\necho 'Hello'\necho 'world'\n```\n\nhruck(" {
|
||||||
t.Errorf("Wrong intersecting formatting: %v", markup)
|
t.Errorf("Wrong intersecting formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -374,7 +374,7 @@ func TestFormattingMarkdownStrikethrough(t *testing.T) {
|
||||||
Length: 3,
|
Length: 3,
|
||||||
Type: &client.TextEntityTypeStrikethrough{},
|
Type: &client.TextEntityTypeStrikethrough{},
|
||||||
},
|
},
|
||||||
}, EntityToMarkdown)
|
}, MarkupModeMarkdown)
|
||||||
if markup != "Everyone ~~dis~~likes cake." {
|
if markup != "Everyone ~~dis~~likes cake." {
|
||||||
t.Errorf("Wrong strikethrough formatting: %v", markup)
|
t.Errorf("Wrong strikethrough formatting: %v", markup)
|
||||||
}
|
}
|
||||||
|
@ -387,14 +387,14 @@ func TestFormattingXEP0393Strikethrough(t *testing.T) {
|
||||||
Length: 3,
|
Length: 3,
|
||||||
Type: &client.TextEntityTypeStrikethrough{},
|
Type: &client.TextEntityTypeStrikethrough{},
|
||||||
},
|
},
|
||||||
}, EntityToXEP0393)
|
}, MarkupModeXEP0393)
|
||||||
if markup != "Everyone ~dis~likes cake." {
|
if markup != "Everyone ~dis~likes cake." {
|
||||||
t.Errorf("Wrong strikethrough formatting: %v", markup)
|
t.Errorf("Wrong strikethrough formatting: %v", markup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClaspLeft(t *testing.T) {
|
func TestClaspLeft(t *testing.T) {
|
||||||
text := "a b c"
|
text := textToDoubledRunes("a b c")
|
||||||
entities := []*client.TextEntity{
|
entities := []*client.TextEntity{
|
||||||
&client.TextEntity{
|
&client.TextEntity{
|
||||||
Offset: 1,
|
Offset: 1,
|
||||||
|
@ -409,7 +409,7 @@ func TestClaspLeft(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClaspBoth(t *testing.T) {
|
func TestClaspBoth(t *testing.T) {
|
||||||
text := "a b c"
|
text := textToDoubledRunes("a b c")
|
||||||
entities := []*client.TextEntity{
|
entities := []*client.TextEntity{
|
||||||
&client.TextEntity{
|
&client.TextEntity{
|
||||||
Offset: 1,
|
Offset: 1,
|
||||||
|
@ -424,7 +424,7 @@ func TestClaspBoth(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClaspNotNeeded(t *testing.T) {
|
func TestClaspNotNeeded(t *testing.T) {
|
||||||
text := " abc "
|
text := textToDoubledRunes(" abc ")
|
||||||
entities := []*client.TextEntity{
|
entities := []*client.TextEntity{
|
||||||
&client.TextEntity{
|
&client.TextEntity{
|
||||||
Offset: 1,
|
Offset: 1,
|
||||||
|
@ -439,7 +439,7 @@ func TestClaspNotNeeded(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClaspNested(t *testing.T) {
|
func TestClaspNested(t *testing.T) {
|
||||||
text := "a b c"
|
text := textToDoubledRunes("a b c")
|
||||||
entities := []*client.TextEntity{
|
entities := []*client.TextEntity{
|
||||||
&client.TextEntity{
|
&client.TextEntity{
|
||||||
Offset: 1,
|
Offset: 1,
|
||||||
|
@ -459,7 +459,7 @@ func TestClaspNested(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClaspEmoji(t *testing.T) {
|
func TestClaspEmoji(t *testing.T) {
|
||||||
text := "a 🐖 c"
|
text := textToDoubledRunes("a 🐖 c")
|
||||||
entities := []*client.TextEntity{
|
entities := []*client.TextEntity{
|
||||||
&client.TextEntity{
|
&client.TextEntity{
|
||||||
Offset: 1,
|
Offset: 1,
|
||||||
|
@ -472,3 +472,111 @@ func TestClaspEmoji(t *testing.T) {
|
||||||
t.Errorf("Wrong claspemoji: %#v", entities)
|
t.Errorf("Wrong claspemoji: %#v", entities)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNoNewlineBlockquoteXEP0393(t *testing.T) {
|
||||||
|
markup := Format("yes it can i think", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 4,
|
||||||
|
Length: 6,
|
||||||
|
Type: &client.TextEntityTypeBlockQuote{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "yes \n> it can\n i think" {
|
||||||
|
t.Errorf("Wrong blockquote formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNoNewlineBlockquoteMarkdown(t *testing.T) {
|
||||||
|
markup := Format("yes it can i think", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 4,
|
||||||
|
Length: 6,
|
||||||
|
Type: &client.TextEntityTypeBlockQuote{},
|
||||||
|
},
|
||||||
|
}, MarkupModeMarkdown)
|
||||||
|
if markup != "yes \n> it can\n\n i think" {
|
||||||
|
t.Errorf("Wrong blockquote formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultilineBlockquoteXEP0393(t *testing.T) {
|
||||||
|
markup := Format("hruck\npuck\n\nshuck\ntext", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 17,
|
||||||
|
Type: &client.TextEntityTypeBlockQuote{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "> hruck\n> puck\n> \n> shuck\ntext" {
|
||||||
|
t.Errorf("Wrong blockquote formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultilineBlockquoteMarkdown(t *testing.T) {
|
||||||
|
markup := Format("hruck\npuck\n\nshuck\ntext", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 17,
|
||||||
|
Type: &client.TextEntityTypeBlockQuote{},
|
||||||
|
},
|
||||||
|
}, MarkupModeMarkdown)
|
||||||
|
if markup != "> hruck\npuck\n\n> shuck\n\ntext" {
|
||||||
|
t.Errorf("Wrong blockquote formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMixedBlockquoteXEP0393(t *testing.T) {
|
||||||
|
markup := Format("hruck\npuck\nshuck\ntext", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 16,
|
||||||
|
Type: &client.TextEntityTypeBlockQuote{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 16,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 10,
|
||||||
|
Type: &client.TextEntityTypeItalic{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 7,
|
||||||
|
Length: 2,
|
||||||
|
Type: &client.TextEntityTypeStrikethrough{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "> *_hruck\n> p~uc~k_\n> shuck*\ntext" {
|
||||||
|
t.Errorf("Wrong blockquote formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMixedBlockquoteMarkdown(t *testing.T) {
|
||||||
|
markup := Format("hruck\npuck\nshuck\ntext", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 16,
|
||||||
|
Type: &client.TextEntityTypeBlockQuote{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 16,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 10,
|
||||||
|
Type: &client.TextEntityTypeItalic{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 7,
|
||||||
|
Length: 2,
|
||||||
|
Type: &client.TextEntityTypeStrikethrough{},
|
||||||
|
},
|
||||||
|
}, MarkupModeMarkdown)
|
||||||
|
if markup != "> **_hruck\np~~uc~~k_\nshuck**\n\ntext" {
|
||||||
|
t.Errorf("Wrong blockquote formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -55,6 +55,31 @@ func (c *Client) cleanTempFile(path string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) sendMarker(chatId, messageId int64, typ gateway.MarkerType) {
|
||||||
|
xmppId, err := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, chatId, messageId)
|
||||||
|
if err != nil {
|
||||||
|
xmppId = strconv.FormatInt(messageId, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
var stringType string
|
||||||
|
if typ == gateway.MarkerTypeReceived {
|
||||||
|
stringType = "received"
|
||||||
|
} else if typ == gateway.MarkerTypeDisplayed {
|
||||||
|
stringType = "displayed"
|
||||||
|
}
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"xmppId": xmppId,
|
||||||
|
}).Debugf("marker: %s", stringType)
|
||||||
|
|
||||||
|
gateway.SendMessageMarker(
|
||||||
|
c.jid,
|
||||||
|
strconv.FormatInt(chatId, 10),
|
||||||
|
c.xmpp,
|
||||||
|
typ,
|
||||||
|
xmppId,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) updateHandler() {
|
func (c *Client) updateHandler() {
|
||||||
listener := c.client.GetListener()
|
listener := c.client.GetListener()
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
|
@ -141,6 +166,12 @@ func (c *Client) updateHandler() {
|
||||||
uhOh()
|
uhOh()
|
||||||
}
|
}
|
||||||
c.updateChatTitle(typedUpdate)
|
c.updateChatTitle(typedUpdate)
|
||||||
|
case client.TypeUpdateChatReadOutbox:
|
||||||
|
typedUpdate, ok := update.(*client.UpdateChatReadOutbox)
|
||||||
|
if !ok {
|
||||||
|
uhOh()
|
||||||
|
}
|
||||||
|
c.updateChatReadOutbox(typedUpdate)
|
||||||
default:
|
default:
|
||||||
// log only handled types
|
// log only handled types
|
||||||
continue
|
continue
|
||||||
|
@ -203,14 +234,19 @@ func (c *Client) updateChatLastMessage(update *client.UpdateChatLastMessage) {
|
||||||
|
|
||||||
// message received
|
// message received
|
||||||
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
|
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
|
||||||
go func() {
|
|
||||||
chatId := update.Message.ChatId
|
chatId := update.Message.ChatId
|
||||||
|
if c.Session.IsChatIgnored(chatId) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// guarantee sequential message delivering per chat
|
// guarantee sequential message delivering per chat
|
||||||
lock := c.getChatMessageLock(chatId)
|
lock := c.getChatMessageLock(chatId)
|
||||||
|
go func() {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
c.updateLastMessageHash(update.Message.ChatId, update.Message.Id, update.Message.Content)
|
||||||
|
|
||||||
// ignore self outgoing messages
|
// ignore self outgoing messages
|
||||||
if update.Message.IsOutgoing &&
|
if update.Message.IsOutgoing &&
|
||||||
update.Message.SendingState != nil &&
|
update.Message.SendingState != nil &&
|
||||||
|
@ -228,27 +264,106 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
|
||||||
|
|
||||||
// message content updated
|
// message content updated
|
||||||
func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
|
func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
|
||||||
markupFunction := formatter.EntityToXEP0393
|
if c.Session.IsChatIgnored(update.ChatId) {
|
||||||
if update.NewContent.MessageContentType() == client.TypeMessageText {
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
markupFunction := c.getFormatter()
|
||||||
|
|
||||||
|
defer c.updateLastMessageHash(update.ChatId, update.MessageId, update.NewContent)
|
||||||
|
|
||||||
|
log.Debugf("newContent: %#v", update.NewContent)
|
||||||
|
|
||||||
|
lock := c.getChatMessageLock(update.ChatId)
|
||||||
|
lock.Lock()
|
||||||
|
lock.Unlock()
|
||||||
|
c.SendMessageLock.Lock()
|
||||||
|
c.SendMessageLock.Unlock()
|
||||||
|
|
||||||
|
xmppId, xmppIdErr := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, update.ChatId, update.MessageId)
|
||||||
|
var ignoredResource string
|
||||||
|
if xmppIdErr == nil {
|
||||||
|
ignoredResource = c.popFromEditOutbox(xmppId)
|
||||||
|
} else {
|
||||||
|
log.Infof("Couldn't retrieve XMPP message ids for %v, an echo may happen", update.MessageId)
|
||||||
|
}
|
||||||
|
log.Infof("ignoredResource: %v", ignoredResource)
|
||||||
|
|
||||||
|
jids := c.getCarbonFullJids(true, ignoredResource)
|
||||||
|
if len(jids) == 0 {
|
||||||
|
log.Info("The only resource is ignored, aborting")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if update.NewContent.MessageContentType() == client.TypeMessageText && c.hasLastMessageHashChanged(update.ChatId, update.MessageId, update.NewContent) {
|
||||||
textContent := update.NewContent.(*client.MessageText)
|
textContent := update.NewContent.(*client.MessageText)
|
||||||
|
log.Debugf("textContent: %#v", textContent.Text)
|
||||||
|
|
||||||
|
var replaceId string
|
||||||
|
sId := strconv.FormatInt(update.MessageId, 10)
|
||||||
|
var isCarbon bool
|
||||||
|
|
||||||
|
// use XEP-0308 edits only if the last message is edited for sure, fallback otherwise
|
||||||
|
if c.Session.NativeEdits {
|
||||||
|
lastXmppId, ok := c.getLastChatMessageId(update.ChatId)
|
||||||
|
if xmppIdErr != nil {
|
||||||
|
xmppId = sId
|
||||||
|
}
|
||||||
|
if ok && lastXmppId == xmppId {
|
||||||
|
replaceId = xmppId
|
||||||
|
} else {
|
||||||
|
log.Infof("Mismatching message ids: %v %v, falling back to separate edit message", lastXmppId, xmppId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
message, messageErr := c.client.GetMessage(&client.GetMessageRequest{
|
||||||
|
ChatId: update.ChatId,
|
||||||
|
MessageId: update.MessageId,
|
||||||
|
})
|
||||||
|
var prefix string
|
||||||
|
if messageErr == nil {
|
||||||
|
isCarbon = c.isCarbonsEnabled() && message.IsOutgoing
|
||||||
|
// reply correction support in clients is suboptimal yet, so cut them out for now
|
||||||
|
prefix, _ = c.messageToPrefix(message, "", "", true)
|
||||||
|
} else {
|
||||||
|
log.Errorf("No message %v/%v found, cannot reliably determine if it's a carbon", update.ChatId, update.MessageId)
|
||||||
|
}
|
||||||
|
|
||||||
|
var text strings.Builder
|
||||||
|
|
||||||
|
if replaceId == "" {
|
||||||
var editChar string
|
var editChar string
|
||||||
if c.Session.AsciiArrows {
|
if c.Session.AsciiArrows {
|
||||||
editChar = "e "
|
editChar = "e"
|
||||||
} else {
|
} else {
|
||||||
editChar = "✎ "
|
editChar = "✎"
|
||||||
}
|
}
|
||||||
text := editChar + fmt.Sprintf("%v | %s", update.MessageId, formatter.Format(
|
text.WriteString(fmt.Sprintf("%s %v | ", editChar, update.MessageId))
|
||||||
|
} else if prefix != "" {
|
||||||
|
text.WriteString(prefix)
|
||||||
|
text.WriteString(c.getPrefixSeparator(update.ChatId))
|
||||||
|
}
|
||||||
|
|
||||||
|
text.WriteString(formatter.Format(
|
||||||
textContent.Text.Text,
|
textContent.Text.Text,
|
||||||
textContent.Text.Entities,
|
textContent.Text.Entities,
|
||||||
markupFunction,
|
markupFunction,
|
||||||
))
|
))
|
||||||
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, "e"+strconv.FormatInt(update.MessageId, 10), c.xmpp, nil, false, "")
|
|
||||||
|
sChatId := strconv.FormatInt(update.ChatId, 10)
|
||||||
|
for _, jid := range jids {
|
||||||
|
gateway.SendMessage(jid, sChatId, text.String(), "e"+sId, c.xmpp, nil, replaceId, isCarbon, false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// message(s) deleted
|
// message(s) deleted
|
||||||
func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
|
func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
|
||||||
if update.IsPermanent {
|
if update.IsPermanent {
|
||||||
|
if c.Session.IsChatIgnored(update.ChatId) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var deleteChar string
|
var deleteChar string
|
||||||
if c.Session.AsciiArrows {
|
if c.Session.AsciiArrows {
|
||||||
deleteChar = "X "
|
deleteChar = "X "
|
||||||
|
@ -270,19 +385,25 @@ func (c *Client) updateAuthorizationState(update *client.UpdateAuthorizationStat
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// clean uploaded files
|
|
||||||
func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucceeded) {
|
func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucceeded) {
|
||||||
|
// replace message ID in local database
|
||||||
log.Debugf("replace message %v with %v", update.OldMessageId, update.Message.Id)
|
log.Debugf("replace message %v with %v", update.OldMessageId, update.Message.Id)
|
||||||
if err := gateway.IdsDB.ReplaceTgId(c.Session.Login, c.jid, update.Message.ChatId, update.OldMessageId, update.Message.Id); err != nil {
|
if err := gateway.IdsDB.ReplaceTgId(c.Session.Login, c.jid, update.Message.ChatId, update.OldMessageId, update.Message.Id); err != nil {
|
||||||
log.Errorf("failed to replace %v with %v: %v", update.OldMessageId, update.Message.Id, err.Error())
|
log.Errorf("failed to replace %v with %v: %v", update.OldMessageId, update.Message.Id, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.updateLastMessageHash(update.Message.ChatId, update.Message.Id, update.Message.Content)
|
||||||
|
|
||||||
|
c.sendMarker(update.Message.ChatId, update.Message.Id, gateway.MarkerTypeReceived)
|
||||||
|
|
||||||
|
// clean uploaded files
|
||||||
file, _ := c.contentToFile(update.Message.Content)
|
file, _ := c.contentToFile(update.Message.Content)
|
||||||
if file != nil && file.Local != nil {
|
if file != nil && file.Local != nil {
|
||||||
c.cleanTempFile(file.Local.Path)
|
c.cleanTempFile(file.Local.Path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed) {
|
func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed) {
|
||||||
|
// clean uploaded files
|
||||||
file, _ := c.contentToFile(update.Message.Content)
|
file, _ := c.contentToFile(update.Message.Content)
|
||||||
if file != nil && file.Local != nil {
|
if file != nil && file.Local != nil {
|
||||||
c.cleanTempFile(file.Local.Path)
|
c.cleanTempFile(file.Local.Path)
|
||||||
|
@ -294,8 +415,17 @@ func (c *Client) updateChatTitle(update *client.UpdateChatTitle) {
|
||||||
gateway.SetNickname(c.jid, strconv.FormatInt(update.ChatId, 10), update.Title, c.xmpp)
|
gateway.SetNickname(c.jid, strconv.FormatInt(update.ChatId, 10), update.Title, c.xmpp)
|
||||||
|
|
||||||
// set also the status (for group chats only)
|
// set also the status (for group chats only)
|
||||||
_, user, _ := c.GetContactByID(update.ChatId, nil)
|
chat, user, _ := c.GetContactByID(update.ChatId, nil)
|
||||||
if user == nil {
|
if user == nil {
|
||||||
c.ProcessStatusUpdate(update.ChatId, update.Title, "chat", gateway.SPImmed(true))
|
c.ProcessStatusUpdate(update.ChatId, update.Title, "chat", gateway.SPImmed(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update chat title in the cache
|
||||||
|
if chat != nil {
|
||||||
|
chat.Title = update.Title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) updateChatReadOutbox(update *client.UpdateChatReadOutbox) {
|
||||||
|
c.sendMarker(update.ChatId, update.LastReadOutboxMessageId, gateway.MarkerTypeDisplayed)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,10 @@ package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"hash/maphash"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -14,6 +16,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"dev.narayana.im/narayana/telegabber/telegram/cache"
|
"dev.narayana.im/narayana/telegabber/telegram/cache"
|
||||||
"dev.narayana.im/narayana/telegabber/telegram/formatter"
|
"dev.narayana.im/narayana/telegabber/telegram/formatter"
|
||||||
|
@ -27,20 +30,28 @@ import (
|
||||||
type VCardInfo struct {
|
type VCardInfo struct {
|
||||||
Fn string
|
Fn string
|
||||||
Photo *client.File
|
Photo *client.File
|
||||||
Nickname string
|
Nicknames []string
|
||||||
Given string
|
Given string
|
||||||
Family string
|
Family string
|
||||||
Tel string
|
Tel string
|
||||||
Info string
|
Info string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type messageStub struct {
|
||||||
|
MessageId int64
|
||||||
|
ChatId int64
|
||||||
|
Sender string
|
||||||
|
Date int32
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
var errOffline = errors.New("TDlib instance is offline")
|
var errOffline = errors.New("TDlib instance is offline")
|
||||||
|
|
||||||
var spaceRegex = regexp.MustCompile(`\s+`)
|
var spaceRegex = regexp.MustCompile(`\s+`)
|
||||||
var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
|
var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
|
||||||
|
|
||||||
const newlineChar string = "\n"
|
const newlineChar string = "\n"
|
||||||
const messageHeaderSeparator string = " | "
|
const messageHeaderSeparator string = " | " // no hrunicode allowed here yet
|
||||||
|
|
||||||
// GetContactByUsername resolves username to user id retrieves user and chat information
|
// GetContactByUsername resolves username to user id retrieves user and chat information
|
||||||
func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) {
|
func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) {
|
||||||
|
@ -241,33 +252,46 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
|
||||||
cachedStatus, ok := c.cache.GetStatus(chatID)
|
cachedStatus, ok := c.cache.GetStatus(chatID)
|
||||||
if status == "" {
|
if status == "" {
|
||||||
if ok {
|
if ok {
|
||||||
show, status = cachedStatus.XMPP, cachedStatus.Description
|
var typ string
|
||||||
|
show, status, typ = cachedStatus.Destruct()
|
||||||
|
if presenceType == "" {
|
||||||
|
presenceType = typ
|
||||||
|
}
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"show": show,
|
||||||
|
"status": status,
|
||||||
|
"presenceType": presenceType,
|
||||||
|
}).Debug("Cached status")
|
||||||
} else if user != nil && user.Status != nil {
|
} else if user != nil && user.Status != nil {
|
||||||
show, status, presenceType = c.userStatusToText(user.Status, chatID)
|
show, status, presenceType = c.userStatusToText(user.Status, chatID)
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"show": show,
|
||||||
|
"status": status,
|
||||||
|
"presenceType": presenceType,
|
||||||
|
}).Debug("Status to text")
|
||||||
} else {
|
} else {
|
||||||
show, status = "chat", chat.Title
|
show, status = "chat", chat.Title
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.cache.SetStatus(chatID, show, status)
|
cacheShow := show
|
||||||
|
if presenceType == "unavailable" {
|
||||||
|
cacheShow = presenceType
|
||||||
|
}
|
||||||
|
c.cache.SetStatus(chatID, cacheShow, status)
|
||||||
|
|
||||||
newArgs := []args.V{
|
newArgs := []args.V{
|
||||||
gateway.SPFrom(strconv.FormatInt(chatID, 10)),
|
|
||||||
gateway.SPShow(show),
|
gateway.SPShow(show),
|
||||||
gateway.SPStatus(status),
|
gateway.SPStatus(status),
|
||||||
gateway.SPPhoto(photo),
|
gateway.SPPhoto(photo),
|
||||||
gateway.SPResource(gateway.Jid.Resource),
|
|
||||||
gateway.SPImmed(gateway.SPImmed.Get(oldArgs)),
|
gateway.SPImmed(gateway.SPImmed.Get(oldArgs)),
|
||||||
}
|
}
|
||||||
|
newArgs = gateway.SPAppendFrom(newArgs, chatID)
|
||||||
if presenceType != "" {
|
if presenceType != "" {
|
||||||
newArgs = append(newArgs, gateway.SPType(presenceType))
|
newArgs = append(newArgs, gateway.SPType(presenceType))
|
||||||
}
|
}
|
||||||
|
|
||||||
return gateway.SendPresence(
|
return c.sendPresence(newArgs...)
|
||||||
c.xmpp,
|
|
||||||
c.jid,
|
|
||||||
newArgs...,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) formatContact(chatID int64) string {
|
func (c *Client) formatContact(chatID int64) string {
|
||||||
|
@ -284,12 +308,15 @@ func (c *Client) formatContact(chatID int64) string {
|
||||||
if chat != nil {
|
if chat != nil {
|
||||||
str = fmt.Sprintf("%s (%v)", chat.Title, chat.Id)
|
str = fmt.Sprintf("%s (%v)", chat.Title, chat.Id)
|
||||||
} else if user != nil {
|
} else if user != nil {
|
||||||
username := user.Username
|
var usernames string
|
||||||
if username == "" {
|
if user.Usernames != nil {
|
||||||
username = strconv.FormatInt(user.Id, 10)
|
usernames = c.usernamesToString(user.Usernames.ActiveUsernames)
|
||||||
|
}
|
||||||
|
if usernames == "" {
|
||||||
|
usernames = strconv.FormatInt(user.Id, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
str = fmt.Sprintf("%s %s (%v)", user.FirstName, user.LastName, username)
|
str = fmt.Sprintf("%s %s (%v)", user.FirstName, user.LastName, usernames)
|
||||||
} else {
|
} else {
|
||||||
str = strconv.FormatInt(chatID, 10)
|
str = strconv.FormatInt(chatID, 10)
|
||||||
}
|
}
|
||||||
|
@ -318,26 +345,75 @@ func (c *Client) formatSender(message *client.Message) string {
|
||||||
return c.formatContact(c.getSenderId(message))
|
return c.formatContact(c.getSenderId(message))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getMessageReply(message *client.Message) (reply *gateway.Reply, replyMsg *client.Message) {
|
func (c *Client) messageToStub(message *client.Message, preview bool, text string) *messageStub {
|
||||||
if message.ReplyToMessageId != 0 {
|
if text == "" {
|
||||||
var err error
|
text = c.messageContentToText(message.Content, message.ChatId, preview)
|
||||||
replyMsg, err = c.client.GetMessage(&client.GetMessageRequest{
|
}
|
||||||
|
return &messageStub{
|
||||||
|
MessageId: message.Id,
|
||||||
ChatId: message.ChatId,
|
ChatId: message.ChatId,
|
||||||
MessageId: message.ReplyToMessageId,
|
Sender: c.formatSender(message),
|
||||||
|
Date: message.Date,
|
||||||
|
Text: text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getMessageReply(message *client.Message, preview bool, noContent bool) (gatewayReply *gateway.Reply, tgReply *messageStub) {
|
||||||
|
if message.ReplyTo != nil && message.ReplyTo.MessageReplyToType() == client.TypeMessageReplyToMessage {
|
||||||
|
replyTo, _ := message.ReplyTo.(*client.MessageReplyToMessage)
|
||||||
|
var text string
|
||||||
|
if replyTo.Quote != nil && replyTo.Quote.Text != nil && !noContent {
|
||||||
|
text = formatter.Format(
|
||||||
|
replyTo.Quote.Text.Text,
|
||||||
|
replyTo.Quote.Text.Entities,
|
||||||
|
c.getFormatter(),
|
||||||
|
)
|
||||||
|
// make the whole quote fit one line
|
||||||
|
text = strings.ReplaceAll(text, "\n", " ")
|
||||||
|
}
|
||||||
|
if message.ChatId == replyTo.ChatId {
|
||||||
|
// obtain message from this chat
|
||||||
|
replyMsg, err := c.client.GetMessage(&client.GetMessageRequest{
|
||||||
|
ChatId: message.ChatId,
|
||||||
|
MessageId: replyTo.MessageId,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("<error fetching message: %s>", err.Error())
|
log.Errorf("<error fetching message: %s>", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
replyId, err := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, message.ChatId, message.ReplyToMessageId)
|
if !noContent {
|
||||||
if err != nil {
|
tgReply = c.messageToStub(replyMsg, preview, text)
|
||||||
replyId = strconv.FormatInt(message.ReplyToMessageId, 10)
|
|
||||||
}
|
}
|
||||||
reply = &gateway.Reply{
|
|
||||||
|
replyId, err := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, message.ChatId, replyTo.MessageId)
|
||||||
|
if err != nil {
|
||||||
|
replyId = strconv.FormatInt(replyTo.MessageId, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
gatewayReply = &gateway.Reply{
|
||||||
Author: fmt.Sprintf("%v@%s", c.getSenderId(replyMsg), gateway.Jid.Full()),
|
Author: fmt.Sprintf("%v@%s", c.getSenderId(replyMsg), gateway.Jid.Full()),
|
||||||
Id: replyId,
|
Id: replyId,
|
||||||
}
|
}
|
||||||
|
} else if !noContent {
|
||||||
|
// it's safe to assume there's no need to pass ChatId here
|
||||||
|
// as it's needed only for pin messages which are not allowed in replies
|
||||||
|
if text == "" && replyTo.Content != nil {
|
||||||
|
text = c.messageContentToText(replyTo.Content, 0, preview)
|
||||||
|
}
|
||||||
|
|
||||||
|
if text == "" {
|
||||||
|
log.Error("Empty reply from other/unknown chat")
|
||||||
|
log.Debugf("replyTo: %#v", replyTo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tgReply = &messageStub{
|
||||||
|
Sender: c.formatOrigin(replyTo.Origin) + " @ " + c.formatContact(replyTo.ChatId),
|
||||||
|
Date: replyTo.OriginSendDate,
|
||||||
|
Text: text,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -359,9 +435,16 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return c.formatMessageContent(preview, c.messageToStub(message, preview, ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) formatMessageContent(preview bool, message *messageStub) string {
|
||||||
var str strings.Builder
|
var str strings.Builder
|
||||||
// add messageid and sender
|
// add messageid and sender
|
||||||
str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatSender(message)))
|
if message.MessageId != 0 {
|
||||||
|
str.WriteString(fmt.Sprintf("%v | ", message.MessageId))
|
||||||
|
}
|
||||||
|
str.WriteString(fmt.Sprintf("%s | ", message.Sender))
|
||||||
// add date
|
// add date
|
||||||
if !preview {
|
if !preview {
|
||||||
str.WriteString(
|
str.WriteString(
|
||||||
|
@ -372,10 +455,7 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
|
||||||
}
|
}
|
||||||
|
|
||||||
// text message
|
// text message
|
||||||
var text string
|
text := message.Text
|
||||||
if message.Content != nil {
|
|
||||||
text = c.messageToText(message, preview)
|
|
||||||
}
|
|
||||||
if text != "" {
|
if text != "" {
|
||||||
if !preview {
|
if !preview {
|
||||||
str.WriteString(text)
|
str.WriteString(text)
|
||||||
|
@ -392,33 +472,33 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
|
||||||
return str.String()
|
return str.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) formatForward(fwd *client.MessageForwardInfo) string {
|
func (c *Client) formatOrigin(origin client.MessageOrigin) string {
|
||||||
switch fwd.Origin.MessageForwardOriginType() {
|
if origin == nil {
|
||||||
case client.TypeMessageForwardOriginUser:
|
return ""
|
||||||
originUser := fwd.Origin.(*client.MessageForwardOriginUser)
|
}
|
||||||
|
switch origin.MessageOriginType() {
|
||||||
|
case client.TypeMessageOriginUser:
|
||||||
|
originUser := origin.(*client.MessageOriginUser)
|
||||||
return c.formatContact(originUser.SenderUserId)
|
return c.formatContact(originUser.SenderUserId)
|
||||||
case client.TypeMessageForwardOriginChat:
|
case client.TypeMessageOriginChat:
|
||||||
originChat := fwd.Origin.(*client.MessageForwardOriginChat)
|
originChat := origin.(*client.MessageOriginChat)
|
||||||
var signature string
|
var signature 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.TypeMessageOriginHiddenUser:
|
||||||
originUser := fwd.Origin.(*client.MessageForwardOriginHiddenUser)
|
originUser := origin.(*client.MessageOriginHiddenUser)
|
||||||
return originUser.SenderName
|
return originUser.SenderName
|
||||||
case client.TypeMessageForwardOriginChannel:
|
case client.TypeMessageOriginChannel:
|
||||||
channel := fwd.Origin.(*client.MessageForwardOriginChannel)
|
channel := origin.(*client.MessageOriginChannel)
|
||||||
var signature string
|
var signature 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:
|
|
||||||
originImport := fwd.Origin.(*client.MessageForwardOriginMessageImport)
|
|
||||||
return originImport.SenderName
|
|
||||||
}
|
}
|
||||||
return "Unknown forward type"
|
return "Unknown origin type"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) formatFile(file *client.File, compact bool) (string, string) {
|
func (c *Client) formatFile(file *client.File, compact bool) (string, string) {
|
||||||
|
@ -564,20 +644,24 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
|
||||||
return "<empty message>"
|
return "<empty message>"
|
||||||
}
|
}
|
||||||
|
|
||||||
markupFunction := formatter.EntityToXEP0393
|
return c.messageContentToText(message.Content, message.ChatId, preview)
|
||||||
switch message.Content.MessageContentType() {
|
}
|
||||||
|
|
||||||
|
func (c *Client) messageContentToText(content client.MessageContent, chatId int64, preview bool) string {
|
||||||
|
markupMode := c.getFormatter()
|
||||||
|
switch content.MessageContentType() {
|
||||||
case client.TypeMessageSticker:
|
case client.TypeMessageSticker:
|
||||||
sticker, _ := message.Content.(*client.MessageSticker)
|
sticker, _ := content.(*client.MessageSticker)
|
||||||
return sticker.Sticker.Emoji
|
return sticker.Sticker.Emoji
|
||||||
case client.TypeMessageAnimatedEmoji:
|
case client.TypeMessageAnimatedEmoji:
|
||||||
animatedEmoji, _ := message.Content.(*client.MessageAnimatedEmoji)
|
animatedEmoji, _ := content.(*client.MessageAnimatedEmoji)
|
||||||
return animatedEmoji.Emoji
|
return animatedEmoji.Emoji
|
||||||
case client.TypeMessageBasicGroupChatCreate, client.TypeMessageSupergroupChatCreate:
|
case client.TypeMessageBasicGroupChatCreate, client.TypeMessageSupergroupChatCreate:
|
||||||
return "has created chat"
|
return "has created chat"
|
||||||
case client.TypeMessageChatJoinByLink:
|
case client.TypeMessageChatJoinByLink:
|
||||||
return "joined chat via invite link"
|
return "joined chat via invite link"
|
||||||
case client.TypeMessageChatAddMembers:
|
case client.TypeMessageChatAddMembers:
|
||||||
addMembers, _ := message.Content.(*client.MessageChatAddMembers)
|
addMembers, _ := content.(*client.MessageChatAddMembers)
|
||||||
|
|
||||||
text := "invited "
|
text := "invited "
|
||||||
if len(addMembers.MemberUserIds) > 0 {
|
if len(addMembers.MemberUserIds) > 0 {
|
||||||
|
@ -586,19 +670,19 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
|
||||||
|
|
||||||
return text
|
return text
|
||||||
case client.TypeMessageChatDeleteMember:
|
case client.TypeMessageChatDeleteMember:
|
||||||
deleteMember, _ := message.Content.(*client.MessageChatDeleteMember)
|
deleteMember, _ := content.(*client.MessageChatDeleteMember)
|
||||||
return "kicked " + c.formatContact(deleteMember.UserId)
|
return "kicked " + c.formatContact(deleteMember.UserId)
|
||||||
case client.TypeMessagePinMessage:
|
case client.TypeMessagePinMessage:
|
||||||
pinMessage, _ := message.Content.(*client.MessagePinMessage)
|
pinMessage, _ := content.(*client.MessagePinMessage)
|
||||||
return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, preview, nil)
|
return "pinned message: " + c.formatMessage(chatId, pinMessage.MessageId, preview, nil)
|
||||||
case client.TypeMessageChatChangeTitle:
|
case client.TypeMessageChatChangeTitle:
|
||||||
changeTitle, _ := message.Content.(*client.MessageChatChangeTitle)
|
changeTitle, _ := content.(*client.MessageChatChangeTitle)
|
||||||
return "chat title set to: " + changeTitle.Title
|
return "chat title set to: " + changeTitle.Title
|
||||||
case client.TypeMessageLocation:
|
case client.TypeMessageLocation:
|
||||||
location, _ := message.Content.(*client.MessageLocation)
|
location, _ := content.(*client.MessageLocation)
|
||||||
return c.formatLocation(location.Location)
|
return c.formatLocation(location.Location)
|
||||||
case client.TypeMessageVenue:
|
case client.TypeMessageVenue:
|
||||||
venue, _ := message.Content.(*client.MessageVenue)
|
venue, _ := content.(*client.MessageVenue)
|
||||||
if preview {
|
if preview {
|
||||||
return venue.Venue.Title
|
return venue.Venue.Title
|
||||||
} else {
|
} else {
|
||||||
|
@ -610,86 +694,86 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessagePhoto:
|
case client.TypeMessagePhoto:
|
||||||
photo, _ := message.Content.(*client.MessagePhoto)
|
photo, _ := content.(*client.MessagePhoto)
|
||||||
if preview {
|
if preview {
|
||||||
return photo.Caption.Text
|
return photo.Caption.Text
|
||||||
} else {
|
} else {
|
||||||
return formatter.Format(
|
return formatter.Format(
|
||||||
photo.Caption.Text,
|
photo.Caption.Text,
|
||||||
photo.Caption.Entities,
|
photo.Caption.Entities,
|
||||||
markupFunction,
|
markupMode,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessageAudio:
|
case client.TypeMessageAudio:
|
||||||
audio, _ := message.Content.(*client.MessageAudio)
|
audio, _ := content.(*client.MessageAudio)
|
||||||
if preview {
|
if preview {
|
||||||
return audio.Caption.Text
|
return audio.Caption.Text
|
||||||
} else {
|
} else {
|
||||||
return formatter.Format(
|
return formatter.Format(
|
||||||
audio.Caption.Text,
|
audio.Caption.Text,
|
||||||
audio.Caption.Entities,
|
audio.Caption.Entities,
|
||||||
markupFunction,
|
markupMode,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessageVideo:
|
case client.TypeMessageVideo:
|
||||||
video, _ := message.Content.(*client.MessageVideo)
|
video, _ := content.(*client.MessageVideo)
|
||||||
if preview {
|
if preview {
|
||||||
return video.Caption.Text
|
return video.Caption.Text
|
||||||
} else {
|
} else {
|
||||||
return formatter.Format(
|
return formatter.Format(
|
||||||
video.Caption.Text,
|
video.Caption.Text,
|
||||||
video.Caption.Entities,
|
video.Caption.Entities,
|
||||||
markupFunction,
|
markupMode,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessageDocument:
|
case client.TypeMessageDocument:
|
||||||
document, _ := message.Content.(*client.MessageDocument)
|
document, _ := content.(*client.MessageDocument)
|
||||||
if preview {
|
if preview {
|
||||||
return document.Caption.Text
|
return document.Caption.Text
|
||||||
} else {
|
} else {
|
||||||
return formatter.Format(
|
return formatter.Format(
|
||||||
document.Caption.Text,
|
document.Caption.Text,
|
||||||
document.Caption.Entities,
|
document.Caption.Entities,
|
||||||
markupFunction,
|
markupMode,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessageText:
|
case client.TypeMessageText:
|
||||||
text, _ := message.Content.(*client.MessageText)
|
text, _ := content.(*client.MessageText)
|
||||||
if preview {
|
if preview {
|
||||||
return text.Text.Text
|
return text.Text.Text
|
||||||
} else {
|
} else {
|
||||||
return formatter.Format(
|
return formatter.Format(
|
||||||
text.Text.Text,
|
text.Text.Text,
|
||||||
text.Text.Entities,
|
text.Text.Entities,
|
||||||
markupFunction,
|
markupMode,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessageVoiceNote:
|
case client.TypeMessageVoiceNote:
|
||||||
voice, _ := message.Content.(*client.MessageVoiceNote)
|
voice, _ := content.(*client.MessageVoiceNote)
|
||||||
if preview {
|
if preview {
|
||||||
return voice.Caption.Text
|
return voice.Caption.Text
|
||||||
} else {
|
} else {
|
||||||
return formatter.Format(
|
return formatter.Format(
|
||||||
voice.Caption.Text,
|
voice.Caption.Text,
|
||||||
voice.Caption.Entities,
|
voice.Caption.Entities,
|
||||||
markupFunction,
|
markupMode,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessageVideoNote:
|
case client.TypeMessageVideoNote:
|
||||||
return ""
|
return ""
|
||||||
case client.TypeMessageAnimation:
|
case client.TypeMessageAnimation:
|
||||||
animation, _ := message.Content.(*client.MessageAnimation)
|
animation, _ := content.(*client.MessageAnimation)
|
||||||
if preview {
|
if preview {
|
||||||
return animation.Caption.Text
|
return animation.Caption.Text
|
||||||
} else {
|
} else {
|
||||||
return formatter.Format(
|
return formatter.Format(
|
||||||
animation.Caption.Text,
|
animation.Caption.Text,
|
||||||
animation.Caption.Entities,
|
animation.Caption.Entities,
|
||||||
markupFunction,
|
markupMode,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessageContact:
|
case client.TypeMessageContact:
|
||||||
contact, _ := message.Content.(*client.MessageContact)
|
contact, _ := content.(*client.MessageContact)
|
||||||
if preview {
|
if preview {
|
||||||
return contact.Contact.FirstName + " " + contact.Contact.LastName
|
return contact.Contact.FirstName + " " + contact.Contact.LastName
|
||||||
} else {
|
} else {
|
||||||
|
@ -707,10 +791,10 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case client.TypeMessageDice:
|
case client.TypeMessageDice:
|
||||||
dice, _ := message.Content.(*client.MessageDice)
|
dice, _ := content.(*client.MessageDice)
|
||||||
return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value)
|
return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value)
|
||||||
case client.TypeMessagePoll:
|
case client.TypeMessagePoll:
|
||||||
poll, _ := message.Content.(*client.MessagePoll)
|
poll, _ := content.(*client.MessagePoll)
|
||||||
|
|
||||||
if preview {
|
if preview {
|
||||||
return poll.Poll.Question
|
return poll.Poll.Question
|
||||||
|
@ -735,9 +819,25 @@ func (c *Client) messageToText(message *client.Message, preview bool) string {
|
||||||
|
|
||||||
return strings.Join(rows, "\n")
|
return strings.Join(rows, "\n")
|
||||||
}
|
}
|
||||||
|
case client.TypeMessageChatSetMessageAutoDeleteTime:
|
||||||
|
ttl, _ := content.(*client.MessageChatSetMessageAutoDeleteTime)
|
||||||
|
name := c.formatContact(ttl.FromUserId)
|
||||||
|
if name == "" {
|
||||||
|
if ttl.MessageAutoDeleteTime == 0 {
|
||||||
|
return "The self-destruct timer was disabled"
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("The self-destruct timer was set to %v seconds", ttl.MessageAutoDeleteTime)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ttl.MessageAutoDeleteTime == 0 {
|
||||||
|
return fmt.Sprintf("%s disabled the self-destruct timer", name)
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("%s set the self-destruct timer to %v seconds", name, ttl.MessageAutoDeleteTime)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType())
|
return fmt.Sprintf("unknown message (%s)", content.MessageContentType())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) contentToFile(content client.MessageContent) (*client.File, *client.File) {
|
func (c *Client) contentToFile(content client.MessageContent) (*client.File, *client.File) {
|
||||||
|
@ -749,7 +849,7 @@ func (c *Client) contentToFile(content client.MessageContent) (*client.File, *cl
|
||||||
case client.TypeMessageSticker:
|
case client.TypeMessageSticker:
|
||||||
sticker, _ := content.(*client.MessageSticker)
|
sticker, _ := content.(*client.MessageSticker)
|
||||||
file := sticker.Sticker.Sticker
|
file := sticker.Sticker.Sticker
|
||||||
if sticker.Sticker.IsAnimated && sticker.Sticker.Thumbnail != nil && sticker.Sticker.Thumbnail.File != nil {
|
if sticker.Sticker.Format.StickerFormatType() == client.TypeStickerFormatTgs && sticker.Sticker.Thumbnail != nil && sticker.Sticker.Thumbnail.File != nil {
|
||||||
file = sticker.Sticker.Thumbnail.File
|
file = sticker.Sticker.Thumbnail.File
|
||||||
}
|
}
|
||||||
return file, nil
|
return file, nil
|
||||||
|
@ -806,22 +906,27 @@ func (c *Client) contentToFile(content client.MessageContent) (*client.File, *cl
|
||||||
|
|
||||||
func (c *Client) countCharsInLines(lines *[]string) (count int) {
|
func (c *Client) countCharsInLines(lines *[]string) (count int) {
|
||||||
for _, line := range *lines {
|
for _, line := range *lines {
|
||||||
count += len(line)
|
count += utf8.RuneCountInString(line)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, replyMsg *client.Message) (string, int, int) {
|
func (c *Client) isCarbonsEnabled() bool {
|
||||||
|
return gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, suppressReply bool) (string, *gateway.Reply) {
|
||||||
isPM, err := c.IsPM(message.ChatId)
|
isPM, err := c.IsPM(message.ChatId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Could not determine if chat is PM: %v", err)
|
log.Errorf("Could not determine if chat is PM: %v", err)
|
||||||
}
|
}
|
||||||
|
// with carbons, hide for all messages in PM and only for outgoing in group chats
|
||||||
|
hideSender := c.isCarbonsEnabled() && (message.IsOutgoing || isPM)
|
||||||
|
|
||||||
var replyStart, replyEnd int
|
|
||||||
prefix := []string{}
|
prefix := []string{}
|
||||||
// message direction
|
// message direction
|
||||||
var directionChar string
|
var directionChar string
|
||||||
if !isPM || !gateway.MessageOutgoingPermission || !c.Session.Carbons {
|
if !hideSender {
|
||||||
if c.Session.AsciiArrows {
|
if c.Session.AsciiArrows {
|
||||||
if message.IsOutgoing {
|
if message.IsOutgoing {
|
||||||
directionChar = "> "
|
directionChar = "> "
|
||||||
|
@ -840,26 +945,45 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
|
||||||
prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10))
|
prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10))
|
||||||
}
|
}
|
||||||
// show sender in group chats
|
// show sender in group chats
|
||||||
if !isPM {
|
if !hideSender {
|
||||||
sender := c.formatSender(message)
|
sender := c.formatSender(message)
|
||||||
if sender != "" {
|
if sender != "" {
|
||||||
prefix = append(prefix, sender)
|
prefix = append(prefix, sender)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// reply to
|
// reply to
|
||||||
if message.ReplyToMessageId != 0 {
|
var reply *gateway.Reply
|
||||||
|
if !suppressReply {
|
||||||
|
preview := true
|
||||||
|
gwReply, tgReply := c.getMessageReply(message, preview, false)
|
||||||
|
|
||||||
|
if tgReply != nil {
|
||||||
|
reply = gwReply
|
||||||
|
|
||||||
|
var replyStart, replyEnd int
|
||||||
|
|
||||||
if len(prefix) > 0 {
|
if len(prefix) > 0 {
|
||||||
replyStart = c.countCharsInLines(&prefix) + (len(prefix)-1)*len(messageHeaderSeparator)
|
replyStart = c.countCharsInLines(&prefix) + (len(prefix)-1)*len(messageHeaderSeparator)
|
||||||
}
|
}
|
||||||
replyLine := "reply: " + c.formatMessage(message.ChatId, message.ReplyToMessageId, true, replyMsg)
|
|
||||||
|
replyLine := "reply: " + c.formatMessageContent(preview, tgReply)
|
||||||
prefix = append(prefix, replyLine)
|
prefix = append(prefix, replyLine)
|
||||||
replyEnd = replyStart + len(replyLine)
|
|
||||||
|
replyEnd = replyStart + utf8.RuneCountInString(replyLine)
|
||||||
if len(prefix) > 0 {
|
if len(prefix) > 0 {
|
||||||
replyEnd += len(messageHeaderSeparator)
|
replyEnd += len(messageHeaderSeparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if reply != nil {
|
||||||
|
reply.Start = uint64(replyStart)
|
||||||
|
reply.End = uint64(replyEnd)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if message.ForwardInfo != nil {
|
if message.ForwardInfo != nil {
|
||||||
prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo))
|
prefix = append(prefix, "fwd: "+c.formatOrigin(message.ForwardInfo.Origin))
|
||||||
}
|
}
|
||||||
// preview
|
// preview
|
||||||
if previewString != "" {
|
if previewString != "" {
|
||||||
|
@ -870,7 +994,7 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
|
||||||
prefix = append(prefix, "file: "+fileString)
|
prefix = append(prefix, "file: "+fileString)
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(prefix, messageHeaderSeparator), replyStart, replyEnd
|
return strings.Join(prefix, messageHeaderSeparator), reply
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ensureDownloadFile(file *client.File) *client.File {
|
func (c *Client) ensureDownloadFile(file *client.File) *client.File {
|
||||||
|
@ -889,24 +1013,28 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File {
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// \n if it is groupchat and message is not empty
|
||||||
|
func (c *Client) getPrefixSeparator(chatId int64) string {
|
||||||
|
var separator string
|
||||||
|
if chatId < 0 {
|
||||||
|
separator = "\n"
|
||||||
|
} else if chatId > 0 {
|
||||||
|
separator = " | "
|
||||||
|
}
|
||||||
|
return separator
|
||||||
|
}
|
||||||
|
|
||||||
// ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side
|
// ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side
|
||||||
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
||||||
var text, oob, auxText, nick, contentType string
|
isCarbon := c.isCarbonsEnabled() && message.IsOutgoing
|
||||||
var err error
|
jids := c.getCarbonFullJids(isCarbon, "")
|
||||||
|
|
||||||
reply, replyMsg := c.getMessageReply(message)
|
var text, oob, auxText string
|
||||||
|
var reply *gateway.Reply
|
||||||
|
var replyObtained bool
|
||||||
|
|
||||||
content := message.Content
|
content := message.Content
|
||||||
if content != nil {
|
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
|
||||||
contentType = content.MessageContentType()
|
|
||||||
}
|
|
||||||
|
|
||||||
if contentType == client.TypeMessageChatChangeTitle {
|
|
||||||
changeTitle, _ := content.(*client.MessageChatChangeTitle)
|
|
||||||
nick = changeTitle.Title
|
|
||||||
}
|
|
||||||
|
|
||||||
if contentType == client.TypeMessageChatChangePhoto {
|
|
||||||
chat, err := c.client.GetChat(&client.GetChatRequest{
|
chat, err := c.client.GetChat(&client.GetChatRequest{
|
||||||
ChatId: chatId,
|
ChatId: chatId,
|
||||||
})
|
})
|
||||||
|
@ -930,73 +1058,72 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
||||||
fileName, link := c.formatFile(file, false)
|
fileName, link := c.formatFile(file, false)
|
||||||
|
|
||||||
oob = link
|
oob = link
|
||||||
if c.Session.OOBMode && oob != "" {
|
oobSwap := c.Session.OOBMode && oob != ""
|
||||||
typ := message.Content.MessageContentType()
|
|
||||||
if typ != client.TypeMessageSticker {
|
var ignorePrefix bool
|
||||||
auxText = text
|
if oobSwap {
|
||||||
|
if text == "" || message.Content.MessageContentType() == client.TypeMessageSticker {
|
||||||
|
isPM, err := c.IsPM(chatId)
|
||||||
|
if err == nil {
|
||||||
|
ignorePrefix = isPM && c.isCarbonsEnabled()
|
||||||
}
|
}
|
||||||
text = oob
|
}
|
||||||
} else if !c.Session.RawMessages {
|
}
|
||||||
|
|
||||||
|
if !c.Session.RawMessages && !ignorePrefix {
|
||||||
var newText strings.Builder
|
var newText strings.Builder
|
||||||
|
|
||||||
prefix, replyStart, replyEnd := c.messageToPrefix(message, previewName, fileName, replyMsg)
|
prefix, prefixReply := c.messageToPrefix(message, previewName, fileName, false)
|
||||||
|
reply = prefixReply
|
||||||
|
replyObtained = true
|
||||||
newText.WriteString(prefix)
|
newText.WriteString(prefix)
|
||||||
if reply != nil {
|
|
||||||
reply.Start = uint64(replyStart)
|
|
||||||
reply.End = uint64(replyEnd)
|
|
||||||
}
|
|
||||||
|
|
||||||
if text != "" {
|
if text != "" {
|
||||||
// \n if it is groupchat and message is not empty
|
|
||||||
if prefix != "" {
|
if prefix != "" {
|
||||||
if chatId < 0 {
|
newText.WriteString(c.getPrefixSeparator(chatId))
|
||||||
newText.WriteString("\n")
|
|
||||||
} else if chatId > 0 {
|
|
||||||
newText.WriteString(" | ")
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
newText.WriteString(text)
|
newText.WriteString(text)
|
||||||
}
|
}
|
||||||
text = newText.String()
|
text = newText.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if oobSwap {
|
||||||
|
if !ignorePrefix {
|
||||||
|
auxText = text
|
||||||
}
|
}
|
||||||
|
text = oob
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !replyObtained {
|
||||||
|
reply, _ = c.getMessageReply(message, false, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// mark message as read
|
// mark message as read
|
||||||
c.client.ViewMessages(&client.ViewMessagesRequest{
|
if !c.Session.Receipts {
|
||||||
ChatId: chatId,
|
c.MarkAsRead(chatId, message.Id)
|
||||||
MessageIds: []int64{message.Id},
|
}
|
||||||
ForceRead: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
// forward message to XMPP
|
// forward message to XMPP
|
||||||
sId := strconv.FormatInt(message.Id, 10)
|
sId := strconv.FormatInt(message.Id, 10)
|
||||||
sChatId := strconv.FormatInt(chatId, 10)
|
sChatId := strconv.FormatInt(chatId, 10)
|
||||||
|
|
||||||
var jids []string
|
|
||||||
var isPM bool
|
|
||||||
if gateway.MessageOutgoingPermission && c.Session.Carbons {
|
|
||||||
isPM, err = c.IsPM(chatId)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Could not determine if chat is PM: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
isOutgoing := isPM && message.IsOutgoing
|
|
||||||
if isOutgoing {
|
|
||||||
for resource := range c.resourcesRange() {
|
|
||||||
jids = append(jids, c.jid+"/"+resource)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
jids = []string{c.jid}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, jid := range jids {
|
for _, jid := range jids {
|
||||||
gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, isOutgoing, nick)
|
gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, "", isCarbon, c.Session.Receipts)
|
||||||
if auxText != "" {
|
if auxText != "" {
|
||||||
gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, isOutgoing, nick)
|
gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, "", isCarbon, c.Session.Receipts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.UpdateLastChatMessageId(chatId, sId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkAsRead marks a message as read
|
||||||
|
func (c *Client) MarkAsRead(chatId, messageId int64) {
|
||||||
|
c.client.ViewMessages(&client.ViewMessagesRequest{
|
||||||
|
ChatId: chatId,
|
||||||
|
MessageIds: []int64{messageId},
|
||||||
|
ForceRead: true,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrepareMessageContent creates a simple text message
|
// PrepareMessageContent creates a simple text message
|
||||||
|
@ -1097,7 +1224,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
||||||
|
|
||||||
tgMessage, err := c.client.SendMessage(&client.SendMessageRequest{
|
tgMessage, err := c.client.SendMessage(&client.SendMessageRequest{
|
||||||
ChatId: chatID,
|
ChatId: chatID,
|
||||||
ReplyToMessageId: reply,
|
ReplyTo: &client.InputMessageReplyToMessage{MessageId: reply},
|
||||||
InputMessageContent: content,
|
InputMessageContent: content,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1181,9 +1308,12 @@ func (c *Client) resourcesRange() chan string {
|
||||||
|
|
||||||
// resend statuses to (to another resource, for example)
|
// resend statuses to (to another resource, for example)
|
||||||
func (c *Client) roster(resource string) {
|
func (c *Client) roster(resource string) {
|
||||||
|
c.locks.resourcesLock.Lock()
|
||||||
if _, ok := c.resources[resource]; ok {
|
if _, ok := c.resources[resource]; ok {
|
||||||
|
c.locks.resourcesLock.Unlock()
|
||||||
return // we know it
|
return // we know it
|
||||||
}
|
}
|
||||||
|
c.locks.resourcesLock.Unlock()
|
||||||
|
|
||||||
log.Warnf("Sending roster for %v", resource)
|
log.Warnf("Sending roster for %v", resource)
|
||||||
|
|
||||||
|
@ -1191,13 +1321,13 @@ func (c *Client) roster(resource string) {
|
||||||
c.ProcessStatusUpdate(chat, "", "")
|
c.ProcessStatusUpdate(chat, "", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login))
|
c.sendPresence(gateway.SPStatus("Logged in as: " + c.Session.Login))
|
||||||
|
|
||||||
c.addResource(resource)
|
c.addResource(resource)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get last messages from specified chat
|
// get last messages from specified chat
|
||||||
func (c *Client) getLastMessages(id int64, query string, from int64, count int32) (*client.Messages, error) {
|
func (c *Client) getLastMessages(id int64, query string, from int64, count int32) (*client.FoundChatMessages, error) {
|
||||||
return c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
|
return c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
|
||||||
ChatId: id,
|
ChatId: id,
|
||||||
Query: query,
|
Query: query,
|
||||||
|
@ -1250,10 +1380,18 @@ func (c *Client) GetChatDescription(chat *client.Chat) string {
|
||||||
UserId: privateType.UserId,
|
UserId: privateType.UserId,
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if fullInfo.Bio != "" {
|
if fullInfo.Bio != nil && fullInfo.Bio.Text != "" {
|
||||||
return fullInfo.Bio
|
return formatter.Format(
|
||||||
} else if fullInfo.Description != "" {
|
fullInfo.Bio.Text,
|
||||||
return fullInfo.Description
|
fullInfo.Bio.Entities,
|
||||||
|
c.getFormatter(),
|
||||||
|
)
|
||||||
|
} else if fullInfo.BotInfo != nil {
|
||||||
|
if fullInfo.BotInfo.ShortDescription != "" {
|
||||||
|
return fullInfo.BotInfo.ShortDescription
|
||||||
|
} else {
|
||||||
|
return fullInfo.BotInfo.Description
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Warnf("Coudln't retrieve private chat info: %v", err.Error())
|
log.Warnf("Coudln't retrieve private chat info: %v", err.Error())
|
||||||
|
@ -1284,9 +1422,7 @@ func (c *Client) GetChatDescription(chat *client.Chat) string {
|
||||||
|
|
||||||
// subscribe to a Telegram ID
|
// subscribe to a Telegram ID
|
||||||
func (c *Client) subscribeToID(id int64, chat *client.Chat) {
|
func (c *Client) subscribeToID(id int64, chat *client.Chat) {
|
||||||
var args []args.V
|
args := gateway.SimplePresence(id, "subscribe")
|
||||||
args = append(args, gateway.SPFrom(strconv.FormatInt(id, 10)))
|
|
||||||
args = append(args, gateway.SPType("subscribe"))
|
|
||||||
|
|
||||||
if chat == nil {
|
if chat == nil {
|
||||||
chat, _, _ = c.GetContactByID(id, nil)
|
chat, _, _ = c.GetContactByID(id, nil)
|
||||||
|
@ -1297,11 +1433,11 @@ func (c *Client) subscribeToID(id int64, chat *client.Chat) {
|
||||||
gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp)
|
gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp)
|
||||||
}
|
}
|
||||||
|
|
||||||
gateway.SendPresence(
|
c.sendPresence(args...)
|
||||||
c.xmpp,
|
}
|
||||||
c.jid,
|
|
||||||
args...,
|
func (c *Client) sendPresence(args ...args.V) error {
|
||||||
)
|
return gateway.SendPresence(c.xmpp, c.jid, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) prepareDiskSpace(size uint64) {
|
func (c *Client) prepareDiskSpace(size uint64) {
|
||||||
|
@ -1333,7 +1469,10 @@ func (c *Client) GetVcardInfo(toID int64) (VCardInfo, error) {
|
||||||
info.Info = c.GetChatDescription(chat)
|
info.Info = c.GetChatDescription(chat)
|
||||||
}
|
}
|
||||||
if user != nil {
|
if user != nil {
|
||||||
info.Nickname = user.Username
|
if user.Usernames != nil {
|
||||||
|
info.Nicknames = make([]string, len(user.Usernames.ActiveUsernames))
|
||||||
|
copy(info.Nicknames, user.Usernames.ActiveUsernames)
|
||||||
|
}
|
||||||
info.Given = user.FirstName
|
info.Given = user.FirstName
|
||||||
info.Family = user.LastName
|
info.Family = user.LastName
|
||||||
info.Tel = user.PhoneNumber
|
info.Tel = user.PhoneNumber
|
||||||
|
@ -1341,3 +1480,164 @@ func (c *Client) GetVcardInfo(toID int64) (VCardInfo, error) {
|
||||||
|
|
||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) UpdateChatNicknames() {
|
||||||
|
for _, id := range c.cache.ChatsKeys() {
|
||||||
|
chat, ok := c.cache.GetChat(id)
|
||||||
|
if ok {
|
||||||
|
newArgs := []args.V{
|
||||||
|
gateway.SPNickname(chat.Title),
|
||||||
|
}
|
||||||
|
newArgs = gateway.SPAppendFrom(newArgs, id)
|
||||||
|
|
||||||
|
cachedStatus, ok := c.cache.GetStatus(id)
|
||||||
|
if ok {
|
||||||
|
show, status, typ := cachedStatus.Destruct()
|
||||||
|
newArgs = append(newArgs, gateway.SPShow(show), gateway.SPStatus(status))
|
||||||
|
if typ != "" {
|
||||||
|
newArgs = append(newArgs, gateway.SPType(typ))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.sendPresence(newArgs...)
|
||||||
|
|
||||||
|
gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddToEditOutbox temporarily store the resource from which a replace message with given ID was sent
|
||||||
|
func (c *Client) AddToEditOutbox(xmppId, resource string) {
|
||||||
|
c.locks.editOutboxLock.Lock()
|
||||||
|
defer c.locks.editOutboxLock.Unlock()
|
||||||
|
|
||||||
|
c.editOutbox[xmppId] = resource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) popFromEditOutbox(xmppId string) string {
|
||||||
|
c.locks.editOutboxLock.Lock()
|
||||||
|
defer c.locks.editOutboxLock.Unlock()
|
||||||
|
|
||||||
|
resource, ok := c.editOutbox[xmppId]
|
||||||
|
if ok {
|
||||||
|
delete(c.editOutbox, xmppId)
|
||||||
|
} else {
|
||||||
|
log.Warnf("No %v xmppId in edit outbox", xmppId)
|
||||||
|
}
|
||||||
|
return resource
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddToOutbox remembers the resource from which a message with given ID was sent
|
||||||
|
func (c *Client) AddToOutbox(xmppId, resource string) {
|
||||||
|
c.locks.outboxLock.Lock()
|
||||||
|
defer c.locks.outboxLock.Unlock()
|
||||||
|
|
||||||
|
c.outbox[xmppId] = resource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getFromOutbox(xmppId string) string {
|
||||||
|
c.locks.outboxLock.Lock()
|
||||||
|
defer c.locks.outboxLock.Unlock()
|
||||||
|
|
||||||
|
resource, ok := c.outbox[xmppId]
|
||||||
|
if !ok {
|
||||||
|
log.Warnf("No %v xmppId in outbox", xmppId)
|
||||||
|
}
|
||||||
|
return resource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getCarbonFullJids(isOutgoing bool, ignoredResource string) []string {
|
||||||
|
var jids []string
|
||||||
|
if isOutgoing {
|
||||||
|
for resource := range c.resourcesRange() {
|
||||||
|
if ignoredResource == "" || resource != ignoredResource {
|
||||||
|
jids = append(jids, c.jid+"/"+resource)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
jids = []string{c.jid}
|
||||||
|
}
|
||||||
|
return jids
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) calculateMessageHash(messageId int64, content client.MessageContent) uint64 {
|
||||||
|
var h maphash.Hash
|
||||||
|
h.SetSeed(c.msgHashSeed)
|
||||||
|
|
||||||
|
buf8 := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(buf8, uint64(messageId))
|
||||||
|
h.Write(buf8)
|
||||||
|
|
||||||
|
if content != nil && content.MessageContentType() == client.TypeMessageText {
|
||||||
|
textContent, ok := content.(*client.MessageText)
|
||||||
|
if !ok {
|
||||||
|
uhOh()
|
||||||
|
}
|
||||||
|
|
||||||
|
if textContent.Text != nil {
|
||||||
|
h.WriteString(textContent.Text.Text)
|
||||||
|
for _, entity := range textContent.Text.Entities {
|
||||||
|
buf4 := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(buf4, uint32(entity.Offset))
|
||||||
|
h.Write(buf4)
|
||||||
|
binary.BigEndian.PutUint32(buf4, uint32(entity.Length))
|
||||||
|
h.Write(buf4)
|
||||||
|
h.WriteString(entity.Type.TextEntityTypeType())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.Sum64()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) updateLastMessageHash(chatId, messageId int64, content client.MessageContent) {
|
||||||
|
c.locks.lastMsgHashesLock.Lock()
|
||||||
|
defer c.locks.lastMsgHashesLock.Unlock()
|
||||||
|
|
||||||
|
c.lastMsgHashes[chatId] = c.calculateMessageHash(messageId, content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) hasLastMessageHashChanged(chatId, messageId int64, content client.MessageContent) bool {
|
||||||
|
c.locks.lastMsgHashesLock.Lock()
|
||||||
|
defer c.locks.lastMsgHashesLock.Unlock()
|
||||||
|
|
||||||
|
oldHash, ok := c.lastMsgHashes[chatId]
|
||||||
|
newHash := c.calculateMessageHash(messageId, content)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
log.Warnf("Last message hash for chat %v does not exist", chatId)
|
||||||
|
}
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"old hash": oldHash,
|
||||||
|
"new hash": newHash,
|
||||||
|
}).Info("Message hashes")
|
||||||
|
|
||||||
|
return !ok || oldHash != newHash
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) UpdateLastChatMessageId(chatId int64, messageId string) {
|
||||||
|
c.locks.lastMsgIdsLock.Lock()
|
||||||
|
defer c.locks.lastMsgIdsLock.Unlock()
|
||||||
|
|
||||||
|
c.lastMsgIds[chatId] = messageId
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getLastChatMessageId(chatId int64) (string, bool) {
|
||||||
|
c.locks.lastMsgIdsLock.RLock()
|
||||||
|
defer c.locks.lastMsgIdsLock.RUnlock()
|
||||||
|
|
||||||
|
xmppId, ok := c.lastMsgIds[chatId]
|
||||||
|
return xmppId, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getFormatter() formatter.MarkupModeType {
|
||||||
|
return formatter.MarkupModeXEP0393
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) usernamesToString(usernames []string) string {
|
||||||
|
var atUsernames []string
|
||||||
|
for _, username := range usernames {
|
||||||
|
atUsernames = append(atUsernames, "@"+username)
|
||||||
|
}
|
||||||
|
return strings.Join(atUsernames, ", ")
|
||||||
|
}
|
||||||
|
|
|
@ -369,6 +369,53 @@ func TestMessageAnimation(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMessageTtl1(t *testing.T) {
|
||||||
|
ttl := client.Message{
|
||||||
|
Content: &client.MessageChatSetMessageAutoDeleteTime{},
|
||||||
|
}
|
||||||
|
text := (&Client{}).messageToText(&ttl, false)
|
||||||
|
if text != "The self-destruct timer was disabled" {
|
||||||
|
t.Errorf("Wrong anonymous off ttl label: %v", text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMessageTtl2(t *testing.T) {
|
||||||
|
ttl := client.Message{
|
||||||
|
Content: &client.MessageChatSetMessageAutoDeleteTime{
|
||||||
|
MessageAutoDeleteTime: 3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
text := (&Client{}).messageToText(&ttl, false)
|
||||||
|
if text != "The self-destruct timer was set to 3 seconds" {
|
||||||
|
t.Errorf("Wrong anonymous ttl label: %v", text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMessageTtl3(t *testing.T) {
|
||||||
|
ttl := client.Message{
|
||||||
|
Content: &client.MessageChatSetMessageAutoDeleteTime{
|
||||||
|
FromUserId: 3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
text := (&Client{}).messageToText(&ttl, false)
|
||||||
|
if text != "unknown contact: TDlib instance is offline disabled the self-destruct timer" {
|
||||||
|
t.Errorf("Wrong off ttl label: %v", text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMessageTtl4(t *testing.T) {
|
||||||
|
ttl := client.Message{
|
||||||
|
Content: &client.MessageChatSetMessageAutoDeleteTime{
|
||||||
|
FromUserId: 3,
|
||||||
|
MessageAutoDeleteTime: 3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
text := (&Client{}).messageToText(&ttl, false)
|
||||||
|
if text != "unknown contact: TDlib instance is offline set the self-destruct timer to 3 seconds" {
|
||||||
|
t.Errorf("Wrong ttl label: %v", text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMessageUnknown(t *testing.T) {
|
func TestMessageUnknown(t *testing.T) {
|
||||||
unknown := client.Message{
|
unknown := client.Message{
|
||||||
Content: &client.MessageExpiredPhoto{},
|
Content: &client.MessageExpiredPhoto{},
|
||||||
|
@ -384,20 +431,17 @@ func TestMessageToPrefix1(t *testing.T) {
|
||||||
Id: 42,
|
Id: 42,
|
||||||
IsOutgoing: true,
|
IsOutgoing: true,
|
||||||
ForwardInfo: &client.MessageForwardInfo{
|
ForwardInfo: &client.MessageForwardInfo{
|
||||||
Origin: &client.MessageForwardOriginHiddenUser{
|
Origin: &client.MessageOriginHiddenUser{
|
||||||
SenderName: "ziz",
|
SenderName: "ziz",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "", "", nil)
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "", "", false)
|
||||||
if prefix != "➡ 42 | fwd: ziz" {
|
if prefix != "➡ 42 | fwd: ziz" {
|
||||||
t.Errorf("Wrong prefix: %v", prefix)
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
}
|
}
|
||||||
if replyStart != 0 {
|
if gatewayReply != nil {
|
||||||
t.Errorf("Wrong replyStart: %v", replyStart)
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
}
|
|
||||||
if replyEnd != 0 {
|
|
||||||
t.Errorf("Wrong replyEnd: %v", replyEnd)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,20 +449,17 @@ func TestMessageToPrefix2(t *testing.T) {
|
||||||
message := client.Message{
|
message := client.Message{
|
||||||
Id: 56,
|
Id: 56,
|
||||||
ForwardInfo: &client.MessageForwardInfo{
|
ForwardInfo: &client.MessageForwardInfo{
|
||||||
Origin: &client.MessageForwardOriginChannel{
|
Origin: &client.MessageOriginChannel{
|
||||||
AuthorSignature: "zaz",
|
AuthorSignature: "zaz",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "", nil)
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "", false)
|
||||||
if prefix != "⬅ 56 | fwd: (zaz) | preview: y.jpg" {
|
if prefix != "⬅ 56 | fwd: (zaz) | preview: y.jpg" {
|
||||||
t.Errorf("Wrong prefix: %v", prefix)
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
}
|
}
|
||||||
if replyStart != 0 {
|
if gatewayReply != nil {
|
||||||
t.Errorf("Wrong replyStart: %v", replyStart)
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
}
|
|
||||||
if replyEnd != 0 {
|
|
||||||
t.Errorf("Wrong replyEnd: %v", replyEnd)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -426,20 +467,17 @@ func TestMessageToPrefix3(t *testing.T) {
|
||||||
message := client.Message{
|
message := client.Message{
|
||||||
Id: 56,
|
Id: 56,
|
||||||
ForwardInfo: &client.MessageForwardInfo{
|
ForwardInfo: &client.MessageForwardInfo{
|
||||||
Origin: &client.MessageForwardOriginChannel{
|
Origin: &client.MessageOriginChannel{
|
||||||
AuthorSignature: "zuz",
|
AuthorSignature: "zuz",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg", nil)
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg", false)
|
||||||
if prefix != "< 56 | fwd: (zuz) | file: a.jpg" {
|
if prefix != "< 56 | fwd: (zuz) | file: a.jpg" {
|
||||||
t.Errorf("Wrong prefix: %v", prefix)
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
}
|
}
|
||||||
if replyStart != 0 {
|
if gatewayReply != nil {
|
||||||
t.Errorf("Wrong replyStart: %v", replyStart)
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
}
|
|
||||||
if replyEnd != 0 {
|
|
||||||
t.Errorf("Wrong replyEnd: %v", replyEnd)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -448,15 +486,12 @@ func TestMessageToPrefix4(t *testing.T) {
|
||||||
Id: 23,
|
Id: 23,
|
||||||
IsOutgoing: true,
|
IsOutgoing: true,
|
||||||
}
|
}
|
||||||
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", nil)
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", false)
|
||||||
if prefix != "> 23" {
|
if prefix != "> 23" {
|
||||||
t.Errorf("Wrong prefix: %v", prefix)
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
}
|
}
|
||||||
if replyStart != 0 {
|
if gatewayReply != nil {
|
||||||
t.Errorf("Wrong replyStart: %v", replyStart)
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
}
|
|
||||||
if replyEnd != 0 {
|
|
||||||
t.Errorf("Wrong replyEnd: %v", replyEnd)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -464,46 +499,95 @@ func TestMessageToPrefix5(t *testing.T) {
|
||||||
message := client.Message{
|
message := client.Message{
|
||||||
Id: 560,
|
Id: 560,
|
||||||
ForwardInfo: &client.MessageForwardInfo{
|
ForwardInfo: &client.MessageForwardInfo{
|
||||||
Origin: &client.MessageForwardOriginChat{
|
Origin: &client.MessageOriginChat{
|
||||||
AuthorSignature: "zyz",
|
AuthorSignature: "zyz",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "h.jpg", "a.jpg", nil)
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "h.jpg", "a.jpg", false)
|
||||||
if prefix != "< 560 | fwd: (zyz) | preview: h.jpg | file: a.jpg" {
|
if prefix != "< 560 | fwd: (zyz) | preview: h.jpg | file: a.jpg" {
|
||||||
t.Errorf("Wrong prefix: %v", prefix)
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
}
|
}
|
||||||
if replyStart != 0 {
|
if gatewayReply != nil {
|
||||||
t.Errorf("Wrong replyStart: %v", replyStart)
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
}
|
|
||||||
if replyEnd != 0 {
|
|
||||||
t.Errorf("Wrong replyEnd: %v", replyEnd)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageToPrefix6(t *testing.T) {
|
func TestMessageToPrefix6(t *testing.T) {
|
||||||
message := client.Message{
|
message := client.Message{
|
||||||
Id: 23,
|
Id: 23,
|
||||||
|
ChatId: 25,
|
||||||
IsOutgoing: true,
|
IsOutgoing: true,
|
||||||
ReplyToMessageId: 42,
|
ReplyTo: &client.MessageReplyToMessage{
|
||||||
|
ChatId: 41,
|
||||||
|
Quote: &client.TextQuote{
|
||||||
|
Text: &client.FormattedText{
|
||||||
|
Text: "tist\nuz\niz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Origin: &client.MessageOriginHiddenUser{
|
||||||
|
SenderName: "ziz",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
reply := client.Message{
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", false)
|
||||||
Id: 42,
|
if prefix != "> 23 | reply: ziz @ unknown contact: TDlib instance is offline | tist uz iz" {
|
||||||
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
|
}
|
||||||
|
if gatewayReply != nil {
|
||||||
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMessageToPrefix7(t *testing.T) {
|
||||||
|
message := client.Message{
|
||||||
|
Id: 23,
|
||||||
|
ChatId: 42,
|
||||||
|
IsOutgoing: true,
|
||||||
|
ReplyTo: &client.MessageReplyToMessage{
|
||||||
|
ChatId: 41,
|
||||||
Content: &client.MessageText{
|
Content: &client.MessageText{
|
||||||
Text: &client.FormattedText{
|
Text: &client.FormattedText{
|
||||||
Text: "tist",
|
Text: "tist",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Origin: &client.MessageOriginChannel{
|
||||||
|
AuthorSignature: "zaz",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", &reply)
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", false)
|
||||||
if prefix != "> 23 | reply: 42 | | tist" {
|
if prefix != "> 23 | reply: (zaz) @ unknown contact: TDlib instance is offline | tist" {
|
||||||
t.Errorf("Wrong prefix: %v", prefix)
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
}
|
}
|
||||||
if replyStart != 4 {
|
if gatewayReply != nil {
|
||||||
t.Errorf("Wrong replyStart: %v", replyStart)
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
}
|
}
|
||||||
if replyEnd != 26 {
|
}
|
||||||
t.Errorf("Wrong replyEnd: %v", replyEnd)
|
|
||||||
|
func TestMessageToPrefix8(t *testing.T) {
|
||||||
|
message := client.Message{
|
||||||
|
Id: 23,
|
||||||
|
ChatId: 42,
|
||||||
|
IsOutgoing: true,
|
||||||
|
ReplyTo: &client.MessageReplyToMessage{
|
||||||
|
ChatId: 41,
|
||||||
|
Content: &client.MessageText{
|
||||||
|
Text: &client.FormattedText{
|
||||||
|
Text: "tist",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Origin: &client.MessageOriginChannel{
|
||||||
|
AuthorSignature: "zuz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", true)
|
||||||
|
if prefix != "> 23" {
|
||||||
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
|
}
|
||||||
|
if gatewayReply != nil {
|
||||||
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,8 @@ import (
|
||||||
"gosrc.io/xmpp/stanza"
|
"gosrc.io/xmpp/stanza"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NickExtension is from XEP-0172
|
// PresenceNickExtension is from XEP-0172
|
||||||
type NickExtension struct {
|
type PresenceNickExtension struct {
|
||||||
XMLName xml.Name `xml:"http://jabber.org/protocol/nick nick"`
|
XMLName xml.Name `xml:"http://jabber.org/protocol/nick nick"`
|
||||||
Text string `xml:",chardata"`
|
Text string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
@ -154,12 +154,19 @@ type CarbonSent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ComponentPrivilege is from XEP-0356
|
// ComponentPrivilege is from XEP-0356
|
||||||
type ComponentPrivilege struct {
|
type ComponentPrivilege1 struct {
|
||||||
XMLName xml.Name `xml:"urn:xmpp:privilege:1 privilege"`
|
XMLName xml.Name `xml:"urn:xmpp:privilege:1 privilege"`
|
||||||
Perms []ComponentPerm `xml:"perm"`
|
Perms []ComponentPerm `xml:"perm"`
|
||||||
Forwarded stanza.Forwarded `xml:"urn:xmpp:forward:0 forwarded"`
|
Forwarded stanza.Forwarded `xml:"urn:xmpp:forward:0 forwarded"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ComponentPrivilege is from XEP-0356
|
||||||
|
type ComponentPrivilege2 struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:privilege:2 privilege"`
|
||||||
|
Perms []ComponentPerm `xml:"perm"`
|
||||||
|
Forwarded stanza.Forwarded `xml:"urn:xmpp:forward:0 forwarded"`
|
||||||
|
}
|
||||||
|
|
||||||
// ComponentPerm is from XEP-0356
|
// ComponentPerm is from XEP-0356
|
||||||
type ComponentPerm struct {
|
type ComponentPerm struct {
|
||||||
XMLName xml.Name `xml:"perm"`
|
XMLName xml.Name `xml:"perm"`
|
||||||
|
@ -186,8 +193,28 @@ type Replace struct {
|
||||||
Id string `xml:"id,attr"`
|
Id string `xml:"id,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryRegister is from XEP-0077
|
||||||
|
type QueryRegister struct {
|
||||||
|
XMLName xml.Name `xml:"jabber:iq:register query"`
|
||||||
|
Instructions string `xml:"instructions"`
|
||||||
|
Username string `xml:"username"`
|
||||||
|
Registered *QueryRegisterRegistered `xml:"registered"`
|
||||||
|
Remove *QueryRegisterRemove `xml:"remove"`
|
||||||
|
ResultSet *stanza.ResultSet `xml:"set,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRegisterRegistered is a child element from XEP-0077
|
||||||
|
type QueryRegisterRegistered struct {
|
||||||
|
XMLName xml.Name `xml:"registered"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRegisterRemove is a child element from XEP-0077
|
||||||
|
type QueryRegisterRemove struct {
|
||||||
|
XMLName xml.Name `xml:"remove"`
|
||||||
|
}
|
||||||
|
|
||||||
// Namespace is a namespace!
|
// Namespace is a namespace!
|
||||||
func (c NickExtension) Namespace() string {
|
func (c PresenceNickExtension) Namespace() string {
|
||||||
return c.XMLName.Space
|
return c.XMLName.Space
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,7 +254,12 @@ func (c CarbonSent) Namespace() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Namespace is a namespace!
|
// Namespace is a namespace!
|
||||||
func (c ComponentPrivilege) Namespace() string {
|
func (c ComponentPrivilege1) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c ComponentPrivilege2) Namespace() string {
|
||||||
return c.XMLName.Space
|
return c.XMLName.Space
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,6 +268,16 @@ func (c Replace) Namespace() string {
|
||||||
return c.XMLName.Space
|
return c.XMLName.Space
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c QueryRegister) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSet getsets!
|
||||||
|
func (c QueryRegister) GetSet() *stanza.ResultSet {
|
||||||
|
return c.ResultSet
|
||||||
|
}
|
||||||
|
|
||||||
// Name is a packet name
|
// Name is a packet name
|
||||||
func (ClientMessage) Name() string {
|
func (ClientMessage) Name() string {
|
||||||
return "message"
|
return "message"
|
||||||
|
@ -259,13 +301,7 @@ func init() {
|
||||||
stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{
|
stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{
|
||||||
"http://jabber.org/protocol/nick",
|
"http://jabber.org/protocol/nick",
|
||||||
"nick",
|
"nick",
|
||||||
}, NickExtension{})
|
}, PresenceNickExtension{})
|
||||||
|
|
||||||
// message nick
|
|
||||||
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
|
||||||
"http://jabber.org/protocol/nick",
|
|
||||||
"nick",
|
|
||||||
}, NickExtension{})
|
|
||||||
|
|
||||||
// presence vcard update
|
// presence vcard update
|
||||||
stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{
|
stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{
|
||||||
|
@ -303,15 +339,27 @@ func init() {
|
||||||
"sent",
|
"sent",
|
||||||
}, CarbonSent{})
|
}, CarbonSent{})
|
||||||
|
|
||||||
// component privilege
|
// component privilege v1
|
||||||
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
"urn:xmpp:privilege:1",
|
"urn:xmpp:privilege:1",
|
||||||
"privilege",
|
"privilege",
|
||||||
}, ComponentPrivilege{})
|
}, ComponentPrivilege1{})
|
||||||
|
|
||||||
|
// component privilege v2
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:privilege:2",
|
||||||
|
"privilege",
|
||||||
|
}, ComponentPrivilege2{})
|
||||||
|
|
||||||
// message edit
|
// message edit
|
||||||
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
"urn:xmpp:message-correct:0",
|
"urn:xmpp:message-correct:0",
|
||||||
"replace",
|
"replace",
|
||||||
}, Replace{})
|
}, Replace{})
|
||||||
|
|
||||||
|
// register query
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTIQ, xml.Name{
|
||||||
|
"jabber:iq:register",
|
||||||
|
"query",
|
||||||
|
}, QueryRegister{})
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,14 @@ package gateway
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"dev.narayana.im/narayana/telegabber/badger"
|
"dev.narayana.im/narayana/telegabber/badger"
|
||||||
"dev.narayana.im/narayana/telegabber/xmpp/extensions"
|
"dev.narayana.im/narayana/telegabber/xmpp/extensions"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/soheilhy/args"
|
"github.com/soheilhy/args"
|
||||||
"gosrc.io/xmpp"
|
"gosrc.io/xmpp"
|
||||||
|
@ -22,6 +24,18 @@ type Reply struct {
|
||||||
End uint64
|
End uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MarkerType byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
MarkerTypeReceived MarkerType = iota
|
||||||
|
MarkerTypeDisplayed
|
||||||
|
)
|
||||||
|
|
||||||
|
type marker struct {
|
||||||
|
Type MarkerType
|
||||||
|
Id string
|
||||||
|
}
|
||||||
|
|
||||||
const NSNick string = "http://jabber.org/protocol/nick"
|
const NSNick string = "http://jabber.org/protocol/nick"
|
||||||
|
|
||||||
// Queue stores presences to send later
|
// Queue stores presences to send later
|
||||||
|
@ -38,30 +52,46 @@ var IdsDB badger.IdsDB
|
||||||
// were changed and need to be re-flushed to the YamlDB
|
// were changed and need to be re-flushed to the YamlDB
|
||||||
var DirtySessions = false
|
var DirtySessions = false
|
||||||
|
|
||||||
// MessageOutgoingPermission allows to fake outgoing messages by foreign JIDs
|
// MessageOutgoingPermissionVersion contains a XEP-0356 version to fake outgoing messages by foreign JIDs
|
||||||
var MessageOutgoingPermission = false
|
var MessageOutgoingPermissionVersion = 0
|
||||||
|
|
||||||
// SendMessage creates and sends a message stanza
|
// SendMessage creates and sends a message stanza
|
||||||
func SendMessage(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, isOutgoing bool, nick string) {
|
func SendMessage(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, replaceId string, isCarbon, requestReceipt bool) {
|
||||||
sendMessageWrapper(to, from, body, id, component, reply, "", isOutgoing, nick)
|
sendMessageWrapper(to, from, body, id, component, reply, nil, "", replaceId, isCarbon, requestReceipt)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendServiceMessage creates and sends a simple message stanza from transport
|
// SendServiceMessage creates and sends a simple message stanza from transport
|
||||||
func SendServiceMessage(to string, body string, component *xmpp.Component) {
|
func SendServiceMessage(to string, body string, component *xmpp.Component) {
|
||||||
sendMessageWrapper(to, "", body, "", component, nil, "", false, "")
|
var id string
|
||||||
|
if uuid, err := uuid.NewRandom(); err == nil {
|
||||||
|
id = uuid.String()
|
||||||
|
}
|
||||||
|
sendMessageWrapper(to, "", body, id, component, nil, nil, "", "", false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendTextMessage creates and sends a simple message stanza
|
// SendTextMessage creates and sends a simple message stanza
|
||||||
func SendTextMessage(to string, from string, body string, component *xmpp.Component) {
|
func SendTextMessage(to string, from string, body string, component *xmpp.Component) {
|
||||||
sendMessageWrapper(to, from, body, "", component, nil, "", false, "")
|
var id string
|
||||||
|
if uuid, err := uuid.NewRandom(); err == nil {
|
||||||
|
id = uuid.String()
|
||||||
|
}
|
||||||
|
sendMessageWrapper(to, from, body, id, component, nil, nil, "", "", false, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendMessageWithOOB creates and sends a message stanza with OOB URL
|
// SendMessageWithOOB creates and sends a message stanza with OOB URL
|
||||||
func SendMessageWithOOB(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, oob string, isOutgoing bool, nick string) {
|
func SendMessageWithOOB(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, oob, replaceId string, isCarbon, requestReceipt bool) {
|
||||||
sendMessageWrapper(to, from, body, id, component, reply, oob, isOutgoing, nick)
|
sendMessageWrapper(to, from, body, id, component, reply, nil, oob, replaceId, isCarbon, requestReceipt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendMessageWrapper(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, oob string, isOutgoing bool, nick string) {
|
// SendMessageMarker creates and sends a message stanza with a XEP-0333 marker
|
||||||
|
func SendMessageMarker(to string, from string, component *xmpp.Component, markerType MarkerType, markerId string) {
|
||||||
|
sendMessageWrapper(to, from, "", "", component, nil, &marker{
|
||||||
|
Type: markerType,
|
||||||
|
Id: markerId,
|
||||||
|
}, "", "", false, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendMessageWrapper(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, marker *marker, oob, replaceId string, isCarbon, requestReceipt bool) {
|
||||||
toJid, err := stanza.NewJid(to)
|
toJid, err := stanza.NewJid(to)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
|
@ -83,7 +113,7 @@ func sendMessageWrapper(to string, from string, body string, id string, componen
|
||||||
logFrom = from
|
logFrom = from
|
||||||
messageFrom = from + "@" + componentJid
|
messageFrom = from + "@" + componentJid
|
||||||
}
|
}
|
||||||
if isOutgoing {
|
if isCarbon {
|
||||||
messageTo = messageFrom
|
messageTo = messageFrom
|
||||||
messageFrom = bareTo + "/" + Jid.Resource
|
messageFrom = bareTo + "/" + Jid.Resource
|
||||||
} else {
|
} else {
|
||||||
|
@ -119,13 +149,25 @@ func sendMessageWrapper(to string, from string, body string, id string, componen
|
||||||
message.Extensions = append(message.Extensions, extensions.NewReplyFallback(reply.Start, reply.End))
|
message.Extensions = append(message.Extensions, extensions.NewReplyFallback(reply.Start, reply.End))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nick != "" {
|
if marker != nil {
|
||||||
message.Extensions = append(message.Extensions, extensions.NickExtension{
|
if marker.Type == MarkerTypeReceived {
|
||||||
Text: nick,
|
message.Extensions = append(message.Extensions, stanza.MarkReceived{ID: marker.Id})
|
||||||
})
|
} else if marker.Type == MarkerTypeDisplayed {
|
||||||
|
message.Extensions = append(message.Extensions, stanza.MarkDisplayed{ID: marker.Id})
|
||||||
|
message.Extensions = append(message.Extensions, stanza.ReceiptReceived{ID: marker.Id})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !isCarbon && toJid.Resource != "" {
|
||||||
|
message.Extensions = append(message.Extensions, stanza.HintNoCopy{})
|
||||||
|
}
|
||||||
|
if requestReceipt {
|
||||||
|
message.Extensions = append(message.Extensions, stanza.Markable{})
|
||||||
|
}
|
||||||
|
if replaceId != "" {
|
||||||
|
message.Extensions = append(message.Extensions, extensions.Replace{Id: replaceId})
|
||||||
}
|
}
|
||||||
|
|
||||||
if isOutgoing {
|
if isCarbon {
|
||||||
carbonMessage := extensions.ClientMessage{
|
carbonMessage := extensions.ClientMessage{
|
||||||
Attrs: stanza.Attrs{
|
Attrs: stanza.Attrs{
|
||||||
From: bareTo,
|
From: bareTo,
|
||||||
|
@ -144,11 +186,19 @@ func sendMessageWrapper(to string, from string, body string, id string, componen
|
||||||
To: toJid.Domain,
|
To: toJid.Domain,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
privilegeMessage.Extensions = append(privilegeMessage.Extensions, extensions.ComponentPrivilege{
|
if MessageOutgoingPermissionVersion == 2 {
|
||||||
|
privilegeMessage.Extensions = append(privilegeMessage.Extensions, extensions.ComponentPrivilege2{
|
||||||
Forwarded: stanza.Forwarded{
|
Forwarded: stanza.Forwarded{
|
||||||
Stanza: carbonMessage,
|
Stanza: carbonMessage,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
privilegeMessage.Extensions = append(privilegeMessage.Extensions, extensions.ComponentPrivilege1{
|
||||||
|
Forwarded: stanza.Forwarded{
|
||||||
|
Stanza: carbonMessage,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
sendMessage(&privilegeMessage, component)
|
sendMessage(&privilegeMessage, component)
|
||||||
} else {
|
} else {
|
||||||
sendMessage(&message, component)
|
sendMessage(&message, component)
|
||||||
|
@ -274,7 +324,7 @@ func newPresence(bareJid string, to string, args ...args.V) stanza.Presence {
|
||||||
if SPNickname.IsSet(args) {
|
if SPNickname.IsSet(args) {
|
||||||
nickname := SPNickname.Get(args)
|
nickname := SPNickname.Get(args)
|
||||||
if nickname != "" {
|
if nickname != "" {
|
||||||
presence.Extensions = append(presence.Extensions, extensions.NickExtension{
|
presence.Extensions = append(presence.Extensions, extensions.PresenceNickExtension{
|
||||||
Text: nickname,
|
Text: nickname,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -337,6 +387,20 @@ func SendPresence(component *xmpp.Component, to string, args ...args.V) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SPAppendFrom appends numeric from and resource to varargs
|
||||||
|
func SPAppendFrom(oldArgs []args.V, id int64) []args.V {
|
||||||
|
newArgs := append(oldArgs, SPFrom(strconv.FormatInt(id, 10)))
|
||||||
|
newArgs = append(newArgs, SPResource(Jid.Resource))
|
||||||
|
return newArgs
|
||||||
|
}
|
||||||
|
|
||||||
|
// SimplePresence crafts simple presence varargs
|
||||||
|
func SimplePresence(from int64, typ string) []args.V {
|
||||||
|
args := []args.V{SPType(typ)}
|
||||||
|
args = SPAppendFrom(args, from)
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
// ResumableSend tries to resume the connection once and sends the packet again
|
// ResumableSend tries to resume the connection once and sends the packet again
|
||||||
func ResumableSend(component *xmpp.Component, packet stanza.Packet) error {
|
func ResumableSend(component *xmpp.Component, packet stanza.Packet) error {
|
||||||
err := component.Send(packet)
|
err := component.Send(packet)
|
||||||
|
@ -354,6 +418,12 @@ func ResumableSend(component *xmpp.Component, packet stanza.Packet) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SubscribeToTransport ensures a two-way subscription to the transport
|
||||||
|
func SubscribeToTransport(component *xmpp.Component, jid string) {
|
||||||
|
SendPresence(component, jid, SPType("subscribe"))
|
||||||
|
SendPresence(component, jid, SPType("subscribed"))
|
||||||
|
}
|
||||||
|
|
||||||
// SplitJID tokenizes a JID string to bare JID and resource
|
// SplitJID tokenizes a JID string to bare JID and resource
|
||||||
func SplitJID(from string) (string, string, bool) {
|
func SplitJID(from string) (string, string, bool) {
|
||||||
fromJid, err := stanza.NewJid(from)
|
fromJid, err := stanza.NewJid(from)
|
||||||
|
|
289
xmpp/handlers.go
289
xmpp/handlers.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -15,6 +16,7 @@ import (
|
||||||
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/soheilhy/args"
|
||||||
"gosrc.io/xmpp"
|
"gosrc.io/xmpp"
|
||||||
"gosrc.io/xmpp/stanza"
|
"gosrc.io/xmpp/stanza"
|
||||||
)
|
)
|
||||||
|
@ -56,6 +58,22 @@ func HandleIq(s xmpp.Sender, p stanza.Packet) {
|
||||||
go handleGetDiscoInfo(s, iq)
|
go handleGetDiscoInfo(s, iq)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
_, ok = iq.Payload.(*stanza.DiscoItems)
|
||||||
|
if ok {
|
||||||
|
go handleGetDiscoItems(s, iq)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, ok = iq.Payload.(*extensions.QueryRegister)
|
||||||
|
if ok {
|
||||||
|
go handleGetQueryRegister(s, iq)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if iq.Type == "set" {
|
||||||
|
query, ok := iq.Payload.(*extensions.QueryRegister)
|
||||||
|
if ok {
|
||||||
|
go handleSetQueryRegister(s, iq, query)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,8 +108,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||||
session, ok := sessions[bare]
|
session, ok := sessions[bare]
|
||||||
if !ok {
|
if !ok {
|
||||||
if msg.To == gatewayJid {
|
if msg.To == gatewayJid {
|
||||||
gateway.SendPresence(component, msg.From, gateway.SPType("subscribe"))
|
gateway.SubscribeToTransport(component, msg.From)
|
||||||
gateway.SendPresence(component, msg.From, gateway.SPType("subscribed"))
|
|
||||||
} else {
|
} else {
|
||||||
log.Error("Message from stranger")
|
log.Error("Message from stranger")
|
||||||
}
|
}
|
||||||
|
@ -148,7 +165,12 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||||
"end": body.End,
|
"end": body.End,
|
||||||
}).Warn(errors.Wrap(err, "Failed to parse fallback end!"))
|
}).Warn(errors.Wrap(err, "Failed to parse fallback end!"))
|
||||||
}
|
}
|
||||||
text = text[:start] + text[end:]
|
|
||||||
|
fullRunes := []rune(text)
|
||||||
|
cutRunes := make([]rune, 0, len(text)-int(end-start))
|
||||||
|
cutRunes = append(cutRunes, fullRunes[:start]...)
|
||||||
|
cutRunes = append(cutRunes, fullRunes[end:]...)
|
||||||
|
text = string(cutRunes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var replaceId int64
|
var replaceId int64
|
||||||
|
@ -167,6 +189,8 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session.SendMessageLock.Lock()
|
||||||
|
defer session.SendMessageLock.Unlock()
|
||||||
tgMessageId := session.ProcessOutgoingMessage(toID, text, msg.From, replyId, replaceId)
|
tgMessageId := session.ProcessOutgoingMessage(toID, text, msg.From, replyId, replaceId)
|
||||||
if tgMessageId != 0 {
|
if tgMessageId != 0 {
|
||||||
if replaceId != 0 {
|
if replaceId != 0 {
|
||||||
|
@ -175,9 +199,13 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to replace id %v with %v %v", replace.Id, msg.Id, tgMessageId)
|
log.Errorf("Failed to replace id %v with %v %v", replace.Id, msg.Id, tgMessageId)
|
||||||
} */
|
} */
|
||||||
|
session.AddToEditOutbox(replace.Id, resource)
|
||||||
} else {
|
} else {
|
||||||
err = gateway.IdsDB.Set(session.Session.Login, bare, toID, tgMessageId, msg.Id)
|
err = gateway.IdsDB.Set(session.Session.Login, bare, toID, tgMessageId, msg.Id)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
|
// session.AddToOutbox(msg.Id, resource)
|
||||||
|
session.UpdateLastChatMessageId(toID, msg.Id)
|
||||||
|
} else {
|
||||||
log.Errorf("Failed to save ids %v/%v %v", toID, tgMessageId, msg.Id)
|
log.Errorf("Failed to save ids %v/%v %v", toID, tgMessageId, msg.Id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -206,16 +234,51 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.Body == "" {
|
if msg.Body == "" {
|
||||||
var privilege extensions.ComponentPrivilege
|
var privilege1 extensions.ComponentPrivilege1
|
||||||
if ok := msg.Get(&privilege); ok {
|
if ok := msg.Get(&privilege1); ok {
|
||||||
log.Debugf("privilege: %#v", privilege)
|
log.Debugf("privilege1: %#v", privilege1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, perm := range privilege.Perms {
|
for _, perm := range privilege1.Perms {
|
||||||
if perm.Access == "message" && perm.Type == "outgoing" {
|
if perm.Access == "message" && perm.Type == "outgoing" {
|
||||||
gateway.MessageOutgoingPermission = true
|
gateway.MessageOutgoingPermissionVersion = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var privilege2 extensions.ComponentPrivilege2
|
||||||
|
if ok := msg.Get(&privilege2); ok {
|
||||||
|
log.Debugf("privilege2: %#v", privilege2)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, perm := range privilege2.Perms {
|
||||||
|
if perm.Access == "message" && perm.Type == "outgoing" {
|
||||||
|
gateway.MessageOutgoingPermissionVersion = 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var displayed stanza.MarkDisplayed
|
||||||
|
msg.Get(&displayed)
|
||||||
|
if displayed.ID != "" {
|
||||||
|
log.Debugf("displayed: %#v", displayed)
|
||||||
|
|
||||||
|
bare, _, ok := gateway.SplitJID(msg.From)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
session, ok := sessions[bare]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
toID, ok := toToID(msg.To)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msgId, err := strconv.ParseInt(displayed.ID, 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
session.MarkAsRead(toID, msgId)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.Type == "error" {
|
if msg.Type == "error" {
|
||||||
|
@ -335,13 +398,21 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
|
||||||
log.Error(errors.Wrap(err, "TDlib connection failure"))
|
log.Error(errors.Wrap(err, "TDlib connection failure"))
|
||||||
} else {
|
} else {
|
||||||
for status := range session.StatusesRange() {
|
for status := range session.StatusesRange() {
|
||||||
|
show, description, typ := status.Destruct()
|
||||||
|
newArgs := []args.V{
|
||||||
|
gateway.SPImmed(false),
|
||||||
|
}
|
||||||
|
if typ != "" {
|
||||||
|
newArgs = append(newArgs, gateway.SPType(typ))
|
||||||
|
}
|
||||||
go session.ProcessStatusUpdate(
|
go session.ProcessStatusUpdate(
|
||||||
status.ID,
|
status.ID,
|
||||||
status.Description,
|
description,
|
||||||
status.XMPP,
|
show,
|
||||||
gateway.SPImmed(false),
|
newArgs...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
session.UpdateChatNicknames()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
@ -414,8 +485,11 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ) {
|
||||||
_, ok := toToID(iq.To)
|
_, ok := toToID(iq.To)
|
||||||
if ok {
|
if ok {
|
||||||
disco.AddIdentity("", "account", "registered")
|
disco.AddIdentity("", "account", "registered")
|
||||||
|
disco.AddFeatures(stanza.NSMsgChatMarkers)
|
||||||
|
disco.AddFeatures(stanza.NSMsgReceipts)
|
||||||
} else {
|
} else {
|
||||||
disco.AddIdentity("Telegram Gateway", "gateway", "telegram")
|
disco.AddIdentity("Telegram Gateway", "gateway", "telegram")
|
||||||
|
disco.AddFeatures("jabber:iq:register")
|
||||||
}
|
}
|
||||||
answer.Payload = disco
|
answer.Payload = disco
|
||||||
|
|
||||||
|
@ -430,6 +504,189 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ) {
|
||||||
_ = gateway.ResumableSend(component, answer)
|
_ = 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
|
||||||
|
}
|
||||||
|
|
||||||
|
answer.Payload = answer.DiscoItems()
|
||||||
|
|
||||||
|
component, ok := s.(*xmpp.Component)
|
||||||
|
if !ok {
|
||||||
|
log.Error("Not a component")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = gateway.ResumableSend(component, answer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleGetQueryRegister(s xmpp.Sender, iq *stanza.IQ) {
|
||||||
|
component, ok := s.(*xmpp.Component)
|
||||||
|
if !ok {
|
||||||
|
log.Error("Not a component")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
var login string
|
||||||
|
bare, _, ok := gateway.SplitJID(iq.From)
|
||||||
|
if ok {
|
||||||
|
session, ok := sessions[bare]
|
||||||
|
if ok {
|
||||||
|
login = session.Session.Login
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var query stanza.IQPayload
|
||||||
|
if login == "" {
|
||||||
|
query = extensions.QueryRegister{
|
||||||
|
Instructions: fmt.Sprintf("Authorization in Telegram is a multi-step process, so please accept %v to your contacts and follow further instructions (provide the authentication code there, etc.).\nFor now, please provide your login.", iq.To),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
query = extensions.QueryRegister{
|
||||||
|
Instructions: "Already logged in",
|
||||||
|
Username: login,
|
||||||
|
Registered: &extensions.QueryRegisterRegistered{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
answer.Payload = query
|
||||||
|
|
||||||
|
log.Debugf("%#v", query)
|
||||||
|
|
||||||
|
_ = gateway.ResumableSend(component, answer)
|
||||||
|
|
||||||
|
if login == "" {
|
||||||
|
gateway.SubscribeToTransport(component, iq.From)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSetQueryRegister(s xmpp.Sender, iq *stanza.IQ, query *extensions.QueryRegister) {
|
||||||
|
component, ok := s.(*xmpp.Component)
|
||||||
|
if !ok {
|
||||||
|
log.Error("Not a component")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
defer gateway.ResumableSend(component, answer)
|
||||||
|
|
||||||
|
if query.Remove != nil {
|
||||||
|
iqAnswerSetError(answer, query, 405)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var login string
|
||||||
|
var session *telegram.Client
|
||||||
|
bare, resource, ok := gateway.SplitJID(iq.From)
|
||||||
|
if ok {
|
||||||
|
session, ok = sessions[bare]
|
||||||
|
if ok {
|
||||||
|
login = session.Session.Login
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if login == "" {
|
||||||
|
if !ok {
|
||||||
|
session, ok = getTelegramInstance(bare, &persistence.Session{}, component)
|
||||||
|
if !ok {
|
||||||
|
iqAnswerSetError(answer, query, 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := session.TryLogin(resource, query.Username)
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == telegram.TelegramAuthDone {
|
||||||
|
iqAnswerSetError(answer, query, 406)
|
||||||
|
} else {
|
||||||
|
iqAnswerSetError(answer, query, 500)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = session.SetPhoneNumber(query.Username)
|
||||||
|
if err != nil {
|
||||||
|
iqAnswerSetError(answer, query, 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// everything okay, the response should be empty with no payload/error at this point
|
||||||
|
gateway.SubscribeToTransport(component, iq.From)
|
||||||
|
} else {
|
||||||
|
iqAnswerSetError(answer, query, 406)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func iqAnswerSetError(answer *stanza.IQ, payload *extensions.QueryRegister, code int) {
|
||||||
|
answer.Type = stanza.IQTypeError
|
||||||
|
answer.Payload = *payload
|
||||||
|
switch code {
|
||||||
|
case 400:
|
||||||
|
answer.Error = &stanza.Err{
|
||||||
|
Code: code,
|
||||||
|
Type: stanza.ErrorTypeModify,
|
||||||
|
Reason: "bad-request",
|
||||||
|
}
|
||||||
|
case 405:
|
||||||
|
answer.Error = &stanza.Err{
|
||||||
|
Code: code,
|
||||||
|
Type: stanza.ErrorTypeCancel,
|
||||||
|
Reason: "not-allowed",
|
||||||
|
Text: "Logging out is dangerous. If you are sure you would be able to receive the authentication code again, issue the /logout command to the transport",
|
||||||
|
}
|
||||||
|
case 406:
|
||||||
|
answer.Error = &stanza.Err{
|
||||||
|
Code: code,
|
||||||
|
Type: stanza.ErrorTypeModify,
|
||||||
|
Reason: "not-acceptable",
|
||||||
|
Text: "Phone number already provided, chat with the transport for further instruction",
|
||||||
|
}
|
||||||
|
case 500:
|
||||||
|
answer.Error = &stanza.Err{
|
||||||
|
Code: code,
|
||||||
|
Type: stanza.ErrorTypeWait,
|
||||||
|
Reason: "internal-server-error",
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Error("Unknown error code, falling back with empty reason")
|
||||||
|
answer.Error = &stanza.Err{
|
||||||
|
Code: code,
|
||||||
|
Type: stanza.ErrorTypeCancel,
|
||||||
|
Reason: "undefined-condition",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func toToID(to string) (int64, bool) {
|
func toToID(to string) (int64, bool) {
|
||||||
toParts := strings.Split(to, "@")
|
toParts := strings.Split(to, "@")
|
||||||
if len(toParts) < 2 {
|
if len(toParts) < 2 {
|
||||||
|
@ -476,7 +733,7 @@ func makeVCardPayload(typ byte, id string, info telegram.VCardInfo, session *tel
|
||||||
vcard.Photo.Type.Text = "image/jpeg"
|
vcard.Photo.Type.Text = "image/jpeg"
|
||||||
vcard.Photo.Binval.Text = base64Photo
|
vcard.Photo.Binval.Text = base64Photo
|
||||||
}
|
}
|
||||||
vcard.Nickname.Text = info.Nickname
|
vcard.Nickname.Text = strings.Join(info.Nicknames, ",")
|
||||||
vcard.N.Given.Text = info.Given
|
vcard.N.Given.Text = info.Given
|
||||||
vcard.N.Family.Text = info.Family
|
vcard.N.Family.Text = info.Family
|
||||||
vcard.Tel.Number.Text = info.Tel
|
vcard.Tel.Number.Text = info.Tel
|
||||||
|
@ -507,13 +764,13 @@ func makeVCardPayload(typ byte, id string, info telegram.VCardInfo, session *tel
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if info.Nickname != "" {
|
for _, nickname := range info.Nicknames {
|
||||||
nodes = append(nodes, stanza.Node{
|
nodes = append(nodes, stanza.Node{
|
||||||
XMLName: xml.Name{Local: "nickname"},
|
XMLName: xml.Name{Local: "nickname"},
|
||||||
Nodes: []stanza.Node{
|
Nodes: []stanza.Node{
|
||||||
stanza.Node{
|
stanza.Node{
|
||||||
XMLName: xml.Name{Local: "text"},
|
XMLName: xml.Name{Local: "text"},
|
||||||
Content: info.Nickname,
|
Content: nickname,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, stanza.Node{
|
}, stanza.Node{
|
||||||
|
@ -521,7 +778,7 @@ func makeVCardPayload(typ byte, id string, info telegram.VCardInfo, session *tel
|
||||||
Nodes: []stanza.Node{
|
Nodes: []stanza.Node{
|
||||||
stanza.Node{
|
stanza.Node{
|
||||||
XMLName: xml.Name{Local: "uri"},
|
XMLName: xml.Name{Local: "uri"},
|
||||||
Content: "https://t.me/" + info.Nickname,
|
Content: "https://t.me/" + nickname,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue