@@ -102,12 +102,14 @@ async def process_event_message(mc, ev, json_output, end="\n", above=False):
102102 if (data ['type' ] == "PRIV" ) :
103103 ct = mc .get_contact_by_key_prefix (data ['pubkey_prefix' ])
104104 if ct is None :
105- logger .info (f"Unknown contact with pubkey prefix: { data ['pubkey_prefix' ]} " )
105+ logger .debug (f"Unknown contact with pubkey prefix: { data ['pubkey_prefix' ]} " )
106106 name = data ["pubkey_prefix" ]
107107 else :
108108 name = ct ["adv_name" ]
109109
110- if ct ["type" ] == 3 : # room
110+ if ct is None : # Unknown
111+ disp = f"{ ANSI_RED } "
112+ elif ct ["type" ] == 3 : # room
111113 disp = f"{ ANSI_CYAN } "
112114 elif ct ["type" ] == 2 : # repeater
113115 disp = f"{ ANSI_MAGENTA } "
@@ -136,8 +138,11 @@ async def process_event_message(mc, ev, json_output, end="\n", above=False):
136138 print (disp )
137139
138140 elif (data ['type' ] == "CHAN" ) :
139- path_str = f" { ANSI_YELLOW } ({ path_str } ){ ANSI_END } "
140- disp = f"{ ANSI_GREEN } ch{ data ['channel_idx' ]} { path_str } "
141+ path_str = f"{ ANSI_YELLOW } ({ path_str } ){ ANSI_END } "
142+ if data ["channel_idx" ] == 0 : #public
143+ disp = f"{ ANSI_GREEN } public { path_str } "
144+ else :
145+ disp = f"{ ANSI_GREEN } ch{ data ['channel_idx' ]} { path_str } "
141146 disp = disp + f"{ ANSI_END } "
142147 disp = disp + f": { data ['text' ]} "
143148
@@ -171,6 +176,8 @@ def make_completion_dict(contacts):
171176
172177 completion_list = {
173178 "to" : contact_list ,
179+ "to_ch" : None ,
180+ "to_public" : None ,
174181 "send" : None ,
175182 "cmd" : None ,
176183 "public" : None ,
@@ -235,10 +242,12 @@ async def interactive_loop(mc, to=None) :
235242 await mc .ensure_contacts ()
236243 handle_message .above = True
237244 await subscribe_to_msgs (mc )
238- if to is None :
239- contact = next (iter (mc .contacts .items ()))[1 ]
240- else :
245+ if not to is None :
241246 contact = to
247+ elif len (mc .contacts .items ()) == 0 :
248+ contact = {"adv_name" : "public" , "type" : 0 , "chan_nb" : 0 }
249+ else :
250+ contact = next (iter (mc .contacts .items ()))[1 ]
242251
243252 try :
244253 while True : # purge msgs
@@ -283,6 +292,8 @@ def _(event):
283292 prompt = prompt + f"{ ANSI_BCYAN } "
284293 elif contact ["type" ] == 2 :
285294 prompt = prompt + f"{ ANSI_BMAGENTA } "
295+ elif contact ["type" ] == 0 : # public channel
296+ prompt = prompt + f"{ ANSI_BGREEN } "
286297 else :
287298 prompt = prompt + f"{ ANSI_BBLUE } "
288299 # some possible symbols 🭬🬛🬗🭬🬛🬃🬗🭬🬛🬃🬗🬏🭀🭋🭨🮋
@@ -339,19 +350,19 @@ def _(event):
339350 await process_cmds (mc , args )
340351
341352 # commands that take contact as second arg will be sent to recipient
342- elif line == "sc" or line == "share_contact" or \
353+ elif contact [ "type" ] > 0 and ( line == "sc" or line == "share_contact" or \
343354 line == "ec" or line == "export_contact" or \
344355 line == "uc" or line == "upload_contact" or \
345356 line == "rp" or line == "reset_path" or \
346357 line == "contact_info" or line == "ci" or \
347358 line == "path" or \
348- line == "logout" :
359+ line == "logout" ) :
349360 args = [line , contact ['adv_name' ]]
350361 await process_cmds (mc , args )
351362
352363 # same but for commands with a parameter
353- elif line .startswith ("cmd " ) or \
354- line .startswith ("cp " ) or line .startswith ("change_path " ) :
364+ elif contact [ "type" ] > 0 and ( line .startswith ("cmd " ) or \
365+ line .startswith ("cp " ) or line .startswith ("change_path " )) :
355366 cmds = line .split (" " , 1 )
356367 args = [cmds [0 ], contact ['adv_name' ], cmds [1 ]]
357368 await process_cmds (mc , args )
@@ -382,10 +393,27 @@ def _(event):
382393 dest = line [3 :]
383394 nc = mc .get_contact_by_name (dest )
384395 if nc is None :
385- print (f"Contact '{ dest } ' not found in contacts." )
396+ if dest == "public" :
397+ contact = {"adv_name" : "public" , "type" : 0 , "chan_nb" : 0 }
398+ elif dest .startswith ("ch" ):
399+ dest = int (dest [2 :])
400+ contact = {"adv_name" : "chan" + str (dest ), "type" : 0 , "chan_nb" : dest }
401+ else :
402+ print (f"Contact '{ dest } ' not found in contacts." )
386403 else :
387404 contact = nc
388405
406+ elif line .startswith ("to_ch" ) :
407+ last_ack = True
408+ dest = int (line .split (" " )[1 ])
409+ if dest == 0 :
410+ contact = {"adv_name" : "public" , "type" : 0 , "chan_nb" : 0 }
411+ else :
412+ contact = {"adv_name" : "chan" + str (dest ), "type" : 0 , "chan_nb" : dest }
413+
414+ elif line == "to_public" or line == "to_pub" :
415+ contact = {"adv_name" : "public" , "type" : 0 , "chan_nb" : 0 }
416+
389417 elif line == "to" :
390418 print (contact ["adv_name" ])
391419
@@ -411,22 +439,21 @@ def _(event):
411439 print (f"{ c [1 ]['adv_name' ]} " , end = "" )
412440 print ("" )
413441
414- else :
442+ elif line . startswith ( "send" ) or line . startswith ( " \" " ) :
415443 if line .startswith ("send" ) :
416444 line = line [5 :]
417445 if line .startswith ("\" " ) :
418446 line = line [1 :]
419- result = await mc .commands .send_msg (contact , line )
420- if result .type == EventType .ERROR :
421- print (f"⚠️ Failed to send message: { result .payload } " )
422- continue
423-
424- exp_ack = result .payload ["expected_ack" ].hex ()
425- res = await mc .wait_for_event (EventType .ACK , attribute_filters = {"code" : exp_ack }, timeout = 5 )
426- if res is None :
427- last_ack = False
428- else :
429- last_ack = True
447+ last_ack = await msg_ack (mc , contact , line )
448+
449+ elif contact ["type" ] == 0 : # channel, send msg to channel
450+ await process_cmds (mc , ["chan" , str (contact ["chan_nb" ]), line ] )
451+
452+ elif contact ["type" ] == 1 : # chat, send to recipient and wait ack
453+ await msg_ack (mc , contact , line )
454+
455+ elif contact ["type" ] == 2 or contact ["type" ] == 3 : # repeater, send cmd
456+ await process_cmds (mc , ["cmd" , contact ["adv_name" ], line ])
430457
431458 except (EOFError , KeyboardInterrupt ):
432459 mc .stop ()
@@ -436,6 +463,19 @@ def _(event):
436463 print ("Exiting cli" )
437464interactive_loop .classic = False
438465
466+ async def msg_ack (mc , contact , msg ) :
467+ result = await mc .commands .send_msg (contact , msg )
468+ if result .type == EventType .ERROR :
469+ print (f"⚠️ Failed to send message: { result .payload } " )
470+ return False
471+
472+ exp_ack = result .payload ["expected_ack" ].hex ()
473+ res = await mc .wait_for_event (EventType .ACK , attribute_filters = {"code" : exp_ack }, timeout = 5 )
474+ if res is None :
475+ return False
476+
477+ return True
478+
439479async def next_cmd (mc , cmds , json_output = False ):
440480 """ process next command """
441481 try :
0 commit comments