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