18d1f0844ae063c0b5cf5fe108d35780d6948c6d
[citadel.git] / webcit / who.c
1 /*
2  * $Id$
3  *
4  * Display a list of all users currently logged on to the Citadel server.
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include "webcit.h"
27
28
29
30
31
32
33 /*
34  * who is on?
35  */
36 void whobbs(void)
37 {
38         char buf[SIZ], user[SIZ], room[SIZ], host[SIZ],
39                 realroom[SIZ], realhost[SIZ];
40         int sess;
41         time_t last_activity;
42         time_t now;
43         int bg = 0;
44
45         output_headers(1, 1, 2, 0, 1, 0, 0);
46
47         wprintf("<script type=\"text/javascript\">\n"
48                 "function ConfirmKill() { \n"
49                 "return confirm('Do you really want to kill this session?');\n"
50                 "}\n"
51                 "</script>\n"
52         );
53
54         wprintf("<div id=\"banner\">\n");
55         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
56         wprintf("<IMG SRC=\"/static/users-icon.gif\" ALT=\" \" ALIGN=MIDDLE>");
57         wprintf("<SPAN CLASS=\"titlebar\">&nbsp;Users currently on ");
58         escputs(serv_info.serv_humannode);
59         wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
60         offer_start_page();
61         wprintf("</TD></TR></TABLE>\n");
62         wprintf("</div>\n"
63                 "<div id=\"content\">\n");
64
65         wprintf("<div id=\"fix_scrollbar_bug\">"
66                 "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">"
67                 "<tr>\n");
68         wprintf("<TH COLSPAN=3>&nbsp;</TH>\n");
69         wprintf("<TH>User Name</TH>\n");
70         wprintf("<TH>Room</TH>");
71         wprintf("<TH>From host</TH>\n</TR>\n");
72
73         serv_puts("TIME");
74         serv_gets(buf);
75         if (buf[0] == '2') {
76                 now = extract_long(&buf[4], 0);
77         }
78         else {
79                 now = time(NULL);
80         }
81
82         serv_puts("RWHO");
83         serv_gets(buf);
84         if (buf[0] == '1') {
85                 while (serv_gets(buf), strcmp(buf, "000")) {
86                         sess = extract_int(buf, 0);
87                         extract(user, buf, 1);
88                         extract(room, buf, 2);
89                         extract(host, buf, 3);
90                         extract(realroom, buf, 9);
91                         extract(realhost, buf, 10);
92                         last_activity = extract_long(buf, 5);
93
94                         bg = 1 - bg;
95                         wprintf("<TR BGCOLOR=\"#%s\">",
96                                 (bg ? "DDDDDD" : "FFFFFF")
97                         );
98
99
100                         wprintf("<td>");
101                         if ((WC->is_aide) &&
102                             (sess != WC->ctdl_pid)) {
103                                 wprintf(" <A HREF=\"/terminate_session&which_session=%d&session_owner=", sess);
104                                 urlescputs(user);
105                                 wprintf("\" onClick=\"return ConfirmKill();\" "
106                                 ">[kill]</A>");
107                         }
108                         if (sess == WC->ctdl_pid) {
109                                 wprintf(" <A HREF=\"/edit_me\" "
110                                         ">[edit]</A>");
111                         }
112                         wprintf("</TD>");
113
114                         /* (link to page this user) */
115                         wprintf("<TD><A HREF=\"/display_page&recp=");
116                         urlescputs(user);
117                         wprintf("\">"
118                                 "<IMG ALIGN=MIDDLE WIDTH=20 HEIGHT=15 "
119                                 "SRC=\"/static/page.gif\" "
120                                 "ALT=\"(p)\""
121                                 " BORDER=0></A>&nbsp;");
122                         wprintf("</TD>");
123
124                         /* (idle flag) */
125                         wprintf("<TD>");
126                         if ((now - last_activity) > 900L) {
127                                 wprintf("&nbsp;"
128                                         "<IMG ALIGN=MIDDLE "
129                                         "SRC=\"/static/idle.gif\" "
130                                         "ALT=\"[idle]\" BORDER=0>");
131                         }
132                         wprintf("</TD>\n\t<TD>");
133
134
135
136                         /* username (link to user bio/photo page) */
137                         wprintf("<A HREF=\"/showuser&who=");
138                         urlescputs(user);
139                         wprintf("\">");
140                         escputs(user);
141                         wprintf("</A>");
142
143                         /* room */
144                         wprintf("</TD>\n\t<TD>");
145                         escputs(room);
146                         if (strlen(realroom) > 0) {
147                                 wprintf("<br /><I>");
148                                 escputs(realroom);
149                                 wprintf("</I>");
150                         }
151                         wprintf("</TD>\n\t<TD>");
152
153                         /* hostname */
154                         escputs(host);
155                         if (strlen(realhost) > 0) {
156                                 wprintf("<br /><I>");
157                                 escputs(realhost);
158                                 wprintf("</I>");
159                         }
160                         wprintf("</TD>\n</TR>");
161                 }
162         }
163         wprintf("</TABLE></div>\n"
164                 "<div align=center>"
165                 "Click on a name to read user info.  Click on "
166                 "<IMG ALIGN=MIDDLE SRC=\"/static/page.gif\" ALT=\"(p)\" "
167                 "BORDER=0> to send "
168                 "a page (instant message) to that user.</div>\n");
169         wDumpContent(1);
170 }
171
172
173 void terminate_session(void)
174 {
175         char buf[SIZ];
176
177         serv_printf("TERM %s", bstr("which_session"));
178         serv_gets(buf);
179         whobbs();
180 }
181
182
183 /*
184  * Change your session info (fake roomname and hostname)
185  */
186 void edit_me(void)
187 {
188         char buf[SIZ];
189
190         if (!strcasecmp(bstr("sc"), "Change room name")) {
191                 serv_printf("RCHG %s", bstr("fake_roomname"));
192                 serv_gets(buf);
193                 http_redirect("/whobbs");
194         } else if (!strcasecmp(bstr("sc"), "Change host name")) {
195                 serv_printf("HCHG %s", bstr("fake_hostname"));
196                 serv_gets(buf);
197                 http_redirect("/whobbs");
198         } else if (!strcasecmp(bstr("sc"), "Change user name")) {
199                 serv_printf("UCHG %s", bstr("fake_username"));
200                 serv_gets(buf);
201                 http_redirect("/whobbs");
202         } else if (!strcasecmp(bstr("sc"), "Cancel")) {
203                 http_redirect("/whobbs");
204         } else {
205
206                 output_headers(1, 1, 0, 0, 0, 0, 0);
207
208                 wprintf("<div id=\"banner\">\n");
209                 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
210                 wprintf("<SPAN CLASS=\"titlebar\">");
211                 wprintf("Edit your session display");
212                 wprintf("</SPAN></TD></TR></TABLE>\n");
213                 wprintf("</div>\n<div id=\"content\">\n");
214
215                 wprintf("This screen allows you to change the way your\n");
216                 wprintf("session appears in the 'Who is online' listing.\n");
217                 wprintf("To turn off any 'fake' name you've previously\n");
218                 wprintf("set, simply click the appropriate 'change' button\n");
219                 wprintf("without typing anything in the corresponding box.\n");
220                 wprintf("<br />\n");
221
222                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/edit_me\">\n");
223
224                 wprintf("<TABLE border=0 width=100%%>\n");
225
226                 wprintf("<TR><TD><B>Room name:</B></TD>\n<TD>");
227                 wprintf("<INPUT TYPE=\"text\" NAME=\"fake_roomname\" MAXLENGTH=\"64\">\n");
228                 wprintf("</TD>\n<TD ALIGN=center>");
229                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Change room name\">");
230                 wprintf("</TD>\n</TR>\n");
231
232                 wprintf("<TR><TD><B>Host name:</B></TD><TD>");
233                 wprintf("<INPUT TYPE=\"text\" NAME=\"fake_hostname\" MAXLENGTH=\"64\">\n");
234                 wprintf("</TD>\n<TD ALIGN=center>");
235                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Change host name\">");
236                 wprintf("</TD>\n</TR>\n");
237
238                 if (WC->is_aide) {
239                         wprintf("<TR><TD><B>User name:</B></TD><TD>");
240                         wprintf("<INPUT TYPE=\"text\" NAME=\"fake_username\" MAXLENGTH=\"64\">\n");
241                         wprintf("</TD>\n<TD ALIGN=center>");
242                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Change user name\">");
243                         wprintf("</TD>\n</TR>\n");
244                 }
245                 wprintf("<TR><TD>&nbsp;</TD><TD>&nbsp;</TD><TD ALIGN=center>");
246                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
247                 wprintf("</TD></TR></TABLE>\n");
248
249                 wprintf("</FORM></CENTER>\n");
250                 wDumpContent(1);
251         }
252 }