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