* The size constant "256" which shows up everywhere as a buffer size has now
[citadel.git] / citadel / serv_rwho.c
1 /*
2  * $Id$
3  *
4  * This module implementsserver 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 #include <sys/time.h>
19 #include <sys/wait.h>
20 #include <string.h>
21 #include <limits.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include <time.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
27 #include "support.h"
28 #include "config.h"
29 #include "control.h"
30 #include "dynloader.h"
31 #include "room_ops.h"
32 #include "user_ops.h"
33 #include "policy.h"
34 #include "database.h"
35 #include "msgbase.h"
36 #include "tools.h"
37
38
39
40 /*
41  * display who's online
42  */
43 void cmd_rwho(char *argbuf) {
44         struct CitContext *cptr;
45         int spoofed = 0;
46         int user_spoofed = 0;
47         int room_spoofed = 0;
48         int host_spoofed = 0;
49         int aide;
50         char un[40];
51         char real_room[ROOMNAMELEN], room[ROOMNAMELEN];
52         char host[40], flags[5];
53         
54         aide = CC->usersupp.axlevel >= 6;
55         cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
56         
57         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) 
58         {
59                 flags[0] = '\0';
60                 spoofed = 0;
61                 user_spoofed = 0;
62                 room_spoofed = 0;
63                 host_spoofed = 0;
64                 
65                 if (cptr->cs_flags & CS_POSTING)
66                    strcat(flags, "*");
67                 else
68                    strcat(flags, ".");
69                    
70                 if (cptr->fake_username[0])
71                 {
72                    strcpy(un, cptr->fake_username);
73                    spoofed = 1;
74                    user_spoofed = 1;
75                 }
76                 else
77                    strcpy(un, cptr->curr_user);
78                    
79                 if (cptr->fake_hostname[0])
80                 {
81                    strcpy(host, cptr->fake_hostname);
82                    spoofed = 1;
83                    host_spoofed = 1;
84                 }
85                 else
86                    strcpy(host, cptr->cs_host);
87
88                 GenerateRoomDisplay(real_room, cptr, CC);
89
90                 if (cptr->fake_roomname[0]) {
91                         strcpy(room, cptr->fake_roomname);
92                         spoofed = 1;
93                         room_spoofed = 1;
94                 }
95                 else {
96                         strcpy(room, real_room);
97                 }
98                 
99                 if ((aide) && (spoofed))
100                    strcat(flags, "+");
101                 
102                 if ((cptr->cs_flags & CS_STEALTH) && (aide))
103                    strcat(flags, "-");
104                 
105                 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
106                 {
107                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s|",
108                                 cptr->cs_pid, un, room,
109                                 host, cptr->cs_clientname,
110                                 (long)(cptr->lastidle),
111                                 cptr->lastcmdname, flags);
112                 }
113
114                 if ((user_spoofed) && (aide)) {
115                         cprintf("%s|", cptr->curr_user);
116                 }
117                 else {
118                         cprintf("|");
119                 }
120
121                 if ((room_spoofed) && (aide)) {
122                         cprintf("%s|", real_room);
123                 }
124                 else {
125                         cprintf("|");
126                 }
127
128                 if ((host_spoofed) && (aide)) {
129                         cprintf("%s|", cptr->cs_host);
130                 }
131                 else {
132                         cprintf("|");
133                 }
134
135                 cprintf("\n");
136
137         }
138
139         /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
140          * so that external paging modules such as serv_icq can add more
141          * content to the Wholist.
142          */
143         PerformSessionHooks(EVT_RWHO);
144         cprintf("000\n");
145         }
146
147
148 /*
149  * Masquerade roomname
150  */
151 void cmd_rchg(char *argbuf)
152 {
153         char newroomname[SIZ];
154
155         extract(newroomname, argbuf, 0);
156         newroomname[ROOMNAMELEN-1] = 0;
157         if (strlen(newroomname) > 0) {
158                 safestrncpy(CC->fake_roomname, newroomname,
159                         sizeof(CC->fake_roomname) );
160                 }
161         else {
162                 strcpy(CC->fake_roomname, "");
163                 }
164         cprintf("%d OK\n", OK);
165 }
166
167 /*
168  * Masquerade hostname 
169  */
170 void cmd_hchg(char *argbuf)
171 {
172         char newhostname[SIZ];
173
174         extract(newhostname, argbuf, 0);
175         if (strlen(newhostname) > 0) {
176                 safestrncpy(CC->fake_hostname, newhostname,
177                         sizeof(CC->fake_hostname) );
178                 }
179         else {
180                 strcpy(CC->fake_hostname, "");
181                 }
182         cprintf("%d OK\n", OK);
183 }
184
185
186 /*
187  * Masquerade username (aides only)
188  */
189 void cmd_uchg(char *argbuf)
190 {
191
192         char newusername[SIZ];
193
194         extract(newusername, argbuf, 0);
195
196         if (CtdlAccessCheck(ac_aide)) return;
197
198         if (strlen(newusername) > 0) {
199                 CC->cs_flags &= ~CS_STEALTH;
200                 memset(CC->fake_username, 0, 32);
201                 if (strncasecmp(newusername, CC->curr_user,
202                                 strlen(CC->curr_user)))
203                         safestrncpy(CC->fake_username, newusername,
204                                 sizeof(CC->fake_username));
205         }
206         else {
207                 CC->fake_username[0] = '\0';
208                 CC->cs_flags |= CS_STEALTH;
209         }
210         cprintf("%d\n",OK);
211 }
212
213
214
215
216 /*
217  * enter or exit "stealth mode"
218  */
219 void cmd_stel(char *cmdbuf)
220 {
221         int requested_mode;
222
223         requested_mode = extract_int(cmdbuf,0);
224         if (requested_mode !=0) requested_mode = 1;
225
226         if (CtdlAccessCheck(ac_aide)) return;
227
228         if (CC->cs_flags & CS_STEALTH) {
229                 if (requested_mode == 0)
230                         CC->cs_flags = CC->cs_flags-CS_STEALTH;
231                 }
232         else {
233                 if (requested_mode == 1)
234                         CC->cs_flags = CC->cs_flags|CS_STEALTH;
235                 }
236
237         cprintf("%d Ok\n",OK);
238         }
239
240
241
242
243
244
245
246 char *Dynamic_Module_Init(void)
247 {
248         CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
249         CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
250         CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
251         CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
252         CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
253         return "$Id$";
254 }