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