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