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