* fix_scrollbar_bug is now a class instead of an id. Fixes validator warnings.
[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 class=\"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  * If there are instant messages waiting, and we notice that we haven't checked them in
153  * a while, it probably means that we need to open the instant messenger window.
154  */
155 void page_popup(void)
156 {
157         char buf[SIZ];
158
159         /* First, do the check as part of our page load. */
160         serv_puts("NOOP");
161         serv_getln(buf, sizeof buf);
162         if (buf[3] == '*') {
163                 if ((time(NULL) - WC->last_pager_check) > 60) {
164                         wprintf("<script type=\"text/javascript\">"
165                                 " window.open('static/instant_messenger.html', 'CTDL_MESSENGER', "
166                                 " 'width=700,height=400');"
167                                 "</script>"
168                         );      
169                 }
170         }
171
172         /* Then schedule it to happen again a minute from now if the user is idle. */
173         wprintf("<script type=\"text/javascript\">      \n"
174                 " function HandleSslp(sslg_xmlresponse) {       \n"
175                 "  sslg_response = sslg_xmlresponse.responseText.substr(0, 1);  \n"
176                 "  if (sslg_response == 'Y') {  \n"
177                 "   window.open('static/instant_messenger.html', 'CTDL_MESSENGER',      \n"
178                 "    'width=700,height=400');   \n"
179                 "   }   \n"
180                 " }     \n"
181                 " function CheckPager() {       \n"
182                 "  new Ajax.Request('sslg', { method: 'get', parameters: Math.random(), \n"
183                 "   onSuccess: HandleSslp } );  \n"
184                 " }     \n"
185                 " new PeriodicalExecuter(CheckPager, 30);       \n"
186                 "</script>      \n"
187         );
188 }
189
190
191
192 /*
193  * Support function for chat -- make sure the chat socket is connected
194  * and in chat mode.
195  */
196 int setup_chat_socket(void) {
197         char buf[SIZ];
198         int i;
199         int good_chatmode = 0;
200
201         if (WC->chat_sock < 0) {
202
203                 if (!strcasecmp(ctdlhost, "uds")) {
204                         /* unix domain socket */
205                         sprintf(buf, "%s/citadel.socket", ctdlport);
206                         WC->chat_sock = uds_connectsock(buf);
207                 }
208                 else {
209                         /* tcp socket */
210                         WC->chat_sock = tcp_connectsock(ctdlhost, ctdlport);
211                 }
212
213                 if (WC->chat_sock < 0) {
214                         return(errno);
215                 }
216
217                 /* Temporarily swap the serv and chat sockets during chat talk */
218                 i = WC->serv_sock;
219                 WC->serv_sock = WC->chat_sock;
220                 WC->chat_sock = i;
221
222                 serv_getln(buf, sizeof buf);
223                 if (buf[0] == '2') {
224                         serv_printf("USER %s", WC->wc_username);
225                         serv_getln(buf, sizeof buf);
226                         if (buf[0] == '3') {
227                                 serv_printf("PASS %s", WC->wc_password);
228                                 serv_getln(buf, sizeof buf);
229                                 if (buf[0] == '2') {
230                                         serv_printf("GOTO %s", WC->wc_roomname);
231                                         serv_getln(buf, sizeof buf);
232                                         if (buf[0] == '2') {
233                                                 serv_puts("CHAT");
234                                                 serv_getln(buf, sizeof buf);
235                                                 if (buf[0] == '8') {
236                                                         good_chatmode = 1;
237                                                 }
238                                         }
239                                 }
240                         }
241                 }
242
243                 /* Unswap the sockets. */
244                 i = WC->serv_sock;
245                 WC->serv_sock = WC->chat_sock;
246                 WC->chat_sock = i;
247
248                 if (!good_chatmode) close(WC->serv_sock);
249
250         }
251         return(0);
252 }
253
254
255
256 /*
257  * Receiving side of the chat window.  This is implemented in a
258  * tiny hidden IFRAME that just does JavaScript writes to
259  * other frames whenever it refreshes and finds new data.
260  */
261 void chat_recv(void) {
262         int i;
263         struct pollfd pf;
264         int got_data = 0;
265         int end_chat_now = 0;
266         char buf[SIZ];
267         char cl_user[SIZ];
268         char cl_text[SIZ];
269         char *output_data = NULL;
270
271         output_headers(0, 0, 0, 0, 0, 0);
272
273         wprintf("Content-type: text/html; charset=utf-8\n");
274         wprintf("\n");
275         wprintf("<html>\n"
276                 "<head>\n"
277                 "<meta http-equiv=\"refresh\" content=\"3\" />\n"
278                 "</head>\n"
279
280                 "<body bgcolor=\"#FFFFFF\">\n"
281         );
282
283         if (setup_chat_socket() != 0) {
284                 wprintf(_("An error occurred while setting up the chat socket."));
285                 wprintf("</BODY></HTML>\n");
286                 wDumpContent(0);
287                 return;
288         }
289
290         /*
291          * See if there is any chat data waiting.
292          */
293         output_data = strdup("");
294         do {
295                 got_data = 0;
296                 pf.fd = WC->chat_sock;
297                 pf.events = POLLIN;
298                 pf.revents = 0;
299                 if (poll(&pf, 1, 1) > 0) if (pf.revents & POLLIN) {
300                         ++got_data;
301
302                         /* Temporarily swap the serv and chat sockets during chat talk */
303                         i = WC->serv_sock;
304                         WC->serv_sock = WC->chat_sock;
305                         WC->chat_sock = i;
306         
307                         serv_getln(buf, sizeof buf);
308
309                         if (!strcmp(buf, "000")) {
310                                 strcpy(buf, ":|");
311                                 strcat(buf, _("Now exiting chat mode."));
312                                 end_chat_now = 1;
313                         }
314                         
315                         /* Unswap the sockets. */
316                         i = WC->serv_sock;
317                         WC->serv_sock = WC->chat_sock;
318                         WC->chat_sock = i;
319
320                         /* Append our output data */
321                         output_data = realloc(output_data, strlen(output_data) + strlen(buf) + 4);
322                         strcat(output_data, buf);
323                         strcat(output_data, "\n");
324                 }
325
326         } while ( (got_data) && (!end_chat_now) );
327
328         if (end_chat_now) {
329                 close(WC->chat_sock);
330                 WC->chat_sock = (-1);
331                 wprintf("<img src=\"static/blank.gif\" onLoad=\"parent.window.close();\">\n");
332         }
333
334         if (strlen(output_data) > 0) {
335
336                 if (output_data[strlen(output_data)-1] == '\n') {
337                         output_data[strlen(output_data)-1] = 0;
338                 }
339
340                 /* Output our fun to the other frame. */
341                 wprintf("<img src=\"static/blank.gif\" WIDTH=1 HEIGHT=1\n"
342                         "onLoad=\" \n"
343                 );
344
345                 for (i=0; i<num_tokens(output_data, '\n'); ++i) {
346                         extract_token(buf, output_data, i, '\n', sizeof buf);
347                         extract_token(cl_user, buf, 0, '|', sizeof cl_user);
348                         extract_token(cl_text, buf, 1, '|', sizeof cl_text);
349
350                         if (strcasecmp(cl_text, "NOOP")) {
351
352                                 wprintf("parent.chat_transcript.document.write('");
353         
354                                 if (strcasecmp(cl_user, WC->last_chat_user)) {
355                                         wprintf("<TABLE border=0 WIDTH=100%% "
356                                                 "CELLSPACING=1 CELLPADDING=0 "
357                                                 "BGCOLOR=&quot;#FFFFFF&quot;>"
358                                                 "<TR><TD></TR></TD></TABLE>"
359                                         );
360         
361                                 }
362
363                                 wprintf("<TABLE border=0 WIDTH=100%% "
364                                         "CELLSPACING=0 CELLPADDING=0 "
365                                         "BGCOLOR=&quot;#EEEEEE&quot;>");
366         
367                                 wprintf("<TR><TD>");
368         
369                                 if (!strcasecmp(cl_user, ":")) {
370                                         wprintf("<I>");
371                                 }
372
373                                 if (strcasecmp(cl_user, WC->last_chat_user)) {
374                                         wprintf("<B>");
375         
376                                         if (!strcasecmp(cl_user, WC->wc_fullname)) {
377                                                 wprintf("<FONT COLOR=&quot;#FF0000&quot;>");
378                                         }
379                                         else {
380                                                 wprintf("<FONT COLOR=&quot;#0000FF&quot;>");
381                                         }
382                                         jsescputs(cl_user);
383         
384                                         wprintf("</FONT>: </B>");
385                                 }
386                                 else {
387                                         wprintf("&nbsp;&nbsp;&nbsp;");
388                                 }
389                                 jsescputs(cl_text);
390                                 if (!strcasecmp(cl_user, ":")) {
391                                         wprintf("</I>");
392                                 }
393
394                                 wprintf("</TD></TR></TABLE>");
395                                 wprintf("'); \n");
396
397                                 strcpy(WC->last_chat_user, cl_user);
398                         }
399                 }
400
401                 wprintf("parent.chat_transcript.scrollTo(999999,999999);\">\n");
402         }
403
404         free(output_data);
405
406         wprintf("</BODY></HTML>\n");
407         wDumpContent(0);
408 }
409
410
411 /*
412  * sending side of the chat window
413  */
414 void chat_send(void) {
415         int i;
416         char send_this[SIZ];
417         char buf[SIZ];
418
419         output_headers(0, 0, 0, 0, 0, 0);
420         wprintf("Content-type: text/html; charset=utf-8\n");
421         wprintf("\n");
422         wprintf("<HTML>"
423                 "<BODY onLoad=\"document.chatsendform.send_this.focus();\" >"
424         );
425
426         if (bstr("send_this") != NULL) {
427                 strcpy(send_this, bstr("send_this"));
428         }
429         else {
430                 strcpy(send_this, "");
431         }
432
433         if (strlen(bstr("help_button")) > 0) {
434                 strcpy(send_this, "/help");
435         }
436
437         if (strlen(bstr("list_button")) > 0) {
438                 strcpy(send_this, "/who");
439         }
440
441         if (strlen(bstr("exit_button")) > 0) {
442                 strcpy(send_this, "/quit");
443         }
444
445         if (setup_chat_socket() != 0) {
446                 wprintf(_("An error occurred while setting up the chat socket."));
447                 wprintf("</BODY></HTML>\n");
448                 wDumpContent(0);
449                 return;
450         }
451
452         /* Temporarily swap the serv and chat sockets during chat talk */
453         i = WC->serv_sock;
454         WC->serv_sock = WC->chat_sock;
455         WC->chat_sock = i;
456
457         while (strlen(send_this) > 0) {
458                 if (strlen(send_this) < 67) {
459                         serv_puts(send_this);
460                         strcpy(send_this, "");
461                 }
462                 else {
463                         for (i=55; i<67; ++i) {
464                                 if (send_this[i] == ' ') break;
465                         }
466                         strncpy(buf, send_this, i);
467                         buf[i] = 0;
468                         strcpy(send_this, &send_this[i]);
469                         serv_puts(buf);
470                 }
471         }
472
473         /* Unswap the sockets. */
474         i = WC->serv_sock;
475         WC->serv_sock = WC->chat_sock;
476         WC->chat_sock = i;
477
478         wprintf("<FORM METHOD=\"POST\" action=\"chat_send\" NAME=\"chatsendform\">\n");
479         wprintf("<INPUT TYPE=\"text\" SIZE=\"80\" MAXLENGTH=\"%d\" "
480                 "NAME=\"send_this\">\n", SIZ-10);
481         wprintf("<br />");
482         wprintf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">\n", _("Send"));
483         wprintf("<INPUT TYPE=\"submit\" NAME=\"help_button\" VALUE=\"%s\">\n", _("Help"));
484         wprintf("<INPUT TYPE=\"submit\" NAME=\"list_button\" VALUE=\"%s\">\n", _("List users"));
485         wprintf("<INPUT TYPE=\"submit\" NAME=\"exit_button\" VALUE=\"%s\">\n", _("Exit"));
486         wprintf("</FORM>\n");
487
488         wprintf("</BODY></HTML>\n");
489         wDumpContent(0);
490 }
491