* Added missing HTTP headers to do_chat()
[citadel.git] / webcit / paging.c
1 /*
2  * $Id$
3  *
4  * Functions which implement the chat and paging facilities.
5  */
6
7 #include "webcit.h"
8
9 /*
10  * display the form for paging (x-messaging) another user
11  */
12 void display_page(void)
13 {
14         char recp[SIZ];
15
16         strcpy(recp, bstr("recp"));
17
18         output_headers(1, 1, 2, 0, 0, 0);
19         wprintf("<div id=\"banner\">\n"
20                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
21                 "<SPAN CLASS=\"titlebar\">");
22         wprintf(_("Send instant message"));
23         wprintf("</SPAN>"
24                 "</TD></TR></TABLE>\n"
25                 "</div>\n<div id=\"content\">\n"
26         );
27                                                                                                                              
28         wprintf("<div id=\"fix_scrollbar_bug\">"
29                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
30
31         wprintf(_("Send an instant message to: "));
32         escputs(recp);
33         wprintf("<br>\n");
34
35         wprintf("<FORM METHOD=\"POST\" action=\"page_user\">\n");
36
37         wprintf("<TABLE border=0 width=100%%><TR><TD>\n");
38
39         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
40         escputs(recp);
41         wprintf("\">\n");
42
43         wprintf("<INPUT TYPE=\"hidden\" NAME=\"closewin\" VALUE=\"");
44         escputs(bstr("closewin"));
45         wprintf("\">\n");
46
47         wprintf(_("Enter message text:"));
48         wprintf("<br />");
49
50         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=5 COLS=40 "
51                 "WIDTH=40></TEXTAREA>\n");
52
53         wprintf("</TD></TR></TABLE><br />\n");
54
55         wprintf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">", _("Send message"));
56         wprintf("<br /><a href=\"javascript:window.close();\"%s</A>\n", _("Cancel"));
57
58         wprintf("</FORM></CENTER>\n");
59         wprintf("</td></tr></table></div>\n");
60         wDumpContent(1);
61 }
62
63 /*
64  * page another user
65  */
66 void page_user(void)
67 {
68         char recp[SIZ];
69         char buf[SIZ];
70         char closewin[SIZ];
71
72         output_headers(1, 1, 2, 0, 0, 0);
73         wprintf("<div id=\"banner\">\n"
74                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
75                 "<SPAN CLASS=\"titlebar\">");
76         wprintf(_("Add or edit an event"));
77         wprintf("</SPAN>"
78                 "</TD></TR></TABLE>\n"
79                 "</div>\n<div id=\"content\">\n"
80         );
81                                                                                                                              
82         strcpy(recp, bstr("recp"));
83         strcpy(closewin, bstr("closewin"));
84
85         if (strlen(bstr("send_button")) == 0) {
86                 wprintf("<EM>");
87                 wprintf(_("Message was not sent."));
88                 wprintf("</EM><br />\n");
89         } else {
90                 serv_printf("SEXP %s|-", recp);
91                 serv_getln(buf, sizeof buf);
92
93                 if (buf[0] == '4') {
94                         text_to_server(bstr("msgtext"), 0);
95                         serv_puts("000");
96                         wprintf("<EM>");
97                         wprintf(_("Message has been sent to "));
98                         escputs(recp);
99                         wprintf(".</EM><br />\n");
100                 }
101                 else {
102                         wprintf("<EM>%s</EM><br />\n", &buf[4]);
103                 }
104         }
105         
106         if (!strcasecmp(closewin, "yes")) {
107                 wprintf("<CENTER><a href=\"javascript:window.close();\">");
108                 wprintf(_("[ close window ]"));
109                 wprintf("</A></CENTER>\n");
110         }
111
112         wDumpContent(1);
113 }
114
115
116
117 /*
118  * multiuser chat
119  */
120 void do_chat(void)
121 {
122         char buf[SIZ];
123
124         /* First, check to make sure we're still allowed in this room. */
125         serv_printf("GOTO %s", WC->wc_roomname);
126         serv_getln(buf, sizeof buf);
127         if (buf[0] != '2') {
128                 smart_goto("_BASEROOM_");
129                 return;
130         }
131
132         /* If the chat socket is still open from a previous chat,
133          * close it -- because it might be stale or in the wrong room.
134          */
135         if (WC->chat_sock < 0) {
136                 close(WC->chat_sock);
137                 WC->chat_sock = (-1);
138         }
139
140         /* WebCit Chat works by having transmit, receive, and refresh
141          * frames.  Load the frameset.  (This isn't AJAX but the headers
142          * output by begin_ajax_response() happen to be the ones we need.)
143          */
144         begin_ajax_response();
145         do_template("chatframeset");
146         end_ajax_response();
147         return;
148 }
149
150
151 /*
152  *
153  */
154 void page_popup(void)
155 {
156         char buf[SIZ];
157         char pagefrom[SIZ];
158
159         while (serv_puts("GEXP"), serv_getln(buf, sizeof buf), buf[0]=='1') {
160
161                 extract_token(pagefrom, &buf[4], 3, '|', sizeof pagefrom);
162
163                 wprintf("<table border=1 bgcolor=\"#880000\"><tr><td>");
164                 wprintf("<span class=\"titlebar\">");
165                 wprintf(_("Instant message from "));
166                 escputs(pagefrom);
167                 wprintf("</span></td></tr><tr><td><font color=\"#FFFFFF\">");
168                 fmout("LEFT");
169                 wprintf("</font></td></tr>"
170                         "<tr><td><div align=center><font color=\"#FFFFFF\">"
171                         "<a href=\"javascript:hide_page_popup()\">");
172                 wprintf(_("[ close window ]"));
173                 wprintf("</a>"
174                         "</font></div>"
175                         "</td></tr>"
176                         "</table>\n");
177         }
178
179         WC->HaveInstantMessages = 0;
180 }
181
182
183
184 /*
185  * Support function for chat -- make sure the chat socket is connected
186  * and in chat mode.
187  */
188 int setup_chat_socket(void) {
189         char buf[SIZ];
190         int i;
191         int good_chatmode = 0;
192
193         if (WC->chat_sock < 0) {
194
195                 if (!strcasecmp(ctdlhost, "uds")) {
196                         /* unix domain socket */
197                         sprintf(buf, "%s/citadel.socket", ctdlport);
198                         WC->chat_sock = uds_connectsock(buf);
199                 }
200                 else {
201                         /* tcp socket */
202                         WC->chat_sock = tcp_connectsock(ctdlhost, ctdlport);
203                 }
204
205                 if (WC->chat_sock < 0) {
206                         return(errno);
207                 }
208
209                 /* Temporarily swap the serv and chat sockets during chat talk */
210                 i = WC->serv_sock;
211                 WC->serv_sock = WC->chat_sock;
212                 WC->chat_sock = i;
213
214                 serv_getln(buf, sizeof buf);
215                 if (buf[0] == '2') {
216                         serv_printf("USER %s", WC->wc_username);
217                         serv_getln(buf, sizeof buf);
218                         if (buf[0] == '3') {
219                                 serv_printf("PASS %s", WC->wc_password);
220                                 serv_getln(buf, sizeof buf);
221                                 if (buf[0] == '2') {
222                                         serv_printf("GOTO %s", WC->wc_roomname);
223                                         serv_getln(buf, sizeof buf);
224                                         if (buf[0] == '2') {
225                                                 serv_puts("CHAT");
226                                                 serv_getln(buf, sizeof buf);
227                                                 if (buf[0] == '8') {
228                                                         good_chatmode = 1;
229                                                 }
230                                         }
231                                 }
232                         }
233                 }
234
235                 /* Unswap the sockets. */
236                 i = WC->serv_sock;
237                 WC->serv_sock = WC->chat_sock;
238                 WC->chat_sock = i;
239
240                 if (!good_chatmode) close(WC->serv_sock);
241
242         }
243         return(0);
244 }
245
246
247
248 /*
249  * Receiving side of the chat window.  This is implemented in a
250  * tiny hidden IFRAME that just does JavaScript writes to
251  * other frames whenever it refreshes and finds new data.
252  */
253 void chat_recv(void) {
254         int i;
255         struct pollfd pf;
256         int got_data = 0;
257         int end_chat_now = 0;
258         char buf[SIZ];
259         char cl_user[SIZ];
260         char cl_text[SIZ];
261         char *output_data = NULL;
262
263         output_headers(0, 0, 0, 0, 0, 0);
264
265         wprintf("Content-type: text/html; charset=utf-8\n");
266         wprintf("\n");
267         wprintf("<html>\n"
268                 "<head>\n"
269                 "<meta http-equiv=\"refresh\" content=\"3\" />\n"
270                 "</head>\n"
271
272                 "<body bgcolor=\"#FFFFFF\">\n"
273         );
274
275         if (setup_chat_socket() != 0) {
276                 wprintf(_("An error occurred while setting up the chat socket."));
277                 wprintf("</BODY></HTML>\n");
278                 wDumpContent(0);
279                 return;
280         }
281
282         /*
283          * See if there is any chat data waiting.
284          */
285         output_data = strdup("");
286         do {
287                 got_data = 0;
288                 pf.fd = WC->chat_sock;
289                 pf.events = POLLIN;
290                 pf.revents = 0;
291                 if (poll(&pf, 1, 1) > 0) if (pf.revents & POLLIN) {
292                         ++got_data;
293
294                         /* Temporarily swap the serv and chat sockets during chat talk */
295                         i = WC->serv_sock;
296                         WC->serv_sock = WC->chat_sock;
297                         WC->chat_sock = i;
298         
299                         serv_getln(buf, sizeof buf);
300
301                         if (!strcmp(buf, "000")) {
302                                 strcpy(buf, ":|");
303                                 strcat(buf, _("Now exiting chat mode."));
304                                 end_chat_now = 1;
305                         }
306                         
307                         /* Unswap the sockets. */
308                         i = WC->serv_sock;
309                         WC->serv_sock = WC->chat_sock;
310                         WC->chat_sock = i;
311
312                         /* Append our output data */
313                         output_data = realloc(output_data, strlen(output_data) + strlen(buf) + 4);
314                         strcat(output_data, buf);
315                         strcat(output_data, "\n");
316                 }
317
318         } while ( (got_data) && (!end_chat_now) );
319
320         if (end_chat_now) {
321                 close(WC->chat_sock);
322                 WC->chat_sock = (-1);
323                 wprintf("<img src=\"static/blank.gif\" onLoad=\"parent.window.close();\">\n");
324         }
325
326         if (strlen(output_data) > 0) {
327
328                 if (output_data[strlen(output_data)-1] == '\n') {
329                         output_data[strlen(output_data)-1] = 0;
330                 }
331
332                 /* Output our fun to the other frame. */
333                 wprintf("<img src=\"static/blank.gif\" WIDTH=1 HEIGHT=1\n"
334                         "onLoad=\" \n"
335                 );
336
337                 for (i=0; i<num_tokens(output_data, '\n'); ++i) {
338                         extract_token(buf, output_data, i, '\n', sizeof buf);
339                         extract_token(cl_user, buf, 0, '|', sizeof cl_user);
340                         extract_token(cl_text, buf, 1, '|', sizeof cl_text);
341
342                         if (strcasecmp(cl_text, "NOOP")) {
343
344                                 wprintf("parent.chat_transcript.document.write('");
345         
346                                 if (strcasecmp(cl_user, WC->last_chat_user)) {
347                                         wprintf("<TABLE border=0 WIDTH=100%% "
348                                                 "CELLSPACING=1 CELLPADDING=0 "
349                                                 "BGCOLOR=&quot;#FFFFFF&quot;>"
350                                                 "<TR><TD></TR></TD></TABLE>"
351                                         );
352         
353                                 }
354
355                                 wprintf("<TABLE border=0 WIDTH=100%% "
356                                         "CELLSPACING=0 CELLPADDING=0 "
357                                         "BGCOLOR=&quot;#EEEEEE&quot;>");
358         
359                                 wprintf("<TR><TD>");
360         
361                                 if (!strcasecmp(cl_user, ":")) {
362                                         wprintf("<I>");
363                                 }
364
365                                 if (strcasecmp(cl_user, WC->last_chat_user)) {
366                                         wprintf("<B>");
367         
368                                         if (!strcasecmp(cl_user, WC->wc_username)) {
369                                                 wprintf("<FONT COLOR=&quot;#FF0000&quot;>");
370                                         }
371                                         else {
372                                                 wprintf("<FONT COLOR=&quot;#0000FF&quot;>");
373                                         }
374                                         jsescputs(cl_user);
375         
376                                         wprintf("</FONT>: </B>");
377                                 }
378                                 else {
379                                         wprintf("&nbsp;&nbsp;&nbsp;");
380                                 }
381                                 jsescputs(cl_text);
382                                 if (!strcasecmp(cl_user, ":")) {
383                                         wprintf("</I>");
384                                 }
385
386                                 wprintf("</TD></TR></TABLE>");
387                                 wprintf("'); \n");
388
389                                 strcpy(WC->last_chat_user, cl_user);
390                         }
391                 }
392
393                 wprintf("parent.chat_transcript.scrollTo(999999,999999);\">\n");
394         }
395
396         free(output_data);
397
398         wprintf("</BODY></HTML>\n");
399         wDumpContent(0);
400 }
401
402
403 /*
404  * sending side of the chat window
405  */
406 void chat_send(void) {
407         int i;
408         char send_this[SIZ];
409         char buf[SIZ];
410
411         output_headers(0, 0, 0, 0, 0, 0);
412         wprintf("Content-type: text/html; charset=utf-8\n");
413         wprintf("\n");
414         wprintf("<HTML>"
415                 "<BODY onLoad=\"document.chatsendform.send_this.focus();\" >"
416         );
417
418         if (bstr("send_this") != NULL) {
419                 strcpy(send_this, bstr("send_this"));
420         }
421         else {
422                 strcpy(send_this, "");
423         }
424
425         if (strlen(bstr("help_button")) > 0) {
426                 strcpy(send_this, "/help");
427         }
428
429         if (strlen(bstr("list_button")) > 0) {
430                 strcpy(send_this, "/who");
431         }
432
433         if (strlen(bstr("exit_button")) > 0) {
434                 strcpy(send_this, "/quit");
435         }
436
437         if (setup_chat_socket() != 0) {
438                 wprintf(_("An error occurred while setting up the chat socket."));
439                 wprintf("</BODY></HTML>\n");
440                 wDumpContent(0);
441                 return;
442         }
443
444         /* Temporarily swap the serv and chat sockets during chat talk */
445         i = WC->serv_sock;
446         WC->serv_sock = WC->chat_sock;
447         WC->chat_sock = i;
448
449         while (strlen(send_this) > 0) {
450                 if (strlen(send_this) < 67) {
451                         serv_puts(send_this);
452                         strcpy(send_this, "");
453                 }
454                 else {
455                         for (i=55; i<67; ++i) {
456                                 if (send_this[i] == ' ') break;
457                         }
458                         strncpy(buf, send_this, i);
459                         buf[i] = 0;
460                         strcpy(send_this, &send_this[i]);
461                         serv_puts(buf);
462                 }
463         }
464
465         /* Unswap the sockets. */
466         i = WC->serv_sock;
467         WC->serv_sock = WC->chat_sock;
468         WC->chat_sock = i;
469
470         wprintf("<FORM METHOD=\"POST\" action=\"chat_send\" NAME=\"chatsendform\">\n");
471         wprintf("<INPUT TYPE=\"text\" SIZE=\"80\" MAXLENGTH=\"%d\" "
472                 "NAME=\"send_this\">\n", SIZ-10);
473         wprintf("<br />");
474         wprintf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">\n", _("Send"));
475         wprintf("<INPUT TYPE=\"submit\" NAME=\"help_button\" VALUE=\"%s\">\n", _("Help"));
476         wprintf("<INPUT TYPE=\"submit\" NAME=\"list_button\" VALUE=\"%s\">\n", _("List users"));
477         wprintf("<INPUT TYPE=\"submit\" NAME=\"exit_button\" VALUE=\"%s\">\n", _("Exit"));
478         wprintf("</FORM>\n");
479
480         wprintf("</BODY></HTML>\n");
481         wDumpContent(0);
482 }
483
484