515c5f6afa7f14e79480fbeed05c139346b3ceb1
[citadel.git] / citadel / modules / rwho / serv_rwho.c
1 /*
2  * $Id$
3  *
4  * This module implements server commands related to the display and
5  * manipulation of the "Who's online" list.
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <libcitadel.h>
34 #include "citadel.h"
35 #include "server.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "control.h"
40 #include "room_ops.h"
41 #include "user_ops.h"
42 #include "policy.h"
43 #include "database.h"
44 #include "msgbase.h"
45
46
47 #include "ctdl_module.h"
48
49
50 /*
51  * display who's online
52  */
53 void cmd_rwho(char *argbuf) {
54         struct CitContext *cptr;
55         struct CitContext *nptr;
56         int nContexts, i;
57         int spoofed = 0;
58         int user_spoofed = 0;
59         int room_spoofed = 0;
60         int host_spoofed = 0;
61         int aide;
62         char un[40];
63         char real_room[ROOMNAMELEN], room[ROOMNAMELEN];
64         char host[64], flags[5];
65         
66         /* So that we don't keep the context list locked for a long time
67          * we create a copy of it first
68          */
69         
70
71         nptr = CtdlGetContextArray(&nContexts) ;
72         if (!nptr)
73         {
74                 /* Couldn't malloc so we have to bail but stick to the protocol */
75                 cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
76                 cprintf("000\n");
77                 return;
78         }
79         
80         aide = CC->user.axlevel >= 6;
81         cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
82         
83         for (i=0; i<nContexts; i++) 
84         {
85                 flags[0] = '\0';
86                 spoofed = 0;
87                 user_spoofed = 0;
88                 room_spoofed = 0;
89                 host_spoofed = 0;
90                 
91                 if (nptr[i].cs_flags & CS_POSTING)
92                    strcat(flags, "*");
93                 else
94                    strcat(flags, ".");
95                    
96                 if (nptr[i].fake_username[0])
97                 {
98                    strcpy(un, nptr[i].fake_username);
99                    spoofed = 1;
100                    user_spoofed = 1;
101                 }
102                 else
103                    strcpy(un, nptr[i].curr_user);
104                    
105                 if (nptr[i].fake_hostname[0])
106                 {
107                    strcpy(host, nptr[i].fake_hostname);
108                    spoofed = 1;
109                    host_spoofed = 1;
110                 }
111                 else
112                    strcpy(host, nptr[i].cs_host);
113
114                 GenerateRoomDisplay(real_room, &nptr[i], CC);
115
116                 if (nptr[i].fake_roomname[0]) {
117                         strcpy(room, nptr[i].fake_roomname);
118                         spoofed = 1;
119                         room_spoofed = 1;
120                 }
121                 else {
122                         strcpy(room, real_room);
123                 }
124                 
125                 if ((aide) && (spoofed)) {
126                         strcat(flags, "+");
127                 }
128                 
129                 if ((nptr[i].cs_flags & CS_STEALTH) && (aide)) {
130                         strcat(flags, "-");
131                 }
132                 
133                 if (((nptr[i].cs_flags&CS_STEALTH)==0) || (aide))
134                 {
135                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s|",
136                                 nptr[i].cs_pid, un, room,
137                                 host, nptr[i].cs_clientname,
138                                 (long)(nptr[i].lastidle),
139                                 nptr[i].lastcmdname, flags
140                         );
141
142                         if ((user_spoofed) && (aide)) {
143                                 cprintf("%s|", nptr[i].curr_user);
144                         }
145                         else {
146                                 cprintf("|");
147                         }
148         
149                         if ((room_spoofed) && (aide)) {
150                                 cprintf("%s|", real_room);
151                         }
152                         else {
153                                 cprintf("|");
154                         }
155         
156                         if ((host_spoofed) && (aide)) {
157                                 cprintf("%s|", nptr[i].cs_host);
158                         }
159                         else {
160                                 cprintf("|");
161                         }
162         
163                         cprintf("%d\n", nptr[i].logged_in);
164                 }
165         }
166         
167         /* release out copy of the context list */
168         free(nptr);
169
170         /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
171          * so that external paging modules such as serv_icq can add more
172          * content to the Wholist.
173          */
174         PerformSessionHooks(EVT_RWHO);
175         cprintf("000\n");
176 }
177
178
179 /*
180  * Masquerade roomname
181  */
182 void cmd_rchg(char *argbuf)
183 {
184         char newroomname[ROOMNAMELEN];
185
186         extract_token(newroomname, argbuf, 0, '|', sizeof newroomname);
187         newroomname[ROOMNAMELEN-1] = 0;
188         if (!IsEmptyStr(newroomname)) {
189                 safestrncpy(CC->fake_roomname, newroomname,
190                         sizeof(CC->fake_roomname) );
191         }
192         else {
193                 safestrncpy(CC->fake_roomname, "", sizeof CC->fake_roomname);
194         }
195         cprintf("%d OK\n", CIT_OK);
196 }
197
198 /*
199  * Masquerade hostname 
200  */
201 void cmd_hchg(char *argbuf)
202 {
203         char newhostname[64];
204
205         extract_token(newhostname, argbuf, 0, '|', sizeof newhostname);
206         if (!IsEmptyStr(newhostname)) {
207                 safestrncpy(CC->fake_hostname, newhostname,
208                         sizeof(CC->fake_hostname) );
209         }
210         else {
211                 safestrncpy(CC->fake_hostname, "", sizeof CC->fake_hostname);
212         }
213         cprintf("%d OK\n", CIT_OK);
214 }
215
216
217 /*
218  * Masquerade username (aides only)
219  */
220 void cmd_uchg(char *argbuf)
221 {
222
223         char newusername[USERNAME_SIZE];
224
225         extract_token(newusername, argbuf, 0, '|', sizeof newusername);
226
227         if (CtdlAccessCheck(ac_aide)) return;
228
229         if (!IsEmptyStr(newusername)) {
230                 CC->cs_flags &= ~CS_STEALTH;
231                 memset(CC->fake_username, 0, 32);
232                 if (strncasecmp(newusername, CC->curr_user,
233                                 strlen(CC->curr_user)))
234                         safestrncpy(CC->fake_username, newusername,
235                                 sizeof(CC->fake_username));
236         }
237         else {
238                 CC->fake_username[0] = '\0';
239                 CC->cs_flags |= CS_STEALTH;
240         }
241         cprintf("%d\n",CIT_OK);
242 }
243
244
245
246
247 /*
248  * enter or exit "stealth mode"
249  */
250 void cmd_stel(char *cmdbuf)
251 {
252         int requested_mode;
253
254         requested_mode = extract_int(cmdbuf,0);
255
256         if (CtdlAccessCheck(ac_logged_in)) return;
257
258         if (requested_mode == 1) {
259                 CC->cs_flags = CC->cs_flags | CS_STEALTH;
260                 PerformSessionHooks(EVT_STEALTH);
261         }
262         if (requested_mode == 0) {
263                 CC->cs_flags = CC->cs_flags & ~CS_STEALTH;
264                 PerformSessionHooks(EVT_UNSTEALTH);
265         }
266
267         cprintf("%d %d\n", CIT_OK,
268                 ((CC->cs_flags & CS_STEALTH) ? 1 : 0) );
269 }
270
271
272 CTDL_MODULE_INIT(rwho)
273 {
274         if(!threading)
275         {
276                 CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
277                 CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
278                 CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
279                 CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
280                 CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
281         }
282         
283         /* return our Subversion id for the Log */
284         return "$Id$";
285 }