ebd2adbb7af131213a2eeb35379205bdfe9c590b
[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                 
103                 if ((cptr->cs_flags & CS_STEALTH) && (aide)) {
104                         strcat(flags, "-");
105                 }
106                 
107                 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
108                 {
109                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s|",
110                                 cptr->cs_pid, un, room,
111                                 host, cptr->cs_clientname,
112                                 (long)(cptr->lastidle),
113                                 cptr->lastcmdname, flags);
114
115                         if ((user_spoofed) && (aide)) {
116                                 cprintf("%s|", cptr->curr_user);
117                         }
118                         else {
119                                 cprintf("|");
120                         }
121         
122                         if ((room_spoofed) && (aide)) {
123                                 cprintf("%s|", real_room);
124                         }
125                         else {
126                                 cprintf("|");
127                         }
128         
129                         if ((host_spoofed) && (aide)) {
130                                 cprintf("%s|", cptr->cs_host);
131                         }
132                         else {
133                                 cprintf("|");
134                         }
135         
136                         cprintf("\n");
137                 }
138         }
139
140         /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
141          * so that external paging modules such as serv_icq can add more
142          * content to the Wholist.
143          */
144         PerformSessionHooks(EVT_RWHO);
145         cprintf("000\n");
146         }
147
148
149 /*
150  * Masquerade roomname
151  */
152 void cmd_rchg(char *argbuf)
153 {
154         char newroomname[SIZ];
155
156         extract(newroomname, argbuf, 0);
157         newroomname[ROOMNAMELEN-1] = 0;
158         if (strlen(newroomname) > 0) {
159                 safestrncpy(CC->fake_roomname, newroomname,
160                         sizeof(CC->fake_roomname) );
161                 }
162         else {
163                 strcpy(CC->fake_roomname, "");
164                 }
165         cprintf("%d OK\n", OK);
166 }
167
168 /*
169  * Masquerade hostname 
170  */
171 void cmd_hchg(char *argbuf)
172 {
173         char newhostname[SIZ];
174
175         extract(newhostname, argbuf, 0);
176         if (strlen(newhostname) > 0) {
177                 safestrncpy(CC->fake_hostname, newhostname,
178                         sizeof(CC->fake_hostname) );
179                 }
180         else {
181                 strcpy(CC->fake_hostname, "");
182                 }
183         cprintf("%d OK\n", OK);
184 }
185
186
187 /*
188  * Masquerade username (aides only)
189  */
190 void cmd_uchg(char *argbuf)
191 {
192
193         char newusername[SIZ];
194
195         extract(newusername, argbuf, 0);
196
197         if (CtdlAccessCheck(ac_aide)) return;
198
199         if (strlen(newusername) > 0) {
200                 CC->cs_flags &= ~CS_STEALTH;
201                 memset(CC->fake_username, 0, 32);
202                 if (strncasecmp(newusername, CC->curr_user,
203                                 strlen(CC->curr_user)))
204                         safestrncpy(CC->fake_username, newusername,
205                                 sizeof(CC->fake_username));
206         }
207         else {
208                 CC->fake_username[0] = '\0';
209                 CC->cs_flags |= CS_STEALTH;
210         }
211         cprintf("%d\n",OK);
212 }
213
214
215
216
217 /*
218  * enter or exit "stealth mode"
219  */
220 void cmd_stel(char *cmdbuf)
221 {
222         int requested_mode;
223
224         requested_mode = extract_int(cmdbuf,0);
225         if (requested_mode !=0) requested_mode = 1;
226
227         if (CtdlAccessCheck(ac_aide)) return;
228
229         if (CC->cs_flags & CS_STEALTH) {
230                 if (requested_mode == 0)
231                         CC->cs_flags = CC->cs_flags-CS_STEALTH;
232                 }
233         else {
234                 if (requested_mode == 1)
235                         CC->cs_flags = CC->cs_flags|CS_STEALTH;
236                 }
237
238         cprintf("%d Ok\n",OK);
239         }
240
241
242
243
244
245
246
247 char *Dynamic_Module_Init(void)
248 {
249         CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
250         CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
251         CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
252         CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
253         CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
254         return "$Id$";
255 }