teleproto 203.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/CryptoFile.d.ts +2 -0
  2. package/CryptoFile.js +23 -0
  3. package/Helpers.d.ts +151 -0
  4. package/Helpers.js +477 -0
  5. package/LICENSE +23 -0
  6. package/Password.d.ts +15 -0
  7. package/Password.js +271 -0
  8. package/README.md +111 -0
  9. package/Utils.d.ts +228 -0
  10. package/Utils.js +1248 -0
  11. package/Version.d.ts +1 -0
  12. package/Version.js +4 -0
  13. package/client/2fa.d.ts +48 -0
  14. package/client/2fa.js +109 -0
  15. package/client/TelegramClient.d.ts +1042 -0
  16. package/client/TelegramClient.js +1167 -0
  17. package/client/account.d.ts +0 -0
  18. package/client/account.js +1 -0
  19. package/client/auth.d.ts +93 -0
  20. package/client/auth.js +375 -0
  21. package/client/bots.d.ts +6 -0
  22. package/client/bots.js +24 -0
  23. package/client/buttons.d.ts +4 -0
  24. package/client/buttons.js +87 -0
  25. package/client/chats.d.ts +46 -0
  26. package/client/chats.js +350 -0
  27. package/client/dialogs.d.ts +52 -0
  28. package/client/dialogs.js +174 -0
  29. package/client/downloads.d.ts +157 -0
  30. package/client/downloads.js +598 -0
  31. package/client/fs.d.ts +1 -0
  32. package/client/fs.js +13 -0
  33. package/client/index.d.ts +15 -0
  34. package/client/index.js +50 -0
  35. package/client/messageParse.d.ts +17 -0
  36. package/client/messageParse.js +191 -0
  37. package/client/messages.d.ts +259 -0
  38. package/client/messages.js +804 -0
  39. package/client/os.d.ts +2 -0
  40. package/client/os.js +23 -0
  41. package/client/path.d.ts +2 -0
  42. package/client/path.js +7 -0
  43. package/client/telegramBaseClient.d.ts +238 -0
  44. package/client/telegramBaseClient.js +368 -0
  45. package/client/updates.d.ts +31 -0
  46. package/client/updates.js +254 -0
  47. package/client/uploads.d.ts +137 -0
  48. package/client/uploads.js +514 -0
  49. package/client/users.d.ts +29 -0
  50. package/client/users.js +491 -0
  51. package/crypto/AuthKey.d.ts +20 -0
  52. package/crypto/AuthKey.js +76 -0
  53. package/crypto/CTR.d.ts +6 -0
  54. package/crypto/CTR.js +35 -0
  55. package/crypto/Factorizator.d.ts +19 -0
  56. package/crypto/Factorizator.js +74 -0
  57. package/crypto/IGE.d.ts +18 -0
  58. package/crypto/IGE.js +34 -0
  59. package/crypto/RSA.d.ts +15 -0
  60. package/crypto/RSA.js +70 -0
  61. package/crypto/converters.d.ts +19 -0
  62. package/crypto/converters.js +52 -0
  63. package/crypto/crypto.d.ts +27 -0
  64. package/crypto/crypto.js +117 -0
  65. package/crypto/words.d.ts +6 -0
  66. package/crypto/words.js +48 -0
  67. package/define.d.ts +77 -0
  68. package/entityCache.d.ts +7 -0
  69. package/entityCache.js +79 -0
  70. package/errors/Common.d.ts +72 -0
  71. package/errors/Common.js +122 -0
  72. package/errors/RPCBaseErrors.d.ts +84 -0
  73. package/errors/RPCBaseErrors.js +134 -0
  74. package/errors/RPCErrorList.d.ts +37 -0
  75. package/errors/RPCErrorList.js +117 -0
  76. package/errors/index.d.ts +11 -0
  77. package/errors/index.js +29 -0
  78. package/events/Album.d.ts +36 -0
  79. package/events/Album.js +104 -0
  80. package/events/CallbackQuery.d.ts +74 -0
  81. package/events/CallbackQuery.js +193 -0
  82. package/events/DeletedMessage.d.ts +45 -0
  83. package/events/DeletedMessage.js +63 -0
  84. package/events/EditedMessage.d.ts +30 -0
  85. package/events/EditedMessage.js +41 -0
  86. package/events/NewMessage.d.ts +89 -0
  87. package/events/NewMessage.js +182 -0
  88. package/events/Raw.d.ts +29 -0
  89. package/events/Raw.js +43 -0
  90. package/events/common.d.ts +66 -0
  91. package/events/common.js +141 -0
  92. package/events/index.d.ts +2 -0
  93. package/events/index.js +8 -0
  94. package/extensions/AsyncQueue.d.ts +10 -0
  95. package/extensions/AsyncQueue.js +32 -0
  96. package/extensions/BinaryReader.d.ts +100 -0
  97. package/extensions/BinaryReader.js +242 -0
  98. package/extensions/BinaryWriter.d.ts +7 -0
  99. package/extensions/BinaryWriter.js +15 -0
  100. package/extensions/Deferred.d.ts +8 -0
  101. package/extensions/Deferred.js +16 -0
  102. package/extensions/Logger.d.ts +56 -0
  103. package/extensions/Logger.js +107 -0
  104. package/extensions/MessagePacker.d.ts +22 -0
  105. package/extensions/MessagePacker.js +154 -0
  106. package/extensions/PendingState.d.ts +12 -0
  107. package/extensions/PendingState.js +29 -0
  108. package/extensions/PromisedNetSockets.d.ts +25 -0
  109. package/extensions/PromisedNetSockets.js +178 -0
  110. package/extensions/PromisedWebSockets.d.ts +19 -0
  111. package/extensions/PromisedWebSockets.js +124 -0
  112. package/extensions/html.d.ts +5 -0
  113. package/extensions/html.js +213 -0
  114. package/extensions/index.d.ts +7 -0
  115. package/extensions/index.js +17 -0
  116. package/extensions/markdown.d.ts +5 -0
  117. package/extensions/markdown.js +76 -0
  118. package/extensions/markdownv2.d.ts +5 -0
  119. package/extensions/markdownv2.js +51 -0
  120. package/extensions/net.d.ts +1 -0
  121. package/extensions/net.js +13 -0
  122. package/extensions/socks.d.ts +1 -0
  123. package/extensions/socks.js +13 -0
  124. package/index.d.ts +14 -0
  125. package/index.js +48 -0
  126. package/inspect.d.ts +1 -0
  127. package/inspect.js +5 -0
  128. package/network/Authenticator.d.ts +12 -0
  129. package/network/Authenticator.js +193 -0
  130. package/network/MTProtoPlainSender.d.ts +19 -0
  131. package/network/MTProtoPlainSender.js +74 -0
  132. package/network/MTProtoSender.d.ts +291 -0
  133. package/network/MTProtoSender.js +854 -0
  134. package/network/MTProtoState.d.ts +103 -0
  135. package/network/MTProtoState.js +267 -0
  136. package/network/RequestState.d.ts +19 -0
  137. package/network/RequestState.js +35 -0
  138. package/network/connection/Connection.d.ts +71 -0
  139. package/network/connection/Connection.js +157 -0
  140. package/network/connection/TCPAbridged.d.ts +20 -0
  141. package/network/connection/TCPAbridged.js +58 -0
  142. package/network/connection/TCPFull.d.ts +17 -0
  143. package/network/connection/TCPFull.js +61 -0
  144. package/network/connection/TCPMTProxy.d.ts +50 -0
  145. package/network/connection/TCPMTProxy.js +121 -0
  146. package/network/connection/TCPObfuscated.d.ts +19 -0
  147. package/network/connection/TCPObfuscated.js +78 -0
  148. package/network/connection/index.d.ts +4 -0
  149. package/network/connection/index.js +11 -0
  150. package/network/index.d.ts +11 -0
  151. package/network/index.js +23 -0
  152. package/package.json +69 -0
  153. package/requestIter.d.ts +24 -0
  154. package/requestIter.js +107 -0
  155. package/sessions/Abstract.d.ts +103 -0
  156. package/sessions/Abstract.js +6 -0
  157. package/sessions/CacheApiSession.d.ts +18 -0
  158. package/sessions/CacheApiSession.js +99 -0
  159. package/sessions/Memory.d.ts +38 -0
  160. package/sessions/Memory.js +272 -0
  161. package/sessions/StoreSession.d.ts +14 -0
  162. package/sessions/StoreSession.js +77 -0
  163. package/sessions/StringSession.d.ts +33 -0
  164. package/sessions/StringSession.js +116 -0
  165. package/sessions/index.d.ts +4 -0
  166. package/sessions/index.js +13 -0
  167. package/sessions/localStorage.d.ts +1 -0
  168. package/sessions/localStorage.js +4 -0
  169. package/tl/AllTLObjects.d.ts +3 -0
  170. package/tl/AllTLObjects.js +17 -0
  171. package/tl/MTProtoRequest.d.ts +19 -0
  172. package/tl/MTProtoRequest.js +38 -0
  173. package/tl/api.d.ts +31425 -0
  174. package/tl/api.js +507 -0
  175. package/tl/apiTl.d.ts +2 -0
  176. package/tl/apiTl.js +2142 -0
  177. package/tl/core/GZIPPacked.d.ts +16 -0
  178. package/tl/core/GZIPPacked.js +51 -0
  179. package/tl/core/MessageContainer.d.ts +12 -0
  180. package/tl/core/MessageContainer.js +42 -0
  181. package/tl/core/RPCResult.d.ts +15 -0
  182. package/tl/core/RPCResult.js +32 -0
  183. package/tl/core/TLMessage.d.ts +10 -0
  184. package/tl/core/TLMessage.js +14 -0
  185. package/tl/core/index.d.ts +6 -0
  186. package/tl/core/index.js +16 -0
  187. package/tl/custom/button.d.ts +25 -0
  188. package/tl/custom/button.js +78 -0
  189. package/tl/custom/chatGetter.d.ts +30 -0
  190. package/tl/custom/chatGetter.js +114 -0
  191. package/tl/custom/dialog.d.ts +31 -0
  192. package/tl/custom/dialog.js +40 -0
  193. package/tl/custom/draft.d.ts +22 -0
  194. package/tl/custom/draft.js +48 -0
  195. package/tl/custom/file.d.ts +22 -0
  196. package/tl/custom/file.js +68 -0
  197. package/tl/custom/forward.d.ts +16 -0
  198. package/tl/custom/forward.js +47 -0
  199. package/tl/custom/index.d.ts +1 -0
  200. package/tl/custom/index.js +5 -0
  201. package/tl/custom/inlineResult.d.ts +33 -0
  202. package/tl/custom/inlineResult.js +87 -0
  203. package/tl/custom/inlineResults.d.ts +21 -0
  204. package/tl/custom/inlineResults.js +26 -0
  205. package/tl/custom/message.d.ts +428 -0
  206. package/tl/custom/message.js +702 -0
  207. package/tl/custom/messageButton.d.ts +55 -0
  208. package/tl/custom/messageButton.js +152 -0
  209. package/tl/custom/senderGetter.d.ts +29 -0
  210. package/tl/custom/senderGetter.js +55 -0
  211. package/tl/generateModule.d.ts +1 -0
  212. package/tl/generateModule.js +17 -0
  213. package/tl/generationHelpers.d.ts +12 -0
  214. package/tl/generationHelpers.js +289 -0
  215. package/tl/index.d.ts +3 -0
  216. package/tl/index.js +10 -0
  217. package/tl/patched/index.d.ts +2 -0
  218. package/tl/patched/index.js +77 -0
  219. package/tl/schemaTl.d.ts +2 -0
  220. package/tl/schemaTl.js +64 -0
  221. package/tl/types-generator/generate.d.ts +1 -0
  222. package/tl/types-generator/generate.js +84 -0
  223. package/tl/types-generator/template.d.ts +6 -0
  224. package/tl/types-generator/template.js +257 -0
@@ -0,0 +1,25 @@
1
+ /// <reference types="node" />
2
+ import { ProxyInterface } from "../network/connection/TCPMTProxy";
3
+ export declare class PromisedNetSockets {
4
+ private client?;
5
+ private closed;
6
+ private stream;
7
+ private canRead?;
8
+ private resolveRead;
9
+ private proxy?;
10
+ constructor(proxy?: ProxyInterface);
11
+ readExactly(number: number): Promise<Buffer>;
12
+ read(number: number): Promise<Buffer>;
13
+ readAll(): Promise<Buffer>;
14
+ /**
15
+ * Creates a new connection
16
+ * @param port
17
+ * @param ip
18
+ * @returns {Promise<void>}
19
+ */
20
+ connect(port: number, ip: string): Promise<unknown>;
21
+ write(data: Buffer): void;
22
+ close(): Promise<void>;
23
+ receive(): Promise<void>;
24
+ toString(): string;
25
+ }
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.PromisedNetSockets = void 0;
23
+ const net = __importStar(require("./net"));
24
+ const socks_1 = require("./socks");
25
+ const async_mutex_1 = require("async-mutex");
26
+ const mutex = new async_mutex_1.Mutex();
27
+ const closeError = new Error("NetSocket was closed");
28
+ class PromisedNetSockets {
29
+ constructor(proxy) {
30
+ this.client = undefined;
31
+ this.closed = true;
32
+ this.stream = Buffer.alloc(0);
33
+ if (proxy) {
34
+ // we only want to use this when it's not an MTProto proxy.
35
+ if (!("MTProxy" in proxy)) {
36
+ if (!proxy.ip || !proxy.port || !proxy.socksType) {
37
+ throw new Error(`Invalid sockets params: ip=${proxy.ip}, port=${proxy.port}, socksType=${proxy.socksType}`);
38
+ }
39
+ this.proxy = proxy;
40
+ }
41
+ }
42
+ }
43
+ async readExactly(number) {
44
+ let readData = Buffer.alloc(0);
45
+ while (true) {
46
+ const thisTime = await this.read(number);
47
+ readData = Buffer.concat([readData, thisTime]);
48
+ number = number - thisTime.length;
49
+ if (!number || number === -437) {
50
+ return readData;
51
+ }
52
+ }
53
+ }
54
+ async read(number) {
55
+ if (this.closed) {
56
+ throw closeError;
57
+ }
58
+ await this.canRead;
59
+ if (this.closed) {
60
+ throw closeError;
61
+ }
62
+ const toReturn = this.stream.slice(0, number);
63
+ this.stream = this.stream.slice(number);
64
+ if (this.stream.length === 0) {
65
+ this.canRead = new Promise((resolve) => {
66
+ this.resolveRead = resolve;
67
+ });
68
+ }
69
+ return toReturn;
70
+ }
71
+ async readAll() {
72
+ if (this.closed || !(await this.canRead)) {
73
+ throw closeError;
74
+ }
75
+ const toReturn = this.stream;
76
+ this.stream = Buffer.alloc(0);
77
+ this.canRead = new Promise((resolve) => {
78
+ this.resolveRead = resolve;
79
+ });
80
+ return toReturn;
81
+ }
82
+ /**
83
+ * Creates a new connection
84
+ * @param port
85
+ * @param ip
86
+ * @returns {Promise<void>}
87
+ */
88
+ async connect(port, ip) {
89
+ this.stream = Buffer.alloc(0);
90
+ let connected = false;
91
+ if (this.proxy) {
92
+ const info = await socks_1.SocksClient.createConnection({
93
+ proxy: {
94
+ host: this.proxy.ip,
95
+ port: this.proxy.port,
96
+ type: this.proxy.socksType,
97
+ userId: this.proxy.username,
98
+ password: this.proxy.password,
99
+ },
100
+ command: "connect",
101
+ timeout: (this.proxy.timeout || 5) * 1000,
102
+ destination: {
103
+ host: ip,
104
+ port: port,
105
+ },
106
+ });
107
+ this.client = info.socket;
108
+ connected = true;
109
+ }
110
+ else {
111
+ this.client = new net.Socket();
112
+ }
113
+ this.canRead = new Promise((resolve) => {
114
+ this.resolveRead = resolve;
115
+ });
116
+ this.closed = false;
117
+ return new Promise((resolve, reject) => {
118
+ if (this.client) {
119
+ if (connected) {
120
+ this.receive();
121
+ resolve(this);
122
+ }
123
+ else {
124
+ this.client.connect(port, ip, () => {
125
+ this.receive();
126
+ resolve(this);
127
+ });
128
+ }
129
+ this.client.on("error", reject);
130
+ this.client.on("close", () => {
131
+ if (this.client && this.client.destroyed) {
132
+ if (this.resolveRead) {
133
+ this.resolveRead(false);
134
+ }
135
+ this.closed = true;
136
+ }
137
+ });
138
+ }
139
+ });
140
+ }
141
+ write(data) {
142
+ if (this.closed) {
143
+ throw closeError;
144
+ }
145
+ if (this.client) {
146
+ this.client.write(data);
147
+ }
148
+ }
149
+ async close() {
150
+ if (this.client) {
151
+ await this.client.destroy();
152
+ this.client.unref();
153
+ }
154
+ this.closed = true;
155
+ }
156
+ async receive() {
157
+ if (this.client) {
158
+ this.client.on("data", async (message) => {
159
+ const release = await mutex.acquire();
160
+ try {
161
+ let data;
162
+ //CONTEST BROWSER
163
+ this.stream = Buffer.concat([this.stream, message]);
164
+ if (this.resolveRead) {
165
+ this.resolveRead(true);
166
+ }
167
+ }
168
+ finally {
169
+ release();
170
+ }
171
+ });
172
+ }
173
+ }
174
+ toString() {
175
+ return "PromisedNetSocket";
176
+ }
177
+ }
178
+ exports.PromisedNetSockets = PromisedNetSockets;
@@ -0,0 +1,19 @@
1
+ /// <reference types="node" />
2
+ export declare class PromisedWebSockets {
3
+ private closed;
4
+ private stream;
5
+ private canRead?;
6
+ private resolveRead;
7
+ private client;
8
+ private website?;
9
+ constructor();
10
+ readExactly(number: number): Promise<Buffer>;
11
+ read(number: number): Promise<Buffer>;
12
+ readAll(): Promise<Buffer>;
13
+ getWebSocketLink(ip: string, port: number, testServers: boolean): string;
14
+ connect(port: number, ip: string, testServers?: boolean): Promise<unknown>;
15
+ write(data: Buffer): void;
16
+ close(): Promise<void>;
17
+ receive(): Promise<void>;
18
+ toString(): string;
19
+ }
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PromisedWebSockets = void 0;
4
+ const websocket_1 = require("websocket");
5
+ const async_mutex_1 = require("async-mutex");
6
+ const mutex = new async_mutex_1.Mutex();
7
+ const closeError = new Error("WebSocket was closed");
8
+ class PromisedWebSockets {
9
+ constructor() {
10
+ this.client = undefined;
11
+ this.stream = Buffer.alloc(0);
12
+ this.closed = true;
13
+ }
14
+ async readExactly(number) {
15
+ let readData = Buffer.alloc(0);
16
+ while (true) {
17
+ const thisTime = await this.read(number);
18
+ readData = Buffer.concat([readData, thisTime]);
19
+ number = number - thisTime.length;
20
+ if (!number) {
21
+ return readData;
22
+ }
23
+ }
24
+ }
25
+ async read(number) {
26
+ if (this.closed) {
27
+ throw closeError;
28
+ }
29
+ await this.canRead;
30
+ if (this.closed) {
31
+ throw closeError;
32
+ }
33
+ const toReturn = this.stream.slice(0, number);
34
+ this.stream = this.stream.slice(number);
35
+ if (this.stream.length === 0) {
36
+ this.canRead = new Promise((resolve) => {
37
+ this.resolveRead = resolve;
38
+ });
39
+ }
40
+ return toReturn;
41
+ }
42
+ async readAll() {
43
+ if (this.closed || !(await this.canRead)) {
44
+ throw closeError;
45
+ }
46
+ const toReturn = this.stream;
47
+ this.stream = Buffer.alloc(0);
48
+ this.canRead = new Promise((resolve) => {
49
+ this.resolveRead = resolve;
50
+ });
51
+ return toReturn;
52
+ }
53
+ getWebSocketLink(ip, port, testServers) {
54
+ if (port === 443) {
55
+ return `wss://${ip}:${port}/apiws${testServers ? "_test" : ""}`;
56
+ }
57
+ else {
58
+ return `ws://${ip}:${port}/apiws${testServers ? "_test" : ""}`;
59
+ }
60
+ }
61
+ async connect(port, ip, testServers = false) {
62
+ this.stream = Buffer.alloc(0);
63
+ this.canRead = new Promise((resolve) => {
64
+ this.resolveRead = resolve;
65
+ });
66
+ this.closed = false;
67
+ this.website = this.getWebSocketLink(ip, port, testServers);
68
+ this.client = new websocket_1.w3cwebsocket(this.website, "binary");
69
+ return new Promise((resolve, reject) => {
70
+ if (this.client) {
71
+ this.client.onopen = () => {
72
+ this.receive();
73
+ resolve(this);
74
+ };
75
+ this.client.onerror = (error) => {
76
+ reject(error);
77
+ };
78
+ this.client.onclose = () => {
79
+ if (this.resolveRead) {
80
+ this.resolveRead(false);
81
+ }
82
+ this.closed = true;
83
+ };
84
+ }
85
+ });
86
+ }
87
+ write(data) {
88
+ if (this.closed) {
89
+ throw closeError;
90
+ }
91
+ if (this.client) {
92
+ this.client.send(data);
93
+ }
94
+ }
95
+ async close() {
96
+ if (this.client) {
97
+ await this.client.close();
98
+ }
99
+ this.closed = true;
100
+ }
101
+ async receive() {
102
+ if (this.client) {
103
+ this.client.onmessage = async (message) => {
104
+ const release = await mutex.acquire();
105
+ try {
106
+ let data;
107
+ //CONTEST BROWSER
108
+ data = Buffer.from(await new Response(message.data).arrayBuffer());
109
+ this.stream = Buffer.concat([this.stream, data]);
110
+ if (this.resolveRead) {
111
+ this.resolveRead(true);
112
+ }
113
+ }
114
+ finally {
115
+ release();
116
+ }
117
+ };
118
+ }
119
+ }
120
+ toString() {
121
+ return "PromisedWebSocket";
122
+ }
123
+ }
124
+ exports.PromisedWebSockets = PromisedWebSockets;
@@ -0,0 +1,5 @@
1
+ import { Api } from "../tl";
2
+ export declare class HTMLParser {
3
+ static parse(html: string): [string, Api.TypeMessageEntity[]];
4
+ static unparse(text: string, entities: Api.TypeMessageEntity[] | undefined, _offset?: number, _length?: number): string;
5
+ }
@@ -0,0 +1,213 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HTMLParser = void 0;
4
+ const htmlparser2_1 = require("htmlparser2");
5
+ const tl_1 = require("../tl");
6
+ const index_1 = require("../index");
7
+ class HTMLToTelegramParser {
8
+ constructor() {
9
+ this.text = "";
10
+ this.entities = [];
11
+ this._buildingEntities = new Map();
12
+ this._openTags = [];
13
+ this._openTagsMeta = [];
14
+ }
15
+ onopentag(name, attributes) {
16
+ /*
17
+ * This fires when a new tag is opened.
18
+ *
19
+ * If you don't need an aggregated `attributes` object,
20
+ * have a look at the `onopentagname` and `onattribute` events.
21
+ */
22
+ this._openTags.unshift(name);
23
+ this._openTagsMeta.unshift(undefined);
24
+ let EntityType;
25
+ const args = {};
26
+ if (name == "strong" || name == "b") {
27
+ EntityType = tl_1.Api.MessageEntityBold;
28
+ }
29
+ else if (name == "spoiler") {
30
+ EntityType = tl_1.Api.MessageEntitySpoiler;
31
+ }
32
+ else if (name == "em" || name == "i") {
33
+ EntityType = tl_1.Api.MessageEntityItalic;
34
+ }
35
+ else if (name == "u") {
36
+ EntityType = tl_1.Api.MessageEntityUnderline;
37
+ }
38
+ else if (name == "del" || name == "s") {
39
+ EntityType = tl_1.Api.MessageEntityStrike;
40
+ }
41
+ else if (name == "blockquote") {
42
+ EntityType = tl_1.Api.MessageEntityBlockquote;
43
+ if (attributes.expandable !== undefined) {
44
+ args.collapsed = true;
45
+ }
46
+ }
47
+ else if (name == "code") {
48
+ const pre = this._buildingEntities.get("pre");
49
+ if (pre && pre instanceof tl_1.Api.MessageEntityPre) {
50
+ try {
51
+ pre.language = attributes.class.slice("language-".length, attributes.class.length);
52
+ }
53
+ catch (e) {
54
+ // no language block
55
+ }
56
+ }
57
+ else {
58
+ EntityType = tl_1.Api.MessageEntityCode;
59
+ }
60
+ }
61
+ else if (name == "pre") {
62
+ EntityType = tl_1.Api.MessageEntityPre;
63
+ args["language"] = "";
64
+ }
65
+ else if (name == "a") {
66
+ let url = attributes.href;
67
+ if (!url) {
68
+ return;
69
+ }
70
+ if (url.startsWith("mailto:")) {
71
+ url = url.slice("mailto:".length, url.length);
72
+ EntityType = tl_1.Api.MessageEntityEmail;
73
+ }
74
+ else {
75
+ EntityType = tl_1.Api.MessageEntityTextUrl;
76
+ args["url"] = url;
77
+ url = undefined;
78
+ }
79
+ this._openTagsMeta.shift();
80
+ this._openTagsMeta.unshift(url);
81
+ }
82
+ else if (name == "tg-emoji") {
83
+ EntityType = tl_1.Api.MessageEntityCustomEmoji;
84
+ args["documentId"] = attributes["emoji-id"];
85
+ }
86
+ if (EntityType && !this._buildingEntities.has(name)) {
87
+ this._buildingEntities.set(name, new EntityType(Object.assign({ offset: this.text.length, length: 0 }, args)));
88
+ }
89
+ }
90
+ ontext(text) {
91
+ const previousTag = this._openTags.length > 0 ? this._openTags[0] : "";
92
+ if (previousTag == "a") {
93
+ const url = this._openTagsMeta[0];
94
+ if (url) {
95
+ text = url;
96
+ }
97
+ }
98
+ for (let [tag, entity] of this._buildingEntities) {
99
+ entity.length += text.length;
100
+ }
101
+ this.text += text;
102
+ }
103
+ onclosetag(tagname) {
104
+ this._openTagsMeta.shift();
105
+ this._openTags.shift();
106
+ const entity = this._buildingEntities.get(tagname);
107
+ if (entity) {
108
+ this._buildingEntities.delete(tagname);
109
+ this.entities.push(entity);
110
+ }
111
+ }
112
+ onattribute(name, value, quote) { }
113
+ oncdataend() { }
114
+ oncdatastart() { }
115
+ oncomment(data) { }
116
+ oncommentend() { }
117
+ onend() { }
118
+ onerror(error) { }
119
+ onopentagname(name) { }
120
+ onparserinit(parser) { }
121
+ onprocessinginstruction(name, data) { }
122
+ onreset() { }
123
+ }
124
+ class HTMLParser {
125
+ static parse(html) {
126
+ if (!html) {
127
+ return [html, []];
128
+ }
129
+ const handler = new HTMLToTelegramParser();
130
+ const parser = new htmlparser2_1.Parser(handler);
131
+ parser.write(html);
132
+ parser.end();
133
+ const text = index_1.helpers.stripText(handler.text, handler.entities);
134
+ return [text, handler.entities];
135
+ }
136
+ static unparse(text, entities, _offset = 0, _length) {
137
+ if (!text || !entities || !entities.length) {
138
+ return text;
139
+ }
140
+ if (_length == undefined) {
141
+ _length = text.length;
142
+ }
143
+ const html = [];
144
+ let lastOffset = 0;
145
+ for (let i = 0; i < entities.length; i++) {
146
+ const entity = entities[i];
147
+ if (entity.offset >= _offset + _length) {
148
+ break;
149
+ }
150
+ let relativeOffset = entity.offset - _offset;
151
+ if (relativeOffset > lastOffset) {
152
+ html.push(text.slice(lastOffset, relativeOffset));
153
+ }
154
+ else if (relativeOffset < lastOffset) {
155
+ continue;
156
+ }
157
+ let skipEntity = false;
158
+ let length = entity.length;
159
+ let entityText = this.unparse(text.slice(relativeOffset, relativeOffset + length), entities.slice(i + 1, entities.length), entity.offset, length);
160
+ if (entity instanceof tl_1.Api.MessageEntityBold) {
161
+ html.push(`<strong>${entityText}</strong>`);
162
+ }
163
+ else if (entity instanceof tl_1.Api.MessageEntitySpoiler) {
164
+ html.push(`<spoiler>${entityText}</spoiler>`);
165
+ }
166
+ else if (entity instanceof tl_1.Api.MessageEntityItalic) {
167
+ html.push(`<em>${entityText}</em>`);
168
+ }
169
+ else if (entity instanceof tl_1.Api.MessageEntityCode) {
170
+ html.push(`<code>${entityText}</code>`);
171
+ }
172
+ else if (entity instanceof tl_1.Api.MessageEntityUnderline) {
173
+ html.push(`<u>${entityText}</u>`);
174
+ }
175
+ else if (entity instanceof tl_1.Api.MessageEntityStrike) {
176
+ html.push(`<del>${entityText}</del>`);
177
+ }
178
+ else if (entity instanceof tl_1.Api.MessageEntityBlockquote) {
179
+ html.push(`<blockquote>${entityText}</blockquote>`);
180
+ }
181
+ else if (entity instanceof tl_1.Api.MessageEntityPre) {
182
+ if (entity.language) {
183
+ html.push(`<pre><code class="language-${entity.language}">${entityText}</code></pre>`);
184
+ }
185
+ else {
186
+ html.push(`<pre>${entityText}</pre>`);
187
+ }
188
+ }
189
+ else if (entity instanceof tl_1.Api.MessageEntityEmail) {
190
+ html.push(`<a href="mailto:${entityText}">${entityText}</a>`);
191
+ }
192
+ else if (entity instanceof tl_1.Api.MessageEntityUrl) {
193
+ html.push(`<a href="${entityText}">${entityText}</a>`);
194
+ }
195
+ else if (entity instanceof tl_1.Api.MessageEntityTextUrl) {
196
+ html.push(`<a href="${entity.url}">${entityText}</a>`);
197
+ }
198
+ else if (entity instanceof tl_1.Api.MessageEntityMentionName) {
199
+ html.push(`<a href="tg://user?id=${entity.userId}">${entityText}</a>`);
200
+ }
201
+ else if (entity instanceof tl_1.Api.MessageEntityCustomEmoji) {
202
+ html.push(`<tg-emoji emoji-id="${entity.documentId}">${entityText}</tg-emoji>`);
203
+ }
204
+ else {
205
+ skipEntity = true;
206
+ }
207
+ lastOffset = relativeOffset + (skipEntity ? 0 : length);
208
+ }
209
+ html.push(text.slice(lastOffset, text.length));
210
+ return html.join("");
211
+ }
212
+ }
213
+ exports.HTMLParser = HTMLParser;
@@ -0,0 +1,7 @@
1
+ export { Logger } from "./Logger";
2
+ export { BinaryWriter } from "./BinaryWriter";
3
+ export { BinaryReader } from "./BinaryReader";
4
+ export { PromisedWebSockets } from "./PromisedWebSockets";
5
+ export { PromisedNetSockets } from "./PromisedNetSockets";
6
+ export { MessagePacker } from "./MessagePacker";
7
+ export { AsyncQueue } from "./AsyncQueue";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AsyncQueue = exports.MessagePacker = exports.PromisedNetSockets = exports.PromisedWebSockets = exports.BinaryReader = exports.BinaryWriter = exports.Logger = void 0;
4
+ var Logger_1 = require("./Logger");
5
+ Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return Logger_1.Logger; } });
6
+ var BinaryWriter_1 = require("./BinaryWriter");
7
+ Object.defineProperty(exports, "BinaryWriter", { enumerable: true, get: function () { return BinaryWriter_1.BinaryWriter; } });
8
+ var BinaryReader_1 = require("./BinaryReader");
9
+ Object.defineProperty(exports, "BinaryReader", { enumerable: true, get: function () { return BinaryReader_1.BinaryReader; } });
10
+ var PromisedWebSockets_1 = require("./PromisedWebSockets");
11
+ Object.defineProperty(exports, "PromisedWebSockets", { enumerable: true, get: function () { return PromisedWebSockets_1.PromisedWebSockets; } });
12
+ var PromisedNetSockets_1 = require("./PromisedNetSockets");
13
+ Object.defineProperty(exports, "PromisedNetSockets", { enumerable: true, get: function () { return PromisedNetSockets_1.PromisedNetSockets; } });
14
+ var MessagePacker_1 = require("./MessagePacker");
15
+ Object.defineProperty(exports, "MessagePacker", { enumerable: true, get: function () { return MessagePacker_1.MessagePacker; } });
16
+ var AsyncQueue_1 = require("./AsyncQueue");
17
+ Object.defineProperty(exports, "AsyncQueue", { enumerable: true, get: function () { return AsyncQueue_1.AsyncQueue; } });
@@ -0,0 +1,5 @@
1
+ import { Api } from "../tl";
2
+ export declare class MarkdownParser {
3
+ static parse(message: string): [string, Api.TypeMessageEntity[]];
4
+ static unparse(text: string, entities: Api.TypeMessageEntity[] | undefined): string;
5
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MarkdownParser = void 0;
4
+ const messageParse_1 = require("../client/messageParse");
5
+ class MarkdownParser {
6
+ // TODO maybe there is a better way :shrug:
7
+ static parse(message) {
8
+ let i = 0;
9
+ const keys = {};
10
+ for (const k in messageParse_1.DEFAULT_DELIMITERS) {
11
+ keys[k] = false;
12
+ }
13
+ const entities = [];
14
+ const tempEntities = {};
15
+ while (i < message.length) {
16
+ let foundIndex = -1;
17
+ let foundDelim = undefined;
18
+ for (const key of Object.keys(messageParse_1.DEFAULT_DELIMITERS)) {
19
+ const index = message.indexOf(key, i);
20
+ if (index > -1 && (foundIndex === -1 || index < foundIndex)) {
21
+ foundIndex = index;
22
+ foundDelim = key;
23
+ }
24
+ }
25
+ if (foundIndex === -1 || foundDelim == undefined) {
26
+ break;
27
+ }
28
+ if (!keys[foundDelim]) {
29
+ tempEntities[foundDelim] = new messageParse_1.DEFAULT_DELIMITERS[foundDelim]({
30
+ offset: foundIndex,
31
+ length: -1,
32
+ language: "",
33
+ });
34
+ keys[foundDelim] = true;
35
+ }
36
+ else {
37
+ keys[foundDelim] = false;
38
+ tempEntities[foundDelim].length =
39
+ foundIndex - tempEntities[foundDelim].offset;
40
+ entities.push(tempEntities[foundDelim]);
41
+ }
42
+ message = message.toString().replace(foundDelim, "");
43
+ i = foundIndex;
44
+ }
45
+ return [message, entities];
46
+ }
47
+ static unparse(text, entities) {
48
+ const delimiters = messageParse_1.DEFAULT_DELIMITERS;
49
+ if (!text || !entities) {
50
+ return text;
51
+ }
52
+ let insertAt = [];
53
+ const tempDelimiters = new Map();
54
+ Object.keys(delimiters).forEach((key) => {
55
+ tempDelimiters.set(delimiters[key].className, key);
56
+ });
57
+ for (const entity of entities) {
58
+ const s = entity.offset;
59
+ const e = entity.offset + entity.length;
60
+ const delimiter = tempDelimiters.get(entity.className);
61
+ if (delimiter) {
62
+ insertAt.push([s, delimiter]);
63
+ insertAt.push([e, delimiter]);
64
+ }
65
+ }
66
+ insertAt = insertAt.sort((a, b) => {
67
+ return a[0] - b[0];
68
+ });
69
+ while (insertAt.length) {
70
+ const [at, what] = insertAt.pop();
71
+ text = text.slice(0, at) + what + text.slice(at);
72
+ }
73
+ return text;
74
+ }
75
+ }
76
+ exports.MarkdownParser = MarkdownParser;
@@ -0,0 +1,5 @@
1
+ import { Api } from "../tl";
2
+ export declare class MarkdownV2Parser {
3
+ static parse(message: string): [string, Api.TypeMessageEntity[]];
4
+ static unparse(text: string, entities: Api.TypeMessageEntity[] | undefined): string;
5
+ }