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