@mtkruto/node 0.0.976 → 0.0.978

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.
@@ -23,7 +23,7 @@ export declare enum ParseMode {
23
23
  export interface AuthorizeUserParams<S = string> {
24
24
  phone: S | (() => MaybePromise<S>);
25
25
  code: S | (() => MaybePromise<S>);
26
- password: S | (() => MaybePromise<S>);
26
+ password: S | ((hint: string | null) => MaybePromise<S>);
27
27
  }
28
28
  export type UpdateHandler = null | ((client: Client, update: types.TypeUpdate) => MaybePromise<void>);
29
29
  export interface ClientParams {
@@ -32,7 +32,7 @@ export interface ClientParams {
32
32
  */
33
33
  parseMode?: ParseMode;
34
34
  /**
35
- * The transport provider to use. Defaults to `defaultTransportProvider`.
35
+ * The transport provider to use. Defaults to `webSocketTransportProvider`.
36
36
  */
37
37
  transportProvider?: TransportProvider;
38
38
  /**
@@ -59,7 +59,14 @@ export interface ClientParams {
59
59
  * The system_version parameter to be passed to initConnection when calling `authorize`.
60
60
  */
61
61
  systemVersion?: string;
62
+ /**
63
+ * MTProto public keys to use in the `[keyId, [key, exponent]][]` format. Don't set this unless you know what you are doing.
64
+ */
62
65
  publicKeys?: PublicKeys;
66
+ /**
67
+ * Whether to automatically call `start` with no parameters in the first `invoke` call.
68
+ */
69
+ autoStart?: boolean;
63
70
  }
64
71
  export interface ForwardMessagesParams {
65
72
  messageThreadId?: number;
@@ -88,6 +95,7 @@ export declare class Client extends ClientAbstract {
88
95
  readonly systemLangCode: string;
89
96
  readonly systemVersion: string;
90
97
  private readonly publicKeys?;
98
+ private readonly autoStart;
91
99
  /**
92
100
  * Constructs the client.
93
101
  *
@@ -138,6 +146,7 @@ export declare class Client extends ClientAbstract {
138
146
  start(params?: string | types.AuthExportedAuthorization | AuthorizeUserParams): Promise<void>;
139
147
  private receiveLoop;
140
148
  private pingLoop;
149
+ private autoStarted;
141
150
  private lastMsgId;
142
151
  /**
143
152
  * Invokes a function waiting and returning its reply if the second parameter is not `true`. Requires the client
@@ -187,4 +196,5 @@ export declare class Client extends ClientAbstract {
187
196
  [getStickerSetName](inputStickerSet: types.InputStickerSetID, hash?: number): Promise<string>;
188
197
  forwardMessages(from: number | string, to: number | string, messageIds: number[], params?: ForwardMessagesParams): Promise<Message[]>;
189
198
  forwardMessage(from: number | string, to: number | string, messageId: number, params?: ForwardMessagesParams): Promise<Message>;
199
+ getMe(): Promise<import("../types/1_user.js").User>;
190
200
  }
@@ -23,9 +23,9 @@ import { parseHtml } from "./0_html.js";
23
23
  import { checkPassword } from "./0_password.js";
24
24
  import { ClientAbstract } from "./1_client_abstract.js";
25
25
  import { ClientPlain } from "./2_client_plain.js";
26
- import { drop } from "../utilities/1_misc.js";
26
+ import { drop, mustPrompt, mustPromptOneOf } from "../utilities/1_misc.js";
27
27
  import { getChannelChatId, peerToChatId } from "./0_utilities.js";
28
- import { mustPrompt, mustPromptNumber } from "../utilities/1_misc.js";
28
+ import { constructUser } from "../types/1_user.js";
29
29
  const d = debug("Client");
30
30
  const dGap = debug("Client/recoverUpdateGap");
31
31
  const dGapC = debug("Client/recoverChannelUpdateGap");
@@ -160,12 +160,24 @@ export class Client extends ClientAbstract {
160
160
  writable: true,
161
161
  value: void 0
162
162
  });
163
+ Object.defineProperty(this, "autoStart", {
164
+ enumerable: true,
165
+ configurable: true,
166
+ writable: true,
167
+ value: void 0
168
+ });
163
169
  Object.defineProperty(this, "storageInited", {
164
170
  enumerable: true,
165
171
  configurable: true,
166
172
  writable: true,
167
173
  value: false
168
174
  });
175
+ Object.defineProperty(this, "autoStarted", {
176
+ enumerable: true,
177
+ configurable: true,
178
+ writable: true,
179
+ value: false
180
+ });
169
181
  Object.defineProperty(this, "lastMsgId", {
170
182
  enumerable: true,
171
183
  configurable: true,
@@ -198,6 +210,7 @@ export class Client extends ClientAbstract {
198
210
  this.systemLangCode = params?.systemLangCode ?? SYSTEM_LANG_CODE;
199
211
  this.systemVersion = params?.systemVersion ?? SYSTEM_VERSION;
200
212
  this.publicKeys = params?.publicKeys;
213
+ this.autoStart = params?.autoStart ?? true;
201
214
  }
202
215
  /**
203
216
  * Sets the DC and resets the auth key stored in the session provider
@@ -305,16 +318,12 @@ export class Client extends ClientAbstract {
305
318
  throw new Error("apiHash not set");
306
319
  }
307
320
  if (typeof params === "undefined") {
308
- const phoneNumberOrBotToken = mustPrompt("Enter a phone number or a bot token:");
309
- if (phoneNumberOrBotToken.startsWith("+")) {
310
- params = {
311
- phone: phoneNumberOrBotToken,
312
- code: () => String(mustPromptNumber("Enter the verification code:")),
313
- password: () => mustPrompt("Enter the account password:"),
314
- };
321
+ const loginType = mustPromptOneOf("Do you want to login as bot [b] or user [u]?", ["b", "u"]);
322
+ if (loginType == "b") {
323
+ params = mustPrompt("Bot token:");
315
324
  }
316
325
  else {
317
- params = phoneNumberOrBotToken;
326
+ params = { phone: () => mustPrompt("Phone number:"), code: () => mustPrompt("Verification code:"), password: () => mustPrompt(`Password:`) };
318
327
  }
319
328
  }
320
329
  dAuth("authorizing with %s", typeof params === "string" ? "bot token" : params instanceof types.AuthExportedAuthorization ? "exported authorization" : "AuthorizeUserParams");
@@ -339,7 +348,7 @@ export class Client extends ClientAbstract {
339
348
  const ap = await this.invoke(new functions.AccountGetPassword());
340
349
  if (ap.currentAlgo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
341
350
  try {
342
- const password = typeof params.password === "string" ? params.password : await params.password();
351
+ const password = typeof params.password === "string" ? params.password : await params.password(ap.hint ?? null);
343
352
  const input = await checkPassword(password, ap);
344
353
  await this.invoke(new functions.AuthCheckPassword({ password: input }));
345
354
  await this.storage.setAccountType("user");
@@ -393,7 +402,7 @@ export class Client extends ClientAbstract {
393
402
  apiId: this.apiId,
394
403
  apiHash: this.apiHash,
395
404
  phoneNumber,
396
- settings: new types.CodeSettings({}),
405
+ settings: new types.CodeSettings(),
397
406
  }));
398
407
  dAuth("verification code sent");
399
408
  if (sentCode instanceof types.AuthSentCode) {
@@ -585,7 +594,15 @@ export class Client extends ClientAbstract {
585
594
  }
586
595
  async invoke(function_, noWait) {
587
596
  if (!this.auth) {
588
- throw new Error("Not connected");
597
+ if (this.autoStart && !this.autoStarted) {
598
+ await this.start();
599
+ }
600
+ else {
601
+ throw new Error("Not connected");
602
+ }
603
+ }
604
+ if (!this.auth) {
605
+ UNREACHABLE();
589
606
  }
590
607
  let seqNo = this.state.seqNo * 2;
591
608
  if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
@@ -1313,4 +1330,11 @@ export class Client extends ClientAbstract {
1313
1330
  async forwardMessage(from, to, messageId, params) {
1314
1331
  return await this.forwardMessages(from, to, [messageId], params).then((v) => v[0]);
1315
1332
  }
1333
+ async getMe() {
1334
+ const users = await this.invoke(new functions.UsersGetUsers({ id: [new types.InputUserSelf()] }));
1335
+ if (users.length < 1) {
1336
+ UNREACHABLE();
1337
+ }
1338
+ return constructUser(users[0][as](types.User));
1339
+ }
1316
1340
  }
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
5
5
  export declare const VECTOR_CONSTRUCTOR = 481674261;
6
6
  export declare const INITIAL_DC: DC;
7
7
  export declare const LAYER = 160;
8
- export declare const APP_VERSION = "MTKruto 0.0.976";
8
+ export declare const APP_VERSION = "MTKruto 0.0.978";
9
9
  export declare const DEVICE_MODEL: string;
10
10
  export declare const LANG_CODE: string;
11
11
  export declare const LANG_PACK = "";
package/esm/constants.js CHANGED
@@ -54,7 +54,7 @@ export const PUBLIC_KEYS = Object.freeze([
54
54
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
55
55
  export const INITIAL_DC = "2-test";
56
56
  export const LAYER = 160;
57
- export const APP_VERSION = "MTKruto 0.0.976";
57
+ export const APP_VERSION = "MTKruto 0.0.978";
58
58
  // @ts-ignore: lib
59
59
  export const DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
60
60
  export const LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
@@ -1,3 +1,4 @@
1
1
  export declare function drop<T>(promise: Promise<T>): void;
2
2
  export declare function mustPrompt(message: string): string;
3
3
  export declare function mustPromptNumber(message: string): number;
4
+ export declare function mustPromptOneOf<T extends readonly string[]>(message: string, choices: T): T[number];
@@ -19,3 +19,10 @@ export function mustPromptNumber(message) {
19
19
  }
20
20
  return result;
21
21
  }
22
+ export function mustPromptOneOf(message, choices) {
23
+ let result = prompt(message);
24
+ while (result == null || !choices.includes(result)) {
25
+ result = prompt(message);
26
+ }
27
+ return result;
28
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/mod.js",
3
3
  "main": "./script/mod.js",
4
4
  "name": "@mtkruto/node",
5
- "version": "0.0.976",
5
+ "version": "0.0.978",
6
6
  "description": "MTKruto for Node.js",
7
7
  "author": "Roj <rojvv@icloud.com>",
8
8
  "license": "LGPL-3.0-or-later",
@@ -23,7 +23,7 @@ export declare enum ParseMode {
23
23
  export interface AuthorizeUserParams<S = string> {
24
24
  phone: S | (() => MaybePromise<S>);
25
25
  code: S | (() => MaybePromise<S>);
26
- password: S | (() => MaybePromise<S>);
26
+ password: S | ((hint: string | null) => MaybePromise<S>);
27
27
  }
28
28
  export type UpdateHandler = null | ((client: Client, update: types.TypeUpdate) => MaybePromise<void>);
29
29
  export interface ClientParams {
@@ -32,7 +32,7 @@ export interface ClientParams {
32
32
  */
33
33
  parseMode?: ParseMode;
34
34
  /**
35
- * The transport provider to use. Defaults to `defaultTransportProvider`.
35
+ * The transport provider to use. Defaults to `webSocketTransportProvider`.
36
36
  */
37
37
  transportProvider?: TransportProvider;
38
38
  /**
@@ -59,7 +59,14 @@ export interface ClientParams {
59
59
  * The system_version parameter to be passed to initConnection when calling `authorize`.
60
60
  */
61
61
  systemVersion?: string;
62
+ /**
63
+ * MTProto public keys to use in the `[keyId, [key, exponent]][]` format. Don't set this unless you know what you are doing.
64
+ */
62
65
  publicKeys?: PublicKeys;
66
+ /**
67
+ * Whether to automatically call `start` with no parameters in the first `invoke` call.
68
+ */
69
+ autoStart?: boolean;
63
70
  }
64
71
  export interface ForwardMessagesParams {
65
72
  messageThreadId?: number;
@@ -88,6 +95,7 @@ export declare class Client extends ClientAbstract {
88
95
  readonly systemLangCode: string;
89
96
  readonly systemVersion: string;
90
97
  private readonly publicKeys?;
98
+ private readonly autoStart;
91
99
  /**
92
100
  * Constructs the client.
93
101
  *
@@ -138,6 +146,7 @@ export declare class Client extends ClientAbstract {
138
146
  start(params?: string | types.AuthExportedAuthorization | AuthorizeUserParams): Promise<void>;
139
147
  private receiveLoop;
140
148
  private pingLoop;
149
+ private autoStarted;
141
150
  private lastMsgId;
142
151
  /**
143
152
  * Invokes a function waiting and returning its reply if the second parameter is not `true`. Requires the client
@@ -187,4 +196,5 @@ export declare class Client extends ClientAbstract {
187
196
  [getStickerSetName](inputStickerSet: types.InputStickerSetID, hash?: number): Promise<string>;
188
197
  forwardMessages(from: number | string, to: number | string, messageIds: number[], params?: ForwardMessagesParams): Promise<Message[]>;
189
198
  forwardMessage(from: number | string, to: number | string, messageId: number, params?: ForwardMessagesParams): Promise<Message>;
199
+ getMe(): Promise<import("../types/1_user.js").User>;
190
200
  }
@@ -51,7 +51,7 @@ const _1_client_abstract_js_1 = require("./1_client_abstract.js");
51
51
  const _2_client_plain_js_1 = require("./2_client_plain.js");
52
52
  const _1_misc_js_1 = require("../utilities/1_misc.js");
53
53
  const _0_utilities_js_1 = require("./0_utilities.js");
54
- const _1_misc_js_2 = require("../utilities/1_misc.js");
54
+ const _1_user_js_1 = require("../types/1_user.js");
55
55
  const d = (0, deps_js_1.debug)("Client");
56
56
  const dGap = (0, deps_js_1.debug)("Client/recoverUpdateGap");
57
57
  const dGapC = (0, deps_js_1.debug)("Client/recoverChannelUpdateGap");
@@ -186,12 +186,24 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
186
186
  writable: true,
187
187
  value: void 0
188
188
  });
189
+ Object.defineProperty(this, "autoStart", {
190
+ enumerable: true,
191
+ configurable: true,
192
+ writable: true,
193
+ value: void 0
194
+ });
189
195
  Object.defineProperty(this, "storageInited", {
190
196
  enumerable: true,
191
197
  configurable: true,
192
198
  writable: true,
193
199
  value: false
194
200
  });
201
+ Object.defineProperty(this, "autoStarted", {
202
+ enumerable: true,
203
+ configurable: true,
204
+ writable: true,
205
+ value: false
206
+ });
195
207
  Object.defineProperty(this, "lastMsgId", {
196
208
  enumerable: true,
197
209
  configurable: true,
@@ -224,6 +236,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
224
236
  this.systemLangCode = params?.systemLangCode ?? constants_js_1.SYSTEM_LANG_CODE;
225
237
  this.systemVersion = params?.systemVersion ?? constants_js_1.SYSTEM_VERSION;
226
238
  this.publicKeys = params?.publicKeys;
239
+ this.autoStart = params?.autoStart ?? true;
227
240
  }
228
241
  /**
229
242
  * Sets the DC and resets the auth key stored in the session provider
@@ -331,16 +344,12 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
331
344
  throw new Error("apiHash not set");
332
345
  }
333
346
  if (typeof params === "undefined") {
334
- const phoneNumberOrBotToken = (0, _1_misc_js_2.mustPrompt)("Enter a phone number or a bot token:");
335
- if (phoneNumberOrBotToken.startsWith("+")) {
336
- params = {
337
- phone: phoneNumberOrBotToken,
338
- code: () => String((0, _1_misc_js_2.mustPromptNumber)("Enter the verification code:")),
339
- password: () => (0, _1_misc_js_2.mustPrompt)("Enter the account password:"),
340
- };
347
+ const loginType = (0, _1_misc_js_1.mustPromptOneOf)("Do you want to login as bot [b] or user [u]?", ["b", "u"]);
348
+ if (loginType == "b") {
349
+ params = (0, _1_misc_js_1.mustPrompt)("Bot token:");
341
350
  }
342
351
  else {
343
- params = phoneNumberOrBotToken;
352
+ params = { phone: () => (0, _1_misc_js_1.mustPrompt)("Phone number:"), code: () => (0, _1_misc_js_1.mustPrompt)("Verification code:"), password: () => (0, _1_misc_js_1.mustPrompt)(`Password:`) };
344
353
  }
345
354
  }
346
355
  dAuth("authorizing with %s", typeof params === "string" ? "bot token" : params instanceof types.AuthExportedAuthorization ? "exported authorization" : "AuthorizeUserParams");
@@ -365,7 +374,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
365
374
  const ap = await this.invoke(new functions.AccountGetPassword());
366
375
  if (ap.currentAlgo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
367
376
  try {
368
- const password = typeof params.password === "string" ? params.password : await params.password();
377
+ const password = typeof params.password === "string" ? params.password : await params.password(ap.hint ?? null);
369
378
  const input = await (0, _0_password_js_1.checkPassword)(password, ap);
370
379
  await this.invoke(new functions.AuthCheckPassword({ password: input }));
371
380
  await this.storage.setAccountType("user");
@@ -419,7 +428,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
419
428
  apiId: this.apiId,
420
429
  apiHash: this.apiHash,
421
430
  phoneNumber,
422
- settings: new types.CodeSettings({}),
431
+ settings: new types.CodeSettings(),
423
432
  }));
424
433
  dAuth("verification code sent");
425
434
  if (sentCode instanceof types.AuthSentCode) {
@@ -611,7 +620,15 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
611
620
  }
612
621
  async invoke(function_, noWait) {
613
622
  if (!this.auth) {
614
- throw new Error("Not connected");
623
+ if (this.autoStart && !this.autoStarted) {
624
+ await this.start();
625
+ }
626
+ else {
627
+ throw new Error("Not connected");
628
+ }
629
+ }
630
+ if (!this.auth) {
631
+ (0, _0_control_js_1.UNREACHABLE)();
615
632
  }
616
633
  let seqNo = this.state.seqNo * 2;
617
634
  if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
@@ -1339,5 +1356,12 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1339
1356
  async forwardMessage(from, to, messageId, params) {
1340
1357
  return await this.forwardMessages(from, to, [messageId], params).then((v) => v[0]);
1341
1358
  }
1359
+ async getMe() {
1360
+ const users = await this.invoke(new functions.UsersGetUsers({ id: [new types.InputUserSelf()] }));
1361
+ if (users.length < 1) {
1362
+ (0, _0_control_js_1.UNREACHABLE)();
1363
+ }
1364
+ return (0, _1_user_js_1.constructUser)(users[0][_1_tl_object_js_1.as](types.User));
1365
+ }
1342
1366
  }
1343
1367
  exports.Client = Client;
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
5
5
  export declare const VECTOR_CONSTRUCTOR = 481674261;
6
6
  export declare const INITIAL_DC: DC;
7
7
  export declare const LAYER = 160;
8
- export declare const APP_VERSION = "MTKruto 0.0.976";
8
+ export declare const APP_VERSION = "MTKruto 0.0.978";
9
9
  export declare const DEVICE_MODEL: string;
10
10
  export declare const LANG_CODE: string;
11
11
  export declare const LANG_PACK = "";
@@ -80,7 +80,7 @@ exports.PUBLIC_KEYS = Object.freeze([
80
80
  exports.VECTOR_CONSTRUCTOR = 0x1CB5C415;
81
81
  exports.INITIAL_DC = "2-test";
82
82
  exports.LAYER = 160;
83
- exports.APP_VERSION = "MTKruto 0.0.976";
83
+ exports.APP_VERSION = "MTKruto 0.0.978";
84
84
  // @ts-ignore: lib
85
85
  exports.DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
86
86
  exports.LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
@@ -1,3 +1,4 @@
1
1
  export declare function drop<T>(promise: Promise<T>): void;
2
2
  export declare function mustPrompt(message: string): string;
3
3
  export declare function mustPromptNumber(message: string): number;
4
+ export declare function mustPromptOneOf<T extends readonly string[]>(message: string, choices: T): T[number];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mustPromptNumber = exports.mustPrompt = exports.drop = void 0;
3
+ exports.mustPromptOneOf = exports.mustPromptNumber = exports.mustPrompt = exports.drop = void 0;
4
4
  const _0_control_js_1 = require("./0_control.js");
5
5
  function drop(promise) {
6
6
  promise.then().catch();
@@ -25,3 +25,11 @@ function mustPromptNumber(message) {
25
25
  return result;
26
26
  }
27
27
  exports.mustPromptNumber = mustPromptNumber;
28
+ function mustPromptOneOf(message, choices) {
29
+ let result = prompt(message);
30
+ while (result == null || !choices.includes(result)) {
31
+ result = prompt(message);
32
+ }
33
+ return result;
34
+ }
35
+ exports.mustPromptOneOf = mustPromptOneOf;