stable now but there are GIANT PIECES MISSING
[citadel.git] / citadel / modules / rwho / serv_rwho.c
1 /*
2  * This module implements server commands related to the display and
3  * manipulation of the "Who's online" list.
4  *
5  * Copyright (c) 1987-2019 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include "sysdep.h"
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <time.h>
27 #include <sys/wait.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <libcitadel.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include "citserver.h"
34 #include "support.h"
35 #include "config.h"
36 #include "control.h"
37 #include "user_ops.h"
38 #include "database.h"
39 #include "msgbase.h"
40 #include "ctdl_module.h"
41
42 /* Don't show the names of private rooms unless the viewing
43  * user also knows the rooms.
44  */
45 void GenerateRoomDisplay(char *real_room,
46                         CitContext *viewed,
47                         CitContext *viewer) {
48
49         int ra;
50
51         strcpy(real_room, viewed->room.QRname);
52         if (viewed->room.QRflags & QR_MAILBOX) {
53                 strcpy(real_room, &real_room[11]);
54         }
55         if (viewed->room.QRflags & QR_PRIVATE) {
56                 CtdlRoomAccess(&viewed->room, &viewer->user, &ra, NULL);
57                 if ( (ra & UA_KNOWN) == 0) {
58                         strcpy(real_room, " ");
59                 }
60         }
61
62         if (viewed->cs_flags & CS_CHAT) {
63                 while (strlen(real_room) < 14) {
64                         strcat(real_room, " ");
65                 }
66                 strcpy(&real_room[14], "<chat>");
67         }
68
69 }
70
71
72
73 /*
74  * display who's online
75  */
76 void cmd_rwho(char *argbuf) {
77         struct CitContext *nptr;
78         int nContexts, i;
79         int spoofed = 0;
80         int aide;
81         char room[ROOMNAMELEN];
82         char flags[5];
83         
84         /* So that we don't keep the context list locked for a long time
85          * we create a copy of it first
86          */
87         nptr = CtdlGetContextArray(&nContexts) ;
88         if (!nptr)
89         {
90                 /* Couldn't malloc so we have to bail but stick to the protocol */
91                 cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
92                 cprintf("000\n");
93                 return;
94         }
95         
96         aide = ( (CC->user.axlevel >= AxAideU) || (CC->internal_pgm) ) ;
97         cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
98         
99         for (i=0; i<nContexts; i++)  {
100                 flags[0] = '\0';
101                 spoofed = 0;
102                 
103                 if (!aide && nptr[i].state == CON_SYS)
104                         continue;
105
106                 if (!aide && nptr[i].kill_me != 0)
107                         continue;
108
109                 if (nptr[i].cs_flags & CS_POSTING) {
110                         strcat(flags, "*");
111                 }
112                 else {
113                         strcat(flags, ".");
114                 }
115                    
116                 GenerateRoomDisplay(room, &nptr[i], CC);
117
118                 if ((aide) && (spoofed)) {
119                         strcat(flags, "+");
120                 }
121                 
122                 if ((nptr[i].cs_flags & CS_STEALTH) && (aide)) {
123                         strcat(flags, "-");
124                 }
125                 
126                 if (((nptr[i].cs_flags&CS_STEALTH)==0) || (aide)) {
127
128                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s|",
129                                 nptr[i].cs_pid, nptr[i].curr_user, room,
130                                 nptr[i].cs_host, nptr[i].cs_clientname,
131                                 (long)(nptr[i].lastidle),
132                                 nptr[i].lastcmdname, flags
133                         );
134
135                         cprintf("|");   // no spoofed user names anymore
136                         cprintf("|");   // no spoofed room names anymore
137                         cprintf("|");   // no spoofed host names anymore
138         
139                         cprintf("%d\n", nptr[i].logged_in);
140                 }
141         }
142         
143         /* release our copy of the context list */
144         free(nptr);
145
146         /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
147          * so that external paging modules such as serv_icq can add more
148          * content to the Wholist.
149          */
150         PerformSessionHooks(EVT_RWHO);
151         cprintf("000\n");
152 }
153
154
155 /*
156  * enter or exit "stealth mode"
157  */
158 void cmd_stel(char *cmdbuf)
159 {
160         int requested_mode;
161
162         requested_mode = extract_int(cmdbuf,0);
163
164         if (CtdlAccessCheck(ac_logged_in)) return;
165
166         if (requested_mode == 1) {
167                 CC->cs_flags = CC->cs_flags | CS_STEALTH;
168                 PerformSessionHooks(EVT_STEALTH);
169         }
170         if (requested_mode == 0) {
171                 CC->cs_flags = CC->cs_flags & ~CS_STEALTH;
172                 PerformSessionHooks(EVT_UNSTEALTH);
173         }
174
175         cprintf("%d %d\n", CIT_OK,
176                 ((CC->cs_flags & CS_STEALTH) ? 1 : 0) );
177 }
178
179
180 CTDL_MODULE_INIT(rwho)
181 {
182         if(!threading)
183         {
184                 CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
185                 CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
186                 //CtdlRegisterSessionHook(dead_io_check, EVT_TIMER, PRIO_QUEUE + 50);
187
188         }
189         
190         /* return our module name for the log */
191         return "rwho";
192 }