Skip to content

Commit dbcbcc1

Browse files
committed
password management
1 parent 00e5bf4 commit dbcbcc1

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/meshcore_cli/meshcore_cli.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
from prompt_toolkit.formatted_text import ANSI
2323
from prompt_toolkit.key_binding import KeyBindings
2424
from prompt_toolkit.shortcuts import radiolist_dialog
25-
26-
#from typing import Iterable, Mapping, Set, Union
27-
2825
from prompt_toolkit.completion.word_completer import WordCompleter
2926
from prompt_toolkit.document import Document
3027

@@ -316,7 +313,7 @@ def get_completions( self, document, complete_event):
316313
opts = []
317314
for k in self.options.keys():
318315
if k[0] == "/" :
319-
v = "/" + k.split("/")[1] + ("/" if k.count("/") == 2 else "")
316+
v = "/" + k.split("/")[1] #+ ("/" if k.count("/") == 2 else "")
320317
if v not in opts:
321318
opts.append(v)
322319
else:
@@ -478,6 +475,7 @@ def make_completion_dict(contacts, pending={}, to=None, channels=None):
478475
"change_flags" : None,
479476
"req_telemetry" : None,
480477
"req_binary" : None,
478+
"forget_password" : None,
481479
}
482480

483481
client_completion_list = dict(contact_completion_list)
@@ -623,7 +621,6 @@ async def interactive_loop(mc, to=None) :
623621
contact = to
624622
prev_contact = None
625623

626-
# await get_contacts(mc, anim=True)
627624
await get_contacts(mc, anim=True)
628625
await get_channels(mc, anim=True)
629626
await subscribe_to_msgs(mc, above=True)
@@ -889,6 +886,14 @@ async def process_contact_chat_line(mc, contact, line):
889886
await process_cmds(mc, args)
890887
return True
891888

889+
# special case for rp that can be chained from cmdline
890+
if line.startswith("rp ") or line.startswith("reset_path ") :
891+
args = ["rp", contact['adv_name']]
892+
await process_cmds(mc, args)
893+
secline = line.split(" ", 1)[1]
894+
await process_contact_chat_line(mc, contact, secline)
895+
return True
896+
892897
if line.startswith("set timeout "):
893898
cmds=line.split(" ")
894899
contact["timeout"] = float(cmds[2])
@@ -961,6 +966,38 @@ async def process_contact_chat_line(mc, contact, line):
961966
await process_cmds(mc, args)
962967
return True
963968

969+
if line == "login": # use stored password or prompt for it
970+
password_file = ""
971+
password = ""
972+
if os.path.isdir(MCCLI_CONFIG_DIR) :
973+
password_file = MCCLI_CONFIG_DIR + contact['adv_name'] + ".pass"
974+
if os.path.exists(password_file) :
975+
with open(password_file, "r", encoding="utf-8") as f :
976+
password=f.readline().strip()
977+
978+
if password == "":
979+
sess = PromptSession("Password: ", is_password=True)
980+
password = await sess.prompt_async()
981+
982+
if password_file != "":
983+
with open(password_file, "w", encoding="utf-8") as f :
984+
f.write(password)
985+
986+
args = ["login", contact['adv_name'], password]
987+
await process_cmds(mc, args)
988+
return True
989+
990+
if line.startswith("forget_password") or line.startswith("fp"):
991+
password_file = MCCLI_CONFIG_DIR + contact['adv_name'] + ".pass"
992+
if os.path.exists(password_file):
993+
os.remove(password_file)
994+
try:
995+
secline = line.split(" ", 1)[1]
996+
await process_contact_chat_line(mc, contact, secline)
997+
except IndexError:
998+
pass
999+
return True
1000+
9641001
if contact["type"] == 4 and \
9651002
(line.startswith("req_mma ") or line.startswith('rm ')) :
9661003
cmds = line.split(" ")
@@ -1089,6 +1126,9 @@ async def get_channel_by_name (mc, name):
10891126
return None
10901127

10911128
async def get_contacts (mc, anim=False, lastomod=0, timeout=5) :
1129+
if mc._contacts:
1130+
return
1131+
10921132
if anim:
10931133
print("Fetching contacts ", end="", flush=True)
10941134

0 commit comments

Comments
 (0)