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