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