mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 04:07:07 +00:00
Compare commits
165 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc5e8f1217 | ||
|
|
1c160a8a13 | ||
|
|
7d9f20d007 | ||
|
|
eee277e36e | ||
|
|
5650850be9 | ||
|
|
1d29aa4694 | ||
|
|
e073ded9e4 | ||
|
|
78a4305872 | ||
|
|
aaca93e66d | ||
|
|
e7c6318e48 | ||
|
|
140cf7fa4a | ||
|
|
326c94973a | ||
|
|
3fd49923a1 | ||
|
|
06964d832e | ||
|
|
5f4165ac13 | ||
|
|
68c3bece71 | ||
|
|
9378fa4991 | ||
|
|
4414c147d8 | ||
|
|
3cac57e0f3 | ||
|
|
85846346d1 | ||
|
|
0368b8cad8 | ||
|
|
7ebcdb0826 | ||
|
|
c5e41c7ce8 | ||
|
|
f4d5ebc3ad | ||
|
|
43399a1fbc | ||
|
|
421477ad8c | ||
|
|
85485bb147 | ||
|
|
dd0df7188a | ||
|
|
ba8f4c08cf | ||
|
|
bd5f41a76b | ||
|
|
e94a646e19 | ||
|
|
249c942fc2 | ||
|
|
9aec929e71 | ||
|
|
af07773b07 | ||
|
|
4eae44b9a2 | ||
|
|
a74e2bcb7d | ||
|
|
43f9603b88 | ||
|
|
a3f6d5f774 | ||
|
|
2459b14948 | ||
|
|
f15e44436b | ||
|
|
a36856b768 | ||
|
|
b499992148 | ||
|
|
144c5724ea | ||
|
|
3e772be7a6 | ||
|
|
908bd76aac | ||
|
|
67c38823f2 | ||
|
|
154b59de44 | ||
|
|
5dd60450c2 | ||
|
|
0b1cbda1cc | ||
|
|
9b5fee8826 | ||
|
|
dc6f99dc3c | ||
|
|
772246ee4b | ||
|
|
b0c5302c82 | ||
|
|
a0180eff75 | ||
|
|
e7d5a2a266 | ||
|
|
21dc5fa6c6 | ||
|
|
e3a5191905 | ||
|
|
eace19eef7 | ||
|
|
f56e6ac187 | ||
|
|
fd0d7411c2 | ||
|
|
20e6d2558e | ||
|
|
3a60a1cfaa | ||
|
|
ea004b7f7c | ||
|
|
c141c4ad2b | ||
|
|
599cf16cdb | ||
|
|
81fc3ea370 | ||
|
|
e37c428c67 | ||
|
|
b9b6ba14a4 | ||
|
|
b40ccf4a4d | ||
|
|
4532748c84 | ||
|
|
f2807779aa | ||
|
|
705cfc1d49 | ||
|
|
dcb802358b | ||
|
|
6bd8379114 | ||
|
|
576acba0d1 | ||
|
|
67b8ad57f0 | ||
|
|
282a6fc21b | ||
|
|
4588170d1e | ||
|
|
aa561c5be6 | ||
|
|
20994e2995 | ||
|
|
8ba7596ab5 | ||
|
|
64515e2c66 | ||
|
|
9377d7a155 | ||
|
|
c03ccfdfb7 | ||
|
|
608f675512 | ||
|
|
3c917c1698 | ||
|
|
8fc9edd7e7 | ||
|
|
a5f6c60035 | ||
|
|
131f6eba38 | ||
|
|
a595d9db0a | ||
|
|
ef831fc972 | ||
|
|
748366ad6a | ||
|
|
eadef987be | ||
|
|
563cb2d624 | ||
|
|
e954c73bd2 | ||
|
|
959dc061ff | ||
|
|
30b3fd1615 | ||
|
|
f8ad8c0204 | ||
|
|
739fc4110a | ||
|
|
fdd867cf7a | ||
|
|
edf2c08a5b | ||
|
|
00f1417cb2 | ||
|
|
79fc0ddbe5 | ||
|
|
945b9c063b | ||
|
|
7215d11d79 | ||
|
|
a5c90340ad | ||
|
|
9a84e9a8b6 | ||
|
|
8663a29e15 | ||
|
|
75f0532193 | ||
|
|
0a2c4e09d9 | ||
|
|
22b46c71ce | ||
|
|
281b9691af | ||
|
|
42ed16bf9e | ||
|
|
90807b2d9e | ||
|
|
7bd165a738 | ||
|
|
b1135b070b | ||
|
|
4a5b83dff5 | ||
|
|
6e32c62f8d | ||
|
|
b3d66993e5 | ||
|
|
18a17cb7a8 | ||
|
|
d66f87485d | ||
|
|
d3c6360e3c | ||
|
|
559d6443e5 | ||
|
|
b3b53b6145 | ||
|
|
68e3581724 | ||
|
|
326b436fc2 | ||
|
|
ddc9c8ff76 | ||
|
|
e9e65b6778 | ||
|
|
0c71036148 | ||
|
|
710a88cd7d | ||
|
|
e2edbe37ea | ||
|
|
501c1a7daa | ||
|
|
5628a15ac4 | ||
|
|
38ffdb9e47 | ||
|
|
76ac662366 | ||
|
|
3a43c6223f | ||
|
|
493c49cde5 | ||
|
|
ebc2f244d7 | ||
|
|
5698ce62c0 | ||
|
|
3f96664ef3 | ||
|
|
70841a2bea | ||
|
|
ae16fd2a0b | ||
|
|
b78779dad0 | ||
|
|
25e8c98c3e | ||
|
|
b3edc45526 | ||
|
|
86cbbcb8bd | ||
|
|
2f38af12a2 | ||
|
|
17afd3f8c7 | ||
|
|
5c238db1da | ||
|
|
976abf68ce | ||
|
|
d6b3ed3aaa | ||
|
|
8aa5187e2e | ||
|
|
022a12ce31 | ||
|
|
c6556eb6b8 | ||
|
|
ae8152ad87 | ||
|
|
5514bc9d16 | ||
|
|
cdbd960ff4 | ||
|
|
f6e62fe1fd | ||
|
|
7ddc3c4482 | ||
|
|
404e4f8d3e | ||
|
|
1b291e55e5 | ||
|
|
699d75552a | ||
|
|
4307f85a04 | ||
|
|
b67e29c0bb | ||
|
|
72b336a5f5 |
35 changed files with 6638 additions and 1162 deletions
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 /
|
||||||
19
Makefile
19
Makefile
|
|
@ -1,10 +1,25 @@
|
||||||
.PHONY: all test
|
.PHONY: all test
|
||||||
|
|
||||||
|
COMMIT := $(shell git rev-parse --short HEAD)
|
||||||
|
TD_COMMIT := "5bbfc1cf5dab94f82e02f3430ded7241d4653551"
|
||||||
|
VERSION := "v1.12.8"
|
||||||
|
MAKEOPTS := "-j4"
|
||||||
|
|
||||||
all:
|
all:
|
||||||
go build -o telegabber
|
mkdir -p release
|
||||||
|
go build -ldflags "-X main.commit=${COMMIT}" -o release/telegabber
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -v ./config ./ ./telegram ./xmpp/gateway ./persistence ./telegram/formatter
|
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 .
|
||||||
|
|
|
||||||
32
README.md
32
README.md
|
|
@ -75,6 +75,7 @@ It is good idea to obtain Telegram API ID from [**https://my.telegram.org**](htt
|
||||||
* `--profiling-port=xxxx`: start the pprof server on port `xxxx`. Access is limited to localhost.
|
* `--profiling-port=xxxx`: start the pprof server on port `xxxx`. Access is limited to localhost.
|
||||||
* `--config=/bla/bla/config.yml`: set the config file path (default: `config.yml`).
|
* `--config=/bla/bla/config.yml`: set the config file path (default: `config.yml`).
|
||||||
* `--schema=/bla/bla/schema.json`: set the schema file path (default: `./config_schema.json`).
|
* `--schema=/bla/bla/schema.json`: set the schema file path (default: `./config_schema.json`).
|
||||||
|
* `--ids=/bla/bla/ids`: set the folder for ids database (default: `ids`).
|
||||||
|
|
||||||
### How to receive files from Telegram ###
|
### How to receive files from Telegram ###
|
||||||
|
|
||||||
|
|
@ -142,3 +143,34 @@ server {
|
||||||
```
|
```
|
||||||
|
|
||||||
Finally, update `:upload:` in your config.yml to match `server_name` in nginx config.
|
Finally, update `:upload:` in your config.yml to match `server_name` in nginx config.
|
||||||
|
|
||||||
|
### Carbons ###
|
||||||
|
|
||||||
|
Telegabber needs special privileges according to XEP-0356 to simulate message carbons from the users (to display messages they have sent earlier or via other clients). Example configuration for Prosody:
|
||||||
|
|
||||||
|
```
|
||||||
|
modules_enabled = {
|
||||||
|
[...]
|
||||||
|
|
||||||
|
"privilege";
|
||||||
|
}
|
||||||
|
|
||||||
|
[...]
|
||||||
|
|
||||||
|
Component "telegabber.yourdomain.tld"
|
||||||
|
component_secret = "yourpassword"
|
||||||
|
modules_enabled = {"privilege"}
|
||||||
|
|
||||||
|
[...]
|
||||||
|
|
||||||
|
VirtualHost "yourdomain.tld"
|
||||||
|
[...]
|
||||||
|
|
||||||
|
privileged_entities = {
|
||||||
|
[...]
|
||||||
|
|
||||||
|
["telegabber.yourdomain.tld"] = {
|
||||||
|
message = "outgoing";
|
||||||
|
},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
|
||||||
230
badger/ids.go
Normal file
230
badger/ids.go
Normal file
|
|
@ -0,0 +1,230 @@
|
||||||
|
package badger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
badger "github.com/dgraph-io/badger/v4"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IdsDB represents a Badger database
|
||||||
|
type IdsDB struct {
|
||||||
|
db *badger.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
// IdsDBOpen returns a new DB object
|
||||||
|
func IdsDBOpen(path string) IdsDB {
|
||||||
|
bdb, err := badger.Open(badger.DefaultOptions(path))
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to open ids database: %v, falling back to in-memory database", path)
|
||||||
|
bdb, err = badger.Open(badger.DefaultOptions("").WithInMemory(true))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Couldn't initialize the ids database")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return IdsDB{
|
||||||
|
db: bdb,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set stores an id pair
|
||||||
|
func (db *IdsDB) Set(tgAccount, xmppAccount string, tgChatId, tgMsgId int64, xmppId string) error {
|
||||||
|
bPrefix := toKeyPrefix(tgAccount, xmppAccount)
|
||||||
|
bTgId := toTgByteString(tgChatId, tgMsgId)
|
||||||
|
bXmppId := toXmppByteString(xmppId)
|
||||||
|
bTgKey := toByteKey(bPrefix, bTgId, "tg")
|
||||||
|
bXmppKey := toByteKey(bPrefix, bXmppId, "xmpp")
|
||||||
|
|
||||||
|
return db.db.Update(func(txn *badger.Txn) error {
|
||||||
|
if err := txn.Set(bTgKey, bXmppId); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return txn.Set(bXmppKey, bTgId)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *IdsDB) getByteValue(key []byte) ([]byte, error) {
|
||||||
|
var valCopy []byte
|
||||||
|
err := db.db.View(func(txn *badger.Txn) error {
|
||||||
|
item, err := txn.Get(key)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
valCopy, err = item.ValueCopy(nil)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return valCopy, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetByTgIds obtains an XMPP id by Telegram chat/message ids
|
||||||
|
func (db *IdsDB) GetByTgIds(tgAccount, xmppAccount string, tgChatId, tgMsgId int64) (string, error) {
|
||||||
|
val, err := db.getByteValue(toByteKey(
|
||||||
|
toKeyPrefix(tgAccount, xmppAccount),
|
||||||
|
toTgByteString(tgChatId, tgMsgId),
|
||||||
|
"tg",
|
||||||
|
))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(val), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetByXmppId obtains Telegram chat/message ids by an XMPP id
|
||||||
|
func (db *IdsDB) GetByXmppId(tgAccount, xmppAccount, xmppId string) (int64, int64, error) {
|
||||||
|
val, err := db.getByteValue(toByteKey(
|
||||||
|
toKeyPrefix(tgAccount, xmppAccount),
|
||||||
|
toXmppByteString(xmppId),
|
||||||
|
"xmpp",
|
||||||
|
))
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
return splitTgByteString(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func toKeyPrefix(tgAccount, xmppAccount string) []byte {
|
||||||
|
return []byte(fmt.Sprintf("%v/%v/", tgAccount, xmppAccount))
|
||||||
|
}
|
||||||
|
|
||||||
|
func toByteKey(prefix, suffix []byte, typ string) []byte {
|
||||||
|
key := make([]byte, 0, len(prefix)+len(suffix)+6)
|
||||||
|
key = append(key, prefix...)
|
||||||
|
key = append(key, []byte(typ)...)
|
||||||
|
key = append(key, []byte("/")...)
|
||||||
|
key = append(key, suffix...)
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
func toTgByteString(tgChatId, tgMsgId int64) []byte {
|
||||||
|
return []byte(fmt.Sprintf("%v/%v", tgChatId, tgMsgId))
|
||||||
|
}
|
||||||
|
|
||||||
|
func toXmppByteString(xmppId string) []byte {
|
||||||
|
return []byte(xmppId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitTgByteString(val []byte) (int64, int64, error) {
|
||||||
|
parts := bytes.Split(val, []byte("/"))
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return 0, 0, errors.New("Couldn't parse tg id pair")
|
||||||
|
}
|
||||||
|
tgChatId, err := strconv.ParseInt(string(parts[0]), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
tgMsgId, err := strconv.ParseInt(string(parts[1]), 10, 64)
|
||||||
|
return tgChatId, tgMsgId, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceIdPair replaces an old entry by XMPP ID with both new XMPP and Tg ID
|
||||||
|
func (db *IdsDB) ReplaceIdPair(tgAccount, xmppAccount, oldXmppId, newXmppId string, newMsgId int64) error {
|
||||||
|
// read old pair
|
||||||
|
chatId, oldMsgId, err := db.GetByXmppId(tgAccount, xmppAccount, oldXmppId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
bPrefix := toKeyPrefix(tgAccount, xmppAccount)
|
||||||
|
|
||||||
|
bOldTgId := toTgByteString(chatId, oldMsgId)
|
||||||
|
bOldXmppId := toXmppByteString(oldXmppId)
|
||||||
|
bOldTgKey := toByteKey(bPrefix, bOldTgId, "tg")
|
||||||
|
bOldXmppKey := toByteKey(bPrefix, bOldXmppId, "xmpp")
|
||||||
|
|
||||||
|
bTgId := toTgByteString(chatId, newMsgId)
|
||||||
|
bXmppId := toXmppByteString(newXmppId)
|
||||||
|
bTgKey := toByteKey(bPrefix, bTgId, "tg")
|
||||||
|
bXmppKey := toByteKey(bPrefix, bXmppId, "xmpp")
|
||||||
|
|
||||||
|
return db.db.Update(func(txn *badger.Txn) error {
|
||||||
|
// save new pair
|
||||||
|
if err := txn.Set(bTgKey, bXmppId); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := txn.Set(bXmppKey, bTgId); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// delete old pair
|
||||||
|
if err := txn.Delete(bOldTgKey); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return txn.Delete(bOldXmppKey)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceXmppId replaces an old XMPP ID with new XMPP ID and keeps Tg ID intact
|
||||||
|
func (db *IdsDB) ReplaceXmppId(tgAccount, xmppAccount, oldXmppId, newXmppId string) error {
|
||||||
|
// read old Tg IDs
|
||||||
|
chatId, msgId, err := db.GetByXmppId(tgAccount, xmppAccount, oldXmppId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
bPrefix := toKeyPrefix(tgAccount, xmppAccount)
|
||||||
|
|
||||||
|
bOldXmppId := toXmppByteString(oldXmppId)
|
||||||
|
bOldXmppKey := toByteKey(bPrefix, bOldXmppId, "xmpp")
|
||||||
|
|
||||||
|
bTgId := toTgByteString(chatId, msgId)
|
||||||
|
bXmppId := toXmppByteString(newXmppId)
|
||||||
|
bTgKey := toByteKey(bPrefix, bTgId, "tg")
|
||||||
|
bXmppKey := toByteKey(bPrefix, bXmppId, "xmpp")
|
||||||
|
|
||||||
|
return db.db.Update(func(txn *badger.Txn) error {
|
||||||
|
// save new pair
|
||||||
|
if err := txn.Set(bTgKey, bXmppId); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := txn.Set(bXmppKey, bTgId); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// delete old xmpp->tg entry
|
||||||
|
return txn.Delete(bOldXmppKey)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceTgId replaces an old Tg ID with new Tg ID and keeps Tg chat ID and XMPP ID intact
|
||||||
|
func (db *IdsDB) ReplaceTgId(tgAccount, xmppAccount string, chatId, oldMsgId, newMsgId int64) error {
|
||||||
|
// read old XMPP ID
|
||||||
|
xmppId, err := db.GetByTgIds(tgAccount, xmppAccount, chatId, oldMsgId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
bPrefix := toKeyPrefix(tgAccount, xmppAccount)
|
||||||
|
|
||||||
|
bOldTgId := toTgByteString(chatId, oldMsgId)
|
||||||
|
bOldTgKey := toByteKey(bPrefix, bOldTgId, "tg")
|
||||||
|
|
||||||
|
bTgId := toTgByteString(chatId, newMsgId)
|
||||||
|
bXmppId := toXmppByteString(xmppId)
|
||||||
|
bTgKey := toByteKey(bPrefix, bTgId, "tg")
|
||||||
|
bXmppKey := toByteKey(bPrefix, bXmppId, "xmpp")
|
||||||
|
|
||||||
|
return db.db.Update(func(txn *badger.Txn) error {
|
||||||
|
// save new pair
|
||||||
|
if err := txn.Set(bTgKey, bXmppId); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := txn.Set(bXmppKey, bTgId); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// delete old tg->xmpp entry
|
||||||
|
return txn.Delete(bOldTgKey)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gc compacts the value log
|
||||||
|
func (db *IdsDB) Gc() {
|
||||||
|
db.db.RunValueLogGC(0.7)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes a DB
|
||||||
|
func (db *IdsDB) Close() {
|
||||||
|
db.db.Close()
|
||||||
|
}
|
||||||
72
badger/ids_test.go
Normal file
72
badger/ids_test.go
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
package badger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToKeyPrefix(t *testing.T) {
|
||||||
|
if !reflect.DeepEqual(toKeyPrefix("+123456789", "test@example.com"), []byte("+123456789/test@example.com/")) {
|
||||||
|
t.Error("Wrong prefix")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToByteKey(t *testing.T) {
|
||||||
|
if !reflect.DeepEqual(toByteKey([]byte("ababa/galamaga/"), []byte("123"), "ppp"), []byte("ababa/galamaga/ppp/123")) {
|
||||||
|
t.Error("Wrong key")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToTgByteString(t *testing.T) {
|
||||||
|
if !reflect.DeepEqual(toTgByteString(-2345, 6789), []byte("-2345/6789")) {
|
||||||
|
t.Error("Wrong tg string")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToXmppByteString(t *testing.T) {
|
||||||
|
if !reflect.DeepEqual(toXmppByteString("aboba"), []byte("aboba")) {
|
||||||
|
t.Error("Wrong xmpp string")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitTgByteStringUnparsable(t *testing.T) {
|
||||||
|
_, _, err := splitTgByteString([]byte("@#U*&$(@#"))
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Unparsable should not be parsed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err.Error() != "Couldn't parse tg id pair" {
|
||||||
|
t.Error("Wrong parse error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitTgByteManyParts(t *testing.T) {
|
||||||
|
_, _, err := splitTgByteString([]byte("a/b/c/d"))
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Should not parse many parts")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err.Error() != "Couldn't parse tg id pair" {
|
||||||
|
t.Error("Wrong parse error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitTgByteNonNumeric(t *testing.T) {
|
||||||
|
_, _, err := splitTgByteString([]byte("0/a"))
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Should not parse non-numeric msgid")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitTgByteSuccess(t *testing.T) {
|
||||||
|
chatId, msgId, err := splitTgByteString([]byte("-198282398/23798478"))
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Should be parsed well")
|
||||||
|
}
|
||||||
|
if chatId != -198282398 {
|
||||||
|
t.Error("Wrong chatId")
|
||||||
|
}
|
||||||
|
if msgId != 23798478 {
|
||||||
|
t.Error("Wrong msgId")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
:path: '/var/www/telegabber/content' # webserver workdir
|
:path: '/var/www/telegabber/content' # webserver workdir
|
||||||
:link: 'http://tlgrm.localhost/content' # webserver public address
|
:link: 'http://tlgrm.localhost/content' # webserver public address
|
||||||
:upload: 'https:///xmppfiles.localhost' # xmpp http upload address
|
:upload: 'https:///xmppfiles.localhost' # xmpp http upload address
|
||||||
|
:user: 'www-data' # owner of content files
|
||||||
|
:quota: '256MB' # maximum storage size
|
||||||
:tdlib_verbosity: 1
|
:tdlib_verbosity: 1
|
||||||
:tdlib:
|
:tdlib:
|
||||||
:datadir: './sessions/'
|
:datadir: './sessions/'
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,8 @@ type TelegramContentConfig struct {
|
||||||
Path string `yaml:":path"`
|
Path string `yaml:":path"`
|
||||||
Link string `yaml:":link"`
|
Link string `yaml:":link"`
|
||||||
Upload string `yaml:":upload"`
|
Upload string `yaml:":upload"`
|
||||||
|
User string `yaml:":user"`
|
||||||
|
Quota string `yaml:":quota"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TelegramTdlibConfig is for :tdlib: subtree
|
// TelegramTdlibConfig is for :tdlib: subtree
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,12 @@
|
||||||
},
|
},
|
||||||
":upload": {
|
":upload": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
},
|
||||||
|
":user": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
":quota": {
|
||||||
|
"type": "string"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
30
go.mod
30
go.mod
|
|
@ -1,10 +1,11 @@
|
||||||
module dev.narayana.im/narayana/telegabber
|
module dev.narayana.im/narayana/telegabber
|
||||||
|
|
||||||
go 1.13
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Arman92/go-tdlib v0.0.0-20191002071913-526f4e1d15f7
|
github.com/dgraph-io/badger/v4 v4.1.0
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/google/uuid v1.1.1
|
||||||
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/santhosh-tekuri/jsonschema v1.2.4
|
github.com/santhosh-tekuri/jsonschema v1.2.4
|
||||||
github.com/sirupsen/logrus v1.4.2
|
github.com/sirupsen/logrus v1.4.2
|
||||||
github.com/soheilhy/args v0.0.0-20150720134047-6bcf4c78e87e
|
github.com/soheilhy/args v0.0.0-20150720134047-6bcf4c78e87e
|
||||||
|
|
@ -13,4 +14,25 @@ require (
|
||||||
gosrc.io/xmpp v0.5.2-0.20211214110136-5f99e1cd06e1
|
gosrc.io/xmpp v0.5.2-0.20211214110136-5f99e1cd06e1
|
||||||
)
|
)
|
||||||
|
|
||||||
replace gosrc.io/xmpp => dev.narayana.im/narayana/go-xmpp v0.0.0-20211218155535-e55463fc9829
|
require (
|
||||||
|
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||||
|
github.com/dgraph-io/ristretto v0.1.1 // indirect
|
||||||
|
github.com/dustin/go-humanize v1.0.0 // indirect
|
||||||
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
|
||||||
|
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
|
||||||
|
github.com/golang/protobuf v1.3.2 // indirect
|
||||||
|
github.com/golang/snappy v0.0.3 // indirect
|
||||||
|
github.com/google/flatbuffers v1.12.1 // indirect
|
||||||
|
github.com/klauspost/compress v1.12.3 // indirect
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||||
|
go.opencensus.io v0.22.5 // indirect
|
||||||
|
golang.org/x/net v0.7.0 // indirect
|
||||||
|
golang.org/x/sys v0.5.0 // indirect
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
|
nhooyr.io/websocket v1.6.5 // indirect
|
||||||
|
)
|
||||||
|
|
||||||
|
replace gosrc.io/xmpp => dev.narayana.im/narayana/go-xmpp v0.0.0-20240512132113-6725c3862314
|
||||||
|
|
||||||
|
replace github.com/zelenin/go-tdlib => dev.narayana.im/narayana/go-tdlib v0.0.0-20240124222245-b4c12addb061
|
||||||
|
|
|
||||||
136
go.sum
136
go.sum
|
|
@ -1,32 +1,39 @@
|
||||||
dev.narayana.im/narayana/go-xmpp v0.0.0-20211218155535-e55463fc9829 h1:qe81G6+t1V1ySRMa7lSu5CayN5aP5GEiHXL2DYwHzuA=
|
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
dev.narayana.im/narayana/go-xmpp v0.0.0-20211218155535-e55463fc9829/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
dev.narayana.im/narayana/go-tdlib v0.0.0-20230730021136-47da33180615 h1:RRUZJSro+k8FkazNx7QEYLVoO4wZtchvsd0Y2RBWjeU=
|
||||||
github.com/Arman92/go-tdlib v0.0.0-20191002071913-526f4e1d15f7 h1:GbV1Lv3lVHsSeKAqPTBem72OCsGjXntW4jfJdXciE+w=
|
dev.narayana.im/narayana/go-tdlib v0.0.0-20230730021136-47da33180615/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
||||||
github.com/Arman92/go-tdlib v0.0.0-20191002071913-526f4e1d15f7/go.mod h1:ZzkRfuaFj8etIYMj/ECtXtgfz72RE6U+dos27b3XIwk=
|
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/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
||||||
|
dev.narayana.im/narayana/go-xmpp v0.0.0-20240131013505-18c46e6c59fd h1:+UW+E7JjI88aH4beDn1cw6D8rs1I061hN91HU4Y4pT8=
|
||||||
|
dev.narayana.im/narayana/go-xmpp v0.0.0-20240131013505-18c46e6c59fd/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
||||||
|
dev.narayana.im/narayana/go-xmpp v0.0.0-20240512132113-6725c3862314 h1:29/NjOGOUDceO73Hk4Nj4uVa1je8MULJlsDSvKxSN/k=
|
||||||
|
dev.narayana.im/narayana/go-xmpp v0.0.0-20240512132113-6725c3862314/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
||||||
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/agnivade/wasmbrowsertest v0.3.1/go.mod h1:zQt6ZTdl338xxRaMW395qccVE2eQm0SjC/SDz0mPWQI=
|
github.com/agnivade/wasmbrowsertest v0.3.1/go.mod h1:zQt6ZTdl338xxRaMW395qccVE2eQm0SjC/SDz0mPWQI=
|
||||||
github.com/bodqhrohro/go-tdlib v0.1.1 h1:lmHognymABxP3cmHkfAGhGnWaJaZ3htpJ7RSbZacin4=
|
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/bodqhrohro/go-tdlib v0.1.2-0.20191121200156-e826071d3317 h1:+mv4FwWXl8hTa7PrhekwVzPknH+rHqB60jIPBi2XqI8=
|
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||||
github.com/bodqhrohro/go-tdlib v0.1.2-0.20191121200156-e826071d3317/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/bodqhrohro/go-tdlib v0.1.2-0.20191121233100-48d2382034fb h1:y5PnjdAnNVS0q8xuwjm3TxBfLriJmykQdoGiyYZB3s0=
|
|
||||||
github.com/bodqhrohro/go-tdlib v0.1.2-0.20191121233100-48d2382034fb/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
|
||||||
github.com/bodqhrohro/go-tdlib v0.4.4-0.20211229000346-ee6018be8ec0 h1:9ysLk2hG2q0NeNdX6StzS+4fTAG2FeZJYKKegCuB4q4=
|
|
||||||
github.com/bodqhrohro/go-tdlib v0.4.4-0.20211229000346-ee6018be8ec0/go.mod h1:sOdXFpJ3zn6RHRc8aNVkJYALHpoplwBgMwIbRCYABIg=
|
|
||||||
github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c h1:LzcQyE+Gs+0kAbpnPAUD68FvUCieKZip44URAmH70PI=
|
|
||||||
github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c/go.mod h1:fWixaMaFvx8cxXcJVJ5kU9csMeD/JN8on7ybassU8rY=
|
|
||||||
github.com/bodqhrohro/go-xmpp v0.2.1-0.20191105232737-9abd5be0aa1b h1:9BLd/SNO4JJZLRl1Qb1v9mNivIlHuwHDe2c8hQvBxFA=
|
|
||||||
github.com/bodqhrohro/go-xmpp v0.2.1-0.20191105232737-9abd5be0aa1b/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
|
||||||
github.com/bodqhrohro/go-xmpp v0.2.1-0.20211205194122-f8c4ecb59d8b h1:rTK55SNCBmssyRgNAweVwVVfuoRstI8RbL+8Ys/RzxE=
|
|
||||||
github.com/bodqhrohro/go-xmpp v0.2.1-0.20211205194122-f8c4ecb59d8b/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
|
||||||
github.com/bodqhrohro/go-xmpp v0.2.1-0.20211218153313-a8aadd78b65b h1:VDi8z3PzEDhQzazRRuv1fkv662DT3Mm/TY/Lni2Sgrc=
|
|
||||||
github.com/bodqhrohro/go-xmpp v0.2.1-0.20211218153313-a8aadd78b65b/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
|
||||||
github.com/chromedp/cdproto v0.0.0-20190614062957-d6d2f92b486d/go.mod h1:S8mB5wY3vV+vRIzf39xDXsw3XKYewW9X6rW2aEmkrSw=
|
github.com/chromedp/cdproto v0.0.0-20190614062957-d6d2f92b486d/go.mod h1:S8mB5wY3vV+vRIzf39xDXsw3XKYewW9X6rW2aEmkrSw=
|
||||||
github.com/chromedp/cdproto v0.0.0-20190621002710-8cbd498dd7a0/go.mod h1:S8mB5wY3vV+vRIzf39xDXsw3XKYewW9X6rW2aEmkrSw=
|
github.com/chromedp/cdproto v0.0.0-20190621002710-8cbd498dd7a0/go.mod h1:S8mB5wY3vV+vRIzf39xDXsw3XKYewW9X6rW2aEmkrSw=
|
||||||
github.com/chromedp/cdproto v0.0.0-20190812224334-39ef923dcb8d/go.mod h1:0YChpVzuLJC5CPr+x3xkHN6Z8KOSXjNbL7qV8Wc4GW0=
|
github.com/chromedp/cdproto v0.0.0-20190812224334-39ef923dcb8d/go.mod h1:0YChpVzuLJC5CPr+x3xkHN6Z8KOSXjNbL7qV8Wc4GW0=
|
||||||
github.com/chromedp/cdproto v0.0.0-20190926234355-1b4886c6fad6/go.mod h1:0YChpVzuLJC5CPr+x3xkHN6Z8KOSXjNbL7qV8Wc4GW0=
|
github.com/chromedp/cdproto v0.0.0-20190926234355-1b4886c6fad6/go.mod h1:0YChpVzuLJC5CPr+x3xkHN6Z8KOSXjNbL7qV8Wc4GW0=
|
||||||
github.com/chromedp/chromedp v0.3.1-0.20190619195644-fd957a4d2901/go.mod h1:mJdvfrVn594N9tfiPecUidF6W5jPRKHymqHfzbobPsM=
|
github.com/chromedp/chromedp v0.3.1-0.20190619195644-fd957a4d2901/go.mod h1:mJdvfrVn594N9tfiPecUidF6W5jPRKHymqHfzbobPsM=
|
||||||
github.com/chromedp/chromedp v0.4.0/go.mod h1:DC3QUn4mJ24dwjcaGQLoZrhm4X/uPHZ6spDbS2uFhm4=
|
github.com/chromedp/chromedp v0.4.0/go.mod h1:DC3QUn4mJ24dwjcaGQLoZrhm4X/uPHZ6spDbS2uFhm4=
|
||||||
|
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgraph-io/badger/v4 v4.1.0 h1:E38jc0f+RATYrycSUf9LMv/t47XAy+3CApyYSq4APOQ=
|
||||||
|
github.com/dgraph-io/badger/v4 v4.1.0/go.mod h1:P50u28d39ibBRmIJuQC/NSdBOg46HnHw7al2SW5QRHg=
|
||||||
|
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
|
||||||
|
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
|
||||||
|
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
|
||||||
|
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
|
||||||
|
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
|
||||||
|
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
||||||
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
|
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
|
||||||
github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||||
|
|
@ -36,25 +43,43 @@ github.com/go-interpreter/wagon v0.6.0/go.mod h1:5+b/MBYkclRZngKF5s6qrgWxSLgE9F5
|
||||||
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
|
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
|
||||||
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||||
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
|
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
|
||||||
github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac h1:5FQGW4yHSkbwm+4i/8ef7FvkIFt4NOM4HexSbvPduRo=
|
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||||
github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||||
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
|
||||||
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
|
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I=
|
||||||
|
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||||
|
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
|
||||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
|
||||||
|
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
|
github.com/google/flatbuffers v1.12.1 h1:MVlul7pQNoDzWRLTw5imwYsl+usrS1TXG2H4jg6ImGw=
|
||||||
|
github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
|
||||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||||
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
|
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
|
||||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||||
github.com/google/pprof v0.0.0-20190908185732-236ed259b199/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
github.com/google/pprof v0.0.0-20190908185732-236ed259b199/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||||
|
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||||
|
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||||
|
github.com/klauspost/compress v1.12.3 h1:G5AfA94pHPysR56qqrkO2pxEexdDzrpFJ6yt/VqWxVU=
|
||||||
|
github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
|
||||||
github.com/knq/sysutil v0.0.0-20181215143952-f05b59f0f307/go.mod h1:BjPj+aVjl9FW/cCGiF3nGh5v+9Gd3VCgBQbod/GlMaQ=
|
github.com/knq/sysutil v0.0.0-20181215143952-f05b59f0f307/go.mod h1:BjPj+aVjl9FW/cCGiF3nGh5v+9Gd3VCgBQbod/GlMaQ=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
|
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||||
|
|
@ -70,8 +95,9 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
|
||||||
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis=
|
github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis=
|
||||||
|
|
@ -85,48 +111,95 @@ github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A=
|
github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A=
|
||||||
github.com/zelenin/go-tdlib v0.1.0 h1:Qq+FGE0/EWdsRB6m26ULDndu2DtW558aFXNzi0Y/FqQ=
|
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
github.com/zelenin/go-tdlib v0.1.0/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
github.com/zelenin/go-tdlib v0.5.2 h1:inEATEM0Pz6/HBI3wTlhd+brDHpmoXGgwdSb8/V6GiA=
|
github.com/zelenin/go-tdlib v0.5.2 h1:inEATEM0Pz6/HBI3wTlhd+brDHpmoXGgwdSb8/V6GiA=
|
||||||
github.com/zelenin/go-tdlib v0.5.2/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
github.com/zelenin/go-tdlib v0.5.2/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU=
|
||||||
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16/go.mod h1:iKV5yK9t+J5nG9O3uF6KYdPEz3dyfMyB15MN1rbQ8Qw=
|
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16/go.mod h1:iKV5yK9t+J5nG9O3uF6KYdPEz3dyfMyB15MN1rbQ8Qw=
|
||||||
|
go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0=
|
||||||
|
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
|
||||||
|
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
|
||||||
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
||||||
|
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
|
||||||
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
|
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
|
||||||
golang.org/x/crypto v0.0.0-20180426230345-b49d69b5da94/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
golang.org/x/crypto v0.0.0-20180426230345-b49d69b5da94/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
|
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||||
|
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||||
|
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||||
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||||
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
|
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
|
||||||
|
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190306220234-b354f8bf4d9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190306220234-b354f8bf4d9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20190927073244-c990c680b611 h1:q9u40nxWT5zRClI/uU9dHCiYGottAg6Nzz4YUQyHxdA=
|
|
||||||
golang.org/x/sys v0.0.0-20190927073244-c990c680b611/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190927073244-c990c680b611/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
|
||||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||||
|
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||||
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
|
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||||
|
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||||
|
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||||
|
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
|
||||||
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
|
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
|
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
|
|
@ -134,12 +207,9 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gosrc.io/xmpp v0.1.3 h1:VYP1bA35irlQ1ZAJqNhJOz8NSsSTkzQRhREfmuG1H80=
|
|
||||||
gosrc.io/xmpp v0.1.3/go.mod h1:fWixaMaFvx8cxXcJVJ5kU9csMeD/JN8on7ybassU8rY=
|
|
||||||
gosrc.io/xmpp v0.5.2-0.20211214110136-5f99e1cd06e1 h1:E3uJqX6ImJL9AFdjGbiW04jq8IQ+NcOK+JSiWq2TbRw=
|
|
||||||
gosrc.io/xmpp v0.5.2-0.20211214110136-5f99e1cd06e1/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
|
|
||||||
gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||||
gotest.tools/gotestsum v0.3.5/go.mod h1:Mnf3e5FUzXbkCfynWBGOwLssY7gTQgCHObK9tMpAriY=
|
gotest.tools/gotestsum v0.3.5/go.mod h1:Mnf3e5FUzXbkCfynWBGOwLssY7gTQgCHObK9tMpAriY=
|
||||||
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
mvdan.cc/sh v2.6.4+incompatible/go.mod h1:IeeQbZq+x2SUGBensq/jge5lLQbS3XT2ktyp3wrt4x8=
|
mvdan.cc/sh v2.6.4+incompatible/go.mod h1:IeeQbZq+x2SUGBensq/jge5lLQbS3XT2ktyp3wrt4x8=
|
||||||
nhooyr.io/websocket v1.6.5 h1:8TzpkldRfefda5JST+CnOH135bzVPz5uzfn/AF+gVKg=
|
nhooyr.io/websocket v1.6.5 h1:8TzpkldRfefda5JST+CnOH135bzVPz5uzfn/AF+gVKg=
|
||||||
nhooyr.io/websocket v1.6.5/go.mod h1:F259lAzPRAH0htX2y3ehpJe09ih1aSHN7udWki1defY=
|
nhooyr.io/websocket v1.6.5/go.mod h1:F259lAzPRAH0htX2y3ehpJe09ih1aSHN7udWki1defY=
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -34,29 +35,57 @@ type SessionsMap struct {
|
||||||
|
|
||||||
// Session is a key-values subtree
|
// Session is a key-values subtree
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Login string `yaml:":login"`
|
Login string `yaml:":login"`
|
||||||
Timezone string `yaml:":timezone"`
|
Timezone string `yaml:":timezone"`
|
||||||
KeepOnline bool `yaml:":keeponline"`
|
KeepOnline bool `yaml:":keeponline"`
|
||||||
RawMessages bool `yaml:":rawmessages"`
|
RawMessages bool `yaml:":rawmessages"`
|
||||||
AsciiArrows bool `yaml:":asciiarrows"`
|
AsciiArrows bool `yaml:":asciiarrows"`
|
||||||
|
OOBMode bool `yaml:":oobmode"`
|
||||||
|
Carbons bool `yaml:":carbons"`
|
||||||
|
HideIds bool `yaml:":hideids"`
|
||||||
|
Receipts bool `yaml:":receipts"`
|
||||||
|
NativeEdits bool `yaml:":nativeedits"`
|
||||||
|
IgnoredChats []int64 `yaml:":ignoredchats"`
|
||||||
|
ignoredChatsMap map[int64]bool `yaml:"-"`
|
||||||
|
|
||||||
|
IgnoreGroupDeletions bool `yaml:":ignoregroupdeletions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var configKeys = []string{
|
const (
|
||||||
|
PropertyTypeUnknown byte = iota
|
||||||
|
PropertyTypeString
|
||||||
|
PropertyTypeBool
|
||||||
|
)
|
||||||
|
|
||||||
|
var ConfigKeys = []string{
|
||||||
"timezone",
|
"timezone",
|
||||||
"keeponline",
|
"keeponline",
|
||||||
"rawmessages",
|
"rawmessages",
|
||||||
"asciiarrows",
|
"asciiarrows",
|
||||||
|
"oobmode",
|
||||||
|
"carbons",
|
||||||
|
"hideids",
|
||||||
|
"receipts",
|
||||||
|
"nativeedits",
|
||||||
|
"ignoregroupdeletions",
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -98,6 +127,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,
|
||||||
|
|
@ -109,6 +148,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
|
||||||
|
|
@ -118,6 +164,18 @@ func (s *Session) Get(key string) (string, error) {
|
||||||
return fromBool(s.RawMessages), nil
|
return fromBool(s.RawMessages), nil
|
||||||
case "asciiarrows":
|
case "asciiarrows":
|
||||||
return fromBool(s.AsciiArrows), nil
|
return fromBool(s.AsciiArrows), nil
|
||||||
|
case "oobmode":
|
||||||
|
return fromBool(s.OOBMode), nil
|
||||||
|
case "carbons":
|
||||||
|
return fromBool(s.Carbons), nil
|
||||||
|
case "hideids":
|
||||||
|
return fromBool(s.HideIds), nil
|
||||||
|
case "receipts":
|
||||||
|
return fromBool(s.Receipts), nil
|
||||||
|
case "nativeedits":
|
||||||
|
return fromBool(s.NativeEdits), nil
|
||||||
|
case "ignoregroupdeletions":
|
||||||
|
return fromBool(s.IgnoreGroupDeletions), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("Unknown session property")
|
return "", errors.New("Unknown session property")
|
||||||
|
|
@ -125,9 +183,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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,6 +197,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
|
||||||
|
|
@ -161,11 +225,65 @@ func (s *Session) Set(key string, value string) (string, error) {
|
||||||
}
|
}
|
||||||
s.AsciiArrows = b
|
s.AsciiArrows = b
|
||||||
return value, nil
|
return value, nil
|
||||||
|
case "oobmode":
|
||||||
|
b, err := toBool(value)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
s.OOBMode = b
|
||||||
|
return value, nil
|
||||||
|
case "carbons":
|
||||||
|
b, err := toBool(value)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
s.Carbons = b
|
||||||
|
return value, nil
|
||||||
|
case "hideids":
|
||||||
|
b, err := toBool(value)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
s.HideIds = b
|
||||||
|
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
|
||||||
|
case "ignoregroupdeletions":
|
||||||
|
b, err := toBool(value)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
s.IgnoreGroupDeletions = b
|
||||||
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("Unknown session property")
|
return "", errors.New("Unknown session property")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PropertyType determines the property type
|
||||||
|
func PropertyType(key string) byte {
|
||||||
|
switch key {
|
||||||
|
case "timezone":
|
||||||
|
return PropertyTypeString
|
||||||
|
case "keeponline", "rawmessages", "asciiarrows", "oobmode", "carbons", "hideids",
|
||||||
|
"receipts", "nativeedits", "ignoregroupdeletions":
|
||||||
|
return PropertyTypeBool
|
||||||
|
}
|
||||||
|
return PropertyTypeUnknown
|
||||||
|
}
|
||||||
|
|
||||||
// TimezoneToLocation tries to convert config timezone to location
|
// TimezoneToLocation tries to convert config timezone to location
|
||||||
func (s *Session) TimezoneToLocation() *time.Location {
|
func (s *Session) TimezoneToLocation() *time.Location {
|
||||||
time, err := time.Parse("-07:00", s.Timezone)
|
time, err := time.Parse("-07:00", s.Timezone)
|
||||||
|
|
@ -177,6 +295,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"
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,8 @@ func TestSessionToMap(t *testing.T) {
|
||||||
session := Session{
|
session := Session{
|
||||||
Timezone: "klsf",
|
Timezone: "klsf",
|
||||||
RawMessages: true,
|
RawMessages: true,
|
||||||
|
OOBMode: true,
|
||||||
|
Receipts: true,
|
||||||
}
|
}
|
||||||
m := session.ToMap()
|
m := session.ToMap()
|
||||||
sample := map[string]string{
|
sample := map[string]string{
|
||||||
|
|
@ -54,6 +56,11 @@ func TestSessionToMap(t *testing.T) {
|
||||||
"keeponline": "false",
|
"keeponline": "false",
|
||||||
"rawmessages": "true",
|
"rawmessages": "true",
|
||||||
"asciiarrows": "false",
|
"asciiarrows": "false",
|
||||||
|
"oobmode": "true",
|
||||||
|
"carbons": "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)
|
||||||
|
|
@ -81,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,rw \
|
||||||
|
/bin/bash -c 'go mod tidy; go get -t'
|
||||||
|
|
||||||
|
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,9 +12,13 @@ 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.12.8"
|
||||||
|
var commit string
|
||||||
|
|
||||||
var sm *goxmpp.StreamManager
|
var sm *goxmpp.StreamManager
|
||||||
var component *goxmpp.Component
|
var component *goxmpp.Component
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -23,13 +27,25 @@ var cleanupDone chan struct{}
|
||||||
var sigintChannel chan os.Signal
|
var sigintChannel chan os.Signal
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if commit != "" {
|
||||||
|
version = fmt.Sprintf("%v-%v", version, commit)
|
||||||
|
}
|
||||||
|
|
||||||
var profilingPort = flag.Int("profiling-port", 0, "The port for pprof server")
|
var profilingPort = flag.Int("profiling-port", 0, "The port for pprof server")
|
||||||
// YAML config, compatible with the format of Zhabogram 2.0.0
|
// YAML config, compatible with the format of Zhabogram 2.0.0
|
||||||
var configPath = flag.String("config", "config.yml", "Config file path")
|
var configPath = flag.String("config", "config.yml", "Config file path")
|
||||||
// JSON schema (not for editing by a user)
|
// JSON schema (not for editing by a user)
|
||||||
var schemaPath = flag.String("schema", "./config_schema.json", "Schema file path")
|
var schemaPath = flag.String("schema", "./config_schema.json", "Schema file path")
|
||||||
|
// Folder for Badger DB of message ids
|
||||||
|
var idsPath = flag.String("ids", "ids", "Ids folder path")
|
||||||
|
var versionFlag = flag.Bool("version", false, "Print the version and exit")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
if *versionFlag {
|
||||||
|
fmt.Printf("%v\n", version)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
if *profilingPort > 0 {
|
if *profilingPort > 0 {
|
||||||
go func() {
|
go func() {
|
||||||
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%v", *profilingPort), nil))
|
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%v", *profilingPort), nil))
|
||||||
|
|
@ -45,9 +61,14 @@ 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)
|
||||||
|
|
||||||
sm, component, err = xmpp.NewComponent(config.XMPP, config.Telegram)
|
log.Infof("Starting telegabber version %v", version)
|
||||||
|
|
||||||
|
sm, component, err = xmpp.NewComponent(config.XMPP, config.Telegram, *idsPath, version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -72,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")
|
||||||
|
}
|
||||||
|
}
|
||||||
53
telegram/cache/cache.go
vendored
53
telegram/cache/cache.go
vendored
|
|
@ -16,7 +16,8 @@ type Status struct {
|
||||||
// Cache allows operating the chats and users cache in
|
// Cache allows operating the chats and users cache in
|
||||||
// a thread-safe manner
|
// a thread-safe manner
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
chats map[int64]*client.Chat
|
ownChats map[int64]*client.Chat
|
||||||
|
auxChats map[int64]*client.Chat
|
||||||
users map[int64]*client.User
|
users map[int64]*client.User
|
||||||
statuses map[int64]*Status
|
statuses map[int64]*Status
|
||||||
chatsLock sync.Mutex
|
chatsLock sync.Mutex
|
||||||
|
|
@ -27,7 +28,8 @@ type Cache struct {
|
||||||
// NewCache initializes a cache
|
// NewCache initializes a cache
|
||||||
func NewCache() *Cache {
|
func NewCache() *Cache {
|
||||||
return &Cache{
|
return &Cache{
|
||||||
chats: map[int64]*client.Chat{},
|
ownChats: map[int64]*client.Chat{},
|
||||||
|
auxChats: map[int64]*client.Chat{},
|
||||||
users: map[int64]*client.User{},
|
users: map[int64]*client.User{},
|
||||||
statuses: map[int64]*Status{},
|
statuses: map[int64]*Status{},
|
||||||
}
|
}
|
||||||
|
|
@ -40,7 +42,23 @@ func (cache *Cache) ChatsKeys() []int64 {
|
||||||
defer cache.chatsLock.Unlock()
|
defer cache.chatsLock.Unlock()
|
||||||
|
|
||||||
var keys []int64
|
var keys []int64
|
||||||
for id := range cache.chats {
|
for id := range cache.ownChats {
|
||||||
|
keys = append(keys, id)
|
||||||
|
}
|
||||||
|
for id := range cache.auxChats {
|
||||||
|
keys = append(keys, id)
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// OwnChatsKeys grabs only own chat ids synchronously to avoid lockups
|
||||||
|
// while they are used
|
||||||
|
func (cache *Cache) OwnChatsKeys() []int64 {
|
||||||
|
cache.chatsLock.Lock()
|
||||||
|
defer cache.chatsLock.Unlock()
|
||||||
|
|
||||||
|
var keys []int64
|
||||||
|
for id := range cache.ownChats {
|
||||||
keys = append(keys, id)
|
keys = append(keys, id)
|
||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
|
|
@ -84,7 +102,10 @@ func (cache *Cache) GetChat(id int64) (*client.Chat, bool) {
|
||||||
cache.chatsLock.Lock()
|
cache.chatsLock.Lock()
|
||||||
defer cache.chatsLock.Unlock()
|
defer cache.chatsLock.Unlock()
|
||||||
|
|
||||||
chat, ok := cache.chats[id]
|
chat, ok := cache.ownChats[id]
|
||||||
|
if !ok {
|
||||||
|
chat, ok = cache.auxChats[id]
|
||||||
|
}
|
||||||
return chat, ok
|
return chat, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,11 +128,21 @@ func (cache *Cache) GetStatus(id int64) (*Status, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetChat stores a chat in the cache
|
// SetChat stores a chat in the cache
|
||||||
func (cache *Cache) SetChat(id int64, chat *client.Chat) {
|
func (cache *Cache) SetChat(id int64, chat *client.Chat, own bool) {
|
||||||
cache.chatsLock.Lock()
|
cache.chatsLock.Lock()
|
||||||
defer cache.chatsLock.Unlock()
|
defer cache.chatsLock.Unlock()
|
||||||
|
|
||||||
cache.chats[id] = chat
|
if own {
|
||||||
|
cache.ownChats[id] = chat
|
||||||
|
// move from aux to own, but not vice versa
|
||||||
|
// (own: true means that presences for the chat are needed
|
||||||
|
// for sure, false means just "not necessarily")
|
||||||
|
if _, ok := cache.auxChats[id]; ok {
|
||||||
|
delete(cache.auxChats, id)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cache.auxChats[id] = chat
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetUser stores a user in the cache
|
// SetUser stores a user in the cache
|
||||||
|
|
@ -133,3 +164,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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,40 +15,61 @@ 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
|
||||||
TimestampExpired int64
|
TimestampExpired int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HashedAvatar stores a SHA-1 hash and a Telegram file ID
|
||||||
|
type HashedAvatar struct {
|
||||||
|
Hash string
|
||||||
|
File int32
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewId stores message ids and timestamps of their additions so old ones can be truncated to save memory
|
||||||
|
type newId struct {
|
||||||
|
Id int64
|
||||||
|
Ts int64
|
||||||
|
lock sync.Mutex
|
||||||
|
ownLock sync.Mutex
|
||||||
|
locked bool
|
||||||
|
fired bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNewId() *newId {
|
||||||
|
return &newId{Ts: time.Now().Unix()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *newId) Lock() {
|
||||||
|
i.ownLock.Lock()
|
||||||
|
if i.fired {
|
||||||
|
i.ownLock.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
i.locked = true
|
||||||
|
i.ownLock.Unlock()
|
||||||
|
i.lock.Lock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *newId) Unlock() {
|
||||||
|
i.ownLock.Lock()
|
||||||
|
if i.locked {
|
||||||
|
i.lock.Unlock()
|
||||||
|
i.locked = false
|
||||||
|
i.fired = true
|
||||||
|
}
|
||||||
|
i.ownLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// Client stores the metadata for lazily invoked TDlib instance
|
// Client stores the metadata for lazily invoked TDlib instance
|
||||||
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
|
||||||
|
|
||||||
resource string
|
|
||||||
xmpp *xmpp.Component
|
xmpp *xmpp.Component
|
||||||
jid string
|
jid string
|
||||||
Session *persistence.Session
|
Session *persistence.Session
|
||||||
|
|
@ -57,26 +78,60 @@ type Client struct {
|
||||||
cache *cache.Cache
|
cache *cache.Cache
|
||||||
online bool
|
online bool
|
||||||
|
|
||||||
|
loginWizard *loginWizardMetadata
|
||||||
|
|
||||||
|
lastAuthorizationStateType string
|
||||||
|
|
||||||
|
outbox map[string]string
|
||||||
|
editOutbox map[string]string
|
||||||
|
|
||||||
DelayedStatuses map[int64]*DelayedStatus
|
DelayedStatuses map[int64]*DelayedStatus
|
||||||
DelayedStatusesLock sync.Mutex
|
DelayedStatusesLock sync.Mutex
|
||||||
|
|
||||||
locks clientLocks
|
lastMsgHashes map[int64]uint64
|
||||||
|
lastMsgIds map[int64]string
|
||||||
|
|
||||||
|
LastBotCmdString string
|
||||||
|
|
||||||
|
XmppClientFeatures map[string]*[]string
|
||||||
|
XmppClientFeaturesLock sync.Mutex
|
||||||
|
|
||||||
|
AvatarHashes map[int64]*HashedAvatar
|
||||||
|
AvatarHashesLock sync.Mutex
|
||||||
|
|
||||||
|
MessageIdChanges map[int64]map[int64]*newId
|
||||||
|
MessageIdChangesLock sync.Mutex
|
||||||
|
|
||||||
|
locks clientLocks
|
||||||
|
SendMessageLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientLocks struct {
|
type clientLocks struct {
|
||||||
authorizationReady sync.WaitGroup
|
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
|
||||||
|
|
||||||
|
loginWizardReadLock sync.Mutex
|
||||||
|
loginWizardWriteLock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
type loginWizardMetadata struct {
|
||||||
|
nextStage chan string
|
||||||
|
chanBusy bool
|
||||||
|
commandSent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,
|
||||||
|
|
@ -93,7 +148,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),
|
||||||
|
|
@ -116,22 +171,23 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component
|
||||||
IgnoreFileNames: false,
|
IgnoreFileNames: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
resource := conf.Tdlib.Client.DeviceModel
|
|
||||||
if resource == "" {
|
|
||||||
resource = "telegabber"
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
parameters: ¶meters,
|
parameters: ¶meters,
|
||||||
resource: resource,
|
xmpp: component,
|
||||||
xmpp: component,
|
jid: jid,
|
||||||
jid: jid,
|
Session: session,
|
||||||
Session: session,
|
resources: make(map[string]bool),
|
||||||
resources: make(map[string]bool),
|
content: &conf.Content,
|
||||||
content: &conf.Content,
|
cache: cache.NewCache(),
|
||||||
cache: cache.NewCache(),
|
outbox: make(map[string]string),
|
||||||
options: options,
|
editOutbox: make(map[string]string),
|
||||||
DelayedStatuses: make(map[int64]*DelayedStatus),
|
options: options,
|
||||||
|
DelayedStatuses: make(map[int64]*DelayedStatus),
|
||||||
|
lastMsgHashes: make(map[int64]uint64),
|
||||||
|
lastMsgIds: make(map[int64]string),
|
||||||
|
XmppClientFeatures: make(map[string]*[]string),
|
||||||
|
AvatarHashes: make(map[int64]*HashedAvatar),
|
||||||
|
MessageIdChanges: make(map[int64]map[int64]*newId),
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
1073
telegram/commands.go
1073
telegram/commands.go
File diff suppressed because it is too large
Load diff
|
|
@ -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,25 +13,25 @@ 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
|
||||||
Password chan string
|
Password chan string
|
||||||
|
FirstName chan string
|
||||||
|
LastName chan string
|
||||||
|
isClosed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.AuthorizationState) error {
|
func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.AuthorizationState) error {
|
||||||
|
if stateHandler.isClosed {
|
||||||
|
return errors.New("Channel is closed")
|
||||||
|
}
|
||||||
stateHandler.State <- state
|
stateHandler.State <- state
|
||||||
|
|
||||||
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:
|
||||||
|
|
@ -52,7 +52,11 @@ func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.Auth
|
||||||
return err
|
return err
|
||||||
|
|
||||||
case client.TypeAuthorizationStateWaitRegistration:
|
case client.TypeAuthorizationStateWaitRegistration:
|
||||||
return client.ErrNotSupportedAuthorizationState
|
_, err := c.RegisterUser(&client.RegisterUserRequest{
|
||||||
|
FirstName: <-stateHandler.FirstName,
|
||||||
|
LastName: <-stateHandler.LastName,
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
|
||||||
case client.TypeAuthorizationStateWaitPassword:
|
case client.TypeAuthorizationStateWaitPassword:
|
||||||
_, err := c.CheckAuthenticationPassword(&client.CheckAuthenticationPasswordRequest{
|
_, err := c.CheckAuthenticationPassword(&client.CheckAuthenticationPasswordRequest{
|
||||||
|
|
@ -64,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
|
||||||
|
|
@ -77,41 +81,55 @@ func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.Auth
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stateHandler *clientAuthorizer) Close() {
|
func (stateHandler *clientAuthorizer) Close() {
|
||||||
|
if stateHandler.isClosed {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stateHandler.isClosed = true
|
||||||
close(stateHandler.TdlibParameters)
|
close(stateHandler.TdlibParameters)
|
||||||
close(stateHandler.PhoneNumber)
|
close(stateHandler.PhoneNumber)
|
||||||
close(stateHandler.Code)
|
close(stateHandler.Code)
|
||||||
close(stateHandler.State)
|
close(stateHandler.State)
|
||||||
close(stateHandler.Password)
|
close(stateHandler.Password)
|
||||||
|
close(stateHandler.FirstName)
|
||||||
|
close(stateHandler.LastName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect starts TDlib connection
|
// Connect starts TDlib connection
|
||||||
func (c *Client) Connect(resource string) error {
|
func (c *Client) Connect(resource string) error {
|
||||||
|
log.Warn("Attempting to connect to Telegram network...")
|
||||||
|
|
||||||
// avoid conflict if another authorization is pending already
|
// avoid conflict if another authorization is pending already
|
||||||
c.locks.authorizationReady.Wait()
|
c.locks.authorizationReady.Lock()
|
||||||
|
|
||||||
if c.Online() {
|
if c.Online() {
|
||||||
c.roster(resource)
|
c.roster(resource)
|
||||||
|
c.locks.authorizationReady.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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),
|
||||||
Password: make(chan string, 1),
|
Password: make(chan string, 1),
|
||||||
|
FirstName: make(chan string, 1),
|
||||||
|
LastName: make(chan string, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
c.locks.authorizationReady.Add(1)
|
|
||||||
|
|
||||||
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 {
|
||||||
|
c.locks.authorizationReady.Unlock()
|
||||||
|
c.wizardStageOrPrompt("cancel", "")
|
||||||
return errors.Wrap(err, "Couldn't initialize a Telegram client instance")
|
return errors.Wrap(err, "Couldn't initialize a Telegram client instance")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -120,6 +138,8 @@ func (c *Client) Connect(resource string) error {
|
||||||
// stage 3: if a client is succesfully created, AuthorizationStateReady is already reached
|
// stage 3: if a client is succesfully created, AuthorizationStateReady is already reached
|
||||||
log.Warn("Authorization successful!")
|
log.Warn("Authorization successful!")
|
||||||
|
|
||||||
|
c.wizardStageOrPrompt("success", "")
|
||||||
|
|
||||||
c.me, err = c.client.GetMe()
|
c.me, err = c.client.GetMe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Could not retrieve me info")
|
log.Error("Could not retrieve me info")
|
||||||
|
|
@ -129,25 +149,68 @@ func (c *Client) Connect(resource string) error {
|
||||||
|
|
||||||
go c.updateHandler()
|
go c.updateHandler()
|
||||||
c.online = true
|
c.online = true
|
||||||
c.locks.authorizationReady.Done()
|
c.locks.authorizationReady.Unlock()
|
||||||
c.addResource(resource)
|
c.addResource(resource)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
_, err = c.client.GetChats(&client.GetChatsRequest{
|
chats, err := c.client.GetChats(&client.GetChatsRequest{
|
||||||
Limit: chatsLimit,
|
Limit: chatsLimit,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Could not retrieve chats: %v", err)
|
log.Errorf("Could not retrieve chats: %v", err)
|
||||||
|
} else {
|
||||||
|
log.Infof("Obtained ≈%v chats for initialization", chats.TotalCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|
@ -168,29 +231,28 @@ func (c *Client) Disconnect(resource string, quit bool) bool {
|
||||||
log.Warn("Disconnecting from Telegram network...")
|
log.Warn("Disconnecting from Telegram network...")
|
||||||
|
|
||||||
// we're offline (unsubscribe if logout)
|
// we're offline (unsubscribe if logout)
|
||||||
for _, id := range c.cache.ChatsKeys() {
|
for _, id := range c.cache.OwnChatsKeys() {
|
||||||
gateway.SendPresence(
|
args := gateway.SimplePresence(id, "unavailable")
|
||||||
c.xmpp,
|
c.sendPresence(args...)
|
||||||
c.jid,
|
|
||||||
gateway.SPFrom(strconv.FormatInt(id, 10)),
|
|
||||||
gateway.SPType("unavailable"),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := c.client.Close()
|
c.close()
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Couldn't close the Telegram instance: %v; %#v", err, c)
|
|
||||||
}
|
|
||||||
c.forceClose()
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -198,6 +260,8 @@ func (c *Client) interactor() {
|
||||||
log.Infof("Telegram authorization state: %#v", stateType)
|
log.Infof("Telegram authorization state: %#v", stateType)
|
||||||
log.Debugf("%#v", state)
|
log.Debugf("%#v", state)
|
||||||
|
|
||||||
|
c.lastAuthorizationStateType = stateType
|
||||||
|
|
||||||
switch stateType {
|
switch stateType {
|
||||||
// stage 0: set login
|
// stage 0: set login
|
||||||
case client.TypeAuthorizationStateWaitPhoneNumber:
|
case client.TypeAuthorizationStateWaitPhoneNumber:
|
||||||
|
|
@ -205,25 +269,58 @@ func (c *Client) interactor() {
|
||||||
if c.Session.Login != "" {
|
if c.Session.Login != "" {
|
||||||
c.authorizer.PhoneNumber <- c.Session.Login
|
c.authorizer.PhoneNumber <- c.Session.Login
|
||||||
} else {
|
} else {
|
||||||
gateway.SendMessage(c.jid, "", "Please, enter your Telegram login via /login 12345", c.xmpp)
|
c.wizardStageOrPrompt("login", "Please, enter your Telegram login via /login 12345, or use the Login Wizard via Ad-Hoc commands")
|
||||||
}
|
}
|
||||||
// stage 1: wait for auth code
|
// stage 1: wait for auth code
|
||||||
case client.TypeAuthorizationStateWaitCode:
|
case client.TypeAuthorizationStateWaitCode:
|
||||||
log.Warn("Waiting for authorization code...")
|
log.Warn("Waiting for authorization code...")
|
||||||
gateway.SendMessage(c.jid, "", "Please, enter authorization code via /code 12345", c.xmpp)
|
c.wizardStageOrPrompt("code", "Please, enter authorization code via /code 12345")
|
||||||
|
// stage 1b: wait for registration
|
||||||
|
case client.TypeAuthorizationStateWaitRegistration:
|
||||||
|
log.Warn("Waiting for full name...")
|
||||||
|
gateway.SendServiceMessage(c.jid, "This number is not registered yet! Please, enter your name via /setname John Doe", c.xmpp)
|
||||||
// stage 2: wait for 2fa
|
// stage 2: wait for 2fa
|
||||||
case client.TypeAuthorizationStateWaitPassword:
|
case client.TypeAuthorizationStateWaitPassword:
|
||||||
log.Warn("Waiting for 2FA password...")
|
log.Warn("Waiting for 2FA password...")
|
||||||
gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp)
|
c.wizardStageOrPrompt("password", "Please, enter 2FA passphrase via /password 12345")
|
||||||
}
|
}
|
||||||
|
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() {
|
||||||
|
c.locks.authorizerWriteLock.Lock()
|
||||||
|
if c.authorizer != nil && !c.authorizer.isClosed {
|
||||||
|
log.Debug("Closing authorizer")
|
||||||
|
c.authorizer.Close()
|
||||||
|
}
|
||||||
|
c.locks.authorizerWriteLock.Unlock()
|
||||||
|
|
||||||
|
if c.client != nil {
|
||||||
|
_, err := c.client.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Couldn't close the Telegram instance: %v; %#v", err, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.forceClose()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) cancelAuth() {
|
||||||
|
c.StopLoginWizard()
|
||||||
|
c.close()
|
||||||
|
c.Session.Login = ""
|
||||||
|
}
|
||||||
|
|
||||||
// Online checks if the updates listener is alive
|
// Online checks if the updates listener is alive
|
||||||
func (c *Client) Online() bool {
|
func (c *Client) Online() bool {
|
||||||
return c.online
|
return c.online
|
||||||
|
|
|
||||||
|
|
@ -2,30 +2,56 @@ package formatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"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("*")
|
||||||
var italicRunes = []rune("_")
|
var italicRunes = []rune("_")
|
||||||
var codeRunes = []rune("\n```\n")
|
var strikeRunesMarkdown = []rune("~~")
|
||||||
|
var strikeRunesXEP0393 = []rune("~")
|
||||||
|
var codeRunes = []rune("`")
|
||||||
|
var preRunesStart = []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 at 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]
|
||||||
|
|
@ -36,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]
|
||||||
|
|
@ -54,8 +80,8 @@ func SortEntities(entities []*client.TextEntity) []*client.TextEntity {
|
||||||
copy(sortedEntities, entities)
|
copy(sortedEntities, entities)
|
||||||
|
|
||||||
sort.Slice(sortedEntities, func(i int, j int) bool {
|
sort.Slice(sortedEntities, func(i int, j int) bool {
|
||||||
entity1 := entities[i]
|
entity1 := sortedEntities[i]
|
||||||
entity2 := entities[j]
|
entity2 := sortedEntities[j]
|
||||||
if entity1.Offset < entity2.Offset {
|
if entity1.Offset < entity2.Offset {
|
||||||
return true
|
return true
|
||||||
} else if entity1.Offset == entity2.Offset {
|
} else if entity1.Offset == entity2.Offset {
|
||||||
|
|
@ -66,74 +92,271 @@ func SortEntities(entities []*client.TextEntity) []*client.TextEntity {
|
||||||
return sortedEntities
|
return sortedEntities
|
||||||
}
|
}
|
||||||
|
|
||||||
func markupBraces(entity *client.TextEntity, lbrace, rbrace []rune) (*Insertion, *Insertion) {
|
// MergeAdjacentEntities merges entities of a same kind
|
||||||
return &Insertion{
|
func MergeAdjacentEntities(entities []*client.TextEntity) []*client.TextEntity {
|
||||||
Offset: entity.Offset,
|
mergedEntities := make([]*client.TextEntity, 0, len(entities))
|
||||||
Runes: lbrace,
|
excludedIndices := make(map[int]bool)
|
||||||
}, &Insertion{
|
|
||||||
Offset: entity.Offset + entity.Length,
|
for i, entity := range entities {
|
||||||
Runes: rbrace,
|
if excludedIndices[i] || entity.Type == nil {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typ := entity.Type.TextEntityTypeType()
|
||||||
|
start := entity.Offset
|
||||||
|
end := start + entity.Length
|
||||||
|
ei := make(map[int]bool)
|
||||||
|
|
||||||
|
// collect continuations
|
||||||
|
for j, entity2 := range entities[i+1:] {
|
||||||
|
if entity2.Type != nil && entity2.Type.TextEntityTypeType() == typ && entity2.Offset == end {
|
||||||
|
end += entity2.Length
|
||||||
|
ei[j+i+1] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for intersections with other entities
|
||||||
|
var isIntersecting bool
|
||||||
|
if len(ei) > 0 {
|
||||||
|
for _, entity2 := range entities {
|
||||||
|
entity2End := entity2.Offset + entity2.Length
|
||||||
|
if (entity2.Offset < start && entity2End > start && entity2End < end) ||
|
||||||
|
(entity2.Offset > start && entity2.Offset < end && entity2End > end) {
|
||||||
|
isIntersecting = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isIntersecting {
|
||||||
|
entity.Length = end - start
|
||||||
|
for j := range ei {
|
||||||
|
excludedIndices[j] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mergedEntities = append(mergedEntities, entity)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mergedEntities
|
||||||
}
|
}
|
||||||
|
|
||||||
// EntityToMarkdown generates the wrapping Markdown tags
|
// ClaspDirectives to the following span as required by XEP-0393
|
||||||
func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) {
|
func ClaspDirectives(doubledRunes []rune, entities []*client.TextEntity) []*client.TextEntity {
|
||||||
|
alignedEntities := make([]*client.TextEntity, len(entities))
|
||||||
|
copy(alignedEntities, entities)
|
||||||
|
|
||||||
|
for i, entity := range alignedEntities {
|
||||||
|
var dirty bool
|
||||||
|
endOffset := entity.Offset + entity.Length
|
||||||
|
|
||||||
|
if unicode.IsSpace(doubledRunes[entity.Offset]) {
|
||||||
|
for j, r := range doubledRunes[entity.Offset+1 : endOffset] {
|
||||||
|
if !unicode.IsSpace(r) {
|
||||||
|
dirty = true
|
||||||
|
entity.Offset += int32(j + 1)
|
||||||
|
entity.Length -= int32(j + 1)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if unicode.IsSpace(doubledRunes[endOffset-1]) {
|
||||||
|
for j := endOffset - 2; j >= entity.Offset; j-- {
|
||||||
|
if !unicode.IsSpace(doubledRunes[j]) {
|
||||||
|
dirty = true
|
||||||
|
entity.Length = j + 1 - entity.Offset
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if dirty {
|
||||||
|
alignedEntities[i] = entity
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return alignedEntities
|
||||||
|
}
|
||||||
|
|
||||||
|
func markupBraces(entity *client.TextEntity, lbrace, rbrace []rune) []*insertion {
|
||||||
|
return []*insertion{
|
||||||
|
&insertion{
|
||||||
|
Offset: entity.Offset,
|
||||||
|
Runes: lbrace,
|
||||||
|
Type: insertionOpening,
|
||||||
|
},
|
||||||
|
&insertion{
|
||||||
|
Offset: entity.Offset + entity.Length,
|
||||||
|
Runes: rbrace,
|
||||||
|
Type: insertionClosing,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func quotePrependNewlines(entity *client.TextEntity, doubledRunes []rune, markupMode MarkupModeType) []*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)
|
||||||
case client.TypeTextEntityTypeItalic:
|
case client.TypeTextEntityTypeItalic:
|
||||||
return markupBraces(entity, italicRunes, italicRunes)
|
return markupBraces(entity, italicRunes, italicRunes)
|
||||||
case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre:
|
case client.TypeTextEntityTypeStrikethrough:
|
||||||
|
return markupBraces(entity, strikeRunesMarkdown, strikeRunesMarkdown)
|
||||||
|
case client.TypeTextEntityTypeCode:
|
||||||
return markupBraces(entity, codeRunes, codeRunes)
|
return markupBraces(entity, codeRunes, codeRunes)
|
||||||
|
case client.TypeTextEntityTypePre:
|
||||||
|
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 {
|
||||||
|
return []*insertion{}
|
||||||
|
}
|
||||||
|
|
||||||
switch entity.Type.TextEntityTypeType() {
|
switch entity.Type.TextEntityTypeType() {
|
||||||
case client.TypeTextEntityTypeBold:
|
case client.TypeTextEntityTypeBold:
|
||||||
return markupBraces(entity, boldRunesXEP0393, boldRunesXEP0393)
|
return markupBraces(entity, boldRunesXEP0393, boldRunesXEP0393)
|
||||||
case client.TypeTextEntityTypeItalic:
|
case client.TypeTextEntityTypeItalic:
|
||||||
return markupBraces(entity, italicRunes, italicRunes)
|
return markupBraces(entity, italicRunes, italicRunes)
|
||||||
case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre:
|
case client.TypeTextEntityTypeStrikethrough:
|
||||||
|
return markupBraces(entity, strikeRunesXEP0393, strikeRunesXEP0393)
|
||||||
|
case client.TypeTextEntityTypeCode:
|
||||||
return markupBraces(entity, codeRunes, codeRunes)
|
return markupBraces(entity, codeRunes, codeRunes)
|
||||||
|
case client.TypeTextEntityTypePre:
|
||||||
|
return markupBraces(entity, preRunesStart, preRunesEnd)
|
||||||
case client.TypeTextEntityTypePreCode:
|
case client.TypeTextEntityTypePreCode:
|
||||||
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
|
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
|
||||||
// TODO: inline code support (non-standard too)
|
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), preRunesEnd)
|
||||||
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
startStack := make(InsertionStack, 0, len(sourceText))
|
var entityToMarkup func(*client.TextEntity, []rune, MarkupModeType) []*insertion
|
||||||
endStack := make(InsertionStack, 0, len(sourceText))
|
if markupMode == MarkupModeXEP0393 {
|
||||||
|
entityToMarkup = entityToXEP0393
|
||||||
|
} else {
|
||||||
|
entityToMarkup = entityToMarkdown
|
||||||
|
}
|
||||||
|
|
||||||
|
doubledRunes := textToDoubledRunes(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
|
||||||
for _, entity := range entities {
|
for _, entity := range mergedEntities {
|
||||||
log.Debugf("%#v", entity)
|
log.Debugf("%#v", entity)
|
||||||
if entity.Length <= 0 {
|
if entity.Length <= 0 {
|
||||||
continue
|
continue
|
||||||
|
|
@ -146,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**👙**🐧\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()
|
||||||
|
|
@ -206,3 +206,377 @@ func TestSortEmpty(t *testing.T) {
|
||||||
t.Errorf("Empty entities set sorting error: %#v", entities)
|
t.Errorf("Empty entities set sorting error: %#v", entities)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNoFormattingXEP0393(t *testing.T) {
|
||||||
|
markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeXEP0393)
|
||||||
|
if markup != "abc\ndef" {
|
||||||
|
t.Errorf("No formatting expected, but: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingXEP0393Simple(t *testing.T) {
|
||||||
|
markup := Format("👙🐧🐖", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 2,
|
||||||
|
Length: 4,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "👙*🐧🐖*" {
|
||||||
|
t.Errorf("Wrong simple formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingXEP0393Adjacent(t *testing.T) {
|
||||||
|
markup := Format("a👙🐧🐖", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 3,
|
||||||
|
Length: 2,
|
||||||
|
Type: &client.TextEntityTypeItalic{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 5,
|
||||||
|
Length: 2,
|
||||||
|
Type: &client.TextEntityTypeTextUrl{
|
||||||
|
Url: "https://narayana.im/",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "a👙_🐧_🐖 <https://narayana.im/>" {
|
||||||
|
t.Errorf("Wrong adjacent formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingXEP0393AdjacentAndNested(t *testing.T) {
|
||||||
|
markup := Format("👙🐧🐖", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 4,
|
||||||
|
Type: &client.TextEntityTypePre{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 2,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 4,
|
||||||
|
Length: 2,
|
||||||
|
Type: &client.TextEntityTypeItalic{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "```\n*👙*🐧\n```_🐖_" {
|
||||||
|
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingXEP0393AdjacentItalicBoldItalic(t *testing.T) {
|
||||||
|
markup := Format("раса двуногих крысолюдей, которую так редко замечают, что многие отрицают само их существование", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 0,
|
||||||
|
Length: 26,
|
||||||
|
Type: &client.TextEntityTypeItalic{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 26,
|
||||||
|
Length: 69,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 26,
|
||||||
|
Length: 69,
|
||||||
|
Type: &client.TextEntityTypeItalic{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "_раса двуногих крысолюдей, *которую так редко замечают, что многие отрицают само их существование*_" {
|
||||||
|
t.Errorf("Wrong adjacent italic/bold-italic formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingXEP0393MultipleAdjacent(t *testing.T) {
|
||||||
|
markup := Format("abcde", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 1,
|
||||||
|
Length: 1,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 2,
|
||||||
|
Length: 1,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 3,
|
||||||
|
Length: 1,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 4,
|
||||||
|
Length: 1,
|
||||||
|
Type: &client.TextEntityTypeItalic{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "a*bcd*_e_" {
|
||||||
|
t.Errorf("Wrong multiple adjacent formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingXEP0393Intersecting(t *testing.T) {
|
||||||
|
markup := Format("abcde", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 1,
|
||||||
|
Length: 1,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 2,
|
||||||
|
Length: 3,
|
||||||
|
Type: &client.TextEntityTypeItalic{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 2,
|
||||||
|
Length: 1,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 3,
|
||||||
|
Length: 1,
|
||||||
|
Type: &client.TextEntityTypeBold{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "a*b*_*cd*e_" {
|
||||||
|
t.Errorf("Wrong intersecting formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingXEP0393InlineCode(t *testing.T) {
|
||||||
|
markup := Format("Is Gajim a thing?\n\necho 'Hello'\necho 'world'\n\nhruck(", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 3,
|
||||||
|
Length: 5,
|
||||||
|
Type: &client.TextEntityTypeCode{},
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 19,
|
||||||
|
Length: 25,
|
||||||
|
Type: &client.TextEntityTypePre{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "Is `Gajim` a thing?\n\n```\necho 'Hello'\necho 'world'\n```\n\nhruck(" {
|
||||||
|
t.Errorf("Wrong intersecting formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingMarkdownStrikethrough(t *testing.T) {
|
||||||
|
markup := Format("Everyone dislikes cake.", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 9,
|
||||||
|
Length: 3,
|
||||||
|
Type: &client.TextEntityTypeStrikethrough{},
|
||||||
|
},
|
||||||
|
}, MarkupModeMarkdown)
|
||||||
|
if markup != "Everyone ~~dis~~likes cake." {
|
||||||
|
t.Errorf("Wrong strikethrough formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattingXEP0393Strikethrough(t *testing.T) {
|
||||||
|
markup := Format("Everyone dislikes cake.", []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 9,
|
||||||
|
Length: 3,
|
||||||
|
Type: &client.TextEntityTypeStrikethrough{},
|
||||||
|
},
|
||||||
|
}, MarkupModeXEP0393)
|
||||||
|
if markup != "Everyone ~dis~likes cake." {
|
||||||
|
t.Errorf("Wrong strikethrough formatting: %v", markup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClaspLeft(t *testing.T) {
|
||||||
|
text := textToDoubledRunes("a b c")
|
||||||
|
entities := []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 1,
|
||||||
|
Length: 2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
entities = ClaspDirectives(text, entities)
|
||||||
|
if !(len(entities) == 1 &&
|
||||||
|
entities[0].Offset == 2 && entities[0].Length == 1) {
|
||||||
|
t.Errorf("Wrong claspleft: %#v", entities)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClaspBoth(t *testing.T) {
|
||||||
|
text := textToDoubledRunes("a b c")
|
||||||
|
entities := []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 1,
|
||||||
|
Length: 3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
entities = ClaspDirectives(text, entities)
|
||||||
|
if !(len(entities) == 1 &&
|
||||||
|
entities[0].Offset == 2 && entities[0].Length == 1) {
|
||||||
|
t.Errorf("Wrong claspboth: %#v", entities)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClaspNotNeeded(t *testing.T) {
|
||||||
|
text := textToDoubledRunes(" abc ")
|
||||||
|
entities := []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 1,
|
||||||
|
Length: 3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
entities = ClaspDirectives(text, entities)
|
||||||
|
if !(len(entities) == 1 &&
|
||||||
|
entities[0].Offset == 1 && entities[0].Length == 3) {
|
||||||
|
t.Errorf("Wrong claspnotneeded: %#v", entities)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClaspNested(t *testing.T) {
|
||||||
|
text := textToDoubledRunes("a b c")
|
||||||
|
entities := []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 1,
|
||||||
|
Length: 3,
|
||||||
|
},
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 2,
|
||||||
|
Length: 2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
entities = ClaspDirectives(text, entities)
|
||||||
|
if !(len(entities) == 2 &&
|
||||||
|
entities[0].Offset == 2 && entities[0].Length == 1 &&
|
||||||
|
entities[1].Offset == 2 && entities[1].Length == 1) {
|
||||||
|
t.Errorf("Wrong claspnested: %#v", entities)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClaspEmoji(t *testing.T) {
|
||||||
|
text := textToDoubledRunes("a 🐖 c")
|
||||||
|
entities := []*client.TextEntity{
|
||||||
|
&client.TextEntity{
|
||||||
|
Offset: 1,
|
||||||
|
Length: 4,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
entities = ClaspDirectives(text, entities)
|
||||||
|
if !(len(entities) == 1 &&
|
||||||
|
entities[0].Offset == 2 && entities[0].Length == 2) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,41 @@ func (c *Client) getChatMessageLock(chatID int64) *sync.Mutex {
|
||||||
return lock
|
return lock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) cleanTempFile(path string) {
|
||||||
|
os.Remove(path)
|
||||||
|
|
||||||
|
dir := filepath.Dir(path)
|
||||||
|
dirName := filepath.Base(dir)
|
||||||
|
if strings.HasPrefix(dirName, "telegabber-") {
|
||||||
|
os.Remove(dir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
@ -107,19 +142,36 @@ func (c *Client) updateHandler() {
|
||||||
uhOh()
|
uhOh()
|
||||||
}
|
}
|
||||||
c.updateDeleteMessages(typedUpdate)
|
c.updateDeleteMessages(typedUpdate)
|
||||||
case client.TypeUpdateFile:
|
|
||||||
typedUpdate, ok := update.(*client.UpdateFile)
|
|
||||||
if !ok {
|
|
||||||
uhOh()
|
|
||||||
}
|
|
||||||
c.updateFile(typedUpdate)
|
|
||||||
log.Debugf("%#v", typedUpdate.File)
|
|
||||||
case client.TypeUpdateAuthorizationState:
|
case client.TypeUpdateAuthorizationState:
|
||||||
typedUpdate, ok := update.(*client.UpdateAuthorizationState)
|
typedUpdate, ok := update.(*client.UpdateAuthorizationState)
|
||||||
if !ok {
|
if !ok {
|
||||||
uhOh()
|
uhOh()
|
||||||
}
|
}
|
||||||
c.updateAuthorizationState(typedUpdate)
|
c.updateAuthorizationState(typedUpdate)
|
||||||
|
case client.TypeUpdateMessageSendSucceeded:
|
||||||
|
typedUpdate, ok := update.(*client.UpdateMessageSendSucceeded)
|
||||||
|
if !ok {
|
||||||
|
uhOh()
|
||||||
|
}
|
||||||
|
c.updateMessageSendSucceeded(typedUpdate)
|
||||||
|
case client.TypeUpdateMessageSendFailed:
|
||||||
|
typedUpdate, ok := update.(*client.UpdateMessageSendFailed)
|
||||||
|
if !ok {
|
||||||
|
uhOh()
|
||||||
|
}
|
||||||
|
c.updateMessageSendFailed(typedUpdate)
|
||||||
|
case client.TypeUpdateChatTitle:
|
||||||
|
typedUpdate, ok := update.(*client.UpdateChatTitle)
|
||||||
|
if !ok {
|
||||||
|
uhOh()
|
||||||
|
}
|
||||||
|
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
|
||||||
|
|
@ -134,13 +186,13 @@ func (c *Client) updateHandler() {
|
||||||
func (c *Client) updateUser(update *client.UpdateUser) {
|
func (c *Client) updateUser(update *client.UpdateUser) {
|
||||||
c.cache.SetUser(update.User.Id, update.User)
|
c.cache.SetUser(update.User.Id, update.User)
|
||||||
show, status, presenceType := c.userStatusToText(update.User.Status, update.User.Id)
|
show, status, presenceType := c.userStatusToText(update.User.Status, update.User.Id)
|
||||||
go c.ProcessStatusUpdate(update.User.Id, status, show, gateway.SPType(presenceType))
|
go c.ProcessStatusUpdate(update.User.Id, status, show, false, gateway.SPType(presenceType))
|
||||||
}
|
}
|
||||||
|
|
||||||
// user status changed
|
// user status changed
|
||||||
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
|
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
|
||||||
show, status, presenceType := c.userStatusToText(update.Status, update.UserId)
|
show, status, presenceType := c.userStatusToText(update.Status, update.UserId)
|
||||||
go c.ProcessStatusUpdate(update.UserId, status, show, gateway.SPImmed(false), gateway.SPType(presenceType))
|
go c.ProcessStatusUpdate(update.UserId, status, show, false, gateway.SPImmed(false), gateway.SPType(presenceType))
|
||||||
}
|
}
|
||||||
|
|
||||||
// new chat discovered
|
// new chat discovered
|
||||||
|
|
@ -154,14 +206,14 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.cache.SetChat(update.Chat.Id, update.Chat)
|
c.cache.SetChat(update.Chat.Id, update.Chat, true)
|
||||||
|
|
||||||
if update.Chat.Positions != nil && len(update.Chat.Positions) > 0 {
|
if update.Chat.Positions != nil && len(update.Chat.Positions) > 0 {
|
||||||
c.subscribeToID(update.Chat.Id, update.Chat)
|
c.subscribeToID(update.Chat.Id, update.Chat)
|
||||||
}
|
}
|
||||||
|
|
||||||
if update.Chat.Id < 0 {
|
if update.Chat.Id < 0 {
|
||||||
c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat")
|
c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat", true)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
@ -182,18 +234,34 @@ 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()
|
||||||
|
|
||||||
|
var forceCmd bool
|
||||||
|
if c.LastBotCmdString != "" && update.Message.IsOutgoing {
|
||||||
|
if update.Message.Content.MessageContentType() == client.TypeMessageText {
|
||||||
|
textMessage, _ := update.Message.Content.(*client.MessageText)
|
||||||
|
|
||||||
|
if textMessage.Text != nil && textMessage.Text.Text == c.LastBotCmdString {
|
||||||
|
forceCmd = true
|
||||||
|
c.LastBotCmdString = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ignore self outgoing messages
|
// ignore self outgoing messages
|
||||||
if update.Message.IsOutgoing &&
|
if update.Message.IsOutgoing &&
|
||||||
update.Message.SendingState != nil &&
|
update.Message.SendingState != nil &&
|
||||||
update.Message.SendingState.MessageSendingStateType() == client.TypeMessageSendingStatePending {
|
update.Message.SendingState.MessageSendingStateType() == client.TypeMessageSendingStatePending &&
|
||||||
|
!forceCmd {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -201,98 +269,160 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
|
||||||
"chat_id": chatId,
|
"chat_id": chatId,
|
||||||
}).Warn("New message from chat")
|
}).Warn("New message from chat")
|
||||||
|
|
||||||
var text string
|
c.ProcessIncomingMessage(chatId, update.Message)
|
||||||
content := update.Message.Content
|
|
||||||
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
|
|
||||||
chat, err := c.client.GetChat(&client.GetChatRequest{
|
|
||||||
ChatId: chatId,
|
|
||||||
})
|
|
||||||
if err == nil {
|
|
||||||
c.cache.SetChat(chatId, chat)
|
|
||||||
go c.ProcessStatusUpdate(chatId, "", "", gateway.SPImmed(true))
|
|
||||||
text = "<Chat photo has changed>"
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
text = c.messageToText(update.Message, false)
|
|
||||||
file, filename := c.contentToFilename(content)
|
|
||||||
|
|
||||||
// download file(s)
|
|
||||||
if file != nil && !file.Local.IsDownloadingCompleted {
|
|
||||||
newFile, err := c.DownloadFile(file.Id, 1, true)
|
|
||||||
if err == nil {
|
|
||||||
file = newFile
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// OTR support (I do not know why would you need it, seriously)
|
|
||||||
if !(strings.HasPrefix(text, "?OTR") || c.Session.RawMessages) {
|
|
||||||
var prefix strings.Builder
|
|
||||||
prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename)))
|
|
||||||
if text != "" {
|
|
||||||
// \n if it is groupchat and message is not empty
|
|
||||||
if chatId < 0 {
|
|
||||||
prefix.WriteString("\n")
|
|
||||||
} else if chatId > 0 {
|
|
||||||
prefix.WriteString(" | ")
|
|
||||||
}
|
|
||||||
|
|
||||||
prefix.WriteString(text)
|
|
||||||
}
|
|
||||||
|
|
||||||
text = prefix.String()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// mark message as read
|
|
||||||
c.client.ViewMessages(&client.ViewMessagesRequest{
|
|
||||||
ChatId: chatId,
|
|
||||||
MessageIds: []int64{update.Message.Id},
|
|
||||||
ForceRead: true,
|
|
||||||
})
|
|
||||||
// forward message to XMPP
|
|
||||||
gateway.SendMessage(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp)
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
markupFunction := c.getFormatter()
|
||||||
|
|
||||||
|
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 {
|
if update.NewContent.MessageContentType() == client.TypeMessageText {
|
||||||
textContent := update.NewContent.(*client.MessageText)
|
textContent := update.NewContent.(*client.MessageText)
|
||||||
text := fmt.Sprintf("✎ %v | %s", update.MessageId, formatter.Format(
|
log.Debugf("textContent: %#v", textContent.Text)
|
||||||
textContent.Text.Text,
|
|
||||||
formatter.SortEntities(textContent.Text.Entities),
|
var replaceId string
|
||||||
markupFunction,
|
sId := strconv.FormatInt(update.MessageId, 10)
|
||||||
))
|
var isCarbon bool
|
||||||
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
|
|
||||||
|
go func() {
|
||||||
|
message, messageErr := c.client.GetMessage(&client.GetMessageRequest{
|
||||||
|
ChatId: update.ChatId,
|
||||||
|
MessageId: update.MessageId,
|
||||||
|
})
|
||||||
|
if messageErr != nil {
|
||||||
|
// odnako za vremya puti
|
||||||
|
// sobaka mogla podrasti
|
||||||
|
c.MessageIdChangesLock.Lock()
|
||||||
|
idsMap, idsMapOk := c.MessageIdChanges[update.ChatId]
|
||||||
|
hadNoId := false
|
||||||
|
if idsMapOk {
|
||||||
|
newId, newIdOk := idsMap[update.MessageId]
|
||||||
|
if newIdOk {
|
||||||
|
if newId.Id == 0 {
|
||||||
|
hadNoId = true
|
||||||
|
c.MessageIdChangesLock.Unlock()
|
||||||
|
newId.Lock()
|
||||||
|
}
|
||||||
|
log.Infof("falling back to updated message id: %v/%v->%v", update.ChatId, update.MessageId, newId.Id)
|
||||||
|
message, messageErr = c.client.GetMessage(&client.GetMessageRequest{
|
||||||
|
ChatId: update.ChatId,
|
||||||
|
MessageId: newId.Id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hadNoId {
|
||||||
|
c.MessageIdChangesLock.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var prefix string
|
||||||
|
if messageErr == nil {
|
||||||
|
if message.EditDate == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("editDate: %v", message.EditDate)
|
||||||
|
|
||||||
|
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 is a carbon and if it is edited: %v", update.ChatId, update.MessageId, messageErr.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var text strings.Builder
|
||||||
|
|
||||||
|
if replaceId == "" {
|
||||||
|
var editChar string
|
||||||
|
if c.Session.AsciiArrows {
|
||||||
|
editChar = "e"
|
||||||
|
} else {
|
||||||
|
editChar = "✎"
|
||||||
|
}
|
||||||
|
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.Entities,
|
||||||
|
markupFunction,
|
||||||
|
))
|
||||||
|
|
||||||
|
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 {
|
||||||
text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",")
|
for _, deleteId := range update.MessageIds {
|
||||||
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
|
c.tryUnlockMessageId(update.ChatId, deleteId)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// file downloaded
|
|
||||||
func (c *Client) updateFile(update *client.UpdateFile) {
|
|
||||||
// not really
|
|
||||||
if !update.File.Local.IsDownloadingCompleted {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err := os.Symlink(
|
|
||||||
update.File.Local.Path,
|
|
||||||
c.formatFilePath(c.content.Path, update.File.Remote.UniqueId, filepath.Ext(update.File.Local.Path)),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
linkErr := err.(*os.LinkError)
|
|
||||||
if linkErr.Err.Error() == "file exists" {
|
|
||||||
log.Warn(err.Error())
|
|
||||||
} else {
|
|
||||||
log.Errorf("Error creating symlink: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.Session.IsChatIgnored(update.ChatId) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if c.Session.IgnoreGroupDeletions {
|
||||||
|
chatType, chatTypeErr := c.GetChatType(update.ChatId, false)
|
||||||
|
if chatTypeErr == nil && (chatType == ChatTypeBasicGroup || chatType == ChatTypeSupergroup) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var deleteChar string
|
||||||
|
if c.Session.AsciiArrows {
|
||||||
|
deleteChar = "X "
|
||||||
|
} else {
|
||||||
|
deleteChar = "✗ "
|
||||||
|
}
|
||||||
|
text := deleteChar + strings.Join(int64SliceToStringSlice(update.MessageIds), ",")
|
||||||
|
gateway.SendTextMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -305,3 +435,74 @@ func (c *Client) updateAuthorizationState(update *client.UpdateAuthorizationStat
|
||||||
c.forceClose()
|
c.forceClose()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
c.MessageIdChangesLock.Lock()
|
||||||
|
idsMap, ok := c.MessageIdChanges[update.Message.ChatId]
|
||||||
|
if !ok {
|
||||||
|
idsMap = make(map[int64]*newId)
|
||||||
|
c.MessageIdChanges[update.Message.ChatId] = idsMap
|
||||||
|
}
|
||||||
|
id, ok := idsMap[update.OldMessageId]
|
||||||
|
if !ok {
|
||||||
|
id = newNewId()
|
||||||
|
idsMap[update.OldMessageId] = id
|
||||||
|
}
|
||||||
|
id.Id = update.Message.Id
|
||||||
|
c.MessageIdChangesLock.Unlock()
|
||||||
|
|
||||||
|
c.sendMarker(update.Message.ChatId, update.Message.Id, gateway.MarkerTypeReceived)
|
||||||
|
|
||||||
|
// clean uploaded files
|
||||||
|
file, _ := c.contentToFile(update.Message.Content)
|
||||||
|
if file != nil && file.Local != nil {
|
||||||
|
c.cleanTempFile(file.Local.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed) {
|
||||||
|
c.tryUnlockMessageId(update.Message.ChatId, update.OldMessageId)
|
||||||
|
|
||||||
|
// clean uploaded files
|
||||||
|
file, _ := c.contentToFile(update.Message.Content)
|
||||||
|
if file != nil && file.Local != nil {
|
||||||
|
c.cleanTempFile(file.Local.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// chat title changed
|
||||||
|
func (c *Client) updateChatTitle(update *client.UpdateChatTitle) {
|
||||||
|
gateway.SetNickname(c.jid, strconv.FormatInt(update.ChatId, 10), update.Title, c.xmpp)
|
||||||
|
|
||||||
|
// set also the status (for group chats only)
|
||||||
|
chat, user, _ := c.GetContactByID(update.ChatId, nil, false)
|
||||||
|
if user == nil {
|
||||||
|
c.ProcessStatusUpdate(update.ChatId, update.Title, "chat", false, 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) tryUnlockMessageId(chatId, messageId int64) {
|
||||||
|
c.MessageIdChangesLock.Lock()
|
||||||
|
idsMap, ok := c.MessageIdChanges[chatId]
|
||||||
|
if ok {
|
||||||
|
id, ok := idsMap[messageId]
|
||||||
|
if ok {
|
||||||
|
id.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.MessageIdChangesLock.Unlock()
|
||||||
|
}
|
||||||
|
|
|
||||||
82
telegram/loginwizard.go
Normal file
82
telegram/loginwizard.go
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/zelenin/go-tdlib/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StartLoginWizard initiates a loginWizard object
|
||||||
|
func (c *Client) StartLoginWizard(inCommand bool) {
|
||||||
|
if c.loginWizard == nil {
|
||||||
|
c.loginWizard = &loginWizardMetadata{
|
||||||
|
nextStage: make(chan string, 1),
|
||||||
|
commandSent: inCommand,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.loginWizard.commandSent = inCommand
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopLoginWizard safely destroys the loginWizard object
|
||||||
|
func (c *Client) StopLoginWizard() {
|
||||||
|
c.locks.loginWizardReadLock.Lock()
|
||||||
|
c.locks.loginWizardWriteLock.Lock()
|
||||||
|
if c.loginWizard != nil {
|
||||||
|
close(c.loginWizard.nextStage)
|
||||||
|
c.loginWizard = nil
|
||||||
|
}
|
||||||
|
c.locks.loginWizardReadLock.Unlock()
|
||||||
|
c.locks.loginWizardWriteLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLoginWizardNextStage waits for the next stage from the channel
|
||||||
|
func (c *Client) GetLoginWizardNextStage() string {
|
||||||
|
c.locks.loginWizardReadLock.Lock()
|
||||||
|
defer c.locks.loginWizardReadLock.Unlock()
|
||||||
|
|
||||||
|
if c.loginWizard != nil {
|
||||||
|
if c.loginWizard.commandSent {
|
||||||
|
log.Debugf("waiting for nextStage...")
|
||||||
|
nextStage := <-c.loginWizard.nextStage
|
||||||
|
c.loginWizard.commandSent = false
|
||||||
|
c.loginWizard.chanBusy = false
|
||||||
|
log.Debugf("yielded stage %v", nextStage)
|
||||||
|
return nextStage
|
||||||
|
} else {
|
||||||
|
if c.lastAuthorizationStateType == client.TypeAuthorizationStateWaitPhoneNumber ||
|
||||||
|
c.lastAuthorizationStateType == client.TypeAuthorizationStateClosing ||
|
||||||
|
c.Session.Login == "" {
|
||||||
|
return "login"
|
||||||
|
}
|
||||||
|
switch c.lastAuthorizationStateType {
|
||||||
|
case client.TypeAuthorizationStateWaitCode:
|
||||||
|
return "code"
|
||||||
|
case client.TypeAuthorizationStateWaitPassword:
|
||||||
|
return "password"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) wizardStageOrPrompt(stage, message string) {
|
||||||
|
c.locks.loginWizardWriteLock.Lock()
|
||||||
|
if c.loginWizard == nil {
|
||||||
|
c.locks.loginWizardWriteLock.Unlock()
|
||||||
|
if message != "" {
|
||||||
|
gateway.SendServiceMessage(c.jid, message, c.xmpp)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !c.loginWizard.chanBusy {
|
||||||
|
log.Debugf("writing wizard stage %v", stage)
|
||||||
|
c.loginWizard.nextStage <- stage
|
||||||
|
} else {
|
||||||
|
log.Warn("Skipping stage %v, wizard cannot keep up", stage)
|
||||||
|
}
|
||||||
|
c.loginWizard.chanBusy = true
|
||||||
|
c.locks.loginWizardWriteLock.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
1480
telegram/utils.go
1480
telegram/utils.go
File diff suppressed because it is too large
Load diff
|
|
@ -30,7 +30,7 @@ func TestOnlineRecently(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOnlineOfflineAway(t *testing.T) {
|
func TestOnlineOfflineAway(t *testing.T) {
|
||||||
location, _ := time.LoadLocation("Europe/Berlin")
|
location := time.FixedZone("UTC+1", 3600)
|
||||||
timestamp := time.Now().In(location).Unix() - 3599
|
timestamp := time.Now().In(location).Unix() - 3599
|
||||||
tm := time.Unix(timestamp, 0).In(location)
|
tm := time.Unix(timestamp, 0).In(location)
|
||||||
c := &Client{
|
c := &Client{
|
||||||
|
|
@ -130,7 +130,7 @@ func TestFormatMessageMultilinePreview(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFormatContent(t *testing.T) {
|
func TestFormatFile(t *testing.T) {
|
||||||
file := client.File{
|
file := client.File{
|
||||||
Size: 23899,
|
Size: 23899,
|
||||||
Local: &client.LocalFile{
|
Local: &client.LocalFile{
|
||||||
|
|
@ -146,10 +146,38 @@ func TestFormatContent(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
content := c.formatContent(&file, "a.jpg")
|
content, link := c.formatFile(&file, false)
|
||||||
if content != "a.jpg (23 kbytes) | localhvost/b0896d9e9f1de7d2af59b080c3f0947b838c5c6c64f71c68a4b690a15de2ccf8.jpg" {
|
if content != ". (23 kbytes) | " {
|
||||||
t.Errorf("Wrong file label: %v", content)
|
t.Errorf("Wrong file label: %v", content)
|
||||||
}
|
}
|
||||||
|
if link != "" {
|
||||||
|
t.Errorf("Wrong file link: %v", link)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormatPreview(t *testing.T) {
|
||||||
|
file := client.File{
|
||||||
|
Size: 23899,
|
||||||
|
Local: &client.LocalFile{
|
||||||
|
Path: "c:/pron/smokovnica_vhsrip.mov",
|
||||||
|
},
|
||||||
|
Remote: &client.RemoteFile{
|
||||||
|
UniqueId: "aZaZaZ",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
c := Client{
|
||||||
|
content: &config.TelegramContentConfig{
|
||||||
|
Link: "localhvost",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
content, link := c.formatFile(&file, true)
|
||||||
|
if content != "" {
|
||||||
|
t.Errorf("Wrong preview label: %v", content)
|
||||||
|
}
|
||||||
|
if link != "" {
|
||||||
|
t.Errorf("Wrong preview link: %v", link)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageToTextSticker(t *testing.T) {
|
func TestMessageToTextSticker(t *testing.T) {
|
||||||
|
|
@ -341,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{},
|
||||||
|
|
@ -351,184 +426,59 @@ func TestMessageUnknown(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContentToFilenameSticker(t *testing.T) {
|
|
||||||
sticker := client.MessageSticker{
|
|
||||||
Sticker: &client.Sticker{},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&sticker)
|
|
||||||
if filename != "sticker.webp" {
|
|
||||||
t.Errorf("Wrong sticker filename")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenameVoice(t *testing.T) {
|
|
||||||
voice := client.MessageVoiceNote{
|
|
||||||
VoiceNote: &client.VoiceNote{
|
|
||||||
Duration: 56,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&voice)
|
|
||||||
if filename != "voice note (56 s.).oga" {
|
|
||||||
t.Errorf("Wrong voice note filename")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenameVideoNote(t *testing.T) {
|
|
||||||
video := client.MessageVideoNote{
|
|
||||||
VideoNote: &client.VideoNote{
|
|
||||||
Duration: 56,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&video)
|
|
||||||
if filename != "video note (56 s.).mp4" {
|
|
||||||
t.Errorf("Wrong video note filename")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenameAnimation(t *testing.T) {
|
|
||||||
animation := client.MessageAnimation{
|
|
||||||
Animation: &client.Animation{},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&animation)
|
|
||||||
if filename != "animation.mp4" {
|
|
||||||
t.Errorf("Wrong animation filename")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenamePhoto(t *testing.T) {
|
|
||||||
photo := client.MessagePhoto{
|
|
||||||
Photo: &client.Photo{
|
|
||||||
Sizes: []*client.PhotoSize{
|
|
||||||
&client.PhotoSize{
|
|
||||||
Photo: &client.File{},
|
|
||||||
},
|
|
||||||
&client.PhotoSize{
|
|
||||||
Photo: &client.File{
|
|
||||||
Id: 56,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&photo)
|
|
||||||
if filename != "56.jpg" {
|
|
||||||
t.Errorf("Wrong photo filename")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenamePhotoNoSizes(t *testing.T) {
|
|
||||||
photo := client.MessagePhoto{
|
|
||||||
Photo: &client.Photo{
|
|
||||||
Sizes: []*client.PhotoSize{},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&photo)
|
|
||||||
if filename != "" {
|
|
||||||
t.Errorf("Wrong filename of sizeless photo")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenameAudio(t *testing.T) {
|
|
||||||
audio := client.MessageAudio{
|
|
||||||
Audio: &client.Audio{
|
|
||||||
FileName: "swine.mp3",
|
|
||||||
Audio: &client.File{
|
|
||||||
Local: &client.LocalFile{
|
|
||||||
Path: "C:/WINNT/swine.mp3",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&audio)
|
|
||||||
if filename != "swine.mp3" {
|
|
||||||
t.Errorf("Not oinking, shame on you!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenameVideo(t *testing.T) {
|
|
||||||
video := client.MessageVideo{
|
|
||||||
Video: &client.Video{
|
|
||||||
FileName: "swine.3gp",
|
|
||||||
Video: &client.File{
|
|
||||||
Local: &client.LocalFile{
|
|
||||||
Path: "C:/Document and Settings/Svinarchuk-PC/swine.3gp",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&video)
|
|
||||||
if filename != "swine.3gp" {
|
|
||||||
t.Errorf("Not pigdancing, shame on you!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenameDocument(t *testing.T) {
|
|
||||||
document := client.MessageDocument{
|
|
||||||
Document: &client.Document{
|
|
||||||
FileName: "swine.doc",
|
|
||||||
Document: &client.File{
|
|
||||||
Local: &client.LocalFile{
|
|
||||||
Path: "D:/My Documents/swine.doc",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&document)
|
|
||||||
if filename != "swine.doc" {
|
|
||||||
t.Errorf("Not hoofstomping, shame on you!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContentToFilenameUnknown(t *testing.T) {
|
|
||||||
unknown := client.MessageExpiredPhoto{}
|
|
||||||
_, filename := (&Client{}).contentToFilename(&unknown)
|
|
||||||
if filename != "" {
|
|
||||||
t.Errorf("Wrong filename of unknown content")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMessageToPrefix1(t *testing.T) {
|
func TestMessageToPrefix1(t *testing.T) {
|
||||||
message := client.Message{
|
message := client.Message{
|
||||||
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 := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "")
|
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 gatewayReply != nil {
|
||||||
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageToPrefix2(t *testing.T) {
|
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 := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "")
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "", false)
|
||||||
if prefix != "⬅ 56 | fwd: (zaz)" {
|
if prefix != "⬅ 56 | fwd: (zaz) | preview: y.jpg" {
|
||||||
t.Errorf("Wrong prefix: %v", prefix)
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
}
|
}
|
||||||
|
if gatewayReply != nil {
|
||||||
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageToPrefix3(t *testing.T) {
|
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",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
prefix := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "a.jpg")
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg", false)
|
||||||
if prefix != "< 56 | fwd: | file: a.jpg" {
|
if prefix != "< 56 | fwd: (zuz) | file: a.jpg" {
|
||||||
t.Errorf("Wrong prefix: %v", prefix)
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
}
|
}
|
||||||
|
if gatewayReply != nil {
|
||||||
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageToPrefix4(t *testing.T) {
|
func TestMessageToPrefix4(t *testing.T) {
|
||||||
|
|
@ -536,8 +486,139 @@ func TestMessageToPrefix4(t *testing.T) {
|
||||||
Id: 23,
|
Id: 23,
|
||||||
IsOutgoing: true,
|
IsOutgoing: true,
|
||||||
}
|
}
|
||||||
prefix := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "")
|
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 gatewayReply != nil {
|
||||||
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMessageToPrefix5(t *testing.T) {
|
||||||
|
message := client.Message{
|
||||||
|
Id: 560,
|
||||||
|
ForwardInfo: &client.MessageForwardInfo{
|
||||||
|
Origin: &client.MessageOriginChat{
|
||||||
|
AuthorSignature: "zyz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
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" {
|
||||||
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
|
}
|
||||||
|
if gatewayReply != nil {
|
||||||
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMessageToPrefix6(t *testing.T) {
|
||||||
|
message := client.Message{
|
||||||
|
Id: 23,
|
||||||
|
ChatId: 25,
|
||||||
|
IsOutgoing: true,
|
||||||
|
ReplyTo: &client.MessageReplyToMessage{
|
||||||
|
ChatId: 41,
|
||||||
|
Quote: &client.TextQuote{
|
||||||
|
Text: &client.FormattedText{
|
||||||
|
Text: "tist\nuz\niz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Origin: &client.MessageOriginHiddenUser{
|
||||||
|
SenderName: "ziz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", false)
|
||||||
|
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{
|
||||||
|
Text: &client.FormattedText{
|
||||||
|
Text: "tist",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Origin: &client.MessageOriginChannel{
|
||||||
|
AuthorSignature: "zaz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
prefix, gatewayReply := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", false)
|
||||||
|
if prefix != "> 23 | reply: (zaz) @ unknown contact: TDlib instance is offline | tist" {
|
||||||
|
t.Errorf("Wrong prefix: %v", prefix)
|
||||||
|
}
|
||||||
|
if gatewayReply != nil {
|
||||||
|
t.Errorf("Reply is not nil: %v", gatewayReply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSenderIdEmpty(t *testing.T) {
|
||||||
|
message := client.Message{}
|
||||||
|
senderId := (&Client{}).getMessageSenderId(&message)
|
||||||
|
if senderId != 0 {
|
||||||
|
t.Errorf("Wrong sender id: %v", senderId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSenderIdUser(t *testing.T) {
|
||||||
|
message := client.Message{
|
||||||
|
SenderId: &client.MessageSenderUser{
|
||||||
|
UserId: 42,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
senderId := (&Client{}).getMessageSenderId(&message)
|
||||||
|
if senderId != 42 {
|
||||||
|
t.Errorf("Wrong sender id: %v", senderId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSenderIdChat(t *testing.T) {
|
||||||
|
message := client.Message{
|
||||||
|
SenderId: &client.MessageSenderChat{
|
||||||
|
ChatId: -42,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
senderId := (&Client{}).getMessageSenderId(&message)
|
||||||
|
if senderId != -42 {
|
||||||
|
t.Errorf("Wrong sender id: %v", senderId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,12 @@ package xmpp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"dev.narayana.im/narayana/telegabber/badger"
|
||||||
"dev.narayana.im/narayana/telegabber/config"
|
"dev.narayana.im/narayana/telegabber/config"
|
||||||
"dev.narayana.im/narayana/telegabber/persistence"
|
"dev.narayana.im/narayana/telegabber/persistence"
|
||||||
"dev.narayana.im/narayana/telegabber/telegram"
|
"dev.narayana.im/narayana/telegabber/telegram"
|
||||||
|
|
@ -20,18 +23,49 @@ var sessions map[string]*telegram.Client
|
||||||
var db *persistence.SessionsYamlDB
|
var db *persistence.SessionsYamlDB
|
||||||
var sessionLock sync.Mutex
|
var sessionLock sync.Mutex
|
||||||
|
|
||||||
|
const (
|
||||||
|
B uint64 = 1
|
||||||
|
KB = B << 10
|
||||||
|
MB = KB << 10
|
||||||
|
GB = MB << 10
|
||||||
|
TB = GB << 10
|
||||||
|
PB = TB << 10
|
||||||
|
EB = PB << 10
|
||||||
|
|
||||||
|
maxUint64 uint64 = (1 << 64) - 1
|
||||||
|
)
|
||||||
|
|
||||||
|
var sizeRegex = regexp.MustCompile("\\A([0-9]+) ?([KMGTPE]?B?)\\z")
|
||||||
|
|
||||||
// NewComponent starts a new component and wraps it in
|
// NewComponent starts a new component and wraps it in
|
||||||
// a stream manager that you should start yourself
|
// a stream manager that you should start yourself
|
||||||
func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.StreamManager, *xmpp.Component, error) {
|
func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig, idsPath string, version string) (*xmpp.StreamManager, *xmpp.Component, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
gateway.Jid, err = stanza.NewJid(conf.Jid)
|
gateway.Jid, err = stanza.NewJid(conf.Jid)
|
||||||
|
gateway.Version = version
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
if gateway.Jid.Resource == "" {
|
||||||
|
if tc.Tdlib.Client.DeviceModel != "" {
|
||||||
|
gateway.Jid.Resource = tc.Tdlib.Client.DeviceModel
|
||||||
|
} else {
|
||||||
|
gateway.Jid.Resource = "telegabber"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gateway.IdsDB = badger.IdsDBOpen(idsPath)
|
||||||
|
|
||||||
tgConf = tc
|
tgConf = tc
|
||||||
|
|
||||||
|
if tc.Content.Quota != "" {
|
||||||
|
gateway.StorageQuota, err = parseSize(tc.Content.Quota)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("Error parsing the storage quota: %v; the cleaner is disabled", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
options := xmpp.ComponentOptions{
|
options := xmpp.ComponentOptions{
|
||||||
TransportConfiguration: xmpp.TransportConfiguration{
|
TransportConfiguration: xmpp.TransportConfiguration{
|
||||||
Address: conf.Host + ":" + conf.Port,
|
Address: conf.Host + ":" + conf.Port,
|
||||||
|
|
@ -80,10 +114,22 @@ func heartbeat(component *xmpp.Component) {
|
||||||
}
|
}
|
||||||
sessionLock.Unlock()
|
sessionLock.Unlock()
|
||||||
|
|
||||||
|
quotaLowThreshold := gateway.StorageQuota / 10 * 9
|
||||||
|
|
||||||
log.Info("Starting heartbeat queue")
|
log.Info("Starting heartbeat queue")
|
||||||
|
|
||||||
// status updater thread
|
// status updater thread
|
||||||
for {
|
for {
|
||||||
|
gateway.StorageLock.Lock()
|
||||||
|
if quotaLowThreshold > 0 && tgConf.Content.Path != "" {
|
||||||
|
gateway.MeasureStorageSize(tgConf.Content.Path)
|
||||||
|
|
||||||
|
if gateway.CachedStorageSize > quotaLowThreshold {
|
||||||
|
gateway.CleanOldFiles(tgConf.Content.Path, quotaLowThreshold)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gateway.StorageLock.Unlock()
|
||||||
|
|
||||||
time.Sleep(60e9)
|
time.Sleep(60e9)
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
|
|
@ -96,11 +142,24 @@ func heartbeat(component *xmpp.Component) {
|
||||||
chatID,
|
chatID,
|
||||||
session.LastSeenStatus(delayedStatus.TimestampOnline),
|
session.LastSeenStatus(delayedStatus.TimestampOnline),
|
||||||
"away",
|
"away",
|
||||||
|
true,
|
||||||
)
|
)
|
||||||
delete(session.DelayedStatuses, chatID)
|
delete(session.DelayedStatuses, chatID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
session.DelayedStatusesLock.Unlock()
|
session.DelayedStatusesLock.Unlock()
|
||||||
|
|
||||||
|
// shrink message id maps
|
||||||
|
session.MessageIdChangesLock.Lock()
|
||||||
|
for _, idsMap := range session.MessageIdChanges {
|
||||||
|
for oldMessageId, newId := range idsMap {
|
||||||
|
if newId.Ts < now - 60 {
|
||||||
|
newId.Unlock()
|
||||||
|
delete(idsMap, oldMessageId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
session.MessageIdChangesLock.Unlock()
|
||||||
}
|
}
|
||||||
sessionLock.Unlock()
|
sessionLock.Unlock()
|
||||||
|
|
||||||
|
|
@ -121,6 +180,8 @@ func heartbeat(component *xmpp.Component) {
|
||||||
// it would be resolved on the next iteration
|
// it would be resolved on the next iteration
|
||||||
SaveSessions()
|
SaveSessions()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gateway.IdsDB.Gc()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -198,6 +259,53 @@ func Close(component *xmpp.Component) {
|
||||||
// save sessions
|
// save sessions
|
||||||
SaveSessions()
|
SaveSessions()
|
||||||
|
|
||||||
|
// flush the ids database
|
||||||
|
gateway.IdsDB.Close()
|
||||||
|
|
||||||
// close stream
|
// close stream
|
||||||
component.Disconnect()
|
component.Disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// based on https://github.com/c2h5oh/datasize/blob/master/datasize.go
|
||||||
|
func parseSize(sSize string) (uint64, error) {
|
||||||
|
sizeParts := sizeRegex.FindStringSubmatch(sSize)
|
||||||
|
|
||||||
|
if len(sizeParts) > 2 {
|
||||||
|
numPart, err := strconv.ParseInt(sizeParts[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var divisor uint64
|
||||||
|
val := uint64(numPart)
|
||||||
|
|
||||||
|
if len(sizeParts[2]) > 0 {
|
||||||
|
switch sizeParts[2][0] {
|
||||||
|
case 'B':
|
||||||
|
divisor = 1
|
||||||
|
case 'K':
|
||||||
|
divisor = KB
|
||||||
|
case 'M':
|
||||||
|
divisor = MB
|
||||||
|
case 'G':
|
||||||
|
divisor = GB
|
||||||
|
case 'T':
|
||||||
|
divisor = TB
|
||||||
|
case 'P':
|
||||||
|
divisor = PB
|
||||||
|
case 'E':
|
||||||
|
divisor = EB
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if divisor == 0 {
|
||||||
|
return 0, &strconv.NumError{"Wrong suffix", sSize, strconv.ErrSyntax}
|
||||||
|
}
|
||||||
|
if val > maxUint64/divisor {
|
||||||
|
return 0, &strconv.NumError{"Overflow", sSize, strconv.ErrRange}
|
||||||
|
}
|
||||||
|
return val * divisor, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, &strconv.NumError{"Not enough parts", sSize, strconv.ErrSyntax}
|
||||||
|
}
|
||||||
|
|
|
||||||
47
xmpp/component_test.go
Normal file
47
xmpp/component_test.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package xmpp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseSizeGarbage(t *testing.T) {
|
||||||
|
_, err := parseSize("abc")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("abc should not be accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSizeAsphalt(t *testing.T) {
|
||||||
|
size, err := parseSize("2B")
|
||||||
|
if size != 2 {
|
||||||
|
t.Errorf("Error parsing two bytes: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSize9K(t *testing.T) {
|
||||||
|
size, err := parseSize("9 KB")
|
||||||
|
if size != 9216 {
|
||||||
|
t.Errorf("Error parsing 9K: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSizeBits(t *testing.T) {
|
||||||
|
size, err := parseSize("9 Kb")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Error parsing kilobits: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSizeEB(t *testing.T) {
|
||||||
|
size, err := parseSize("3EB")
|
||||||
|
if size != 3458764513820540928 {
|
||||||
|
t.Errorf("Error parsing exabytes: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseSizeOverflow(t *testing.T) {
|
||||||
|
size, err := parseSize("314EB")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Overflow is not overflowing: %v %v", size, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ package extensions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"gosrc.io/xmpp/stanza"
|
"gosrc.io/xmpp/stanza"
|
||||||
)
|
)
|
||||||
|
|
@ -32,6 +33,7 @@ type IqVcardTemp struct {
|
||||||
N IqVcardN
|
N IqVcardN
|
||||||
Tel IqVcardTel
|
Tel IqVcardTel
|
||||||
Photo IqVcardPhoto
|
Photo IqVcardPhoto
|
||||||
|
Desc IqVcardDesc
|
||||||
ResultSet *stanza.ResultSet `xml:"set,omitempty"`
|
ResultSet *stanza.ResultSet `xml:"set,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,6 +106,127 @@ type IqVcardPhotoBinval struct {
|
||||||
Text string `xml:",chardata"`
|
Text string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IqVcardDesc is vCard/DESC
|
||||||
|
type IqVcardDesc struct {
|
||||||
|
XMLName xml.Name `xml:"DESC"`
|
||||||
|
Text string `xml:",chardata"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reply is from XEP-0461
|
||||||
|
type Reply struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:reply:0 reply"`
|
||||||
|
To string `xml:"to,attr"`
|
||||||
|
Id string `xml:"id,attr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback is from XEP-0428
|
||||||
|
type Fallback struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:fallback:0 fallback"`
|
||||||
|
For string `xml:"for,attr"`
|
||||||
|
Body []FallbackBody `xml:"urn:xmpp:fallback:0 body"`
|
||||||
|
Subject []FallbackSubject `xml:"urn:xmpp:fallback:0 subject"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FallbackBody is from XEP-0428
|
||||||
|
type FallbackBody struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:fallback:0 body"`
|
||||||
|
Start string `xml:"start,attr"`
|
||||||
|
End string `xml:"end,attr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FallbackSubject is from XEP-0428
|
||||||
|
type FallbackSubject struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:fallback:0 subject"`
|
||||||
|
Start string `xml:"start,attr"`
|
||||||
|
End string `xml:"end,attr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CarbonReceived is from XEP-0280
|
||||||
|
type CarbonReceived struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:carbons:2 received"`
|
||||||
|
Forwarded stanza.Forwarded `xml:"urn:xmpp:forward:0 forwarded"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CarbonSent is from XEP-0280
|
||||||
|
type CarbonSent struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:carbons:2 sent"`
|
||||||
|
Forwarded stanza.Forwarded `xml:"urn:xmpp:forward:0 forwarded"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ComponentPrivilege is from XEP-0356
|
||||||
|
type ComponentPrivilege1 struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:privilege:1 privilege"`
|
||||||
|
Perms []ComponentPerm `xml:"perm"`
|
||||||
|
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
|
||||||
|
type ComponentPerm struct {
|
||||||
|
XMLName xml.Name `xml:"perm"`
|
||||||
|
Access string `xml:"access,attr"`
|
||||||
|
Type string `xml:"type,attr"`
|
||||||
|
Push bool `xml:"push,attr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClientMessage is a jabber:client NS message compatible with Prosody's XEP-0356 implementation
|
||||||
|
type ClientMessage struct {
|
||||||
|
XMLName xml.Name `xml:"jabber:client message"`
|
||||||
|
stanza.Attrs
|
||||||
|
|
||||||
|
Subject string `xml:"subject,omitempty"`
|
||||||
|
Body string `xml:"body,omitempty"`
|
||||||
|
Thread string `xml:"thread,omitempty"`
|
||||||
|
Error stanza.Err `xml:"error,omitempty"`
|
||||||
|
Extensions []stanza.MsgExtension `xml:",omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace is from XEP-0308
|
||||||
|
type Replace struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:message-correct:0 replace"`
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// EntityTime is from XEP-0202
|
||||||
|
type EntityTime struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:time time"`
|
||||||
|
Tzo string `xml:"tzo"`
|
||||||
|
Utc string `xml:"utc"`
|
||||||
|
ResultSet *stanza.ResultSet `xml:"set,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AttachTo is from XEP-0367
|
||||||
|
type AttachTo struct {
|
||||||
|
XMLName xml.Name `xml:"urn:xmpp:message-attaching:1 attach-to"`
|
||||||
|
Id string `xml:"id,attr"`
|
||||||
|
}
|
||||||
|
|
||||||
// Namespace is a namespace!
|
// Namespace is a namespace!
|
||||||
func (c PresenceNickExtension) Namespace() string {
|
func (c PresenceNickExtension) Namespace() string {
|
||||||
return c.XMLName.Space
|
return c.XMLName.Space
|
||||||
|
|
@ -119,11 +242,89 @@ func (c IqVcardTemp) Namespace() string {
|
||||||
return c.XMLName.Space
|
return c.XMLName.Space
|
||||||
}
|
}
|
||||||
|
|
||||||
// Namespace is a namespace!
|
// GetSet getsets!
|
||||||
func (c IqVcardTemp) GetSet() *stanza.ResultSet {
|
func (c IqVcardTemp) GetSet() *stanza.ResultSet {
|
||||||
return c.ResultSet
|
return c.ResultSet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c Reply) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c Fallback) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c CarbonReceived) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c CarbonSent) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c ComponentPrivilege1) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c ComponentPrivilege2) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c Replace) Namespace() string {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c EntityTime) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSet getsets!
|
||||||
|
func (c EntityTime) GetSet() *stanza.ResultSet {
|
||||||
|
return c.ResultSet
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name is a packet name
|
||||||
|
func (ClientMessage) Name() string {
|
||||||
|
return "message"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Namespace is a namespace!
|
||||||
|
func (c AttachTo) Namespace() string {
|
||||||
|
return c.XMLName.Space
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewReplyFallback initializes a fallback range
|
||||||
|
func NewReplyFallback(start uint64, end uint64) Fallback {
|
||||||
|
return Fallback{
|
||||||
|
For: "urn:xmpp:reply:0",
|
||||||
|
Body: []FallbackBody{
|
||||||
|
FallbackBody{
|
||||||
|
Start: strconv.FormatUint(start, 10),
|
||||||
|
End: strconv.FormatUint(end, 10),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// presence nick
|
// presence nick
|
||||||
stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{
|
stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{
|
||||||
|
|
@ -142,4 +343,64 @@ func init() {
|
||||||
"vcard-temp",
|
"vcard-temp",
|
||||||
"vCard",
|
"vCard",
|
||||||
}, IqVcardTemp{})
|
}, IqVcardTemp{})
|
||||||
|
|
||||||
|
// reply
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:reply:0",
|
||||||
|
"reply",
|
||||||
|
}, Reply{})
|
||||||
|
|
||||||
|
// fallback
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:fallback:0",
|
||||||
|
"fallback",
|
||||||
|
}, Fallback{})
|
||||||
|
|
||||||
|
// carbon received
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:carbons:2",
|
||||||
|
"received",
|
||||||
|
}, CarbonReceived{})
|
||||||
|
|
||||||
|
// carbon sent
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:carbons:2",
|
||||||
|
"sent",
|
||||||
|
}, CarbonSent{})
|
||||||
|
|
||||||
|
// component privilege v1
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:privilege:1",
|
||||||
|
"privilege",
|
||||||
|
}, ComponentPrivilege1{})
|
||||||
|
|
||||||
|
// component privilege v2
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:privilege:2",
|
||||||
|
"privilege",
|
||||||
|
}, ComponentPrivilege2{})
|
||||||
|
|
||||||
|
// message edit
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:message-correct:0",
|
||||||
|
"replace",
|
||||||
|
}, Replace{})
|
||||||
|
|
||||||
|
// register query
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTIQ, xml.Name{
|
||||||
|
"jabber:iq:register",
|
||||||
|
"query",
|
||||||
|
}, QueryRegister{})
|
||||||
|
|
||||||
|
// entity time
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTIQ, xml.Name{
|
||||||
|
"urn:xmpp:time",
|
||||||
|
"time",
|
||||||
|
}, EntityTime{})
|
||||||
|
|
||||||
|
// attach-to
|
||||||
|
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
|
||||||
|
"urn:xmpp:message-attaching:1",
|
||||||
|
"attach-to",
|
||||||
|
}, AttachTo{})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,47 @@ package gateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"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"
|
||||||
"gosrc.io/xmpp/stanza"
|
"gosrc.io/xmpp/stanza"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Reply struct {
|
||||||
|
Author string
|
||||||
|
Id string
|
||||||
|
Start 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 NodeVCard4 string = "urn:xmpp:vcard4"
|
||||||
|
const NodeAvatarMetadata string = "urn:xmpp:avatar:metadata"
|
||||||
|
const NodeAvatarMetadataNotify string = NodeAvatarMetadata + "+notify"
|
||||||
|
const NodeAvatarData string = "urn:xmpp:avatar:data"
|
||||||
|
const NSCommand string = "http://jabber.org/protocol/commands"
|
||||||
|
|
||||||
// Queue stores presences to send later
|
// Queue stores presences to send later
|
||||||
var Queue = make(map[string]*stanza.Presence)
|
var Queue = make(map[string]*stanza.Presence)
|
||||||
var QueueLock = sync.Mutex{}
|
var QueueLock = sync.Mutex{}
|
||||||
|
|
@ -20,16 +50,70 @@ var QueueLock = sync.Mutex{}
|
||||||
// Jid stores the component's JID object
|
// Jid stores the component's JID object
|
||||||
var Jid *stanza.Jid
|
var Jid *stanza.Jid
|
||||||
|
|
||||||
|
// Version stores this software's version
|
||||||
|
var Version string
|
||||||
|
|
||||||
|
// IdsDB provides a disk-backed bidirectional dictionary of Telegram and XMPP ids
|
||||||
|
var IdsDB badger.IdsDB
|
||||||
|
|
||||||
// DirtySessions denotes that some Telegram session configurations
|
// DirtySessions denotes that some Telegram session configurations
|
||||||
// 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
|
||||||
|
|
||||||
|
// MessageOutgoingPermissionVersion contains a XEP-0356 version to fake outgoing messages by foreign JIDs
|
||||||
|
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, component *xmpp.Component) {
|
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, nil, "", replaceId, isCarbon, requestReceipt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendServiceMessage creates and sends a simple message stanza from transport
|
||||||
|
func SendServiceMessage(to string, body string, component *xmpp.Component) {
|
||||||
|
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
|
||||||
|
func SendTextMessage(to string, from string, body string, component *xmpp.Component) {
|
||||||
|
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
|
||||||
|
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, nil, oob, replaceId, isCarbon, requestReceipt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
if err != nil {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"to": to,
|
||||||
|
}).Error(errors.Wrap(err, "Invalid to JID!"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
bareTo := toJid.Bare()
|
||||||
|
|
||||||
componentJid := Jid.Full()
|
componentJid := Jid.Full()
|
||||||
|
|
||||||
var logFrom string
|
var logFrom string
|
||||||
var messageFrom string
|
var messageFrom string
|
||||||
|
var messageTo string
|
||||||
if from == "" {
|
if from == "" {
|
||||||
logFrom = componentJid
|
logFrom = componentJid
|
||||||
messageFrom = componentJid
|
messageFrom = componentJid
|
||||||
|
|
@ -37,6 +121,12 @@ func SendMessage(to string, from string, body string, component *xmpp.Component)
|
||||||
logFrom = from
|
logFrom = from
|
||||||
messageFrom = from + "@" + componentJid
|
messageFrom = from + "@" + componentJid
|
||||||
}
|
}
|
||||||
|
if isCarbon {
|
||||||
|
messageTo = messageFrom
|
||||||
|
messageFrom = bareTo + "/" + Jid.Resource
|
||||||
|
} else {
|
||||||
|
messageTo = to
|
||||||
|
}
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"from": logFrom,
|
"from": logFrom,
|
||||||
|
|
@ -46,12 +136,120 @@ func SendMessage(to string, from string, body string, component *xmpp.Component)
|
||||||
message := stanza.Message{
|
message := stanza.Message{
|
||||||
Attrs: stanza.Attrs{
|
Attrs: stanza.Attrs{
|
||||||
From: messageFrom,
|
From: messageFrom,
|
||||||
To: to,
|
To: messageTo,
|
||||||
Type: "chat",
|
Type: "chat",
|
||||||
|
Id: id,
|
||||||
},
|
},
|
||||||
Body: body,
|
Body: body,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if oob != "" {
|
||||||
|
message.Extensions = append(message.Extensions, stanza.OOB{
|
||||||
|
URL: oob,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if reply != nil {
|
||||||
|
message.Extensions = append(message.Extensions, extensions.Reply{
|
||||||
|
To: reply.Author,
|
||||||
|
Id: reply.Id,
|
||||||
|
})
|
||||||
|
if reply.End > 0 {
|
||||||
|
message.Extensions = append(message.Extensions, extensions.NewReplyFallback(reply.Start, reply.End))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if marker != nil {
|
||||||
|
if marker.Type == MarkerTypeReceived {
|
||||||
|
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 isCarbon {
|
||||||
|
carbonMessage := extensions.ClientMessage{
|
||||||
|
Attrs: stanza.Attrs{
|
||||||
|
From: bareTo,
|
||||||
|
To: to,
|
||||||
|
Type: "chat",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
carbonMessage.Extensions = append(carbonMessage.Extensions, extensions.CarbonSent{
|
||||||
|
Forwarded: stanza.Forwarded{
|
||||||
|
Stanza: extensions.ClientMessage(message),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
privilegeMessage := stanza.Message{
|
||||||
|
Attrs: stanza.Attrs{
|
||||||
|
From: Jid.Bare(),
|
||||||
|
To: toJid.Domain,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if MessageOutgoingPermissionVersion == 2 {
|
||||||
|
privilegeMessage.Extensions = append(privilegeMessage.Extensions, extensions.ComponentPrivilege2{
|
||||||
|
Forwarded: stanza.Forwarded{
|
||||||
|
Stanza: carbonMessage,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
privilegeMessage.Extensions = append(privilegeMessage.Extensions, extensions.ComponentPrivilege1{
|
||||||
|
Forwarded: stanza.Forwarded{
|
||||||
|
Stanza: carbonMessage,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
sendMessage(&privilegeMessage, component)
|
||||||
|
} else {
|
||||||
|
sendMessage(&message, component)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetNickname sets a new nickname for a contact
|
||||||
|
func SetNickname(to string, from string, nickname string, component *xmpp.Component) {
|
||||||
|
componentJid := Jid.Bare()
|
||||||
|
messageFrom := from + "@" + componentJid
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"from": from,
|
||||||
|
"to": to,
|
||||||
|
}).Warn("Set nickname")
|
||||||
|
|
||||||
|
message := stanza.Message{
|
||||||
|
Attrs: stanza.Attrs{
|
||||||
|
From: messageFrom,
|
||||||
|
To: to,
|
||||||
|
Type: "headline",
|
||||||
|
},
|
||||||
|
Extensions: []stanza.MsgExtension{
|
||||||
|
stanza.PubSubEvent{
|
||||||
|
EventElement: stanza.ItemsEvent{
|
||||||
|
Node: NSNick,
|
||||||
|
Items: []stanza.ItemEvent{
|
||||||
|
stanza.ItemEvent{
|
||||||
|
Any: &stanza.Node{
|
||||||
|
XMLName: xml.Name{Space: NSNick, Local: "nick"},
|
||||||
|
Content: nickname,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessage(&message, component)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendMessage(message *stanza.Message, component *xmpp.Component) {
|
||||||
// explicit check, as marshalling is expensive
|
// explicit check, as marshalling is expensive
|
||||||
if log.GetLevel() == log.DebugLevel {
|
if log.GetLevel() == log.DebugLevel {
|
||||||
xmlMessage, err := xml.Marshal(message)
|
xmlMessage, err := xml.Marshal(message)
|
||||||
|
|
@ -197,6 +395,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)
|
||||||
|
|
@ -213,3 +425,64 @@ 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
|
||||||
|
func SplitJID(from string) (string, string, bool) {
|
||||||
|
fromJid, err := stanza.NewJid(from)
|
||||||
|
if err != nil {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"from": from,
|
||||||
|
}).Error(errors.Wrap(err, "Invalid from JID!"))
|
||||||
|
return "", "", false
|
||||||
|
}
|
||||||
|
return fromJid.Bare(), fromJid.Resource, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendPubSubAvatarNotification encourages clients to fetch an avatar
|
||||||
|
func SendPubSubAvatarNotification(component *xmpp.Component, jid string, chatId int64, sha1 string, size int64) {
|
||||||
|
info := stanza.Node{
|
||||||
|
XMLName: xml.Name{Local: "info"},
|
||||||
|
Attrs: []xml.Attr{
|
||||||
|
xml.Attr{Name: xml.Name{Local: "bytes"}, Value: strconv.FormatInt(size, 10)},
|
||||||
|
xml.Attr{Name: xml.Name{Local: "height"}, Value: "160"},
|
||||||
|
xml.Attr{Name: xml.Name{Local: "id"}, Value: sha1},
|
||||||
|
xml.Attr{Name: xml.Name{Local: "type"}, Value: "image/jpeg"},
|
||||||
|
xml.Attr{Name: xml.Name{Local: "width"}, Value: "160"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"chatId": chatId,
|
||||||
|
}).Debugf("%#v", info)
|
||||||
|
|
||||||
|
event := &stanza.PubSubEvent{
|
||||||
|
EventElement: &stanza.ItemsEvent{
|
||||||
|
Node: NodeAvatarMetadata,
|
||||||
|
Items: []stanza.ItemEvent{
|
||||||
|
stanza.ItemEvent{
|
||||||
|
Id: sha1,
|
||||||
|
Any: &stanza.Node{
|
||||||
|
XMLName: xml.Name{Local: "metadata", Space: NodeAvatarMetadata},
|
||||||
|
Nodes: []stanza.Node{info},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
message := stanza.Message{
|
||||||
|
Attrs: stanza.Attrs{
|
||||||
|
From: strconv.FormatInt(chatId, 10) + "@" + Jid.Bare(),
|
||||||
|
To: jid,
|
||||||
|
Type: stanza.MessageTypeHeadline,
|
||||||
|
},
|
||||||
|
Extensions: []stanza.MsgExtension{event},
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = ResumableSend(component, message)
|
||||||
|
}
|
||||||
|
|
|
||||||
90
xmpp/gateway/storage.go
Normal file
90
xmpp/gateway/storage.go
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StorageQuota is a value from config parsed to bytes number
|
||||||
|
var StorageQuota uint64
|
||||||
|
|
||||||
|
// CachedStorageSize estimates the storage size between full rescans
|
||||||
|
var CachedStorageSize uint64
|
||||||
|
var StorageLock = sync.Mutex{}
|
||||||
|
|
||||||
|
// MeasureStorageSize replaces the estimated storage size with relevant data from the filesystem
|
||||||
|
func MeasureStorageSize(path string) {
|
||||||
|
dents, err := ioutil.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var total uint64
|
||||||
|
for _, fi := range dents {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
total += uint64(fi.Size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if total != CachedStorageSize {
|
||||||
|
if CachedStorageSize > 0 {
|
||||||
|
log.Warnf("Correcting cached storage size: was %v, actually %v", CachedStorageSize, total)
|
||||||
|
}
|
||||||
|
CachedStorageSize = total
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanOldFiles purges the oldest files in a directory that exceed the limit
|
||||||
|
func CleanOldFiles(path string, limit uint64) {
|
||||||
|
dents, err := ioutil.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var total uint64
|
||||||
|
for _, fi := range dents {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
total += uint64(fi.Size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort by time
|
||||||
|
sort.Slice(dents, func(i int, j int) bool {
|
||||||
|
return dents[i].ModTime().Before(dents[j].ModTime())
|
||||||
|
})
|
||||||
|
|
||||||
|
// purge
|
||||||
|
if total > limit {
|
||||||
|
toPurge := total - limit
|
||||||
|
|
||||||
|
var purgedAmount uint64
|
||||||
|
var purgedCount uint64
|
||||||
|
|
||||||
|
for _, fi := range dents {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
err = os.Remove(path + string(os.PathSeparator) + fi.Name())
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Couldn't remove %v: %v", fi.Name(), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
purgedAmount += uint64(fi.Size())
|
||||||
|
purgedCount += 1
|
||||||
|
if purgedAmount >= toPurge {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Cleaned %v bytes of %v old files", purgedAmount, purgedCount)
|
||||||
|
if CachedStorageSize > purgedAmount {
|
||||||
|
CachedStorageSize -= purgedAmount
|
||||||
|
} else {
|
||||||
|
CachedStorageSize = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1432
xmpp/handlers.go
1432
xmpp/handlers.go
File diff suppressed because it is too large
Load diff
101
xmpp/loginwizard.go
Normal file
101
xmpp/loginwizard.go
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
package xmpp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"dev.narayana.im/narayana/telegabber/telegram"
|
||||||
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gosrc.io/xmpp"
|
||||||
|
"gosrc.io/xmpp/stanza"
|
||||||
|
)
|
||||||
|
|
||||||
|
func loginWizardPayload(bare string, requestForm *stanza.Form, resource string) (payload *stanza.Command, cancelSend bool, returnSession *telegram.Client) {
|
||||||
|
payload = &stanza.Command{
|
||||||
|
SessionId: "loginwizard",
|
||||||
|
Node: "loginwizard",
|
||||||
|
}
|
||||||
|
|
||||||
|
session, ok := sessions[bare]
|
||||||
|
if ok {
|
||||||
|
returnSession = session
|
||||||
|
|
||||||
|
if requestForm == nil {
|
||||||
|
session.StartLoginWizard(false)
|
||||||
|
cancelSend = true
|
||||||
|
} else {
|
||||||
|
if len(requestForm.Fields) != 1 {
|
||||||
|
setCommandPayloadError(payload, "Hey, don't tinker with the form!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
field := requestForm.Fields[0]
|
||||||
|
if field != nil {
|
||||||
|
if len(field.ValuesList) < 1 {
|
||||||
|
setCommandPayloadError(payload, "No value")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch field.Var {
|
||||||
|
case "login", "code", "password":
|
||||||
|
default:
|
||||||
|
setCommandPayloadError(payload, "Unknown field")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session.StartLoginWizard(true)
|
||||||
|
response, success := session.ProcessTransportCommand(fmt.Sprintf("/%v %v", field.Var, field.ValuesList[0]), resource)
|
||||||
|
if !success {
|
||||||
|
setCommandPayloadError(payload, response)
|
||||||
|
session.StopLoginWizard()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cancelSend = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setCommandPayloadError(payload, fmt.Sprintf("Session is not initialized, add the transport (%v) to contacts first", gateway.Jid.Bare()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendLoginWizardResponse(component *xmpp.Component, answer *stanza.IQ, session *telegram.Client) {
|
||||||
|
payload := &stanza.Command{
|
||||||
|
SessionId: "loginwizard",
|
||||||
|
Node: "loginwizard",
|
||||||
|
}
|
||||||
|
|
||||||
|
nextStage := "login"
|
||||||
|
if session != nil {
|
||||||
|
nextStage = session.GetLoginWizardNextStage()
|
||||||
|
}
|
||||||
|
log.Debugf("nextStage: %v", nextStage)
|
||||||
|
|
||||||
|
if nextStage == "cancel" {
|
||||||
|
setCommandPayloadError(payload, "Cancelled")
|
||||||
|
session.StopLoginWizard()
|
||||||
|
} else if nextStage == "success" {
|
||||||
|
payload.Status = stanza.CommandStatusCompleted
|
||||||
|
session.StopLoginWizard()
|
||||||
|
} else {
|
||||||
|
required := ""
|
||||||
|
form := stanza.Form{
|
||||||
|
Type: stanza.FormTypeForm,
|
||||||
|
Title: "Login Wizard",
|
||||||
|
Fields: []*stanza.Field{
|
||||||
|
&stanza.Field{
|
||||||
|
Var: nextStage,
|
||||||
|
Label: nextStage,
|
||||||
|
Required: &required,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
payload.Status = stanza.CommandStatusExecuting
|
||||||
|
payload.CommandElements = append(payload.CommandElements, &form)
|
||||||
|
}
|
||||||
|
|
||||||
|
answer.Payload = payload
|
||||||
|
|
||||||
|
gateway.ResumableSend(component, answer)
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue