Skip to content

Commit a56dbb9

Browse files
committed
add scope to commands
1 parent 56935e7 commit a56dbb9

File tree

1 file changed

+85
-33
lines changed

1 file changed

+85
-33
lines changed

src/meshcore_cli/meshcore_cli.py

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -710,13 +710,7 @@ async def interactive_loop(mc, to=None) :
710710
contact = to
711711
prev_contact = None
712712

713-
res = await mc.commands.set_flood_scope("0")
714-
if res is None or res.type == EventType.ERROR:
715-
scope = None
716-
prev_scope = None
717-
else:
718-
scope = "*"
719-
prev_scope = "*"
713+
scope = await set_scope(mc, "*")
720714

721715
await get_contacts(mc, anim=True)
722716
await get_channels(mc, anim=True)
@@ -756,6 +750,9 @@ def _(event):
756750

757751
last_ack = True
758752
while True:
753+
# reset scope (if changed)
754+
scope = await set_scope(mc, scope)
755+
759756
color = process_event_message.color
760757
classic = interactive_loop.classic or not color
761758
print_name = interactive_loop.print_name
@@ -834,6 +831,8 @@ def _(event):
834831
completer=completer,
835832
key_bindings=bindings)
836833

834+
line = line.strip()
835+
837836
if line == "" : # blank line
838837
pass
839838

@@ -848,24 +847,41 @@ def _(event):
848847
except ValueError:
849848
logger.error("Error parsing line {line[1:]}")
850849

851-
elif line.startswith("/scope") :
850+
elif line.startswith("/scope") or\
851+
line.startswith("scope") and contact is None:
852852
if not scope is None:
853853
prev_scope = scope
854-
newscope = line.split(" ", 1)[1]
855-
scope = await set_scope(mc, newscope)
854+
try:
855+
newscope = line.split(" ", 1)[1]
856+
scope = await set_scope(mc, newscope)
857+
except IndexError:
858+
print(scope)
859+
860+
elif contact is None and (line.startswith("apply_to ") or line.startswith("at ")) or\
861+
line.startswith("/apply_to ") or line.startswith("/at ") :
862+
try:
863+
await apply_command_to_contacts(mc, line.split(" ",2)[1], line.split(" ",2)[2])
864+
except IndexError:
865+
logger.error(f"Error with apply_to command parameters")
856866

857867
elif line.startswith("/") :
858868
path = line.split(" ", 1)[0]
859869
if path.count("/") == 1:
860870
args = line[1:].split(" ")
861-
tct = mc.get_contact_by_name(args[0])
871+
dest = args[0]
872+
dest_scope = None
873+
if "%" in dest :
874+
dest_scope = dest.split("%")[-1]
875+
dest = dest[:-len(dest_scope)-1]
876+
await set_scope (mc, dest_scope)
877+
tct = mc.get_contact_by_name(dest)
862878
if len(args)>1 and not tct is None: # a contact, send a message
863879
if tct["type"] == 1 or tct["type"] == 3: # client or room
864880
last_ack = await msg_ack(mc, tct, line.split(" ", 1)[1])
865881
else:
866882
print("Can only send msg to chan, client or room")
867883
else :
868-
ch = await get_channel_by_name(mc, args[0])
884+
ch = await get_channel_by_name(mc, dest)
869885
if len(args)>1 and not ch is None: # a channel, send message
870886
await send_chan_msg(mc, ch["channel_idx"], line.split(" ", 1)[1])
871887
else :
@@ -876,6 +892,11 @@ def _(event):
876892
else:
877893
cmdline = line[1:].split("/",1)[1]
878894
contact_name = path[1:].split("/",1)[0]
895+
dest_scope = None
896+
if "%" in contact_name:
897+
dest_scope = contact_name.split("%")[-1]
898+
contact_name = contact_name[:-len(dest_scope)-1]
899+
await set_scope (mc, dest_scope)
879900
tct = mc.get_contact_by_name(contact_name)
880901
if tct is None:
881902
print(f"{contact_name} is not a contact")
@@ -891,6 +912,10 @@ def _(event):
891912
dest = line[3:]
892913
if dest.startswith("\"") or dest.startswith("\'") : # if name starts with a quote
893914
dest = shlex.split(dest)[0] # use shlex.split to get contact name between quotes
915+
dest_scope = None
916+
if '%' in dest and scope!=None :
917+
dest_scope = dest.split("%")[-1]
918+
dest = dest[:-len(dest_scope)-1]
894919
nc = mc.get_contact_by_name(dest)
895920
if nc is None:
896921
if dest == "public" :
@@ -904,6 +929,8 @@ def _(event):
904929
nc["adv_name"] = mc.channels[dest]["channel_name"]
905930
elif dest == ".." : # previous recipient
906931
nc = prev_contact
932+
if dest_scope is None and not scope is None:
933+
dest_scope = prev_scope
907934
elif dest == "~" or dest == "/" or dest == mc.self_info['name']:
908935
nc = None
909936
elif dest == "!" :
@@ -921,6 +948,12 @@ def _(event):
921948
last_ack = True
922949
prev_contact = contact
923950
contact = nc
951+
if dest_scope is None:
952+
dest_scope = scope
953+
if not scope is None and dest_scope != scope:
954+
prev_scope = scope
955+
if not dest_scope is None:
956+
scope = await set_scope(mc, dest_scope)
924957

925958
elif line == "to" :
926959
if contact is None :
@@ -949,13 +982,6 @@ def _(event):
949982
if last_ack == False :
950983
contact = ln
951984

952-
elif contact is None and\
953-
(line.startswith("apply_to ") or line.startswith("at ")):
954-
try:
955-
await apply_command_to_contacts(mc, line.split(" ",2)[1], line.split(" ",2)[2])
956-
except IndexError:
957-
logger.error(f"Error with apply_to command parameters")
958-
959985
# commands are passed through if at root
960986
elif contact is None or line.startswith(".") :
961987
try:
@@ -1007,6 +1033,12 @@ async def process_contact_chat_line(mc, contact, line):
10071033
if contact["type"] == 0:
10081034
return False
10091035

1036+
# if one element in line (most cases) strip the scope and apply it
1037+
if not " " in line and "%" in line:
1038+
dest_scope = line.split("%")[-1]
1039+
line = line[:-len(dest_scope)-1]
1040+
await set_scope (mc, dest_scope)
1041+
10101042
if line.startswith(":") : # : will send a command to current recipient
10111043
args=["cmd", contact['adv_name'], line[1:]]
10121044
await process_cmds(mc, args)
@@ -1121,15 +1153,21 @@ async def process_contact_chat_line(mc, contact, line):
11211153
return True
11221154

11231155
# same but for commands with a parameter
1124-
if line.startswith("cmd ") or line.startswith("msg ") or\
1125-
line.startswith("cp ") or line.startswith("change_path ") or\
1126-
line.startswith("cf ") or line.startswith("change_flags ") or\
1127-
line.startswith("req_binary ") or\
1128-
line.startswith("login ") :
1156+
if " " in line:
11291157
cmds = line.split(" ", 1)
1130-
args = [cmds[0], contact['adv_name'], cmds[1]]
1131-
await process_cmds(mc, args)
1132-
return True
1158+
if "%" in cmds[0]:
1159+
dest_scope = cmds[0].split("%")[-1]
1160+
cmds[0] = cmds[0][:-len(dest_scope)-1]
1161+
await set_scope(mc, dest_scope)
1162+
1163+
if cmds[0] == "cmd" or cmds[0] == "msg" or\
1164+
cmds[0] == "cp" or cmds[0] == "change_path" or\
1165+
cmds[0] == "cf" or cmds[0] == "change_flags" or\
1166+
cmds[0] == "req_binary" or\
1167+
cmds[0] == "login" :
1168+
args = [cmds[0], contact['adv_name'], cmds[1]]
1169+
await process_cmds(mc, args)
1170+
return True
11331171

11341172
if line == "login": # use stored password or prompt for it
11351173
password_file = ""
@@ -1318,7 +1356,7 @@ async def send_msg (mc, contact, msg) :
13181356

13191357
async def msg_ack (mc, contact, msg) :
13201358
timeout = 0 if not 'timeout' in contact else contact['timeout']
1321-
res = await mc.commands.send_msg_with_retry(contact, msg,
1359+
res = await mc.commands.send_msg_with_retry(contact, msg,
13221360
max_attempts=msg_ack.max_attempts,
13231361
flood_after=msg_ack.flood_after,
13241362
max_flood_attempts=msg_ack.max_flood_attempts,
@@ -1339,12 +1377,26 @@ async def msg_ack (mc, contact, msg) :
13391377
msg_ack.max_flood_attempts=1
13401378

13411379
async def set_scope (mc, scope) :
1380+
if not set_scope.has_scope:
1381+
return None
1382+
13421383
if scope == "None" or scope == "0" or scope == "clear" or scope == "":
13431384
scope = "*"
1385+
1386+
if set_scope.current_scope == scope:
1387+
return scope
1388+
13441389
res = await mc.commands.set_flood_scope(scope)
13451390
if res is None or res.type == EventType.ERROR:
1391+
if not res is None and res.payload["error_code"] == 1: #unsupported
1392+
set_scope.has_scope = False
13461393
return None
1394+
1395+
set_scope.current_scope = scope
1396+
13471397
return scope
1398+
set_scope.has_scope = True
1399+
set_scope.current_scope = None
13481400

13491401
async def get_channel (mc, chan) :
13501402
if not chan.isnumeric():
@@ -2432,7 +2484,7 @@ async def next_cmd(mc, cmds, json_output=False):
24322484
types = 0xFF
24332485
else :
24342486
types = 0
2435-
if "rep" in cmds[1]:
2487+
if "rep" in cmds[1] or "rpt" in cmds[1]:
24362488
types = types | 4
24372489
if "cli" in cmds[1] or "comp" in cmds[1]:
24382490
types = types | 2
@@ -2472,13 +2524,13 @@ async def next_cmd(mc, cmds, json_output=False):
24722524
name = n["pubkey"][0:16]
24732525
type = f"t:{n['node_type']}"
24742526
if n['node_type'] == 1:
2475-
type = "cli"
2527+
type = "CLI"
24762528
elif n['node_type'] == 2:
2477-
type = "rep"
2529+
type = "REP"
24782530
elif n['node_type'] == 3:
2479-
type = "room"
2531+
type = "ROOM"
24802532
elif n['node_type'] == 4:
2481-
type = "sens"
2533+
type = "SENS"
24822534

24832535
print(f" {name:16} {type:>4} SNR: {n['SNR_in']:6,.2f}->{n['SNR']:6,.2f} RSSI: ->{n['RSSI']:4}")
24842536

0 commit comments

Comments
 (0)