deltabot-cli 7.2.0__tar.gz → 8.0.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deltabot-cli
3
- Version: 7.2.0
3
+ Version: 8.0.0
4
4
  Summary: Library to speedup Delta Chat bot development
5
5
  Author-email: adbenitez <adb@merlinux.eu>
6
6
  Project-URL: Homepage, https://github.com/deltachat-bot/deltabot-cli-py
@@ -8,7 +8,7 @@ import time
8
8
  from argparse import ArgumentParser, Namespace
9
9
  from pathlib import Path
10
10
  from threading import Thread
11
- from typing import Callable, Set, Union
11
+ from typing import Callable, List, Set, Union
12
12
 
13
13
  from appdirs import user_config_dir
14
14
  from deltachat2 import Bot, CoreEvent, Event, EventType, IOTransport, JsonRpcError, Rpc
@@ -108,8 +108,9 @@ class BotCli:
108
108
  self.add_generic_option(
109
109
  "--account",
110
110
  "-a",
111
- help="operate only over the given account when running any subcommand",
112
- metavar="ADDR",
111
+ help="operate only over the profile with the given ID when running any subcommand",
112
+ metavar="ID",
113
+ type=int,
113
114
  )
114
115
  choices = ["debug", "info", "warning", "error"]
115
116
  assert (
@@ -124,8 +125,18 @@ class BotCli:
124
125
  )
125
126
 
126
127
  init_parser = self.add_subcommand(_init_cmd, name="init")
127
- init_parser.add_argument("addr", help="the e-mail address to use")
128
- init_parser.add_argument("password", help="account password")
128
+ init_parser.add_argument(
129
+ "addr",
130
+ help=(
131
+ "the e-mail address to use or a DCACCOUNT URI ex."
132
+ " DCACCOUNT:https://nine.testrun.org/new"
133
+ ),
134
+ )
135
+ init_parser.add_argument(
136
+ "password",
137
+ nargs="?",
138
+ help="account password, this field is required only if addr is not a DCACCOUNT URI",
139
+ )
129
140
 
130
141
  config_parser = self.add_subcommand(_config_cmd, name="config")
131
142
  config_parser.add_argument("option", help="option name", nargs="?")
@@ -146,32 +157,6 @@ class BotCli:
146
157
  os.makedirs(args.config_dir)
147
158
  return os.path.join(args.config_dir, "accounts")
148
159
 
149
- def get_or_create_account(self, rpc: Rpc, addr: str) -> int:
150
- """Get account for address, if no account exists create a new one."""
151
- accid = self.get_account(rpc, addr)
152
- if not accid:
153
- accid = rpc.add_account()
154
- rpc.set_config(accid, "addr", addr)
155
- return accid
156
-
157
- def get_account(self, rpc: Rpc, addr: str) -> int:
158
- """Get account id for address.
159
- If no account exists with the given address, zero is returned.
160
- """
161
- try:
162
- return int(addr)
163
- except ValueError:
164
- for accid in rpc.get_all_account_ids():
165
- if addr == self.get_address(rpc, accid):
166
- return accid
167
-
168
- return 0
169
-
170
- def get_address(self, rpc: Rpc, accid: int) -> str:
171
- if rpc.is_configured(accid):
172
- return rpc.get_config(accid, "configured_addr")
173
- return rpc.get_config(accid, "addr")
174
-
175
160
  def is_admin(self, rpc: Rpc, accid: int, contactid: int) -> bool:
176
161
  """Return True if the contact is an administrator.
177
162
  Administrators are the members of the Administration group.
@@ -228,37 +213,41 @@ class BotCli:
228
213
  def _init_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None: # noqa: C901
229
214
  """initialize the account"""
230
215
 
231
- def configure() -> None:
232
- try:
233
- bot.configure(accid, email=args.addr, password=args.password)
234
- except JsonRpcError as err:
235
- bot.logger.error(err)
216
+ def process_events() -> None:
217
+ events = (EventType.INFO, EventType.WARNING, EventType.ERROR)
218
+ while True:
219
+ raw_event = bot.rpc.get_next_event()
220
+ accid = raw_event.context_id
221
+ event = CoreEvent(raw_event.event)
222
+ if event.kind == EventType.CONFIGURE_PROGRESS:
223
+ if event.comment:
224
+ bot.logger.info(event.comment)
225
+ pbar.set_progress(event.progress)
226
+ elif event.kind in events:
227
+ bot._on_event(Event(accid, event), RawEvent) # noqa: access to protected field
228
+ if pbar.progress in (-1, pbar.total):
229
+ break
236
230
 
237
231
  if args.account:
238
- accid = cli.get_account(bot.rpc, args.account)
239
- if not accid:
240
- bot.logger.error(f"unknown account: {args.account!r}")
241
- sys.exit(1)
232
+ accid = args.account
242
233
  else:
243
- accid = cli.get_or_create_account(bot.rpc, args.addr)
234
+ accid = bot.rpc.add_account()
244
235
 
245
236
  bot.logger.info("Starting configuration process...")
246
- task = Thread(target=configure, daemon=True)
247
- task.start()
237
+ bot.rpc.set_config(accid, "bot", "1")
248
238
  pbar = ConfigProgressBar()
249
- while True:
250
- raw_event = bot.rpc.get_next_event()
251
- accid = raw_event.context_id
252
- event = CoreEvent(raw_event.event)
253
- if event.kind == EventType.CONFIGURE_PROGRESS:
254
- if event.comment:
255
- bot.logger.info(event.comment)
256
- pbar.set_progress(event.progress)
257
- elif event.kind in (EventType.INFO, EventType.WARNING, EventType.ERROR):
258
- bot._on_event(Event(accid, event), RawEvent) # noqa: access to protected field
259
- if pbar.progress in (-1, pbar.total):
260
- break
261
- task.join()
239
+ task = Thread(target=process_events, daemon=True)
240
+ task.start()
241
+ try:
242
+ if not args.password:
243
+ bot.rpc.add_transport_from_qr(accid, args.addr)
244
+ else:
245
+ params = {"addr": args.addr, "password": args.password}
246
+ bot.rpc.add_or_update_transport(accid, params)
247
+ task.join()
248
+ except JsonRpcError as err:
249
+ bot.logger.error(err)
250
+ pbar.progress = -1
262
251
  pbar.close()
263
252
  if pbar.progress == -1:
264
253
  bot.logger.error("Configuration failed.")
@@ -267,20 +256,23 @@ def _init_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None: # noqa: C901
267
256
  bot.logger.info("Account configured successfully.")
268
257
 
269
258
 
259
+ def _get_addresses(rpc: Rpc, accid: int) -> List[str]:
260
+ transports = rpc.list_transports(accid)
261
+ return [params["addr"] for params in transports]
262
+
263
+
270
264
  def _serve_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None:
271
265
  """start processing messages"""
272
266
  rpc = bot.rpc
273
267
  if args.account:
274
- accounts = [cli.get_account(rpc, args.account)]
275
- if not accounts[0]:
276
- bot.logger.error(f"unknown account: {args.account!r}")
277
- sys.exit(1)
268
+ accounts = [args.account]
278
269
  else:
279
270
  accounts = rpc.get_all_account_ids()
280
271
  addrs = []
281
272
  for accid in accounts:
282
- if rpc.is_configured(accid):
283
- addrs.append(rpc.get_config(accid, "configured_addr"))
273
+ addrs2 = _get_addresses(bot.rpc, accid)
274
+ if addrs2:
275
+ addrs.extend(addrs2)
284
276
  else:
285
277
  bot.logger.error(f"account {accid} not configured")
286
278
  if len(addrs) != 0:
@@ -299,18 +291,14 @@ def _serve_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None:
299
291
  sys.exit(1)
300
292
 
301
293
 
302
- def _config_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None:
294
+ def _config_cmd(_cli: BotCli, bot: Bot, args: Namespace) -> None:
303
295
  """set/get account configuration values"""
304
296
  if args.account:
305
- accounts = [cli.get_account(bot.rpc, args.account)]
306
- if not accounts[0]:
307
- bot.logger.error(f"unknown account: {args.account!r}")
308
- sys.exit(1)
297
+ accounts = [args.account]
309
298
  else:
310
299
  accounts = bot.rpc.get_all_account_ids()
311
300
  for accid in accounts:
312
- addr = cli.get_address(bot.rpc, accid)
313
- print(f"Account #{accid} ({addr}):")
301
+ print(f"Account #{accid}:")
314
302
  _config_cmd_for_acc(bot, accid, args)
315
303
  print("")
316
304
  if not accounts:
@@ -339,15 +327,11 @@ def _admin_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None:
339
327
  """print the invitation link to the bot administration group.
340
328
  WARNING: don't share this, anyone joining will become admin of the bot"""
341
329
  if args.account:
342
- accounts = [cli.get_account(bot.rpc, args.account)]
343
- if not accounts[0]:
344
- bot.logger.error(f"unknown account: {args.account!r}")
345
- sys.exit(1)
330
+ accounts = [args.account]
346
331
  else:
347
332
  accounts = bot.rpc.get_all_account_ids()
348
333
  for accid in accounts:
349
- addr = cli.get_address(bot.rpc, accid)
350
- print(f"Account #{accid} ({addr}):")
334
+ print(f"Account #{accid}:")
351
335
  _admin_cmd_for_acc(cli, bot, accid)
352
336
  print("")
353
337
  if not accounts:
@@ -365,18 +349,14 @@ def _admin_cmd_for_acc(cli: BotCli, bot: Bot, accid: int) -> None:
365
349
  bot.logger.error("account not configured")
366
350
 
367
351
 
368
- def _link_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None:
352
+ def _link_cmd(_cli: BotCli, bot: Bot, args: Namespace) -> None:
369
353
  """print the bot's chat invitation link"""
370
354
  if args.account:
371
- accounts = [cli.get_account(bot.rpc, args.account)]
372
- if not accounts[0]:
373
- bot.logger.error(f"unknown account: {args.account!r}")
374
- sys.exit(1)
355
+ accounts = [args.account]
375
356
  else:
376
357
  accounts = bot.rpc.get_all_account_ids()
377
358
  for accid in accounts:
378
- addr = cli.get_address(bot.rpc, accid)
379
- print(f"Account #{accid} ({addr}):")
359
+ print(f"Account #{accid}:")
380
360
  _link_cmd_for_acc(bot, accid)
381
361
  print("")
382
362
  if not accounts:
@@ -393,38 +373,34 @@ def _link_cmd_for_acc(bot: Bot, accid: int) -> None:
393
373
  bot.logger.error("account not configured")
394
374
 
395
375
 
396
- def _list_cmd(cli: BotCli, bot: Bot, _args: Namespace) -> None:
376
+ def _list_cmd(_cli: BotCli, bot: Bot, _args: Namespace) -> None:
397
377
  """show a list of existing bot accounts"""
398
378
  rpc = bot.rpc
399
379
  accounts = rpc.get_all_account_ids()
400
380
  for accid in accounts:
401
- addr = cli.get_address(rpc, accid)
402
- if not rpc.is_configured(accid):
403
- addr = f"{addr or ''} (not configured)"
404
- print(f"#{accid} - {addr}")
381
+ addrs = _get_addresses(bot.rpc, accid)
382
+ info = ", ".join(addrs) or "(not configured)"
383
+ print(f"#{accid} - {info}")
405
384
 
406
385
 
407
- def _remove_cmd(cli: BotCli, bot: Bot, args: Namespace) -> None:
386
+ def _remove_cmd(_cli: BotCli, bot: Bot, args: Namespace) -> None:
408
387
  """remove Delta Chat accounts from the bot"""
409
388
  if args.account:
410
- accid = cli.get_account(bot.rpc, args.account)
411
- if not accid:
412
- bot.logger.error(f"unknown account: {args.account!r}")
413
- sys.exit(1)
389
+ accounts = [args.account]
414
390
  else:
415
391
  accounts = bot.rpc.get_all_account_ids()
416
- if len(accounts) == 1:
417
- accid = accounts[0]
418
- else:
419
- bot.logger.error(
420
- "There are more than one account, to remove one of them, pass the account"
421
- " address with -a/--account option"
422
- )
423
- sys.exit(1)
424
392
 
425
- addr = cli.get_address(bot.rpc, accid)
393
+ if len(accounts) == 1:
394
+ accid = accounts[0]
395
+ else:
396
+ bot.logger.error(
397
+ "There are more than one account, to remove one of them, pass the account"
398
+ " address with -a/--account option"
399
+ )
400
+ sys.exit(1)
401
+
426
402
  bot.rpc.remove_account(accid)
427
- print(f"Account #{accid} ({addr}) removed successfully.")
403
+ print(f"Account #{accid} removed successfully.")
428
404
 
429
405
 
430
406
  def _import_cmd(_cli: BotCli, bot: Bot, args: Namespace) -> None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deltabot-cli
3
- Version: 7.2.0
3
+ Version: 8.0.0
4
4
  Summary: Library to speedup Delta Chat bot development
5
5
  Author-email: adbenitez <adb@merlinux.eu>
6
6
  Project-URL: Homepage, https://github.com/deltachat-bot/deltabot-cli-py
File without changes
File without changes
File without changes
File without changes
File without changes