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