--- ChivanetModcon/modcon	2025/01/31 19:41:00	1.4
+++ ChivanetModcon/modcon	2025/03/10 15:31:54	1.7
@@ -4,13 +4,23 @@
 # ChivaNet Moderator Console
 #  Copyright (C) 2025 Coherent Logic Development LLC
 #
-# $Id: modcon,v 1.4 2025/01/31 19:41:00 snw Exp $
+# $Id: modcon,v 1.7 2025/03/10 15:31:54 snw Exp $
 #
 # Author: Serena Willis <snw@coherent-logic.com>
 #
 # Licensed AGPL-3.0
 #
+#
 # $Log: modcon,v $
+# Revision 1.7  2025/03/10 15:31:54  snw
+# Test
+#
+# Revision 1.6  2025/02/04 18:55:12  snw
+# Updates
+#
+# Revision 1.5  2025/02/01 03:17:46  snw
+# Fix session list
+#
 # Revision 1.4  2025/01/31 19:41:00  snw
 # Move to a UNIX UI paradigm
 #
@@ -30,6 +40,7 @@ use JSON;
 use Data::Dumper;
 use Term::ReadKey;
 use Getopt::Long;
+use Text::Glob qw(match_glob);
 
 my $sso_account = '';
 my $apikey = '';
@@ -121,45 +132,52 @@ sub list_ras_sessions {
     my $hashref = decode_json($json);
     my $sessions = $hashref->{sessions};
     my $arrayref = $sessions->{sessions};
-    my $ct = 0;
+    my @result = ();
     
     foreach my $session (@{$arrayref}) {
-        print "$session->{screen_name}\n";
-
-        $ct = $ct + 1;
-
-        if($ct > $hchar - 2) {
-            print "ENTER to continue, Q to quit...";
-            my $resp = <STDIN>;
-            chomp($resp);
-            if($resp eq "Q") {
-                return;
-            }
-            elsif($resp eq "q") {
-                return;
-            }
-            $ct = 0;
-        }
+        push(@result, $session->{id});
     }
     
-    print "$sessions->{count} current sessions\n";    
+    return @result;
 }
 
 sub list_ras_screennames {
     my($id) = @_;
-    
-    $cnclient->GET("/chivanet/ras_screen_names?id=$id");
+
+    my @result = ();
+    $cnclient->GET("/chivanet/user_ras_screen_names?id=$id");
     my $json = $cnclient->responseContent();
     my $hashref = decode_json($json);
     my $arrayref = $hashref->{screen_names};
+
+    foreach my $item (@{$arrayref}) {
+        push(@result, $item->{screen_name});
+    }
+
     
-    return @{$arrayref};   
+    return @result;   
 }
 
 sub list_ras_users {
 
-    return ();
+    my @result = ();
     
+    $cnclient->GET("/chivanet/all_ras_screen_names");
+    my $json = $cnclient->responseContent();
+    my $hashref = decode_json($json);
+
+    if($hashref->{ok} == 0) {
+        print "RPC error\n";
+        return @result;
+    }
+    
+    my $arrayref = $hashref->{screen_names};
+
+    foreach my $entryref (@{$arrayref}) {
+        push(@result, $entryref->{id});
+    }
+
+    return @result;    
 }
 
 sub print_sso_user {
@@ -217,9 +235,7 @@ sub print_ras_user {
     }
     else {
         print "Screen Name       :  $id [$online]\n\n";
-    }
-    
-#    print Dumper($status);
+    }    
     
 }
 
@@ -242,6 +258,47 @@ sub send_im {
     }
 }
 
+sub ls {
+    my ($description, $match, @entries) = @_;
+
+    if($match eq "") {
+        $match = '*';
+    }
+
+    print "Directory of [$description] matching \'$match\':\n\n";
+        
+    my @sorted = sort(@entries);
+    my $maxlen = 0;
+    
+    foreach my $entry (@sorted) {
+        my $len = length($entry);
+        if ($len > $maxlen) {
+            $maxlen = $len;
+        }
+    }
+
+    $maxlen = $maxlen + 2;
+
+    my $ct = $#sorted + 1;
+    foreach my $entry (@sorted) {
+        if(match_glob($match, $entry)) {
+            if($col + $maxlen >= $wchar) {
+                print "$entry\n";
+                $col = 0;
+            }
+            else {
+                printf("%-$maxlen\s", $entry);
+                $col = $col + $maxlen;
+            }
+        }
+        else {
+            $ct = $ct - 1;
+        }
+    }
+    
+    print "\n\n$ct matching items in directory\n";
+}
+
 sub prompt {
     
     my $rawcmd = '';
@@ -269,7 +326,7 @@ sub prompt {
         
         my @cmd = split(' ', $rawcmd);
         
-        if ($cmd[0] eq "exit" || $cmd[0] eq "logout" || $cmd[0] eq "quit" || $cmd[0] eq "bye") {
+        if ($cmd[0] eq "exit" || $cmd[0] eq "logout" || $cmd[0] eq "quit" || $cmd[0] eq "bye" || $cmd[0] eq "lo") {
             return;
         }
         elsif ($cmd[0] eq "pwd") {
@@ -387,8 +444,8 @@ sub prompt {
                                     @path = @oldpath;
                                 }
                                 else {
-                                    $mode = "SSO";
-                                    @path = ('sso', $user);
+                                    $mode = "RAS";
+                                    @path = ('ras', $user);
                                 }
                             }
                             else {
@@ -402,8 +459,7 @@ sub prompt {
                         }
                     }
                 }                                         
-            } # if ..                                    
-            
+            } # if ..                                  
         }
         elsif ($cmd[0] eq "im") {
             if($mode eq "RAS") {
@@ -450,6 +506,33 @@ sub prompt {
                 print "field:  command invalid outside of ras directory\n";
             }
         }
+        elsif ($cmd[0] eq "sessions") {
+            if($mode ne "RAS") {
+                print "sessions:  must be in ras mode\n";
+            }
+            else {
+                if($cmd[1]) {
+                    ls "active RAS sessions", $cmd[1], list_ras_sessions();
+                }
+                else {
+                    ls "active RAS sessions", '*', list_ras_sessions();
+                }
+            }
+        }
+        elsif ($cmd[0] eq "lssn") {
+            if($mode ne "SSO" || $user eq "---") {
+                print "lssn:  must be in sso mode with a user selected\n";
+            }
+            else {
+                my @sns = list_ras_screennames($user);
+                if($cmd[1]) {
+                    ls "RAS screen names for $user", $match, @sns;
+                }
+                else {
+                    ls "RAS screen names for $user", '*', @sns;
+                }
+            }                    
+        }
         elsif ($cmd[0] eq "ls") {
             my @entries = ();
             
@@ -479,27 +562,12 @@ sub prompt {
                 my $col = 0;
                 my $pstr = join('/', @path);
                 my $pfin = "/$pstr";
-                my @sorted = sort(@entries);
-                my $maxlen = 0;
-                foreach my $entry (@sorted) {
-                    my $len = length($entry);
-                    if ($len > $maxlen) {
-                        $maxlen = $len;
-                    }
+                if($cmd[1]) {
+                    ls $pfin, $cmd[1], @entries;
+                }
+                else {
+                    ls $pfin, '*', @entries;
                 }
-                $maxlen = $maxlen + 2;
-                my $ct = $#sorted + 1;
-                foreach my $entry (@sorted) {
-                    if($col + $maxlen >= $wchar) {
-                        print "\n";
-                        $col = 0;
-                    }
-                    else {
-                        printf("%-$maxlen\s", $entry);
-                        $col = $col + $maxlen;
-                    }                    
-                }                
-                print "\n\t$ct items in $pfin\n";
             }
 
         }