More changes of 'free software' to 'open source' made for the express purpose of...
[citadel.git] / citadel / modules / rwho / serv_rwho.c
index d8a1285babaf72f612b672d2896d3fe81a161e36..23fec4bbf8b1a071f20d51c8152bb8332b14a53f 100644 (file)
@@ -1,9 +1,22 @@
 /*
- * $Id$
- *
- * This module implementsserver commands related to the display and
+ * This module implements server commands related to the display and
  * manipulation of the "Who's online" list.
  *
+ * Copyright (c) 1987-2012 by the citadel.org team
+ *
+ *  This program is open source software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 3.
+ *  
+ *  
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  
+ *  
+ *  
  */
 
 #include "sysdep.h"
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
 #include "control.h"
-#include "room_ops.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
-#include "tools.h"
 
 
 #include "ctdl_module.h"
@@ -51,7 +62,8 @@
  * display who's online
  */
 void cmd_rwho(char *argbuf) {
-       struct CitContext *cptr;
+       struct CitContext *nptr;
+       int nContexts, i;
        int spoofed = 0;
        int user_spoofed = 0;
        int room_spoofed = 0;
@@ -61,10 +73,24 @@ void cmd_rwho(char *argbuf) {
        char real_room[ROOMNAMELEN], room[ROOMNAMELEN];
        char host[64], flags[5];
        
-       aide = CC->user.axlevel >= 6;
+       /* So that we don't keep the context list locked for a long time
+        * we create a copy of it first
+        */
+       
+
+       nptr = CtdlGetContextArray(&nContexts) ;
+       if (!nptr)
+       {
+               /* Couldn't malloc so we have to bail but stick to the protocol */
+               cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
+               cprintf("000\n");
+               return;
+       }
+       
+       aide = CC->user.axlevel >= AxAideU;
        cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
        
-       for (cptr = ContextList; cptr != NULL; cptr = cptr->next
+       for (i=0; i<nContexts; i++
        {
                flags[0] = '\0';
                spoofed = 0;
@@ -72,33 +98,39 @@ void cmd_rwho(char *argbuf) {
                room_spoofed = 0;
                host_spoofed = 0;
                
-               if (cptr->cs_flags & CS_POSTING)
+               if (!aide && nptr[i].state == CON_SYS)
+                       continue;
+
+               if (!aide && nptr[i].kill_me != 0)
+                       continue;
+
+               if (nptr[i].cs_flags & CS_POSTING)
                   strcat(flags, "*");
                else
                   strcat(flags, ".");
                   
-               if (cptr->fake_username[0])
+               if (nptr[i].fake_username[0])
                {
-                  strcpy(un, cptr->fake_username);
+                  strcpy(un, nptr[i].fake_username);
                   spoofed = 1;
                   user_spoofed = 1;
                }
                else
-                  strcpy(un, cptr->curr_user);
+                  strcpy(un, nptr[i].curr_user);
                   
-               if (cptr->fake_hostname[0])
+               if (nptr[i].fake_hostname[0])
                {
-                  strcpy(host, cptr->fake_hostname);
+                  strcpy(host, nptr[i].fake_hostname);
                   spoofed = 1;
                   host_spoofed = 1;
                }
                else
-                  strcpy(host, cptr->cs_host);
+                  strcpy(host, nptr[i].cs_host);
 
-               GenerateRoomDisplay(real_room, cptr, CC);
+               GenerateRoomDisplay(real_room, &nptr[i], CC);
 
-               if (cptr->fake_roomname[0]) {
-                       strcpy(room, cptr->fake_roomname);
+               if (nptr[i].fake_roomname[0]) {
+                       strcpy(room, nptr[i].fake_roomname);
                        spoofed = 1;
                        room_spoofed = 1;
                }
@@ -110,21 +142,21 @@ void cmd_rwho(char *argbuf) {
                        strcat(flags, "+");
                }
                
-               if ((cptr->cs_flags & CS_STEALTH) && (aide)) {
+               if ((nptr[i].cs_flags & CS_STEALTH) && (aide)) {
                        strcat(flags, "-");
                }
                
-               if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
+               if (((nptr[i].cs_flags&CS_STEALTH)==0) || (aide))
                {
                        cprintf("%d|%s|%s|%s|%s|%ld|%s|%s|",
-                               cptr->cs_pid, un, room,
-                               host, cptr->cs_clientname,
-                               (long)(cptr->lastidle),
-                               cptr->lastcmdname, flags
+                               nptr[i].cs_pid, un, room,
+                               host, nptr[i].cs_clientname,
+                               (long)(nptr[i].lastidle),
+                               nptr[i].lastcmdname, flags
                        );
 
                        if ((user_spoofed) && (aide)) {
-                               cprintf("%s|", cptr->curr_user);
+                               cprintf("%s|", nptr[i].curr_user);
                        }
                        else {
                                cprintf("|");
@@ -138,15 +170,18 @@ void cmd_rwho(char *argbuf) {
                        }
        
                        if ((host_spoofed) && (aide)) {
-                               cprintf("%s|", cptr->cs_host);
+                               cprintf("%s|", nptr[i].cs_host);
                        }
                        else {
                                cprintf("|");
                        }
        
-                       cprintf("%d\n", cptr->logged_in);
+                       cprintf("%d\n", nptr[i].logged_in);
                }
        }
+       
+       /* release out copy of the context list */
+       free(nptr);
 
        /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
         * so that external paging modules such as serv_icq can add more
@@ -154,7 +189,7 @@ void cmd_rwho(char *argbuf) {
         */
        PerformSessionHooks(EVT_RWHO);
        cprintf("000\n");
-       }
+}
 
 
 /*
@@ -238,9 +273,11 @@ void cmd_stel(char *cmdbuf)
 
        if (requested_mode == 1) {
                CC->cs_flags = CC->cs_flags | CS_STEALTH;
+               PerformSessionHooks(EVT_STEALTH);
        }
        if (requested_mode == 0) {
                CC->cs_flags = CC->cs_flags & ~CS_STEALTH;
+               PerformSessionHooks(EVT_UNSTEALTH);
        }
 
        cprintf("%d %d\n", CIT_OK,
@@ -248,19 +285,17 @@ void cmd_stel(char *cmdbuf)
 }
 
 
-
-
-
-
-
 CTDL_MODULE_INIT(rwho)
 {
-        CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
-        CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
-        CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
-        CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
-        CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
-
-       /* return our Subversion id for the Log */
-        return "$Id$";
+       if(!threading)
+       {
+               CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
+               CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
+               CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
+               CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
+               CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
+       }
+       
+       /* return our module name for the log */
+        return "rwho";
 }